Add a command to list records

This commit is contained in:
Prathamesh Musale 2024-02-15 19:30:39 +05:30
parent 538010f377
commit 3d478568ac
7 changed files with 192 additions and 13 deletions

View File

@ -101,6 +101,33 @@ func (k Keeper) HasRecord(ctx sdk.Context, id string) (bool, error) {
return has, nil
}
// GetRecord - gets a record from the store.
func (k Keeper) GetRecord(ctx sdk.Context, id string) (record registrytypes.Record) {
panic("unimplemented")
}
// ListRecords - get all records.
func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) {
iter, err := k.Records.Iterate(ctx, nil)
if err != nil {
return nil, err
}
// TODO: Check if required
// decodeRecordNames(store, &record)
return iter.Values()
}
// RecordsFromAttributes gets a list of records whose attributes match all provided values
func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*registrytypes.QueryRecordsRequest_KeyValueInput, all bool) ([]registrytypes.Record, error) {
panic("unimplemented")
}
func (k Keeper) GetRecordExpiryQueue(ctx sdk.Context) []*registrytypes.ExpiryQueueRecord {
panic("unimplemented")
}
// PutRecord - saves a record to the store.
func (k Keeper) SaveRecord(ctx sdk.Context, record registrytypes.Record) error {
return k.Records.Set(ctx, record.Id, record)
@ -254,3 +281,8 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre
}
return nil
}
// GetModuleBalances gets the registry module account(s) balances.
func (k Keeper) GetModuleBalances(ctx sdk.Context) []*registrytypes.AccountBalance {
panic("unimplemented")
}

View File

@ -6,6 +6,26 @@ import (
registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry"
)
// GetNameAuthority - gets a name authority from the store.
func (k Keeper) GetNameAuthority(ctx sdk.Context, name string) registrytypes.NameAuthority {
panic("unimplemented")
}
// HasNameRecord - checks if a name record exists.
func (k Keeper) HasNameRecord(ctx sdk.Context, crn string) bool {
panic("unimplemented")
}
// GetNameRecord - gets a name record from the store.
func (k Keeper) GetNameRecord(ctx sdk.Context, crn string) *registrytypes.NameRecord {
panic("unimplemented")
}
// ListNameRecords - get all name records.
func (k Keeper) ListNameRecords(ctx sdk.Context) []registrytypes.NameEntry {
panic("unimplemented")
}
// ProcessSetName creates a CRN -> Record ID mapping.
func (k Keeper) ProcessSetName(ctx sdk.Context, msg registrytypes.MsgSetName) error {
panic("unimplemented")
@ -24,3 +44,12 @@ func (k Keeper) ProcessSetAuthorityBond(ctx sdk.Context, msg registrytypes.MsgSe
func (k Keeper) ProcessDeleteName(ctx sdk.Context, msg registrytypes.MsgDeleteNameAuthority) error {
panic("unimplemented")
}
func (k Keeper) GetAuthorityExpiryQueue(ctx sdk.Context) []*registrytypes.ExpiryQueueRecord {
panic("unimplemented")
}
// ResolveCRN resolves a CRN to a record.
func (k Keeper) ResolveCRN(ctx sdk.Context, crn string) *registrytypes.Record {
panic("unimplemented")
}

View File

@ -14,7 +14,3 @@ func (k Keeper) GetParams(ctx sdk.Context) (*registrytypes.Params, error) {
return &params, nil
}
// SetParams - set the params.
func (k Keeper) SetParams(ctx sdk.Context, params registrytypes.Params) {
}

View File

@ -3,21 +3,23 @@ package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
registrytypes "git.vdb.to/cerc-io/laconic2d/x/registry"
)
// var _ registrytypes.QueryServer = queryServer{}
var _ registrytypes.QueryServer = queryServer{}
type queryServer struct {
k Keeper
}
// // NewQueryServerImpl returns an implementation of the module QueryServer.
// func NewQueryServerImpl(k Keeper) registrytypes.QueryServer {
// return queryServer{k}
// }
// NewQueryServerImpl returns an implementation of the module QueryServer.
func NewQueryServerImpl(k Keeper) registrytypes.QueryServer {
return queryServer{k}
}
func (qs queryServer) Params(c context.Context, _ *registrytypes.QueryParamsRequest) (*registrytypes.QueryParamsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
@ -30,4 +32,106 @@ func (qs queryServer) Params(c context.Context, _ *registrytypes.QueryParamsRequ
return &registrytypes.QueryParamsResponse{Params: params}, nil
}
// TODO: Add remaining methods
func (qs queryServer) Records(c context.Context, req *registrytypes.QueryRecordsRequest) (*registrytypes.QueryRecordsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
attributes := req.GetAttributes()
all := req.GetAll()
var records []registrytypes.Record
var err error
if len(attributes) > 0 {
records, err = qs.k.RecordsFromAttributes(ctx, attributes, all)
if err != nil {
return nil, err
}
} else {
records, err = qs.k.ListRecords(ctx)
if err != nil {
return nil, err
}
}
return &registrytypes.QueryRecordsResponse{Records: records}, nil
}
func (qs queryServer) GetRecord(c context.Context, req *registrytypes.QueryRecordByIdRequest) (*registrytypes.QueryRecordByIdResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
id := req.GetId()
if has, err := qs.k.HasRecord(ctx, req.Id); !has {
if err != nil {
return nil, err
}
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.")
}
record := qs.k.GetRecord(ctx, id)
return &registrytypes.QueryRecordByIdResponse{Record: record}, nil
}
func (qs queryServer) GetRecordsByBondId(c context.Context, req *registrytypes.QueryRecordsByBondIdRequest) (*registrytypes.QueryRecordsByBondIdResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
records := qs.k.recordKeeper.QueryRecordsByBond(ctx, req.GetId())
return &registrytypes.QueryRecordsByBondIdResponse{Records: records}, nil
}
func (qs queryServer) GetRegistryModuleBalance(c context.Context,
_ *registrytypes.QueryGetRegistryModuleBalanceRequest,
) (*registrytypes.QueryGetRegistryModuleBalanceResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
balances := qs.k.GetModuleBalances(ctx)
return &registrytypes.QueryGetRegistryModuleBalanceResponse{
Balances: balances,
}, nil
}
func (qs queryServer) NameRecords(c context.Context, _ *registrytypes.QueryNameRecordsRequest) (*registrytypes.QueryNameRecordsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
nameRecords := qs.k.ListNameRecords(ctx)
return &registrytypes.QueryNameRecordsResponse{Names: nameRecords}, nil
}
func (qs queryServer) Whois(c context.Context, request *registrytypes.QueryWhoisRequest) (*registrytypes.QueryWhoisResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
nameAuthority := qs.k.GetNameAuthority(ctx, request.GetName())
return &registrytypes.QueryWhoisResponse{NameAuthority: nameAuthority}, nil
}
func (qs queryServer) LookupCrn(c context.Context, req *registrytypes.QueryLookupCrnRequest) (*registrytypes.QueryLookupCrnResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
crn := req.GetCrn()
if !qs.k.HasNameRecord(ctx, crn) {
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "CRN not found.")
}
nameRecord := qs.k.GetNameRecord(ctx, crn)
if nameRecord == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
}
return &registrytypes.QueryLookupCrnResponse{Name: nameRecord}, nil
}
func (qs queryServer) ResolveCrn(c context.Context, req *registrytypes.QueryResolveCrnRequest) (*registrytypes.QueryResolveCrnResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
crn := req.GetCrn()
record := qs.k.ResolveCRN(ctx, crn)
if record == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
}
return &registrytypes.QueryResolveCrnResponse{Record: record}, nil
}
func (qs queryServer) GetRecordExpiryQueue(c context.Context, _ *registrytypes.QueryGetRecordExpiryQueueRequest) (*registrytypes.QueryGetRecordExpiryQueueResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
records := qs.k.GetRecordExpiryQueue(ctx)
return &registrytypes.QueryGetRecordExpiryQueueResponse{Records: records}, nil
}
func (qs queryServer) GetAuthorityExpiryQueue(c context.Context,
_ *registrytypes.QueryGetAuthorityExpiryQueueRequest,
) (*registrytypes.QueryGetAuthorityExpiryQueueResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
authorities := qs.k.GetAuthorityExpiryQueue(ctx)
return &registrytypes.QueryGetAuthorityExpiryQueueResponse{Authorities: authorities}, nil
}

View File

@ -17,6 +17,11 @@ type RecordKeeper struct {
// storeKey storetypes.StoreKey // Unexposed key to access store from sdk.Context
}
// QueryRecordsByBond - get all records for the given bond.
func (k RecordKeeper) QueryRecordsByBond(ctx sdk.Context, bondID string) []registrytypes.Record {
panic("unimplemented")
}
// ProcessRenewRecord renews a record.
func (k Keeper) ProcessRenewRecord(ctx sdk.Context, msg registrytypes.MsgRenewRecord) error {
panic("unimplemented")

View File

@ -14,7 +14,20 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
Service: registryv1.Query_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{},
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Params",
Use: "params",
Short: "Get the current registry parameters",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{},
},
{
RpcMethod: "Records",
Use: "list",
Short: "List records",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{},
},
},
},
Tx: &autocliv1.ServiceCommandDescriptor{
Service: registryv1.Msg_ServiceDesc.ServiceName,

View File

@ -118,7 +118,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
func (am AppModule) RegisterServices(cfg module.Configurator) {
// Register servers
registrytypes.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
// registrytypes.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
registrytypes.RegisterQueryServer(cfg.QueryServer(), keeper.NewQueryServerImpl(am.keeper))
}
// appmodule.HasEndBlocker