Skip to content

Commit

Permalink
Merge branch 'main' into feat/tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Nov 22, 2024
2 parents 59e70ea + 9bcc1da commit 7c0dcf6
Show file tree
Hide file tree
Showing 36 changed files with 518 additions and 258 deletions.
3 changes: 2 additions & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ ignore:
- "**/test_utils.go"
- "**/module.go"
- "x/ibc/testing"
- "**/contracts"
- "x/evm/contracts"
- "**/*.sol"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ mytestnet
coverage.txt
profile.out
sim_log_file
testdata

# Vagrant
.vagrant/
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ contracts-gen: $(CONTRACTS_DIR)/*
benchmark:
@go test -timeout 20m -mod=readonly -bench=. ./...

fuzz:
@go test --timeout 2m -mod=readonly -fuzz=Fuzz ./x/evm/keeper

.PHONY: test test-all test-cover test-unit test-race benchmark contracts-gen

###############################################################################
Expand Down
19 changes: 13 additions & 6 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

opchildante "github.com/initia-labs/OPinit/x/opchild/ante"
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
opchildkeeper "github.com/initia-labs/OPinit/x/opchild/keeper"
"github.com/initia-labs/initia/app/ante/accnum"
"github.com/initia-labs/initia/app/ante/sigverify"
evmante "github.com/initia-labs/minievm/x/evm/ante"
Expand All @@ -28,8 +28,8 @@ type HandlerOptions struct {
ante.HandlerOptions
Codec codec.BinaryCodec
IBCkeeper *ibckeeper.Keeper
OPChildKeeper opchildtypes.AnteKeeper
AuctionKeeper auctionkeeper.Keeper
OPChildKeeper *opchildkeeper.Keeper
AuctionKeeper *auctionkeeper.Keeper
EVMKeeper EVMKeeper

TxEncoder sdk.TxEncoder
Expand All @@ -44,18 +44,24 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
}

if options.BankKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder")
}

if options.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}

if options.EVMKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "EVM keeper is required for ante builder")
}
if options.IBCkeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "IBC keeper is required for ante builder")
}
if options.OPChildKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "OPChild keeper is required for ante builder")
}
if options.AuctionKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "Auction keeper is required for ante builder")
}

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
Expand Down Expand Up @@ -100,6 +106,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
evmante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewRedundantRelayDecorator(options.IBCkeeper),
auctionante.NewAuctionDecorator(options.AuctionKeeper, options.TxEncoder, options.MevLane),
opchildante.NewRedundantBridgeDecorator(options.OPChildKeeper),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
Expand Down
2 changes: 1 addition & 1 deletion app/blocksdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func setupBlockSDK(
Codec: app.appCodec,
OPChildKeeper: app.OPChildKeeper,
TxEncoder: app.txConfig.TxEncoder(),
AuctionKeeper: *app.AuctionKeeper,
AuctionKeeper: app.AuctionKeeper,
MevLane: mevLane,
FreeLane: freeLane,
EVMKeeper: app.EVMKeeper,
Expand Down
1 change: 0 additions & 1 deletion app/ibc-hooks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ func _createTestInput(
ac,
appCodec,
runtime.NewKVStoreService(keys[evmtypes.StoreKey]),
runtime.NewTransientStoreService(tkeys[evmtypes.TStoreKey]),
accountKeeper,
bankKeeper,
communityPoolKeeper,
Expand Down
3 changes: 1 addition & 2 deletions app/ibc-hooks/receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
nfttransfertypes "github.com/initia-labs/initia/x/ibc/nft-transfer/types"
evm_hooks "github.com/initia-labs/minievm/app/ibc-hooks"
"github.com/initia-labs/minievm/x/evm/contracts/counter"
"github.com/initia-labs/minievm/x/evm/types"
evmtypes "github.com/initia-labs/minievm/x/evm/types"
)

Expand Down Expand Up @@ -111,7 +110,7 @@ func Test_onReceiveIcs20Packet_memo(t *testing.T) {
require.Equal(t, uint256.NewInt(1).Bytes32(), [32]byte(queryRes))

// check allowance
erc20Addr, err := types.DenomToContractAddr(ctx, input.EVMKeeper, localDenom)
erc20Addr, err := evmtypes.DenomToContractAddr(ctx, input.EVMKeeper, localDenom)
require.NoError(t, err)
queryInputBz, err = input.EVMKeeper.ERC20Keeper().GetERC20ABI().Pack("allowance", common.BytesToAddress(intermediateSender.Bytes()), contractAddr)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ func NewAppKeeper(
appKeepers.FeeGrantKeeper = &feeGrantKeeper

authzKeeper := authzkeeper.NewKeeper(runtime.NewKVStoreService(appKeepers.keys[authzkeeper.StoreKey]), appCodec, bApp.MsgServiceRouter(), appKeepers.AccountKeeper)
authzKeeper = authzKeeper.SetBankKeeper(appKeepers.BankKeeper)
appKeepers.AuthzKeeper = &authzKeeper

groupConfig := group.DefaultConfig()
Expand Down Expand Up @@ -553,7 +554,6 @@ func NewAppKeeper(
ac,
appCodec,
runtime.NewKVStoreService(appKeepers.keys[evmtypes.StoreKey]),
runtime.NewTransientStoreService(appKeepers.tkeys[evmtypes.TStoreKey]),
accountKeeper,
bankKeeper,
communityPoolKeeper,
Expand Down
2 changes: 1 addition & 1 deletion app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (appKeepers *AppKeepers) GenerateKeys() {
)

// Define transient store keys
appKeepers.tkeys = storetypes.NewTransientStoreKeys(evmtypes.TStoreKey, forwardingtypes.TransientStoreKey)
appKeepers.tkeys = storetypes.NewTransientStoreKeys(forwardingtypes.TransientStoreKey)

// MemKeys are for information that is stored only in RAM.
appKeepers.memKeys = storetypes.NewMemoryStoreKeys(capabilitytypes.MemStoreKey)
Expand Down
101 changes: 53 additions & 48 deletions app/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
)

const upgradeName = "0.6.5"
const upgradeName = "0.6.6"

// RegisterUpgradeHandlers returns upgrade handlers
func (app *MinitiaApp) RegisterUpgradeHandlers(cfg module.Configurator) {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ require (
github.com/cosmos/ibc-apps/modules/rate-limiting/v8 v8.0.0
github.com/cosmos/ibc-go/modules/capability v1.0.1
github.com/cosmos/ibc-go/v8 v8.5.0
github.com/ethereum/go-ethereum v1.14.9
github.com/ethereum/go-ethereum v1.14.11
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.4
github.com/gorilla/mux v1.8.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/go-metrics v0.5.3
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/holiman/uint256 v1.3.1
github.com/initia-labs/OPinit v0.5.7
github.com/initia-labs/OPinit v0.6.1
github.com/initia-labs/initia v0.6.1
github.com/initia-labs/kvindexer v0.1.9
github.com/initia-labs/kvindexer/submodules/block v0.1.0
Expand Down Expand Up @@ -178,7 +178,7 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/initia-labs/OPinit/api v0.5.7 // indirect
github.com/initia-labs/OPinit/api v0.6.0 // indirect
github.com/initia-labs/kvindexer/submodules/tx v0.1.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1433,10 +1433,10 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/OPinit v0.5.7 h1:25G0o2VXcO4EXzh9afdTdsXl1PTdYG9VMUx4efmWCuM=
github.com/initia-labs/OPinit v0.5.7/go.mod h1:lx1amLMszculwPu8ln+btJno38UV28fd2nP7XC88ZeE=
github.com/initia-labs/OPinit/api v0.5.7 h1:jghTmrrDEmowrqSpy7iL7IW9xJh40rn/S1y/LrwPkyE=
github.com/initia-labs/OPinit/api v0.5.7/go.mod h1:gHK6DEWb3/DqQD5LjKirUx9jilAh2UioXanoQdgqVfU=
github.com/initia-labs/OPinit v0.6.1 h1:G9ebeYeqPlV9Z2s3JdSWfwQAUgIM+nhkcA8xSJUMR7M=
github.com/initia-labs/OPinit v0.6.1/go.mod h1:gDpCh4Zx94mihwgzP/PLav8eVHLroZBu3dFyzCy8iIs=
github.com/initia-labs/OPinit/api v0.6.0 h1:Q3hDHpTd9EqlDfY/OryCKIwuXYWJxGJdGfJicV1RjL4=
github.com/initia-labs/OPinit/api v0.6.0/go.mod h1:gHK6DEWb3/DqQD5LjKirUx9jilAh2UioXanoQdgqVfU=
github.com/initia-labs/cometbft v0.0.0-20241113064430-a371e2dc387f h1:PpYBvJ0L58bAgH7KwS/7GBEUphLnhf4fnTLua9uOov8=
github.com/initia-labs/cometbft v0.0.0-20241113064430-a371e2dc387f/go.mod h1:y7+6kPknafzWlkSMCekzXC81wpRf1pcVAUnO0wRy9lQ=
github.com/initia-labs/cometbft-client v0.0.0-20240924071428-ef115cefa07e h1:k+pg63SFozCAK4LZFSiZtof6z69Tlu0O/Zftj1aAwes=
Expand Down
6 changes: 3 additions & 3 deletions integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/cometbft/cometbft v0.38.12
github.com/cosmos/cosmos-sdk v0.50.10
github.com/cosmos/ibc-go/v8 v8.5.0
github.com/ethereum/go-ethereum v1.14.9
github.com/ethereum/go-ethereum v1.14.11
github.com/initia-labs/initia v0.6.1
github.com/initia-labs/minievm v0.5.2
github.com/stretchr/testify v1.9.0
Expand Down Expand Up @@ -146,8 +146,8 @@ require (
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/improbable-eng/grpc-web v0.15.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/initia-labs/OPinit v0.5.7 // indirect
github.com/initia-labs/OPinit/api v0.5.7 // indirect
github.com/initia-labs/OPinit v0.6.1 // indirect
github.com/initia-labs/OPinit/api v0.6.0 // indirect
github.com/initia-labs/kvindexer v0.1.9 // indirect
github.com/initia-labs/kvindexer/submodules/block v0.1.0 // indirect
github.com/initia-labs/kvindexer/submodules/evm-tx v0.1.2 // indirect
Expand Down
6 changes: 2 additions & 4 deletions integration-tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1393,10 +1393,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/initia-labs/OPinit v0.5.7 h1:25G0o2VXcO4EXzh9afdTdsXl1PTdYG9VMUx4efmWCuM=
github.com/initia-labs/OPinit v0.5.7/go.mod h1:lx1amLMszculwPu8ln+btJno38UV28fd2nP7XC88ZeE=
github.com/initia-labs/OPinit/api v0.5.7 h1:jghTmrrDEmowrqSpy7iL7IW9xJh40rn/S1y/LrwPkyE=
github.com/initia-labs/OPinit/api v0.5.7/go.mod h1:gHK6DEWb3/DqQD5LjKirUx9jilAh2UioXanoQdgqVfU=
github.com/initia-labs/OPinit v0.6.1 h1:G9ebeYeqPlV9Z2s3JdSWfwQAUgIM+nhkcA8xSJUMR7M=
github.com/initia-labs/OPinit/api v0.6.0 h1:Q3hDHpTd9EqlDfY/OryCKIwuXYWJxGJdGfJicV1RjL4=
github.com/initia-labs/cometbft v0.0.0-20241113064430-a371e2dc387f h1:PpYBvJ0L58bAgH7KwS/7GBEUphLnhf4fnTLua9uOov8=
github.com/initia-labs/cometbft v0.0.0-20241113064430-a371e2dc387f/go.mod h1:y7+6kPknafzWlkSMCekzXC81wpRf1pcVAUnO0wRy9lQ=
github.com/initia-labs/evm v0.0.0-20241108055119-3d312736d7fb h1:oyH9gg/4f7uMCIJYnSpp7wa1NrGjSMsXTtypUfrsPLU=
Expand Down
6 changes: 3 additions & 3 deletions jsonrpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ type AccMut struct {

// acquireAccMut acquires the mutex for the account with the given senderHex
// and increments the reference count. If the mutex does not exist, it is created.
func (b *JSONRPCBackend) acquireAccMut(senderHex string) {
func (b *JSONRPCBackend) acquireAccMut(senderHex string) *AccMut {
// critical section for rc and create
b.mut.Lock()
accMut, ok := b.accMuts[senderHex]
Expand All @@ -205,13 +205,13 @@ func (b *JSONRPCBackend) acquireAccMut(senderHex string) {
// critical section end

accMut.mut.Lock()
return accMut
}

// releaseAccMut releases the mutex for the account with the given senderHex
// and decrements the reference count. If the reference count reaches zero,
// the mutex is deleted.
func (b *JSONRPCBackend) releaseAccMut(senderHex string) {
accMut := b.accMuts[senderHex]
func (b *JSONRPCBackend) releaseAccMut(senderHex string, accMut *AccMut) {
accMut.mut.Unlock()

// critical section for rc and delete
Expand Down
4 changes: 2 additions & 2 deletions jsonrpc/backend/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func (b *JSONRPCBackend) SendTx(tx *coretypes.Transaction) error {
senderHex := hexutil.Encode(sender.Bytes())

// hold mutex for each sender
b.acquireAccMut(senderHex)
defer b.releaseAccMut(senderHex)
accMut := b.acquireAccMut(senderHex)
defer b.releaseAccMut(senderHex, accMut)

checkCtx := b.app.GetContextForCheckTx(nil)
if acc := b.app.AccountKeeper.GetAccount(checkCtx, sender); acc != nil {
Expand Down
1 change: 0 additions & 1 deletion x/bank/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,6 @@ func _createTestInput(
ac,
appCodec,
runtime.NewKVStoreService(keys[evmtypes.StoreKey]),
runtime.NewTransientStoreService(tkeys[evmtypes.TStoreKey]),
accountKeeper,
bankKeeper,
communityPoolKeeper,
Expand Down
25 changes: 23 additions & 2 deletions x/evm/contracts/counter/Counter.go

Large diffs are not rendered by default.

Loading

0 comments on commit 7c0dcf6

Please sign in to comment.