Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ibc testing framework #73

Merged
merged 7 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ import (

ics20wrapper "github.com/Lorenzo-Protocol/lorenzo/x/ibctransfer"
ics20wrapperkeeper "github.com/Lorenzo-Protocol/lorenzo/x/ibctransfer/keeper"
"github.com/Lorenzo-Protocol/lorenzo/x/token"
tokenkeeper "github.com/Lorenzo-Protocol/lorenzo/x/token/keeper"
tokentypes "github.com/Lorenzo-Protocol/lorenzo/x/token/types"
)
Expand Down Expand Up @@ -314,7 +315,6 @@ func NewLorenzoApp(
// grant capabilities for the ibc and ibc-transfer modules
scopedIBCKeeper := app.CapabilityKeeper.ScopeToModule(ibcexported.ModuleName)
scopedTransferKeeper := app.CapabilityKeeper.ScopeToModule(ibctransfertypes.ModuleName)
// this line is used by starport scaffolding # stargate/app/scopedKeeper

app.CapabilityKeeper.Seal()

Expand Down Expand Up @@ -516,7 +516,8 @@ func NewLorenzoApp(
app.TokenKeeper,
)

transferStack := ics20wrapper.NewIBCModule(app.ICS20WrapperKeeper)
ics20wrapperModule := ics20wrapper.NewIBCModule(app.ICS20WrapperKeeper)
transferStack := token.NewIBCMiddleware(ics20wrapperModule, app.TokenKeeper)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := ibcporttypes.NewRouter()
Expand Down
64 changes: 49 additions & 15 deletions app/helpers/test_helper.go → app/test_helper.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package helpers
package app

import (
"bytes"
"encoding/json"
"math/rand"
"strconv"
"testing"
"time"

appparams "github.com/Lorenzo-Protocol/lorenzo/app/params"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/spf13/cast"
"github.com/stretchr/testify/require"

Expand All @@ -19,6 +17,8 @@ import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
Expand All @@ -31,14 +31,16 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/Lorenzo-Protocol/lorenzo/app"
appparams "github.com/Lorenzo-Protocol/lorenzo/app/params"
)

// SimAppChainID hardcoded chainID for simulation
const (
SimAppChainID = "lorenzo_8329-1"
)

var DefaultAppVersion uint64 = 1

// DefaultConsensusParams defines the default Tendermint consensus params used
// in LorenzoApp testing.
var DefaultConsensusParams = &tmproto.ConsensusParams{
Expand All @@ -56,6 +58,7 @@ var DefaultConsensusParams = &tmproto.ConsensusParams{
tmtypes.ABCIPubKeyTypeEd25519,
},
},
Version: &tmproto.VersionParams{App: DefaultAppVersion},
}

// PV implements PrivValidator interface
Expand All @@ -75,7 +78,7 @@ func (EmptyAppOptions) Get(_ string) interface{} { return nil }
// - t: A testing.T instance for testing purposes.
// Returns:
// - *app.LorenzoApp: The initialized instance of app.LorenzoApp.
func Setup(t *testing.T) *app.LorenzoApp {
func Setup(t *testing.T) *LorenzoApp {
t.Helper()

privVal := mock.NewPV()
Expand Down Expand Up @@ -105,7 +108,7 @@ func Setup(t *testing.T) *app.LorenzoApp {
// - merge: A function to merge override module genesis data with the default genesis data.
// Returns:
// - *app.LorenzoApp: The initialized instance of app.LorenzoApp.
func SetupWithGenesisMergeFn(t *testing.T, merge func(cdc codec.Codec, state map[string]json.RawMessage)) *app.LorenzoApp {
func SetupWithGenesisMergeFn(t *testing.T, merge func(cdc codec.Codec, state map[string]json.RawMessage)) *LorenzoApp {
t.Helper()

privVal := mock.NewPV()
Expand Down Expand Up @@ -162,7 +165,7 @@ func SetupWithGenesisMergeFn(t *testing.T, merge func(cdc codec.Codec, state map
// that also act as delegators. For simplicity, each validator is bonded with a delegation
// of one consensus engine unit in the default token of the LorenzoApp from first genesis
// account. A Nop logger is set in LorenzoApp.
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *app.LorenzoApp {
func SetupWithGenesisValSet(t *testing.T, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *LorenzoApp {
t.Helper()

lorenzoApp, genesisState := setup()
Expand Down Expand Up @@ -213,35 +216,35 @@ func CreateTestAddrs(numAddrs int) []sdk.AccAddress {
return addresses
}

func setup() (*app.LorenzoApp, app.GenesisState) {
func setup() (*LorenzoApp, GenesisState) {
db := dbm.NewMemDB()
appOptions := make(simtestutil.AppOptionsMap)
appOptions[server.FlagInvCheckPeriod] = 5
appOptions[server.FlagMinGasPrices] = "10alrz"
appOptions["btc-config.network"] = "simnet"

invCheckPeriod := cast.ToUint(appOptions.Get(server.FlagInvCheckPeriod))
encConfig := app.MakeEncodingConfig()
LorenzoApp := app.NewLorenzoApp(
encConfig := MakeEncodingConfig()
LorenzoApp := NewLorenzoApp(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
app.DefaultNodeHome,
DefaultNodeHome,
invCheckPeriod,
encConfig,
appOptions,
baseapp.SetChainID(SimAppChainID),
)
return LorenzoApp, app.NewDefaultGenesisState(encConfig.Codec)
return LorenzoApp, NewDefaultGenesisState(encConfig.Codec)
}

func genesisStateWithValSet(t *testing.T,
app *app.LorenzoApp, genesisState app.GenesisState,
app *LorenzoApp, genesisState GenesisState,
valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount,
balances ...banktypes.Balance,
) app.GenesisState {
) GenesisState {
t.Helper()
// set genesis accounts
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
Expand Down Expand Up @@ -321,3 +324,34 @@ func testAddr(addr string, bech string) sdk.AccAddress {

return res
}

func SignAndDeliver(
t *testing.T, txCfg client.TxConfig, app *baseapp.BaseApp, header tmproto.Header, msgs []sdk.Msg,
chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey,
) (sdk.GasInfo, *sdk.Result, error) {
tx, err := simtestutil.GenSignedMockTx(
rand.New(rand.NewSource(time.Now().UnixNano())), //nolint: gosec
txCfg,
msgs,
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)},
simtestutil.DefaultGenTxGas,
chainID,
accNums,
accSeqs,
priv...,
)
require.NoError(t, err)

// Simulate a sending a transaction
gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx)

if expPass {
require.NoError(t, err)
require.NotNil(t, res)
} else {
require.Error(t, err)
require.Nil(t, res)
}

return gInfo, res, err
}
2 changes: 1 addition & 1 deletion proto/lorenzo/token/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ message GenesisState {

// Params defines the token module parameters.
message Params {
bool enable_convert = 1;
bool enable_conversion = 1;
bool enable_evm_hook = 2 [(gogoproto.customname) = "EnableEVMHook"];
}
20 changes: 10 additions & 10 deletions proto/lorenzo/token/v1/token.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import "gogoproto/gogo.proto";

option go_package = "github.com/Lorenzo-Protocol/lorenzo/x/token/types";

// Ownership defines the ownership of an erc20 contract, if ownership belongs to:
// - token module: the token origin is sdk coin.
// - external account: the token origin is erc20 contract.
enum Ownership {
// Source defines the source type of token asset, if source is:
// - module: token origin is sdk module;
// - contract: token origin is erc20 contract;
enum Source {
option (gogoproto.goproto_enum_prefix) = false;
// undefined owner
// undefined source
OWNER_UNDEFINED = 0;
// contract owned by convert module
// token source is module
OWNER_MODULE = 1;
// contract owned by external account
OWNER_EXTERNAL = 2;
// token source is erc20 contract
OWNER_CONTRACT = 2;
}

// TokenPair defines a pairing of a cosmos coin and an erc20 token
Expand All @@ -28,6 +28,6 @@ message TokenPair {
string denom = 2;
// allows for token conversion
bool enabled = 3;
// ownership of the contract
Ownership ownership = 4;
// source of token asset
Source source = 4;
}
1 change: 0 additions & 1 deletion simulation/simapp.go

This file was deleted.

23 changes: 23 additions & 0 deletions testutil/ibc/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
MIT License

Copyright (c) 2022 COSMOS

Copyright (c) 2024 Lorenzo Protocol

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions testutil/ibc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# IBC Testing

This package contains the necessary code to test the IBC modules. It is a forked version of IBC Testing, customized for Lorenzo.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reduce line length.

The lines exceed the recommended length of 120 characters. Consider breaking them into multiple lines.

- This package contains the necessary code to test the IBC modules. It is a forked version of IBC Testing, customized for Lorenzo.
+ This package contains the necessary code to test the IBC modules.
+ It is a forked version of IBC Testing, customized for Lorenzo.

- The customization aims to maximize compatibility with the original source code while ensuring it is suitably adapted for Lorenzo’s specific requirements.
+ The customization aims to maximize compatibility with the original source code
+ while ensuring it is suitably adapted for Lorenzo’s specific requirements.

- The maintainer must keep it updated with the currently used version of the IBC code repository to ensure ongoing compatibility and functionality.
+ The maintainer must keep it updated with the currently used version of the IBC code repository
+ to ensure ongoing compatibility and functionality.

Also applies to: 5-5, 7-7

Tools
Markdownlint

3-3: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


3-3: Expected: 120; Actual: 129
Line length

(MD013, line-length)


Remove trailing spaces.

There are trailing spaces at the end of this line.

- This package contains the necessary code to test the IBC modules. It is a forked version of IBC Testing, customized for Lorenzo. 
+ This package contains the necessary code to test the IBC modules. It is a forked version of IBC Testing, customized for Lorenzo.
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
This package contains the necessary code to test the IBC modules. It is a forked version of IBC Testing, customized for Lorenzo.
This package contains the necessary code to test the IBC modules. It is a forked version of IBC Testing, customized for Lorenzo.
Tools
Markdownlint

3-3: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


3-3: Expected: 120; Actual: 129
Line length

(MD013, line-length)


The customization aims to maximize compatibility with the original source code while ensuring it is suitably adapted for Lorenzo’s specific requirements.

The maintainer must keep it updated with the currently used version of the IBC code repository to ensure ongoing compatibility and functionality.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a newline at the end of the file.

Files should end with a single newline character.

- to ensure ongoing compatibility and functionality.
+ to ensure ongoing compatibility and functionality.
+
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
The maintainer must keep it updated with the currently used version of the IBC code repository to ensure ongoing compatibility and functionality.
The maintainer must keep it updated with the currently used version of the IBC code repository to ensure ongoing compatibility and functionality.
Tools
Markdownlint

7-7: Expected: 120; Actual: 145
Line length

(MD013, line-length)


7-7: null
Files should end with a single newline character

(MD047, single-trailing-newline)

Loading
Loading