2023-07-11 21:43:23 +08:00
|
|
|
// Copyright 2023 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"testing/quick"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-08-26 16:13:22 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/state/snapshot"
|
2024-03-22 18:53:53 +01:00
|
|
|
"github.com/ethereum/go-ethereum/core/tracing"
|
2023-07-11 21:43:23 +08:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2023-07-31 20:07:51 +08:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-07-11 21:43:23 +08:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/trie"
|
2024-02-13 21:49:53 +08:00
|
|
|
"github.com/ethereum/go-ethereum/triedb"
|
|
|
|
"github.com/ethereum/go-ethereum/triedb/pathdb"
|
2024-01-23 14:51:58 +01:00
|
|
|
"github.com/holiman/uint256"
|
2023-07-11 21:43:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// A stateTest checks that the state changes are correctly captured. Instances
|
|
|
|
// of this test with pseudorandom content are created by Generate.
|
|
|
|
//
|
|
|
|
// The test works as follows:
|
|
|
|
//
|
|
|
|
// A list of states are created by applying actions. The state changes between
|
|
|
|
// each state instance are tracked and be verified.
|
|
|
|
type stateTest struct {
|
|
|
|
addrs []common.Address // all account addresses
|
|
|
|
actions [][]testAction // modifications to the state, grouped by block
|
|
|
|
chunk int // The number of actions per chunk
|
|
|
|
err error // failure details are reported through this field
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStateTestAction creates a random action that changes state.
|
|
|
|
func newStateTestAction(addr common.Address, r *rand.Rand, index int) testAction {
|
|
|
|
actions := []testAction{
|
|
|
|
{
|
|
|
|
name: "SetBalance",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
2024-03-22 18:53:53 +01:00
|
|
|
s.SetBalance(addr, uint256.NewInt(uint64(a.args[0])), tracing.BalanceChangeUnspecified)
|
2023-07-11 21:43:23 +08:00
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetNonce",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
s.SetNonce(addr, uint64(a.args[0]))
|
|
|
|
},
|
|
|
|
args: make([]int64, 1),
|
|
|
|
},
|
|
|
|
{
|
core/state: semantic journalling (part 1) (#28880)
This is a follow-up to #29520, and a preparatory PR to a more thorough
change in the journalling system.
### API methods instead of `append` operations
This PR hides the journal-implementation details away, so that the
statedb invokes methods like `JournalCreate`, instead of explicitly
appending journal-events in a list. This means that it's up to the
journal whether to implement it as a sequence of events or
aggregate/merge events.
### Snapshot-management inside the journal
This PR also makes it so that management of valid snapshots is moved
inside the journal, exposed via the methods `Snapshot() int` and
`RevertToSnapshot(revid int, s *StateDB)`.
### SetCode
JournalSetCode journals the setting of code: it is implicit that the
previous values were "no code" and emptyCodeHash. Therefore, we can
simplify the setCode journal.
### Selfdestruct
The self-destruct journalling is a bit strange: we allow the
selfdestruct operation to be journalled several times. This makes it so
that we also are forced to store whether the account was already
destructed.
What we can do instead, is to only journal the first destruction, and
after that only journal balance-changes, but not journal the
selfdestruct itself.
This simplifies the journalling, so that internals about state
management does not leak into the journal-API.
### Preimages
Preimages were, for some reason, integrated into the journal management,
despite not being a consensus-critical data structure. This PR undoes
that.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2024-08-28 08:18:23 +02:00
|
|
|
name: "SetStorage",
|
2023-07-11 21:43:23 +08:00
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
var key, val common.Hash
|
|
|
|
binary.BigEndian.PutUint16(key[:], uint16(a.args[0]))
|
|
|
|
binary.BigEndian.PutUint16(val[:], uint16(a.args[1]))
|
|
|
|
s.SetState(addr, key, val)
|
|
|
|
},
|
|
|
|
args: make([]int64, 2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetCode",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
|
|
|
code := make([]byte, 16)
|
|
|
|
binary.BigEndian.PutUint64(code, uint64(a.args[0]))
|
|
|
|
binary.BigEndian.PutUint64(code[8:], uint64(a.args[1]))
|
|
|
|
s.SetCode(addr, code)
|
|
|
|
},
|
|
|
|
args: make([]int64, 2),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CreateAccount",
|
|
|
|
fn: func(a testAction, s *StateDB) {
|
2024-04-24 11:59:06 +02:00
|
|
|
if !s.Exist(addr) {
|
|
|
|
s.CreateAccount(addr)
|
|
|
|
}
|
2023-07-11 21:43:23 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-07-15 16:35:30 +02:00
|
|
|
name: "Selfdestruct",
|
2023-07-11 21:43:23 +08:00
|
|
|
fn: func(a testAction, s *StateDB) {
|
2023-07-15 16:35:30 +02:00
|
|
|
s.SelfDestruct(addr)
|
2023-07-11 21:43:23 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var nonRandom = index != -1
|
|
|
|
if index == -1 {
|
|
|
|
index = r.Intn(len(actions))
|
|
|
|
}
|
|
|
|
action := actions[index]
|
|
|
|
var names []string
|
|
|
|
if !action.noAddr {
|
|
|
|
names = append(names, addr.Hex())
|
|
|
|
}
|
|
|
|
for i := range action.args {
|
|
|
|
if nonRandom {
|
|
|
|
action.args[i] = rand.Int63n(10000) + 1 // set balance to non-zero
|
|
|
|
} else {
|
|
|
|
action.args[i] = rand.Int63n(10000)
|
|
|
|
}
|
|
|
|
names = append(names, fmt.Sprint(action.args[i]))
|
|
|
|
}
|
|
|
|
action.name += " " + strings.Join(names, ", ")
|
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate returns a new snapshot test of the given size. All randomness is
|
|
|
|
// derived from r.
|
|
|
|
func (*stateTest) Generate(r *rand.Rand, size int) reflect.Value {
|
|
|
|
addrs := make([]common.Address, 5)
|
|
|
|
for i := range addrs {
|
|
|
|
addrs[i][0] = byte(i)
|
|
|
|
}
|
|
|
|
actions := make([][]testAction, rand.Intn(5)+1)
|
|
|
|
|
|
|
|
for i := 0; i < len(actions); i++ {
|
|
|
|
actions[i] = make([]testAction, size)
|
|
|
|
for j := range actions[i] {
|
|
|
|
if j == 0 {
|
|
|
|
// Always include a set balance action to make sure
|
|
|
|
// the state changes are not empty.
|
|
|
|
actions[i][j] = newStateTestAction(common.HexToAddress("0xdeadbeef"), r, 0)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
actions[i][j] = newStateTestAction(addrs[r.Intn(len(addrs))], r, -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chunk := int(math.Sqrt(float64(size)))
|
|
|
|
if size > 0 && chunk == 0 {
|
|
|
|
chunk = 1
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(&stateTest{
|
|
|
|
addrs: addrs,
|
|
|
|
actions: actions,
|
|
|
|
chunk: chunk,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *stateTest) String() string {
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
for i, actions := range test.actions {
|
|
|
|
fmt.Fprintf(out, "---- block %d ----\n", i)
|
|
|
|
for j, action := range actions {
|
|
|
|
if j%test.chunk == 0 {
|
|
|
|
fmt.Fprintf(out, "---- transaction %d ----\n", j/test.chunk)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(out, "%4d: %s\n", j%test.chunk, action.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (test *stateTest) run() bool {
|
|
|
|
var (
|
|
|
|
roots []common.Hash
|
2023-07-31 20:07:51 +08:00
|
|
|
accountList []map[common.Address][]byte
|
|
|
|
storageList []map[common.Address]map[common.Hash][]byte
|
2024-06-03 19:17:12 +08:00
|
|
|
copyUpdate = func(update *stateUpdate) {
|
|
|
|
accounts := make(map[common.Address][]byte, len(update.accountsOrigin))
|
|
|
|
for key, val := range update.accountsOrigin {
|
|
|
|
accounts[key] = common.CopyBytes(val)
|
|
|
|
}
|
|
|
|
accountList = append(accountList, accounts)
|
|
|
|
|
|
|
|
storages := make(map[common.Address]map[common.Hash][]byte, len(update.storagesOrigin))
|
|
|
|
for addr, subset := range update.storagesOrigin {
|
|
|
|
storages[addr] = make(map[common.Hash][]byte, len(subset))
|
|
|
|
for key, val := range subset {
|
|
|
|
storages[addr][key] = common.CopyBytes(val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
storageList = append(storageList, storages)
|
2023-07-11 21:43:23 +08:00
|
|
|
}
|
|
|
|
disk = rawdb.NewMemoryDatabase()
|
2024-02-13 21:49:53 +08:00
|
|
|
tdb = triedb.NewDatabase(disk, &triedb.Config{PathDB: pathdb.Defaults})
|
2023-07-11 21:43:23 +08:00
|
|
|
byzantium = rand.Intn(2) == 0
|
|
|
|
)
|
2023-08-26 16:13:22 +08:00
|
|
|
defer disk.Close()
|
|
|
|
defer tdb.Close()
|
|
|
|
|
|
|
|
var snaps *snapshot.Tree
|
|
|
|
if rand.Intn(3) == 0 {
|
|
|
|
snaps, _ = snapshot.New(snapshot.Config{
|
|
|
|
CacheSize: 1,
|
|
|
|
Recovery: false,
|
|
|
|
NoBuild: false,
|
|
|
|
AsyncBuild: false,
|
|
|
|
}, disk, tdb, types.EmptyRootHash)
|
|
|
|
}
|
2023-07-11 21:43:23 +08:00
|
|
|
for i, actions := range test.actions {
|
|
|
|
root := types.EmptyRootHash
|
|
|
|
if i != 0 {
|
|
|
|
root = roots[len(roots)-1]
|
|
|
|
}
|
2024-09-05 18:10:47 +08:00
|
|
|
state, err := New(root, NewDatabase(tdb, snaps))
|
2023-07-11 21:43:23 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for i, action := range actions {
|
|
|
|
if i%test.chunk == 0 && i != 0 {
|
|
|
|
if byzantium {
|
|
|
|
state.Finalise(true) // call finalise at the transaction boundary
|
|
|
|
} else {
|
|
|
|
state.IntermediateRoot(true) // call intermediateRoot at the transaction boundary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
action.fn(action, state)
|
|
|
|
}
|
|
|
|
if byzantium {
|
|
|
|
state.Finalise(true) // call finalise at the transaction boundary
|
|
|
|
} else {
|
|
|
|
state.IntermediateRoot(true) // call intermediateRoot at the transaction boundary
|
|
|
|
}
|
2024-06-03 19:17:12 +08:00
|
|
|
ret, err := state.commitAndFlush(0, true) // call commit at the block boundary
|
2023-07-11 21:43:23 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-06-03 19:17:12 +08:00
|
|
|
if ret.empty() {
|
|
|
|
return true
|
2023-07-11 21:43:23 +08:00
|
|
|
}
|
2024-06-03 19:17:12 +08:00
|
|
|
copyUpdate(ret)
|
|
|
|
roots = append(roots, ret.root)
|
2023-07-11 21:43:23 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < len(test.actions); i++ {
|
|
|
|
root := types.EmptyRootHash
|
|
|
|
if i != 0 {
|
|
|
|
root = roots[i-1]
|
|
|
|
}
|
|
|
|
test.err = test.verify(root, roots[i], tdb, accountList[i], storageList[i])
|
|
|
|
if test.err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyAccountCreation this function is called once the state diff says that
|
|
|
|
// specific account was not present. A serial of checks will be performed to
|
|
|
|
// ensure the state diff is correct, includes:
|
|
|
|
//
|
|
|
|
// - the account was indeed not present in trie
|
|
|
|
// - the account is present in new trie, nil->nil is regarded as invalid
|
|
|
|
// - the slots transition is correct
|
2024-02-13 21:49:53 +08:00
|
|
|
func (test *stateTest) verifyAccountCreation(next common.Hash, db *triedb.Database, otr, ntr *trie.Trie, addr common.Address, slots map[common.Hash][]byte) error {
|
2023-07-11 21:43:23 +08:00
|
|
|
// Verify account change
|
2023-07-31 20:07:51 +08:00
|
|
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
2023-07-11 21:43:23 +08:00
|
|
|
oBlob, err := otr.Get(addrHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nBlob, err := ntr.Get(addrHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oBlob) != 0 {
|
|
|
|
return fmt.Errorf("unexpected account in old trie, %x", addrHash)
|
|
|
|
}
|
|
|
|
if len(nBlob) == 0 {
|
|
|
|
return fmt.Errorf("missing account in new trie, %x", addrHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify storage changes
|
|
|
|
var nAcct types.StateAccount
|
|
|
|
if err := rlp.DecodeBytes(nBlob, &nAcct); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Account has no slot, empty slot set is expected
|
|
|
|
if nAcct.Root == types.EmptyRootHash {
|
|
|
|
if len(slots) != 0 {
|
|
|
|
return fmt.Errorf("unexpected slot changes %x", addrHash)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Account has slots, ensure all new slots are contained
|
|
|
|
st, err := trie.New(trie.StorageTrieID(next, addrHash, nAcct.Root), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for key, val := range slots {
|
|
|
|
st.Update(key.Bytes(), val)
|
|
|
|
}
|
|
|
|
if st.Hash() != types.EmptyRootHash {
|
|
|
|
return errors.New("invalid slot changes")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyAccountUpdate this function is called once the state diff says that
|
|
|
|
// specific account was present. A serial of checks will be performed to
|
|
|
|
// ensure the state diff is correct, includes:
|
|
|
|
//
|
|
|
|
// - the account was indeed present in trie
|
|
|
|
// - the account in old trie matches the provided value
|
|
|
|
// - the slots transition is correct
|
2024-02-13 21:49:53 +08:00
|
|
|
func (test *stateTest) verifyAccountUpdate(next common.Hash, db *triedb.Database, otr, ntr *trie.Trie, addr common.Address, origin []byte, slots map[common.Hash][]byte) error {
|
2023-07-11 21:43:23 +08:00
|
|
|
// Verify account change
|
2023-07-31 20:07:51 +08:00
|
|
|
addrHash := crypto.Keccak256Hash(addr.Bytes())
|
2023-07-11 21:43:23 +08:00
|
|
|
oBlob, err := otr.Get(addrHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nBlob, err := ntr.Get(addrHash.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(oBlob) == 0 {
|
|
|
|
return fmt.Errorf("missing account in old trie, %x", addrHash)
|
|
|
|
}
|
|
|
|
full, err := types.FullAccountRLP(origin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(full, oBlob) {
|
|
|
|
return fmt.Errorf("account value is not matched, %x", addrHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode accounts
|
|
|
|
var (
|
|
|
|
oAcct types.StateAccount
|
|
|
|
nAcct types.StateAccount
|
|
|
|
nRoot common.Hash
|
|
|
|
)
|
|
|
|
if err := rlp.DecodeBytes(oBlob, &oAcct); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(nBlob) == 0 {
|
|
|
|
nRoot = types.EmptyRootHash
|
|
|
|
} else {
|
|
|
|
if err := rlp.DecodeBytes(nBlob, &nAcct); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
nRoot = nAcct.Root
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify storage
|
|
|
|
st, err := trie.New(trie.StorageTrieID(next, addrHash, nRoot), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for key, val := range slots {
|
|
|
|
st.Update(key.Bytes(), val)
|
|
|
|
}
|
|
|
|
if st.Hash() != oAcct.Root {
|
|
|
|
return errors.New("invalid slot changes")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-13 21:49:53 +08:00
|
|
|
func (test *stateTest) verify(root common.Hash, next common.Hash, db *triedb.Database, accountsOrigin map[common.Address][]byte, storagesOrigin map[common.Address]map[common.Hash][]byte) error {
|
2023-07-11 21:43:23 +08:00
|
|
|
otr, err := trie.New(trie.StateTrieID(root), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ntr, err := trie.New(trie.StateTrieID(next), db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-31 20:07:51 +08:00
|
|
|
for addr, account := range accountsOrigin {
|
2023-07-11 21:43:23 +08:00
|
|
|
var err error
|
|
|
|
if len(account) == 0 {
|
2023-07-31 20:07:51 +08:00
|
|
|
err = test.verifyAccountCreation(next, db, otr, ntr, addr, storagesOrigin[addr])
|
2023-07-11 21:43:23 +08:00
|
|
|
} else {
|
2023-07-31 20:07:51 +08:00
|
|
|
err = test.verifyAccountUpdate(next, db, otr, ntr, addr, accountsOrigin[addr], storagesOrigin[addr])
|
2023-07-11 21:43:23 +08:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateChanges(t *testing.T) {
|
|
|
|
config := &quick.Config{MaxCount: 1000}
|
|
|
|
err := quick.Check((*stateTest).run, config)
|
|
|
|
if cerr, ok := err.(*quick.CheckError); ok {
|
|
|
|
test := cerr.In[0].(*stateTest)
|
|
|
|
t.Errorf("%v:\n%s", test.err, test)
|
|
|
|
} else if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|