Skip to content

Commit

Permalink
Merge pull request #5212 from onflow/ramtin/fix-evm-statedb-create-func
Browse files Browse the repository at this point in the history
[Beyond EVM] Updateing StateDB createAccount behaviour to match Geth's latest version
  • Loading branch information
ramtinms authored Jan 12, 2024
2 parents b384458 + d908c60 commit 7c2f987
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions fvm/evm/emulator/state/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,35 @@ func (d *DeltaView) Exist(addr gethCommon.Address) (bool, error) {

// CreateAccount creates a new account for the given address
//
// if address has been flaged earlier for destruction, carry over the balance
// if address already extists (even if destructed), carry over the balance
// and reset the data from the orginal account.
func (d *DeltaView) CreateAccount(addr gethCommon.Address) error {
// if is already created return
if d.IsCreated(addr) {
return nil
}
exist, err := d.Exist(addr)
if err != nil {
return err
}
if exist {
// check if already destructed
destructed, balance := d.HasSelfDestructed(addr)
if !destructed {
balance, err = d.GetBalance(addr)
if err != nil {
return err
}
err = d.SelfDestruct(addr)
if err != nil {
return err
}
}

d.dirtyAddresses[addr] = struct{}{}

// if it has flagged for destruction in this transction,
// reset everything and carry over the balance
destructed, balance := d.HasSelfDestructed(addr)
if destructed {
d.nonces[addr] = 0
d.codes[addr] = nil
d.codeHashes[addr] = gethTypes.EmptyCodeHash
// carrying over the balance. (legacy behaviour of the Geth stateDB)
d.balances[addr] = balance

// flag addr as recreated, this flag helps with postponing deletion of slabs
Expand All @@ -131,16 +143,9 @@ func (d *DeltaView) CreateAccount(addr gethCommon.Address) error {
delete(d.slots, k)
}
}
return nil
}

d.dirtyAddresses[addr] = struct{}{}
d.created[addr] = struct{}{}
// Carrying over the balance ensures that Ether doesn't disappear. (legacy behaviour of the Geth stateDB)
bal, err := d.GetBalance(addr)
if err != nil {
return err
}
d.balances[addr] = bal
return nil
}

Expand Down

0 comments on commit 7c2f987

Please sign in to comment.