-
Notifications
You must be signed in to change notification settings - Fork 180
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
[EVM] Add EVM type ID #5091
Merged
Merged
[EVM] Add EVM type ID #5091
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7d896c6
add evm event location
c022967
add evm location type to event
52239fb
add parser for event type
6f25040
Merge branch 'master' into gregor/evm/event-encoding
11005c3
remove proofs and reorder
29d5c71
add test for transaction events
51a4b59
import lint
1a02a24
check for correct events emitted
6aa6434
fix typo
b633a9c
fix unneeded test call
2863cea
Merge branch 'master' into gregor/evm/event-encoding
1ab6284
update master
f062c46
update master
010094d
Merge branch 'master' into gregor/evm/event-encoding
sideninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,23 @@ 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/stdlib" | ||
"github.com/onflow/cadence/runtime/common" | ||
|
||
"github.com/onflow/flow-go/model/flow" | ||
) | ||
|
||
const ( | ||
EventTypeBlockExecuted flow.EventType = "evm.BlockExecuted" | ||
EventTypeTransactionExecuted flow.EventType = "evm.TransactionExecuted" | ||
EventTypeBlockExecuted flow.EventType = "BlockExecuted" | ||
EventTypeTransactionExecuted flow.EventType = "TransactionExecuted" | ||
evmLocationPrefix = "evm" | ||
locationDivider = "." | ||
) | ||
|
||
type EventPayload interface { | ||
|
@@ -26,6 +31,72 @@ 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", 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 evmLocationPrefix | ||
} | ||
|
||
func (l EVMLocation) Description() string { | ||
return evmLocationPrefix | ||
} | ||
|
||
func (l EVMLocation) ID() string { | ||
return evmLocationPrefix | ||
} | ||
|
||
func (l EVMLocation) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(&struct { | ||
Type string | ||
}{ | ||
Type: "EVMLocation", | ||
}) | ||
} | ||
|
||
func init() { | ||
common.RegisterTypeIDDecoder( | ||
evmLocationPrefix, | ||
func(_ common.MemoryGauge, typeID string) (common.Location, string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need a test for this part specifically? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is covered in the test I wrote, however edge cases with errors are not. |
||
if typeID == "" { | ||
return nil, "", fmt.Errorf("invalid EVM type location ID: missing type prefix") | ||
} | ||
|
||
parts := strings.SplitN(typeID, ".", 2) | ||
prefix := parts[0] | ||
if prefix != 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 | ||
|
@@ -34,24 +105,6 @@ type TransactionExecutedPayload struct { | |
Result *Result | ||
} | ||
|
||
var transactionExecutedEventCadenceType = &cadence.EventType{ | ||
Location: stdlib.FlowLocation{}, | ||
QualifiedIdentifier: string(EventTypeTransactionExecuted), | ||
Fields: []cadence.Field{ | ||
cadence.NewField("blockHeight", cadence.UInt64Type{}), | ||
cadence.NewField("transactionHash", cadence.StringType{}), | ||
cadence.NewField("transaction", cadence.StringType{}), | ||
cadence.NewField("failed", cadence.BoolType{}), | ||
cadence.NewField("transactionType", cadence.UInt8Type{}), | ||
cadence.NewField("gasConsumed", cadence.UInt64Type{}), | ||
cadence.NewField("deployedContractAddress", cadence.StringType{}), | ||
cadence.NewField("returnedValue", cadence.StringType{}), | ||
cadence.NewField("logs", cadence.StringType{}), | ||
}, | ||
} | ||
|
||
// todo add decoder for events from cadence to evm payload | ||
|
||
func (p *TransactionExecutedPayload) CadenceEvent() (cadence.Event, error) { | ||
var encodedLogs []byte | ||
var err error | ||
|
@@ -62,21 +115,35 @@ func (p *TransactionExecutedPayload) CadenceEvent() (cadence.Event, error) { | |
} | ||
} | ||
|
||
fields := []cadence.Value{ | ||
cadence.NewUInt64(p.BlockHeight), | ||
cadence.String(p.TxHash.String()), | ||
cadence.String(hex.EncodeToString(p.TxEncoded)), | ||
cadence.NewBool(p.Result.Failed), | ||
cadence.NewUInt8(p.Result.TxType), | ||
cadence.NewUInt64(p.Result.GasConsumed), | ||
cadence.String(hex.EncodeToString(p.Result.DeployedContractAddress.Bytes())), | ||
cadence.String(hex.EncodeToString(p.Result.ReturnedValue)), | ||
cadence.String(hex.EncodeToString(encodedLogs)), | ||
} | ||
|
||
return cadence. | ||
NewEvent(fields). | ||
WithType(transactionExecutedEventCadenceType), nil | ||
return cadence.Event{ | ||
EventType: cadence.NewEventType( | ||
EVMLocation{}, | ||
string(EventTypeTransactionExecuted), | ||
[]cadence.Field{ | ||
cadence.NewField("blockHeight", cadence.UInt64Type{}), | ||
cadence.NewField("transactionHash", cadence.StringType{}), | ||
cadence.NewField("transaction", cadence.StringType{}), | ||
cadence.NewField("failed", cadence.BoolType{}), | ||
cadence.NewField("transactionType", cadence.UInt8Type{}), | ||
cadence.NewField("gasConsumed", cadence.UInt64Type{}), | ||
cadence.NewField("deployedContractAddress", cadence.StringType{}), | ||
cadence.NewField("returnedValue", cadence.StringType{}), | ||
cadence.NewField("logs", cadence.StringType{}), | ||
}, | ||
nil, | ||
), | ||
Fields: []cadence.Value{ | ||
cadence.NewUInt64(p.BlockHeight), | ||
cadence.String(p.TxHash.String()), | ||
cadence.String(hex.EncodeToString(p.TxEncoded)), | ||
cadence.NewBool(p.Result.Failed), | ||
cadence.NewUInt8(p.Result.TxType), | ||
cadence.NewUInt64(p.Result.GasConsumed), | ||
cadence.String(hex.EncodeToString(p.Result.DeployedContractAddress.Bytes())), | ||
cadence.String(hex.EncodeToString(p.Result.ReturnedValue)), | ||
cadence.String(hex.EncodeToString(encodedLogs)), | ||
}, | ||
}, nil | ||
} | ||
|
||
func NewTransactionExecutedEvent( | ||
|
@@ -97,7 +164,7 @@ func NewTransactionExecutedEvent( | |
} | ||
|
||
var blockExecutedEventCadenceType = &cadence.EventType{ | ||
Location: stdlib.FlowLocation{}, // todo create evm custom location | ||
Location: EVMLocation{}, | ||
QualifiedIdentifier: string(EventTypeBlockExecuted), | ||
Fields: []cadence.Field{ | ||
cadence.NewField("height", cadence.UInt64Type{}), | ||
|
@@ -124,8 +191,8 @@ func (p *BlockExecutedEventPayload) CadenceEvent() (cadence.Event, error) { | |
fields := []cadence.Value{ | ||
cadence.NewUInt64(p.Block.Height), | ||
cadence.NewUInt64(p.Block.TotalSupply), | ||
cadence.String(p.Block.ReceiptRoot.String()), | ||
cadence.String(p.Block.ParentBlockHash.String()), | ||
cadence.String(p.Block.ReceiptRoot.String()), | ||
cadence.NewArray(hashes).WithType(cadence.NewVariableSizedArrayType(cadence.StringType{})), | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like using init but not sure if there's a better way of doing this without exporting types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like these are usually in init... Not sure if there is another way.