Skip to content

Commit

Permalink
correct priv implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
catShaark committed Sep 20, 2024
1 parent 23b7c96 commit 8ffadb0
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 52 deletions.
24 changes: 24 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ import (
assetmodulekeeper "github.com/realiotech/realio-network/x/asset/keeper"
assetmoduletypes "github.com/realiotech/realio-network/x/asset/types"

"github.com/realiotech/realio-network/x/asset/priviledges/clawback"
"github.com/realiotech/realio-network/x/asset/priviledges/freeze"
mintpriviledge "github.com/realiotech/realio-network/x/asset/priviledges/mint"
"github.com/realiotech/realio-network/x/asset/priviledges/transfer_auth"

realionetworktypes "github.com/realiotech/realio-network/types"

// unnamed import of statik for swagger UI support
Expand Down Expand Up @@ -309,6 +314,8 @@ func New(
// multi-staking keys
multistakingtypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
freeze.StoreKey,
transfer_auth.StoreKey,
)

// Add the EVM transient store key
Expand Down Expand Up @@ -415,6 +422,23 @@ func New(
app.AccountKeeper,
)

err := app.AssetKeeper.AddPrivilege(mintpriviledge.NewMintPriviledge(app.BankKeeper))
if err != nil {
panic(err)
}
err = app.AssetKeeper.AddPrivilege(freeze.NewFreezePriviledge(keys[freeze.StoreKey]))
if err != nil {
panic(err)
}
err = app.AssetKeeper.AddPrivilege(transfer_auth.NewTransferAuthPriviledge(keys[transfer_auth.StoreKey]))
if err != nil {
panic(err)
}
err = app.AssetKeeper.AddPrivilege(clawback.NewClawbackPriviledge(app.BankKeeper))
if err != nil {
panic(err)
}

app.BankKeeper.AppendSendRestriction(app.AssetKeeper.AssetSendRestriction)

// IBC Keeper
Expand Down
45 changes: 45 additions & 0 deletions app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package apptesting

import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/realiotech/realio-network/app"
"github.com/realiotech/realio-network/x/asset/keeper"
"github.com/realiotech/realio-network/x/asset/types"
)

type KeeperTestSuite struct {
suite.Suite
app *app.RealioNetwork
ctx sdk.Context

assetKeeper *keeper.Keeper
govkeeper govkeeper.Keeper
msgServer types.MsgServer
bankKeeper bankkeeper.Keeper
}

func (suite *KeeperTestSuite) SetupTest() {
app := app.Setup(false, nil, 3)

suite.app = app
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: app.LastBlockHeight() + 1})
suite.assetKeeper = keeper.NewKeeper(
app.AppCodec(), app.InterfaceRegistry(), app.GetKey(types.StoreKey),
app.GetMemKey(types.MemStoreKey), app.GetSubspace(types.ModuleName), app.BankKeeper, app.AccountKeeper,
)
suite.govkeeper = app.GovKeeper
suite.bankKeeper = app.BankKeeper
}

func (suite *KeeperTestSuite)

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
25 changes: 14 additions & 11 deletions x/asset/priviledges/clawback/msg_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
assettypes "github.com/realiotech/realio-network/x/asset/types"
// "github.com/cosmos/cosmos-sdk/store/types"
)

Expand All @@ -19,9 +20,9 @@ func (cp ClawbackPriviledge) clawbackToken(ctx sdk.Context, msg *MsgClawbackToke
return fmt.Errorf("invalid bech 32 address: %v", err)
}

spendable := cp.bk.SpendableCoin(ctx, senderAddr, tokenID)
spendable := cp.bk.SpendableCoins(ctx, senderAddr)

if spendable.Amount.LT(sdk.NewIntFromUint64(msg.Amount)) {
if spendable.IsAllLT(sdk.NewCoins(sdk.NewCoin(tokenID, sdk.NewIntFromUint64(msg.Amount)))) {
return fmt.Errorf("insufficient funds want %s have %s", clawbackCoin.String(), spendable.String())
}

Expand All @@ -33,14 +34,16 @@ func (cp ClawbackPriviledge) clawbackToken(ctx sdk.Context, msg *MsgClawbackToke
return err
}

func (cp ClawbackPriviledge) MsgHandler(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch msg := msg.(type) {
case *MsgClawbackToken:
return nil, cp.clawbackToken(ctx, msg, tokenID, privAcc)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Clawback priviledge", msg)
return nil, errors.Errorf(errMsg)
func (cp ClawbackPriviledge) MsgHandler() assettypes.MsgHandler {
return func(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch msg := msg.(type) {
case *MsgClawbackToken:
return nil, cp.clawbackToken(ctx, msg, tokenID, privAcc)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Clawback priviledge", msg)
return nil, errors.Errorf(errMsg)
}
}
}
9 changes: 9 additions & 0 deletions x/asset/priviledges/clawback/priv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package clawback

import (
"context"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
proto "github.com/gogo/protobuf/proto"
assettypes "github.com/realiotech/realio-network/x/asset/types"
)

Expand All @@ -26,3 +29,9 @@ func (cp ClawbackPriviledge) RegisterInterfaces(registry cdctypes.InterfaceRegis
&MsgClawbackToken{},
)
}

func (cb ClawbackPriviledge) QueryHandler() assettypes.QueryHandler {
return func(context context.Context, privQuery proto.Message, tokenID string) (proto.Message, error) {
return nil, nil
}
}
5 changes: 5 additions & 0 deletions x/asset/priviledges/clawback/priv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package clawback_test

// import (
// assettest "github.com/realiotech/realio-network/x/asset/keeper"
// )
2 changes: 1 addition & 1 deletion x/asset/priviledges/clawback/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import (

type BankKeeper interface {
SendCoins(ctx sdk.Context, senderAddr, recipientAddr sdk.AccAddress, amt sdk.Coins) error
SpendableCoin(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
}
23 changes: 13 additions & 10 deletions x/asset/priviledges/freeze/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
assettypes "github.com/realiotech/realio-network/x/asset/types"
// "github.com/cosmos/cosmos-sdk/store/types"
)

Expand All @@ -24,16 +25,18 @@ func (mp FreezePriviledge) UnfreezeToken(ctx sdk.Context, msg *MsgUnfreezeToken,
return nil
}

func (mp FreezePriviledge) MsgHandler(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)
func (mp FreezePriviledge) MsgHandler() assettypes.MsgHandler {
return func(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch msg := msg.(type) {
case *MsgFreezeToken:
return nil, mp.FreezeToken(ctx, msg, tokenID)
case *MsgUnfreezeToken:
return nil, mp.UnfreezeToken(ctx, msg, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Transfer auth priviledge", msg)
return nil, errors.Errorf(errMsg)
switch msg := msg.(type) {
case *MsgFreezeToken:
return nil, mp.FreezeToken(ctx, msg, tokenID)
case *MsgUnfreezeToken:
return nil, mp.UnfreezeToken(ctx, msg, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Transfer auth priviledge", msg)
return nil, errors.Errorf(errMsg)
}
}
}
3 changes: 2 additions & 1 deletion x/asset/priviledges/freeze/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

var (
_ keeper.RestrictionChecker = (*FreezePriviledge)(nil)
_ keeper.RestrictionChecker = (*FreezePriviledge)(nil)
StoreKey = "freezepriv"
)

const priv_name = "transfer_auth"
Expand Down
20 changes: 11 additions & 9 deletions x/asset/priviledges/mint/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ func (mp MintPriviledge) MintToken(ctx sdk.Context, msg *MsgMintToken, tokenID s
return err
}

func (mp MintPriviledge) MsgHandler(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch msg := msg.(type) {
case *MsgMintToken:
return nil, mp.MintToken(ctx, msg, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Mint priviledge", msg)
return nil, errors.Errorf(errMsg)
func (mp MintPriviledge) MsgHandler() assettypes.MsgHandler {
return func(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch msg := msg.(type) {
case *MsgMintToken:
return nil, mp.MintToken(ctx, msg, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Mint priviledge", msg)
return nil, errors.Errorf(errMsg)
}
}
}
9 changes: 9 additions & 0 deletions x/asset/priviledges/mint/priv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package mint

import (
"context"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
proto "github.com/gogo/protobuf/proto"
assettypes "github.com/realiotech/realio-network/x/asset/types"
)

Expand All @@ -26,3 +29,9 @@ func (mp MintPriviledge) RegisterInterfaces(registry cdctypes.InterfaceRegistry)
&MsgMintToken{},
)
}

func (mp MintPriviledge) QueryHandler() assettypes.QueryHandler {
return func(context context.Context, privQuery proto.Message, tokenID string) (proto.Message, error) {
return nil, nil
}
}
19 changes: 11 additions & 8 deletions x/asset/priviledges/transfer_auth/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
assettypes "github.com/realiotech/realio-network/x/asset/types"
// "github.com/cosmos/cosmos-sdk/store/types"
)

Expand All @@ -21,14 +22,16 @@ func (mp TransferAuthPriviledge) UpdateAllowList(ctx sdk.Context, msg *MsgUpdate
return nil
}

func (mp TransferAuthPriviledge) MsgHandler(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)
func (mp TransferAuthPriviledge) MsgHandler() assettypes.MsgHandler {
return func(context context.Context, msg proto.Message, tokenID string, privAcc sdk.AccAddress) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch msg := msg.(type) {
case *MsgUpdateAllowList:
return nil, mp.UpdateAllowList(ctx, msg, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Transfer auth priviledge", msg)
return nil, errors.Errorf(errMsg)
switch msg := msg.(type) {
case *MsgUpdateAllowList:
return nil, mp.UpdateAllowList(ctx, msg, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized message type: %T for Transfer auth priviledge", msg)
return nil, errors.Errorf(errMsg)
}
}
}
5 changes: 3 additions & 2 deletions x/asset/priviledges/transfer_auth/priv.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package transfer_auth

import (
"github.com/realiotech/realio-network/x/asset/keeper"
assetkeeper "github.com/realiotech/realio-network/x/asset/keeper"

cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
Expand All @@ -10,7 +10,8 @@ import (
)

var (
_ keeper.RestrictionChecker = (*TransferAuthPriviledge)(nil)
_ assetkeeper.RestrictionChecker = (*TransferAuthPriviledge)(nil)
StoreKey = "transferauthpriv"
)

const priv_name = "transfer_auth"
Expand Down
23 changes: 13 additions & 10 deletions x/asset/priviledges/transfer_auth/query_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
proto "github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
assettypes "github.com/realiotech/realio-network/x/asset/types"
)

func (tp TransferAuthPriviledge) QueryWhitelistedAddresses(ctx sdk.Context, req *QueryWhitelistedAddressesRequest, tokenID string) (*QueryWhitelistedAddressesResponse, error) {
Expand All @@ -23,16 +24,18 @@ func (mp TransferAuthPriviledge) QueryIsAddressWhitelisted(ctx sdk.Context, req
}, nil
}

func (mp TransferAuthPriviledge) QueryHandler(context context.Context, req proto.Message, tokenID string) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)
func (mp TransferAuthPriviledge) QueryHandler() assettypes.QueryHandler {
return func(context context.Context, req proto.Message, tokenID string) (proto.Message, error) {
ctx := sdk.UnwrapSDKContext(context)

switch req := req.(type) {
case *QueryWhitelistedAddressesRequest:
return mp.QueryWhitelistedAddresses(ctx, req, tokenID)
case *QueryIsAddressWhitelistedRequest:
return mp.QueryIsAddressWhitelisted(ctx, req, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized query request type: %T for Transfer auth priviledge", req)
return nil, errors.Errorf(errMsg)
switch req := req.(type) {
case *QueryWhitelistedAddressesRequest:
return mp.QueryWhitelistedAddresses(ctx, req, tokenID)
case *QueryIsAddressWhitelistedRequest:
return mp.QueryIsAddressWhitelisted(ctx, req, tokenID)
default:
errMsg := fmt.Sprintf("unrecognized query request type: %T for Transfer auth priviledge", req)
return nil, errors.Errorf(errMsg)
}
}
}

0 comments on commit 8ffadb0

Please sign in to comment.