From 00caabe0301cf72c44f508489bf1dff38aae834f Mon Sep 17 00:00:00 2001 From: Roy Crihfield Date: Fri, 15 Mar 2024 18:09:31 +0800 Subject: [PATCH] more updates --- builder_test.go | 23 +++++++++++------------ indexer/mocks/test_data.go | 25 +++++++++++++++---------- test_helpers/helpers.go | 6 +++--- test_helpers/test_data.go | 1 + 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/builder_test.go b/builder_test.go index 8fbe4f2..d2ee51a 100644 --- a/builder_test.go +++ b/builder_test.go @@ -18,7 +18,6 @@ package statediff_test import ( "encoding/hex" - "math/big" "testing" "github.com/ethereum/go-ethereum/common" @@ -232,7 +231,7 @@ var ( }) bankAccountAtBlock0 = &types.StateAccount{ Nonce: 0, - Balance: uint256.NewInt(test_helpers.TestBankFunds.Int64()), + Balance: uint256.MustFromBig(test_helpers.TestBankFunds), CodeHash: test_helpers.NullCodeHash.Bytes(), Root: test_helpers.EmptyContractRoot, } @@ -242,10 +241,10 @@ var ( bankAccountAtBlock0RLP, }) - block1BankBalance = uint256.NewInt(test_helpers.TestBankFunds.Int64() - test_helpers.BalanceChange10000 - test_helpers.GasFees) + block1BankBalance = test_helpers.TestBankFunds.Int64() - test_helpers.BalanceChange10000 - test_helpers.GasFees bankAccountAtBlock1 = &types.StateAccount{ Nonce: 1, - Balance: block1BankBalance, + Balance: uint256.NewInt(uint64(block1BankBalance)), CodeHash: test_helpers.NullCodeHash.Bytes(), Root: test_helpers.EmptyContractRoot, } @@ -255,10 +254,10 @@ var ( bankAccountAtBlock1RLP, }) - block2BankBalance = block1BankBalance.Int64() - test_helpers.BalanceChange1Ether - test_helpers.GasFees + block2BankBalance = block1BankBalance - test_helpers.BalanceChange1Ether - test_helpers.GasFees bankAccountAtBlock2 = &types.StateAccount{ Nonce: 2, - Balance: uint256.NewInt(block2BankBalance), + Balance: uint256.NewInt(uint64(block2BankBalance)), CodeHash: test_helpers.NullCodeHash.Bytes(), Root: test_helpers.EmptyContractRoot, } @@ -1867,8 +1866,8 @@ contract test { */ var ( - b = uint256.NewInt(0).Sub(test_helpers.TestBIGBankFunds, test_helpers.BalanceChangeBIG) - block1BankBigBalance = uint256.NewInt(0).Sub(b, uint256.NewInt(test_helpers.GasFees2)) + b = uint256.NewInt(0).Sub(uint256.MustFromBig(test_helpers.TestBIGBankFunds), test_helpers.BalanceChangeBIG) + block1BankBigBalance = uint256.NewInt(0).Sub(b, uint256.NewInt(uint64(test_helpers.GasFees2))) bankAccountAtBlock1b = &types.StateAccount{ Nonce: 1, Balance: block1BankBigBalance, @@ -1893,8 +1892,8 @@ var ( account1AtBlock1bRLP, }) - account1AtBlock2bBalance, _ = uint256.NewInt(0).SetString("1999999999999999999999999761539571000000000", 10) - account1AtBlock2b = &types.StateAccount{ + account1AtBlock2bBalance = uint256.MustFromDecimal("1999999999999999999999999761539571000000000") + account1AtBlock2b = &types.StateAccount{ Nonce: 1, Balance: account1AtBlock2bBalance, CodeHash: test_helpers.NullCodeHash.Bytes(), @@ -1930,8 +1929,8 @@ var ( contractAccountAtBlock2bRLP, }) - bankAccountAtBlock3bBalance, _ = uint256.NewInt(0).SetString("18000000000000000000000001999920365757724976", 10) - bankAccountAtBlock3b = &types.StateAccount{ + bankAccountAtBlock3bBalance = uint256.MustFromDecimal("18000000000000000000000001999920365757724976") + bankAccountAtBlock3b = &types.StateAccount{ Nonce: 3, Balance: bankAccountAtBlock3bBalance, CodeHash: test_helpers.NullCodeHash.Bytes(), diff --git a/indexer/mocks/test_data.go b/indexer/mocks/test_data.go index 07f6e59..d1a684b 100644 --- a/indexer/mocks/test_data.go +++ b/indexer/mocks/test_data.go @@ -28,6 +28,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/trie" + "github.com/holiman/uint256" "github.com/cerc-io/plugeth-statediff/indexer/ipld" "github.com/cerc-io/plugeth-statediff/indexer/shared" @@ -41,6 +42,7 @@ var ( // block data TestChainConfig = params.MainnetChainConfig BlockNumber = TestChainConfig.LondonBlock + BlockTime = *TestChainConfig.CancunTime // TODO: verify this // canonical block at London height // includes 5 transactions: 3 Legacy + 1 EIP-2930 + 1 EIP-1559 @@ -55,7 +57,7 @@ var ( BaseFee: big.NewInt(params.InitialBaseFee), Coinbase: common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476777"), } - MockTransactions, MockReceipts, SenderAddr = createTransactionsAndReceipts(TestChainConfig, BlockNumber) + MockTransactions, MockReceipts, SenderAddr = createTransactionsAndReceipts(TestChainConfig, BlockNumber, BlockTime) MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts, trie.NewEmpty(nil)) MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header()) @@ -63,13 +65,14 @@ var ( // includes 2nd and 5th transactions from the canonical block MockNonCanonicalHeader = MockHeader MockNonCanonicalBlockTransactions = types.Transactions{MockTransactions[1], MockTransactions[4]} - MockNonCanonicalBlockReceipts = createNonCanonicalBlockReceipts(TestChainConfig, BlockNumber, MockNonCanonicalBlockTransactions) + MockNonCanonicalBlockReceipts = createNonCanonicalBlockReceipts(TestChainConfig, BlockNumber, BlockTime, MockNonCanonicalBlockTransactions) MockNonCanonicalBlock = types.NewBlock(&MockNonCanonicalHeader, MockNonCanonicalBlockTransactions, nil, MockNonCanonicalBlockReceipts, trie.NewEmpty(nil)) MockNonCanonicalHeaderRlp, _ = rlp.EncodeToBytes(MockNonCanonicalBlock.Header()) // non-canonical block at London height + 1 // includes 3rd and 5th transactions from the canonical block Block2Number = big.NewInt(BlockNumber.Int64() + 1) + Block2Time = BlockTime + 1 MockNonCanonicalHeader2 = types.Header{ Time: 0, Number: new(big.Int).Set(Block2Number), @@ -82,7 +85,7 @@ var ( Coinbase: common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476777"), } MockNonCanonicalBlock2Transactions = types.Transactions{MockTransactions[2], MockTransactions[4]} - MockNonCanonicalBlock2Receipts = createNonCanonicalBlockReceipts(TestChainConfig, Block2Number, MockNonCanonicalBlock2Transactions) + MockNonCanonicalBlock2Receipts = createNonCanonicalBlockReceipts(TestChainConfig, Block2Number, BlockTime, MockNonCanonicalBlock2Transactions) MockNonCanonicalBlock2 = types.NewBlock(&MockNonCanonicalHeader2, MockNonCanonicalBlock2Transactions, nil, MockNonCanonicalBlock2Receipts, trie.NewEmpty(nil)) MockNonCanonicalHeader2Rlp, _ = rlp.EncodeToBytes(MockNonCanonicalBlock2.Header()) @@ -158,7 +161,7 @@ var ( ContractLeafKey = test_helpers.AddressToLeafKey(ContractAddress) ContractAccount = &types.StateAccount{ Nonce: nonce1, - Balance: big.NewInt(0), + Balance: uint256.NewInt(0), CodeHash: ContractCodeHash.Bytes(), Root: common.HexToHash(ContractRoot), } @@ -182,7 +185,7 @@ var ( AccountCodeHash = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") AccountLeafKey = test_helpers.Account2LeafKey RemovedLeafKey = test_helpers.Account1LeafKey - Balance, _ = new(big.Int).SetString("106387458790507306766", 10) + Balance = uint256.MustFromDecimal("106387458790507306766") Account = &types.StateAccount{ Nonce: nonce0, Balance: Balance, @@ -381,7 +384,9 @@ func createLegacyTransactionsAndReceipts(config *params.ChainConfig, blockNumber trx2 := types.NewTransaction(1, AnotherAddress, big.NewInt(2000), 100, big.NewInt(200), []byte{}) trx3 := types.NewContractCreation(2, big.NewInt(1500), 75, big.NewInt(150), MockContractByteCode) - transactionSigner := types.MakeSigner(config, blockNumber) + // For legacy data, block time is not relevant + blockTime := uint64(0) + transactionSigner := types.MakeSigner(config, blockNumber, blockTime) mockCurve := elliptic.P256() mockPrvKey, err := ecdsa.GenerateKey(mockCurve, rand.Reader) if err != nil { @@ -420,7 +425,7 @@ func createLegacyTransactionsAndReceipts(config *params.ChainConfig, blockNumber } // createTransactionsAndReceipts is a helper function to generate signed mock transactions and mock receipts with mock logs -func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big.Int) (types.Transactions, types.Receipts, common.Address) { +func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big.Int, blockTime uint64) (types.Transactions, types.Receipts, common.Address) { // make transactions trx1 := types.NewTransaction(0, Address, big.NewInt(1000), 50, big.NewInt(100), []byte{}) trx2 := types.NewTransaction(1, AnotherAddress, big.NewInt(2000), 100, big.NewInt(200), []byte{}) @@ -453,7 +458,7 @@ func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big. }, }) - transactionSigner := types.MakeSigner(config, blockNumber) + transactionSigner := types.MakeSigner(config, blockNumber, blockTime) mockCurve := elliptic.P256() mockPrvKey, err := ecdsa.GenerateKey(mockCurve, rand.Reader) if err != nil { @@ -516,8 +521,8 @@ func createTransactionsAndReceipts(config *params.ChainConfig, blockNumber *big. } // createNonCanonicalBlockReceipts is a helper function to generate mock receipts with mock logs for non-canonical blocks -func createNonCanonicalBlockReceipts(config *params.ChainConfig, blockNumber *big.Int, transactions types.Transactions) types.Receipts { - transactionSigner := types.MakeSigner(config, blockNumber) +func createNonCanonicalBlockReceipts(config *params.ChainConfig, blockNumber *big.Int, blockTime uint64, transactions types.Transactions) types.Receipts { + transactionSigner := types.MakeSigner(config, blockNumber, blockTime) mockCurve := elliptic.P256() mockPrvKey, err := ecdsa.GenerateKey(mockCurve, rand.Reader) if err != nil { diff --git a/test_helpers/helpers.go b/test_helpers/helpers.go index ef8ba8e..e608ed8 100644 --- a/test_helpers/helpers.go +++ b/test_helpers/helpers.go @@ -33,9 +33,9 @@ import ( ) func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance, baseFee *big.Int, initialGasLimit uint64) *types.Block { - alloc := map[common.Address]types.Account{ - addr: types.Account{Balance: balance}} + alloc := map[common.Address]types.Account{addr: {Balance: balance}} g := core.Genesis{ + Config: TestChainConfig, Alloc: alloc, BaseFee: baseFee, } @@ -48,7 +48,7 @@ func GenesisBlockForTesting(db ethdb.Database, addr common.Address, balance, bas // MakeChain creates a chain of n blocks starting at and including parent. // the returned hash chain is ordered head->parent. func MakeChain(n int, parent *types.Block, chainGen func(int, *core.BlockGen)) ([]*types.Block, *core.BlockChain) { - config := params.TestChainConfig + config := TestChainConfig blocks, _ := core.GenerateChain(config, parent, ethash.NewFaker(), Testdb, n, chainGen) chain, _ := core.NewBlockChain(Testdb, nil, nil, nil, ethash.NewFaker(), vm.Config{}, nil, nil) return blocks, chain diff --git a/test_helpers/test_data.go b/test_helpers/test_data.go index 5147e58..1b97e38 100644 --- a/test_helpers/test_data.go +++ b/test_helpers/test_data.go @@ -52,6 +52,7 @@ var ( StorageValue = utils.Hex2Bytes("0x03") NullHash = common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000") + TestChainConfig = &*params.TestChainConfig Testdb = rawdb.NewMemoryDatabase() TestBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") TestBankAddress = crypto.PubkeyToAddress(TestBankKey.PublicKey) //0x71562b71999873DB5b286dF957af199Ec94617F7