Skip to content

Commit

Permalink
Add ante to check token (#2488)
Browse files Browse the repository at this point in the history
* add ante to check token

* can't deposit swap coin for proposal

* update irismod version

* apply comment from github

* refactor code
  • Loading branch information
Zhiqiang Zhang authored Jan 11, 2021
1 parent 661df58 commit 6663170
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewAnteHandler(
ante.NewSigGasConsumeDecorator(ak, sigGasConsumer),
ante.NewSigVerificationDecorator(ak, signModeHandler),
ante.NewIncrementSequenceDecorator(ak),
NewCheckTokenDecorator(tk),
tokenkeeper.NewValidateTokenFeeDecorator(tk, bk),
oraclekeeper.NewValidateOracleAuthDecorator(ok, oak),
)
Expand Down
64 changes: 64 additions & 0 deletions app/token_check.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package app

import (
"strings"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
ibctransfertypes "github.com/cosmos/cosmos-sdk/x/ibc/applications/transfer/types"

coinswaptypes "github.com/irisnet/irismod/modules/coinswap/types"
tokenkeeper "github.com/irisnet/irismod/modules/token/keeper"
tokentypes "github.com/irisnet/irismod/modules/token/types"
)

// CheckTokenDecorator is responsible for restricting the token participation of the swap prefix
type CheckTokenDecorator struct {
tk tokenkeeper.Keeper
}

// NewCheckTokenDecorator return a instance of CheckTokenDecorator
func NewCheckTokenDecorator(tk tokenkeeper.Keeper) CheckTokenDecorator {
return CheckTokenDecorator{
tk: tk,
}
}

// AnteHandle check the transaction
func (ctd CheckTokenDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *ibctransfertypes.MsgTransfer:
if containSwapCoin(msg.Token) {
return ctx, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest, "can't transfer coinswap liquidity tokens through the IBC module")
}
case *tokentypes.MsgBurnToken:
if _, err := ctd.tk.GetToken(ctx, msg.Symbol); err != nil {
return ctx, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest, "burnt failed, only native tokens can be burnt")
}
case *govtypes.MsgSubmitProposal:
if containSwapCoin(msg.InitialDeposit...) {
return ctx, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest, "can't deposit coinswap liquidity token for proposal")
}
case *govtypes.MsgDeposit:
if containSwapCoin(msg.Amount...) {
return ctx, sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest, "can't deposit coinswap liquidity token %s for proposal")
}
}
}
return next(ctx, tx, simulate)
}

func containSwapCoin(coins ...sdk.Coin) bool {
for _, coin := range coins {
if strings.HasPrefix(coin.Denom, coinswaptypes.FormatUniABSPrefix) {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/golang/protobuf v1.4.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/irisnet/irismod v1.1.1-0.20210110094232-8d48f9a795f4
github.com/irisnet/irismod v1.1.1-0.20210111090024-463e3e11dc14
github.com/olebedev/config v0.0.0-20190528211619-364964f3a8e4
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmK
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/influxdb1-client v0.0.0-20191209144304-8bf82d3c094d/go.mod h1:qj24IKcXYK6Iy9ceXlo3Tc+vtHo9lIhSX5JddghvEPo=
github.com/irisnet/irismod v1.1.1-0.20210110094232-8d48f9a795f4 h1:yATUgeN/8GE4TJC37Z1xibj0hh2wHgD1qgGclA8udTQ=
github.com/irisnet/irismod v1.1.1-0.20210110094232-8d48f9a795f4/go.mod h1:j7coXR8hg12Jp6B9MlvrnYdjoPGf8vTV/GIyZeL8x9I=
github.com/irisnet/irismod v1.1.1-0.20210111090024-463e3e11dc14 h1:bIwEg3HTAjKtHvUPIQ/RXEzig7cd1VE8PSM7OVqNFws=
github.com/irisnet/irismod v1.1.1-0.20210111090024-463e3e11dc14/go.mod h1:j7coXR8hg12Jp6B9MlvrnYdjoPGf8vTV/GIyZeL8x9I=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
Expand Down
6 changes: 3 additions & 3 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,12 +536,12 @@ func migrateHTLC(initialState v0_16.GenesisFileState) *htlctypes.GenesisState {
func migrateCoinswap(initialState v0_16.GenesisFileState) *coinswaptypes.GenesisState {
fee, _ := sdk.NewDecFromStr(initialState.SwapData.Params.Fee.FloatString(sdk.Precision))
params := coinswaptypes.Params{
Fee: fee,
StandardDenom: UIRIS,
Fee: fee,
}

return &coinswaptypes.GenesisState{
Params: params,
Params: params,
StandardDenom: UIRIS,
}
}

Expand Down

0 comments on commit 6663170

Please sign in to comment.