From 357d9cac9a22d57bd104663c9614cc3fdb261a5f Mon Sep 17 00:00:00 2001 From: beer-1 Date: Tue, 19 Nov 2024 19:26:31 +0900 Subject: [PATCH] update upgrade handler --- app/upgrade.go | 99 ++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/app/upgrade.go b/app/upgrade.go index 241b995..6429fd4 100644 --- a/app/upgrade.go +++ b/app/upgrade.go @@ -47,63 +47,68 @@ func (app *MinitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) { //////////////////////////// MINIEVM /////////////////////////////////// - // deploy and store erc20 factory contract address - if err := app.EVMKeeper.DeployERC20Factory(ctx); err != nil && + // deploy and store erc20 wrapper contract address + if err := app.EVMKeeper.DeployERC20Wrapper(ctx); err != nil && // ignore contract address collision error (contract already deployed) !strings.Contains(err.Error(), vm.ErrContractAddressCollision.Error()) { return nil, err } - // deploy and store erc20 wrapper contract address - if err := app.EVMKeeper.DeployERC20Wrapper(ctx); err != nil && + // deploy and store erc20 factory contract address + if err := app.EVMKeeper.DeployERC20Factory(ctx); err != nil && // ignore contract address collision error (contract already deployed) !strings.Contains(err.Error(), vm.ErrContractAddressCollision.Error()) { return nil, err - } - - code := hexutil.MustDecode(erc20.Erc20MetaData.Bin) - - // runtime code - initCodeOP := common.Hex2Bytes("5ff3fe") - initCodePos := bytes.Index(code, initCodeOP) - code = code[initCodePos+3:] - - // code hash - codeHash := crypto.Keccak256Hash(code).Bytes() - - // iterate all erc20 contracts and replace contract code to new version - err = app.EVMKeeper.ERC20s.Walk(ctx, nil, func(contractAddr []byte) (bool, error) { - acc := app.AccountKeeper.GetAccount(ctx, contractAddr) - if acc == nil { - return true, fmt.Errorf("account not found for contract address %s", contractAddr) - } - - contractAcc, ok := acc.(*evmtypes.ContractAccount) - if !ok { - return true, fmt.Errorf("account is not a contract account for contract address %s", contractAddr) - } - - contractAcc.CodeHash = codeHash - app.AccountKeeper.SetAccount(ctx, contractAcc) - - // set code - codeKey := append(contractAddr, append(state.CodeKeyPrefix, codeHash...)...) - err := app.EVMKeeper.VMStore.Set(ctx, codeKey, code) + } else if err == nil { + // update erc20 contracts only if erc20 factory contract has been deployed without error. + // + // address collision error is ignored because it means that the contract has already been deployed + // and the erc20 contracts have already been updated. + // + code := hexutil.MustDecode(erc20.Erc20MetaData.Bin) + + // runtime code + initCodeOP := common.Hex2Bytes("5ff3fe") + initCodePos := bytes.Index(code, initCodeOP) + code = code[initCodePos+3:] + + // code hash + codeHash := crypto.Keccak256Hash(code).Bytes() + + // iterate all erc20 contracts and replace contract code to new version + err = app.EVMKeeper.ERC20s.Walk(ctx, nil, func(contractAddr []byte) (bool, error) { + acc := app.AccountKeeper.GetAccount(ctx, contractAddr) + if acc == nil { + return true, fmt.Errorf("account not found for contract address %s", contractAddr) + } + + contractAcc, ok := acc.(*evmtypes.ContractAccount) + if !ok { + return true, fmt.Errorf("account is not a contract account for contract address %s", contractAddr) + } + + contractAcc.CodeHash = codeHash + app.AccountKeeper.SetAccount(ctx, contractAcc) + + // set code + codeKey := append(contractAddr, append(state.CodeKeyPrefix, codeHash...)...) + err := app.EVMKeeper.VMStore.Set(ctx, codeKey, code) + if err != nil { + return true, err + } + + // set code size + codeSizeKey := append(contractAddr, append(state.CodeSizeKeyPrefix, codeHash...)...) + err = app.EVMKeeper.VMStore.Set(ctx, codeSizeKey, uint64ToBytes(uint64(len(code)))) + if err != nil { + return true, err + } + + return false, nil + }) if err != nil { - return true, err + return nil, err } - - // set code size - codeSizeKey := append(contractAddr, append(state.CodeSizeKeyPrefix, codeHash...)...) - err = app.EVMKeeper.VMStore.Set(ctx, codeSizeKey, uint64ToBytes(uint64(len(code)))) - if err != nil { - return true, err - } - - return false, nil - }) - if err != nil { - return nil, err } return versionMap, nil