Skip to content

Commit

Permalink
1256 support for tokens restricted with attributes (#563)
Browse files Browse the repository at this point in the history
* add injection of attribute checking...still POC atm

* expect restricted function to only return err, refactor names

* move send restrictions check to SendCoins

* remove line that was missed during refactor

* add code comments

* move accept logic to msg server

* Revert "add code comments"

This reverts commit d88da38.

* revert ensure restirctions function into send keeper, add restrictions check to mult-send

* add back send enable checks to msg server

* add change log
  • Loading branch information
nullpointer0x00 authored and SpicyLemon committed Mar 20, 2023
1 parent 6984379 commit a6dcb24
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## Unreleased

### Features

* [PR 563](https://github.com/provenance-io/cosmos-sdk/pull/563) Add support for applying send restrictions before doing a bank send

### Improvements

* [PR 565](https://github.com/provenance-io/cosmos-sdk/pull/565) Removes locks around state listening. There's no concurrency risk.
Expand Down
2 changes: 2 additions & 0 deletions x/bank/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type Keeper interface {

GetAuthority() string

SetSendRestrictionsFunc(sendRestrictionsFunc func(sdk.Context, string, string, string) error)

types.QueryServer
}

Expand Down
1 change: 0 additions & 1 deletion x/bank/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,3 @@ func (k msgServer) MultiSend(goCtx context.Context, msg *types.MsgMultiSend) (*t

return &types.MsgMultiSendResponse{}, nil
}

44 changes: 43 additions & 1 deletion x/bank/keeper/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SendKeeper interface {

IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool
IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) error
EnsureSendRestrictions(ctx sdk.Context, from, to string, coins ...sdk.Coin) error

BlockedAddr(addr sdk.AccAddress) bool
}
Expand All @@ -58,6 +59,8 @@ type BaseSendKeeper struct {

qk types.QuarantineKeeper
sk types.SanctionKeeper

sendRestrictionsFunc func(sdk.Context, string, string, string) error
}

func NewBaseSendKeeper(
Expand Down Expand Up @@ -109,6 +112,15 @@ func (k BaseSendKeeper) GetParams(ctx sdk.Context) (params types.Params) {
return params
}

// SetSendRestrictionsFunc set a function to be called before sends can occur
// if not set, it is a no-op
func (k *BaseSendKeeper) SetSendRestrictionsFunc(sendRestrictionsFunc func(sdk.Context, string, string, string) error) {
if k.sendRestrictionsFunc != nil {
panic("the send restrictions function has already been set")
}
k.sendRestrictionsFunc = sendRestrictionsFunc
}

// SetParams sets the total set of bank parameters.
func (k BaseSendKeeper) SetParams(ctx sdk.Context, params types.Params) {
if len(params.SendEnabled) > 0 {
Expand All @@ -128,6 +140,16 @@ func (k BaseSendKeeper) InputOutputCoins(ctx sdk.Context, inputs []types.Input,
return err
}

// ensure all inputs and outputs pass any restrictions
for _, input := range inputs {
for _, output := range outputs {
err := k.EnsureSendRestrictions(ctx, input.Address, output.Address, input.Coins...)
if err != nil {
return err
}
}
}

allInputAddrs := make([]sdk.AccAddress, len(inputs))

for i, in := range inputs {
Expand Down Expand Up @@ -229,7 +251,12 @@ func (k BaseSendKeeper) SendCoins(ctx sdk.Context, fromAddr sdk.AccAddress, toAd
// of possible quarantine on the toAddr.
// An error is returned upon failure.
func (k BaseSendKeeper) SendCoinsBypassQuarantine(ctx sdk.Context, fromAddr sdk.AccAddress, toAddr sdk.AccAddress, amt sdk.Coins) error {
err := k.subUnlockedCoins(ctx, fromAddr, amt)
err := k.EnsureSendRestrictions(ctx, fromAddr.String(), toAddr.String(), amt...)
if err != nil {
return err
}

err = k.subUnlockedCoins(ctx, fromAddr, amt)
if err != nil {
return err
}
Expand Down Expand Up @@ -424,6 +451,21 @@ func (k BaseSendKeeper) IsSendEnabledCoins(ctx sdk.Context, coins ...sdk.Coin) e
return nil
}

// EnsureSendRestrictions applies the send restrictions function returns error if send restrictions do not pass
// no-op if the send restrictions function is not set
func (k *BaseSendKeeper) EnsureSendRestrictions(ctx sdk.Context, from, to string, coins ...sdk.Coin) error {
if k.sendRestrictionsFunc == nil {
return nil
}
for _, coin := range coins {
err := k.sendRestrictionsFunc(ctx, from, to, coin.Denom)
if err != nil {
return err
}
}
return nil
}

// IsSendEnabledCoin returns the current SendEnabled status of the provided coin's denom
func (k BaseSendKeeper) IsSendEnabledCoin(ctx sdk.Context, coin sdk.Coin) bool {
return k.IsSendEnabledDenom(ctx, coin.Denom)
Expand Down

0 comments on commit a6dcb24

Please sign in to comment.