Skip to content

Commit

Permalink
Merge pull request #103 from b-harvest/onboarding-middleware
Browse files Browse the repository at this point in the history
feat: Onboarding IBC middleware
  • Loading branch information
tkkwon1998 authored Aug 6, 2023
2 parents 3ea39d9 + c497110 commit 7ebb314
Show file tree
Hide file tree
Showing 106 changed files with 17,756 additions and 118 deletions.
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"
)

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
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:
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

const (
//UpgradeName is the name of the upgrade to be associated with the chain upgrade
UpgradeName = "v7.0.0"
)
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

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

0 comments on commit 7ebb314

Please sign in to comment.