From 030db64f3df6ec3830c15fc8c0b763a0123a5858 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Mon, 24 Jun 2024 18:13:22 +0530 Subject: [PATCH] Use custom KV store gas config for all laconic txs --- utils/context.go | 29 +++++++++++++++++++++++++++ x/auction/keeper/msg_server.go | 10 ++++++++++ x/bond/keeper/keeper.go | 6 ++++++ x/bond/keeper/msg_server.go | 13 ++++++++++++ x/registry/keeper/msg_server.go | 35 +++++++++++++++++++++++++++++++++ 5 files changed, 93 insertions(+) create mode 100644 utils/context.go diff --git a/utils/context.go b/utils/context.go new file mode 100644 index 00000000..7a82bb6e --- /dev/null +++ b/utils/context.go @@ -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)) +} diff --git a/x/auction/keeper/msg_server.go b/x/auction/keeper/msg_server.go index 33004797..a7556353 100644 --- a/x/auction/keeper/msg_server.go +++ b/x/auction/keeper/msg_server.go @@ -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 } diff --git a/x/bond/keeper/keeper.go b/x/bond/keeper/keeper.go index 37145527..50cf345c 100644 --- a/x/bond/keeper/keeper.go +++ b/x/bond/keeper/keeper.go @@ -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") diff --git a/x/bond/keeper/msg_server.go b/x/bond/keeper/msg_server.go index 1a2ed5ae..2bba2d6b 100644 --- a/x/bond/keeper/msg_server.go +++ b/x/bond/keeper/msg_server.go @@ -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 } diff --git a/x/registry/keeper/msg_server.go b/x/registry/keeper/msg_server.go index 809a9061..ce5d800f 100644 --- a/x/registry/keeper/msg_server.go +++ b/x/registry/keeper/msg_server.go @@ -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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.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 ®istrytypes.MsgReassociateRecordsResponse{}, nil }