Skip to content

Commit

Permalink
move some tests to kv interface (erigontech#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Jul 24, 2021
1 parent b56e500 commit 6801208
Show file tree
Hide file tree
Showing 29 changed files with 210 additions and 458 deletions.
4 changes: 2 additions & 2 deletions accounts/abi/bind/backends/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ func (b *SimulatedBackend) emptyPendingBlock() {
// stateByBlockNumber retrieves a state by a given blocknumber.
func (b *SimulatedBackend) stateByBlockNumber(db ethdb.Tx, blockNumber *big.Int) *state.IntraBlockState {
if blockNumber == nil || blockNumber.Cmp(b.pendingBlock.Number()) == 0 {
return state.New(state.NewPlainKvState(db, b.pendingBlock.NumberU64()))
return state.New(state.NewPlainState(db, b.pendingBlock.NumberU64()))
}
return state.New(state.NewPlainKvState(db, uint64(blockNumber.Int64())))
return state.New(state.NewPlainState(db, uint64(blockNumber.Int64())))
}

// CodeAt returns the code associated with a certain account in the blockchain.
Expand Down
8 changes: 3 additions & 5 deletions accounts/abi/bind/backends/simulated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ import (
"time"

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/kv"

ethereum "github.com/ledgerwatch/erigon"
"github.com/ledgerwatch/erigon/accounts/abi"
"github.com/ledgerwatch/erigon/accounts/abi/bind"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/core"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/core/state"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/params"
)

Expand Down Expand Up @@ -148,7 +146,7 @@ func TestNewSimulatedBackend(t *testing.T) {
}); err != nil {
t.Fatal(err)
}
statedb := state.New(state.NewPlainDBState(kv.NewRoTxDb(tx), num))
statedb := state.New(state.NewPlainState(tx, num))
bal := statedb.GetBalance(testAddr)
if !bal.Eq(expectedBal) {
t.Errorf("expected balance for test address not received. expected: %v actual: %v", expectedBal, bal)
Expand Down
8 changes: 4 additions & 4 deletions cmd/evm/internal/t8ntool/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
return db, execRs, nil
}

func MakePreState(chainRules params.Rules, db ethdb.RwTx, accounts core.GenesisAlloc) *state.IntraBlockState {
func MakePreState(chainRules params.Rules, tx ethdb.RwTx, accounts core.GenesisAlloc) *state.IntraBlockState {
var blockNr uint64 = 0
r, _ := state.NewPlainStateReader(db), state.NewPlainStateWriter(db, db, blockNr)
r, _ := state.NewPlainStateReader(tx), state.NewPlainStateWriter(tx, tx, blockNr)
statedb := state.New(r)
for addr, a := range accounts {
statedb.SetCode(addr, a.Code)
Expand All @@ -285,10 +285,10 @@ func MakePreState(chainRules params.Rules, db ethdb.RwTx, accounts core.GenesisA
}
}
// Commit and re-open to start with a clean state.
if err := statedb.FinalizeTx(chainRules, state.NewPlainStateWriter(db, db, blockNr+1)); err != nil {
if err := statedb.FinalizeTx(chainRules, state.NewPlainStateWriter(tx, tx, blockNr+1)); err != nil {
panic(err)
}
if err := statedb.CommitBlock(chainRules, state.NewPlainStateWriter(db, db, blockNr+1)); err != nil {
if err := statedb.CommitBlock(chainRules, state.NewPlainStateWriter(tx, tx, blockNr+1)); err != nil {
panic(err)
}
return statedb
Expand Down
2 changes: 1 addition & 1 deletion cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ func scanReceipts(chaindata string, block uint64) error {
return err
}

dbstate := state.NewPlainKvState(tx, block.NumberU64()-1)
dbstate := state.NewPlainState(tx, block.NumberU64()-1)
intraBlockState := state.New(dbstate)

getHeader := func(hash common.Hash, number uint64) *types.Header { return rawdb.ReadHeader(tx, hash, number) }
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/eth_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (api *APIImpl) CallBundle(ctx context.Context, txHashes []common.Hash, stat
if num, ok := stateBlockNumberOrHash.Number(); ok && num == rpc.LatestBlockNumber {
stateReader = state.NewPlainStateReader(tx)
} else {
stateReader = state.NewPlainKvState(tx, stateBlockNumber)
stateReader = state.NewPlainState(tx, stateBlockNumber)
}
st := state.New(stateReader)

Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/storage_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type StorageEntry struct {
Value common.Hash `json:"value"`
}

func StorageRangeAt(stateReader *state.PlainKVState, contractAddress common.Address, start []byte, maxResult int) (StorageRangeResult, error) {
func StorageRangeAt(stateReader *state.PlainState, contractAddress common.Address, start []byte, maxResult int) (StorageRangeResult, error) {
result := StorageRangeResult{Storage: StorageMap{}}
resultCount := 0

Expand Down
4 changes: 2 additions & 2 deletions cmd/rpcdaemon/commands/trace_adhoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func (api *TraceAPIImpl) Call(ctx context.Context, args TraceCallParam, traceTyp
if num, ok := blockNrOrHash.Number(); ok && num == rpc.LatestBlockNumber {
stateReader = state.NewPlainStateReader(tx)
} else {
stateReader = state.NewPlainKvState(tx, blockNumber)
stateReader = state.NewPlainState(tx, blockNumber)
}
ibs := state.New(stateReader)

Expand Down Expand Up @@ -846,7 +846,7 @@ func (api *TraceAPIImpl) doCallMany(ctx context.Context, dbtx ethdb.Tx, callPara
if num, ok := parentNrOrHash.Number(); ok && num == rpc.LatestBlockNumber {
stateReader = state.NewPlainStateReader(dbtx)
} else {
stateReader = state.NewPlainKvState(dbtx, blockNumber)
stateReader = state.NewPlainState(dbtx, blockNumber)
}
stateCache := shards.NewStateCache(32, 0 /* no limit */)
cachedReader := state.NewCachedReader(stateReader, stateCache)
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpcdaemon/commands/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (api *PrivateDebugAPIImpl) TraceCall(ctx context.Context, args ethapi.CallA
if num, ok := blockNrOrHash.Number(); ok && num == rpc.LatestBlockNumber {
stateReader = state.NewPlainStateReader(dbtx)
} else {
stateReader = state.NewPlainKvState(dbtx, blockNumber)
stateReader = state.NewPlainState(dbtx, blockNumber)
}
header := rawdb.ReadHeader(dbtx, hash, blockNumber)
if header == nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/commands/check_change_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func CheckChangeSets(genesis *core.Genesis, blockNum uint64, chaindata string, h
break
}

intraBlockState := state.New(state.NewPlainKvState(historyTx, block.NumberU64()-1))
intraBlockState := state.New(state.NewPlainState(historyTx, block.NumberU64()-1))
csw := state.NewChangeSetWriterPlain(nil /* db */, block.NumberU64()-1)
var blockWriter state.StateWriter
if nocheck {
Expand Down
2 changes: 1 addition & 1 deletion cmd/state/commands/opcode_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func OpcodeTracer(genesis *core.Genesis, blockNum uint64, chaindata string, numB
ot.fsumWriter = bufio.NewWriter(fsum)
}

dbstate := state.NewPlainKvState(historyTx, block.NumberU64()-1)
dbstate := state.NewPlainState(historyTx, block.NumberU64()-1)
intraBlockState := state.New(dbstate)
intraBlockState.SetTracer(ot)

Expand Down
2 changes: 1 addition & 1 deletion core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (g *Genesis) ToBlock() (*types.Block, *state.IntraBlockState, error) {
panic(err)
}
defer tx.Rollback()
r, w := state.NewDbStateReader(kv.WrapIntoTxDB(tx)), state.NewDbStateWriter(kv.WrapIntoTxDB(tx), 0)
r, w := state.NewDbStateReader(tx), state.NewDbStateWriter(tx, 0)
statedb = state.New(r)
for addr, account := range g.Alloc {
balance, overflow := uint256.FromBig(account.Balance)
Expand Down
15 changes: 6 additions & 9 deletions core/state/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ func TestReproduceCrash(t *testing.T) {

_, tx := kv.NewTestTx(t)
tsw := state.NewPlainStateWriter(tx, nil, 0)
intraBlockState := state.New(state.NewPlainKvState(tx, 0))
intraBlockState := state.New(state.NewPlainState(tx, 0))
// Start the 1st transaction
intraBlockState.CreateAccount(contract, true)
if err := intraBlockState.FinalizeTx(params.Rules{}, tsw); err != nil {
Expand Down Expand Up @@ -1286,8 +1286,8 @@ func TestWrongIncarnation2(t *testing.T) {
func TestChangeAccountCodeBetweenBlocks(t *testing.T) {
contract := common.HexToAddress("0x71dd1027069078091B3ca48093B00E4735B20624")

db := kv.NewTestDB(t)
r, tsw := state.NewPlainStateReader(db), state.NewPlainStateWriter(db, nil, 0)
_, tx := kv.NewTestTx(t)
r, tsw := state.NewPlainStateReader(tx), state.NewPlainStateWriter(tx, nil, 0)
intraBlockState := state.New(r)
// Start the 1st transaction
intraBlockState.CreateAccount(contract, true)
Expand All @@ -1299,10 +1299,7 @@ func TestChangeAccountCodeBetweenBlocks(t *testing.T) {
if err := intraBlockState.FinalizeTx(params.Rules{}, tsw); err != nil {
t.Errorf("error finalising 1st tx: %w", err)
}
err := db.RwKV().View(context.Background(), func(tx ethdb.Tx) error {
_, err := trie.CalcRoot("test", tx)
return err
})
_, err := trie.CalcRoot("test", tx)
require.NoError(t, err)
oldCodeHash := common.BytesToHash(crypto.Keccak256(oldCode))
trieCode, tcErr := r.ReadAccountCode(contract, 1, oldCodeHash)
Expand All @@ -1328,7 +1325,7 @@ func TestCacheCodeSizeSeparately(t *testing.T) {
//root := common.HexToHash("0xb939e5bcf5809adfb87ab07f0795b05b95a1d64a90f0eddd0c3123ac5b433854")

_, tx := kv.NewTestTx(t)
r, w := state.NewPlainKvState(tx, 0), state.NewPlainStateWriter(tx, nil, 0)
r, w := state.NewPlainState(tx, 0), state.NewPlainStateWriter(tx, nil, 0)
intraBlockState := state.New(r)
// Start the 1st transaction
intraBlockState.CreateAccount(contract, true)
Expand Down Expand Up @@ -1361,7 +1358,7 @@ func TestCacheCodeSizeInTrie(t *testing.T) {
root := common.HexToHash("0xb939e5bcf5809adfb87ab07f0795b05b95a1d64a90f0eddd0c3123ac5b433854")

_, tx := kv.NewTestTx(t)
r, w := state.NewPlainKvState(tx, 0), state.NewPlainStateWriter(tx, nil, 0)
r, w := state.NewPlainState(tx, 0), state.NewPlainStateWriter(tx, nil, 0)
intraBlockState := state.New(r)
// Start the 1st transaction
intraBlockState.CreateAccount(contract, true)
Expand Down
27 changes: 15 additions & 12 deletions core/state/db_state_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package state
import (
"bytes"
"encoding/binary"
"errors"

"github.com/VictoriaMetrics/fastcache"
"github.com/ledgerwatch/erigon/common"
Expand All @@ -14,14 +13,14 @@ import (

// Implements StateReader by wrapping database only, without trie
type DbStateReader struct {
db ethdb.Getter
db ethdb.KVGetter
accountCache *fastcache.Cache
storageCache *fastcache.Cache
codeCache *fastcache.Cache
codeSizeCache *fastcache.Cache
}

func NewDbStateReader(db ethdb.Getter) *DbStateReader {
func NewDbStateReader(db ethdb.KVGetter) *DbStateReader {
return &DbStateReader{
db: db,
}
Expand Down Expand Up @@ -88,8 +87,8 @@ func (dbr *DbStateReader) ReadAccountStorage(address common.Address, incarnation
return enc, nil
}
}
enc, err2 := dbr.db.Get(dbutils.HashedStorageBucket, compositeKey)
if err2 != nil && !errors.Is(err2, ethdb.ErrKeyNotFound) {
enc, err2 := dbr.db.GetOne(dbutils.HashedStorageBucket, compositeKey)
if err2 != nil {
return nil, err2
}
if dbr.storageCache != nil {
Expand All @@ -107,7 +106,10 @@ func (dbr *DbStateReader) ReadAccountCode(address common.Address, incarnation ui
return code, nil
}
}
code, err := dbr.db.Get(dbutils.CodeBucket, codeHash[:])
code, err := dbr.db.GetOne(dbutils.CodeBucket, codeHash[:])
if err != nil {
return nil, err
}
if dbr.codeCache != nil && len(code) <= 1024 {
dbr.codeCache.Set(address[:], code)
}
Expand All @@ -129,7 +131,7 @@ func (dbr *DbStateReader) ReadAccountCodeSize(address common.Address, incarnatio
}
}
var code []byte
code, err = dbr.db.Get(dbutils.CodeBucket, codeHash[:])
code, err = dbr.db.GetOne(dbutils.CodeBucket, codeHash[:])
if err != nil {
return 0, err
}
Expand All @@ -142,11 +144,12 @@ func (dbr *DbStateReader) ReadAccountCodeSize(address common.Address, incarnatio
}

func (dbr *DbStateReader) ReadAccountIncarnation(address common.Address) (uint64, error) {
if b, err := dbr.db.Get(dbutils.IncarnationMapBucket, address[:]); err == nil {
return binary.BigEndian.Uint64(b), nil
} else if errors.Is(err, ethdb.ErrKeyNotFound) {
return 0, nil
} else {
b, err := dbr.db.GetOne(dbutils.IncarnationMapBucket, address[:])
if err != nil {
return 0, err
}
if b == nil {
return 0, err
}
return binary.BigEndian.Uint64(b), nil
}
4 changes: 2 additions & 2 deletions core/state/db_state_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

var _ WriterWithChangeSets = (*DbStateWriter)(nil)

func NewDbStateWriter(db ethdb.Database, blockNr uint64) *DbStateWriter {
func NewDbStateWriter(db putDel, blockNr uint64) *DbStateWriter {
return &DbStateWriter{
db: db,
blockNr: blockNr,
Expand All @@ -31,7 +31,7 @@ func NewDbStateWriter(db ethdb.Database, blockNr uint64) *DbStateWriter {
}

type DbStateWriter struct {
db ethdb.Database
db putDel
blockNr uint64
csw *ChangeSetWriter
}
Expand Down
6 changes: 3 additions & 3 deletions core/state/intra_block_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (test *snapshotTest) run() bool {
}
defer tx.Rollback()
var (
ds = NewPlainKvState(tx, 0)
ds = NewPlainState(tx, 0)
state = New(ds)
snapshotRevs = make([]int, len(test.snapshots))
sindex = 0
Expand All @@ -241,7 +241,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-- {
checkds := NewPlainKvState(tx, 0)
checkds := NewPlainState(tx, 0)
checkstate := New(checkds)
for _, action := range test.actions[:test.snapshots[sindex]] {
action.fn(action, checkstate)
Expand Down Expand Up @@ -349,7 +349,7 @@ func TestAccessList(t *testing.T) {
slot := common.HexToHash

_, tx := kv.NewTestTx(t)
state := New(NewPlainKvState(tx, 0))
state := New(NewPlainState(tx, 0))
state.accessList = newAccessList()

verifyAddrs := func(astrings ...string) {
Expand Down
Loading

0 comments on commit 6801208

Please sign in to comment.