Skip to content

Commit

Permalink
Recursively Search Authz Msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsmith-2019 committed Nov 10, 2023
1 parent 8eda7ad commit a35606b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
globalfeeante.NewFeeDecorator(options.BypassMinFeeMsgTypes, options.GlobalFeeKeeper, options.StakingKeeper, maxBypassMinFeeMsgGasUsage),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
feeshareante.NewFeeSharePayoutDecorator(options.BankKeeperFork, options.FeeShareKeeper, options.Cdc),
feeshareante.NewFeeSharePayoutDecorator(options.BankKeeperFork, options.FeeShareKeeper),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
Expand Down
79 changes: 37 additions & 42 deletions x/feeshare/ante/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/cosmos/cosmos-sdk/codec"

errorsmod "cosmossdk.io/errors"

Expand All @@ -21,14 +20,12 @@ import (
type FeeSharePayoutDecorator struct {
bankKeeper BankKeeper
feesharekeeper FeeShareKeeper
cdc codec.BinaryCodec
}

func NewFeeSharePayoutDecorator(bk BankKeeper, fs FeeShareKeeper, cdc codec.BinaryCodec) FeeSharePayoutDecorator {
func NewFeeSharePayoutDecorator(bk BankKeeper, fs FeeShareKeeper) FeeSharePayoutDecorator {
return FeeSharePayoutDecorator{
bankKeeper: bk,
feesharekeeper: fs,
cdc: cdc,
}
}

Expand Down Expand Up @@ -66,19 +63,42 @@ type FeeSharePayoutEventOutput struct {
FeesPaid sdk.Coins `json:"fees_paid"`
}

func addNewFeeSharePayoutsForMsg(ctx sdk.Context, fsk FeeShareKeeper, toPay *[]sdk.AccAddress, m sdk.Msg) error {
if msg, ok := m.(*wasmtypes.MsgExecuteContract); ok {
contractAddr, err := sdk.AccAddressFromBech32(msg.Contract)
if err != nil {
return err
// Loop through all messages and add the withdraw address to the list of addresses to pay
// if the contract opted-in to fee sharing
func addNewFeeSharePayoutsForMsgs(ctx sdk.Context, fsk FeeShareKeeper, toPay *[]sdk.AccAddress, msgs []sdk.Msg) error {
for _, msg := range msgs {

// Check if an authz message, loop through all inner messages, and recursively call this function
if authzMsg, ok := msg.(*authz.MsgExec); ok {

innerMsgs, err := authzMsg.GetMessages()
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
}

// Recursively call this function with the inner messages
err = addNewFeeSharePayoutsForMsgs(ctx, fsk, toPay, innerMsgs)
if err != nil {
return err
}
}

shareData, _ := fsk.GetFeeShare(ctx, contractAddr)
// If an execute contract message, check if the contract opted-in to fee sharing,
// and if so, add the withdraw address to the list of addresses to pay
if execContractMsg, ok := msg.(*wasmtypes.MsgExecuteContract); ok {
contractAddr, err := sdk.AccAddressFromBech32(execContractMsg.Contract)
if err != nil {
return err
}

shareData, _ := fsk.GetFeeShare(ctx, contractAddr)

withdrawAddr := shareData.GetWithdrawerAddr()
if withdrawAddr != nil && !withdrawAddr.Empty() {
*toPay = append(*toPay, withdrawAddr)
withdrawAddr := shareData.GetWithdrawerAddr()
if withdrawAddr != nil && !withdrawAddr.Empty() {
*toPay = append(*toPay, withdrawAddr)
}
}

}

return nil
Expand All @@ -95,35 +115,10 @@ func (fsd FeeSharePayoutDecorator) FeeSharePayout(ctx sdk.Context, bankKeeper Ba
// Get valid withdraw addresses from contracts
toPay := make([]sdk.AccAddress, 0)

// validAuthz checks if the msg is an authz exec msg and if so, call the validExecuteMsg for each
// inner msg. If it is a CosmWasm execute message, that logic runs for nested functions.
validAuthz := func(cdc codec.BinaryCodec, execMsg *authz.MsgExec) error {
for _, v := range execMsg.Msgs {
var innerMsg sdk.Msg
if err := cdc.UnpackAny(v, &innerMsg); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrUnauthorized, "cannot unmarshal authz exec msgs")
}

err := addNewFeeSharePayoutsForMsg(ctx, fsk, &toPay, innerMsg)
if err != nil {
return err
}
}

return nil
}

for _, m := range msgs {
if msg, ok := m.(*authz.MsgExec); ok {
if err := validAuthz(fsd.cdc, msg); err != nil {
return err
}
continue
}

if err := addNewFeeSharePayoutsForMsg(ctx, fsk, &toPay, m); err != nil {
return err
}
// Add fee share payouts for each msg
err := addNewFeeSharePayoutsForMsgs(ctx, fsk, &toPay, msgs)
if err != nil {
return err
}

// Do nothing if no one needs payment
Expand Down

0 comments on commit a35606b

Please sign in to comment.