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: Onboarding IBC middleware #103

Merged
merged 38 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
37612b4
feat: onboarding IBC middleware
poorphd Apr 26, 2023
a44264e
fix: recovery test to use ibc/testing
poorphd Apr 26, 2023
12a1caf
refactor: package name change v6 -> v7
poorphd Apr 26, 2023
ba99a5b
docs: module specs
poorphd Apr 27, 2023
00b81a6
feat: proto for coinswap module
poorphd Apr 26, 2023
c61b8b0
feat: swap event emit in ibc callback
poorphd Apr 26, 2023
8ddce2a
test: convert fail test case added
poorphd Apr 21, 2023
305aee3
feat: WIP coinswap module
poorphd Apr 25, 2023
81e8fd6
refactor: delete migration code
poorphd Apr 21, 2023
31f6c9d
refactor: coinswap module for Canto
poorphd Apr 21, 2023
4f07331
fix: allowing vesting account
poorphd Apr 24, 2023
06746d5
fix: use ethsecp256k address for Canto address
poorphd Apr 24, 2023
73ff366
fix: emit swap event fix
poorphd Apr 25, 2023
1a9e72b
test: IBC transfer to a blocked addr
poorphd Apr 25, 2023
ef781ae
refactor: remove unnecessary test case
poorphd Apr 26, 2023
a137619
fix: default params for coinswap and upgrade handler to use the defau…
poorphd Apr 27, 2023
8f7d8be
refactor: package name v7 -> v6
poorphd Apr 26, 2023
9953db6
refactor: remove ibc/testing/simapp
poorphd Apr 26, 2023
ba3e0ea
fix: go mod
poorphd Apr 26, 2023
da6194f
fix: proto for coinswap
poorphd Apr 26, 2023
cadf019
refactor: sort imports and revert unnecessary changes
poorphd Apr 27, 2023
4bb9024
fix: package name change v7 -> v6
poorphd May 3, 2023
266930b
fix: bug in AmountOf
poorphd May 5, 2023
2b1b03e
fix: validate params
poorphd May 9, 2023
653d35b
fix: default max sawp amount
poorphd Jul 13, 2023
2aafb8e
fix: swappedAmount in error case
poorphd Jul 13, 2023
b186460
Merge remote-tracking branch 'upstream/main' into onboarding-middlewa…
poorphd Jul 24, 2023
b79a715
fix: resolve conflicts
poorphd Jul 24, 2023
4caeaf2
Merge pull request #22 from b-harvest/onboarding-middleware-conflict-…
poorphd Jul 25, 2023
9e54c11
Merge remote-tracking branch 'origin/canto-main' into onboarding-midd…
poorphd Jul 31, 2023
9bcebc8
Merge remote-tracking branch 'upstream/main' into onboarding-middlewa…
poorphd Aug 2, 2023
13f26e8
fix: SetupTestingCantoApp
poorphd Aug 2, 2023
b9e47d5
fix: coinswap module is added to simulation manager
poorphd Aug 2, 2023
6626290
fix: bug in coinswap module's param change simulation
poorphd Aug 3, 2023
6edd5d4
Merge commit 'd780d52648475e8887cacce73acec93ae59fb34b' into onboardi…
dongsam Aug 3, 2023
57b9208
Merge pull request #25 from b-harvest/onboarding-middleware-conflict-…
dongsam Aug 4, 2023
4240965
Merge commit main into onboarding-middleware
dongsam Aug 4, 2023
c497110
fix: update swagger for onboarding, coinswap module
dongsam Aug 4, 2023
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
100 changes: 86 additions & 14 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ import (
"github.com/Canto-Network/Canto/v6/x/inflation"
inflationkeeper "github.com/Canto-Network/Canto/v6/x/inflation/keeper"
inflationtypes "github.com/Canto-Network/Canto/v6/x/inflation/types"
"github.com/Canto-Network/Canto/v6/x/onboarding"
onboardingkeeper "github.com/Canto-Network/Canto/v6/x/onboarding/keeper"
onboardingtypes "github.com/Canto-Network/Canto/v6/x/onboarding/types"

//govshuttle imports
"github.com/Canto-Network/Canto/v6/x/govshuttle"
Expand All @@ -131,11 +134,16 @@ import (
csrkeeper "github.com/Canto-Network/Canto/v6/x/csr/keeper"
csrtypes "github.com/Canto-Network/Canto/v6/x/csr/types"

"github.com/Canto-Network/Canto/v6/x/coinswap"
coinswapkeeper "github.com/Canto-Network/Canto/v6/x/coinswap/keeper"
coinswaptypes "github.com/Canto-Network/Canto/v6/x/coinswap/types"

v2 "github.com/Canto-Network/Canto/v6/app/upgrades/v2"
v3 "github.com/Canto-Network/Canto/v6/app/upgrades/v3"
v4 "github.com/Canto-Network/Canto/v6/app/upgrades/v4"
v5 "github.com/Canto-Network/Canto/v6/app/upgrades/v5"
v6 "github.com/Canto-Network/Canto/v6/app/upgrades/v6"
v7 "github.com/Canto-Network/Canto/v6/app/upgrades/v7"
poorphd marked this conversation as resolved.
Show resolved Hide resolved
)

func init() {
Expand Down Expand Up @@ -194,6 +202,8 @@ var (
govshuttle.AppModuleBasic{},
csr.AppModuleBasic{},
epochs.AppModuleBasic{},
onboarding.AppModuleBasic{},
coinswap.AppModuleBasic{},
)

// module account permissions
Expand All @@ -209,6 +219,8 @@ var (
erc20types.ModuleName: {authtypes.Minter, authtypes.Burner},
csrtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
govshuttletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
onboardingtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
coinswaptypes.ModuleName: {authtypes.Minter, authtypes.Burner},
}

// module accounts that are allowed to receive tokens
Expand Down Expand Up @@ -270,9 +282,13 @@ type Canto struct {
InflationKeeper inflationkeeper.Keeper
Erc20Keeper erc20keeper.Keeper
EpochsKeeper epochskeeper.Keeper
OnboardingKeeper *onboardingkeeper.Keeper
GovshuttleKeeper govshuttlekeeper.Keeper
CSRKeeper csrkeeper.Keeper

// Coinswap keeper
CoinswapKeeper coinswapkeeper.Keeper

// the module manager
mm *module.Manager

Expand Down Expand Up @@ -329,8 +345,11 @@ func NewCanto(
// Canto keys
inflationtypes.StoreKey, erc20types.StoreKey,
epochstypes.StoreKey,
onboardingtypes.StoreKey,
csrtypes.StoreKey,
govshuttletypes.StoreKey,
// Coinswap keys
coinswaptypes.StoreKey,
)

// Add the EVM transient store key
Expand Down Expand Up @@ -406,6 +425,16 @@ func NewCanto(
appCodec, keys[ibchost.StoreKey], app.GetSubspace(ibchost.ModuleName), &stakingKeeper, app.UpgradeKeeper, scopedIBCKeeper,
)

app.CoinswapKeeper = coinswapkeeper.NewKeeper(
appCodec,
keys[coinswaptypes.ModuleName],
app.GetSubspace(coinswaptypes.ModuleName),
app.BankKeeper,
app.AccountKeeper,
app.ModuleAccountAddrs(),
authtypes.FeeCollectorName,
)

// register the proposal types
govRouter := govtypes.NewRouter()
govRouter.AddRoute(govtypes.RouterKey, govtypes.ProposalHandler).
Expand Down Expand Up @@ -479,33 +508,45 @@ func NewCanto(
// Create Transfer Stack

// SendPacket, since it is originating from the application to core IBC:
// transferKeeper.SendPacket -> claim.SendPacket -> recovery.SendPacket -> channel.SendPacket
// transferKeeper.SendPacket -> onboarding.SendPacket -> channel.SendPacket

// RecvPacket, message that originates from core IBC and goes down to app, the flow is the otherway
// channel.RecvPacket -> recovery.OnRecvPacket -> claim.OnRecvPacket -> transfer.OnRecvPacket
// channel.RecvPacket -> onboarding.OnRecvPacket -> transfer.OnRecvPacket

app.OnboardingKeeper = onboardingkeeper.NewKeeper(
app.GetSubspace(onboardingtypes.ModuleName),
app.AccountKeeper,
app.BankKeeper,
app.IBCKeeper.ChannelKeeper,
app.TransferKeeper,
app.CoinswapKeeper,
app.Erc20Keeper,
)

app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName), app.IBCKeeper.ChannelKeeper,
//nil, // ICS4 Wrapper: claims IBC middleware
appCodec, keys[ibctransfertypes.StoreKey], app.GetSubspace(ibctransfertypes.ModuleName), app.OnboardingKeeper,
app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, app.BankKeeper, scopedTransferKeeper,
)

// Set the ICS4 wrappers for onboarding middleware
// SET ONBOARDING MIDDLEWARE ICS4 WRAPPER
app.OnboardingKeeper.SetTransferKeeper(app.TransferKeeper)

// Set the ICS4 wrappers for onboarding middlewares
app.OnboardingKeeper.SetICS4Wrapper(app.IBCKeeper.ChannelKeeper)
// NOTE: ICS4 wrapper for Transfer Keeper already set

// Override the ICS20 app module
transferModule := transfer.NewAppModule(app.TransferKeeper)

// transfer stack contains (from top to bottom):
// - Recovery Middleware
// - Onboarding Middleware
// - Transfer

// create IBC module from bottom to top of stack
var transferStack porttypes.IBCModule

transferStack = transfer.NewIBCModule(app.TransferKeeper)
// ADD ONBOARDING MIDDLEWARE TO TRANSFER STACK
transferStack = onboarding.NewIBCMiddleware(*app.OnboardingKeeper, transferStack)

// Create static IBC router, add transfer route, then set and seal it
ibcRouter := porttypes.NewRouter()
Expand Down Expand Up @@ -557,8 +598,10 @@ func NewCanto(
inflation.NewAppModule(app.InflationKeeper, app.AccountKeeper, app.StakingKeeper),
erc20.NewAppModule(app.Erc20Keeper, app.AccountKeeper),
epochs.NewAppModule(appCodec, app.EpochsKeeper),
onboarding.NewAppModule(*app.OnboardingKeeper),
govshuttle.NewAppModule(app.GovshuttleKeeper, app.AccountKeeper),
csr.NewAppModule(app.CSRKeeper, app.AccountKeeper),
coinswap.NewAppModule(appCodec, app.CoinswapKeeper, app.AccountKeeper, app.BankKeeper),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -591,8 +634,10 @@ func NewCanto(
paramstypes.ModuleName,
inflationtypes.ModuleName,
erc20types.ModuleName,
onboardingtypes.ModuleName,
govshuttletypes.ModuleName,
csrtypes.ModuleName,
coinswaptypes.ModuleName,
)

// NOTE: fee market module must go last in order to retrieve the block gas used.
Expand All @@ -604,6 +649,7 @@ func NewCanto(
feemarkettypes.ModuleName,
// Note: epochs' endblock should be "real" end of epochs, we keep epochs endblock at the end
epochstypes.ModuleName,
onboardingtypes.ModuleName,
// no-op modules
ibchost.ModuleName,
ibctransfertypes.ModuleName,
Expand All @@ -623,7 +669,7 @@ func NewCanto(
erc20types.ModuleName,
govshuttletypes.ModuleName,
csrtypes.ModuleName,
// recoverytypes.ModuleName,
coinswaptypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -659,8 +705,10 @@ func NewCanto(
inflationtypes.ModuleName,
erc20types.ModuleName,
epochstypes.ModuleName,
onboardingtypes.ModuleName,
govshuttletypes.ModuleName,
csrtypes.ModuleName,
coinswaptypes.ModuleName,
// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,
)
Expand Down Expand Up @@ -699,9 +747,10 @@ func NewCanto(
epochs.NewAppModule(appCodec, app.EpochsKeeper),
inflation.NewAppModule(app.InflationKeeper, app.AccountKeeper, app.StakingKeeper),
feemarket.NewAppModule(app.FeeMarketKeeper),
coinswap.NewAppModule(appCodec, app.CoinswapKeeper, app.AccountKeeper, app.BankKeeper),

// TODO: Modules that have not yet been implemented for simulation
// govshuttle, csr, recovery, erc20
// govshuttle, csr, inflation, erc20
)

app.sm.RegisterStoreDecoders()
Expand All @@ -717,10 +766,9 @@ func NewCanto(

maxGasWanted := cast.ToUint64(appOpts.Get(srvflags.EVMMaxTxGasWanted))
options := ante.HandlerOptions{
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
EvmKeeper: app.EvmKeeper,
//StakingKeeper: app.StakingKeeper,
AccountKeeper: app.AccountKeeper,
BankKeeper: app.BankKeeper,
EvmKeeper: app.EvmKeeper,
FeegrantKeeper: app.FeeGrantKeeper,
IBCKeeper: app.IBCKeeper,
FeeMarketKeeper: app.FeeMarketKeeper,
Expand Down Expand Up @@ -929,6 +977,18 @@ func (app *Canto) GetIBCKeeper() *ibckeeper.Keeper {
return app.IBCKeeper
}

func (app *Canto) GetErc20Keeper() erc20keeper.Keeper {
return app.Erc20Keeper
}

func (app *Canto) GetCoinswapKeeper() coinswapkeeper.Keeper {
return app.CoinswapKeeper
}

func (app *Canto) GetOnboardingKeeper() *onboardingkeeper.Keeper {
return app.OnboardingKeeper
}

// GetScopedIBCKeeper implements the TestingApp interface.
func (app *Canto) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
return app.ScopedIBCKeeper
Expand Down Expand Up @@ -983,8 +1043,10 @@ func initParamsKeeper(
// Canto subspaces
paramsKeeper.Subspace(inflationtypes.ModuleName)
paramsKeeper.Subspace(erc20types.ModuleName)
paramsKeeper.Subspace(onboardingtypes.ModuleName)
paramsKeeper.Subspace(govshuttletypes.ModuleName)
paramsKeeper.Subspace(csrtypes.ModuleName)
paramsKeeper.Subspace(coinswaptypes.ModuleName)
return paramsKeeper
}

Expand Down Expand Up @@ -1017,6 +1079,12 @@ func (app *Canto) setupUpgradeHandlers() {
v6.CreateUpgradeHandler(app.mm, app.configurator),
)

// v7 upgrade handler
poorphd marked this conversation as resolved.
Show resolved Hide resolved
app.UpgradeKeeper.SetUpgradeHandler(
v7.UpgradeName,
v7.CreateUpgradeHandler(app.mm, app.configurator, *app.OnboardingKeeper, app.CoinswapKeeper),
)

// When a planned update height is reached, the old binary will panic
// writing on disk the height and name of the update that triggered it
// This will read that value, and execute the preparations for the upgrade.
Expand Down Expand Up @@ -1047,6 +1115,10 @@ func (app *Canto) setupUpgradeHandlers() {
}
case v6.UpgradeName:
// no store upgrades in v6
case v7.UpgradeName:
poorphd marked this conversation as resolved.
Show resolved Hide resolved
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{onboardingtypes.StoreKey, coinswaptypes.StoreKey},
}
}

if storeUpgrades != nil {
Expand Down
43 changes: 33 additions & 10 deletions app/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
ibctesting "github.com/cosmos/ibc-go/v3/testing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

"github.com/cosmos/ibc-go/v3/testing/simapp"

abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand All @@ -26,9 +29,6 @@ func init() {
config.SetBip44CoinType(cfg)
}

// DefaultTestingAppInit defines the IBC application used for testing
var DefaultTestingAppInit func() (ibctesting.TestingApp, map[string]json.RawMessage) = SetupTestingApp

// DefaultConsensusParams defines the default Tendermint consensus params used in
// canto testing.
var DefaultConsensusParams = &abci.ConsensusParams{
Expand Down Expand Up @@ -93,10 +93,33 @@ func Setup(
return app
}

// SetupTestingApp initializes the IBC-go testing application
func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
db := dbm.NewMemDB()
cfg := encoding.MakeConfig(ModuleBasics)
app := NewCanto(log.NewNopLogger(), db, nil, true, map[int64]bool{}, DefaultNodeHome, 5, false, cfg, simapp.EmptyAppOptions{})
return app, NewDefaultGenesisState()
func SetupWithGenesisAccounts(genAccs []authtypes.GenesisAccount, balances ...banktypes.Balance) *Canto {
app := Setup(false, feemarkettypes.DefaultGenesisState())
genesisState := NewDefaultGenesisState()
authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs)
genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis)

totalSupply := sdk.NewCoins()
for _, b := range balances {
totalSupply = totalSupply.Add(b.Coins...)
}

bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, totalSupply, []banktypes.Metadata{})
genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis)

stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}

app.InitChain(
abci.RequestInitChain{
ChainId: types.MainnetChainID + "-1",
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)

return app
}
6 changes: 6 additions & 0 deletions app/upgrades/v7/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package v7
poorphd marked this conversation as resolved.
Show resolved Hide resolved

const (
//UpgradeName is the name of the upgrade to be associated with the chain upgrade
UpgradeName = "v7.0.0"
poorphd marked this conversation as resolved.
Show resolved Hide resolved
)
40 changes: 40 additions & 0 deletions app/upgrades/v7/upgrades.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package v7
poorphd marked this conversation as resolved.
Show resolved Hide resolved

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

coinswapkeeper "github.com/Canto-Network/Canto/v6/x/coinswap/keeper"
coinswaptypes "github.com/Canto-Network/Canto/v6/x/coinswap/types"
onboardingkeeper "github.com/Canto-Network/Canto/v6/x/onboarding/keeper"
onboardingtypes "github.com/Canto-Network/Canto/v6/x/onboarding/types"
)

// CreateUpgradeHandler creates an SDK upgrade handler for v7
func CreateUpgradeHandler(
mm *module.Manager,
configurator module.Configurator,
onboardingKeeper onboardingkeeper.Keeper,
coinswapKeeper coinswapkeeper.Keeper,
) upgradetypes.UpgradeHandler {
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
logger := ctx.Logger().With("upgrading to v7.0.0", UpgradeName)

newVM, err := mm.RunMigrations(ctx, configurator, vm)
if err != nil {
return nil, err
}

onboardingParams := onboardingtypes.DefaultParams()
onboardingKeeper.SetParams(ctx, onboardingParams)

coinswapParams := coinswaptypes.DefaultParams()
coinswapKeeper.SetParams(ctx, coinswapParams)
coinswapKeeper.SetStandardDenom(ctx, "acanto")

// Leave modules are as-is to avoid running InitGenesis.
logger.Debug("running module migrations ...")
return newVM, nil
}
}
Loading
Loading