github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/evm/types/events_test.go (about) 1 package types_test 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "math/big" 7 "testing" 8 9 "github.com/onflow/flow-go/fvm/systemcontracts" 10 "github.com/onflow/flow-go/model/flow" 11 12 "github.com/onflow/go-ethereum/core/vm" 13 14 "github.com/onflow/cadence/encoding/ccf" 15 cdcCommon "github.com/onflow/cadence/runtime/common" 16 gethCommon "github.com/onflow/go-ethereum/common" 17 gethTypes "github.com/onflow/go-ethereum/core/types" 18 "github.com/onflow/go-ethereum/rlp" 19 "github.com/stretchr/testify/assert" 20 "github.com/stretchr/testify/require" 21 22 "github.com/onflow/flow-go/fvm/evm/testutils" 23 "github.com/onflow/flow-go/fvm/evm/types" 24 ) 25 26 var evmLocation = cdcCommon.NewAddressLocation( 27 nil, 28 cdcCommon.Address(systemcontracts.SystemContractsForChain(flow.Emulator).EVMContract.Address), 29 "EVM", 30 ) 31 32 func TestEVMBlockExecutedEventCCFEncodingDecoding(t *testing.T) { 33 t.Parallel() 34 35 block := &types.Block{ 36 Height: 2, 37 Timestamp: 100, 38 TotalSupply: big.NewInt(1500), 39 ParentBlockHash: gethCommon.HexToHash("0x2813452cff514c3054ac9f40cd7ce1b016cc78ab7f99f1c6d49708837f6e06d1"), 40 ReceiptRoot: gethCommon.Hash{}, 41 TotalGasUsed: 15, 42 TransactionHashes: []gethCommon.Hash{ 43 gethCommon.HexToHash("0x70b67ce6710355acf8d69b2ea013d34e212bc4824926c5d26f189c1ca9667246"), 44 }, 45 } 46 47 event := types.NewBlockEvent(block) 48 ev, err := event.Payload.ToCadence(evmLocation) 49 require.NoError(t, err) 50 51 bep, err := types.DecodeBlockEventPayload(ev) 52 require.NoError(t, err) 53 54 assert.Equal(t, bep.Height, block.Height) 55 56 blockHash, err := block.Hash() 57 require.NoError(t, err) 58 assert.Equal(t, bep.Hash, blockHash.Hex()) 59 60 assert.Equal(t, bep.TotalSupply.Value, block.TotalSupply) 61 assert.Equal(t, bep.Timestamp, block.Timestamp) 62 assert.Equal(t, bep.TotalGasUsed, block.TotalGasUsed) 63 assert.Equal(t, bep.ParentBlockHash, block.ParentBlockHash.Hex()) 64 assert.Equal(t, bep.ReceiptRoot, block.ReceiptRoot.Hex()) 65 66 hashes := make([]gethCommon.Hash, len(bep.TransactionHashes)) 67 for i, h := range bep.TransactionHashes { 68 hashes[i] = gethCommon.HexToHash(string(h)) 69 } 70 assert.Equal(t, hashes, block.TransactionHashes) 71 72 v, err := ccf.Encode(ev) 73 require.NoError(t, err) 74 assert.Equal(t, ccf.HasMsgPrefix(v), true) 75 76 evt, err := ccf.Decode(nil, v) 77 require.NoError(t, err) 78 79 assert.Equal(t, evt.Type().ID(), fmt.Sprintf( 80 "A.%s.EVM.BlockExecuted", 81 systemcontracts.SystemContractsForChain(flow.Emulator).EVMContract.Address, 82 )) 83 } 84 85 func TestEVMTransactionExecutedEventCCFEncodingDecoding(t *testing.T) { 86 t.Parallel() 87 88 txEncoded := "fff83b81ff0194000000000000000000000000000000000000000094000000000000000000000000000000000000000180895150ae84a8cdf00000825208" 89 txBytes, err := hex.DecodeString(txEncoded) 90 require.NoError(t, err) 91 txHash := testutils.RandomCommonHash(t) 92 blockHash := testutils.RandomCommonHash(t) 93 data := "000000000000000000000000000000000000000000000000000000000000002a" 94 dataBytes, err := hex.DecodeString(data) 95 require.NoError(t, err) 96 blockHeight := uint64(2) 97 deployedAddress := types.NewAddress(gethCommon.HexToAddress("0x99466ed2e37b892a2ee3e9cd55a98b68f5735db2")) 98 log := &gethTypes.Log{ 99 Index: 1, 100 BlockNumber: blockHeight, 101 BlockHash: blockHash, 102 TxHash: txHash, 103 TxIndex: 3, 104 Address: gethCommon.HexToAddress("0x99466ed2e37b892a2ee3e9cd55a98b68f5735db2"), 105 Data: dataBytes, 106 Topics: []gethCommon.Hash{ 107 gethCommon.HexToHash("0x24abdb5865df5079dcc5ac590ff6f01d5c16edbc5fab4e195d9febd1114503da"), 108 }, 109 } 110 vmError := vm.ErrOutOfGas 111 txResult := &types.Result{ 112 VMError: vmError, 113 TxType: 255, 114 GasConsumed: 23200, 115 DeployedContractAddress: &deployedAddress, 116 ReturnedValue: dataBytes, 117 Logs: []*gethTypes.Log{log}, 118 TxHash: txHash, 119 } 120 121 t.Run("evm.TransactionExecuted with failed status", func(t *testing.T) { 122 event := types.NewTransactionEvent(txResult, txBytes, blockHeight, blockHash) 123 ev, err := event.Payload.ToCadence(evmLocation) 124 require.NoError(t, err) 125 126 tep, err := types.DecodeTransactionEventPayload(ev) 127 require.NoError(t, err) 128 129 assert.Equal(t, tep.BlockHeight, blockHeight) 130 assert.Equal(t, tep.BlockHash, blockHash.Hex()) 131 assert.Equal(t, tep.Hash, txHash.Hex()) 132 assert.Equal(t, tep.Payload, txEncoded) 133 assert.Equal(t, types.ErrorCode(tep.ErrorCode), types.ExecutionErrCodeOutOfGas) 134 assert.Equal(t, tep.TransactionType, txResult.TxType) 135 assert.Equal(t, tep.GasConsumed, txResult.GasConsumed) 136 assert.Equal( 137 t, 138 tep.ContractAddress, 139 txResult.DeployedContractAddress.ToCommon().Hex(), 140 ) 141 142 encodedLogs, err := rlp.EncodeToBytes(txResult.Logs) 143 require.NoError(t, err) 144 assert.Equal(t, tep.Logs, hex.EncodeToString(encodedLogs)) 145 146 v, err := ccf.Encode(ev) 147 require.NoError(t, err) 148 assert.Equal(t, ccf.HasMsgPrefix(v), true) 149 150 evt, err := ccf.Decode(nil, v) 151 require.NoError(t, err) 152 153 assert.Equal(t, evt.Type().ID(), fmt.Sprintf( 154 "A.%s.EVM.TransactionExecuted", 155 systemcontracts.SystemContractsForChain(flow.Emulator).EVMContract.Address, 156 )) 157 }) 158 159 t.Run("evm.TransactionExecuted with non-failed status", func(t *testing.T) { 160 txResult.VMError = nil 161 162 event := types.NewTransactionEvent(txResult, txBytes, blockHeight, blockHash) 163 ev, err := event.Payload.ToCadence(evmLocation) 164 require.NoError(t, err) 165 166 tep, err := types.DecodeTransactionEventPayload(ev) 167 require.NoError(t, err) 168 169 assert.Equal(t, tep.BlockHeight, blockHeight) 170 assert.Equal(t, tep.BlockHash, blockHash.Hex()) 171 assert.Equal(t, tep.Hash, txHash.Hex()) 172 assert.Equal(t, tep.Payload, txEncoded) 173 assert.Equal(t, types.ErrCodeNoError, types.ErrorCode(tep.ErrorCode)) 174 assert.Equal(t, tep.TransactionType, txResult.TxType) 175 assert.Equal(t, tep.GasConsumed, txResult.GasConsumed) 176 assert.NotNil(t, txResult.DeployedContractAddress) 177 assert.Equal( 178 t, 179 tep.ContractAddress, 180 txResult.DeployedContractAddress.ToCommon().Hex(), 181 ) 182 183 encodedLogs, err := rlp.EncodeToBytes(txResult.Logs) 184 require.NoError(t, err) 185 assert.Equal(t, tep.Logs, hex.EncodeToString(encodedLogs)) 186 187 v, err := ccf.Encode(ev) 188 require.NoError(t, err) 189 assert.Equal(t, ccf.HasMsgPrefix(v), true) 190 191 evt, err := ccf.Decode(nil, v) 192 require.NoError(t, err) 193 194 assert.Equal(t, evt.Type().ID(), fmt.Sprintf( 195 "A.%s.EVM.TransactionExecuted", 196 systemcontracts.SystemContractsForChain(flow.Emulator).EVMContract.Address, 197 )) 198 }) 199 }