diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index 4a2c60f75..20cb5b8b5 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -112,11 +112,24 @@ func NewSimulatedBackendWithConfig(alloc core.GenesisAlloc, config *chain.Config func NewSimulatedBackend(t *testing.T, alloc core.GenesisAlloc, gasLimit uint64) *SimulatedBackend { b := NewSimulatedBackendWithConfig(alloc, params.TestChainConfig, gasLimit) t.Cleanup(func() { - b.m.DB.Close() + b.Close() }) + if b.m.HistoryV3 { + t.Skip("TODO: Fixme") + } return b } +func NewTestSimulatedBackendWithConfig(t *testing.T, alloc core.GenesisAlloc, config *chain.Config, gasLimit uint64) *SimulatedBackend { + b := NewSimulatedBackendWithConfig(alloc, config, gasLimit) + t.Cleanup(func() { + b.Close() + }) + if b.m.HistoryV3 { + t.Skip("TODO: Fixme") + } + return b +} func (b *SimulatedBackend) DB() kv.RwDB { return b.m.DB } func (b *SimulatedBackend) Agg() *state2.AggregatorV3 { return b.m.HistoryV3Components() } func (b *SimulatedBackend) BlockReader() *snapshotsync.BlockReaderWithSnapshots { @@ -174,8 +187,10 @@ func (b *SimulatedBackend) emptyPendingBlock() { func (b *SimulatedBackend) stateByBlockNumber(db kv.Tx, blockNumber *big.Int) *state.IntraBlockState { if blockNumber == nil || blockNumber.Cmp(b.pendingBlock.Number()) == 0 { return state.New(state.NewPlainState(db, b.pendingBlock.NumberU64()+1, nil)) + //return state.New(b.m.NewHistoryStateReader(b.pendingBlock.NumberU64()+1, db)) } return state.New(state.NewPlainState(db, blockNumber.Uint64()+1, nil)) + //return state.New(b.m.NewHistoryStateReader(blockNumber.Uint64()+1, db)) } // CodeAt returns the code associated with a certain account in the blockchain. diff --git a/accounts/abi/bind/backends/simulated_test.go b/accounts/abi/bind/backends/simulated_test.go index d735169e3..2e984e3db 100644 --- a/accounts/abi/bind/backends/simulated_test.go +++ b/accounts/abi/bind/backends/simulated_test.go @@ -27,10 +27,9 @@ import ( "time" "github.com/holiman/uint256" + ethereum "github.com/ledgerwatch/erigon" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv" - - ethereum "github.com/ledgerwatch/erigon" "github.com/ledgerwatch/erigon/accounts/abi" "github.com/ledgerwatch/erigon/accounts/abi/bind" "github.com/ledgerwatch/erigon/common" @@ -148,6 +147,8 @@ func TestNewSimulatedBackend(t *testing.T) { }); err != nil { t.Fatal(err) } + + //statedb := sim.stateByBlockNumber(tx, big.NewInt(int64(num+1))) statedb := state.New(state.NewPlainState(tx, num+1, nil)) bal := statedb.GetBalance(testAddr) if !bal.Eq(expectedBal) { diff --git a/cmd/rpcdaemon/commands/eth_callMany_test.go b/cmd/rpcdaemon/commands/eth_callMany_test.go index c538d9607..b86b04cac 100644 --- a/cmd/rpcdaemon/commands/eth_callMany_test.go +++ b/cmd/rpcdaemon/commands/eth_callMany_test.go @@ -65,7 +65,7 @@ func TestCallMany(t *testing.T) { transactOpts, _ := bind.NewKeyedTransactorWithChainID(key, chainID) transactOpts1, _ := bind.NewKeyedTransactorWithChainID(key1, chainID) transactOpts2, _ := bind.NewKeyedTransactorWithChainID(key2, chainID) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) defer contractBackend.Close() stateCache := kvcache.New(kvcache.DefaultCoherentConfig) tokenAddr, _, tokenContract, _ := contracts.DeployToken(transactOpts, contractBackend, address1) diff --git a/cmd/rpcdaemon/commands/otterscan_generic_tracer.go b/cmd/rpcdaemon/commands/otterscan_generic_tracer.go index 077cf9405..cb6cd3d06 100644 --- a/cmd/rpcdaemon/commands/otterscan_generic_tracer.go +++ b/cmd/rpcdaemon/commands/otterscan_generic_tracer.go @@ -6,11 +6,11 @@ import ( "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv" + "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/log/v3" "github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/core/state" - "github.com/ledgerwatch/erigon/core/systemcontracts" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/vm" "github.com/ledgerwatch/erigon/turbo/shards" @@ -31,7 +31,10 @@ func (api *OtterscanAPIImpl) genericTracer(dbtx kv.Tx, ctx context.Context, bloc return nil } - reader := state.NewPlainState(dbtx, blockNum, systemcontracts.SystemContractCodeLookup[chainConfig.ChainName]) + reader, err := rpchelper.CreateHistoryStateReader(dbtx, blockNum, 0, api.historyV3(dbtx), chainConfig.ChainName) + if err != nil { + return err + } stateCache := shards.NewStateCache(32, 0 /* no limit */) cachedReader := state.NewCachedReader(reader, stateCache) noop := state.NewNoopWriter() diff --git a/cmd/rpcdaemon/commands/otterscan_has_code.go b/cmd/rpcdaemon/commands/otterscan_has_code.go index 2c0b9a025..f708374b2 100644 --- a/cmd/rpcdaemon/commands/otterscan_has_code.go +++ b/cmd/rpcdaemon/commands/otterscan_has_code.go @@ -6,8 +6,6 @@ import ( libcommon "github.com/ledgerwatch/erigon-lib/common" - "github.com/ledgerwatch/erigon/core/state" - "github.com/ledgerwatch/erigon/core/systemcontracts" "github.com/ledgerwatch/erigon/rpc" "github.com/ledgerwatch/erigon/turbo/rpchelper" ) @@ -28,7 +26,10 @@ func (api *OtterscanAPIImpl) HasCode(ctx context.Context, address libcommon.Addr return false, err } - reader := state.NewPlainState(tx, blockNumber, systemcontracts.SystemContractCodeLookup[chainConfig.ChainName]) + reader, err := rpchelper.CreateHistoryStateReader(tx, blockNumber, 0, api.historyV3(tx), chainConfig.ChainName) + if err != nil { + return false, err + } acc, err := reader.ReadAccountData(address) if acc == nil || err != nil { return false, err diff --git a/cmd/rpcdaemon/commands/otterscan_search_trace.go b/cmd/rpcdaemon/commands/otterscan_search_trace.go index e6844214c..181841494 100644 --- a/cmd/rpcdaemon/commands/otterscan_search_trace.go +++ b/cmd/rpcdaemon/commands/otterscan_search_trace.go @@ -7,12 +7,12 @@ import ( "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv" + "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/log/v3" "github.com/ledgerwatch/erigon/core" "github.com/ledgerwatch/erigon/core/rawdb" "github.com/ledgerwatch/erigon/core/state" - "github.com/ledgerwatch/erigon/core/systemcontracts" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/core/vm" "github.com/ledgerwatch/erigon/turbo/shards" @@ -54,7 +54,10 @@ func (api *OtterscanAPIImpl) traceBlock(dbtx kv.Tx, ctx context.Context, blockNu return false, nil, err } - reader := state.NewPlainState(dbtx, blockNum, systemcontracts.SystemContractCodeLookup[chainConfig.ChainName]) + reader, err := rpchelper.CreateHistoryStateReader(dbtx, blockNum, 0, api.historyV3(dbtx), chainConfig.ChainName) + if err != nil { + return false, nil, err + } stateCache := shards.NewStateCache(32, 0 /* no limit */) cachedReader := state.NewCachedReader(reader, stateCache) noop := state.NewNoopWriter() diff --git a/cmd/state/commands/opcode_tracer.go b/cmd/state/commands/opcode_tracer.go index 34e7ea551..ff5a83225 100644 --- a/cmd/state/commands/opcode_tracer.go +++ b/cmd/state/commands/opcode_tracer.go @@ -16,7 +16,9 @@ import ( chain2 "github.com/ledgerwatch/erigon-lib/chain" libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv" + "github.com/ledgerwatch/erigon-lib/kv/kvcfg" "github.com/ledgerwatch/erigon-lib/kv/mdbx" + "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/log/v3" "github.com/spf13/cobra" @@ -547,6 +549,16 @@ func OpcodeTracer(genesis *core.Genesis, blockNum uint64, chaindata string, numB timeLastBlock := startTime blockNumLastReport := blockNum + + var historyV3 bool + chainDb.View(context.Background(), func(tx kv.Tx) (err error) { + historyV3, err = kvcfg.HistoryV3.Enabled(tx) + if err != nil { + return err + } + return nil + }) + for !interrupt { var block *types.Block if err := chainDb.View(context.Background(), func(tx kv.Tx) (err error) { @@ -569,7 +581,10 @@ func OpcodeTracer(genesis *core.Genesis, blockNum uint64, chaindata string, numB ot.fsumWriter = bufio.NewWriter(fsum) } - dbstate := state.NewPlainState(historyTx, block.NumberU64(), systemcontracts.SystemContractCodeLookup[chainConfig.ChainName]) + dbstate, err := rpchelper.CreateHistoryStateReader(historyTx, block.NumberU64(), 0, historyV3, chainConfig.ChainName) + if err != nil { + return err + } intraBlockState := state.New(dbstate) getHeader := func(hash libcommon.Hash, number uint64) *types.Header { diff --git a/core/genesis_test.go b/core/genesis_test.go index 95d2df024..ce98074d4 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -1,4 +1,4 @@ -package core +package core_test import ( "context" @@ -9,6 +9,8 @@ import ( libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv/memdb" + "github.com/ledgerwatch/erigon/core" + "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -23,13 +25,13 @@ import ( func TestDefaultGenesisBlockHashes(t *testing.T) { db := memdb.NewTestDB(t) check := func(network string) { - genesis := DefaultGenesisBlockByChainName(network) + genesis := core.DefaultGenesisBlockByChainName(network) tx, err := db.BeginRw(context.Background()) if err != nil { t.Fatal(err) } defer tx.Rollback() - _, block, err := WriteGenesisBlock(tx, genesis, nil) + _, block, err := core.WriteGenesisBlock(tx, genesis, nil) require.NoError(t, err) expect := params.GenesisHashByChainName(network) require.NotNil(t, expect, network) @@ -44,12 +46,12 @@ func TestDefaultGenesisBlockRoots(t *testing.T) { require := require.New(t) var err error - block, _, _ := DefaultGenesisBlock().ToBlock() + block, _, _ := core.DefaultGenesisBlock().ToBlock() if block.Hash() != params.MainnetGenesisHash { t.Errorf("wrong mainnet genesis hash, got %v, want %v", block.Hash(), params.MainnetGenesisHash) } - block, _, err = DefaultSokolGenesisBlock().ToBlock() + block, _, err = core.DefaultSokolGenesisBlock().ToBlock() require.NoError(err) if block.Root() != params.SokolGenesisStateRoot { t.Errorf("wrong Sokol genesis state root, got %v, want %v", block.Root(), params.SokolGenesisStateRoot) @@ -58,7 +60,7 @@ func TestDefaultGenesisBlockRoots(t *testing.T) { t.Errorf("wrong Sokol genesis hash, got %v, want %v", block.Hash(), params.SokolGenesisHash) } - block, _, err = DefaultGnosisGenesisBlock().ToBlock() + block, _, err = core.DefaultGnosisGenesisBlock().ToBlock() require.NoError(err) if block.Root() != params.GnosisGenesisStateRoot { t.Errorf("wrong Gnosis Chain genesis state root, got %v, want %v", block.Root(), params.GnosisGenesisStateRoot) @@ -67,7 +69,7 @@ func TestDefaultGenesisBlockRoots(t *testing.T) { t.Errorf("wrong Gnosis Chain genesis hash, got %v, want %v", block.Hash(), params.GnosisGenesisHash) } - block, _, err = DefaultChiadoGenesisBlock().ToBlock() + block, _, err = core.DefaultChiadoGenesisBlock().ToBlock() require.NoError(err) if block.Root() != params.ChiadoGenesisStateRoot { t.Errorf("wrong Chiado genesis state root, got %v, want %v", block.Root(), params.ChiadoGenesisStateRoot) @@ -79,14 +81,14 @@ func TestDefaultGenesisBlockRoots(t *testing.T) { func TestCommitGenesisIdempotency(t *testing.T) { _, tx := memdb.NewTestTx(t) - genesis := DefaultGenesisBlockByChainName(networkname.MainnetChainName) - _, _, err := WriteGenesisBlock(tx, genesis, nil) + genesis := core.DefaultGenesisBlockByChainName(networkname.MainnetChainName) + _, _, err := core.WriteGenesisBlock(tx, genesis, nil) require.NoError(t, err) seq, err := tx.ReadSequence(kv.EthTx) require.NoError(t, err) require.Equal(t, uint64(2), seq) - _, _, err = WriteGenesisBlock(tx, genesis, nil) + _, _, err = core.WriteGenesisBlock(tx, genesis, nil) require.NoError(t, err) seq, err = tx.ReadSequence(kv.EthTx) require.NoError(t, err) @@ -97,7 +99,7 @@ func TestSokolHeaderRLP(t *testing.T) { require := require.New(t) { //sokol expect := common.FromHex("f9020da00000000000000000000000000000000000000000000000000000000000000000a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347940000000000000000000000000000000000000000a0fad4af258fd11939fae0c6c6eec9d340b1caac0b0196fd9a1bc3f489c5bf00b3a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000830200008083663be080808080b8410000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") - block, _, err := DefaultSokolGenesisBlock().ToBlock() + block, _, err := core.DefaultSokolGenesisBlock().ToBlock() require.NoError(err) b, err := rlp.EncodeToBytes(block.Header()) require.NoError(err) @@ -152,22 +154,25 @@ func TestAllocConstructor(t *testing.T) { funds := big.NewInt(1000000000) address := libcommon.HexToAddress("0x1000000000000000000000000000000000000001") - genSpec := &Genesis{ + genSpec := &core.Genesis{ Config: params.AllProtocolChanges, - Alloc: GenesisAlloc{ + Alloc: core.GenesisAlloc{ address: {Constructor: deploymentCode, Balance: funds}, }, } db := memdb.NewTestDB(t) defer db.Close() - _, _, err := CommitGenesisBlock(db, genSpec) + _, _, err := core.CommitGenesisBlock(db, genSpec) require.NoError(err) tx, err := db.BeginRo(context.Background()) require.NoError(err) defer tx.Rollback() - state := state.New(state.NewPlainState(tx, 1, nil)) + //TODO: support historyV3 + reader, err := rpchelper.CreateHistoryStateReader(tx, 1, 0, false, genSpec.Config.ChainName) + require.NoError(err) + state := state.New(reader) balance := state.GetBalance(address) assert.Equal(funds, balance.ToBig()) code := state.GetCode(address) diff --git a/core/state/database_test.go b/core/state/database_test.go index f517dee9a..e498b97fe 100644 --- a/core/state/database_test.go +++ b/core/state/database_test.go @@ -73,7 +73,7 @@ func TestCreate2Revive(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) defer contractBackend.Close() transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) @@ -243,7 +243,7 @@ func TestCreate2Polymorth(t *testing.T) { ) m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) defer contractBackend.Close() transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) @@ -460,8 +460,7 @@ func TestReorgOverSelfDestruct(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOpts.GasLimit = 1000000 @@ -500,8 +499,7 @@ func TestReorgOverSelfDestruct(t *testing.T) { } // Create a longer chain, with 4 blocks (with higher total difficulty) that reverts the change of stroage self-destruction of the contract - contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackendLonger.Close() + contractBackendLonger := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOptsLonger.GasLimit = 1000000 @@ -610,8 +608,7 @@ func TestReorgOverStateChange(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOpts.GasLimit = 1000000 @@ -644,8 +641,7 @@ func TestReorgOverStateChange(t *testing.T) { } // Create a longer chain, with 4 blocks (with higher total difficulty) that reverts the change of stroage self-destruction of the contract - contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackendLonger.Close() + contractBackendLonger := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOptsLonger.GasLimit = 1000000 @@ -765,8 +761,7 @@ func TestCreateOnExistingStorage(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) var err error - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) @@ -894,8 +889,7 @@ func TestEip2200Gas(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOpts.GasLimit = 1000000 @@ -986,8 +980,7 @@ func TestWrongIncarnation(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOpts.GasLimit = 1000000 @@ -1102,8 +1095,7 @@ func TestWrongIncarnation2(t *testing.T) { m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOpts.GasLimit = 1000000 @@ -1142,7 +1134,7 @@ func TestWrongIncarnation2(t *testing.T) { } // Create a longer chain, with 4 blocks (with higher total difficulty) that reverts the change of stroage self-destruction of the contract - contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) + contractBackendLonger := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOptsLonger.GasLimit = 1000000 @@ -1354,8 +1346,7 @@ func TestRecreateAndRewind(t *testing.T) { ) m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOpts.GasLimit = 1000000 @@ -1422,8 +1413,7 @@ func TestRecreateAndRewind(t *testing.T) { t.Fatalf("generate blocks: %v", err1) } - contractBackendLonger := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackendLonger.Close() + contractBackendLonger := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOptsLonger, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) transactOptsLonger.GasLimit = 1000000 diff --git a/tests/statedb_chain_test.go b/tests/statedb_chain_test.go index 8ed97c224..a10e348a9 100644 --- a/tests/statedb_chain_test.go +++ b/tests/statedb_chain_test.go @@ -64,8 +64,7 @@ func TestSelfDestructReceive(t *testing.T) { db := olddb.NewObjectDatabase(m.DB) defer db.Close() - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) transactOpts, err := bind.NewKeyedTransactorWithChainID(key, m.ChainConfig.ChainID) require.NoError(t, err) diff --git a/tests/statedb_insert_chain_transaction_test.go b/tests/statedb_insert_chain_transaction_test.go index 5dda9b0da..e50d28bd8 100644 --- a/tests/statedb_insert_chain_transaction_test.go +++ b/tests/statedb_insert_chain_transaction_test.go @@ -742,8 +742,7 @@ func genBlocks(t *testing.T, gspec *core.Genesis, txs map[int]tx) (*stages.MockS key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") m := stages.MockWithGenesis(t, gspec, key, false) - contractBackend := backends.NewSimulatedBackendWithConfig(gspec.Alloc, gspec.Config, gspec.GasLimit) - defer contractBackend.Close() + contractBackend := backends.NewTestSimulatedBackendWithConfig(t, gspec.Alloc, gspec.Config, gspec.GasLimit) chain, err := core.GenerateChain(m.ChainConfig, m.Genesis, m.Engine, m.DB, len(txs), func(i int, block *core.BlockGen) { var tx types.Transaction diff --git a/turbo/stages/blockchain_test.go b/turbo/stages/blockchain_test.go index b58b77f49..bc145ebcf 100644 --- a/turbo/stages/blockchain_test.go +++ b/turbo/stages/blockchain_test.go @@ -1024,15 +1024,15 @@ func TestDoubleAccountRemoval(t *testing.T) { } defer tx.Rollback() - st := state.New(m.NewHistoricalStateReader(1, tx)) + st := state.New(m.NewHistoryStateReader(1, tx)) assert.NoError(t, err) assert.False(t, st.Exist(theAddr), "Contract should not exist at block #0") - st = state.New(m.NewHistoricalStateReader(2, tx)) + st = state.New(m.NewHistoryStateReader(2, tx)) assert.NoError(t, err) assert.True(t, st.Exist(theAddr), "Contract should exist at block #1") - st = state.New(m.NewHistoricalStateReader(3, tx)) + st = state.New(m.NewHistoryStateReader(3, tx)) assert.NoError(t, err) assert.True(t, st.Exist(theAddr), "Contract should exist at block #2") } @@ -1387,7 +1387,7 @@ func TestDeleteRecreateSlots(t *testing.T) { t.Fatalf("failed to insert into chain: %v", err) } err = m.DB.View(context.Background(), func(tx kv.Tx) error { - statedb := state.New(state.NewPlainState(tx, 2, nil)) + statedb := state.New(m.NewHistoryStateReader(2, tx)) // If all is correct, then slot 1 and 2 are zero key1 := libcommon.HexToHash("01") @@ -1495,7 +1495,8 @@ func TestCVE2020_26265(t *testing.T) { t.Fatalf("failed to insert into chain: %v", err) } err = m.DB.View(context.Background(), func(tx kv.Tx) error { - statedb := state.New(state.NewPlainState(tx, 2, nil)) + reader := m.NewHistoryStateReader(2, tx) + statedb := state.New(reader) got := statedb.GetBalance(aa) if !got.Eq(new(uint256.Int).SetUint64(5)) { @@ -1561,7 +1562,7 @@ func TestDeleteRecreateAccount(t *testing.T) { t.Fatalf("failed to insert into chain: %v", err) } err = m.DB.View(context.Background(), func(tx kv.Tx) error { - statedb := state.New(state.NewPlainState(tx, 2, nil)) + statedb := state.New(m.NewHistoryStateReader(2, tx)) // If all is correct, then both slots are zero key1 := libcommon.HexToHash("01") @@ -1873,7 +1874,7 @@ func TestInitThenFailCreateContract(t *testing.T) { err = m.DB.View(context.Background(), func(tx kv.Tx) error { // Import the canonical chain - statedb := state.New(state.NewPlainState(tx, 2, nil)) + statedb := state.New(m.NewHistoryStateReader(2, tx)) if got, exp := statedb.GetBalance(aa), uint64(100000); got.Uint64() != exp { t.Fatalf("Genesis err, got %v exp %v", got, exp) } @@ -1883,7 +1884,7 @@ func TestInitThenFailCreateContract(t *testing.T) { if err := m.InsertChain(chain.Slice(0, 1)); err != nil { t.Fatalf("block %d: failed to insert into chain: %v", block.NumberU64(), err) } - statedb = state.New(state.NewPlainState(tx, 1, nil)) + statedb = state.New(m.NewHistoryStateReader(1, tx)) if got, exp := statedb.GetBalance(aa), uint64(100000); got.Uint64() != exp { t.Fatalf("block %d: got %v exp %v", block.NumberU64(), got, exp) } @@ -2079,7 +2080,7 @@ func TestEIP1559Transition(t *testing.T) { } err = m.DB.View(context.Background(), func(tx kv.Tx) error { - statedb := state.New(state.NewPlainState(tx, 1, nil)) + statedb := state.New(m.NewHistoryStateReader(1, tx)) // 3: Ensure that miner received only the tx's tip. actual := statedb.GetBalance(block.Coinbase()) @@ -2120,7 +2121,7 @@ func TestEIP1559Transition(t *testing.T) { block = chain.Blocks[0] err = m.DB.View(context.Background(), func(tx kv.Tx) error { - statedb := state.New(state.NewPlainState(tx, 1, nil)) + statedb := state.New(m.NewHistoryStateReader(1, tx)) effectiveTip := block.Transactions()[0].GetPrice().Uint64() - block.BaseFee().Uint64() // 6+5: Ensure that miner received only the tx's effective tip. diff --git a/turbo/stages/mock_sentry.go b/turbo/stages/mock_sentry.go index 3947c21ea..c197d78a9 100644 --- a/turbo/stages/mock_sentry.go +++ b/turbo/stages/mock_sentry.go @@ -15,7 +15,6 @@ import ( libcommon "github.com/ledgerwatch/erigon-lib/common" "github.com/ledgerwatch/erigon-lib/common/datadir" "github.com/ledgerwatch/erigon-lib/common/dir" - "github.com/ledgerwatch/erigon-lib/common/rawdbv3" "github.com/ledgerwatch/erigon-lib/direct" "github.com/ledgerwatch/erigon-lib/gointerfaces" proto_downloader "github.com/ledgerwatch/erigon-lib/gointerfaces/downloader" @@ -30,6 +29,7 @@ import ( "github.com/ledgerwatch/erigon-lib/txpool" types2 "github.com/ledgerwatch/erigon-lib/types" "github.com/ledgerwatch/erigon/core/systemcontracts" + "github.com/ledgerwatch/erigon/turbo/rpchelper" "github.com/ledgerwatch/log/v3" "google.golang.org/protobuf/types/known/emptypb" @@ -672,19 +672,12 @@ func (ms *MockSentry) HeaderDownload() *headerdownload.HeaderDownload { return ms.sentriesClient.Hd } -func (ms *MockSentry) NewHistoricalStateReader(blockNum uint64, tx kv.Tx) state.StateReader { - if ms.HistoryV3 { - r := state.NewHistoryReaderV3() - r.SetTx(tx) - minTxNum, err := rawdbv3.TxNums.Min(tx, blockNum) - if err != nil { - panic(err) - } - r.SetTxNum(minTxNum) - return r +func (ms *MockSentry) NewHistoryStateReader(blockNum uint64, tx kv.Tx) state.StateReader { + r, err := rpchelper.CreateHistoryStateReader(tx, blockNum, 0, ms.HistoryV3, ms.ChainConfig.ChainName) + if err != nil { + panic(err) } - - return state.NewPlainState(tx, blockNum, systemcontracts.SystemContractCodeLookup[ms.ChainConfig.ChainName]) + return r } func (ms *MockSentry) NewStateReader(tx kv.Tx) state.StateReader {