Skip to content

Commit

Permalink
update upgrade handler
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 19, 2024
1 parent 2b02285 commit 357d9ca
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&

Check warning on line 51 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L51

Added line #L51 was not covered by tests
// 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 &&

Check warning on line 58 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L58

Added line #L58 was not covered by tests
// 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)
}

Check warning on line 83 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L62-L83

Added lines #L62 - L83 were not covered by tests

contractAcc, ok := acc.(*evmtypes.ContractAccount)
if !ok {
return true, fmt.Errorf("account is not a contract account for contract address %s", contractAddr)
}

Check warning on line 88 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L85-L88

Added lines #L85 - L88 were not covered by tests

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
}

Check warning on line 98 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L90-L98

Added lines #L90 - L98 were not covered by tests

// 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
}

Check warning on line 105 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L101-L105

Added lines #L101 - L105 were not covered by tests

return false, nil

Check warning on line 107 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L107

Added line #L107 was not covered by tests
})
if err != nil {
return true, err
return nil, err

Check warning on line 110 in app/upgrade.go

View check run for this annotation

Codecov / codecov/patch

app/upgrade.go#L110

Added line #L110 was not covered by tests
}

// 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
Expand Down

0 comments on commit 357d9ca

Please sign in to comment.