Commit 5e11bb1b authored by Matthew Slipper's avatar Matthew Slipper

op-chain-ops: Do not manually copy balance in WipePredeployStorage

`db.CreateAccount` copies over the balance, but wipes nonces. The previous version of this code manually copied the balance after calling `db.CreateAccount`, which effectively doubled the predeploy's balance. None of the L2 predeploys carried balances on Goerli, however to prevent ETH from being created and destroyed during the migration this PR removes the manual balance migration and adds a test to ensure that nonces are copied and state is properly wiped.

Fixes CLI-3443
parent e36ddb03
...@@ -95,14 +95,10 @@ func WipePredeployStorage(db vm.StateDB) error { ...@@ -95,14 +95,10 @@ func WipePredeployStorage(db vm.StateDB) error {
// We need to make sure that we preserve nonces. // We need to make sure that we preserve nonces.
oldNonce := db.GetNonce(*addr) oldNonce := db.GetNonce(*addr)
oldBalance := db.GetBalance(*addr)
db.CreateAccount(*addr) db.CreateAccount(*addr)
if oldNonce > 0 { if oldNonce > 0 {
db.SetNonce(*addr, oldNonce) db.SetNonce(*addr, oldNonce)
} }
if oldBalance.Cmp(common.Big0) != 0 {
db.AddBalance(*addr, oldBalance)
}
} }
return nil return nil
......
package genesis
import (
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-bindings/predeploys"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/trie"
"github.com/stretchr/testify/require"
)
func TestWipePredeployStorage(t *testing.T) {
rawDB := rawdb.NewMemoryDatabase()
rawStateDB := state.NewDatabaseWithConfig(rawDB, &trie.Config{
Preimages: true,
Cache: 1024,
})
stateDB, err := state.New(common.Hash{}, rawStateDB, nil)
require.NoError(t, err)
storeVal := common.Hash{31: 0xff}
for _, addr := range predeploys.Predeploys {
a := *addr
stateDB.SetState(a, storeVal, storeVal)
stateDB.SetBalance(a, big.NewInt(99))
stateDB.SetNonce(a, 99)
}
root, err := stateDB.Commit(false)
require.NoError(t, err)
err = stateDB.Database().TrieDB().Commit(root, true)
require.NoError(t, err)
require.NoError(t, WipePredeployStorage(stateDB))
for _, addr := range predeploys.Predeploys {
a := *addr
if FrozenStoragePredeploys[a] {
require.Equal(t, storeVal, stateDB.GetState(a, storeVal))
} else {
require.Equal(t, common.Hash{}, stateDB.GetState(a, storeVal))
}
require.Equal(t, big.NewInt(99), stateDB.GetBalance(a))
require.Equal(t, uint64(99), stateDB.GetNonce(a))
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment