Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Commit

Permalink
merge conflicts from main
Browse files Browse the repository at this point in the history
  • Loading branch information
avalonche committed Oct 3, 2023
1 parent bb61ba5 commit 3903dab
Show file tree
Hide file tree
Showing 26 changed files with 5,187 additions and 691 deletions.
692 changes: 692 additions & 0 deletions core/state/multi_tx_snapshot.go

Large diffs are not rendered by default.

929 changes: 929 additions & 0 deletions core/state/multi_tx_snapshot_test.go

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ type StateDB struct {
validRevisions []revision
nextRevisionId int

// Multi-Transaction Snapshot Stack
multiTxSnapshotStack *MultiTxSnapshotStack

// Measurements gathered during execution for debugging purposes
AccountReads time.Duration
AccountHashes time.Duration
Expand Down Expand Up @@ -166,6 +169,8 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error)
transientStorage: newTransientStorage(),
hasher: crypto.NewKeccakState(),
}

sdb.multiTxSnapshotStack = NewMultiTxSnapshotStack(sdb)
if sdb.snaps != nil {
sdb.snap = sdb.snaps.Snapshot(root)
}
Expand Down Expand Up @@ -715,6 +720,8 @@ func (s *StateDB) Copy() *StateDB {
snaps: s.snaps,
snap: s.snap,
}
// Initialize copy of multi-transaction snapshot stack for the copied state
state.multiTxSnapshotStack = s.multiTxSnapshotStack.Copy(state)
// Copy the dirty states, logs, and preimages
for addr := range s.journal.dirties {
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
Expand Down Expand Up @@ -832,6 +839,8 @@ func (s *StateDB) GetRefund() uint64 {
// the journal as well as the refunds. Finalise, however, will not push any updates
// into the tries just yet. Only IntermediateRoot or Commit will do that.
func (s *StateDB) Finalise(deleteEmptyObjects bool) {
s.multiTxSnapshotStack.UpdateFromJournal(s.journal)

addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties))
for addr := range s.journal.dirties {
obj, exist := s.stateObjects[addr]
Expand All @@ -845,6 +854,8 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
continue
}
if obj.selfDestructed || (deleteEmptyObjects && obj.empty()) {
s.multiTxSnapshotStack.UpdateObjectDeleted(obj.address, obj.deleted)

obj.deleted = true

// We need to maintain account deletions explicitly (will remain
Expand All @@ -863,7 +874,12 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
} else {
obj.finalise(true) // Prefetch slots in the background
}
obj.created = false

if s.multiTxSnapshotStack.Size() > 0 {
_, wasPending := s.stateObjectsPending[addr]
_, wasDirty := s.stateObjectsDirty[addr]
s.multiTxSnapshotStack.UpdatePendingStatus(addr, wasPending, wasDirty)
}
s.stateObjectsPending[addr] = struct{}{}
s.stateObjectsDirty[addr] = struct{}{}

Expand All @@ -886,6 +902,10 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
// Finalise all the dirty storage states and write them into the tries
s.Finalise(deleteEmptyObjects)

// Intermediate root writes updates to the trie, which will cause
// in memory multi-transaction snapshot to be incompatible with the committed state, so we invalidate.
s.multiTxSnapshotStack.Invalidate()

// If there was a trie prefetcher operating, it gets aborted and irrevocably
// modified after we start retrieving tries. Remove it from the statedb after
// this round of use.
Expand Down Expand Up @@ -1399,6 +1419,25 @@ func (s *StateDB) convertAccountSet(set map[common.Address]*types.StateAccount)
return ret
}

func (s *StateDB) NewMultiTxSnapshot() (err error) {
_, err = s.multiTxSnapshotStack.NewSnapshot()
return
}

func (s *StateDB) MultiTxSnapshotRevert() (err error) {
_, err = s.multiTxSnapshotStack.Revert()
return
}

func (s *StateDB) MultiTxSnapshotCommit() (err error) {
_, err = s.multiTxSnapshotStack.Commit()
return
}

func (s *StateDB) MultiTxSnapshotStackSize() int {
return s.multiTxSnapshotStack.Size()
}

// copySet returns a deep-copied set.
func copySet[k comparable](set map[k][]byte) map[k][]byte {
copied := make(map[k][]byte, len(set))
Expand All @@ -1418,4 +1457,4 @@ func copy2DSet[k comparable](set map[k]map[common.Hash][]byte) map[k]map[common.
}
}
return copied
}
}
Loading

0 comments on commit 3903dab

Please sign in to comment.