Skip to content

Commit

Permalink
migration: Overwrite create2deployer code (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
palango authored Sep 23, 2024
1 parent ecc4700 commit 3d7ab07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
24 changes: 15 additions & 9 deletions op-chain-ops/cmd/celo-migrate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ var (
// Add any addresses that should be allowed to overwrite existing accounts here.
AlfajoresNetworkID: {
// Create2Deployer
common.HexToAddress("0x13b0D85CcB8bf860b6b79AF3029fCA081AE9beF2"): false,
// OP uses a version without an owner who can pause the contract,
// so we overwrite the existing contract during migration
common.HexToAddress("0x13b0D85CcB8bf860b6b79AF3029fCA081AE9beF2"): true,

// Same code as in allocs file
// EntryPoint_v070
Expand Down Expand Up @@ -306,8 +308,8 @@ func applyAllocsToState(db vm.StateDB, allocs types.GenesisAlloc, allowlist map[
}

if db.Exist(k) {
var allowed bool
overwrite, allowed := allowlist[k]
writeNonceAndStorage := false
writeCode, allowed := allowlist[k]

// If the account is not allowed and has a non zero nonce or code size, bail out we will need to manually investigate how to handle this.
if !allowed && (db.GetCodeSize(k) > 0 || db.GetNonce(k) > 0) {
Expand All @@ -316,18 +318,22 @@ func applyAllocsToState(db vm.StateDB, allocs types.GenesisAlloc, allowlist map[

// This means that the account just has balance, in that case we wan to copy over the account
if db.GetCodeSize(k) == 0 && db.GetNonce(k) == 0 {
overwrite = true
writeCode = true
writeNonceAndStorage = true
}

if overwrite {
if writeCode {
overwriteCounter++

db.SetCode(k, v.Code)
db.SetNonce(k, v.Nonce)
for key, value := range v.Storage {
db.SetState(k, key, value)

if writeNonceAndStorage {
db.SetNonce(k, v.Nonce)
for key, value := range v.Storage {
db.SetState(k, key, value)
}
}
log.Info("Overwrote account", "address", k.Hex())
log.Info("Overwrote account", "address", k.Hex(), "writeNonceAndStorage", writeNonceAndStorage)
}
continue
}
Expand Down
10 changes: 8 additions & 2 deletions op-chain-ops/cmd/celo-migrate/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ func TestApplyAllocsToState(t *testing.T) {
wantErr: true,
},
{
name: "Write account with allowlist overwrites",
name: "Write account with allowlist overwrite, keeps nonce",
existingAccount: &types.Account{
Balance: big.NewInt(defaultBalance),
Nonce: 4,
Code: bytes.Repeat([]byte{0x01}, 10),
},
newAccount: types.Account{
Expand Down Expand Up @@ -128,9 +129,14 @@ func TestApplyAllocsToState(t *testing.T) {
t.Errorf("account does not exists as expected: %v", address.Hex())
}

assert.Equal(t, tt.newAccount.Nonce, sdb.GetNonce(address))
assert.Equal(t, tt.newAccount.Code, sdb.GetCode(address))

if tt.existingAccount != nil && tt.existingAccount.Nonce != 0 {
assert.Equal(t, tt.existingAccount.Nonce, sdb.GetNonce(address))
} else {
assert.Equal(t, tt.newAccount.Nonce, sdb.GetNonce(address))
}

if tt.existingAccount != nil {
assert.True(t, big.NewInt(defaultBalance).Cmp(sdb.GetBalance(address).ToBig()) == 0)
} else {
Expand Down

0 comments on commit 3d7ab07

Please sign in to comment.