mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-24 13:46:43 +00:00
core/state: stateUpdate as Public
Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
85077be58e
commit
239318c841
3 changed files with 49 additions and 49 deletions
|
|
@ -1133,7 +1133,7 @@ func (s *StateDB) GetTrie() Trie {
|
||||||
|
|
||||||
// commit gathers the state mutations accumulated along with the associated
|
// commit gathers the state mutations accumulated along with the associated
|
||||||
// trie changes, resetting all internal flags with the new state as the base.
|
// trie changes, resetting all internal flags with the new state as the base.
|
||||||
func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool) (*stateUpdate, error) {
|
func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool) (*StateUpdate, error) {
|
||||||
// Short circuit in case any database failure occurred earlier.
|
// Short circuit in case any database failure occurred earlier.
|
||||||
if s.dbErr != nil {
|
if s.dbErr != nil {
|
||||||
return nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
|
return nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
|
||||||
|
|
@ -1290,15 +1290,15 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool) (*stateU
|
||||||
|
|
||||||
// commitAndFlush is a wrapper of commit which also commits the state mutations
|
// commitAndFlush is a wrapper of commit which also commits the state mutations
|
||||||
// to the configured data stores.
|
// to the configured data stores.
|
||||||
func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorageWiping bool) (*stateUpdate, error) {
|
func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorageWiping bool) (*StateUpdate, error) {
|
||||||
ret, err := s.commit(deleteEmptyObjects, noStorageWiping)
|
ret, err := s.commit(deleteEmptyObjects, noStorageWiping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Commit dirty contract code if any exists
|
// Commit dirty contract code if any exists
|
||||||
if db := s.db.TrieDB().Disk(); db != nil && len(ret.codes) > 0 {
|
if db := s.db.TrieDB().Disk(); db != nil && len(ret.Codes) > 0 {
|
||||||
batch := db.NewBatch()
|
batch := db.NewBatch()
|
||||||
for _, code := range ret.codes {
|
for _, code := range ret.Codes {
|
||||||
rawdb.WriteCode(batch, code.hash, code.blob)
|
rawdb.WriteCode(batch, code.hash, code.blob)
|
||||||
}
|
}
|
||||||
if err := batch.Write(); err != nil {
|
if err := batch.Write(); err != nil {
|
||||||
|
|
@ -1307,24 +1307,24 @@ func (s *StateDB) commitAndFlush(block uint64, deleteEmptyObjects bool, noStorag
|
||||||
}
|
}
|
||||||
if !ret.empty() {
|
if !ret.empty() {
|
||||||
// If snapshotting is enabled, update the snapshot tree with this new version
|
// If snapshotting is enabled, update the snapshot tree with this new version
|
||||||
if snap := s.db.Snapshot(); snap != nil && snap.Snapshot(ret.originRoot) != nil {
|
if snap := s.db.Snapshot(); snap != nil && snap.Snapshot(ret.OriginRoot) != nil {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if err := snap.Update(ret.root, ret.originRoot, ret.accounts, ret.storages); err != nil {
|
if err := snap.Update(ret.Root, ret.OriginRoot, ret.Accounts, ret.Storages); err != nil {
|
||||||
log.Warn("Failed to update snapshot tree", "from", ret.originRoot, "to", ret.root, "err", err)
|
log.Warn("Failed to update snapshot tree", "from", ret.OriginRoot, "to", ret.Root, "err", err)
|
||||||
}
|
}
|
||||||
// Keep 128 diff layers in the memory, persistent layer is 129th.
|
// Keep 128 diff layers in the memory, persistent layer is 129th.
|
||||||
// - head layer is paired with HEAD state
|
// - head layer is paired with HEAD state
|
||||||
// - head-1 layer is paired with HEAD-1 state
|
// - head-1 layer is paired with HEAD-1 state
|
||||||
// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
|
// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
|
||||||
if err := snap.Cap(ret.root, TriesInMemory); err != nil {
|
if err := snap.Cap(ret.Root, TriesInMemory); err != nil {
|
||||||
log.Warn("Failed to cap snapshot tree", "root", ret.root, "layers", TriesInMemory, "err", err)
|
log.Warn("Failed to cap snapshot tree", "root", ret.Root, "layers", TriesInMemory, "err", err)
|
||||||
}
|
}
|
||||||
s.SnapshotCommits += time.Since(start)
|
s.SnapshotCommits += time.Since(start)
|
||||||
}
|
}
|
||||||
// If trie database is enabled, commit the state update as a new layer
|
// If trie database is enabled, commit the state update as a new layer
|
||||||
if db := s.db.TrieDB(); db != nil {
|
if db := s.db.TrieDB(); db != nil {
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
if err := db.Update(ret.root, ret.originRoot, block, ret.nodes, ret.stateSet()); err != nil {
|
if err := db.Update(ret.Root, ret.OriginRoot, block, ret.Nodes, ret.stateSet()); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
s.TrieDBCommits += time.Since(start)
|
s.TrieDBCommits += time.Since(start)
|
||||||
|
|
@ -1353,7 +1353,7 @@ func (s *StateDB) Commit(block uint64, deleteEmptyObjects bool, noStorageWiping
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return common.Hash{}, err
|
return common.Hash{}, err
|
||||||
}
|
}
|
||||||
return ret.root, nil
|
return ret.Root, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare handles the preparatory steps for executing a state transition with.
|
// Prepare handles the preparatory steps for executing a state transition with.
|
||||||
|
|
|
||||||
|
|
@ -182,11 +182,11 @@ func (test *stateTest) run() bool {
|
||||||
accountOrigin []map[common.Address][]byte
|
accountOrigin []map[common.Address][]byte
|
||||||
storages []map[common.Hash]map[common.Hash][]byte
|
storages []map[common.Hash]map[common.Hash][]byte
|
||||||
storageOrigin []map[common.Address]map[common.Hash][]byte
|
storageOrigin []map[common.Address]map[common.Hash][]byte
|
||||||
copyUpdate = func(update *stateUpdate) {
|
copyUpdate = func(update *StateUpdate) {
|
||||||
accounts = append(accounts, maps.Clone(update.accounts))
|
accounts = append(accounts, maps.Clone(update.Accounts))
|
||||||
accountOrigin = append(accountOrigin, maps.Clone(update.accountsOrigin))
|
accountOrigin = append(accountOrigin, maps.Clone(update.AccountsOrigin))
|
||||||
storages = append(storages, maps.Clone(update.storages))
|
storages = append(storages, maps.Clone(update.Storages))
|
||||||
storageOrigin = append(storageOrigin, maps.Clone(update.storagesOrigin))
|
storageOrigin = append(storageOrigin, maps.Clone(update.StoragesOrigin))
|
||||||
}
|
}
|
||||||
disk = rawdb.NewMemoryDatabase()
|
disk = rawdb.NewMemoryDatabase()
|
||||||
tdb = triedb.NewDatabase(disk, &triedb.Config{PathDB: pathdb.Defaults})
|
tdb = triedb.NewDatabase(disk, &triedb.Config{PathDB: pathdb.Defaults})
|
||||||
|
|
@ -236,7 +236,7 @@ func (test *stateTest) run() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
copyUpdate(ret)
|
copyUpdate(ret)
|
||||||
roots = append(roots, ret.root)
|
roots = append(roots, ret.Root)
|
||||||
}
|
}
|
||||||
for i := 0; i < len(test.actions); i++ {
|
for i := 0; i < len(test.actions); i++ {
|
||||||
root := types.EmptyRootHash
|
root := types.EmptyRootHash
|
||||||
|
|
|
||||||
|
|
@ -60,33 +60,33 @@ type accountUpdate struct {
|
||||||
storagesOriginByHash map[common.Hash][]byte
|
storagesOriginByHash map[common.Hash][]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
// stateUpdate represents the difference between two states resulting from state
|
// StateUpdate represents the difference between two states resulting from state
|
||||||
// execution. It contains information about mutated contract codes, accounts,
|
// execution. It contains information about mutated contract codes, accounts,
|
||||||
// and storage slots, along with their original values.
|
// and storage slots, along with their original values.
|
||||||
type stateUpdate struct {
|
type StateUpdate struct {
|
||||||
originRoot common.Hash // hash of the state before applying mutation
|
OriginRoot common.Hash // hash of the state before applying mutation
|
||||||
root common.Hash // hash of the state after applying mutation
|
Root common.Hash // hash of the state after applying mutation
|
||||||
accounts map[common.Hash][]byte // accounts stores mutated accounts in 'slim RLP' encoding
|
Accounts map[common.Hash][]byte // accounts stores mutated accounts in 'slim RLP' encoding
|
||||||
accountsOrigin map[common.Address][]byte // accountsOrigin stores the original values of mutated accounts in 'slim RLP' encoding
|
AccountsOrigin map[common.Address][]byte // accountsOrigin stores the original values of mutated accounts in 'slim RLP' encoding
|
||||||
|
|
||||||
// storages stores mutated slots in 'prefix-zero-trimmed' RLP format.
|
// Storages stores mutated slots in 'prefix-zero-trimmed' RLP format.
|
||||||
// The value is keyed by account hash and **storage slot key hash**.
|
// The value is keyed by account hash and **storage slot key hash**.
|
||||||
storages map[common.Hash]map[common.Hash][]byte
|
Storages map[common.Hash]map[common.Hash][]byte
|
||||||
|
|
||||||
// storagesOrigin stores the original values of mutated slots in
|
// StoragesOrigin stores the original values of mutated slots in
|
||||||
// 'prefix-zero-trimmed' RLP format.
|
// 'prefix-zero-trimmed' RLP format.
|
||||||
// (a) the value is keyed by account hash and **storage slot key** if rawStorageKey is true;
|
// (a) the value is keyed by account hash and **storage slot key** if rawStorageKey is true;
|
||||||
// (b) the value is keyed by account hash and **storage slot key hash** if rawStorageKey is false;
|
// (b) the value is keyed by account hash and **storage slot key hash** if rawStorageKey is false;
|
||||||
storagesOrigin map[common.Address]map[common.Hash][]byte
|
StoragesOrigin map[common.Address]map[common.Hash][]byte
|
||||||
rawStorageKey bool
|
RawStorageKey bool
|
||||||
|
|
||||||
codes map[common.Address]contractCode // codes contains the set of dirty codes
|
Codes map[common.Address]contractCode // codes contains the set of dirty codes
|
||||||
nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes
|
Nodes *trienode.MergedNodeSet // Aggregated dirty nodes caused by state changes
|
||||||
}
|
}
|
||||||
|
|
||||||
// empty returns a flag indicating the state transition is empty or not.
|
// empty returns a flag indicating the state transition is empty or not.
|
||||||
func (sc *stateUpdate) empty() bool {
|
func (sc *StateUpdate) empty() bool {
|
||||||
return sc.originRoot == sc.root
|
return sc.OriginRoot == sc.Root
|
||||||
}
|
}
|
||||||
|
|
||||||
// newStateUpdate constructs a state update object by identifying the differences
|
// newStateUpdate constructs a state update object by identifying the differences
|
||||||
|
|
@ -95,7 +95,7 @@ func (sc *stateUpdate) empty() bool {
|
||||||
//
|
//
|
||||||
// rawStorageKey is a flag indicating whether to use the raw storage slot key or
|
// rawStorageKey is a flag indicating whether to use the raw storage slot key or
|
||||||
// the hash of the slot key for constructing state update object.
|
// the hash of the slot key for constructing state update object.
|
||||||
func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash, deletes map[common.Hash]*accountDelete, updates map[common.Hash]*accountUpdate, nodes *trienode.MergedNodeSet) *stateUpdate {
|
func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash, deletes map[common.Hash]*accountDelete, updates map[common.Hash]*accountUpdate, nodes *trienode.MergedNodeSet) *StateUpdate {
|
||||||
var (
|
var (
|
||||||
accounts = make(map[common.Hash][]byte)
|
accounts = make(map[common.Hash][]byte)
|
||||||
accountsOrigin = make(map[common.Address][]byte)
|
accountsOrigin = make(map[common.Address][]byte)
|
||||||
|
|
@ -161,16 +161,16 @@ func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &stateUpdate{
|
return &StateUpdate{
|
||||||
originRoot: originRoot,
|
OriginRoot: originRoot,
|
||||||
root: root,
|
Root: root,
|
||||||
accounts: accounts,
|
Accounts: accounts,
|
||||||
accountsOrigin: accountsOrigin,
|
AccountsOrigin: accountsOrigin,
|
||||||
storages: storages,
|
Storages: storages,
|
||||||
storagesOrigin: storagesOrigin,
|
StoragesOrigin: storagesOrigin,
|
||||||
rawStorageKey: rawStorageKey,
|
RawStorageKey: rawStorageKey,
|
||||||
codes: codes,
|
Codes: codes,
|
||||||
nodes: nodes,
|
Nodes: nodes,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -178,12 +178,12 @@ func newStateUpdate(rawStorageKey bool, originRoot common.Hash, root common.Hash
|
||||||
// object. This function extracts the necessary data from the stateUpdate
|
// object. This function extracts the necessary data from the stateUpdate
|
||||||
// struct and formats it into the StateSet structure consumed by the triedb
|
// struct and formats it into the StateSet structure consumed by the triedb
|
||||||
// package.
|
// package.
|
||||||
func (sc *stateUpdate) stateSet() *triedb.StateSet {
|
func (sc *StateUpdate) stateSet() *triedb.StateSet {
|
||||||
return &triedb.StateSet{
|
return &triedb.StateSet{
|
||||||
Accounts: sc.accounts,
|
Accounts: sc.Accounts,
|
||||||
AccountsOrigin: sc.accountsOrigin,
|
AccountsOrigin: sc.AccountsOrigin,
|
||||||
Storages: sc.storages,
|
Storages: sc.Storages,
|
||||||
StoragesOrigin: sc.storagesOrigin,
|
StoragesOrigin: sc.StoragesOrigin,
|
||||||
RawStorageKey: sc.rawStorageKey,
|
RawStorageKey: sc.RawStorageKey,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue