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: added params to control whether the module is enabled #13

Merged
merged 2 commits into from
Apr 6, 2023
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
2 changes: 2 additions & 0 deletions keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
panic(fmt.Sprintf("could not claim port capability: %v", err))
}
}
k.SetParams(ctx, state.Params)
}

// ExportGenesis exports ibc nft-transfer module's portID and class trace info into its genesis state.
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{
PortId: k.GetPort(ctx),
Traces: k.GetAllClassTraces(ctx),
Params: k.GetParams(ctx),
}
}
27 changes: 20 additions & 7 deletions keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"

host "github.com/cosmos/ibc-go/v5/modules/core/24-host"

Expand All @@ -16,8 +17,9 @@ import (

// Keeper defines the IBC non fungible transfer keeper
type Keeper struct {
storeKey storetypes.StoreKey
cdc codec.Codec
storeKey storetypes.StoreKey
cdc codec.Codec
paramSpace paramtypes.Subspace

ics4Wrapper types.ICS4Wrapper
channelKeeper types.ChannelKeeper
Expand All @@ -29,18 +31,29 @@ type Keeper struct {

// NewKeeper creates a new IBC nft-transfer Keeper instance
func NewKeeper(
cdc codec.Codec, key storetypes.StoreKey,
ics4Wrapper types.ICS4Wrapper, channelKeeper types.ChannelKeeper, portKeeper types.PortKeeper,
authKeeper types.AccountKeeper, nftKeeper types.NFTKeeper, scopedKeeper capabilitykeeper.ScopedKeeper,
cdc codec.Codec,
key storetypes.StoreKey,
paramSpace paramtypes.Subspace,
ics4Wrapper types.ICS4Wrapper,
channelKeeper types.ChannelKeeper,
portKeeper types.PortKeeper,
authKeeper types.AccountKeeper,
nftKeeper types.NFTKeeper,
scopedKeeper capabilitykeeper.ScopedKeeper,
) Keeper {
// set KeyTable if it has not already been set
if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(types.ParamKeyTable())
}
return Keeper{
cdc: cdc,
storeKey: key,
cdc: cdc,
paramSpace: paramSpace,
ics4Wrapper: ics4Wrapper,
channelKeeper: channelKeeper,
portKeeper: portKeeper,
authKeeper: authKeeper,
nftKeeper: nftKeeper,
authKeeper: authKeeper,
scopedKeeper: scopedKeeper,
}
}
Expand Down
31 changes: 31 additions & 0 deletions keeper/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/bianjieai/nft-transfer/types"
)

// GetSendEnabled retrieves the send enabled boolean from the paramstore
func (k Keeper) GetSendEnabled(ctx sdk.Context) bool {
var res bool
k.paramSpace.Get(ctx, types.KeySendEnabled, &res)
return res
}

// GetReceiveEnabled retrieves the receive enabled boolean from the paramstore
func (k Keeper) GetReceiveEnabled(ctx sdk.Context) bool {
var res bool
k.paramSpace.Get(ctx, types.KeyReceiveEnabled, &res)
return res
}

// GetParams returns the total set of ibc-transfer parameters.
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams(k.GetSendEnabled(ctx), k.GetReceiveEnabled(ctx))
}

// SetParams sets the total set of ibc-transfer parameters.
func (k Keeper) SetParams(ctx sdk.Context, params types.Params) {
k.paramSpace.SetParamSet(ctx, &params)
}
6 changes: 6 additions & 0 deletions keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (k Keeper) SendTransfer(
timeoutTimestamp uint64,
memo string,
) error {
if !k.GetSendEnabled(ctx) {
return types.ErrSendDisabled
}
sourceChannelEnd, found := k.channelKeeper.GetChannel(ctx, sourcePort, sourceChannel)
if !found {
return sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", sourcePort, sourceChannel)
Expand Down Expand Up @@ -127,6 +130,9 @@ func (k Keeper) SendTransfer(
// unescrowed and sent to the receiving address.
func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet,
data types.NonFungibleTokenPacketData) error {
if !k.GetReceiveEnabled(ctx) {
return types.ErrReceiveDisabled
}

// validate packet data upon receiving
if err := data.ValidateBasic(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions proto/ibc/applications/nft_transfer/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ message GenesisState {
string port_id = 1;
repeated ClassTrace traces = 2
[ (gogoproto.castrepeated) = "Traces", (gogoproto.nullable) = false ];
Params params = 3 [(gogoproto.nullable) = false];
}
12 changes: 12 additions & 0 deletions proto/ibc/applications/nft_transfer/v1/transfer.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,15 @@ message ClassTrace {
// base classID of the relayed non-fungible token.
string base_class_id = 2;
}

// Params defines the set of IBC nft-transfer parameters.
// NOTE: To prevent a nft from being transferred, set the
// TransfersEnabled parameter to false.
message Params {
// send_enabled enables or disables all cross-chain nft transfers from this
// chain.
bool send_enabled = 1;
// receive_enabled enables or disables all cross-chain nft transfers to this
// chain.
bool receive_enabled = 2;
}
3 changes: 2 additions & 1 deletion testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func NewSimApp(
)

app.NFTTransferKeeper = ibcnfttransferkeeper.NewKeeper(
appCodec, keys[ibcnfttransfertypes.StoreKey],
appCodec, keys[ibcnfttransfertypes.StoreKey], app.GetSubspace(ibcnfttransfertypes.ModuleName),
app.IBCKeeper.ChannelKeeper, app.IBCKeeper.ChannelKeeper, &app.IBCKeeper.PortKeeper,
app.AccountKeeper, mock.Wrap(appCodec, app.NFTKeeper), scopedNFTTransferKeeper,
)
Expand Down Expand Up @@ -876,6 +876,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(ibchost.ModuleName)
paramsKeeper.Subspace(icacontrollertypes.SubModuleName)
paramsKeeper.Subspace(icahosttypes.SubModuleName)
paramsKeeper.Subspace(ibcnfttransfertypes.ModuleName)

return paramsKeeper
}
2 changes: 2 additions & 0 deletions types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ var (
ErrInvalidPacket = sdkerrors.Register(ModuleName, 7, "invalid non-fungible token packet")
ErrTraceNotFound = sdkerrors.Register(ModuleName, 8, "classTrace trace not found")
ErrMarshal = sdkerrors.Register(ModuleName, 9, "failed to marshal token data")
ErrSendDisabled = sdkerrors.Register(ModuleName, 10, "non-fungible token transfers from this chain are disabled")
ErrReceiveDisabled = sdkerrors.Register(ModuleName, 11, "non-fungible token transfers to this chain are disabled")
)
9 changes: 7 additions & 2 deletions types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

// NewGenesisState creates a new ibc nft-transfer GenesisState instance.
func NewGenesisState(portID string, traces Traces) *GenesisState {
func NewGenesisState(portID string, traces Traces, params Params) *GenesisState {
return &GenesisState{
PortId: portID,
Traces: traces,
Params: params,
}
}

Expand All @@ -17,6 +18,7 @@ func DefaultGenesisState() *GenesisState {
return &GenesisState{
PortId: PortID,
Traces: Traces{},
Params: DefaultParams(),
}
}

Expand All @@ -26,5 +28,8 @@ func (gs GenesisState) Validate() error {
if err := host.PortIdentifierValidator(gs.PortId); err != nil {
return err
}
return gs.Traces.Validate()
if err := gs.Traces.Validate(); err != nil {
return err
}
return gs.Params.Validate()
}
79 changes: 67 additions & 12 deletions types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions types/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package types

import (
"fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
)

const (
// DefaultSendEnabled enabled
DefaultSendEnabled = true
// DefaultReceiveEnabled enabled
DefaultReceiveEnabled = true
)

var (
// KeySendEnabled is store's key for SendEnabled Params
KeySendEnabled = []byte("SendEnabled")
// KeyReceiveEnabled is store's key for ReceiveEnabled Params
KeyReceiveEnabled = []byte("ReceiveEnabled")
)

// ParamKeyTable type declaration for parameters
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// NewParams creates a new parameter configuration for the ibc transfer module
func NewParams(enableSend, enableReceive bool) Params {
return Params{
SendEnabled: enableSend,
ReceiveEnabled: enableReceive,
}
}

// DefaultParams is the default parameter configuration for the ibc-transfer module
func DefaultParams() Params {
return NewParams(DefaultSendEnabled, DefaultReceiveEnabled)
}

// Validate all ibc-transfer module parameters
func (p Params) Validate() error {
if err := validateEnabled(p.SendEnabled); err != nil {
return err
}

return validateEnabled(p.ReceiveEnabled)
}

// ParamSetPairs implements params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeySendEnabled, p.SendEnabled, validateEnabled),
paramtypes.NewParamSetPair(KeyReceiveEnabled, p.ReceiveEnabled, validateEnabled),
}
}

func validateEnabled(i interface{}) error {
_, ok := i.(bool)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

return nil
}
Loading