laconicd-deprecated/x/registry/keeper/msg_server.go
Prathamesh Musale 322d6f188b
All checks were successful
Lint / Run flake8 on python integration tests (push) Successful in 2m4s
Lint / Run golangci-lint (push) Successful in 4m58s
Tests / test-rpc (push) Successful in 5m12s
Tests / test-importer (push) Successful in 18m51s
Tests / sdk_tests (push) Successful in 9m48s
Tests / test-unit (push) Successful in 6m51s
Fix progressively increasing gas usage (#146)
Part of https://www.notion.so/Set-constant-gas-price-in-laconicd-00b470964f0e4020b06153f0f2c9bcfb

- Use custom KV store gas config (0 gas for KV store ops) for laconic module (auction, bond, registry) txs

Reviewed-on: cerc-io/laconicd#146
Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
2024-04-03 12:14:49 +00:00

341 lines
9.6 KiB
Go

package keeper
import (
"context"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cerc-io/laconicd/utils"
"github.com/cerc-io/laconicd/x/registry/types"
)
type msgServer struct {
Keeper
}
// NewMsgServerImpl returns an implementation of the bond MsgServer interface for the provided Keeper.
func NewMsgServerImpl(keeper Keeper) types.MsgServer {
return &msgServer{Keeper: keeper}
}
var _ types.MsgServer = msgServer{}
func (m msgServer) SetRecord(c context.Context, msg *types.MsgSetRecord) (*types.MsgSetRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
record, err := m.Keeper.ProcessSetRecord(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSetRecord,
sdk.NewAttribute(types.AttributeKeySigner, msg.GetSigner()),
sdk.NewAttribute(types.AttributeKeyBondID, msg.GetBondId()),
sdk.NewAttribute(types.AttributeKeyPayload, msg.Payload.Record.Id),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "SetRecord")
return &types.MsgSetRecordResponse{Id: record.ID}, nil
}
func (m msgServer) SetName(c context.Context, msg *types.MsgSetName) (*types.MsgSetNameResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessSetName(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSetRecord,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyCRN, msg.Crn),
sdk.NewAttribute(types.AttributeKeyCID, msg.Cid),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "SetName")
return &types.MsgSetNameResponse{}, nil
}
func (m msgServer) ReserveName(c context.Context, msg *types.MsgReserveAuthority) (*types.MsgReserveAuthorityResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
_, err = sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessReserveAuthority(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeReserveNameAuthority,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyName, msg.Name),
sdk.NewAttribute(types.AttributeKeyOwner, msg.Owner),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "ReserveName")
return &types.MsgReserveAuthorityResponse{}, nil
}
func (m msgServer) SetAuthorityBond(c context.Context, msg *types.MsgSetAuthorityBond) (*types.MsgSetAuthorityBondResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessSetAuthorityBond(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAuthorityBond,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyName, msg.Name),
sdk.NewAttribute(types.AttributeKeyBondID, msg.BondId),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "SetAuthorityBond")
return &types.MsgSetAuthorityBondResponse{}, nil
}
func (m msgServer) DeleteName(c context.Context, msg *types.MsgDeleteNameAuthority) (*types.MsgDeleteNameAuthorityResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessDeleteName(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDeleteName,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyCRN, msg.Crn),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "DeleteName")
return &types.MsgDeleteNameAuthorityResponse{}, nil
}
func (m msgServer) RenewRecord(c context.Context, msg *types.MsgRenewRecord) (*types.MsgRenewRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessRenewRecord(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeRenewRecord,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyRecordID, msg.RecordId),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "RenewRecord")
return &types.MsgRenewRecordResponse{}, nil
}
func (m msgServer) AssociateBond(c context.Context, msg *types.MsgAssociateBond) (*types.MsgAssociateBondResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessAssociateBond(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeAssociateBond,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyRecordID, msg.RecordId),
sdk.NewAttribute(types.AttributeKeyBondID, msg.BondId),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "AssociateBond")
return &types.MsgAssociateBondResponse{}, nil
}
func (m msgServer) DissociateBond(c context.Context, msg *types.MsgDissociateBond) (*types.MsgDissociateBondResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessDissociateBond(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDissociateBond,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyRecordID, msg.RecordId),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "DissociateBond")
return &types.MsgDissociateBondResponse{}, nil
}
func (m msgServer) DissociateRecords(c context.Context, msg *types.MsgDissociateRecords) (*types.MsgDissociateRecordsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessDissociateRecords(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeDissociateRecords,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyBondID, msg.BondId),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "DissociateRecords")
return &types.MsgDissociateRecordsResponse{}, nil
}
func (m msgServer) ReAssociateRecords(c context.Context, msg *types.MsgReAssociateRecords) (*types.MsgReAssociateRecordsResponse, error) { //nolint: all
ctx := sdk.UnwrapSDKContext(c)
ctx = *utils.CtxWithCustomKVGasConfig(&ctx)
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
err = m.Keeper.ProcessReAssociateRecords(ctx, *msg)
if err != nil {
return nil, err
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeReAssociateRecords,
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
sdk.NewAttribute(types.AttributeKeyOldBondID, msg.OldBondId),
sdk.NewAttribute(types.AttributeKeyNewBondID, msg.NewBondId),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(types.AttributeKeySigner, msg.Signer),
),
})
m.logTxGasConsumed(ctx, "ReAssociateRecords")
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))
}