diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index e78d16e5c37..30649d101bb 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -34,6 +34,7 @@ import ( "github.com/urfave/cli/v2" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/hexutility" @@ -307,7 +308,7 @@ func Main(ctx *cli.Context) error { } defer tx.Rollback() - sd, err := libstate.NewSharedDomains(tx, log.New()) + sd, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return err } @@ -630,7 +631,7 @@ func CalculateStateRoot(tx kv.RwTx) (*libcommon.Hash, error) { defer c.Close() h := libcommon.NewHasher() defer libcommon.ReturnHasherToPool(h) - domains, err := libstate.NewSharedDomains(tx, log.New()) + domains, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return nil, fmt.Errorf("NewSharedDomains: %w", err) } diff --git a/cmd/evm/runner.go b/cmd/evm/runner.go index f35ec298106..da731254eef 100644 --- a/cmd/evm/runner.go +++ b/cmd/evm/runner.go @@ -32,6 +32,7 @@ import ( "testing" "time" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/config3" "github.com/erigontech/erigon-lib/kv" "github.com/erigontech/erigon-lib/kv/temporal" @@ -186,7 +187,7 @@ func runCmd(ctx *cli.Context) error { } defer tx.Rollback() - sd, err := state2.NewSharedDomains(tx, log.Root()) + sd, err := state2.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.Root()) if err != nil { return err } diff --git a/cmd/integration/commands/stages.go b/cmd/integration/commands/stages.go index 8164de23eb6..fb307a1f784 100644 --- a/cmd/integration/commands/stages.go +++ b/cmd/integration/commands/stages.go @@ -38,6 +38,7 @@ import ( "github.com/erigontech/secp256k1" chain2 "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/dbg" @@ -664,7 +665,7 @@ func stageSnapshots(db kv.TemporalRwDB, ctx context.Context, logger log.Logger) ac := agg.BeginFilesRo() defer ac.Close() - domains, err := libstate.NewSharedDomains(tx, logger) + domains, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } @@ -1143,7 +1144,7 @@ func stageExec(db kv.TemporalRwDB, ctx context.Context, logger log.Logger) error return err } if execProgress == 0 { - doms, err := libstate.NewSharedDomains(tx, log.New()) + doms, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { panic(err) } diff --git a/cmd/integration/commands/state_domains.go b/cmd/integration/commands/state_domains.go index 5d839881b14..7e7d1bf93f9 100644 --- a/cmd/integration/commands/state_domains.go +++ b/cmd/integration/commands/state_domains.go @@ -29,6 +29,7 @@ import ( "sort" "strings" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/etl" "github.com/erigontech/erigon-lib/seg" state3 "github.com/erigontech/erigon-lib/state" @@ -471,7 +472,7 @@ func requestDomains(chainDb, stateDb kv.RwDB, ctx context.Context, readDomain st stateTx, err := stateDb.BeginRw(ctx) must(err) defer stateTx.Rollback() - domains, err := state3.NewSharedDomains(stateTx, logger) + domains, err := state3.NewSharedDomains(stateTx, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } diff --git a/cmd/integration/commands/state_stages.go b/cmd/integration/commands/state_stages.go index a70d9398594..0684f73bd69 100644 --- a/cmd/integration/commands/state_stages.go +++ b/cmd/integration/commands/state_stages.go @@ -25,6 +25,7 @@ import ( "os" "time" + "github.com/erigontech/erigon-lib/commitment" stateLib "github.com/erigontech/erigon-lib/state" "github.com/erigontech/erigon-lib/wrap" @@ -172,7 +173,7 @@ func syncBySmallSteps(db kv.TemporalRwDB, miningConfig params.MiningConfig, ctx } defer tx.Rollback() - sd, err := stateLib.NewSharedDomains(tx, logger1) + sd, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger1) if err != nil { return err } diff --git a/core/chain_makers.go b/core/chain_makers.go index f167f52d554..aa4c865cc4a 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -30,6 +30,7 @@ import ( "github.com/erigontech/erigon-lib/log/v3" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/length" "github.com/erigontech/erigon-lib/kv" @@ -332,7 +333,7 @@ func GenerateChain(config *chain.Config, parent *types.Block, engine consensus.E defer tx.Rollback() logger := log.New("generate-chain", config.ChainName) - domains, err := libstate.NewSharedDomains(tx, logger) + domains, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) if err != nil { return nil, err } @@ -455,7 +456,7 @@ func hashKeyAndAddIncarnation(k []byte, h *libcommon.Hasher) (newK []byte, err e } func CalcHashRootForTests(tx kv.RwTx, header *types.Header, histV4, trace bool) (hashRoot libcommon.Hash, err error) { - domains, err := libstate.NewSharedDomains(tx, log.New()) + domains, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return hashRoot, fmt.Errorf("NewSharedDomains: %w", err) } diff --git a/core/genesis_write.go b/core/genesis_write.go index 11ec524f70c..07192763551 100644 --- a/core/genesis_write.go +++ b/core/genesis_write.go @@ -35,6 +35,7 @@ import ( "github.com/erigontech/erigon-lib/chain" "github.com/erigontech/erigon-lib/chain/networkname" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/hexutil" @@ -522,7 +523,7 @@ func GenesisToBlock(g *types.Genesis, dirs datadir.Dirs, logger log.Logger) (*ty } defer tx.Rollback() - sd, err := state2.NewSharedDomains(tx, logger) + sd, err := state2.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } diff --git a/core/rawdb/rawtemporaldb/accessors_receipt_test.go b/core/rawdb/rawtemporaldb/accessors_receipt_test.go index 43dcca7249a..36bcf339e13 100644 --- a/core/rawdb/rawtemporaldb/accessors_receipt_test.go +++ b/core/rawdb/rawtemporaldb/accessors_receipt_test.go @@ -4,6 +4,7 @@ import ( "context" "testing" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/kv" "github.com/erigontech/erigon-lib/kv/temporal/temporaltest" @@ -21,7 +22,7 @@ func TestAppendReceipt(t *testing.T) { defer tx.Rollback() ttx := tx.(kv.TemporalTx) - doms, err := state.NewSharedDomains(ttx, log.New()) + doms, err := state.NewSharedDomains(ttx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer doms.Close() doms.SetTx(ttx) diff --git a/core/state/access_list_test.go b/core/state/access_list_test.go index cd53e62af65..1435eed7c9b 100644 --- a/core/state/access_list_test.go +++ b/core/state/access_list_test.go @@ -23,6 +23,7 @@ import ( "github.com/erigontech/erigon-lib/log/v3" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/kv/rawdbv3" stateLib "github.com/erigontech/erigon-lib/state" @@ -90,8 +91,7 @@ func TestAccessList(t *testing.T) { slot := common.HexToHash _, tx, _ := NewTestTemporalDb(t) - - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() diff --git a/core/state/database_test.go b/core/state/database_test.go index 4ca21c21839..e188a027006 100644 --- a/core/state/database_test.go +++ b/core/state/database_test.go @@ -31,6 +31,7 @@ import ( "github.com/stretchr/testify/require" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/crypto" @@ -924,7 +925,7 @@ func TestReproduceCrash(t *testing.T) { value2 := uint256.NewInt(0x58c00a51) _, tx, _ := state.NewTestTemporalDb(t) - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) t.Cleanup(sd.Close) @@ -1344,7 +1345,7 @@ func TestChangeAccountCodeBetweenBlocks(t *testing.T) { contract := libcommon.HexToAddress("0x71dd1027069078091B3ca48093B00E4735B20624") _, tx, _ := state.NewTestTemporalDb(t) - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) t.Cleanup(sd.Close) @@ -1394,7 +1395,7 @@ func TestCacheCodeSizeSeparately(t *testing.T) { //root := libcommon.HexToHash("0xb939e5bcf5809adfb87ab07f0795b05b95a1d64a90f0eddd0c3123ac5b433854") _, tx, _ := state.NewTestTemporalDb(t) - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) t.Cleanup(sd.Close) @@ -1432,7 +1433,7 @@ func TestCacheCodeSizeInTrie(t *testing.T) { root := libcommon.HexToHash("0xb939e5bcf5809adfb87ab07f0795b05b95a1d64a90f0eddd0c3123ac5b433854") _, tx, _ := state.NewTestTemporalDb(t) - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) t.Cleanup(sd.Close) diff --git a/core/state/intra_block_state_logger_test.go b/core/state/intra_block_state_logger_test.go index 64cc73d59f6..85d35cd51a6 100644 --- a/core/state/intra_block_state_logger_test.go +++ b/core/state/intra_block_state_logger_test.go @@ -24,6 +24,7 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/kv/rawdbv3" "github.com/erigontech/erigon-lib/log/v3" @@ -116,7 +117,7 @@ func TestStateLogger(t *testing.T) { t.Run(tt.name, func(t *testing.T) { _, tx, _ := NewTestTemporalDb(t) - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() diff --git a/core/state/intra_block_state_test.go b/core/state/intra_block_state_test.go index 0c463a473c7..437fa9ac1d4 100644 --- a/core/state/intra_block_state_test.go +++ b/core/state/intra_block_state_test.go @@ -36,6 +36,7 @@ import ( "github.com/holiman/uint256" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/kv/memdb" @@ -261,7 +262,7 @@ func (test *snapshotTest) run() bool { } defer tx.Rollback() - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { test.err = err return false diff --git a/core/state/state_test.go b/core/state/state_test.go index c1a3e561f08..201b5376672 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -29,6 +29,7 @@ import ( checker "gopkg.in/check.v1" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/crypto" @@ -139,7 +140,7 @@ func (s *StateSuite) SetUpTest(c *checker.C) { } defer tx.Rollback() - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { panic(err) } @@ -252,7 +253,7 @@ func TestSnapshot2(t *testing.T) { t.Parallel() _, tx, _ := NewTestTemporalDb(t) - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -416,7 +417,7 @@ func TestDump(t *testing.T) { t.Parallel() _, tx, _ := NewTestTemporalDb(t) - domains, err := state.NewSharedDomains(tx, log.New()) + domains, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() diff --git a/core/test/domains_restart_test.go b/core/test/domains_restart_test.go index cb542d46b18..713d0f9b2d8 100644 --- a/core/test/domains_restart_test.go +++ b/core/test/domains_restart_test.go @@ -33,6 +33,7 @@ import ( "github.com/stretchr/testify/require" "github.com/erigontech/erigon-lib/chain/networkname" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/length" @@ -103,7 +104,7 @@ func Test_AggregatorV3_RestartOnDatadir_WithoutDB(t *testing.T) { domCtx := agg.BeginFilesRo() defer domCtx.Close() - domains, err := state.NewSharedDomains(tx, log.New()) + domains, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() domains.SetTxNum(0) @@ -215,7 +216,7 @@ func Test_AggregatorV3_RestartOnDatadir_WithoutDB(t *testing.T) { require.NoError(t, err) domCtx = agg.BeginFilesRo() defer domCtx.Close() - domains, err = state.NewSharedDomains(tx, log.New()) + domains, err = state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -249,7 +250,7 @@ func Test_AggregatorV3_RestartOnDatadir_WithoutDB(t *testing.T) { defer tx.Rollback() domCtx = agg.BeginFilesRo() defer domCtx.Close() - domains, err = state.NewSharedDomains(tx, log.New()) + domains, err = state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() writer = state2.NewWriterV4(domains) @@ -309,7 +310,7 @@ func Test_AggregatorV3_RestartOnDatadir_WithoutAnything(t *testing.T) { domCtx := agg.BeginFilesRo() defer domCtx.Close() - domains, err := state.NewSharedDomains(tx, log.New()) + domains, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() domains.SetTxNum(0) @@ -399,7 +400,7 @@ func Test_AggregatorV3_RestartOnDatadir_WithoutAnything(t *testing.T) { domCtx = agg.BeginFilesRo() defer domCtx.Close() - domains, err = state.NewSharedDomains(tx, log.New()) + domains, err = state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -419,7 +420,7 @@ func Test_AggregatorV3_RestartOnDatadir_WithoutAnything(t *testing.T) { defer tx.Rollback() domCtx = agg.BeginFilesRo() defer domCtx.Close() - domains, err = state.NewSharedDomains(tx, log.New()) + domains, err = state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -486,7 +487,7 @@ func TestCommit(t *testing.T) { domCtx := agg.BeginFilesRo() defer domCtx.Close() - domains, err := state.NewSharedDomains(tx, log.New()) + domains, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() diff --git a/core/vm/gas_table_test.go b/core/vm/gas_table_test.go index 3a2bd414611..a10b6341b4d 100644 --- a/core/vm/gas_table_test.go +++ b/core/vm/gas_table_test.go @@ -31,6 +31,7 @@ import ( "github.com/holiman/uint256" "github.com/stretchr/testify/require" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/hexutil" @@ -118,7 +119,7 @@ func testTemporalTxSD(t *testing.T, db *temporal.DB) (kv.RwTx, *state3.SharedDom require.NoError(t, err) t.Cleanup(tx.Rollback) - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) t.Cleanup(sd.Close) @@ -200,7 +201,7 @@ func TestCreateGas(t *testing.T) { eface := *(*[2]uintptr)(unsafe.Pointer(&tx)) fmt.Printf("init tx %x\n", eface[1]) - domains, err := state3.NewSharedDomains(txc.Tx, log.New()) + domains, err := state3.NewSharedDomains(txc.Tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() txc.Doms = domains diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index cdf3d26dd89..ae810795efb 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -30,6 +30,7 @@ import ( "github.com/holiman/uint256" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/config3" @@ -145,7 +146,7 @@ func Execute(code, input []byte, cfg *Config, tempdir string) ([]byte, *state.In return nil, nil, err } defer tx.Rollback() - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return nil, nil, err } @@ -207,7 +208,7 @@ func Create(input []byte, cfg *Config, blockNr uint64) ([]byte, libcommon.Addres return nil, [20]byte{}, 0, err } defer tx.Rollback() - sd, err := state3.NewSharedDomains(tx, log.New()) + sd, err := state3.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return nil, [20]byte{}, 0, err } diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index 9df1c03717b..c85b0ed49e3 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -31,6 +31,7 @@ import ( "github.com/stretchr/testify/require" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/kv" @@ -147,7 +148,7 @@ func TestExecute(t *testing.T) { func TestCall(t *testing.T) { t.Parallel() _, tx, _ := NewTestTemporalDb(t) - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() state := state.New(state.NewReaderV3(domains)) @@ -191,7 +192,7 @@ func testTemporalTxSD(t testing.TB, db *temporal.DB) (kv.RwTx, *stateLib.SharedD require.NoError(t, err) t.Cleanup(tx.Rollback) - sd, err := stateLib.NewSharedDomains(tx, log.New()) + sd, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) t.Cleanup(sd.Close) @@ -243,7 +244,7 @@ func benchmarkEVM_Create(b *testing.B, code string) { db := testTemporalDB(b) tx, err := db.BeginTemporalRw(context.Background()) require.NoError(b, err) - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(b, err) defer domains.Close() @@ -467,7 +468,7 @@ func benchmarkNonModifyingCode(b *testing.B, gas uint64, code []byte, name strin db := testTemporalDB(b) tx, err := db.BeginTemporalRw(context.Background()) require.NoError(b, err) - domains, err := stateLib.NewSharedDomains(tx, log.New()) + domains, err := stateLib.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(b, err) defer domains.Close() diff --git a/erigon-lib/commitment/commitment.go b/erigon-lib/commitment/commitment.go index a6a6d61d2af..343833f0fd7 100644 --- a/erigon-lib/commitment/commitment.go +++ b/erigon-lib/commitment/commitment.go @@ -22,12 +22,13 @@ import ( "encoding/binary" "errors" "fmt" - "github.com/erigontech/erigon-lib/types/accounts" "math/bits" "sort" "strings" "unsafe" + "github.com/erigontech/erigon-lib/types/accounts" + "github.com/holiman/uint256" "github.com/google/btree" @@ -120,11 +121,13 @@ type TrieVariant string const ( // VariantHexPatriciaTrie used as default commitment approach VariantHexPatriciaTrie TrieVariant = "hex-patricia-hashed" + // VariantHexPatriciaTrieReader used as default commitment reader + VariantHexPatriciaTrieReader TrieVariant = "reader-hex-patricia-hashed" // VariantBinPatriciaTrie - Experimental mode with binary key representation VariantBinPatriciaTrie TrieVariant = "bin-patricia-hashed" ) -func InitializeTrieAndUpdates(tv TrieVariant, mode Mode, tmpdir string) (Trie, *Updates) { +func InitializeTrieAndUpdates(tv TrieVariant, mode Mode, tmpdir string) (Trie, *ReadsOrUpdates) { switch tv { case VariantBinPatriciaTrie: //trie := NewBinPatriciaHashed(length.Addr, nil, tmpdir) @@ -132,12 +135,13 @@ func InitializeTrieAndUpdates(tv TrieVariant, mode Mode, tmpdir string) (Trie, * //tree := NewUpdateTree(mode, tmpdir, fn) //return trie, tree panic("omg its not supported") + case VariantHexPatriciaTrieReader: + return NewHexPatriciaHashedReader(length.Addr, nil, tmpdir), NewReadsOrUpdates(mode, tmpdir, KeyToHexNibbleHash) case VariantHexPatriciaTrie: fallthrough default: - trie := NewHexPatriciaHashed(length.Addr, nil, tmpdir) - tree := NewUpdates(mode, tmpdir, KeyToHexNibbleHash) + tree := NewReadsOrUpdates(mode, tmpdir, KeyToHexNibbleHash) return trie, tree } } @@ -957,7 +961,7 @@ func ParseCommitmentMode(s string) Mode { return mode } -type Updates struct { +type ReadsOrUpdates struct { keccak cryptozerocopy.KeccakState hasher keyHasher keys map[string]struct{} @@ -967,12 +971,15 @@ type Updates struct { tmpdir string } +type Reads = ReadsOrUpdates +type Updates = ReadsOrUpdates + type keyHasher func(key []byte) []byte func keyHasherNoop(key []byte) []byte { return key } -func NewUpdates(m Mode, tmpdir string, hasher keyHasher) *Updates { - t := &Updates{ +func NewReadsOrUpdates(m Mode, tmpdir string, hasher keyHasher) *ReadsOrUpdates { + t := &ReadsOrUpdates{ keccak: sha3.NewLegacyKeccak256().(cryptozerocopy.KeccakState), hasher: hasher, tmpdir: tmpdir, @@ -986,7 +993,8 @@ func NewUpdates(m Mode, tmpdir string, hasher keyHasher) *Updates { } return t } -func (t *Updates) SetMode(m Mode) { + +func (t *ReadsOrUpdates) SetMode(m Mode) { t.mode = m if t.mode == ModeDirect && t.keys == nil { t.keys = make(map[string]struct{}) @@ -997,7 +1005,7 @@ func (t *Updates) SetMode(m Mode) { t.Reset() } -func (t *Updates) initCollector() { +func (t *ReadsOrUpdates) initCollector() { if t.etl != nil { t.etl.Close() t.etl = nil @@ -1007,9 +1015,9 @@ func (t *Updates) initCollector() { t.etl.SortAndFlushInBackground(true) } -func (t *Updates) Mode() Mode { return t.mode } +func (t *ReadsOrUpdates) Mode() Mode { return t.mode } -func (t *Updates) Size() (updates uint64) { +func (t *ReadsOrUpdates) Size() (updates uint64) { switch t.mode { case ModeDirect: return uint64(len(t.keys)) @@ -1022,7 +1030,7 @@ func (t *Updates) Size() (updates uint64) { // TouchPlainKey marks plainKey as updated and applies different fn for different key types // (different behaviour for Code, Account and Storage key modifications). -func (t *Updates) TouchPlainKey(key string, val []byte, fn func(c *KeyUpdate, val []byte)) { +func (t *ReadsOrUpdates) TouchPlainKey(key string, val []byte, fn func(c *KeyUpdate, val []byte)) { switch t.mode { case ModeUpdate: pivot, updated := &KeyUpdate{plainKey: key, update: new(Update)}, false @@ -1051,7 +1059,7 @@ func (t *Updates) TouchPlainKey(key string, val []byte, fn func(c *KeyUpdate, va } } -func (t *Updates) TouchAccount(c *KeyUpdate, val []byte) { +func (t *ReadsOrUpdates) TouchAccount(c *KeyUpdate, val []byte) { if len(val) == 0 { c.update.Flags = DeleteUpdate return @@ -1083,7 +1091,7 @@ func (t *Updates) TouchAccount(c *KeyUpdate, val []byte) { } } -func (t *Updates) TouchStorage(c *KeyUpdate, val []byte) { +func (t *ReadsOrUpdates) TouchStorage(c *KeyUpdate, val []byte) { c.update.StorageLen = len(val) if len(val) == 0 { c.update.Flags = DeleteUpdate @@ -1093,7 +1101,7 @@ func (t *Updates) TouchStorage(c *KeyUpdate, val []byte) { } } -func (t *Updates) TouchCode(c *KeyUpdate, val []byte) { +func (t *ReadsOrUpdates) TouchCode(c *KeyUpdate, val []byte) { c.update.Flags |= CodeUpdate if len(val) == 0 { if c.update.Flags == 0 { @@ -1107,7 +1115,7 @@ func (t *Updates) TouchCode(c *KeyUpdate, val []byte) { t.keccak.Read(c.update.CodeHash[:]) } -func (t *Updates) Close() { +func (t *ReadsOrUpdates) Close() { if t.keys != nil { clear(t.keys) } @@ -1121,7 +1129,7 @@ func (t *Updates) Close() { } // HashSort sorts and applies fn to each key-value pair in the order of hashed keys. -func (t *Updates) HashSort(ctx context.Context, fn func(hk, pk []byte, update *Update) error) error { +func (t *ReadsOrUpdates) HashSort(ctx context.Context, fn func(hk, pk []byte, update *Update) error) error { switch t.mode { case ModeDirect: clear(t.keys) @@ -1155,7 +1163,7 @@ func (t *Updates) HashSort(ctx context.Context, fn func(hk, pk []byte, update *U } // Reset clears all updates -func (t *Updates) Reset() { +func (t *ReadsOrUpdates) Reset() { switch t.mode { case ModeDirect: t.keys = nil diff --git a/erigon-lib/commitment/commitment_test.go b/erigon-lib/commitment/commitment_test.go index abe9d0afee7..7f10eb96690 100644 --- a/erigon-lib/commitment/commitment_test.go +++ b/erigon-lib/commitment/commitment_test.go @@ -302,11 +302,11 @@ func TestBranchData_ReplacePlainKeys_WithEmpty(t *testing.T) { }) } -func TestNewUpdates(t *testing.T) { +func TestNewReadsOrUpdates(t *testing.T) { t.Parallel() t.Run("ModeUpdate", func(t *testing.T) { - ut := NewUpdates(ModeUpdate, t.TempDir(), keyHasherNoop) + ut := NewReadsOrUpdates(ModeUpdate, t.TempDir(), keyHasherNoop) require.NotNil(t, ut.tree) require.NotNil(t, ut.keccak) @@ -315,7 +315,7 @@ func TestNewUpdates(t *testing.T) { }) t.Run("ModeDirect", func(t *testing.T) { - ut := NewUpdates(ModeDirect, t.TempDir(), keyHasherNoop) + ut := NewReadsOrUpdates(ModeDirect, t.TempDir(), keyHasherNoop) require.NotNil(t, ut.keccak) require.NotNil(t, ut.keys) @@ -327,8 +327,8 @@ func TestNewUpdates(t *testing.T) { func TestUpdates_TouchPlainKey(t *testing.T) { t.Parallel() - utUpdate := NewUpdates(ModeUpdate, t.TempDir(), keyHasherNoop) - utDirect := NewUpdates(ModeDirect, t.TempDir(), keyHasherNoop) + utUpdate := NewReadsOrUpdates(ModeUpdate, t.TempDir(), keyHasherNoop) + utDirect := NewReadsOrUpdates(ModeDirect, t.TempDir(), keyHasherNoop) type tc struct { key []byte diff --git a/erigon-lib/commitment/hex_patricia_hashed.go b/erigon-lib/commitment/hex_patricia_hashed.go index af61f965104..37069f2d1b3 100644 --- a/erigon-lib/commitment/hex_patricia_hashed.go +++ b/erigon-lib/commitment/hex_patricia_hashed.go @@ -1185,7 +1185,7 @@ func (hph *HexPatriciaHashed) nCellsInRow(row int) int { //nolint:unused } // Traverse the grid following `hashedKey` and produce the witness `trie.Trie` for that key -func (hph *HexPatriciaHashed) ToTrie(hashedKey []byte, codeReads map[libcommon.Hash]witnesstypes.CodeWithHash) (*trie.Trie, error) { +func (hph *HexPatriciaHashedReader) ToTrie(hashedKey []byte, codeReads map[libcommon.Hash]witnesstypes.CodeWithHash) (*trie.Trie, error) { rootNode := &trie.FullNode{} var currentNode trie.Node = rootNode keyPos := 0 // current position in hashedKey (usually same as row, but could be different due to extension nodes) @@ -1884,26 +1884,48 @@ func (hph *HexPatriciaHashed) RootHash() ([]byte, error) { return rootHash[1:], nil // first byte is 128+hash_len=160 } +func (hph *HexPatriciaHashedReader) PrintGrid() { + fmt.Printf("GRID:\n") + for row := 0; row < hph.activeRows; row++ { + fmt.Printf("row %d depth %d:\n", row, hph.depths[row]) + for col := 0; col < 16; col++ { + cell := &hph.grid[row][col] + if cell.hashedExtLen > 0 || cell.accountAddrLen > 0 { + var cellHash []byte + cellHash, _, _, err := hph.computeCellHashWithStorage(cell, hph.depths[row], nil) + if err != nil { + panic("failed to compute cell hash") + } + fmt.Printf("\t %x: %v cellHash=%x, \n", col, cell, cellHash) + } else { + fmt.Printf("\t %x: %v , \n", col, cell) + } + } + fmt.Printf("\n") + } + fmt.Printf("\n") +} + // Generate the block witness. This works by loading each key from the list of updates (they are not really updates since we won't modify the trie, // but currently need to be defined like that for the fold/unfold algorithm) into the grid and traversing the grid to convert it into `trie.Trie`. // All the individual tries are combined to create the final witness trie. // Because the grid is lacking information about the code in smart contract accounts which is also part of the witness, we need to provide that as an input parameter to this function (`codeReads`) -func (hph *HexPatriciaHashed) GenerateWitness(ctx context.Context, updates *Updates, codeReads map[libcommon.Hash]witnesstypes.CodeWithHash, expectedRootHash []byte, logPrefix string) (witnessTrie *trie.Trie, rootHash []byte, err error) { +func (hph *HexPatriciaHashedReader) GenerateWitness(ctx context.Context, reads *Reads, codeReads map[libcommon.Hash]witnesstypes.CodeWithHash, expectedRootHash []byte, logPrefix string) (witnessTrie *trie.Trie, rootHash []byte, err error) { var ( m runtime.MemStats ki uint64 - updatesCount = updates.Size() - logEvery = time.NewTicker(20 * time.Second) + readCount = reads.Size() + logEvery = time.NewTicker(20 * time.Second) ) defer logEvery.Stop() - var tries []*trie.Trie = make([]*trie.Trie, 0, len(updates.keys)) // slice of tries, i.e the witness for each key, these will be all merged into single trie - err = updates.HashSort(ctx, func(hashedKey, plainKey []byte, stateUpdate *Update) error { + var tries []*trie.Trie = make([]*trie.Trie, 0, len(reads.keys)) // slice of tries, i.e the witness for each key, these will be all merged into single trie + err = reads.HashSort(ctx, func(hashedKey, plainKey []byte, stateUpdate *Update) error { select { case <-logEvery.C: dbg.ReadMemStats(&m) log.Info(fmt.Sprintf("[%s][agg] computing trie", logPrefix), - "progress", fmt.Sprintf("%s/%s", common.PrettyCounter(ki), common.PrettyCounter(updatesCount)), + "progress", fmt.Sprintf("%s/%s", common.PrettyCounter(ki), common.PrettyCounter(readCount)), "alloc", common.ByteCount(m.Alloc), "sys", common.ByteCount(m.Sys)) default: @@ -1912,8 +1934,7 @@ func (hph *HexPatriciaHashed) GenerateWitness(ctx context.Context, updates *Upda var tr *trie.Trie var computedRootHash []byte - fmt.Printf("\n%d/%d) plainKey [%x] hashedKey [%x] currentKey [%x]\n", ki+1, updatesCount, plainKey, hashedKey, hph.currentKey[:hph.currentKeyLen]) - + fmt.Printf("\n%d/%d) plainKey [%x] hashedKey [%x] currentKey [%x]\n", ki+1, readCount, plainKey, hashedKey, hph.currentKey[:hph.currentKeyLen]) if len(plainKey) == 20 { // account account, err := hph.ctx.Account(plainKey) if err != nil { @@ -1929,7 +1950,6 @@ func (hph *HexPatriciaHashed) GenerateWitness(ctx context.Context, updates *Upda } fmt.Printf("storage found = %v\n", storage.Storage) } - // Keep folding until the currentKey is the prefix of the key we modify for hph.needFolding(hashedKey) { if err := hph.fold(); err != nil { @@ -1978,7 +1998,7 @@ func (hph *HexPatriciaHashed) GenerateWitness(ctx context.Context, updates *Upda return nil, nil, fmt.Errorf("root hash evaluation failed: %w", err) } if hph.trace { - fmt.Printf("root hash %x updates %d\n", rootHash, updatesCount) + fmt.Printf("root hash %x reads %d\n", rootHash, readCount) } // merge all individual tries @@ -2810,31 +2830,80 @@ func (hph *HexPatriciaHashedReader) needFolding(hashedKey []byte) bool { return !bytes.HasPrefix(hashedKey, hph.currentKey[:hph.currentKeyLen]) } -func (hph *HexPatriciaHashedReader) Traverse(ctx context.Context, updates *Updates, logPrefix string) (rootHash []byte, err error) { +func (hph *HexPatriciaHashedReader) Process(ctx context.Context, updates *Updates, logPrefix string) (rootHash []byte, err error) { + panic("Process must not be called for HexPatriciaHashedReader") +} + +// this function is only related to the witness +func (hph *HexPatriciaHashedReader) createAccountNode(c *cell, row int, hashedKey []byte, codeReads map[libcommon.Hash]witnesstypes.CodeWithHash) (*trie.AccountNode, error) { + _, storageIsSet, storageRootHash, err := hph.computeCellHashWithStorage(c, hph.depths[row], nil) + if err != nil { + return nil, err + } + accountUpdate, err := hph.ctx.Account(c.accountAddr[:c.accountAddrLen]) + if err != nil { + return nil, err + } + var account accounts.Account + account.Nonce = accountUpdate.Nonce + account.Balance = accountUpdate.Balance + account.Initialised = true + account.Root = accountUpdate.Storage + account.CodeHash = accountUpdate.CodeHash + + addrHash, err := compactKey(hashedKey[:64]) + if err != nil { + return nil, err + } + + // get code + var code []byte + codeWithHash, hasCode := codeReads[[32]byte(addrHash)] + if !hasCode { + code = nil + } else { + code = codeWithHash.Code + // sanity check + if account.CodeHash != codeWithHash.CodeHash { + return nil, fmt.Errorf("account.CodeHash(%x)!=codeReads[%x].CodeHash(%x)", account.CodeHash, addrHash, codeWithHash.CodeHash) + } + } + + var accountNode *trie.AccountNode + if !storageIsSet { + account.Root = trie.EmptyRoot + accountNode = &trie.AccountNode{Account: account, Storage: nil, RootCorrect: true, Code: code, CodeSize: -1} + } else { + accountNode = &trie.AccountNode{Account: account, Storage: trie.NewHashNode(storageRootHash), RootCorrect: true, Code: code, CodeSize: -1} + } + return accountNode, nil +} + +func (hph *HexPatriciaHashedReader) Traverse(ctx context.Context, reads *Reads, logPrefix string) (rootHash []byte, err error) { var ( m runtime.MemStats ki uint64 update *Update - updatesCount = updates.Size() - logEvery = time.NewTicker(20 * time.Second) + readCount = reads.Size() + logEvery = time.NewTicker(20 * time.Second) ) defer logEvery.Stop() //hph.trace = true - err = updates.HashSort(ctx, func(hashedKey, plainKey []byte, stateUpdate *Update) error { + err = reads.HashSort(ctx, func(hashedKey, plainKey []byte, stateUpdate *Update) error { select { case <-logEvery.C: dbg.ReadMemStats(&m) log.Info(fmt.Sprintf("[%s][agg] computing trie", logPrefix), - "progress", fmt.Sprintf("%s/%s", common.PrettyCounter(ki), common.PrettyCounter(updatesCount)), + "progress", fmt.Sprintf("%s/%s", common.PrettyCounter(ki), common.PrettyCounter(readCount)), "alloc", common.ByteCount(m.Alloc), "sys", common.ByteCount(m.Sys)) default: } if hph.trace { - fmt.Printf("\n%d/%d) plainKey [%x] hashedKey [%x] currentKey [%x]\n", ki+1, updatesCount, plainKey, hashedKey, hph.currentKey[:hph.currentKeyLen]) + fmt.Printf("\n%d/%d) plainKey [%x] hashedKey [%x] currentKey [%x]\n", ki+1, readCount, plainKey, hashedKey, hph.currentKey[:hph.currentKeyLen]) } // Keep folding until the currentKey is the prefix of the key we modify for hph.needFolding(hashedKey) { @@ -2892,7 +2961,7 @@ func (hph *HexPatriciaHashedReader) Traverse(ctx context.Context, updates *Updat return nil, fmt.Errorf("root hash evaluation failed: %w", err) } if hph.trace { - fmt.Printf("root hash %x updates %d\n", rootHash, updatesCount) + fmt.Printf("root hash %x reads %d\n", rootHash, readCount) } return rootHash, nil } diff --git a/erigon-lib/commitment/patricia_state_mock_test.go b/erigon-lib/commitment/patricia_state_mock_test.go index c1bb9712f88..65351e0be0b 100644 --- a/erigon-lib/commitment/patricia_state_mock_test.go +++ b/erigon-lib/commitment/patricia_state_mock_test.go @@ -412,7 +412,7 @@ func (ub *UpdateBuilder) Build() (plainKeys [][]byte, updates []Update) { func WrapKeyUpdates(tb testing.TB, mode Mode, hasher keyHasher, keys [][]byte, updates []Update) *Updates { tb.Helper() - upd := NewUpdates(mode, tb.TempDir(), hasher) + upd := NewReadsOrUpdates(mode, tb.TempDir(), hasher) for i, key := range keys { upd.TouchPlainKey(string(key), nil, func(c *KeyUpdate, _ []byte) { c.plainKey = string(key) diff --git a/erigon-lib/kv/kvcache/cache_test.go b/erigon-lib/kv/kvcache/cache_test.go index eb0cc41bb05..f637a014257 100644 --- a/erigon-lib/kv/kvcache/cache_test.go +++ b/erigon-lib/kv/kvcache/cache_test.go @@ -20,13 +20,15 @@ import ( "context" "encoding/binary" "fmt" - "github.com/erigontech/erigon-lib/types/accounts" "runtime" "sync" "sync/atomic" "testing" "time" + "github.com/erigontech/erigon-lib/commitment" + "github.com/erigontech/erigon-lib/types/accounts" + "github.com/holiman/uint256" "github.com/stretchr/testify/require" @@ -227,7 +229,7 @@ func TestAPI(t *testing.T) { var txID uint64 require.NoError(db.Update(context.Background(), func(tx kv.RwTx) error { txID = tx.ViewID() - d, err := state.NewSharedDomains(tx, log.New()) + d, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return err } diff --git a/erigon-lib/state/aggregator_bench_test.go b/erigon-lib/state/aggregator_bench_test.go index 3ae00f393cb..95817e3d554 100644 --- a/erigon-lib/state/aggregator_bench_test.go +++ b/erigon-lib/state/aggregator_bench_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/log/v3" @@ -81,7 +82,7 @@ func BenchmarkAggregator_Processing(b *testing.B) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(b, err) defer domains.Close() diff --git a/erigon-lib/state/aggregator_fuzz_test.go b/erigon-lib/state/aggregator_fuzz_test.go index 470aa21fe23..26a33e95ea8 100644 --- a/erigon-lib/state/aggregator_fuzz_test.go +++ b/erigon-lib/state/aggregator_fuzz_test.go @@ -21,10 +21,12 @@ package state import ( "context" "encoding/binary" - "github.com/erigontech/erigon-lib/types/accounts" "testing" "time" + "github.com/erigontech/erigon-lib/commitment" + "github.com/erigontech/erigon-lib/types/accounts" + "github.com/c2h5oh/datasize" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" @@ -64,7 +66,7 @@ func Fuzz_AggregatorV3_Merge(f *testing.F) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(f, err) defer domains.Close() @@ -191,7 +193,7 @@ func Fuzz_AggregatorV3_MergeValTransform(f *testing.F) { }() ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(f, err) defer domains.Close() diff --git a/erigon-lib/state/aggregator_test.go b/erigon-lib/state/aggregator_test.go index f120961bad7..da969f1f207 100644 --- a/erigon-lib/state/aggregator_test.go +++ b/erigon-lib/state/aggregator_test.go @@ -22,7 +22,6 @@ import ( "encoding/binary" "encoding/hex" "fmt" - "github.com/erigontech/erigon-lib/types/accounts" "math" "math/rand" "os" @@ -33,6 +32,8 @@ import ( "testing" "time" + "github.com/erigontech/erigon-lib/types/accounts" + "github.com/c2h5oh/datasize" "github.com/erigontech/erigon-lib/commitment" @@ -66,7 +67,7 @@ func TestAggregatorV3_Merge(t *testing.T) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -187,7 +188,7 @@ func TestAggregatorV3_MergeValTransform(t *testing.T) { }() ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -318,7 +319,7 @@ func aggregatorV3_RestartOnDatadir(t *testing.T, rc runCfg) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -395,7 +396,7 @@ func aggregatorV3_RestartOnDatadir(t *testing.T, rc runCfg) { startTx := anotherAgg.EndTxNumMinimax() ac2 := anotherAgg.BeginFilesRo() defer ac2.Close() - dom2, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac2), log.New()) + dom2, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac2), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer dom2.Close() @@ -461,7 +462,7 @@ func TestAggregatorV3_PruneSmallBatches(t *testing.T) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -793,7 +794,7 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) { }() ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -865,7 +866,7 @@ func TestAggregatorV3_RestartOnFiles(t *testing.T) { ac = newAgg.BeginFilesRo() defer ac.Close() - newDoms, err := NewSharedDomains(WrapTxWithCtx(newTx, ac), log.New()) + newDoms, err := NewSharedDomains(WrapTxWithCtx(newTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer newDoms.Close() @@ -923,7 +924,7 @@ func TestAggregatorV3_ReplaceCommittedKeys(t *testing.T) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(tx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -937,7 +938,7 @@ func TestAggregatorV3_ReplaceCommittedKeys(t *testing.T) { tx, err = db.BeginRw(context.Background()) require.NoError(t, err) ac = agg.BeginFilesRo() - domains, err = NewSharedDomains(WrapTxWithCtx(tx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(tx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) atomic.StoreUint64(&latestCommitTxNum, txn) return nil @@ -1176,7 +1177,7 @@ func TestAggregatorV3_SharedDomains(t *testing.T) { require.NoError(t, err) defer rwTx.Rollback() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() changesetAt5 := &StateChangeSet{} @@ -1228,7 +1229,7 @@ func TestAggregatorV3_SharedDomains(t *testing.T) { ac = agg.BeginFilesRo() defer ac.Close() - domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() diffs := [kv.DomainLen][]DomainEntryDiff{} @@ -1273,7 +1274,7 @@ func TestAggregatorV3_SharedDomains(t *testing.T) { ac = agg.BeginFilesRo() defer ac.Close() - domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() for idx := range changesetAt3.Diffs { diff --git a/erigon-lib/state/domain_shared.go b/erigon-lib/state/domain_shared.go index b0578ab0290..3ef00b00e6e 100644 --- a/erigon-lib/state/domain_shared.go +++ b/erigon-lib/state/domain_shared.go @@ -23,7 +23,6 @@ import ( "encoding/binary" "encoding/hex" "fmt" - "github.com/erigontech/erigon-lib/types/accounts" "math" "path/filepath" "runtime" @@ -31,6 +30,8 @@ import ( "time" "unsafe" + "github.com/erigontech/erigon-lib/types/accounts" + "github.com/erigontech/erigon-lib/seg" "github.com/erigontech/erigon-lib/trie" "github.com/pkg/errors" @@ -112,7 +113,7 @@ type HasAgg interface { Agg() any } -func NewSharedDomains(tx kv.Tx, logger log.Logger) (*SharedDomains, error) { +func NewSharedDomains(tx kv.Tx, trieVariant commitment.TrieVariant, logger log.Logger) (*SharedDomains, error) { sd := &SharedDomains{ logger: logger, @@ -134,7 +135,7 @@ func NewSharedDomains(tx kv.Tx, logger log.Logger) (*SharedDomains, error) { } sd.SetTxNum(0) - sd.sdCtx = NewSharedDomainsCommitmentContext(sd, commitment.ModeDirect, commitment.VariantHexPatriciaTrie) + sd.sdCtx = NewSharedDomainsCommitmentContext(sd, commitment.ModeDirect, trieVariant) if _, err := sd.SeekCommitment(context.Background(), tx); err != nil { return nil, err @@ -383,7 +384,7 @@ func (sd *SharedDomains) ClearRam(resetCommitment bool) { sd.domains[i] = map[string]dataWithPrevStep{} } if resetCommitment { - sd.sdCtx.updates.Reset() + sd.sdCtx.readsOrUpdates.Reset() sd.sdCtx.Reset() } @@ -1097,12 +1098,12 @@ func (sd *SharedDomains) DomainDelPrefix(domain kv.Domain, prefix []byte) error func (sd *SharedDomains) Tx() kv.Tx { return sd.roTx } type SharedDomainsCommitmentContext struct { - sharedDomains *SharedDomains - branches map[string]cachedBranch - keccak cryptozerocopy.KeccakState - updates *commitment.Updates - patriciaTrie commitment.Trie - justRestored atomic.Bool + sharedDomains *SharedDomains + branches map[string]cachedBranch + keccak cryptozerocopy.KeccakState + readsOrUpdates *commitment.Updates + patriciaTrie commitment.Trie + justRestored atomic.Bool limitReadAsOfTxNum uint64 } @@ -1118,13 +1119,13 @@ func NewSharedDomainsCommitmentContext(sd *SharedDomains, mode commitment.Mode, keccak: sha3.NewLegacyKeccak256().(cryptozerocopy.KeccakState), } - ctx.patriciaTrie, ctx.updates = commitment.InitializeTrieAndUpdates(trieVariant, mode, sd.aggTx.a.tmpdir) + ctx.patriciaTrie, ctx.readsOrUpdates = commitment.InitializeTrieAndUpdates(trieVariant, mode, sd.aggTx.a.tmpdir) ctx.patriciaTrie.ResetContext(ctx) return ctx } func (sdc *SharedDomainsCommitmentContext) Close() { - sdc.updates.Close() + sdc.readsOrUpdates.Close() } type cachedBranch struct { @@ -1295,7 +1296,7 @@ func (sdc *SharedDomainsCommitmentContext) TempDir() string { } func (sdc *SharedDomainsCommitmentContext) KeysCount() uint64 { - return sdc.updates.Size() + return sdc.readsOrUpdates.Size() } func (sdc *SharedDomainsCommitmentContext) Trie() commitment.Trie { @@ -1305,29 +1306,29 @@ func (sdc *SharedDomainsCommitmentContext) Trie() commitment.Trie { // TouchPlainKey marks plainKey as updated and applies different fn for different key types // (different behaviour for Code, Account and Storage key modifications). func (sdc *SharedDomainsCommitmentContext) TouchKey(d kv.Domain, key string, val []byte) { - if sdc.updates.Mode() == commitment.ModeDisabled { + if sdc.readsOrUpdates.Mode() == commitment.ModeDisabled { return } switch d { case kv.AccountsDomain: - sdc.updates.TouchPlainKey(key, val, sdc.updates.TouchAccount) + sdc.readsOrUpdates.TouchPlainKey(key, val, sdc.readsOrUpdates.TouchAccount) case kv.CodeDomain: - sdc.updates.TouchPlainKey(key, val, sdc.updates.TouchCode) + sdc.readsOrUpdates.TouchPlainKey(key, val, sdc.readsOrUpdates.TouchCode) case kv.StorageDomain: - sdc.updates.TouchPlainKey(key, val, sdc.updates.TouchStorage) + sdc.readsOrUpdates.TouchPlainKey(key, val, sdc.readsOrUpdates.TouchStorage) default: panic(fmt.Errorf("TouchKey: unknown domain %s", d)) } } func (sdc *SharedDomainsCommitmentContext) Witness(ctx context.Context, expectedRoot []byte, logPrefix string) (proofTrie *trie.Trie, rootHash []byte, err error) { - hexPatriciaHashed, ok := sdc.Trie().(*commitment.HexPatriciaHashed) + hexPatriciaHashedReader, ok := sdc.Trie().(*commitment.HexPatriciaHashedReader) if ok { - return hexPatriciaHashed.GenerateWitness(ctx, sdc.updates, nil, expectedRoot, logPrefix) + return hexPatriciaHashedReader.GenerateWitness(ctx, sdc.readsOrUpdates, nil, expectedRoot, logPrefix) } - return nil, nil, errors.New("shared domains commitment context doesn't have HexPatriciaHashed") + return nil, nil, errors.New("shared domains commitment context doesn't have HexPatriciaHashedReader") } // Evaluates commitment for processed state. @@ -1339,9 +1340,9 @@ func (sdc *SharedDomainsCommitmentContext) ComputeCommitment(ctx context.Context defer mxCommitmentRunning.Dec() defer func(s time.Time) { mxCommitmentTook.ObserveDuration(s) }(time.Now()) - updateCount := sdc.updates.Size() + updateCount := sdc.readsOrUpdates.Size() if sdc.sharedDomains.trace { - defer sdc.sharedDomains.logger.Trace("ComputeCommitment", "block", blockNum, "keys", updateCount, "mode", sdc.updates.Mode()) + defer sdc.sharedDomains.logger.Trace("ComputeCommitment", "block", blockNum, "keys", updateCount, "mode", sdc.readsOrUpdates.Mode()) } if updateCount == 0 { rootHash, err = sdc.patriciaTrie.RootHash() @@ -1352,7 +1353,7 @@ func (sdc *SharedDomainsCommitmentContext) ComputeCommitment(ctx context.Context sdc.patriciaTrie.SetTrace(sdc.sharedDomains.trace) sdc.Reset() - rootHash, err = sdc.patriciaTrie.Process(ctx, sdc.updates, logPrefix) + rootHash, err = sdc.patriciaTrie.Process(ctx, sdc.readsOrUpdates, logPrefix) if err != nil { return nil, err } @@ -1434,7 +1435,7 @@ func _decodeTxBlockNums(v []byte) (txNum, blockNum uint64) { // LatestCommitmentState searches for last encoded state for CommitmentContext. // Found value does not become current state. func (sdc *SharedDomainsCommitmentContext) LatestCommitmentState() (blockNum, txNum uint64, state []byte, err error) { - if sdc.patriciaTrie.Variant() != commitment.VariantHexPatriciaTrie { + if sdc.patriciaTrie.Variant() != commitment.VariantHexPatriciaTrie && sdc.patriciaTrie.Variant() != commitment.VariantHexPatriciaTrieReader { return 0, 0, nil, fmt.Errorf("state storing is only supported hex patricia trie") } state, _, err = sdc.Branch(keyCommitmentState) @@ -1462,8 +1463,8 @@ func (sdc *SharedDomainsCommitmentContext) SeekCommitment(tx kv.Tx, cd *DomainRo // After commitment state is retored, method .Reset() should NOT be called until new updates. // Otherwise state should be restorePatriciaState()d again. - func (sdc *SharedDomainsCommitmentContext) restorePatriciaState(value []byte) (uint64, uint64, error) { + fmt.Println("adana") cs := new(commitmentState) if err := cs.Decode(value); err != nil { if len(value) > 0 { @@ -1471,7 +1472,15 @@ func (sdc *SharedDomainsCommitmentContext) restorePatriciaState(value []byte) (u } // nil value is acceptable for SetState and will reset trie } - if hext, ok := sdc.patriciaTrie.(*commitment.HexPatriciaHashed); ok { + + _, isHexPatriciaHashed := sdc.patriciaTrie.(*commitment.HexPatriciaHashed) + _, isHexPatriciaHashedReader := sdc.patriciaTrie.(*commitment.HexPatriciaHashedReader) + if !isHexPatriciaHashedReader && !isHexPatriciaHashed { + return 0, 0, errors.New("state storing is only supported hex patricia trie") + } + + if isHexPatriciaHashed { + hext, _ := sdc.patriciaTrie.(*commitment.HexPatriciaHashed) if err := hext.SetState(cs.trieState); err != nil { return 0, 0, fmt.Errorf("failed restore state : %w", err) } @@ -1484,8 +1493,20 @@ func (sdc *SharedDomainsCommitmentContext) restorePatriciaState(value []byte) (u fmt.Printf("[commitment] restored state: block=%d txn=%d rootHash=%x\n", cs.blockNum, cs.txNum, rootHash) } } else { - return 0, 0, errors.New("state storing is only supported hex patricia trie") + hext, _ := sdc.patriciaTrie.(*commitment.HexPatriciaHashedReader) + if err := hext.SetState(cs.trieState); err != nil { + return 0, 0, fmt.Errorf("failed restore state : %w", err) + } + sdc.justRestored.Store(true) // to prevent double reset + if sdc.sharedDomains.trace { + rootHash, err := hext.RootHash() + if err != nil { + return 0, 0, fmt.Errorf("failed to get root hash after state restore: %w", err) + } + fmt.Printf("[commitment] restored state: block=%d txn=%d rootHash=%x\n", cs.blockNum, cs.txNum, rootHash) + } } + return cs.blockNum, cs.txNum, nil } diff --git a/erigon-lib/state/domain_shared_bench_test.go b/erigon-lib/state/domain_shared_bench_test.go index f5157b4c17a..91c4fbb8dd9 100644 --- a/erigon-lib/state/domain_shared_bench_test.go +++ b/erigon-lib/state/domain_shared_bench_test.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common/length" "github.com/erigontech/erigon-lib/kv" "github.com/erigontech/erigon-lib/log/v3" @@ -40,7 +41,7 @@ func Benchmark_SharedDomains_GetLatest(t *testing.B) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() maxTx := stepSize * 258 @@ -127,7 +128,7 @@ func BenchmarkSharedDomains_ComputeCommitment(b *testing.B) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(b, err) defer domains.Close() diff --git a/erigon-lib/state/domain_shared_test.go b/erigon-lib/state/domain_shared_test.go index 7fa9ea86e3a..34c04e9efb1 100644 --- a/erigon-lib/state/domain_shared_test.go +++ b/erigon-lib/state/domain_shared_test.go @@ -20,11 +20,13 @@ import ( "context" "encoding/binary" "fmt" - "github.com/erigontech/erigon-lib/common" - accounts3 "github.com/erigontech/erigon-lib/types/accounts" "testing" "time" + "github.com/erigontech/erigon-lib/commitment" + "github.com/erigontech/erigon-lib/common" + accounts3 "github.com/erigontech/erigon-lib/types/accounts" + "github.com/holiman/uint256" "github.com/stretchr/testify/require" @@ -48,7 +50,7 @@ func TestSharedDomain_CommitmentKeyReplacement(t *testing.T) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -93,7 +95,7 @@ func TestSharedDomain_CommitmentKeyReplacement(t *testing.T) { defer rwTx.Rollback() // 4. restart on same (replaced keys) files - domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -123,7 +125,7 @@ func TestSharedDomain_Unwind(t *testing.T) { ac := agg.BeginFilesRo() defer ac.Close() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -145,7 +147,7 @@ Loop: ac = agg.BeginFilesRo() defer ac.Close() - domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -244,7 +246,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { ac = agg.BeginFilesRo() defer ac.Close() wrwTx := WrapTxWithCtx(rwTx, ac) - domains, err := NewSharedDomains(wrwTx, log.New()) + domains, err := NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() @@ -274,7 +276,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { require.NoError(err) domains.Close() - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() require.Equal(int(stepSize), iterCount(domains)) @@ -282,7 +284,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { { // delete marker is in RAM require.NoError(domains.Flush(ctx, rwTx)) domains.Close() - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() require.Equal(int(stepSize), iterCount(domains)) @@ -312,7 +314,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { require.NoError(err) domains.Close() - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() require.Equal(int(stepSize*2+2-2), iterCount(domains)) @@ -337,7 +339,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { require.NoError(err) wrwTx = WrapTxWithCtx(rwTx, ac) - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() require.Equal(int(stepSize*2+2-2), iterCount(domains)) @@ -346,7 +348,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { { // delete/update more keys in RAM require.NoError(domains.Flush(ctx, rwTx)) domains.Close() - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() @@ -366,7 +368,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { require.NoError(err) domains.Close() - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() require.Equal(int(stepSize*2+2-3), iterCount(domains)) @@ -376,7 +378,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) { require.NoError(err) domains.Close() - domains, err = NewSharedDomains(wrwTx, log.New()) + domains, err = NewSharedDomains(wrwTx, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(err) defer domains.Close() domains.SetTxNum(domains.TxNum() + 1) @@ -403,14 +405,14 @@ func TestSharedDomain_StorageIter(t *testing.T) { defer ac.Close() wtxRw := WrapTxWithCtx(rwTx, ac) - domains, err := NewSharedDomains(wtxRw, log.New()) + domains, err := NewSharedDomains(wtxRw, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() maxTx := 3*stepSize + 10 hashes := make([][]byte, maxTx) - domains, err = NewSharedDomains(wtxRw, log.New()) + domains, err = NewSharedDomains(wtxRw, commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -488,7 +490,7 @@ func TestSharedDomain_StorageIter(t *testing.T) { rwTx, err = db.BeginRw(ctx) require.NoError(t, err) - domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() diff --git a/erigon-lib/state/squeeze.go b/erigon-lib/state/squeeze.go index 73b11e34f63..c60d82b23c5 100644 --- a/erigon-lib/state/squeeze.go +++ b/erigon-lib/state/squeeze.go @@ -13,6 +13,7 @@ import ( "time" "github.com/c2h5oh/datasize" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/dir" @@ -455,7 +456,7 @@ func (a *Aggregator) RebuildCommitmentFiles(ctx context.Context, rwDb kv.RwDB, t } defer rwTx.Rollback() - domains, err = NewSharedDomains(rwTx, log.New()) + domains, err = NewSharedDomains(rwTx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return nil, err } @@ -470,7 +471,7 @@ func (a *Aggregator) RebuildCommitmentFiles(ctx context.Context, rwDb kv.RwDB, t // case when we do testing and temporal db with aggtx is not available ac = a.BeginFilesRo() - domains, err = NewSharedDomains(wrapTxWithCtxForTest(roTx, ac), log.New()) + domains, err = NewSharedDomains(wrapTxWithCtxForTest(roTx, ac), commitment.VariantHexPatriciaTrie, log.New()) if err != nil { ac.Close() return nil, err diff --git a/erigon-lib/state/squeeze_test.go b/erigon-lib/state/squeeze_test.go index e5529f378d4..c58316ae6ec 100644 --- a/erigon-lib/state/squeeze_test.go +++ b/erigon-lib/state/squeeze_test.go @@ -2,11 +2,12 @@ package state import ( "context" - "github.com/erigontech/erigon-lib/common" - accounts3 "github.com/erigontech/erigon-lib/types/accounts" "math" "testing" + "github.com/erigontech/erigon-lib/common" + accounts3 "github.com/erigontech/erigon-lib/types/accounts" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common/length" "github.com/erigontech/erigon-lib/kv" @@ -37,7 +38,7 @@ func testDbAggregatorWithFiles(tb testing.TB, cfg *testAggConfig) (kv.RwDB, *Agg require.NoError(tb, err) defer rwTx.Rollback() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(tb, err) defer domains.Close() @@ -95,7 +96,7 @@ func TestAggregator_SqueezeCommitment(t *testing.T) { require.NoError(t, err) defer rwTx.Rollback() - domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) defer domains.Close() @@ -117,7 +118,7 @@ func TestAggregator_SqueezeCommitment(t *testing.T) { ac = agg.BeginFilesRo() defer ac.Close() - domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New()) + domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), commitment.VariantHexPatriciaTrie, log.New()) require.NoError(t, err) // collect account keys to trigger commitment diff --git a/eth/stagedsync/exec3.go b/eth/stagedsync/exec3.go index cb5977476ec..31a39ea0ce2 100644 --- a/eth/stagedsync/exec3.go +++ b/eth/stagedsync/exec3.go @@ -28,6 +28,7 @@ import ( "time" "github.com/erigontech/erigon-lib/chain/networkname" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/cmp" "github.com/erigontech/erigon-lib/common/dbg" @@ -255,7 +256,7 @@ func ExecV3(ctx context.Context, doms = txc.Doms } else { var err error - doms, err = state2.NewSharedDomains(applyTx, log.New()) + doms, err = state2.NewSharedDomains(applyTx, commitment.VariantHexPatriciaTrie, log.New()) // if we are behind the commitment, we can't execute anything // this can heppen if progress in domain is higher than progress in blocks if errors.Is(err, state2.ErrBehindCommitment) { diff --git a/eth/stagedsync/exec3_serial.go b/eth/stagedsync/exec3_serial.go index 9fc62f3d96c..95dbdc1229f 100644 --- a/eth/stagedsync/exec3_serial.go +++ b/eth/stagedsync/exec3_serial.go @@ -8,6 +8,7 @@ import ( chaos_monkey "github.com/erigontech/erigon/tests/chaos-monkey" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/log/v3" state2 "github.com/erigontech/erigon-lib/state" "github.com/erigontech/erigon/consensus" @@ -155,7 +156,7 @@ func (se *serialExecutor) commit(ctx context.Context, txNum uint64, blockNum uin return t2, err } } - se.doms, err = state2.NewSharedDomains(se.applyTx, se.logger) + se.doms, err = state2.NewSharedDomains(se.applyTx, commitment.VariantHexPatriciaTrie, se.logger) if err != nil { return t2, err } diff --git a/eth/stagedsync/stage_custom_trace.go b/eth/stagedsync/stage_custom_trace.go index f7f41f25f7e..1e9160ddf9e 100644 --- a/eth/stagedsync/stage_custom_trace.go +++ b/eth/stagedsync/stage_custom_trace.go @@ -23,6 +23,7 @@ import ( "time" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/dbg" @@ -129,7 +130,7 @@ func customTraceBatchProduce(ctx context.Context, cfg *exec3.ExecArgs, db kv.RwD var lastTxNum uint64 if err := db.Update(ctx, func(tx kv.RwTx) error { ttx := tx.(kv.TemporalRwTx) - doms, err := state2.NewSharedDomains(tx, logger) + doms, err := state2.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } diff --git a/eth/stagedsync/stage_execute.go b/eth/stagedsync/stage_execute.go index c8db035c8ef..e08ca280832 100644 --- a/eth/stagedsync/stage_execute.go +++ b/eth/stagedsync/stage_execute.go @@ -29,6 +29,7 @@ import ( "github.com/erigontech/erigon/cmd/state/exec3" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/dbg" @@ -170,7 +171,7 @@ var ErrTooDeepUnwind = errors.New("too deep unwind") func unwindExec3(u *UnwindState, s *StageState, txc wrap.TxContainer, ctx context.Context, br services.FullBlockReader, accumulator *shards.Accumulator, logger log.Logger) (err error) { var domains *libstate.SharedDomains if txc.Doms == nil { - domains, err = libstate.NewSharedDomains(txc.Tx, logger) + domains, err = libstate.NewSharedDomains(txc.Tx, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } diff --git a/eth/stagedsync/stage_execute_test.go b/eth/stagedsync/stage_execute_test.go index f2f58c9d098..88362d451cd 100644 --- a/eth/stagedsync/stage_execute_test.go +++ b/eth/stagedsync/stage_execute_test.go @@ -19,6 +19,7 @@ package stagedsync import ( "context" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/kv" "github.com/erigontech/erigon-lib/log/v3" libstate "github.com/erigontech/erigon-lib/state" @@ -27,7 +28,7 @@ import ( ) func apply(tx kv.RwTx, logger log.Logger) (beforeBlock, afterBlock testGenHook, w state.StateWriter) { - domains, err := libstate.NewSharedDomains(tx, logger) + domains, err := libstate.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) if err != nil { panic(err) } diff --git a/eth/stagedsync/stage_headers.go b/eth/stagedsync/stage_headers.go index 24dcc9a71cf..55b2ac3f205 100644 --- a/eth/stagedsync/stage_headers.go +++ b/eth/stagedsync/stage_headers.go @@ -26,6 +26,7 @@ import ( "github.com/c2h5oh/datasize" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/log/v3" "github.com/erigontech/erigon-lib/chain" @@ -313,7 +314,7 @@ Loop: } if headerInserter.Unwind() { unwindTo := headerInserter.UnwindPoint() - doms, err := state.NewSharedDomains(tx, logger) //TODO: if remove this line TestBlockchainHeaderchainReorgConsistency failing + doms, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) //TODO: if remove this line TestBlockchainHeaderchainReorgConsistency failing if err != nil { return err } diff --git a/eth/stagedsync/stage_mining_exec.go b/eth/stagedsync/stage_mining_exec.go index 32f611c2f87..48ee6a4f834 100644 --- a/eth/stagedsync/stage_mining_exec.go +++ b/eth/stagedsync/stage_mining_exec.go @@ -27,6 +27,7 @@ import ( "golang.org/x/net/context" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/metrics" "github.com/erigontech/erigon-lib/kv" @@ -137,7 +138,7 @@ func SpawnMiningExecStage(s *StageState, txc wrap.TxContainer, cfg MiningExecCfg mb := membatchwithdb.NewMemoryBatch(txc.Tx, cfg.tmpdir, logger) defer mb.Close() - sd, err := state2.NewSharedDomains(mb, logger) + sd, err := state2.NewSharedDomains(mb, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 285db0ff9ee..39993b3a303 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -34,6 +34,7 @@ import ( "golang.org/x/crypto/sha3" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/hexutility" @@ -206,7 +207,7 @@ func (t *StateTest) RunNoVerify(tx kv.RwTx, subtest StateSubtest, vmconfig vm.Co var txc wrap.TxContainer txc.Tx = tx - domains, err := state2.NewSharedDomains(tx, log.New()) + domains, err := state2.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return nil, libcommon.Hash{}, UnsupportedForkError{subtest.Fork} } @@ -321,7 +322,7 @@ func MakePreState(rules *chain.Rules, tx kv.RwTx, accounts types.GenesisAlloc, b var txc wrap.TxContainer txc.Tx = tx - domains, err := state2.NewSharedDomains(tx, log.New()) + domains, err := state2.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return nil, err } diff --git a/turbo/engineapi/engine_helpers/fork_validator.go b/turbo/engineapi/engine_helpers/fork_validator.go index 87fd2ddf7ae..5d6a4217d6e 100644 --- a/turbo/engineapi/engine_helpers/fork_validator.go +++ b/turbo/engineapi/engine_helpers/fork_validator.go @@ -23,6 +23,7 @@ import ( "sync" "time" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/math" "github.com/erigontech/erigon-lib/kv" @@ -264,7 +265,7 @@ func (fv *ForkValidator) ValidatePayload(tx kv.RwTx, header *types.Header, body if fv.sharedDom != nil { fv.sharedDom.Close() } - fv.sharedDom, criticalError = state.NewSharedDomains(tx, logger) + fv.sharedDom, criticalError = state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) if criticalError != nil { criticalError = fmt.Errorf("failed to create shared domains: %w", criticalError) return diff --git a/turbo/execution/eth1/forkchoice.go b/turbo/execution/eth1/forkchoice.go index 6da592bd324..d02d8c15da2 100644 --- a/turbo/execution/eth1/forkchoice.go +++ b/turbo/execution/eth1/forkchoice.go @@ -23,6 +23,7 @@ import ( "runtime" "time" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/dbg" "github.com/erigontech/erigon-lib/common/metrics" @@ -70,7 +71,7 @@ func sendForkchoiceErrorWithoutWaiting(logger log.Logger, ch chan forkchoiceOutc } func isDomainAheadOfBlocks(tx kv.RwTx) bool { - doms, err := state.NewSharedDomains(tx, log.New()) + doms, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, log.New()) if err != nil { return errors.Is(err, state.ErrBehindCommitment) } diff --git a/turbo/jsonrpc/eth_call.go b/turbo/jsonrpc/eth_call.go index 6796af3ac24..b1de673f755 100644 --- a/turbo/jsonrpc/eth_call.go +++ b/turbo/jsonrpc/eth_call.go @@ -365,7 +365,7 @@ func (api *APIImpl) getProof(ctx context.Context, roTx *kv.Tx, address libcommon return nil, err } - domains, err := libstate.NewSharedDomains(*roTx, log.New()) + domains, err := libstate.NewSharedDomains(*roTx, commitment.VariantHexPatriciaTrieReader, log.New()) if err != nil { return nil, err } @@ -619,15 +619,14 @@ func (api *BaseAPI) getWitness(ctx context.Context, db kv.RoDB, blockNrOrHash rp return nil, err } - domains, err := libstate.NewSharedDomains(txBatch2, log.New()) + domains, err := libstate.NewSharedDomains(txBatch2, commitment.VariantHexPatriciaTrieReader, log.New()) if err != nil { return nil, err } - sdCtx := libstate.NewSharedDomainsCommitmentContext(domains, commitment.ModeUpdate, commitment.VariantHexPatriciaTrie) - patricieTrie := sdCtx.Trie() - hph, ok := patricieTrie.(*commitment.HexPatriciaHashed) + + hph, ok := domains.GetCommitmentContext().Trie().(*commitment.HexPatriciaHashedReader) if !ok { - return nil, errors.New("casting to HexPatriciaTrieHashed failed") + return nil, errors.New("casting to HexPatriciaTrieHashedReader failed") } // execute block #blockNr ephemerally. This will use TrieStateWriter to record touches of accounts and storage keys. @@ -642,7 +641,7 @@ func (api *BaseAPI) getWitness(ctx context.Context, db kv.RoDB, blockNrOrHash rp // define these keys as "updates", but we are not really updating anything, we just want to load them into the grid, // so this is just to satisfy the current hex patricia trie api. - updates := commitment.NewUpdates(commitment.ModeDirect, sdCtx.TempDir(), commitment.KeyToHexNibbleHash) + updates := commitment.NewReadsOrUpdates(commitment.ModeDirect, domains.GetCommitmentContext().TempDir(), commitment.KeyToHexNibbleHash) for _, key := range touchedPlainKeys { updates.TouchPlainKey(string(key), nil, updates.TouchAccount) } diff --git a/turbo/stages/stageloop.go b/turbo/stages/stageloop.go index 79934ad14f9..d8de071600a 100644 --- a/turbo/stages/stageloop.go +++ b/turbo/stages/stageloop.go @@ -26,6 +26,7 @@ import ( lru "github.com/hashicorp/golang-lru/arc/v2" "github.com/erigontech/erigon-lib/chain" + "github.com/erigontech/erigon-lib/commitment" libcommon "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/dbg" @@ -519,7 +520,7 @@ func MiningStep(ctx context.Context, db kv.RwDB, mining *stagedsync.Sync, tmpDir defer mb.Close() txc := wrap.TxContainer{Tx: mb} - sd, err := state.NewSharedDomains(mb, logger) + sd, err := state.NewSharedDomains(mb, commitment.VariantHexPatriciaTrie, logger) if err != nil { return err } diff --git a/txnprovider/txpool/pool_test.go b/txnprovider/txpool/pool_test.go index 456e2da0b4c..e697b1714ae 100644 --- a/txnprovider/txpool/pool_test.go +++ b/txnprovider/txpool/pool_test.go @@ -20,16 +20,18 @@ import ( "bytes" "context" "fmt" - "github.com/erigontech/erigon-lib/state" - accounts3 "github.com/erigontech/erigon-lib/types/accounts" "math" "math/big" "testing" + "github.com/erigontech/erigon-lib/state" + accounts3 "github.com/erigontech/erigon-lib/types/accounts" + "github.com/holiman/uint256" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/erigontech/erigon-lib/commitment" "github.com/erigontech/erigon-lib/common" "github.com/erigontech/erigon-lib/common/datadir" "github.com/erigontech/erigon-lib/common/fixedgas" @@ -930,7 +932,7 @@ func TestShanghaiValidateTxn(t *testing.T) { tx, err := coreDB.BeginTemporalRw(ctx) defer tx.Rollback() asrt.NoError(err) - sd, err := state.NewSharedDomains(tx, logger) + sd, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) asrt.NoError(err) defer sd.Close() cache := kvcache.NewDummy() @@ -1052,7 +1054,7 @@ func TestSetCodeTxnValidationWithLargeAuthorizationValues(t *testing.T) { tx, err := coreDB.BeginRw(ctx) defer tx.Rollback() assert.NoError(t, err) - sd, err := state.NewSharedDomains(tx, logger) + sd, err := state.NewSharedDomains(tx, commitment.VariantHexPatriciaTrie, logger) assert.NoError(t, err) defer sd.Close()