Skip to content

Commit

Permalink
Remove some db interfaces (erigontech#2452)
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov authored Jul 27, 2021
1 parent 974eb62 commit 63a6c45
Show file tree
Hide file tree
Showing 18 changed files with 83 additions and 396 deletions.
18 changes: 7 additions & 11 deletions cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,13 +802,9 @@ func dumpStorage() {
db := kv2.MustOpen(paths.DefaultDataDir() + "/geth/chaindata")
defer db.Close()
if err := db.View(context.Background(), func(tx ethdb.Tx) error {
c, err := tx.Cursor(dbutils.StorageHistoryBucket)
if err != nil {
return err
}
return ethdb.ForEach(c, func(k, v []byte) (bool, error) {
return tx.ForEach(dbutils.StorageHistoryBucket, nil, func(k, v []byte) error {
fmt.Printf("%x %x\n", k, v)
return true, nil
return nil
})
}); err != nil {
panic(err)
Expand Down Expand Up @@ -1026,7 +1022,7 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo
ts := dbutils.EncodeBlockNumber(block + 1)
accountMap := make(map[string]*accounts.Account)

if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.AccountChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) {
if err := changeset.Walk(tx, dbutils.AccountChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) {
if blockN > *headNumber {
return false, nil
}
Expand Down Expand Up @@ -1056,7 +1052,7 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo
log.Info("Constructed account map", "size", len(accountMap),
"alloc", common.StorageSize(m.Alloc), "sys", common.StorageSize(m.Sys))
storageMap := make(map[string][]byte)
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.StorageChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) {
if err := changeset.Walk(tx, dbutils.StorageChangeSetBucket, ts, 0, func(blockN uint64, address, v []byte) (bool, error) {
if blockN > *headNumber {
return false, nil
}
Expand Down Expand Up @@ -1125,14 +1121,14 @@ func testGetProof(chaindata string, address common.Address, rewind int, regen bo
if err = loader.Reset(unfurl, nil, nil, false); err != nil {
panic(err)
}
_, err = loader.CalcTrieRoot(tx.(ethdb.HasTx).Tx(), nil, nil)
_, err = loader.CalcTrieRoot(tx, nil, nil)
if err != nil {
return err
}
r := &Receiver{defaultReceiver: trie.NewRootHashAggregator(), unfurlList: unfurlList, accountMap: accountMap, storageMap: storageMap}
r.defaultReceiver.Reset(nil, nil /* HashCollector */, false)
loader.SetStreamReceiver(r)
root, err := loader.CalcTrieRoot(tx.(ethdb.HasTx).Tx(), nil, nil)
root, err := loader.CalcTrieRoot(tx, nil, nil)
if err != nil {
return err
}
Expand Down Expand Up @@ -1251,7 +1247,7 @@ func changeSetStats(chaindata string, block1, block2 uint64) error {
}

storage := make(map[string]struct{})
if err := changeset.Walk(tx.(ethdb.HasTx).Tx(), dbutils.StorageChangeSetBucket, dbutils.EncodeBlockNumber(block1), 0, func(blockN uint64, k, v []byte) (bool, error) {
if err := changeset.Walk(tx, dbutils.StorageChangeSetBucket, dbutils.EncodeBlockNumber(block1), 0, func(blockN uint64, k, v []byte) (bool, error) {
if blockN >= block2 {
return false, nil
}
Expand Down
16 changes: 6 additions & 10 deletions cmd/pics/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,10 @@ func stateDatabaseComparison(first ethdb.RwKV, second ethdb.RwKV, number int) er
return first.View(context.Background(), func(firstTx ethdb.Tx) error {
for bucketName := range bucketLabels {
bucketName := bucketName
c, err := readTx.Cursor(bucketName)
if err != nil {
return err
}
if err2 := ethdb.ForEach(c, func(k, v []byte) (bool, error) {
if err := readTx.ForEach(bucketName, nil, func(k, v []byte) error {
if firstV, _ := firstTx.GetOne(bucketName, k); firstV != nil && bytes.Equal(v, firstV) {
// Skip the record that is the same as in the first Db
return true, nil
return nil
}
// Produce pair of nodes
keyKeyBytes := &trie.Keybytes{
Expand All @@ -163,7 +159,7 @@ func stateDatabaseComparison(first ethdb.RwKV, second ethdb.RwKV, number int) er
if f1, ok = perBucketFiles[bucketName]; !ok {
f1, err = os.Create(fmt.Sprintf("changes_%d_%s.dot", number, strings.ReplaceAll(bucketLabels[bucketName], " ", "")))
if err != nil {
return false, err
return err
}
visual.StartGraph(f1, true)
var clusterLabel string
Expand Down Expand Up @@ -202,9 +198,9 @@ func stateDatabaseComparison(first ethdb.RwKV, second ethdb.RwKV, number int) er
lst = append(lst, i)
m[bucketName] = lst
i++
return true, nil
}); err2 != nil {
return err2
return nil
}); err != nil {
return err
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion core/state/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (d *Dumper) DumpToCollector(c DumpCollector, excludeCode, excludeStorage bo

if !excludeCode && codeHash != nil && !bytes.Equal(codeHash, emptyCodeHash[:]) {
var code []byte
if code, err = ethdb.Get(d.db, dbutils.CodeBucket, codeHash); err != nil {
if code, err = d.db.GetOne(dbutils.CodeBucket, codeHash); err != nil {
return nil, err
}
account.Code = code
Expand Down
12 changes: 3 additions & 9 deletions core/state/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/core/types/accounts"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/bitmapdb"
"github.com/ledgerwatch/erigon/ethdb/kv"
"github.com/ledgerwatch/erigon/turbo/trie"
Expand Down Expand Up @@ -91,11 +90,6 @@ func TestMutationCommit(t *testing.T) {

addrs, accState, accStateStorage, accHistory, accHistoryStateStorage := generateAccountsWithStorageAndHistory(t, NewPlainStateWriter(tx, tx, 2), numOfAccounts, numOfStateKeys)

plainState, err := tx.Cursor(dbutils.PlainStateBucket)
if err != nil {
t.Fatal(err)
}
defer plainState.Close()
for i, addr := range addrs {
acc, err := NewPlainStateReader(tx).ReadAccountData(addr)

Expand All @@ -122,9 +116,9 @@ func TestMutationCommit(t *testing.T) {
}

resAccStorage := make(map[common.Hash]uint256.Int)
err = ethdb.Walk(plainState, dbutils.PlainGenerateStoragePrefix(addr[:], acc.Incarnation), 8*(common.AddressLength+8), func(k, v []byte) (b bool, e error) {
err = tx.ForPrefix(dbutils.PlainStateBucket, dbutils.PlainGenerateStoragePrefix(addr[:], acc.Incarnation), func(k, v []byte) error {
resAccStorage[common.BytesToHash(k[common.AddressLength+8:])] = *uint256.NewInt(0).SetBytes(v)
return true, nil
return nil
})
if err != nil {
t.Fatal("error on get account storage", i, err)
Expand All @@ -150,7 +144,7 @@ func TestMutationCommit(t *testing.T) {
}

changeSetInDB := changeset.NewAccountChangeSet()
err = changeset.Walk(tx, dbutils.AccountChangeSetBucket, dbutils.EncodeBlockNumber(2), 8*8, func(_ uint64, k, v []byte) (bool, error) {
err := changeset.Walk(tx, dbutils.AccountChangeSetBucket, dbutils.EncodeBlockNumber(2), 8*8, func(_ uint64, k, v []byte) (bool, error) {
if err := changeSetInDB.Add(k, v); err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion eth/stagedsync/chain_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// Implements consensus.ChainReader
type ChainReader struct {
Cfg params.ChainConfig
Db ethdb.Getter
Db ethdb.KVGetter
}

// Config retrieves the blockchain's chain configuration.
Expand Down
3 changes: 1 addition & 2 deletions eth/stagedsync/stage_bodies.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/eth/stagedsync/stages"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/kv"
"github.com/ledgerwatch/erigon/log"
"github.com/ledgerwatch/erigon/metrics"
"github.com/ledgerwatch/erigon/params"
Expand Down Expand Up @@ -149,7 +148,7 @@ Loop:
}
d4 += time.Since(start)
start = time.Now()
cr := ChainReader{Cfg: cfg.chanConfig, Db: kv.WrapIntoTxDB(tx)}
cr := ChainReader{Cfg: cfg.chanConfig, Db: tx}
for i, header := range headers {
rawBody := rawBodies[i]
blockHeight := header.Number.Uint64()
Expand Down
30 changes: 10 additions & 20 deletions eth/stagedsync/stage_interhashes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/ledgerwatch/erigon/turbo/trie"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func addTestAccount(tx ethdb.Putter, hash common.Hash, balance uint64, incarnation uint64) error {
Expand All @@ -29,7 +28,7 @@ func addTestAccount(tx ethdb.Putter, hash common.Hash, balance uint64, incarnati
}

func TestAccountAndStorageTrie(t *testing.T) {
db, tx := kv.NewTestTx(t)
_, tx := kv.NewTestTx(t)

hash1 := common.HexToHash("0xB000000000000000000000000000000000000000000000000000000000000000")
assert.Nil(t, addTestAccount(tx, hash1, 3*params.Ether, 0))
Expand Down Expand Up @@ -65,16 +64,13 @@ func TestAccountAndStorageTrie(t *testing.T) {
hash6 := common.HexToHash("0xB340000000000000000000000000000000000000000000000000000000000000")
assert.Nil(t, addTestAccount(tx, hash6, 1*params.Ether, 0))

_, err := RegenerateIntermediateHashes("IH", tx, StageTrieCfg(db, false, true, t.TempDir()), common.Hash{} /* expectedRootHash */, nil /* quit */)
_, err := RegenerateIntermediateHashes("IH", tx, StageTrieCfg(nil, false, true, t.TempDir()), common.Hash{} /* expectedRootHash */, nil /* quit */)
assert.Nil(t, err)

accountTrie := make(map[string][]byte)
accountCursor, err := tx.Cursor(dbutils.TrieOfAccountsBucket)
require.NoError(t, err)
defer accountCursor.Close()
err = ethdb.ForEach(accountCursor, func(k, v []byte) (bool, error) {
err = tx.ForEach(dbutils.TrieOfAccountsBucket, nil, func(k, v []byte) error {
accountTrie[string(k)] = v
return true, nil
return nil
})
assert.Nil(t, err)

Expand All @@ -95,12 +91,9 @@ func TestAccountAndStorageTrie(t *testing.T) {
assert.Equal(t, 0, len(rootHash2))

storageTrie := make(map[string][]byte)
storageCursor, err := tx.Cursor(dbutils.TrieOfStorageBucket)
require.NoError(t, err)
defer storageCursor.Close()
err = ethdb.ForEach(storageCursor, func(k, v []byte) (bool, error) {
err = tx.ForEach(dbutils.TrieOfStorageBucket, nil, func(k, v []byte) error {
storageTrie[string(k)] = v
return true, nil
return nil
})
assert.Nil(t, err)

Expand All @@ -119,7 +112,7 @@ func TestAccountAndStorageTrie(t *testing.T) {
}

func TestAccountTrieAroundExtensionNode(t *testing.T) {
db, tx := kv.NewTestTx(t)
_, tx := kv.NewTestTx(t)

acc := accounts.NewAccount()
acc.Balance.SetUint64(1 * params.Ether)
Expand All @@ -144,16 +137,13 @@ func TestAccountTrieAroundExtensionNode(t *testing.T) {
hash6 := common.HexToHash("0x3100000000000000000000000000000000000000000000000000000000000000")
assert.Nil(t, tx.Put(dbutils.HashedAccountsBucket, hash6[:], encoded))

_, err := RegenerateIntermediateHashes("IH", tx, StageTrieCfg(db, false, true, t.TempDir()), common.Hash{} /* expectedRootHash */, nil /* quit */)
_, err := RegenerateIntermediateHashes("IH", tx, StageTrieCfg(nil, false, true, t.TempDir()), common.Hash{} /* expectedRootHash */, nil /* quit */)
assert.Nil(t, err)

accountTrie := make(map[string][]byte)
accountCursor, err := tx.Cursor(dbutils.TrieOfAccountsBucket)
require.NoError(t, err)
defer accountCursor.Close()
err = ethdb.ForEach(accountCursor, func(k, v []byte) (bool, error) {
err = tx.ForEach(dbutils.TrieOfAccountsBucket, nil, func(k, v []byte) error {
accountTrie[string(k)] = v
return true, nil
return nil
})
assert.Nil(t, err)

Expand Down
3 changes: 1 addition & 2 deletions eth/stagedsync/stage_mining_create_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/eth/ethutils"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/kv"
"github.com/ledgerwatch/erigon/log"
"github.com/ledgerwatch/erigon/params"
)
Expand Down Expand Up @@ -113,7 +112,7 @@ func SpawnMiningCreateBlockStage(s *StageState, tx ethdb.RwTx, cfg MiningCreateB
if err != nil {
return err
}
chain := ChainReader{Cfg: cfg.chainConfig, Db: kv.WrapIntoTxDB(tx)}
chain := ChainReader{Cfg: cfg.chainConfig, Db: tx}
var GetBlocksFromHash = func(hash common.Hash, n int) (blocks []*types.Block) {
number := rawdb.ReadHeaderNumber(tx, hash)
if number == nil {
Expand Down
3 changes: 1 addition & 2 deletions eth/stagedsync/stage_mining_finish.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/ledgerwatch/erigon/consensus"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/kv"
"github.com/ledgerwatch/erigon/log"
"github.com/ledgerwatch/erigon/params"
)
Expand Down Expand Up @@ -71,7 +70,7 @@ func SpawnMiningFinishStage(s *StageState, tx ethdb.RwTx, cfg MiningFinishCfg, q
"difficulty", block.Difficulty(),
)

chain := ChainReader{Cfg: cfg.chainConfig, Db: kv.WrapIntoTxDB(tx)}
chain := ChainReader{Cfg: cfg.chainConfig, Db: tx}
if err := cfg.engine.Seal(chain, block, cfg.miningState.MiningResultCh, cfg.sealCancel); err != nil {
log.Warn("Block sealing failed", "err", err)
}
Expand Down
18 changes: 4 additions & 14 deletions eth/stagedsync/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,16 @@ func compareBucket(t *testing.T, db1, db2 ethdb.Tx, bucketName string) {
var err error

bucket1 := make(map[string][]byte)
c1, err := db1.Cursor(bucketName)
if err != nil {
assert.NoError(t, err)
}
defer c1.Close()
err = ethdb.ForEach(c1, func(k, v []byte) (bool, error) {
err = db1.ForEach(bucketName, nil, func(k, v []byte) error {
bucket1[string(k)] = v
return true, nil
return nil
})
assert.NoError(t, err)

bucket2 := make(map[string][]byte)
c2, err := db2.Cursor(bucketName)
if err != nil {
assert.NoError(t, err)
}
defer c2.Close()
err = ethdb.ForEach(c2, func(k, v []byte) (bool, error) {
err = db2.ForEach(bucketName, nil, func(k, v []byte) error {
bucket2[string(k)] = v
return true, nil
return nil
})
assert.NoError(t, err)

Expand Down
33 changes: 3 additions & 30 deletions ethdb/db_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,17 @@ const (
RO TxFlags = 0x02
)

type DatabaseReader interface {
// Getter wraps the database read operations.
type Getter interface {
KVGetter

// Get returns the value for a given key if it's present.
Get(bucket string, key []byte) ([]byte, error)
}

// Getter wraps the database read operations.
type Getter interface {
DatabaseReader
}

type GetterTx interface {
Getter

Rollback()
}

type GetterBeginner interface {
Getter

BeginGetter(ctx context.Context) (GetterTx, error)
}

type GetterPutter interface {
Getter
Putter
}

// Database wraps all database operations. All methods are safe for concurrent use.
type Database interface {
GetterBeginner
Getter
Putter
Deleter
Closer
Expand Down Expand Up @@ -135,10 +114,6 @@ type HasTx interface {
Tx() Tx
}

type HasNetInterface interface {
DB() Database
}

type BucketsMigrator interface {
BucketExists(bucket string) (bool, error) // makes them empty
ClearBuckets(buckets ...string) error // makes them empty
Expand All @@ -154,5 +129,3 @@ func GetOneWrapper(dat []byte, err error) ([]byte, error) {
}
return dat, nil
}

var ErrNotSupported = errors.New("not supported")
Loading

0 comments on commit 63a6c45

Please sign in to comment.