Skip to content

Commit

Permalink
Merge pull request #6223 from onflow/bastian/improve-evm-event-decoding
Browse files Browse the repository at this point in the history
[Flow EVM] Simplify Go types in event types
  • Loading branch information
ramtinms authored Jul 17, 2024
2 parents f1d90e5 + e1a2288 commit 9ea6fae
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 77 deletions.
16 changes: 3 additions & 13 deletions fvm/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,8 @@ func TestEVMRun(t *testing.T) {
txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address)
require.NoError(t, err)

txPayload, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Payload)
require.NoError(t, err)

require.NotEmpty(t, txEventPayload.Hash)
require.Equal(t, innerTxBytes, txPayload)
require.Equal(t, innerTxBytes, txEventPayload.Payload)
require.Equal(t, uint16(types.ErrCodeNoError), txEventPayload.ErrorCode)
require.Equal(t, uint16(0), txEventPayload.Index)
require.Equal(t, blockEventPayload.Height, txEventPayload.BlockHeight)
Expand Down Expand Up @@ -366,11 +363,8 @@ func TestEVMRun(t *testing.T) {

require.NotEmpty(t, txEventPayload.Hash)

encodedLogs, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Logs)
require.NoError(t, err)

var logs []*gethTypes.Log
err = rlp.DecodeBytes(encodedLogs, &logs)
err = rlp.DecodeBytes(txEventPayload.Logs, &logs)
require.NoError(t, err)
require.Len(t, logs, 1)
log := logs[0]
Expand Down Expand Up @@ -477,11 +471,8 @@ func TestEVMBatchRun(t *testing.T) {
event, err := types.DecodeTransactionEventPayload(cadenceEvent)
require.NoError(t, err)

encodedLogs, err := types.CadenceUInt8ArrayValueToBytes(event.Logs)
require.NoError(t, err)

var logs []*gethTypes.Log
err = rlp.DecodeBytes(encodedLogs, &logs)
err = rlp.DecodeBytes(event.Logs, &logs)
require.NoError(t, err)

require.Len(t, logs, 1)
Expand Down Expand Up @@ -962,7 +953,6 @@ func TestEVMAddressDeposit(t *testing.T) {
// tx executed event
txEvent := output.Events[2]
txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address)
require.NoError(t, err)

// deposit event
depositEvent := output.Events[3]
Expand Down
36 changes: 11 additions & 25 deletions fvm/evm/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
gethCommon "github.com/onflow/go-ethereum/common"
gethCore "github.com/onflow/go-ethereum/core"
Expand Down Expand Up @@ -93,11 +92,8 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {
txEventPayload := testutils.TxEventToPayload(t, events[0], rootAddr)

// check logs
encodedLogs, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Logs)
require.NoError(t, err)

var logs []*gethTypes.Log
err = rlp.DecodeBytes(encodedLogs, &logs)
err = rlp.DecodeBytes(txEventPayload.Logs, &logs)
require.NoError(t, err)
for i, l := range result.Logs {
assert.Equal(t, l, logs[i])
Expand All @@ -111,12 +107,13 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {
require.Len(t, events, 2)
blockEventPayload := testutils.BlockEventToPayload(t, events[1], rootAddr)
// make sure block transaction list references the above transaction id

require.Len(t, blockEventPayload.TransactionHashes, 1)
eventTxID := blockEventPayload.TransactionHashes[0] // only one hash in block
// make sure the transaction id included in the block transaction list is the same as tx sumbmitted
assert.Equal(
t,
types.HashToCadenceArrayValue(evmTx.Hash()),
evmTx.Hash(),
eventTxID,
)
})
Expand Down Expand Up @@ -350,18 +347,14 @@ func TestHandler_COA(t *testing.T) {

// deploy COA transaction event
txEventPayload := testutils.TxEventToPayload(t, events[0], rootAddr)
txContent, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Payload)
require.NoError(t, err)
tx, err := types.DirectCallFromEncoded(txContent)
tx, err := types.DirectCallFromEncoded(txEventPayload.Payload)
require.NoError(t, err)
txHashes = append(txHashes, tx.Hash())
totalGasUsed += txEventPayload.GasConsumed

// deposit transaction event
txEventPayload = testutils.TxEventToPayload(t, events[1], rootAddr)
txContent, err = types.CadenceUInt8ArrayValueToBytes(txEventPayload.Payload)
require.NoError(t, err)
tx, err = types.DirectCallFromEncoded(txContent)
tx, err = types.DirectCallFromEncoded(txEventPayload.Payload)
require.NoError(t, err)
require.Equal(t, foa.Address(), tx.To)
require.Equal(t, types.BalanceToBigInt(balance), tx.Value)
Expand All @@ -370,9 +363,7 @@ func TestHandler_COA(t *testing.T) {

// withdraw transaction event
txEventPayload = testutils.TxEventToPayload(t, events[2], rootAddr)
txContent, err = types.CadenceUInt8ArrayValueToBytes(txEventPayload.Payload)
require.NoError(t, err)
tx, err = types.DirectCallFromEncoded(txContent)
tx, err = types.DirectCallFromEncoded(txEventPayload.Payload)
require.NoError(t, err)
require.Equal(t, foa.Address(), tx.From)
require.Equal(t, types.BalanceToBigInt(balance), tx.Value)
Expand All @@ -386,7 +377,7 @@ func TestHandler_COA(t *testing.T) {
blockEventPayload := testutils.BlockEventToPayload(t, events[3], rootAddr)
for i, txHash := range txHashes {
require.Equal(t,
types.HashToCadenceArrayValue(txHash),
txHash,
blockEventPayload.TransactionHashes[i],
)
}
Expand Down Expand Up @@ -646,14 +637,11 @@ func TestHandler_COA(t *testing.T) {
events := backend.Events()
require.Len(t, events, 3)
// last transaction executed event

event := events[2]
txEventPayload := testutils.TxEventToPayload(t, event, rootAddr)
values := txEventPayload.PrecompiledCalls.Values
aggregated := make([]byte, len(values))
for i, v := range values {
aggregated[i] = uint8(v.(cadence.UInt8))
}
apc, err := types.AggregatedPrecompileCallsFromEncoded(aggregated)

apc, err := types.AggregatedPrecompileCallsFromEncoded(txEventPayload.PrecompiledCalls)
require.NoError(t, err)

require.False(t, apc.IsEmpty())
Expand Down Expand Up @@ -931,11 +919,9 @@ func TestHandler_TransactionRun(t *testing.T) {
continue // don't check last block event
}
txEventPayload := testutils.TxEventToPayload(t, event, rootAddr)
encodedLogs, err := types.CadenceUInt8ArrayValueToBytes(txEventPayload.Logs)
require.NoError(t, err)

var logs []*gethTypes.Log
err = rlp.DecodeBytes(encodedLogs, &logs)
err := rlp.DecodeBytes(txEventPayload.Logs, &logs)
require.NoError(t, err)

for k, l := range runResults[i].Logs {
Expand Down
42 changes: 21 additions & 21 deletions fvm/evm/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,14 @@ func (p *blockEvent) ToCadence(location common.Location) (cadence.Event, error)
}

type BlockEventPayload struct {
Height uint64 `cadence:"height"`
Hash cadence.Array `cadence:"hash"`
Timestamp uint64 `cadence:"timestamp"`
TotalSupply cadence.Int `cadence:"totalSupply"`
TotalGasUsed uint64 `cadence:"totalGasUsed"`
ParentBlockHash cadence.Array `cadence:"parentHash"`
ReceiptRoot cadence.Array `cadence:"receiptRoot"`
TransactionHashes []cadence.Array `cadence:"transactionHashes"`
Height uint64 `cadence:"height"`
Hash gethCommon.Hash `cadence:"hash"`
Timestamp uint64 `cadence:"timestamp"`
TotalSupply cadence.Int `cadence:"totalSupply"`
TotalGasUsed uint64 `cadence:"totalGasUsed"`
ParentBlockHash gethCommon.Hash `cadence:"parentHash"`
ReceiptRoot gethCommon.Hash `cadence:"receiptRoot"`
TransactionHashes []gethCommon.Hash `cadence:"transactionHashes"`
}

// DecodeBlockEventPayload decodes Cadence event into block event payload.
Expand All @@ -197,18 +197,18 @@ func DecodeBlockEventPayload(event cadence.Event) (*BlockEventPayload, error) {
}

type TransactionEventPayload struct {
Hash cadence.Array `cadence:"hash"`
Index uint16 `cadence:"index"`
TransactionType uint8 `cadence:"type"`
Payload cadence.Array `cadence:"payload"`
ErrorCode uint16 `cadence:"errorCode"`
GasConsumed uint64 `cadence:"gasConsumed"`
ContractAddress string `cadence:"contractAddress"`
Logs cadence.Array `cadence:"logs"`
BlockHeight uint64 `cadence:"blockHeight"`
ErrorMessage string `cadence:"errorMessage"`
ReturnedData cadence.Array `cadence:"returnedData"`
PrecompiledCalls cadence.Array `cadence:"precompiledCalls"`
Hash gethCommon.Hash `cadence:"hash"`
Index uint16 `cadence:"index"`
TransactionType uint8 `cadence:"type"`
Payload []byte `cadence:"payload"`
ErrorCode uint16 `cadence:"errorCode"`
GasConsumed uint64 `cadence:"gasConsumed"`
ContractAddress string `cadence:"contractAddress"`
Logs []byte `cadence:"logs"`
BlockHeight uint64 `cadence:"blockHeight"`
ErrorMessage string `cadence:"errorMessage"`
ReturnedData []byte `cadence:"returnedData"`
PrecompiledCalls []byte `cadence:"precompiledCalls"`
}

// DecodeTransactionEventPayload decodes Cadence event into transaction event payload.
Expand All @@ -218,7 +218,7 @@ func DecodeTransactionEventPayload(event cadence.Event) (*TransactionEventPayloa
return &tx, err
}

// FLOWTokensEventPayload captures payloads for a FlowTokenDeposited event
// FLOWTokensDepositedEventPayload captures payloads for a FlowTokenDeposited event
type FLOWTokensDepositedEventPayload struct {
Address string `cadence:"address"`
Amount cadence.UFix64 `cadence:"amount"`
Expand Down
30 changes: 12 additions & 18 deletions fvm/evm/types/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"math/big"
"testing"

"github.com/onflow/cadence"

"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"

Expand Down Expand Up @@ -57,21 +55,17 @@ func TestEVMBlockExecutedEventCCFEncodingDecoding(t *testing.T) {

blockHash, err := block.Hash()
require.NoError(t, err)
assert.Equal(t, bep.Hash, types.HashToCadenceArrayValue(blockHash))
assert.Equal(t, bep.Hash, blockHash)

assert.Equal(t, bep.TotalSupply.Value, block.TotalSupply)
assert.Equal(t, bep.Timestamp, block.Timestamp)
assert.Equal(t, bep.TotalGasUsed, block.TotalGasUsed)
assert.Equal(t, bep.ParentBlockHash, types.HashToCadenceArrayValue(block.ParentBlockHash))
assert.Equal(t, bep.ReceiptRoot, types.HashToCadenceArrayValue(block.ReceiptRoot))
assert.Equal(t, bep.ParentBlockHash, block.ParentBlockHash)
assert.Equal(t, bep.ReceiptRoot, block.ReceiptRoot)

hashes := make([]cadence.Array, len(block.TransactionHashes))
for i, h := range block.TransactionHashes {
hashes[i] = types.HashToCadenceArrayValue(h)
}
assert.Equal(t,
bep.TransactionHashes,
hashes,
block.TransactionHashes,
)

v, err := ccf.Encode(ev)
Expand Down Expand Up @@ -138,13 +132,13 @@ func TestEVMTransactionExecutedEventCCFEncodingDecoding(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, tep.BlockHeight, blockHeight)
assert.Equal(t, tep.Hash, types.HashToCadenceArrayValue(txHash))
assert.Equal(t, tep.Payload, types.BytesToCadenceUInt8ArrayValue(txBytes))
assert.Equal(t, tep.Hash, txHash)
assert.Equal(t, tep.Payload, txBytes)
assert.Equal(t, types.ErrorCode(tep.ErrorCode), types.ExecutionErrCodeOutOfGas)
assert.Equal(t, tep.TransactionType, txResult.TxType)
assert.Equal(t, tep.GasConsumed, txResult.GasConsumed)
assert.Equal(t, tep.ErrorMessage, txResult.VMError.Error())
assert.Equal(t, tep.ReturnedData, types.BytesToCadenceUInt8ArrayValue(txResult.ReturnedData))
assert.Equal(t, tep.ReturnedData, txResult.ReturnedData)
assert.Equal(
t,
tep.ContractAddress,
Expand All @@ -153,7 +147,7 @@ func TestEVMTransactionExecutedEventCCFEncodingDecoding(t *testing.T) {

encodedLogs, err := rlp.EncodeToBytes(txResult.Logs)
require.NoError(t, err)
assert.Equal(t, tep.Logs, types.BytesToCadenceUInt8ArrayValue(encodedLogs))
assert.Equal(t, tep.Logs, encodedLogs)

v, err := ccf.Encode(ev)
require.NoError(t, err)
Expand All @@ -179,13 +173,13 @@ func TestEVMTransactionExecutedEventCCFEncodingDecoding(t *testing.T) {
require.NoError(t, err)

assert.Equal(t, tep.BlockHeight, blockHeight)
assert.Equal(t, tep.Hash, types.HashToCadenceArrayValue(txHash))
assert.Equal(t, tep.Payload, types.BytesToCadenceUInt8ArrayValue(txBytes))
assert.Equal(t, tep.Hash, txHash)
assert.Equal(t, tep.Payload, txBytes)
assert.Equal(t, types.ErrCodeNoError, types.ErrorCode(tep.ErrorCode))
assert.Equal(t, tep.TransactionType, txResult.TxType)
assert.Equal(t, tep.GasConsumed, txResult.GasConsumed)
assert.Empty(t, tep.ErrorMessage)
assert.Equal(t, tep.ReturnedData, types.BytesToCadenceUInt8ArrayValue(txResult.ReturnedData))
assert.Equal(t, tep.ReturnedData, txResult.ReturnedData)
assert.NotNil(t, txResult.DeployedContractAddress)
assert.Equal(
t,
Expand All @@ -195,7 +189,7 @@ func TestEVMTransactionExecutedEventCCFEncodingDecoding(t *testing.T) {

encodedLogs, err := rlp.EncodeToBytes(txResult.Logs)
require.NoError(t, err)
assert.Equal(t, tep.Logs, types.BytesToCadenceUInt8ArrayValue(encodedLogs))
assert.Equal(t, tep.Logs, encodedLogs)

v, err := ccf.Encode(ev)
require.NoError(t, err)
Expand Down

0 comments on commit 9ea6fae

Please sign in to comment.