Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use EVMLocation type from flow-go-sdk #5447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 3 additions & 73 deletions fvm/evm/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@ package types

import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"

gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"

flowSdk "github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go/model/flow"
)

const (
EventTypeBlockExecuted flow.EventType = "BlockExecuted"
EventTypeTransactionExecuted flow.EventType = "TransactionExecuted"
locationDivider = "."
)

type EventPayload interface {
Expand All @@ -30,72 +26,6 @@ type Event struct {
Payload EventPayload
}

var _ common.Location = EVMLocation{}

type EVMLocation struct{}

func (l EVMLocation) TypeID(memoryGauge common.MemoryGauge, qualifiedIdentifier string) common.TypeID {
id := fmt.Sprintf("%s%s%s", flow.EVMLocationPrefix, locationDivider, qualifiedIdentifier)
common.UseMemory(memoryGauge, common.NewRawStringMemoryUsage(len(id)))

return common.TypeID(id)
}

func (l EVMLocation) QualifiedIdentifier(typeID common.TypeID) string {
pieces := strings.SplitN(string(typeID), locationDivider, 2)

if len(pieces) < 2 {
return ""
}

return pieces[1]
}

func (l EVMLocation) String() string {
return flow.EVMLocationPrefix
}

func (l EVMLocation) Description() string {
return flow.EVMLocationPrefix
}

func (l EVMLocation) ID() string {
return flow.EVMLocationPrefix
}

func (l EVMLocation) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Type string
}{
Type: "EVMLocation",
})
}

func init() {
common.RegisterTypeIDDecoder(
flow.EVMLocationPrefix,
func(_ common.MemoryGauge, typeID string) (common.Location, string, error) {
if typeID == "" {
return nil, "", fmt.Errorf("invalid EVM type location ID: missing type prefix")
}

parts := strings.SplitN(typeID, ".", 2)
prefix := parts[0]
if prefix != flow.EVMLocationPrefix {
return EVMLocation{}, "", fmt.Errorf("invalid EVM type location ID: invalid prefix")
}

var qualifiedIdentifier string
pieceCount := len(parts)
if pieceCount > 1 {
qualifiedIdentifier = parts[1]
}

return EVMLocation{}, qualifiedIdentifier, nil
},
)
}

// we might break this event into two (tx included /tx executed) if size becomes an issue
type TransactionExecutedPayload struct {
BlockHeight uint64
Expand All @@ -117,7 +47,7 @@ func (p *TransactionExecutedPayload) CadenceEvent() (cadence.Event, error) {

return cadence.Event{
EventType: cadence.NewEventType(
EVMLocation{},
flowSdk.EVMLocation{},
string(EventTypeTransactionExecuted),
[]cadence.Field{
cadence.NewField("blockHeight", cadence.UInt64Type),
Expand Down Expand Up @@ -170,7 +100,7 @@ func NewTransactionExecutedEvent(
}

var blockExecutedEventCadenceType = &cadence.EventType{
Location: EVMLocation{},
Location: flowSdk.EVMLocation{},
QualifiedIdentifier: string(EventTypeBlockExecuted),
Fields: []cadence.Field{
cadence.NewField("height", cadence.UInt64Type),
Expand Down
5 changes: 3 additions & 2 deletions fvm/evm/types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

flowSdk "github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go/fvm/evm/testutils"
"github.com/onflow/flow-go/fvm/evm/types"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func TestEVMEventsCCFEncodingDecoding(t *testing.T) {
location, qualifiedIdentifier, err := cdcCommon.DecodeTypeID(nil, "evm.BlockExecuted")
require.NoError(t, err)

assert.Equal(t, types.EVMLocation{}, location)
assert.Equal(t, flowSdk.EVMLocation{}, location)
assert.Equal(t, "BlockExecuted", qualifiedIdentifier)
})

Expand Down Expand Up @@ -104,7 +105,7 @@ func TestEVMEventsCCFEncodingDecoding(t *testing.T) {
location, qualifiedIdentifier, err := cdcCommon.DecodeTypeID(nil, "evm.TransactionExecuted")
require.NoError(t, err)

assert.Equal(t, types.EVMLocation{}, location)
assert.Equal(t, flowSdk.EVMLocation{}, location)
assert.Equal(t, "TransactionExecuted", qualifiedIdentifier)
})
}
3 changes: 2 additions & 1 deletion fvm/fvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
mockery "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

flowSdk "github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go/engine/execution/testutil"
exeUtils "github.com/onflow/flow-go/engine/execution/utils"
"github.com/onflow/flow-go/fvm"
Expand Down Expand Up @@ -2993,7 +2994,7 @@ func TestEVM(t *testing.T) {
require.NoError(t, output.Err)
require.Len(t, output.Events, 7)

evmLocation := types.EVMLocation{}
evmLocation := flowSdk.EVMLocation{}

// convert events to type ids
eventTypeIDs := make([]common.TypeID, 0, len(output.Events))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ require (
github.com/onflow/flow v0.3.4
github.com/onflow/flow-core-contracts/lib/go/contracts v0.15.2-0.20240206003101-928bf99024d7
github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240206003101-928bf99024d7
github.com/onflow/flow-go-sdk v1.0.0-M5
github.com/onflow/flow-go-sdk v1.0.0-M6
github.com/onflow/flow/protobuf/go/flow v0.3.7
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pierrec/lz4 v2.6.1+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2500,8 +2500,8 @@ github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240206003101-
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0 h1:u6/YcUvO8jU0f3Evb/6agzXqeOo+VbL2a3mmj/5ifRs=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-M5 h1:R86DQQfXS3z8O6AShAaUNbkPOM7lOwcTk06cofdVEsk=
github.com/onflow/flow-go-sdk v1.0.0-M5/go.mod h1:aXSavLzoRlz5FiMjcI7p5QhihWScGctxydzf4dv/avo=
github.com/onflow/flow-go-sdk v1.0.0-M6 h1:FNITFgDhdo2H7mZY5bJPWD87k1MyJreBz87N8Xk9+xY=
github.com/onflow/flow-go-sdk v1.0.0-M6/go.mod h1:aXSavLzoRlz5FiMjcI7p5QhihWScGctxydzf4dv/avo=
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6 h1:/2vvjKkWG/3cKP3IpgiGNqXi0yskn4GmNTjmeCwMoz8=
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
Expand Down
2 changes: 1 addition & 1 deletion insecure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ require (
github.com/onflow/flow-core-contracts/lib/go/contracts v0.15.2-0.20240206003101-928bf99024d7 // indirect
github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240206003101-928bf99024d7 // indirect
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0 // indirect
github.com/onflow/flow-go-sdk v1.0.0-M5 // indirect
github.com/onflow/flow-go-sdk v1.0.0-M6 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6 // indirect
github.com/onflow/flow/protobuf/go/flow v0.3.7 // indirect
github.com/onflow/sdks v0.5.1-0.20230912225508-b35402f12bba // indirect
Expand Down
4 changes: 2 additions & 2 deletions insecure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2483,8 +2483,8 @@ github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240206003101-
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0 h1:u6/YcUvO8jU0f3Evb/6agzXqeOo+VbL2a3mmj/5ifRs=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-M5 h1:R86DQQfXS3z8O6AShAaUNbkPOM7lOwcTk06cofdVEsk=
github.com/onflow/flow-go-sdk v1.0.0-M5/go.mod h1:aXSavLzoRlz5FiMjcI7p5QhihWScGctxydzf4dv/avo=
github.com/onflow/flow-go-sdk v1.0.0-M6 h1:FNITFgDhdo2H7mZY5bJPWD87k1MyJreBz87N8Xk9+xY=
github.com/onflow/flow-go-sdk v1.0.0-M6/go.mod h1:aXSavLzoRlz5FiMjcI7p5QhihWScGctxydzf4dv/avo=
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6 h1:/2vvjKkWG/3cKP3IpgiGNqXi0yskn4GmNTjmeCwMoz8=
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
Expand Down
2 changes: 1 addition & 1 deletion integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/onflow/flow-core-contracts/lib/go/templates v0.15.2-0.20240206003101-928bf99024d7
github.com/onflow/flow-emulator v1.0.0-M2.0.20240213001509-046672c2367a
github.com/onflow/flow-go v0.33.2-0.20240213000303-9fb4c62e8291
github.com/onflow/flow-go-sdk v1.0.0-M5
github.com/onflow/flow-go-sdk v1.0.0-M6
github.com/onflow/flow-go/insecure v0.0.0-00010101000000-000000000000
github.com/onflow/flow/protobuf/go/flow v0.3.7
github.com/plus3it/gorecurcopy v0.0.1
Expand Down
4 changes: 2 additions & 2 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2561,8 +2561,8 @@ github.com/onflow/flow-emulator v1.0.0-M2.0.20240213001509-046672c2367a/go.mod h
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0 h1:u6/YcUvO8jU0f3Evb/6agzXqeOo+VbL2a3mmj/5ifRs=
github.com/onflow/flow-ft/lib/go/contracts v0.7.1-0.20240205224107-320aa3cf09e0/go.mod h1:PwsL8fC81cjnUnTfmyL/HOIyHnyaw/JA474Wfj2tl6A=
github.com/onflow/flow-go-sdk v1.0.0-M1/go.mod h1:TDW0MNuCs4SvqYRUzkbRnRmHQL1h4X8wURsCw9P9beo=
github.com/onflow/flow-go-sdk v1.0.0-M5 h1:R86DQQfXS3z8O6AShAaUNbkPOM7lOwcTk06cofdVEsk=
github.com/onflow/flow-go-sdk v1.0.0-M5/go.mod h1:aXSavLzoRlz5FiMjcI7p5QhihWScGctxydzf4dv/avo=
github.com/onflow/flow-go-sdk v1.0.0-M6 h1:FNITFgDhdo2H7mZY5bJPWD87k1MyJreBz87N8Xk9+xY=
github.com/onflow/flow-go-sdk v1.0.0-M6/go.mod h1:aXSavLzoRlz5FiMjcI7p5QhihWScGctxydzf4dv/avo=
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6 h1:/2vvjKkWG/3cKP3IpgiGNqXi0yskn4GmNTjmeCwMoz8=
github.com/onflow/flow-nft/lib/go/contracts v1.1.1-0.20240205233530-86ee8c352fa6/go.mod h1:2gpbza+uzs1k7x31hkpBPlggIRkI53Suo0n2AyA2HcE=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231121210617-52ee94b830c2/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
Expand Down
3 changes: 2 additions & 1 deletion model/events/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"strings"

flowSdk "github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go/model/flow"
)

Expand Down Expand Up @@ -32,7 +33,7 @@ func ParseEvent(eventType flow.EventType) (*ParsedEvent, error) {
parts := strings.Split(string(eventType), ".")

switch parts[0] {
case "flow", flow.EVMLocationPrefix:
case "flow", flowSdk.EVMLocationPrefix:
if len(parts) == 2 {
return &ParsedEvent{
Type: ProtocolEventType,
Expand Down
2 changes: 0 additions & 2 deletions model/flow/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const (
EventAccountUpdated EventType = "flow.AccountUpdated"
)

const EVMLocationPrefix = "evm"

type EventType string

type Event struct {
Expand Down
Loading