Skip to content

Commit

Permalink
update custom bank
Browse files Browse the repository at this point in the history
  • Loading branch information
hoank101 committed Apr 28, 2024
1 parent 11d8f03 commit 4b6e1e5
Show file tree
Hide file tree
Showing 29 changed files with 1,135 additions and 42 deletions.
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,22 @@ relayer-start:

pica-upgrade:
@echo "Starting upgrade"
bash ./scripts/upgrade/upgrade.sh
bash ./scripts/upgrade/upgrade.


###############################################################################
### Integration Tests ###
###############################################################################

integration-test-all: init-test-framework \
test-ibc-hooks

init-test-framework: clean-testing-data install
@echo "Initializing both blockchains..."
./scripts/tests/init-test-framework.sh
./scripts/relayer/relayer-init.sh

test-ibc-hooks:
@echo "Testing ibc-hooks..."
./scripts/tests/ibc-hooks/increment.sh

7 changes: 7 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
alliancemodule "github.com/terra-money/alliance/x/alliance"
alliancemoduletypes "github.com/terra-money/alliance/x/alliance/types"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -211,6 +213,7 @@ var (
ibctransfermiddleware.AppModuleBasic{},
circuit.AppModuleBasic{},
wasm08.AppModuleBasic{},
alliancemodule.AppModuleBasic{},
// this line is used by starport scaffolding # stargate/app/moduleBasic
)

Expand Down Expand Up @@ -394,6 +397,7 @@ func NewComposableApp(
icaModule,
ratelimitModule,
circuit.NewAppModule(appCodec, app.CircuitKeeper),
alliancemodule.NewAppModule(appCodec, app.AllianceKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry, app.GetSubspace(alliancemoduletypes.ModuleName)),
// this line is used by starport scaffolding # stargate/app/appModule
)

Expand Down Expand Up @@ -442,6 +446,7 @@ func NewComposableApp(
stakingmiddlewaretypes.ModuleName,
ibctransfermiddlewaretypes.ModuleName,
wasm08types.ModuleName,
alliancemoduletypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/beginBlockers
)

Expand Down Expand Up @@ -478,6 +483,7 @@ func NewComposableApp(
stakingmiddlewaretypes.ModuleName,
ibctransfermiddlewaretypes.ModuleName,
wasm08types.ModuleName,
alliancemoduletypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -518,6 +524,7 @@ func NewComposableApp(
stakingmiddlewaretypes.ModuleName,
ibctransfermiddlewaretypes.ModuleName,
wasm08types.ModuleName,
alliancemoduletypes.ModuleName,
// this line is used by starport scaffolding # stargate/app/initGenesis
)

Expand Down
28 changes: 24 additions & 4 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package keepers

import (
"fmt"
alliancemodulekeeper "github.com/terra-money/alliance/x/alliance/keeper"
alliancemoduletypes "github.com/terra-money/alliance/x/alliance/types"
"math"
"path/filepath"
"strings"
Expand Down Expand Up @@ -153,6 +155,7 @@ type AppKeepers struct {
GroupKeeper groupkeeper.Keeper
Wasm08Keeper wasm08Keeper.Keeper // TODO: use this name ?
WasmKeeper wasmkeeper.Keeper
AllianceKeeper alliancemodulekeeper.Keeper
IBCHooksKeeper *ibchookskeeper.Keeper
Ics20WasmHooks *ibc_hooks.WasmHooks
HooksICS4Wrapper ibc_hooks.ICS4Middleware
Expand Down Expand Up @@ -201,7 +204,13 @@ func (appKeepers *AppKeepers) InitNormalKeepers(

appKeepers.BankKeeper = custombankkeeper.NewBaseKeeper(
logger,
appCodec, runtime.NewKVStoreService(appKeepers.keys[banktypes.StoreKey]), appKeepers.AccountKeeper, appKeepers.BlacklistedModuleAccountAddrs(maccPerms), &appKeepers.TransferMiddlewareKeeper, govModAddress)
appCodec,
runtime.NewKVStoreService(appKeepers.keys[banktypes.StoreKey]),
appKeepers.AccountKeeper,
appKeepers.BlacklistedModuleAccountAddrs(maccPerms),
&appKeepers.TransferMiddlewareKeeper,
govModAddress,
)

appKeepers.AuthzKeeper = authzkeeper.NewKeeper(
runtime.NewKVStoreService(appKeepers.keys[authzkeeper.StoreKey]),
Expand Down Expand Up @@ -272,11 +281,21 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.FeeGrantKeeper = feegrantkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(appKeepers.keys[feegrant.StoreKey]), appKeepers.AccountKeeper)
appKeepers.UpgradeKeeper = upgradekeeper.NewKeeper(skipUpgradeHeights, runtime.NewKVStoreService(appKeepers.keys[upgradetypes.StoreKey]), appCodec, homePath, bApp, govModAddress)

appKeepers.BankKeeper.RegisterKeepers(appKeepers.StakingKeeper)
appKeepers.AllianceKeeper = alliancemodulekeeper.NewKeeper(
appCodec,
runtime.NewKVStoreService(appKeepers.keys[alliancemoduletypes.StoreKey]),
appKeepers.AccountKeeper,
appKeepers.BankKeeper,
appKeepers.StakingKeeper,
appKeepers.DistrKeeper,
authtypes.FeeCollectorName,
govModAddress,
)
appKeepers.BankKeeper.RegisterKeepers(appKeepers.AllianceKeeper, appKeepers.StakingKeeper)
// register the staking hooks
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
appKeepers.StakingKeeper.SetHooks(
stakingtypes.NewMultiStakingHooks(appKeepers.DistrKeeper.Hooks(), appKeepers.SlashingKeeper.Hooks()),
stakingtypes.NewMultiStakingHooks(appKeepers.DistrKeeper.Hooks(), appKeepers.SlashingKeeper.Hooks(), appKeepers.AllianceKeeper.StakingHooks()),
)

// ... other modules keepers
Expand Down Expand Up @@ -334,7 +353,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.PfmKeeper = pfmkeeper.NewKeeper(
appCodec,
appKeepers.keys[pfmtypes.StoreKey],
appKeepers.TransferKeeper.Keeper,
nil,
appKeepers.IBCKeeper.ChannelKeeper,
&appKeepers.DistrKeeper,
appKeepers.BankKeeper,
Expand Down Expand Up @@ -583,6 +602,7 @@ func (appKeepers *AppKeepers) initParamsKeeper(appCodec codec.BinaryCodec, legac
paramsKeeper.Subspace(transfermiddlewaretypes.ModuleName).WithKeyTable(transfermiddlewaretypes.ParamKeyTable())
paramsKeeper.Subspace(stakingmiddlewaretypes.ModuleName)
paramsKeeper.Subspace(ibctransfermiddlewaretypes.ModuleName)
paramsKeeper.Subspace(alliancemoduletypes.ModuleName).WithKeyTable(alliancemoduletypes.ParamKeyTable())

return paramsKeeper
}
Expand Down
2 changes: 2 additions & 0 deletions app/keepers/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
alliancemoduletypes "github.com/terra-money/alliance/x/alliance/types"

// bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"

Expand Down Expand Up @@ -65,6 +66,7 @@ func (appKeepers *AppKeepers) GenerateKeys() {
ibcexported.StoreKey,
ibchookstypes.StoreKey, icahosttypes.StoreKey,
ratelimitmoduletypes.StoreKey, txBoundaryTypes.StoreKey,
alliancemoduletypes.StoreKey,
)

// Define transient store keys
Expand Down
14 changes: 7 additions & 7 deletions custom/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@ package keeper

import (
"context"

"cosmossdk.io/core/store"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
accountkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
banktypes "github.com/notional-labs/composable/v6/custom/bank/types"
alliancekeeper "github.com/terra-money/alliance/x/alliance/keeper"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

banktypes "github.com/notional-labs/composable/v6/custom/bank/types"

transfermiddlewarekeeper "github.com/notional-labs/composable/v6/x/transfermiddleware/keeper"
)

type Keeper struct {
bankkeeper.BaseKeeper

tfmk banktypes.TransferMiddlewareKeeper
sk banktypes.StakingKeeper
acck accountkeeper.AccountKeeper
ak alliancekeeper.Keeper
}

var _ bankkeeper.Keeper = Keeper{}
Expand All @@ -40,12 +39,13 @@ func NewBaseKeeper(
keeper := Keeper{
BaseKeeper: bankkeeper.NewBaseKeeper(cdc, storeService, ak, blockedAddrs, authority, logger),
tfmk: tfmk,
acck: ak,
ak: alliancekeeper.Keeper{},
}
return keeper
}

func (k *Keeper) RegisterKeepers(sk banktypes.StakingKeeper) {
func (k *Keeper) RegisterKeepers(ak alliancekeeper.Keeper, sk banktypes.StakingKeeper) {
k.ak = ak
k.sk = sk
}

Expand Down
124 changes: 124 additions & 0 deletions custom/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package keeper

import (
"context"

"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/hashicorp/go-metrics"
)

type msgServer struct {
types.MsgServer

keeper bankkeeper.Keeper
addressCodec address.Codec
}

var _ types.MsgServer = msgServer{}

func NewMsgServerImpl(keeper Keeper, addressCodec address.Codec) types.MsgServer {
return &msgServer{
MsgServer: bankkeeper.NewMsgServerImpl(keeper),
keeper: keeper,
addressCodec: addressCodec,
}
}

func (k msgServer) Send(goCtx context.Context, msg *types.MsgSend) (*types.MsgSendResponse, error) {
from, err := k.addressCodec.StringToBytes(msg.FromAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid from address: %s", err)
}
to, err := k.addressCodec.StringToBytes(msg.ToAddress)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid to address: %s", err)
}

if !msg.Amount.IsValid() {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}

if !msg.Amount.IsAllPositive() {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, msg.Amount.String())
}

ctx := sdk.UnwrapSDKContext(goCtx)
if err := k.keeper.IsSendEnabledCoins(ctx, msg.Amount...); err != nil {
return nil, err
}

if k.keeper.BlockedAddr(to) {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", msg.ToAddress)
}

err = k.keeper.SendCoins(ctx, from, to, msg.Amount)
if err != nil {
return nil, err
}

defer func() {
for _, a := range msg.Amount {
if a.Amount.IsInt64() {
telemetry.SetGaugeWithLabels(
[]string{"tx", "msg", "send"},
float32(a.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", a.Denom)},
)
}
}
}()

return &types.MsgSendResponse{}, nil
}

func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*types.MsgMultiSendResponse, error) {
if len(msg.Inputs) == 0 {
return nil, types.ErrNoInputs
}

if len(msg.Inputs) != 1 {
return nil, types.ErrMultipleSenders
}

if len(msg.Outputs) == 0 {
return nil, types.ErrNoOutputs
}

if err := types.ValidateInputOutputs(msg.Inputs[0], msg.Outputs); err != nil {
return nil, err
}

ctx := sdk.UnwrapSDKContext(goCtx)

// NOTE: totalIn == totalOut should already have been checked
for _, in := range msg.Inputs {
if err := k.keeper.IsSendEnabledCoins(ctx, in.Coins...); err != nil {
return nil, err
}
}

for _, out := range msg.Outputs {
accAddr, err := k.addressCodec.StringToBytes(out.Address)
if err != nil {
return nil, err
}

if k.keeper.BlockedAddr(accAddr) {
return nil, errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", out.Address)
}
}

err := k.keeper.InputOutputCoins(ctx, msg.Inputs[0], msg.Outputs)
if err != nil {
return nil, err
}

return &types.MsgMultiSendResponse{}, nil
}
15 changes: 9 additions & 6 deletions custom/bank/module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bank

import (
"cosmossdk.io/core/address"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
Expand All @@ -15,25 +16,27 @@ import (

type AppModule struct {
bankmodule.AppModule
keeper custombankkeeper.Keeper
subspace exported.Subspace
keeper custombankkeeper.Keeper
subspace exported.Subspace
addressCodec address.Codec
}

// NewAppModule creates a new AppModule object
func NewAppModule(cdc codec.Codec, keeper custombankkeeper.Keeper, accountKeeper types.AccountKeeper, ss exported.Subspace) AppModule {
bankModule := bankmodule.NewAppModule(cdc, keeper, accountKeeper, ss)
return AppModule{
AppModule: bankModule,
keeper: keeper,
subspace: ss,
AppModule: bankModule,
keeper: keeper,
subspace: ss,
addressCodec: accountKeeper.AddressCodec(),
}
}

// RegisterServices registers module services.
// NOTE: Overriding this method as not doing so will cause a panic
// when trying to force this custom keeper into a bankkeeper.BaseKeeper
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), bankkeeper.NewMsgServerImpl(am.keeper))
types.RegisterMsgServer(cfg.MsgServer(), custombankkeeper.NewMsgServerImpl(am.keeper, am.addressCodec))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

m := bankkeeper.NewMigrator(am.keeper.BaseKeeper, am.subspace)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
google.golang.org/grpc v1.62.1
gotest.tools/v3 v3.5.1
mvdan.cc/gofumpt v0.4.0
github.com/terra-money/alliance v0.4.3
)

require github.com/cometbft/cometbft-db v0.11.0 // indirect
Expand Down Expand Up @@ -376,6 +377,5 @@ replace (
github.com/prometheus/common => github.com/prometheus/common v0.47.0

github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
// github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v8 => github.com/notional-labs/ibc-apps/middleware/packet-forward-middleware/v8 v8.0.0-0.20240327105252-f31a09121d57

)
Loading

0 comments on commit 4b6e1e5

Please sign in to comment.