Add a command to resolve name to a record

This commit is contained in:
Prathamesh Musale 2024-02-20 10:11:01 +05:30
parent c809f22b83
commit 22f6e05de0
3 changed files with 68 additions and 7 deletions

View File

@ -100,13 +100,13 @@ func (k Keeper) ListNameRecords(ctx sdk.Context) ([]registrytypes.NameEntry, err
// SaveNameRecord - sets a name record.
func (k Keeper) SaveNameRecord(ctx sdk.Context, crn string, id string) error {
var nameRecord registrytypes.NameRecord
x, err := k.GetNameRecord(ctx, crn)
existingNameRecord, err := k.GetNameRecord(ctx, crn)
if err != nil {
return err
}
if x != nil {
nameRecord = *x
if existingNameRecord != nil {
nameRecord = *existingNameRecord
nameRecord.History = append(nameRecord.History, nameRecord.Latest)
}
@ -339,8 +339,51 @@ func (k Keeper) GetAuthorityExpiryQueue(ctx sdk.Context) []*registrytypes.Expiry
}
// ResolveCRN resolves a CRN to a record.
func (k Keeper) ResolveCRN(ctx sdk.Context, crn string) *registrytypes.Record {
panic("unimplemented")
func (k Keeper) ResolveCRN(ctx sdk.Context, crn string) (*registrytypes.Record, error) {
_, _, authority, err := k.getAuthority(ctx, crn)
if err != nil || authority.Status != registrytypes.AuthorityActive {
// If authority is not active (or any other error), resolution fails.
return nil, err
}
// Name should not resolve if it's stale.
// i.e. authority was registered later than the name.
record, nameRecord, err := k.resolveCRNRecord(ctx, crn)
if err != nil {
return nil, err
}
if authority.Height > nameRecord.Latest.Height {
return nil, nil
}
return record, nil
}
func (k Keeper) resolveCRNRecord(ctx sdk.Context, crn string) (*registrytypes.Record, *registrytypes.NameRecord, error) {
nameRecord, err := k.GetNameRecord(ctx, crn)
if nameRecord == nil {
return nil, nil, err
}
latestRecordId := nameRecord.Latest.Id
if latestRecordId == "" {
return nil, nameRecord, nil
}
if has, err := k.HasRecord(ctx, latestRecordId); !has {
if err != nil {
return nil, nil, err
}
return nil, nameRecord, nil
}
record, err := k.GetRecordById(ctx, latestRecordId)
if err != nil {
return nil, nil, err
}
return &record, nameRecord, nil
}
func (k Keeper) getAuthority(ctx sdk.Context, crn string) (string, *url.URL, *registrytypes.NameAuthority, error) {

View File

@ -132,8 +132,12 @@ func (qs queryServer) LookupCrn(c context.Context, req *registrytypes.QueryLooku
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "CRN not found.")
}
nameRecord, _ := qs.k.LookupNameRecord(ctx, crn)
nameRecord, err := qs.k.LookupNameRecord(ctx, crn)
if nameRecord == nil {
if err != nil {
return nil, err
}
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
}
@ -142,11 +146,17 @@ func (qs queryServer) LookupCrn(c context.Context, req *registrytypes.QueryLooku
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)
record, err := qs.k.ResolveCRN(ctx, crn)
if record == nil {
if err != nil {
return nil, err
}
return nil, errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
}
return &registrytypes.QueryResolveCrnResponse{Record: record}, nil
}

View File

@ -65,6 +65,14 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
{ProtoField: "crn"},
},
},
{
RpcMethod: "ResolveCrn",
Use: "resolve [crn]",
Short: "Resolve CRN to record",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "crn"},
},
},
{
RpcMethod: "GetRegistryModuleBalance",
Use: "balance",