Skip to content

Commit

Permalink
feat(x/swingset): Read beansPerUnit in each message handler and pass …
Browse files Browse the repository at this point in the history
…down to helpers

This minimizes gas consumption vs. helpers re-reading parameters,
making it less likely for an unestimated transaction to run out.
  • Loading branch information
gibson042 committed Nov 9, 2024
1 parent b9b1903 commit 55b9b49
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
4 changes: 2 additions & 2 deletions golang/cosmos/ante/inbound_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ func (msk mockSwingsetKeeper) GetBeansPerUnit(ctx sdk.Context) map[string]sdkmat
return nil
}

func (msk mockSwingsetKeeper) ChargeBeans(ctx sdk.Context, addr sdk.AccAddress, beans sdkmath.Uint) error {
func (msk mockSwingsetKeeper) ChargeBeans(ctx sdk.Context, beansPerUnit map[string]sdkmath.Uint, addr sdk.AccAddress, beans sdkmath.Uint) error {
return fmt.Errorf("not implemented")
}

func (msk mockSwingsetKeeper) GetSmartWalletState(ctx sdk.Context, addr sdk.AccAddress) swingtypes.SmartWalletState {
panic(fmt.Errorf("not implemented"))
}

func (msk mockSwingsetKeeper) ChargeForSmartWallet(ctx sdk.Context, addr sdk.AccAddress) error {
func (msk mockSwingsetKeeper) ChargeForSmartWallet(ctx sdk.Context, beansPerUnit map[string]sdkmath.Uint, addr sdk.AccAddress) error {
return fmt.Errorf("not implemented")
}
18 changes: 12 additions & 6 deletions golang/cosmos/x/swingset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,12 @@ func (k Keeper) SetBeansOwing(ctx sdk.Context, addr sdk.AccAddress, beans sdkmat
// ChargeBeans charges the given address the given number of beans. It divides
// the beans into the number to debit immediately vs. the number to store in the
// beansOwing.
func (k Keeper) ChargeBeans(ctx sdk.Context, addr sdk.AccAddress, beans sdkmath.Uint) error {
beansPerUnit := k.GetBeansPerUnit(ctx)

func (k Keeper) ChargeBeans(
ctx sdk.Context,
beansPerUnit map[string]sdkmath.Uint,
addr sdk.AccAddress,
beans sdkmath.Uint,
) error {
wasOwing := k.GetBeansOwing(ctx, addr)
nowOwing := wasOwing.Add(beans)

Expand Down Expand Up @@ -342,10 +345,13 @@ func (k Keeper) ChargeBeans(ctx sdk.Context, addr sdk.AccAddress, beans sdkmath.
}

// ChargeForSmartWallet charges the fee for provisioning a smart wallet.
func (k Keeper) ChargeForSmartWallet(ctx sdk.Context, addr sdk.AccAddress) error {
beansPerUnit := k.GetBeansPerUnit(ctx)
func (k Keeper) ChargeForSmartWallet(
ctx sdk.Context,
beansPerUnit map[string]sdkmath.Uint,
addr sdk.AccAddress,
) error {
beans := beansPerUnit[types.BeansPerSmartWalletProvision]
err := k.ChargeBeans(ctx, addr, beans)
err := k.ChargeBeans(ctx, beansPerUnit, addr, beans)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions golang/cosmos/x/swingset/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type AccountKeeper interface {

type SwingSetKeeper interface {
GetBeansPerUnit(ctx sdk.Context) map[string]sdkmath.Uint
ChargeBeans(ctx sdk.Context, addr sdk.AccAddress, beans sdkmath.Uint) error
ChargeBeans(ctx sdk.Context, beansPerUnit map[string]sdkmath.Uint, addr sdk.AccAddress, beans sdkmath.Uint) error
IsHighPriorityAddress(ctx sdk.Context, addr sdk.AccAddress) (bool, error)
GetSmartWalletState(ctx sdk.Context, addr sdk.AccAddress) SmartWalletState
ChargeForSmartWallet(ctx sdk.Context, addr sdk.AccAddress) error
ChargeForSmartWallet(ctx sdk.Context, beansPerUnit map[string]sdkmath.Uint, addr sdk.AccAddress) error
}
42 changes: 30 additions & 12 deletions golang/cosmos/x/swingset/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"strings"

sdkioerrors "cosmossdk.io/errors"
"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/Agoric/agoric-sdk/golang/cosmos/vm"
)

const RouterKey = ModuleName // this was defined in your key.go file
Expand Down Expand Up @@ -56,16 +59,22 @@ const (

// Charge an account address for the beans associated with given messages and storage.
// See list of bean charges in default-params.go
func chargeAdmission(ctx sdk.Context, keeper SwingSetKeeper, addr sdk.AccAddress, msgs []string, storageLen uint64) error {
beansPerUnit := keeper.GetBeansPerUnit(ctx)
func chargeAdmission(
ctx sdk.Context,
keeper SwingSetKeeper,
beansPerUnit map[string]sdkmath.Uint,
addr sdk.AccAddress,
msgs []string,
storageLen uint64,
) error {
beans := beansPerUnit[BeansPerInboundTx]
beans = beans.Add(beansPerUnit[BeansPerMessage].MulUint64((uint64(len(msgs)))))
for _, msg := range msgs {
beans = beans.Add(beansPerUnit[BeansPerMessageByte].MulUint64(uint64(len(msg))))
}
beans = beans.Add(beansPerUnit[BeansPerStorageByte].MulUint64(storageLen))

return keeper.ChargeBeans(ctx, addr, beans)
return keeper.ChargeBeans(ctx, beansPerUnit, addr, beans)
}

// checkSmartWalletProvisioned verifies if a smart wallet message (MsgWalletAction
Expand All @@ -74,7 +83,12 @@ func chargeAdmission(ctx sdk.Context, keeper SwingSetKeeper, addr sdk.AccAddress
// provisioning fee is charged successfully.
// All messages for non-provisioned smart wallets allowed here will result in
// an auto-provision action generated by the msg server.
func checkSmartWalletProvisioned(ctx sdk.Context, keeper SwingSetKeeper, addr sdk.AccAddress) error {
func checkSmartWalletProvisioned(
ctx sdk.Context,
keeper SwingSetKeeper,
beansPerUnit map[string]sdkmath.Uint,
addr sdk.AccAddress,
) error {
walletState := keeper.GetSmartWalletState(ctx, addr)

switch walletState {
Expand All @@ -91,7 +105,7 @@ func checkSmartWalletProvisioned(ctx sdk.Context, keeper SwingSetKeeper, addr sd
// This is a separate charge from the smart wallet action which triggered the check
// TODO: Currently this call does not mark the smart wallet provisioning as
// pending, resulting in multiple provisioning charges for the owner.
return keeper.ChargeForSmartWallet(ctx, addr)
return keeper.ChargeForSmartWallet(ctx, beansPerUnit, addr)
}
}

Expand All @@ -118,7 +132,8 @@ func (msg MsgDeliverInbound) CheckAdmissibility(ctx sdk.Context, data interface{
}
*/

return chargeAdmission(ctx, keeper, msg.Submitter, msg.Messages, 0)
beansPerUnit := keeper.GetBeansPerUnit(ctx)
return chargeAdmission(ctx, keeper, beansPerUnit, msg.Submitter, msg.Messages, 0)
}

// GetInboundMsgCount implements InboundMsgCarrier.
Expand Down Expand Up @@ -184,12 +199,13 @@ func (msg MsgWalletAction) CheckAdmissibility(ctx sdk.Context, data interface{})
return sdkioerrors.Wrapf(sdkerrors.ErrInvalidRequest, "data must be a SwingSetKeeper, not a %T", data)
}

err := checkSmartWalletProvisioned(ctx, keeper, msg.Owner)
beansPerUnit := keeper.GetBeansPerUnit(ctx)
err := checkSmartWalletProvisioned(ctx, keeper, beansPerUnit, msg.Owner)
if err != nil {
return err
}

return chargeAdmission(ctx, keeper, msg.Owner, []string{msg.Action}, 0)
return chargeAdmission(ctx, keeper, beansPerUnit, msg.Owner, []string{msg.Action}, 0)
}

// GetInboundMsgCount implements InboundMsgCarrier.
Expand Down Expand Up @@ -256,12 +272,13 @@ func (msg MsgWalletSpendAction) CheckAdmissibility(ctx sdk.Context, data interfa
return sdkioerrors.Wrapf(sdkerrors.ErrInvalidRequest, "data must be a SwingSetKeeper, not a %T", data)
}

err := checkSmartWalletProvisioned(ctx, keeper, msg.Owner)
beansPerUnit := keeper.GetBeansPerUnit(ctx)
err := checkSmartWalletProvisioned(ctx, keeper, beansPerUnit, msg.Owner)
if err != nil {
return err
}

return chargeAdmission(ctx, keeper, msg.Owner, []string{msg.SpendAction}, 0)
return chargeAdmission(ctx, keeper, beansPerUnit, msg.Owner, []string{msg.SpendAction}, 0)
}

// GetInboundMsgCount implements InboundMsgCarrier.
Expand Down Expand Up @@ -373,7 +390,8 @@ func (msg MsgInstallBundle) CheckAdmissibility(ctx sdk.Context, data interface{}
if !ok {
return sdkioerrors.Wrapf(sdkerrors.ErrInvalidRequest, "data must be a SwingSetKeeper, not a %T", data)
}
return chargeAdmission(ctx, keeper, msg.Submitter, []string{msg.Bundle}, msg.ExpectedUncompressedSize())
beansPerUnit := keeper.GetBeansPerUnit(ctx)
return chargeAdmission(ctx, keeper, beansPerUnit, msg.Submitter, []string{msg.Bundle}, msg.ExpectedUncompressedSize())
}

// GetInboundMsgCount implements InboundMsgCarrier.
Expand Down

0 comments on commit 55b9b49

Please sign in to comment.