core/state: copy balance before stashing

This commit is contained in:
Gary Rong 2026-04-24 10:52:25 +08:00 committed by Jared Wasinger
parent a42743c2ce
commit f5c1703dca

View file

@ -78,8 +78,8 @@ func (s *journalMutationState) add(kind journalMutationKind) {
// remove drops one occurrence of the given mutation kind. It returns two // remove drops one occurrence of the given mutation kind. It returns two
// booleans: kindEmpty is true when no entries of that kind remain for the // booleans: kindEmpty is true when no entries of that kind remain for the
// account, and stateEmpty is true when no entries of any kind remain. // account, and stateEmpty is true when no entries of any kind remain.
func (s *journalMutationState) remove(kind journalMutationKind) (kindEmpty, stateEmpty bool) { func (s *journalMutationState) remove(kind journalMutationKind) (bool, bool) {
kindEmpty = s.counts.remove(kind) kindEmpty := s.counts.remove(kind)
return kindEmpty, s.counts == (journalMutationCounts{}) return kindEmpty, s.counts == (journalMutationCounts{})
} }
@ -100,8 +100,8 @@ func (s *journalMutationState) clearKind(kind journalMutationKind) {
} }
} }
func (s journalMutationState) copy() *journalMutationState { func (s *journalMutationState) copy() *journalMutationState {
cpy := s cpy := *s
if s.balance != nil { if s.balance != nil {
cpy.balance = new(uint256.Int).Set(s.balance) cpy.balance = new(uint256.Int).Set(s.balance)
} }
@ -179,7 +179,7 @@ func (j *journal) stashBalance(addr common.Address, prev *uint256.Int) {
if s.balanceSet { if s.balanceSet {
return return
} }
// The balance is already deep-copied in the StateDB // The balance is already deep-copied and safe to hold the object here.
s.balance = prev s.balance = prev
s.balanceSet = true s.balanceSet = true
} }
@ -200,7 +200,8 @@ func (j *journal) stashCode(addr common.Address, prev []byte) {
if s.codeSet { if s.codeSet {
return return
} }
// The code is already deep-copied in the StateDB // The code is already deep-copied in the StateDB, safe to
// hold the reference here.
s.code = prev s.code = prev
s.codeSet = true s.codeSet = true
} }
@ -394,10 +395,11 @@ func (j *journal) refundChange(previous uint64) {
} }
func (j *journal) balanceChange(addr common.Address, previous *uint256.Int) { func (j *journal) balanceChange(addr common.Address, previous *uint256.Int) {
j.stashBalance(addr, previous) prev := previous.Clone()
j.stashBalance(addr, prev)
j.append(balanceChange{ j.append(balanceChange{
account: addr, account: addr,
prev: previous.Clone(), prev: prev,
}) })
} }