cmd, core/state, eth, tests, trie: improve state reader (#27428)
The state availability is checked during the creation of a state reader.
- In hash-based database, if the specified root node does not exist on disk disk, then
the state reader won't be created and an error will be returned.
- In path-based database, if the specified state layer is not available, then the
state reader won't be created and an error will be returned.
This change also contains a stricter semantics regarding the `Commit` operation: once it has been performed, the trie is no longer usable, and certain operations will return an error.
This commit is contained in:
+14
-12
@@ -62,34 +62,34 @@ func TestAccountRange(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var (
|
||||
statedb = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{Preimages: true})
|
||||
state, _ = state.New(types.EmptyRootHash, statedb, nil)
|
||||
addrs = [AccountRangeMaxResults * 2]common.Address{}
|
||||
m = map[common.Address]bool{}
|
||||
statedb = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), &trie.Config{Preimages: true})
|
||||
sdb, _ = state.New(types.EmptyRootHash, statedb, nil)
|
||||
addrs = [AccountRangeMaxResults * 2]common.Address{}
|
||||
m = map[common.Address]bool{}
|
||||
)
|
||||
|
||||
for i := range addrs {
|
||||
hash := common.HexToHash(fmt.Sprintf("%x", i))
|
||||
addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes())
|
||||
addrs[i] = addr
|
||||
state.SetBalance(addrs[i], big.NewInt(1))
|
||||
sdb.SetBalance(addrs[i], big.NewInt(1))
|
||||
if _, ok := m[addr]; ok {
|
||||
t.Fatalf("bad")
|
||||
} else {
|
||||
m[addr] = true
|
||||
}
|
||||
}
|
||||
state.Commit(true)
|
||||
root := state.IntermediateRoot(true)
|
||||
root, _ := sdb.Commit(true)
|
||||
sdb, _ = state.New(root, statedb, nil)
|
||||
|
||||
trie, err := statedb.OpenTrie(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults/2, AccountRangeMaxResults/2)
|
||||
accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults/2, AccountRangeMaxResults/2)
|
||||
// test pagination
|
||||
firstResult := accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
|
||||
secondResult := accountRangeTest(t, &trie, state, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
|
||||
firstResult := accountRangeTest(t, &trie, sdb, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
|
||||
secondResult := accountRangeTest(t, &trie, sdb, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
|
||||
|
||||
hList := make([]common.Hash, 0)
|
||||
for addr1 := range firstResult.Accounts {
|
||||
@@ -107,7 +107,7 @@ func TestAccountRange(t *testing.T) {
|
||||
// set and get an even split between the first and second sets.
|
||||
slices.SortFunc(hList, common.Hash.Less)
|
||||
middleH := hList[AccountRangeMaxResults/2]
|
||||
middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
|
||||
middleResult := accountRangeTest(t, &trie, sdb, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
|
||||
missing, infirst, insecond := 0, 0, 0
|
||||
for h := range middleResult.Accounts {
|
||||
if _, ok := firstResult.Accounts[h]; ok {
|
||||
@@ -136,8 +136,10 @@ func TestEmptyAccountRange(t *testing.T) {
|
||||
statedb = state.NewDatabase(rawdb.NewMemoryDatabase())
|
||||
st, _ = state.New(types.EmptyRootHash, statedb, nil)
|
||||
)
|
||||
// Commit(although nothing to flush) and re-init the statedb
|
||||
st.Commit(true)
|
||||
st.IntermediateRoot(true)
|
||||
st, _ = state.New(types.EmptyRootHash, statedb, nil)
|
||||
|
||||
results := st.IteratorDump(&state.DumpConfig{
|
||||
SkipCode: true,
|
||||
SkipStorage: true,
|
||||
|
||||
Reference in New Issue
Block a user