Log gas consumed for laconic txs

This commit is contained in:
Prathamesh Musale 2024-04-03 14:10:05 +05:30
parent ea30697b50
commit 25c438aa37
5 changed files with 73 additions and 0 deletions

View File

@ -15,6 +15,7 @@ import (
auth "github.com/cosmos/cosmos-sdk/x/auth/keeper" auth "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper" bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
params "github.com/cosmos/cosmos-sdk/x/params/types" params "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
wnsUtils "github.com/cerc-io/laconicd/utils" wnsUtils "github.com/cerc-io/laconicd/utils"
) )
@ -78,6 +79,11 @@ func NewKeeper(accountKeeper auth.AccountKeeper,
} }
} }
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", types.ModuleName)
}
func (k *Keeper) SetUsageKeepers(usageKeepers []types.AuctionUsageKeeper) { func (k *Keeper) SetUsageKeepers(usageKeepers []types.AuctionUsageKeeper) {
k.usageKeepers = usageKeepers k.usageKeepers = usageKeepers
} }

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
"context" "context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -48,6 +49,8 @@ func (s msgServer) CreateAuction(c context.Context, msg *types.MsgCreateAuction)
), ),
}) })
s.logTxGasConsumed(ctx, "CreateAuction")
return &types.MsgCreateAuctionResponse{Auction: resp}, nil return &types.MsgCreateAuctionResponse{Auction: resp}, nil
} }
@ -80,6 +83,8 @@ func (s msgServer) CommitBid(c context.Context, msg *types.MsgCommitBid) (*types
), ),
}) })
s.logTxGasConsumed(ctx, "CommitBid")
return &types.MsgCommitBidResponse{Bid: resp}, nil return &types.MsgCommitBidResponse{Bid: resp}, nil
} }
@ -112,5 +117,12 @@ func (s msgServer) RevealBid(c context.Context, msg *types.MsgRevealBid) (*types
), ),
}) })
s.logTxGasConsumed(ctx, "RevealBid")
return &types.MsgRevealBidResponse{Auction: resp}, nil return &types.MsgRevealBidResponse{Auction: resp}, nil
} }
func (m msgServer) logTxGasConsumed(ctx sdk.Context, tx string) {
gasConsumed := ctx.GasMeter().GasConsumed()
m.Keeper.Logger(ctx).Info("tx executed", "method", tx, "gas_consumed", fmt.Sprintf("%d", gasConsumed))
}

View File

@ -14,6 +14,7 @@ import (
auth "github.com/cosmos/cosmos-sdk/x/auth/keeper" auth "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper" bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types" paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
) )
// prefixIDToBondIndex is the prefix for ID -> Bond index in the KVStore. // prefixIDToBondIndex is the prefix for ID -> Bond index in the KVStore.
@ -62,6 +63,11 @@ func NewKeeper(cdc codec.BinaryCodec,
} }
} }
// Logger returns a module-specific logger.
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", types.ModuleName)
}
// Generates Bond ID -> Bond index key. // Generates Bond ID -> Bond index key.
func getBondIndexKey(id string) []byte { func getBondIndexKey(id string) []byte {
return append(prefixIDToBondIndex, []byte(id)...) return append(prefixIDToBondIndex, []byte(id)...)

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
"context" "context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -46,6 +47,8 @@ func (k msgServer) CreateBond(c context.Context, msg *types.MsgCreateBond) (*typ
), ),
}) })
k.logTxGasConsumed(ctx, "CreateBond")
return &types.MsgCreateBondResponse{}, nil return &types.MsgCreateBondResponse{}, nil
} }
@ -78,6 +81,8 @@ func (k msgServer) RefillBond(c context.Context, msg *types.MsgRefillBond) (*typ
), ),
}) })
k.logTxGasConsumed(ctx, "RefillBond")
return &types.MsgRefillBondResponse{}, nil return &types.MsgRefillBondResponse{}, nil
} }
@ -110,6 +115,8 @@ func (k msgServer) WithdrawBond(c context.Context, msg *types.MsgWithdrawBond) (
), ),
}) })
k.logTxGasConsumed(ctx, "WithdrawBond")
return &types.MsgWithdrawBondResponse{}, nil return &types.MsgWithdrawBondResponse{}, nil
} }
@ -139,5 +146,12 @@ func (k msgServer) CancelBond(c context.Context, msg *types.MsgCancelBond) (*typ
), ),
}) })
k.logTxGasConsumed(ctx, "CancelBond")
return &types.MsgCancelBondResponse{}, nil return &types.MsgCancelBondResponse{}, nil
} }
func (m msgServer) logTxGasConsumed(ctx sdk.Context, tx string) {
gasConsumed := ctx.GasMeter().GasConsumed()
m.Keeper.Logger(ctx).Info("tx executed", "method", tx, "gas_consumed", fmt.Sprintf("%d", gasConsumed))
}

View File

@ -2,6 +2,7 @@ package keeper
import ( import (
"context" "context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -48,6 +49,8 @@ func (m msgServer) SetRecord(c context.Context, msg *types.MsgSetRecord) (*types
), ),
}) })
m.logTxGasConsumed(ctx, "SetRecord")
return &types.MsgSetRecordResponse{Id: record.ID}, nil return &types.MsgSetRecordResponse{Id: record.ID}, nil
} }
@ -77,6 +80,9 @@ func (m msgServer) SetName(c context.Context, msg *types.MsgSetName) (*types.Msg
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "SetName")
return &types.MsgSetNameResponse{}, nil return &types.MsgSetNameResponse{}, nil
} }
@ -109,6 +115,9 @@ func (m msgServer) ReserveName(c context.Context, msg *types.MsgReserveAuthority
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "ReserveName")
return &types.MsgReserveAuthorityResponse{}, nil return &types.MsgReserveAuthorityResponse{}, nil
} }
@ -138,6 +147,9 @@ func (m msgServer) SetAuthorityBond(c context.Context, msg *types.MsgSetAuthorit
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "SetAuthorityBond")
return &types.MsgSetAuthorityBondResponse{}, nil return &types.MsgSetAuthorityBondResponse{}, nil
} }
@ -165,6 +177,9 @@ func (m msgServer) DeleteName(c context.Context, msg *types.MsgDeleteNameAuthori
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "DeleteName")
return &types.MsgDeleteNameAuthorityResponse{}, nil return &types.MsgDeleteNameAuthorityResponse{}, nil
} }
@ -192,6 +207,9 @@ func (m msgServer) RenewRecord(c context.Context, msg *types.MsgRenewRecord) (*t
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "RenewRecord")
return &types.MsgRenewRecordResponse{}, nil return &types.MsgRenewRecordResponse{}, nil
} }
@ -222,6 +240,9 @@ func (m msgServer) AssociateBond(c context.Context, msg *types.MsgAssociateBond)
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "AssociateBond")
return &types.MsgAssociateBondResponse{}, nil return &types.MsgAssociateBondResponse{}, nil
} }
@ -249,6 +270,9 @@ func (m msgServer) DissociateBond(c context.Context, msg *types.MsgDissociateBon
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "DissociateBond")
return &types.MsgDissociateBondResponse{}, nil return &types.MsgDissociateBondResponse{}, nil
} }
@ -276,6 +300,9 @@ func (m msgServer) DissociateRecords(c context.Context, msg *types.MsgDissociate
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "DissociateRecords")
return &types.MsgDissociateRecordsResponse{}, nil return &types.MsgDissociateRecordsResponse{}, nil
} }
@ -304,5 +331,13 @@ func (m msgServer) ReAssociateRecords(c context.Context, msg *types.MsgReAssocia
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer), sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
), ),
}) })
m.logTxGasConsumed(ctx, "ReAssociateRecords")
return &types.MsgReAssociateRecordsResponse{}, nil return &types.MsgReAssociateRecordsResponse{}, nil
} }
func (m msgServer) logTxGasConsumed(ctx sdk.Context, tx string) {
gasConsumed := ctx.GasMeter().GasConsumed()
m.Keeper.Logger(ctx).Info("tx executed", "method", tx, "gas_consumed", fmt.Sprintf("%d", gasConsumed))
}