Skip to content

Commit

Permalink
avoid deepCopy in mergeDB when useDAG
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny2022da committed Sep 13, 2024
1 parent e9ed3cd commit 6bbc2a1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 9 additions & 0 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,15 @@ func (s *stateObject) deepCopy(db *StateDB) *stateObject {
return object
}

// rebase is similar with deepCopy, instead of do copy, it just rebase the db of the Object.
// it is used for the case that the original state object in the mainDB is obsoleted or does not exist,
// so that we can reuse the one generated in slot execution.
func (object *stateObject) rebase(db *StateDB) *stateObject {
object.db = db.getBaseStateDB()
object.dbItf = db
return object
}

func (s *stateObject) MergeSlotObject(db Database, dirtyObjs *stateObject, keys StateKeys) {
for key := range keys {
// In parallel mode, always GetState by StateDB, not by StateObject directly,
Expand Down
21 changes: 18 additions & 3 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2512,7 +2512,12 @@ func (s *StateDB) MergeSlotDB(slotDb *ParallelStateDB, slotReceipt *types.Receip
mainObj, exist := s.loadStateObj(addr)
if !exist || mainObj.deleted {
// addr not exist on main DB, the object is created in the merging tx.
mainObj = dirtyObj.deepCopy(s)
if slotDb.parallel.useDAG {
// if use DAG, unconfirmed DB is not touched, so the dirtyObj can be used directly
mainObj = dirtyObj.rebase(s)
} else {
mainObj = dirtyObj.deepCopy(s)
}
if !dirtyObj.deleted {
mainObj.finalise(true)
}
Expand Down Expand Up @@ -2554,7 +2559,12 @@ func (s *StateDB) MergeSlotDB(slotDb *ParallelStateDB, slotReceipt *types.Receip
// may not empty until block validation. so the pendingStorage filled by the execution of previous txs
// in same block may get overwritten by deepCopy here, which causes issue in root calculation.
if _, created := s.parallel.createdObjectRecord[addr]; created {
newMainObj = dirtyObj.deepCopy(s)
if slotDb.parallel.useDAG {
// if use DAG, unconfirmed DB is not touched, so the dirtyObj can be used directly
newMainObj = dirtyObj.rebase(s)
} else {
newMainObj = dirtyObj.deepCopy(s)
}
} else {
// Merge the dirtyObject with mainObject
if _, balanced := slotDb.parallel.balanceChangesInSlot[addr]; balanced {
Expand All @@ -2579,7 +2589,12 @@ func (s *StateDB) MergeSlotDB(slotDb *ParallelStateDB, slotReceipt *types.Receip
}
} else {
// The object is deleted in the TX.
newMainObj = dirtyObj.deepCopy(s)
if slotDb.parallel.useDAG {
// if use DAG, unconfirmed DB is not touched, so the dirtyObj can be used directly
newMainObj = dirtyObj.rebase(s)
} else {
newMainObj = dirtyObj.deepCopy(s)
}
}

// All cases with addrStateChange set to true/false can be deleted. so handle it here.
Expand Down

0 comments on commit 6bbc2a1

Please sign in to comment.