Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions op-chain-ops/cmd/celo-migrate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func applyStateMigrationChanges(config *genesis.DeployConfig, genesis *core.Gene
}

// Apply the changes to the state DB.
applyAllocsToState(db, genesis, cfg)
err = applyAllocsToState(db, genesis, cfg)
if err != nil {
return nil, fmt.Errorf("cannot apply allocations to state: %w", err)
}

// Initialize the distribution schedule contract
// This uses the original config which won't enable recent hardforks (and things like the PUSH0 opcode)
Expand Down Expand Up @@ -242,40 +245,53 @@ func applyStateMigrationChanges(config *genesis.DeployConfig, genesis *core.Gene
// If an account already exists, it adds the balance of the new account to the existing balance.
// If the code of an existing account is different from the code in the genesis block, it logs a warning.
// This changes the state root, so `Commit` needs to be called after this function.
func applyAllocsToState(db *state.StateDB, genesis *core.Genesis, config *params.ChainConfig) {
func applyAllocsToState(db *state.StateDB, genesis *core.Genesis, config *params.ChainConfig) error {
log.Info("Starting to migrate OP contracts into state DB")

accountCounter := 0
overwriteCounter := 0
for k, v := range genesis.Alloc {
Comment thread
palango marked this conversation as resolved.
accountCounter++

balance := uint256.MustFromBig(v.Balance)
// Check that the balance of the account to written is zero,
// as we must not create new CELo tokens
if v.Balance.Cmp(big.NewInt(0)) != 0 {
log.Error("Account balance is not zero, would change token supply", "address", k.Hex())
return fmt.Errorf("account balance is not zero, would change token supply: %s", k.Hex())
}

nonce := v.Nonce
if db.Exist(k) {
// If the account already has balance, add it to the balance of the new account
balance = balance.Add(balance, db.GetBalance(k))
// Check if it's an EOA, if so bail out
if db.GetCodeSize(k) == 0 && db.GetNonce(k) > 0 {
return fmt.Errorf("account already exists with nonce > 0: %s", k.Hex())
}

currentCode := db.GetCode(k)
equalCode := bytes.Equal(currentCode, v.Code)
if currentCode != nil && !equalCode {
if whitelist, exists := accountOverwriteWhitelist[config.ChainID.Uint64()]; exists {
if _, ok := whitelist[k]; ok {
log.Info("Account already exists with different code and is whitelisted, overwriting...", "address", k)
// keep the existing nonce
nonce = db.GetNonce(k)
log.Info("Account already exists with different code and is whitelisted, overwriting...", "address", k, "nonce", db.GetNonce(k))
} else {
log.Warn("Account already exists with different code and is not whitelisted, overwriting...", "address", k, "oldCode", db.GetCode(k), "newCode", v.Code)
log.Error("Account already exists with different code and is not whitelisted, overwriting...", "address", k, "oldCode", db.GetCode(k), "newCode", v.Code)
Comment thread
palango marked this conversation as resolved.
Outdated
return fmt.Errorf("account already exists with different code and is not whitelisted: %s", k.Hex())
Comment thread
palango marked this conversation as resolved.
Outdated
}
} else {
log.Warn("Account already exists with different code and no whitelist exists", "address", k, "oldCode", db.GetCode(k), "newCode", v.Code)
log.Error("Account already exists with different code and no whitelist exists", "address", k, "oldCode", db.GetCode(k), "newCode", v.Code)
return fmt.Errorf("account already exists with different code and no whitelist exists: %s", k.Hex())
}

overwriteCounter++
Comment thread
palango marked this conversation as resolved.
Outdated
}
}

// This carries over any existing balance
db.CreateAccount(k)

db.SetNonce(k, v.Nonce)
db.SetBalance(k, balance)
db.SetNonce(k, nonce)
Comment thread
palango marked this conversation as resolved.
Outdated
db.SetCode(k, v.Code)
for key, value := range v.Storage {
db.SetState(k, key, value)
Expand All @@ -284,6 +300,7 @@ func applyAllocsToState(db *state.StateDB, genesis *core.Genesis, config *params
log.Info("Moved account", "address", k)
}
log.Info("Migrated OP contracts into state DB", "copiedAccounts", accountCounter, "overwrittenAccounts", overwriteCounter)
return nil
}

// setupDistributionSchedule sets up the distribution schedule contract with the correct balance
Expand Down