From dd3663a39c9922717a5f08077fdb59e422bdd50a Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Mon, 30 Dec 2024 21:17:35 +0700 Subject: [PATCH] commitment domain: make `.kvi` default index (#13276) --- erigon-lib/state/aggregator.go | 12 +++++++++++- erigon-lib/state/aggregator_test.go | 21 ++++++++++++++++++--- erigon-lib/state/domain.go | 4 ++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/erigon-lib/state/aggregator.go b/erigon-lib/state/aggregator.go index 6123509a46d..4eb8d9cd0fe 100644 --- a/erigon-lib/state/aggregator.go +++ b/erigon-lib/state/aggregator.go @@ -126,6 +126,16 @@ func domainIntegrityCheck(name kv.Domain, dirs datadir.Dirs, fromStep, toStep ui } } +var dbgCommBtIndex = dbg.EnvBool("AGG_COMMITMENT_BT", false) + +func init() { + if dbgCommBtIndex { + cfg := Schema[kv.CommitmentDomain] + cfg.indexList = withBTree | withExistence + Schema[kv.CommitmentDomain] = cfg + } +} + var Schema = map[kv.Domain]domainCfg{ kv.AccountsDomain: { name: kv.AccountsDomain, valuesTable: kv.TblAccountVals, @@ -195,7 +205,7 @@ var Schema = map[kv.Domain]domainCfg{ kv.CommitmentDomain: { name: kv.CommitmentDomain, valuesTable: kv.TblCommitmentVals, - indexList: withBTree | withExistence, + indexList: withHashMap, compression: seg.CompressKeys, compressCfg: DomainCompressCfg, diff --git a/erigon-lib/state/aggregator_test.go b/erigon-lib/state/aggregator_test.go index 444f3a57bbe..2df5b21c9f6 100644 --- a/erigon-lib/state/aggregator_test.go +++ b/erigon-lib/state/aggregator_test.go @@ -1273,9 +1273,24 @@ func TestAggregator_RebuildCommitmentBasedOnFiles(t *testing.T) { compression := ac.d[kv.CommitmentDomain].d.compression fnames := []string{} for _, f := range ac.d[kv.CommitmentDomain].files { - k, stateVal, _, found, err := f.src.bindex.Get(keyCommitmentState, seg.NewReader(f.src.decompressor.MakeGetter(), compression)) - require.NoError(t, err) - require.True(t, found) + var k, stateVal []byte + if ac.d[kv.CommitmentDomain].d.indexList&withHashMap != 0 { + idx := f.src.index.GetReaderFromPool() + r := seg.NewReader(f.src.decompressor.MakeGetter(), compression) + + offset, ok := idx.TwoLayerLookup(keyCommitmentState) + require.True(t, ok) + r.Reset(offset) + k, _ = r.Next(nil) + stateVal, _ = r.Next(nil) + } else { + var found bool + var err error + k, stateVal, _, found, err = f.src.bindex.Get(keyCommitmentState, seg.NewReader(f.src.decompressor.MakeGetter(), compression)) + require.NoError(t, err) + require.True(t, found) + require.EqualValues(t, keyCommitmentState, k) + } require.EqualValues(t, keyCommitmentState, k) rh, err := commitment.HexTrieExtractStateRoot(stateVal) require.NoError(t, err) diff --git a/erigon-lib/state/domain.go b/erigon-lib/state/domain.go index a9caa460ad6..3d9fb444dc9 100644 --- a/erigon-lib/state/domain.go +++ b/erigon-lib/state/domain.go @@ -1055,7 +1055,7 @@ func (d *Domain) buildFileRange(ctx context.Context, stepFrom, stepTo uint64, co if err = d.buildAccessor(ctx, stepFrom, stepTo, valuesDecomp, ps); err != nil { return StaticFiles{}, fmt.Errorf("build %s values idx: %w", d.filenameBase, err) } - valuesIdx, err = recsplit.OpenIndex(d.efAccessorFilePath(stepFrom, stepTo)) + valuesIdx, err = recsplit.OpenIndex(d.kvAccessorFilePath(stepFrom, stepTo)) if err != nil { return StaticFiles{}, err } @@ -1158,7 +1158,7 @@ func (d *Domain) buildFiles(ctx context.Context, step uint64, collation Collatio if err = d.buildAccessor(ctx, step, step+1, valuesDecomp, ps); err != nil { return StaticFiles{}, fmt.Errorf("build %s values idx: %w", d.filenameBase, err) } - valuesIdx, err = recsplit.OpenIndex(d.efAccessorFilePath(step, step+1)) + valuesIdx, err = recsplit.OpenIndex(d.kvAccessorFilePath(step, step+1)) if err != nil { return StaticFiles{}, err }