From d0817133dbd0c33e31af753e3e33d4151abb3c19 Mon Sep 17 00:00:00 2001 From: dreamer Date: Mon, 1 Jul 2024 12:04:52 +0800 Subject: [PATCH] refactor code --- README.md | 35 ++++++++- e2e/coinswap/query.go | 13 +--- e2e/farm/query.go | 16 +--- e2e/go.mod | 2 +- e2e/htlc/tx.go | 5 +- e2e/mock.go | 170 ------------------------------------------ e2e/suite.go | 5 +- 7 files changed, 45 insertions(+), 201 deletions(-) delete mode 100644 e2e/mock.go diff --git a/README.md b/README.md index ff2db4ff..669e19bd 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,37 @@ IRISnet Modules **Note**: This repository is meant to house modules that are created outside of the [IRIS Hub](https://github.com/irisnet/irishub) repository. -**Note**: Requires [Go 1.18+](https://golang.org/dl/) +**Note**: Requires [Go 1.19+](https://golang.org/dl/) + +## Version Matrix + +The version matrix below shows which versions of the IRISnet Modules, modules and libraries are compatible with each other. + +### Module Dependencies + +Module Dependencies are the modules that a application may depend on and which version of the Cosmos SDK they are compatible with. + +| modules | versions | Cosmos SDK | +| --------------------------------- | -------- | ---------- | +| mods.irisnet.org/api | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/simapp | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/coinswap | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/htlc | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/mt | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/nft | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/oracle | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/random | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/record | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/service | main | v0.47.x | +| | v1.0.0 | v0.47.x | +| mods.irisnet.org/modules/token | main | v0.47.x | +| | v1.0.0 | v0.47.x | diff --git a/e2e/coinswap/query.go b/e2e/coinswap/query.go index 848bbb29..6adeca8f 100644 --- a/e2e/coinswap/query.go +++ b/e2e/coinswap/query.go @@ -23,19 +23,10 @@ type QueryTestSuite struct { // SetupSuite creates a new network for integration tests func (s *QueryTestSuite) SetupSuite() { - depInjectOptions := simapp.DepinjectOptions{ - Config: e2e.AppConfig, - Providers: []interface{}{ - e2e.ProvideEVMKeeper(), - e2e.ProvideICS20Keeper(), - }, - } - sdk.SetCoinDenomRegex(func() string { + sdk.SetCoinDenomRegex(func() string { return `[a-zA-Z][a-zA-Z0-9/\-]{2,127}` }) - - s.T().Log("setting up integration test suite") - s.Network = simapp.SetupNetwork(s.T(),depInjectOptions) + s.TestSuite.SetupSuite() } // TestCoinswap tests all query command in the nft module diff --git a/e2e/farm/query.go b/e2e/farm/query.go index 1e3f9538..58578aba 100644 --- a/e2e/farm/query.go +++ b/e2e/farm/query.go @@ -16,7 +16,6 @@ import ( farmcli "mods.irisnet.org/modules/farm/client/cli" farmtypes "mods.irisnet.org/modules/farm/types" tokentypes "mods.irisnet.org/modules/token/types/v1" - "mods.irisnet.org/simapp" ) // QueryTestSuite is a suite of end-to-end tests for the farm module @@ -26,21 +25,10 @@ type QueryTestSuite struct { // SetupSuite creates a new network for integration tests func (s *QueryTestSuite) SetupSuite() { - s.T().Log("setting up integration test suite") - - depInjectOptions := simapp.DepinjectOptions{ - Config: e2e.AppConfig, - Providers: []interface{}{ - e2e.ProvideEVMKeeper(), - e2e.ProvideICS20Keeper(), - }, - } - - s.T().Log("setting up integration test suite") - s.Network = simapp.SetupNetwork(s.T(),depInjectOptions) sdk.SetCoinDenomRegex(func() string { return `[a-zA-Z][a-zA-Z0-9/\-]{2,127}` }) + s.TestSuite.SetupSuite() } // TestQueryCmd tests all query command in the farm module @@ -55,7 +43,7 @@ func (s *QueryTestSuite) TestQueryCmd() { creator := val.Address description := "iris-atom farm pool" - startHeight := s.latestHeight() + 1 + startHeight := s.latestHeight() + 2 rewardPerBlock := sdk.NewCoins(sdk.NewCoin(s.BondDenom, sdk.NewInt(10))) lpTokenDenom := "lpt-1" totalReward := sdk.NewCoins(sdk.NewCoin(s.BondDenom, sdk.NewInt(1000))) diff --git a/e2e/go.mod b/e2e/go.mod index b9d3fd74..ceef05d9 100644 --- a/e2e/go.mod +++ b/e2e/go.mod @@ -8,7 +8,6 @@ require ( github.com/cometbft/cometbft v0.37.4 github.com/cosmos/cosmos-sdk v0.47.9 github.com/cosmos/gogoproto v1.4.10 - github.com/ethereum/go-ethereum v1.10.26 github.com/stretchr/testify v1.8.4 google.golang.org/protobuf v1.32.0 mods.irisnet.org/api v1.0.0-beta @@ -77,6 +76,7 @@ require ( github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/dvsekhvalnov/jose2go v1.6.0 // indirect + github.com/ethereum/go-ethereum v1.10.26 // indirect github.com/felixge/httpsnoop v1.0.2 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/getsentry/sentry-go v0.23.0 // indirect diff --git a/e2e/htlc/tx.go b/e2e/htlc/tx.go index faa2df99..e4f32501 100644 --- a/e2e/htlc/tx.go +++ b/e2e/htlc/tx.go @@ -14,6 +14,7 @@ import ( "mods.irisnet.org/e2e" htlccli "mods.irisnet.org/modules/htlc/client/cli" htlctypes "mods.irisnet.org/modules/htlc/types" + "mods.irisnet.org/modules/token/keeper" "mods.irisnet.org/simapp" ) @@ -63,8 +64,8 @@ func (s *TxTestSuite) SetupSuite() { depInjectOptions := simapp.DepinjectOptions{ Config: e2e.AppConfig, Providers: []interface{}{ - e2e.ProvideEVMKeeper(), - e2e.ProvideICS20Keeper(), + keeper.ProvideMockEVM(), + keeper.ProvideMockICS20(), }, } diff --git a/e2e/mock.go b/e2e/mock.go deleted file mode 100644 index 02d9b5ba..00000000 --- a/e2e/mock.go +++ /dev/null @@ -1,170 +0,0 @@ -package e2e - -import ( - "context" - "fmt" - "math/big" - - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core" - "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/crypto" - - _ "mods.irisnet.org/modules/token" - "mods.irisnet.org/modules/token/contracts" - tokentypes "mods.irisnet.org/modules/token/types" -) - -var ( - _ tokentypes.EVMKeeper = (*evm)(nil) - _ tokentypes.ICS20Keeper = (*transferKeeper)(nil) -) - - -// ProvideEVMKeeper returns an instance of tokentypes.EVMKeeper. -// -// No parameters. -// Returns a tokentypes.EVMKeeper. -func ProvideEVMKeeper() tokentypes.EVMKeeper { - return &evm{ - erc20s: make(map[common.Address]*erc20), - } -} - -// ProvideICS20Keeper returns an instance of tokentypes.ICS20Keeper. -// -// No parameters. -// Returns a tokentypes.ICS20Keeper. -func ProvideICS20Keeper() tokentypes.ICS20Keeper { - return &transferKeeper{} -} - -type evm struct { - erc20s map[common.Address]*erc20 -} - -// ApplyMessage implements types.EVMKeeper. -func (e *evm) ApplyMessage(ctx sdk.Context, msg core.Message, tracer vm.EVMLogger, commit bool) (*tokentypes.Result, error) { - isCreate := msg.To() == nil - if isCreate { - contractAddr := crypto.CreateAddress(msg.From(), msg.Nonce()) - - data := msg.Data()[len(contracts.TokenProxyContract.Bin):] - args, err := contracts.TokenProxyContract.ABI.Constructor.Inputs.Unpack(data) - if err != nil { - return nil, err - } - - data = args[1].([]byte) - data = data[4:] - args, err = contracts.ERC20TokenContract.ABI.Methods[contracts.MethodInitialize].Inputs.Unpack(data) - if err != nil { - return nil, err - } - - name, _ := args[0].(string) - symbol, _ := args[1].(string) - scale, _ := args[2].(uint8) - e.erc20s[contractAddr] = &erc20{ - address: contractAddr, - scale: scale, - name: name, - symbol: symbol, - balance: make(map[common.Address]*big.Int), - } - return &tokentypes.Result{ - Hash: contractAddr.Hex(), - }, nil - } - - erc20Contract, ok := e.erc20s[*msg.To()] - if !ok { - return nil, fmt.Errorf("erc20 contract not found") - } - return e.dispatch(erc20Contract, msg.Data()) -} - -// ChainID implements types.EVMKeeper. -func (e *evm) ChainID() *big.Int { - return big.NewInt(16688) -} - -// EstimateGas implements types.EVMKeeper. -func (e *evm) EstimateGas(ctx context.Context, req *tokentypes.EthCallRequest) (uint64, error) { - return 3000000, nil -} - -// SupportedKey implements types.EVMKeeper. -func (e *evm) SupportedKey(pubKey cryptotypes.PubKey) bool { - return true -} - -func (e *evm) dispatch(contract *erc20, data []byte) (*tokentypes.Result, error) { - method, err := contracts.ERC20TokenContract.ABI.MethodById(data[0:4]) - if err != nil { - return nil, err - } - - ret, err := contract.call(method, data[4:]) - if err != nil { - return nil, err - } - return &tokentypes.Result{ - Hash: contract.address.Hex(), - Ret: ret, - }, nil -} - -type erc20 struct { - address common.Address - scale uint8 - name, symbol string - - balance map[common.Address]*big.Int -} - -func (erc20 erc20) call(method *abi.Method, data []byte) ([]byte, error) { - args, err := method.Inputs.Unpack(data) - if err != nil { - return nil, err - } - - switch method.Name { - case "name": - return method.Outputs.Pack(erc20.name) - case "symbol": - return method.Outputs.Pack(erc20.symbol) - case "decimals": - return method.Outputs.Pack(erc20.scale) - case "balanceOf": - balance, ok := erc20.balance[args[0].(common.Address)] - if !ok { - return method.Outputs.Pack(big.NewInt(0)) - } - return method.Outputs.Pack(balance) - case "mint": - to := args[0].(common.Address) - balance, ok := erc20.balance[args[0].(common.Address)] - if !ok { - balance = big.NewInt(0) - } - erc20.balance[to] = new(big.Int).Add(balance, args[1].(*big.Int)) - return nil, nil - case "burn": - from := args[0].(common.Address) - erc20.balance[from] = new(big.Int).Sub(erc20.balance[from], args[1].(*big.Int)) - return nil, nil - default: - return nil, fmt.Errorf("unknown method %s", method.Name) - } -} - -type transferKeeper struct{} - -// HasTrace implements types.ICS20Keeper. -func (t *transferKeeper) HasTrace(ctx sdk.Context, denom string) bool { - return true -} diff --git a/e2e/suite.go b/e2e/suite.go index 055f2554..cf41b6fc 100644 --- a/e2e/suite.go +++ b/e2e/suite.go @@ -4,6 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/testutil/network" "github.com/stretchr/testify/suite" + "mods.irisnet.org/modules/token/keeper" "mods.irisnet.org/simapp" ) @@ -24,8 +25,8 @@ func (s *TestSuite) SetupSuite() { depInjectOptions := simapp.DepinjectOptions{ Config: AppConfig, Providers: []interface{}{ - ProvideEVMKeeper(), - ProvideICS20Keeper(), + keeper.ProvideMockEVM(), + keeper.ProvideMockICS20(), }, } if s.modifyConfigFn == nil {