Use custom KV store config to handle progressively increasing gas usage (#30)

Part of [Create an external fixturenet-laconicd stack](https://www.notion.so/Create-an-external-fixturenet-laconicd-stack-3b33cc2317444f4da209c4472f4244ed)

Reviewed-on: cerc-io/laconic2d#30
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
This commit is contained in:
Prathamesh Musale 2024-06-25 06:33:41 +00:00 committed by ashwin
parent 66df959d51
commit e63f51cacd
5 changed files with 93 additions and 0 deletions

29
utils/context.go Normal file
View File

@ -0,0 +1,29 @@
package utils
import (
"fmt"
"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func CtxWithCustomKVGasConfig(ctx *sdk.Context) *sdk.Context {
updatedCtx := ctx.WithKVGasConfig(storetypes.GasConfig{
HasCost: 0,
DeleteCost: 0,
ReadCostFlat: 0,
ReadCostPerByte: 0,
WriteCostFlat: 0,
WriteCostPerByte: 0,
IterNextCostFlat: 0,
})
return &updatedCtx
}
func LogTxGasConsumed(ctx sdk.Context, logger log.Logger, tx string) {
gasConsumed := ctx.GasMeter().GasConsumed()
logger.Info("tx executed", "method", tx, "gas_consumed", fmt.Sprintf("%d", gasConsumed))
}

View File

@ -3,6 +3,7 @@ package keeper
import (
"context"
"git.vdb.to/cerc-io/laconicd/utils"
auctiontypes "git.vdb.to/cerc-io/laconicd/x/auction"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@ -20,6 +21,7 @@ func NewMsgServerImpl(keeper *Keeper) auctiontypes.MsgServer {
func (ms msgServer) CreateAuction(c context.Context, msg *auctiontypes.MsgCreateAuction) (*auctiontypes.MsgCreateAuctionResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -46,6 +48,8 @@ func (ms msgServer) CreateAuction(c context.Context, msg *auctiontypes.MsgCreate
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "CreateAuction")
return &auctiontypes.MsgCreateAuctionResponse{Auction: resp}, nil
}
@ -57,6 +61,7 @@ func (ms msgServer) CommitBid(c context.Context, msg *auctiontypes.MsgCommitBid)
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -81,6 +86,8 @@ func (ms msgServer) CommitBid(c context.Context, msg *auctiontypes.MsgCommitBid)
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "CommitBid")
return &auctiontypes.MsgCommitBidResponse{Bid: resp}, nil
}
@ -92,6 +99,7 @@ func (ms msgServer) RevealBid(c context.Context, msg *auctiontypes.MsgRevealBid)
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -116,5 +124,7 @@ func (ms msgServer) RevealBid(c context.Context, msg *auctiontypes.MsgRevealBid)
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "RevealBid")
return &auctiontypes.MsgRevealBidResponse{Auction: resp}, nil
}

View File

@ -10,6 +10,7 @@ import (
"cosmossdk.io/collections/indexes"
"cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -84,6 +85,11 @@ func NewKeeper(
return &k
}
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", bondtypes.ModuleName)
}
func (k *Keeper) SetUsageKeepers(usageKeepers []bondtypes.BondUsageKeeper) {
if k.usageKeepers != nil {
panic("cannot set bond hooks twice")

View File

@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"git.vdb.to/cerc-io/laconicd/utils"
"git.vdb.to/cerc-io/laconicd/x/bond"
)
@ -25,6 +26,7 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -49,6 +51,8 @@ func (ms msgServer) CreateBond(c context.Context, msg *bond.MsgCreateBond) (*bon
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "CreateBond")
return &bond.MsgCreateBondResponse{Id: resp.Id}, nil
}
@ -59,6 +63,7 @@ func (ms msgServer) RefillBond(c context.Context, msg *bond.MsgRefillBond) (*bon
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -84,6 +89,8 @@ func (ms msgServer) RefillBond(c context.Context, msg *bond.MsgRefillBond) (*bon
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "RefillBond")
return &bond.MsgRefillBondResponse{}, nil
}
@ -94,6 +101,7 @@ func (ms msgServer) WithdrawBond(c context.Context, msg *bond.MsgWithdrawBond) (
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -119,6 +127,8 @@ func (ms msgServer) WithdrawBond(c context.Context, msg *bond.MsgWithdrawBond) (
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "WithdrawBond")
return &bond.MsgWithdrawBondResponse{}, nil
}
@ -129,6 +139,7 @@ func (ms msgServer) CancelBond(c context.Context, msg *bond.MsgCancelBond) (*bon
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
signerAddress, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -153,5 +164,7 @@ func (ms msgServer) CancelBond(c context.Context, msg *bond.MsgCancelBond) (*bon
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "CancelBond")
return &bond.MsgCancelBondResponse{}, nil
}

View File

@ -5,6 +5,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"git.vdb.to/cerc-io/laconicd/utils"
registrytypes "git.vdb.to/cerc-io/laconicd/x/registry"
)
@ -25,6 +26,7 @@ func (ms msgServer) SetRecord(c context.Context, msg *registrytypes.MsgSetRecord
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -50,6 +52,8 @@ func (ms msgServer) SetRecord(c context.Context, msg *registrytypes.MsgSetRecord
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "SetRecord")
return &registrytypes.MsgSetRecordResponse{Id: record.Id}, nil
}
@ -60,6 +64,7 @@ func (ms msgServer) SetName(c context.Context, msg *registrytypes.MsgSetName) (*
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -84,6 +89,9 @@ func (ms msgServer) SetName(c context.Context, msg *registrytypes.MsgSetName) (*
sdk.NewAttribute(registrytypes.AttributeKeySigner, msg.Signer),
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "SetName")
return &registrytypes.MsgSetNameResponse{}, nil
}
@ -93,6 +101,7 @@ func (ms msgServer) ReserveAuthority(c context.Context, msg *registrytypes.MsgRe
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -121,6 +130,9 @@ func (ms msgServer) ReserveAuthority(c context.Context, msg *registrytypes.MsgRe
sdk.NewAttribute(registrytypes.AttributeKeySigner, msg.Signer),
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "ReserveAuthority")
return &registrytypes.MsgReserveAuthorityResponse{}, nil
}
@ -131,6 +143,7 @@ func (ms msgServer) SetAuthorityBond(c context.Context, msg *registrytypes.MsgSe
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -156,6 +169,8 @@ func (ms msgServer) SetAuthorityBond(c context.Context, msg *registrytypes.MsgSe
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "SetAuthorityBond")
return &registrytypes.MsgSetAuthorityBondResponse{}, nil
}
@ -165,6 +180,7 @@ func (ms msgServer) DeleteName(c context.Context, msg *registrytypes.MsgDeleteNa
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -188,6 +204,9 @@ func (ms msgServer) DeleteName(c context.Context, msg *registrytypes.MsgDeleteNa
sdk.NewAttribute(registrytypes.AttributeKeySigner, msg.Signer),
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "DeleteName")
return &registrytypes.MsgDeleteNameResponse{}, nil
}
@ -197,6 +216,7 @@ func (ms msgServer) RenewRecord(c context.Context, msg *registrytypes.MsgRenewRe
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -220,6 +240,9 @@ func (ms msgServer) RenewRecord(c context.Context, msg *registrytypes.MsgRenewRe
sdk.NewAttribute(registrytypes.AttributeKeySigner, msg.Signer),
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "RenewRecord")
return &registrytypes.MsgRenewRecordResponse{}, nil
}
@ -230,6 +253,7 @@ func (ms msgServer) AssociateBond(c context.Context, msg *registrytypes.MsgAssoc
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -255,6 +279,8 @@ func (ms msgServer) AssociateBond(c context.Context, msg *registrytypes.MsgAssoc
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "AssociateBond")
return &registrytypes.MsgAssociateBondResponse{}, nil
}
@ -264,6 +290,7 @@ func (ms msgServer) DissociateBond(c context.Context, msg *registrytypes.MsgDiss
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -288,6 +315,8 @@ func (ms msgServer) DissociateBond(c context.Context, msg *registrytypes.MsgDiss
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "DissociateBond")
return &registrytypes.MsgDissociateBondResponse{}, nil
}
@ -297,6 +326,7 @@ func (ms msgServer) DissociateRecords(c context.Context, msg *registrytypes.MsgD
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -321,6 +351,8 @@ func (ms msgServer) DissociateRecords(c context.Context, msg *registrytypes.MsgD
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "DissociateRecords")
return &registrytypes.MsgDissociateRecordsResponse{}, nil
}
@ -330,6 +362,7 @@ func (ms msgServer) ReassociateRecords(c context.Context, msg *registrytypes.Msg
}
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
@ -355,5 +388,7 @@ func (ms msgServer) ReassociateRecords(c context.Context, msg *registrytypes.Msg
),
})
utils.LogTxGasConsumed(ctx, ms.k.Logger(ctx), "ReassociateRecords")
return &registrytypes.MsgReassociateRecordsResponse{}, nil
}