Skip to content

Commit

Permalink
Replace ReaderV4 by ReaderV3 (erigontech#11600)
Browse files Browse the repository at this point in the history
- renamed `state.StateReaderV3` to `state.ReaderV3`
- removed `ReaderV4`
  • Loading branch information
AskAlexSharov authored Aug 14, 2024
1 parent 593abd4 commit 12d7e76
Show file tree
Hide file tree
Showing 18 changed files with 73 additions and 241 deletions.
5 changes: 3 additions & 2 deletions cmd/evm/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/erigontech/erigon-lib/common/datadir"
"io"
"math/big"
"os"
Expand All @@ -33,6 +32,8 @@ import (
"testing"
"time"

"github.com/erigontech/erigon-lib/common/datadir"

"github.com/holiman/uint256"
"github.com/urfave/cli/v2"

Expand Down Expand Up @@ -176,7 +177,7 @@ func runCmd(ctx *cli.Context) error {
return err
}
defer sd.Close()
stateReader := state.NewStateReaderV3(sd)
stateReader := state.NewReaderV3(sd)
statedb = state.New(stateReader)
if ctx.String(SenderFlag.Name) != "" {
sender = libcommon.HexToAddress(ctx.String(SenderFlag.Name))
Expand Down
2 changes: 1 addition & 1 deletion cmd/integration/commands/state_domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func requestDomains(chainDb, stateDb kv.RwDB, ctx context.Context, readDomain st
}
defer agg.Close()

r := state.NewReaderV4(domains)
r := state.NewReaderV3(domains)
if err != nil && startTxNum != 0 {
return fmt.Errorf("failed to seek commitment to txn %d: %w", startTxNum, err)
}
Expand Down
14 changes: 6 additions & 8 deletions cmd/state/exec3/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ func NewWorker(lock sync.Locker, logger log.Logger, ctx context.Context, backgro
func (rw *Worker) ResetState(rs *state.StateV3, accumulator *shards.Accumulator) {
rw.rs = rs
if rw.background {
rw.SetReader(state.NewStateReaderParallelV3(rs.Domains()))
rw.SetReader(state.NewReaderParallelV3(rs.Domains()))
} else {
rw.SetReader(state.NewStateReaderV3(rs.Domains()))
rw.SetReader(state.NewReaderV3(rs.Domains()))
}
rw.stateWriter = state.NewStateWriterV3(rs, accumulator)
}
Expand All @@ -118,7 +118,6 @@ func (rw *Worker) ResetTx(chainTx kv.Tx) {
if chainTx != nil {
rw.chainTx = chainTx
rw.stateReader.SetTx(rw.chainTx)
rw.stateWriter.SetTx(rw.chainTx)
rw.chain = consensuschain.NewReader(rw.chainConfig, rw.chainTx, rw.blockReader, rw.logger)
}
}
Expand Down Expand Up @@ -150,7 +149,7 @@ func (rw *Worker) SetReader(reader state.ResettableStateReader) {
switch reader.(type) {
case *state.HistoryReaderV3:
rw.historyMode = true
case *state.StateReaderV3:
case *state.ReaderV3:
rw.historyMode = false
default:
rw.historyMode = false
Expand All @@ -166,9 +165,9 @@ func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask) {
rw.SetReader(state.NewHistoryReaderV3())
} else if !txTask.HistoryExecution && rw.historyMode {
if rw.background {
rw.SetReader(state.NewStateReaderParallelV3(rw.rs.Domains()))
rw.SetReader(state.NewReaderParallelV3(rw.rs.Domains()))
} else {
rw.SetReader(state.NewStateReaderV3(rw.rs.Domains()))
rw.SetReader(state.NewReaderV3(rw.rs.Domains()))
}
}
if rw.background && rw.chainTx == nil {
Expand All @@ -177,13 +176,12 @@ func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask) {
panic(err)
}
rw.stateReader.SetTx(rw.chainTx)
rw.stateWriter.SetTx(rw.chainTx)
rw.chain = consensuschain.NewReader(rw.chainConfig, rw.chainTx, rw.blockReader, rw.logger)
}
txTask.Error = nil

rw.stateReader.SetTxNum(txTask.TxNum)
rw.stateWriter.SetTxNum(rw.ctx, txTask.TxNum)
rw.rs.Domains().SetTxNum(txTask.TxNum)
rw.stateReader.ResetReadSet()
rw.stateWriter.ResetWriteSet()

Expand Down
2 changes: 1 addition & 1 deletion core/chain_makers.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func GenerateChain(config *chain.Config, parent *types.Block, engine consensus.E
return nil, err
}
defer domains.Close()
stateReader := state.NewReaderV4(domains)
stateReader := state.NewReaderV3(domains)
stateWriter := state.NewWriterV4(domains)

txNum := -1
Expand Down
2 changes: 1 addition & 1 deletion core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func GenesisToBlock(g *types.Genesis, dirs datadir.Dirs, logger log.Logger) (*ty
defer sd.Close()

//r, w := state.NewDbStateReader(tx), state.NewDbStateWriter(tx, 0)
r, w := state.NewReaderV4(sd), state.NewWriterV4(sd)
r, w := state.NewReaderV3(sd), state.NewWriterV4(sd)
statedb = state.New(r)
statedb.SetTrace(false)

Expand Down
2 changes: 1 addition & 1 deletion core/state/access_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestAccessList(t *testing.T) {
err = rawdbv3.TxNums.Append(tx, 1, 1)
require.NoError(t, err)

state := New(NewReaderV4(domains))
state := New(NewReaderV3(domains))

state.accessList = newAccessList()

Expand Down
8 changes: 4 additions & 4 deletions core/state/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ func TestReproduceCrash(t *testing.T) {
t.Cleanup(sd.Close)

tsw := state.NewWriterV4(sd)
tsr := state.NewReaderV4(sd)
tsr := state.NewReaderV3(sd)
sd.SetTxNum(1)
sd.SetBlockNum(1)

Expand Down Expand Up @@ -1256,7 +1256,7 @@ func TestChangeAccountCodeBetweenBlocks(t *testing.T) {
require.NoError(t, err)
t.Cleanup(sd.Close)

r, tsw := state.NewReaderV4(sd), state.NewWriterV4(sd)
r, tsw := state.NewReaderV3(sd), state.NewWriterV4(sd)
intraBlockState := state.New(r)
// Start the 1st transaction
intraBlockState.CreateAccount(contract, true)
Expand Down Expand Up @@ -1308,7 +1308,7 @@ func TestCacheCodeSizeSeparately(t *testing.T) {
require.NoError(t, err)
t.Cleanup(sd.Close)

r, w := state.NewReaderV4(sd), state.NewWriterV4(sd)
r, w := state.NewReaderV3(sd), state.NewWriterV4(sd)

intraBlockState := state.New(r)
// Start the 1st transaction
Expand Down Expand Up @@ -1347,7 +1347,7 @@ func TestCacheCodeSizeInTrie(t *testing.T) {
require.NoError(t, err)
t.Cleanup(sd.Close)

r, w := state.NewReaderV4(sd), state.NewWriterV4(sd)
r, w := state.NewReaderV3(sd), state.NewWriterV4(sd)

intraBlockState := state.New(r)
// Start the 1st transaction
Expand Down
4 changes: 2 additions & 2 deletions core/state/intra_block_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (test *snapshotTest) run() bool {
return false
}
var (
state = New(NewReaderV4(domains))
state = New(NewReaderV3(domains))
snapshotRevs = make([]int, len(test.snapshots))
sindex = 0
)
Expand All @@ -292,7 +292,7 @@ func (test *snapshotTest) run() bool {
// Revert all snapshots in reverse order. Each revert must yield a state
// that is equivalent to fresh state with all actions up the snapshot applied.
for sindex--; sindex >= 0; sindex-- {
checkstate := New(NewReaderV4(domains))
checkstate := New(NewReaderV3(domains))
for _, action := range test.actions[:test.snapshots[sindex]] {
action.fn(action, checkstate)
}
Expand Down
82 changes: 38 additions & 44 deletions core/state/rw_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,6 @@ type StateWriterBufferedV3 struct {
storagePrevs map[string][]byte
codePrevs map[string]uint64
accumulator *shards.Accumulator

tx kv.Tx
}

func NewStateWriterBufferedV3(rs *StateV3, accumulator *shards.Accumulator) *StateWriterBufferedV3 {
Expand All @@ -365,7 +363,7 @@ func NewStateWriterBufferedV3(rs *StateV3, accumulator *shards.Accumulator) *Sta
func (w *StateWriterBufferedV3) SetTxNum(ctx context.Context, txNum uint64) {
w.rs.domains.SetTxNum(txNum)
}
func (w *StateWriterBufferedV3) SetTx(tx kv.Tx) { w.tx = tx }
func (w *StateWriterBufferedV3) SetTx(tx kv.Tx) {}

func (w *StateWriterBufferedV3) ResetWriteSet() {
w.writeLists = newWriteList()
Expand Down Expand Up @@ -468,8 +466,6 @@ type StateWriterV3 struct {
rs *StateV3
trace bool
accumulator *shards.Accumulator

tx kv.Tx
}

func NewStateWriterV3(rs *StateV3, accumulator *shards.Accumulator) *StateWriterV3 {
Expand All @@ -480,11 +476,6 @@ func NewStateWriterV3(rs *StateV3, accumulator *shards.Accumulator) *StateWriter
}
}

func (w *StateWriterV3) SetTxNum(ctx context.Context, txNum uint64) {
w.rs.domains.SetTxNum(txNum)
}
func (w *StateWriterV3) SetTx(tx kv.Tx) { w.tx = tx }

func (w *StateWriterV3) ResetWriteSet() {}

func (w *StateWriterV3) WriteSet() map[string]*libstate.KvList {
Expand Down Expand Up @@ -577,30 +568,30 @@ func (w *StateWriterV3) CreateContract(address common.Address) error {
return nil
}

type StateReaderV3 struct {
type ReaderV3 struct {
txNum uint64
trace bool
sd *libstate.SharedDomains
tx kv.TemporalGetter
composite []byte
}

func NewStateReaderV3(sd *libstate.SharedDomains) *StateReaderV3 {
return &StateReaderV3{
func NewReaderV3(tx kv.TemporalGetter) *ReaderV3 {
return &ReaderV3{
//trace: true,
sd: sd,
tx: tx,
composite: make([]byte, 20+32),
}
}

func (r *StateReaderV3) DiscardReadList() {}
func (r *StateReaderV3) SetTxNum(txNum uint64) { r.txNum = txNum }
func (r *StateReaderV3) SetTx(tx kv.Tx) {}
func (r *StateReaderV3) ReadSet() map[string]*libstate.KvList { return nil }
func (r *StateReaderV3) SetTrace(trace bool) { r.trace = trace }
func (r *StateReaderV3) ResetReadSet() {}
func (r *ReaderV3) DiscardReadList() {}
func (r *ReaderV3) SetTxNum(txNum uint64) { r.txNum = txNum }
func (r *ReaderV3) SetTx(tx kv.Tx) {}
func (r *ReaderV3) ReadSet() map[string]*libstate.KvList { return nil }
func (r *ReaderV3) SetTrace(trace bool) { r.trace = trace }
func (r *ReaderV3) ResetReadSet() {}

func (r *StateReaderV3) ReadAccountData(address common.Address) (*accounts.Account, error) {
enc, _, err := r.sd.DomainGet(kv.AccountsDomain, address[:], nil)
func (r *ReaderV3) ReadAccountData(address common.Address) (*accounts.Account, error) {
enc, _, err := r.tx.DomainGet(kv.AccountsDomain, address[:], nil)
if err != nil {
return nil, err
}
Expand All @@ -621,9 +612,9 @@ func (r *StateReaderV3) ReadAccountData(address common.Address) (*accounts.Accou
return &acc, nil
}

func (r *StateReaderV3) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
func (r *ReaderV3) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
r.composite = append(append(r.composite[:0], address[:]...), key.Bytes()...)
enc, _, err := r.sd.DomainGet(kv.StorageDomain, r.composite, nil)
enc, _, err := r.tx.DomainGet(kv.StorageDomain, r.composite, nil)
if err != nil {
return nil, err
}
Expand All @@ -637,8 +628,11 @@ func (r *StateReaderV3) ReadAccountStorage(address common.Address, incarnation u
return enc, nil
}

func (r *StateReaderV3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
enc, _, err := r.sd.DomainGet(kv.CodeDomain, address[:], nil)
func (r *ReaderV3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
//if codeHash == emptyCodeHashH { // TODO: how often do we have this case on mainnet/bor-mainnet?
// return nil, nil
//}
enc, _, err := r.tx.DomainGet(kv.CodeDomain, address[:], nil)
if err != nil {
return nil, err
}
Expand All @@ -648,8 +642,8 @@ func (r *StateReaderV3) ReadAccountCode(address common.Address, incarnation uint
return enc, nil
}

func (r *StateReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
enc, _, err := r.sd.DomainGet(kv.CodeDomain, address[:], nil)
func (r *ReaderV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
enc, _, err := r.tx.DomainGet(kv.CodeDomain, address[:], nil)
if err != nil {
return 0, err
}
Expand All @@ -660,11 +654,11 @@ func (r *StateReaderV3) ReadAccountCodeSize(address common.Address, incarnation
return size, nil
}

func (r *StateReaderV3) ReadAccountIncarnation(address common.Address) (uint64, error) {
func (r *ReaderV3) ReadAccountIncarnation(address common.Address) (uint64, error) {
return 0, nil
}

type StateReaderParallelV3 struct {
type ReaderParallelV3 struct {
txNum uint64
trace bool
sd *libstate.SharedDomains
Expand All @@ -674,23 +668,23 @@ type StateReaderParallelV3 struct {
readLists map[string]*libstate.KvList
}

func NewStateReaderParallelV3(sd *libstate.SharedDomains) *StateReaderParallelV3 {
return &StateReaderParallelV3{
func NewReaderParallelV3(sd *libstate.SharedDomains) *ReaderParallelV3 {
return &ReaderParallelV3{
//trace: true,
sd: sd,
readLists: newReadList(),
composite: make([]byte, 20+32),
}
}

func (r *StateReaderParallelV3) DiscardReadList() { r.discardReadList = true }
func (r *StateReaderParallelV3) SetTxNum(txNum uint64) { r.txNum = txNum }
func (r *StateReaderParallelV3) SetTx(tx kv.Tx) {}
func (r *StateReaderParallelV3) ReadSet() map[string]*libstate.KvList { return r.readLists }
func (r *StateReaderParallelV3) SetTrace(trace bool) { r.trace = trace }
func (r *StateReaderParallelV3) ResetReadSet() { r.readLists = newReadList() }
func (r *ReaderParallelV3) DiscardReadList() { r.discardReadList = true }
func (r *ReaderParallelV3) SetTxNum(txNum uint64) { r.txNum = txNum }
func (r *ReaderParallelV3) SetTx(tx kv.Tx) {}
func (r *ReaderParallelV3) ReadSet() map[string]*libstate.KvList { return r.readLists }
func (r *ReaderParallelV3) SetTrace(trace bool) { r.trace = trace }
func (r *ReaderParallelV3) ResetReadSet() { r.readLists = newReadList() }

func (r *StateReaderParallelV3) ReadAccountData(address common.Address) (*accounts.Account, error) {
func (r *ReaderParallelV3) ReadAccountData(address common.Address) (*accounts.Account, error) {
enc, _, err := r.sd.DomainGet(kv.AccountsDomain, address[:], nil)
if err != nil {
return nil, err
Expand All @@ -716,7 +710,7 @@ func (r *StateReaderParallelV3) ReadAccountData(address common.Address) (*accoun
return &acc, nil
}

func (r *StateReaderParallelV3) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
func (r *ReaderParallelV3) ReadAccountStorage(address common.Address, incarnation uint64, key *common.Hash) ([]byte, error) {
r.composite = append(append(r.composite[:0], address[:]...), key.Bytes()...)
enc, _, err := r.sd.DomainGet(kv.StorageDomain, r.composite, nil)
if err != nil {
Expand All @@ -735,7 +729,7 @@ func (r *StateReaderParallelV3) ReadAccountStorage(address common.Address, incar
return enc, nil
}

func (r *StateReaderParallelV3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
func (r *ReaderParallelV3) ReadAccountCode(address common.Address, incarnation uint64, codeHash common.Hash) ([]byte, error) {
enc, _, err := r.sd.DomainGet(kv.CodeDomain, address[:], nil)
if err != nil {
return nil, err
Expand All @@ -750,7 +744,7 @@ func (r *StateReaderParallelV3) ReadAccountCode(address common.Address, incarnat
return enc, nil
}

func (r *StateReaderParallelV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
func (r *ReaderParallelV3) ReadAccountCodeSize(address common.Address, incarnation uint64, codeHash common.Hash) (int, error) {
enc, _, err := r.sd.DomainGet(kv.CodeDomain, address[:], nil)
if err != nil {
return 0, err
Expand All @@ -767,7 +761,7 @@ func (r *StateReaderParallelV3) ReadAccountCodeSize(address common.Address, inca
return size, nil
}

func (r *StateReaderParallelV3) ReadAccountIncarnation(address common.Address) (uint64, error) {
func (r *ReaderParallelV3) ReadAccountIncarnation(address common.Address) (uint64, error) {
return 0, nil
}

Expand Down
Loading

0 comments on commit 12d7e76

Please sign in to comment.