Skip to content

Commit

Permalink
Merge pull request #110 from notional-labs/transfer_middleware
Browse files Browse the repository at this point in the history
Transfer middleware
  • Loading branch information
vuong177 committed May 9, 2023
2 parents 76b63fb + 0ecc866 commit ee7c28e
Show file tree
Hide file tree
Showing 88 changed files with 12,689 additions and 34 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/interchaintest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Interchain Tests

on:
pull_request:
push:
branches:
- main

jobs:
test-start-cosmos-chain:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: checkout code
uses: actions/checkout@v3

- run: make ictest-start-cosmos

test-start-polkadot-chain:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: checkout code
uses: actions/checkout@v3

- run: make ictest-start-polkadot

test-ibc-transfer:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: checkout code
uses: actions/checkout@v3

- run: make ictest-ibc

test-push-wasm:
runs-on: ubuntu-latest
steps:
- name: Set up Go 1.19
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: checkout code
uses: actions/checkout@v3

- run: make ictest-push-wasm
56 changes: 54 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SDK_PACK := $(shell go list -m github.com/cosmos/cosmos-sdk | sed 's/ /\@/g')
DOCKER := $(shell which docker)
DOCKER_BUF := $(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace bufbuild/buf:1.0.0-rc8
BUILDDIR ?= $(CURDIR)/build
HTTPS_GIT := https://github.com/notional-labs/composable-testnet.git
HTTPS_GIT := https://github.com/notional-labs/banksy/.git

export GO111MODULE = on

Expand Down Expand Up @@ -92,4 +92,56 @@ build:
go build $(BUILD_FLAGS) -o bin/banksyd ./cmd/banksyd

docker-build-debug:
@DOCKER_BUILDKIT=1 docker build -t composable-testnet:debug -f Dockerfile .
@DOCKER_BUILDKIT=1 docker build -t composable-testnet:debug -f Dockerfile .

###############################################################################
### Proto ###
###############################################################################

protoVer=v0.8
protoImageName=ghcr.io/notional-labs/fa-proto-gen:$(protoVer)
containerProtoGen=fa-proto-gen-$(protoVer)
containerProtoFmt=fa-proto-fmt-$(protoVer)

proto-all: proto-format proto-gen

proto-gen:
@echo "Generating Protobuf files"
@if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerProtoGen}$$"; then docker start -a $(containerProtoGen); else docker run --name $(containerProtoGen) -v $(CURDIR):/workspace --workdir /workspace $(protoImageName) \
sh ./scripts/protocgen.sh; fi

proto-format:
@echo "Formatting Protobuf files"
@if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerProtoFmt}$$"; then docker start -a $(containerProtoFmt); else docker run --name $(containerProtoFmt) -v $(CURDIR):/workspace --workdir /workspace tendermintdev/docker-build-proto \
find ./ -not -path "./third_party/*" -name "*.proto" -exec clang-format -i {} \; ; fi

proto-lint:
@$(DOCKER_BUF) lint --error-format=json

proto-check-breaking:
@$(DOCKER_BUF) breaking --against $(HTTPS_GIT)#branch=main

.PHONY: proto-all proto-gen proto-format proto-lint proto-check-breaking
### Interchain test ###
###############################################################################

# Executes start chain tests via interchaintest
ictest-start-cosmos:
cd tests/interchaintest && go test -race -v -run TestStartBanksy .

# Executes start chain tests via interchaintest
ictest-start-polkadot:
cd tests/interchaintest && go test -timeout=25m -race -v -run TestPolkadotComposableChainStart .

# Executes IBC tests via interchaintest
ictest-ibc:
cd tests/interchaintest && go test -timeout=25m -race -v -run TestBanksyPicassoIBCTransfer .

# Executes all tests via interchaintest after compling a local image as juno:local
ictest-all: ictest-start-cosmos ictest-start-polkadot ictest-ibc

# Executes push wasm client tests via interchaintest
ictest-push-wasm:
cd tests/interchaintest && go test -race -v -run TestPushWasmClientCode .

.PHONY: ictest-start-cosmos ictest-start-polkadot ictest-ibc ictest-push-wasm ictest-all
52 changes: 44 additions & 8 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ import (
alliancemodulekeeper "github.com/terra-money/alliance/x/alliance/keeper"
alliancemoduletypes "github.com/terra-money/alliance/x/alliance/types"

transfermiddleware "github.com/notional-labs/banksy/v2/x/transfermiddleware"
transfermiddlewarekeeper "github.com/notional-labs/banksy/v2/x/transfermiddleware/keeper"
transfermiddlewaretypes "github.com/notional-labs/banksy/v2/x/transfermiddleware/types"

consensusparamkeeper "github.com/cosmos/cosmos-sdk/x/consensus/keeper"
consensusparamtypes "github.com/cosmos/cosmos-sdk/x/consensus/types"

// this line is used by starport scaffolding # stargate/app/moduleImport
storetypes "github.com/cosmos/cosmos-sdk/store/types"
wasm08 "github.com/cosmos/ibc-go/v7/modules/light-clients/08-wasm/keeper"
ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types"
)

Expand Down Expand Up @@ -171,6 +176,7 @@ var (
tendermint.AppModuleBasic{},
wasm.AppModuleBasic{},
router.AppModuleBasic{},
transfermiddleware.AppModuleBasic{},
consensus.AppModuleBasic{},
alliancemodule.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
Expand All @@ -184,6 +190,7 @@ var (
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
transfermiddlewaretypes.ModuleName: {authtypes.Minter, authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
alliancemoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
alliancemoduletypes.RewardsPoolName: nil,
Expand Down Expand Up @@ -237,13 +244,15 @@ type BanksyApp struct {
FeeGrantKeeper feegrantkeeper.Keeper
GroupKeeper groupkeeper.Keeper
WasmClientKeeper wasmkeeper.Keeper
Wasm08Keeper wasm08.Keeper // TODO: use this name ?
// make scoped keepers public for test purposes
ScopedIBCKeeper capabilitykeeper.ScopedKeeper
ScopedTransferKeeper capabilitykeeper.ScopedKeeper
ConsensusParamsKeeper consensusparamkeeper.Keeper
// this line is used by starport scaffolding # stargate/app/keeperDeclaration
RouterKeeper *routerkeeper.Keeper
AllianceKeeper alliancemodulekeeper.Keeper
TransferMiddlewareKeeper transfermiddlewarekeeper.Keeper
RouterKeeper *routerkeeper.Keeper
AllianceKeeper alliancemodulekeeper.Keeper

mm *module.Manager
sm *module.SimulationManager
Expand Down Expand Up @@ -277,7 +286,7 @@ func NewBanksyApp(
authtypes.StoreKey, banktypes.StoreKey, stakingtypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey,
govtypes.StoreKey, paramstypes.StoreKey, ibchost.StoreKey, upgradetypes.StoreKey, feegrant.StoreKey,
evidencetypes.StoreKey, ibctransfertypes.StoreKey, icqtypes.StoreKey, capabilitytypes.StoreKey, consensusparamtypes.StoreKey, wasmtypes.StoreKey,
crisistypes.StoreKey, routertypes.StoreKey, alliancemoduletypes.StoreKey,
crisistypes.StoreKey, routertypes.StoreKey, transfermiddlewaretypes.StoreKey, group.StoreKey, alliancemoduletypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
)
tkeys := sdk.NewTransientStoreKeys(paramstypes.TStoreKey)
Expand Down Expand Up @@ -374,22 +383,40 @@ func NewBanksyApp(

app.WasmClientKeeper = wasmkeeper.NewKeeper(appCodec, app.keys[wasmtypes.StoreKey])
// Create Transfer Keepers
app.TransferMiddlewareKeeper = transfermiddlewarekeeper.NewKeeper(
keys[transfermiddlewaretypes.StoreKey],
appCodec,
app.IBCKeeper.ChannelKeeper,
app.TransferKeeper,
app.BankKeeper,
)

app.TransferKeeper = ibctransferkeeper.NewKeeper(
appCodec, keys[ibctransfertypes.StoreKey],
app.GetSubspace(ibctransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper, // ICS4Wrapper
&app.TransferMiddlewareKeeper, // ICS4Wrapper
app.IBCKeeper.ChannelKeeper,
&app.IBCKeeper.PortKeeper,
app.AccountKeeper,
app.BankKeeper,
scopedTransferKeeper,
)

app.RouterKeeper = routerkeeper.NewKeeper(appCodec, keys[routertypes.StoreKey], app.GetSubspace(routertypes.ModuleName), app.TransferKeeper, app.IBCKeeper.ChannelKeeper, &app.DistrKeeper, app.BankKeeper, app.IBCKeeper.ChannelKeeper)
app.RouterKeeper = routerkeeper.NewKeeper(
appCodec,
app.keys[routertypes.StoreKey],
app.GetSubspace(routertypes.ModuleName),
app.TransferKeeper,
app.IBCKeeper.ChannelKeeper,
&app.DistrKeeper,
app.BankKeeper,
app.IBCKeeper.ChannelKeeper,
)

transferModule := transfer.NewAppModule(app.TransferKeeper)
transferIBCModule := transfer.NewIBCModule(app.TransferKeeper)
routerModule := router.NewAppModule(app.RouterKeeper)
transfermiddlewareModule := transfermiddleware.NewAppModule(&app.TransferMiddlewareKeeper)

app.ICQKeeper = icqkeeper.NewKeeper(
appCodec, keys[icqtypes.StoreKey], app.GetSubspace(icqtypes.ModuleName),
Expand All @@ -406,6 +433,11 @@ func NewBanksyApp(
routerkeeper.DefaultRefundTransferPacketTimeoutTimestamp,
)

transfermiddlewareStack := transfermiddleware.NewIBCMiddleware(
ibcMiddlewareStack,
app.TransferMiddlewareKeeper,
)

// Create evidence Keeper for to register the IBC light client misbehaviour evidence route
evidenceKeeper := evidencekeeper.NewKeeper(
appCodec, keys[evidencetypes.StoreKey], &app.StakingKeeper, app.SlashingKeeper,
Expand Down Expand Up @@ -436,7 +468,7 @@ func NewBanksyApp(
)

ibcRouter := porttypes.NewRouter()
ibcRouter.AddRoute(ibctransfertypes.ModuleName, ibcMiddlewareStack)
ibcRouter.AddRoute(ibctransfertypes.ModuleName, transfermiddlewareStack)
ibcRouter.AddRoute(icqtypes.ModuleName, icqIBCModule)

// this line is used by starport scaffolding # ibc/app/router
Expand Down Expand Up @@ -476,6 +508,7 @@ func NewBanksyApp(
consensus.NewAppModule(appCodec, app.ConsensusParamsKeeper),
wasm.NewAppModule(app.WasmClientKeeper),
routerModule,
transfermiddlewareModule,
alliancemodule.NewAppModule(appCodec, app.AllianceKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
// this line is used by starport scaffolding # stargate/app/appModule
)
Expand All @@ -495,6 +528,7 @@ func NewBanksyApp(
ibchost.ModuleName,
ibctransfertypes.ModuleName,
routertypes.ModuleName,
transfermiddlewaretypes.ModuleName,
icqtypes.ModuleName,
authtypes.ModuleName,
banktypes.ModuleName,
Expand Down Expand Up @@ -528,6 +562,7 @@ func NewBanksyApp(
upgradetypes.ModuleName,
ibchost.ModuleName,
routertypes.ModuleName,
transfermiddlewaretypes.ModuleName,
ibctransfertypes.ModuleName,
icqtypes.ModuleName,
consensusparamtypes.ModuleName,
Expand Down Expand Up @@ -558,6 +593,7 @@ func NewBanksyApp(
ibctransfertypes.ModuleName,
icqtypes.ModuleName,
routertypes.ModuleName,
transfermiddlewaretypes.ModuleName,
feegrant.ModuleName,
group.ModuleName,
consensusparamtypes.ModuleName,
Expand Down Expand Up @@ -807,8 +843,8 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(stakingtypes.ModuleName)
paramsKeeper.Subspace(distrtypes.ModuleName)
paramsKeeper.Subspace(slashingtypes.ModuleName)
paramsKeeper.Subspace(routertypes.ModuleName).WithKeyTable(routertypes.ParamKeyTable())
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypesv1.ParamKeyTable()) //nolint:staticcheck
paramsKeeper.Subspace(routertypes.ModuleName).WithKeyTable(routertypes.ParamKeyTable()) // TODO:
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govtypesv1.ParamKeyTable()) //nolint:staticcheck
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(icqtypes.ModuleName)
Expand Down
98 changes: 98 additions & 0 deletions app/helpers/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package helpers

import (
"encoding/json"
"fmt"
"testing"
"time"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/log"
tmrand "github.com/cometbft/cometbft/libs/rand"
abcitypes1 "github.com/cometbft/cometbft/proto/tendermint/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banksy "github.com/notional-labs/banksy/v2/app"
"github.com/stretchr/testify/require"
)

// SimAppChainID hardcoded chainID for simulation
const (
SimAppChainID = "fee-app"
)

// DefaultConsensusParams defines the default Tendermint consensus params used
// in feeapp testing.
var DefaultConsensusParams = &abcitypes1.ConsensusParams{
Block: &abcitypes1.BlockParams{
MaxBytes: 200000,
MaxGas: 2000000,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: 302400,
MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration
MaxBytes: 10000,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: []string{
tmtypes.ABCIPubKeyTypeEd25519,
},
},
}

type EmptyAppOptions struct{}

func (EmptyAppOptions) Get(o string) interface{} { return nil }

func NewContextForApp(app banksy.BanksyApp) sdk.Context {
ctx := app.BaseApp.NewContext(false, tmproto.Header{
ChainID: fmt.Sprintf("test-chain-%s", tmrand.Str(4)),
Height: 1,
})
return ctx
}

func Setup(t *testing.T, isCheckTx bool, invCheckPeriod uint) *banksy.BanksyApp {
t.Helper()

app, genesisState := setup(!isCheckTx, invCheckPeriod)
if !isCheckTx {
// InitChain must be called to stop deliverState from being nil
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)

// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}

return app
}

func setup(withGenesis bool, invCheckPeriod uint) (*banksy.BanksyApp, banksy.GenesisState) {
db := dbm.NewMemDB()
encCdc := banksy.MakeEncodingConfig()
app := banksy.NewBanksyApp(
log.NewNopLogger(),
db,
nil,
true,
map[int64]bool{},
banksy.DefaultNodeHome,
invCheckPeriod,
encCdc,
EmptyAppOptions{},
)
if withGenesis {
return app, banksy.NewDefaultGenesisState()
}

return app, banksy.GenesisState{}
}
2 changes: 2 additions & 0 deletions app/ibctesting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# testing package for ibc
Customized version of cosmos-sdk x/ibc/testing and CosmWasm wasmd x/wasm/ibctesting
Loading

0 comments on commit ee7c28e

Please sign in to comment.