Skip to content

Commit

Permalink
hasInvalidMsgFromPoAAdmin for TF + new Payout
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Feb 23, 2024
1 parent db17851 commit fe9b7d8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 33 deletions.
31 changes: 19 additions & 12 deletions app/decorators/inflation_disable_minting.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

manifestkeeper "github.com/liftedinit/manifest-ledger/x/manifest/keeper"
manifesttypes "github.com/liftedinit/manifest-ledger/x/manifest/types"
tokenfactorytypes "github.com/reecepbcups/tokenfactory/x/tokenfactory/types"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -24,28 +25,34 @@ func NewMsgManualMintFilterDecorator(mk *manifestkeeper.Keeper, isSudoAdminFunc
}

func (mfd MsgManualMintFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
// iterate all messages, see if any are a tokenfactory message from a sudo admin.
// if there is and inflation is current >0%, return an error
if err := mfd.hasInvalidTokenFactoryMsg(ctx, tx.GetMsgs()); err != nil {
if err := mfd.hasInvalidMsgFromPoAAdmin(ctx, tx.GetMsgs()); err != nil {
return ctx, err
}

return next(ctx, tx, simulate)
}

func (mfd MsgManualMintFilterDecorator) hasInvalidTokenFactoryMsg(ctx sdk.Context, msgs []sdk.Msg) error {
func (mfd MsgManualMintFilterDecorator) hasInvalidMsgFromPoAAdmin(ctx sdk.Context, msgs []sdk.Msg) error {
for _, msg := range msgs {
// only payout stakeholders manually if inflation is 0% & the sender is the admin.
if m, ok := msg.(*manifesttypes.MsgPayoutStakeholders); ok {
return mfd.senderAdminOnMintWithInflation(ctx, m.Authority)
}

// if the sender is not the admin, continue as normal
// if they are the admin, check if inflation is 0%. if it is, allow. Else, error.
if m, ok := msg.(*tokenfactorytypes.MsgMint); ok {
if mfd.isSudoAdminFunc(ctx, m.Sender) {
isInflationEnabled := mfd.mk.IsManualMintingEnabled(ctx)
if isInflationEnabled != nil {
return isInflationEnabled
}
}

return nil
return mfd.senderAdminOnMintWithInflation(ctx, m.Sender)
}
}

return nil
}

func (mfd MsgManualMintFilterDecorator) senderAdminOnMintWithInflation(ctx context.Context, sender string) error {
if mfd.isSudoAdminFunc(ctx, sender) {
return mfd.mk.IsManualMintingEnabled(ctx)
}

return nil
}
80 changes: 59 additions & 21 deletions app/decorators/inflation_disable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/liftedinit/manifest-ledger/app/decorators"
appparams "github.com/liftedinit/manifest-ledger/app/params"
manifestkeeper "github.com/liftedinit/manifest-ledger/x/manifest/keeper"
manifesttypes "github.com/liftedinit/manifest-ledger/x/manifest/types"
tokenfactorytypes "github.com/reecepbcups/tokenfactory/x/tokenfactory/types"
poa "github.com/strangelove-ventures/poa"
poakeeper "github.com/strangelove-ventures/poa/keeper"
Expand All @@ -26,6 +27,9 @@ var (
EmptyAnte = func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
return ctx, nil
}

coin = sdk.NewCoin(appparams.BondDenom, sdkmath.NewInt(1))
tfCoin = sdk.NewCoin("factory", sdkmath.NewInt(1))
)

type AnteTestSuite struct {
Expand Down Expand Up @@ -72,25 +76,59 @@ func (s *AnteTestSuite) TestAnteInflationAndMinting() {
inflation := sdkmath.LegacyNewDecWithPrec(1, 2) // 1%
zero := sdkmath.LegacyZeroDec()

// tx: inflation is 0 so manual minting is allowed from the poa admin
s.Require().NoError(s.mintKeeper.Minter.Set(s.ctx, minttypes.InitialMinter(zero)))
msg := tokenfactorytypes.NewMsgMint(poaAdmin.String(), sdk.NewCoin(appparams.BondDenom, sdkmath.NewInt(1)))
_, err := ante.AnteHandle(s.ctx, decorators.NewMockTx(msg), false, EmptyAnte)
s.Require().NoError(err)

// minting is allowed from the stdUser too
msg = tokenfactorytypes.NewMsgMint(stdUser.String(), sdk.NewCoin(appparams.BondDenom, sdkmath.NewInt(1)))
_, err = ante.AnteHandle(s.ctx, decorators.NewMockTx(msg), false, EmptyAnte)
s.Require().NoError(err)

// tx: inflation is 1% so manual minting is not allowed from the poa admin
s.Require().NoError(s.mintKeeper.Minter.Set(s.ctx, minttypes.InitialMinter(inflation)))
msg = tokenfactorytypes.NewMsgMint(poaAdmin.String(), sdk.NewCoin(appparams.BondDenom, sdkmath.NewInt(1)))
_, err = ante.AnteHandle(s.ctx, decorators.NewMockTx(msg), false, EmptyAnte)
s.Require().Contains(err.Error(), manifestkeeper.ErrManualMintingDisabled.Error())

// tx: inflation is still 1%, but normal users can still mint (non admins)
msg = tokenfactorytypes.NewMsgMint(stdUser.String(), sdk.NewCoin(appparams.BondDenom, sdkmath.NewInt(1)))
_, err = ante.AnteHandle(s.ctx, decorators.NewMockTx(msg), false, EmptyAnte)
s.Require().NoError(err)
type tc struct {
name string
inflation sdkmath.LegacyDec
msg sdk.Msg
err string
}

tcs := []tc{
{
name: "success; 0 inflation tokenfactory mint",
inflation: zero,
msg: tokenfactorytypes.NewMsgMint(poaAdmin.String(), coin),
},
{
name: "success; 0 inflation payout stakeholders",
inflation: zero,
msg: manifesttypes.NewMsgPayoutStakeholders(poaAdmin, coin),
},
{
name: "success; TF mint from standard user",
inflation: zero,
msg: tokenfactorytypes.NewMsgMint(stdUser.String(), tfCoin),
},
{
name: "success; TF mint from standard user with inflation still allowed",
inflation: inflation,
msg: tokenfactorytypes.NewMsgMint(stdUser.String(), tfCoin),
},
{
name: "fail; inflation enabled, no manual mint from admin",
inflation: inflation,
msg: tokenfactorytypes.NewMsgMint(poaAdmin.String(), coin),
err: manifestkeeper.ErrManualMintingDisabled.Error(),
},
{
name: "fail; inflation enabled, no manual payout from admin",
inflation: inflation,
msg: manifesttypes.NewMsgPayoutStakeholders(poaAdmin, coin),
err: manifestkeeper.ErrManualMintingDisabled.Error(),
},
}

for _, tc := range tcs {
tc := tc

s.Require().NoError(s.mintKeeper.Minter.Set(s.ctx, minttypes.InitialMinter(tc.inflation)))
_, err := ante.AnteHandle(s.ctx, decorators.NewMockTx(tc.msg), false, EmptyAnte)

if tc.err == "" {
s.Require().NoError(err)
} else {
s.Require().Error(err)
s.Require().Contains(err.Error(), tc.err)
}
}
}

0 comments on commit fe9b7d8

Please sign in to comment.