-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
precompile accepter should take logIdx (#728)
* precompile accepter should take logIdx * minimize mock * copyright yr * add comments
- Loading branch information
Showing
3 changed files
with
155 additions
and
13 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// (c) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package evm | ||
|
||
import ( | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/database/memdb" | ||
"github.com/ava-labs/subnet-evm/core/rawdb" | ||
"github.com/ava-labs/subnet-evm/core/types" | ||
"github.com/ava-labs/subnet-evm/params" | ||
"github.com/ava-labs/subnet-evm/precompile/precompileconfig" | ||
"github.com/ava-labs/subnet-evm/trie" | ||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHandlePrecompileAccept(t *testing.T) { | ||
require := require.New(t) | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
db := memdb.New() | ||
vm := &VM{ | ||
chaindb: Database{db}, | ||
chainConfig: params.TestChainConfig, | ||
} | ||
|
||
precompileAddr := common.Address{0x05} | ||
otherAddr := common.Address{0x06} | ||
|
||
// Prepare a receipt with 3 logs, two of which are from the precompile | ||
receipt := &types.Receipt{ | ||
Logs: []*types.Log{ | ||
{ | ||
Address: precompileAddr, | ||
Topics: []common.Hash{{0x01}, {0x02}, {0x03}}, | ||
Data: []byte("log1"), | ||
}, | ||
{ | ||
Address: otherAddr, | ||
Topics: []common.Hash{{0x01}, {0x02}, {0x04}}, | ||
Data: []byte("log2"), | ||
}, | ||
{ | ||
Address: precompileAddr, | ||
Topics: []common.Hash{{0x01}, {0x02}, {0x05}}, | ||
Data: []byte("log3"), | ||
}, | ||
}, | ||
} | ||
ethBlock := types.NewBlock( | ||
&types.Header{Number: big.NewInt(1)}, | ||
[]*types.Transaction{types.NewTx(&types.LegacyTx{})}, | ||
nil, | ||
[]*types.Receipt{receipt}, | ||
trie.NewStackTrie(nil), | ||
) | ||
// Write the block to the db | ||
rawdb.WriteBlock(db, ethBlock) | ||
rawdb.WriteReceipts(db, ethBlock.Hash(), ethBlock.NumberU64(), []*types.Receipt{receipt}) | ||
|
||
// Set up the mock with the expected calls to Accept | ||
txIndex := 0 | ||
mockAccepter := precompileconfig.NewMockAccepter(ctrl) | ||
gomock.InOrder( | ||
mockAccepter.EXPECT().Accept( | ||
gomock.Not(gomock.Nil()), // acceptCtx | ||
ethBlock.Transactions()[txIndex].Hash(), // txHash | ||
0, // logIndex | ||
receipt.Logs[0].Topics, // topics | ||
receipt.Logs[0].Data, // logData | ||
), | ||
mockAccepter.EXPECT().Accept( | ||
gomock.Not(gomock.Nil()), // acceptCtx | ||
ethBlock.Transactions()[txIndex].Hash(), // txHash | ||
2, // logIndex | ||
receipt.Logs[2].Topics, // topics | ||
receipt.Logs[2].Data, // logData | ||
), | ||
) | ||
|
||
// Call handlePrecompileAccept | ||
blk := vm.newBlock(ethBlock) | ||
rules := ¶ms.Rules{ | ||
AccepterPrecompiles: map[common.Address]precompileconfig.Accepter{ | ||
precompileAddr: mockAccepter, | ||
}, | ||
} | ||
require.NoError(blk.handlePrecompileAccept(rules, nil)) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.