Add a command to renew expired records

This commit is contained in:
Prathamesh Musale 2024-02-23 14:32:14 +05:30
parent 56a95dd83a
commit 033375f6f7
3 changed files with 42 additions and 4 deletions

View File

@ -169,14 +169,17 @@ func (ms msgServer) DeleteName(c context.Context, msg *registrytypes.MsgDeleteNa
func (ms msgServer) RenewRecord(c context.Context, msg *registrytypes.MsgRenewRecord) (*registrytypes.MsgRenewRecordResponse, error) { func (ms msgServer) RenewRecord(c context.Context, msg *registrytypes.MsgRenewRecord) (*registrytypes.MsgRenewRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(c) ctx := sdk.UnwrapSDKContext(c)
_, err := sdk.AccAddressFromBech32(msg.Signer) _, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = ms.k.ProcessRenewRecord(ctx, *msg)
err = ms.k.RenewRecord(ctx, *msg)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ctx.EventManager().EmitEvents(sdk.Events{ ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent( sdk.NewEvent(
registrytypes.EventTypeRenewRecord, registrytypes.EventTypeRenewRecord,

View File

@ -1,8 +1,12 @@
package keeper package keeper
import ( import (
"time"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
auctionkeeper "git.vdb.to/cerc-io/laconic2d/x/auction/keeper" auctionkeeper "git.vdb.to/cerc-io/laconic2d/x/auction/keeper"
registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry" registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry"
@ -17,9 +21,32 @@ type RecordKeeper struct {
// storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context // storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context
} }
// ProcessRenewRecord renews a record. // RenewRecord renews a record.
func (k Keeper) ProcessRenewRecord(ctx sdk.Context, msg registrytypes.MsgRenewRecord) error { func (k Keeper) RenewRecord(ctx sdk.Context, msg registrytypes.MsgRenewRecord) error {
panic("unimplemented") if has, err := k.HasRecord(ctx, msg.RecordId); !has {
if err != nil {
return err
}
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Record not found.")
}
// Check if renewal is required (i.e. expired record marked as deleted).
record, err := k.GetRecordById(ctx, msg.RecordId)
if err != nil {
return err
}
expiryTime, err := time.Parse(time.RFC3339, record.ExpiryTime)
if err != nil {
return err
}
if !record.Deleted || expiryTime.After(ctx.BlockTime()) {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "Renewal not required.")
}
readableRecord := record.ToReadableRecord()
return k.processRecord(ctx, &readableRecord, true)
} }
// ProcessAssociateBond associates a record with a bond. // ProcessAssociateBond associates a record with a bond.

View File

@ -84,6 +84,14 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Tx: &autocliv1.ServiceCommandDescriptor{ Tx: &autocliv1.ServiceCommandDescriptor{
Service: registryv1.Msg_ServiceDesc.ServiceName, Service: registryv1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{ RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "RenewRecord",
Use: "renew-record [record-id]",
Short: "Renew (expired) record",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "record_id"},
},
},
{ {
RpcMethod: "ReserveName", RpcMethod: "ReserveName",
Use: "reserve-name [name] [owner]", Use: "reserve-name [name] [owner]",