From 04b5c0c67b8a0d3a6830ef7c1cb4aabcda172454 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Fri, 31 Mar 2023 09:19:56 +0700 Subject: [PATCH] e4: small tests preparations (#7220) --- accounts/abi/bind/backends/simulated.go | 21 ++++--------- core/genesis_write.go | 40 +++++++++++++++---------- core/state/temporal/kv_temporal.go | 4 ++- 3 files changed, 34 insertions(+), 31 deletions(-) diff --git a/accounts/abi/bind/backends/simulated.go b/accounts/abi/bind/backends/simulated.go index b4627f0d2..522fcf846 100644 --- a/accounts/abi/bind/backends/simulated.go +++ b/accounts/abi/bind/backends/simulated.go @@ -179,20 +179,6 @@ func (b *SimulatedBackend) emptyPendingBlock() { b.pendingReceipts = chain.Receipts[0] b.pendingHeader = chain.Headers[0] b.gasPool = new(core.GasPool).AddGas(b.pendingHeader.GasLimit) - if ethconfig.EnableHistoryV4InTest { - panic("implement domain state reader") - /* - agg := db.(*temporal.DB).GetAgg() - agg.SetTx(tx) - - rs := state.NewStateV3("", agg.BufferedDomains()) - stateWriter = state.NewStateWriterV3(rs) - r := state.NewStateReaderV3(rs) - r.SetTx(tx) - stateReader = r - defer agg.StartUnbufferedWrites().FinishWrites() - */ - } if b.pendingReaderTx != nil { b.pendingReaderTx.Rollback() } @@ -201,7 +187,12 @@ func (b *SimulatedBackend) emptyPendingBlock() { panic(err) } b.pendingReaderTx = tx - b.pendingReader = state.NewPlainStateReader(b.pendingReaderTx) + if ethconfig.EnableHistoryV4InTest { + panic("implement me") + //b.pendingReader = state.NewReaderV4(b.pendingReaderTx.(kv.TemporalTx)) + } else { + b.pendingReader = state.NewPlainStateReader(b.pendingReaderTx) + } b.pendingState = state.New(b.pendingReader) } diff --git a/core/genesis_write.go b/core/genesis_write.go index df586b1ec..c379b0529 100644 --- a/core/genesis_write.go +++ b/core/genesis_write.go @@ -40,6 +40,7 @@ import ( "github.com/ledgerwatch/erigon/core/state" "github.com/ledgerwatch/erigon/core/types" "github.com/ledgerwatch/erigon/crypto" + "github.com/ledgerwatch/erigon/eth/ethconfig" "github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/params/networkname" "github.com/ledgerwatch/erigon/turbo/trie" @@ -175,31 +176,40 @@ func WriteGenesisState(g *types.Genesis, tx kv.RwTx, tmpDir string) (*types.Bloc if err != nil { return nil, nil, err } - for addr, account := range g.Alloc { - if len(account.Code) > 0 || len(account.Storage) > 0 { - // Special case for weird tests - inaccessible storage - var b [8]byte - binary.BigEndian.PutUint64(b[:], state.FirstContractIncarnation) - if err := tx.Put(kv.IncarnationMap, addr[:], b[:]); err != nil { - return nil, nil, err + var stateWriter state.StateWriter + if ethconfig.EnableHistoryV4InTest { + panic("implement me") + //tx.(*temporal.Tx).Agg().SetTxNum(0) + //stateWriter = state.NewWriterV4(tx.(kv.TemporalTx)) + //defer tx.(*temporal.Tx).Agg().StartUnbufferedWrites().FinishWrites() + } else { + for addr, account := range g.Alloc { + if len(account.Code) > 0 || len(account.Storage) > 0 { + // Special case for weird tests - inaccessible storage + var b [8]byte + binary.BigEndian.PutUint64(b[:], state.FirstContractIncarnation) + if err := tx.Put(kv.IncarnationMap, addr[:], b[:]); err != nil { + return nil, nil, err + } } } + stateWriter = state.NewPlainStateWriter(tx, tx, 0) } if block.Number().Sign() != 0 { return nil, statedb, fmt.Errorf("can't commit genesis block with number > 0") } - blockWriter := state.NewPlainStateWriter(tx, tx, 0) - - if err := statedb.CommitBlock(&chain.Rules{}, blockWriter); err != nil { + if err := statedb.CommitBlock(&chain.Rules{}, stateWriter); err != nil { return nil, statedb, fmt.Errorf("cannot write state: %w", err) } - if err := blockWriter.WriteChangeSets(); err != nil { - return nil, statedb, fmt.Errorf("cannot write change sets: %w", err) - } - if err := blockWriter.WriteHistory(); err != nil { - return nil, statedb, fmt.Errorf("cannot write history: %w", err) + if csw, ok := stateWriter.(state.WriterWithChangeSets); ok { + if err := csw.WriteChangeSets(); err != nil { + return nil, statedb, fmt.Errorf("cannot write change sets: %w", err) + } + if err := csw.WriteHistory(); err != nil { + return nil, statedb, fmt.Errorf("cannot write history: %w", err) + } } return block, statedb, nil } diff --git a/core/state/temporal/kv_temporal.go b/core/state/temporal/kv_temporal.go index 733350416..cc89bc71d 100644 --- a/core/state/temporal/kv_temporal.go +++ b/core/state/temporal/kv_temporal.go @@ -71,7 +71,7 @@ func New(db kv.RwDB, agg *state.AggregatorV3, cb1 tConvertV3toV2, cb2 tRestoreCo return &DB{RwDB: db, agg: agg, convertV3toV2: cb1, restoreCodeHash: cb2, parseInc: cb3, systemContractLookup: systemContractLookup}, nil } -func (db *DB) GetAgg() *state.AggregatorV3 { return db.agg } +func (db *DB) Agg() *state.AggregatorV3 { return db.agg } func (db *DB) BeginTemporalRo(ctx context.Context) (kv.TemporalTx, error) { kvTx, err := db.RwDB.BeginRo(ctx) @@ -162,6 +162,8 @@ type Tx struct { resourcesToClose []kv.Closer } +func (tx *Tx) AggCtx() *state.AggregatorV3Context { return tx.agg } +func (tx *Tx) Agg() *state.AggregatorV3 { return tx.db.agg } func (tx *Tx) Rollback() { for _, closer := range tx.resourcesToClose { closer.Close()