|
| 1 | +// (c) 2023, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package evm |
| 5 | + |
| 6 | +import ( |
| 7 | + "math/big" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/ava-labs/avalanchego/database/memdb" |
| 11 | + "github.com/ava-labs/subnet-evm/core/rawdb" |
| 12 | + "github.com/ava-labs/subnet-evm/core/types" |
| 13 | + "github.com/ava-labs/subnet-evm/params" |
| 14 | + "github.com/ava-labs/subnet-evm/precompile/precompileconfig" |
| 15 | + "github.com/ava-labs/subnet-evm/trie" |
| 16 | + "github.com/ethereum/go-ethereum/common" |
| 17 | + "github.com/golang/mock/gomock" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +func TestHandlePrecompileAccept(t *testing.T) { |
| 22 | + require := require.New(t) |
| 23 | + ctrl := gomock.NewController(t) |
| 24 | + defer ctrl.Finish() |
| 25 | + |
| 26 | + db := memdb.New() |
| 27 | + vm := &VM{ |
| 28 | + chaindb: Database{db}, |
| 29 | + chainConfig: params.TestChainConfig, |
| 30 | + } |
| 31 | + |
| 32 | + precompileAddr := common.Address{0x05} |
| 33 | + otherAddr := common.Address{0x06} |
| 34 | + |
| 35 | + // Prepare a receipt with 3 logs, two of which are from the precompile |
| 36 | + receipt := &types.Receipt{ |
| 37 | + Logs: []*types.Log{ |
| 38 | + { |
| 39 | + Address: precompileAddr, |
| 40 | + Topics: []common.Hash{{0x01}, {0x02}, {0x03}}, |
| 41 | + Data: []byte("log1"), |
| 42 | + }, |
| 43 | + { |
| 44 | + Address: otherAddr, |
| 45 | + Topics: []common.Hash{{0x01}, {0x02}, {0x04}}, |
| 46 | + Data: []byte("log2"), |
| 47 | + }, |
| 48 | + { |
| 49 | + Address: precompileAddr, |
| 50 | + Topics: []common.Hash{{0x01}, {0x02}, {0x05}}, |
| 51 | + Data: []byte("log3"), |
| 52 | + }, |
| 53 | + }, |
| 54 | + } |
| 55 | + ethBlock := types.NewBlock( |
| 56 | + &types.Header{Number: big.NewInt(1)}, |
| 57 | + []*types.Transaction{types.NewTx(&types.LegacyTx{})}, |
| 58 | + nil, |
| 59 | + []*types.Receipt{receipt}, |
| 60 | + trie.NewStackTrie(nil), |
| 61 | + ) |
| 62 | + // Write the block to the db |
| 63 | + rawdb.WriteBlock(db, ethBlock) |
| 64 | + rawdb.WriteReceipts(db, ethBlock.Hash(), ethBlock.NumberU64(), []*types.Receipt{receipt}) |
| 65 | + |
| 66 | + // Set up the mock with the expected calls to Accept |
| 67 | + txIndex := 0 |
| 68 | + mockAccepter := precompileconfig.NewMockAccepter(ctrl) |
| 69 | + gomock.InOrder( |
| 70 | + mockAccepter.EXPECT().Accept( |
| 71 | + gomock.Not(gomock.Nil()), // acceptCtx |
| 72 | + ethBlock.Transactions()[txIndex].Hash(), // txHash |
| 73 | + 0, // logIndex |
| 74 | + receipt.Logs[0].Topics, // topics |
| 75 | + receipt.Logs[0].Data, // logData |
| 76 | + ), |
| 77 | + mockAccepter.EXPECT().Accept( |
| 78 | + gomock.Not(gomock.Nil()), // acceptCtx |
| 79 | + ethBlock.Transactions()[txIndex].Hash(), // txHash |
| 80 | + 2, // logIndex |
| 81 | + receipt.Logs[2].Topics, // topics |
| 82 | + receipt.Logs[2].Data, // logData |
| 83 | + ), |
| 84 | + ) |
| 85 | + |
| 86 | + // Call handlePrecompileAccept |
| 87 | + blk := vm.newBlock(ethBlock) |
| 88 | + rules := ¶ms.Rules{ |
| 89 | + AccepterPrecompiles: map[common.Address]precompileconfig.Accepter{ |
| 90 | + precompileAddr: mockAccepter, |
| 91 | + }, |
| 92 | + } |
| 93 | + require.NoError(blk.handlePrecompileAccept(rules, nil)) |
| 94 | +} |
0 commit comments