Add a query to list authorities #42

Merged
nabarun merged 4 commits from pm-list-authorities into main 2024-07-24 09:14:40 +00:00
4 changed files with 39 additions and 18 deletions
Showing only changes of commit be3bcf9687 - Show all commits

View File

@ -67,20 +67,11 @@ func (k *Keeper) ExportGenesis(ctx sdk.Context) (*registry.GenesisState, error)
return nil, err
}
authorities, err := k.ListNameAuthorityRecords(ctx)
authorityEntries, err := k.ListNameAuthorityRecords(ctx, "")
if err != nil {
return nil, err
}
authorityEntries := []registry.AuthorityEntry{}
// #nosec G705
for name, record := range authorities {
authorityEntries = append(authorityEntries, registry.AuthorityEntry{
Name: name,
Entry: &record, //nolint: all
}) // #nosec G601
}
names, err := k.ListNameRecords(ctx)
if err != nil {
return nil, err

View File

@ -41,19 +41,30 @@ func (k Keeper) GetNameAuthority(ctx sdk.Context, name string) (registrytypes.Na
return authority, nil
}
// ListNameAuthorityRecords - get all name authority records.
func (k Keeper) ListNameAuthorityRecords(ctx sdk.Context) (map[string]registrytypes.NameAuthority, error) {
nameAuthorityRecords := make(map[string]registrytypes.NameAuthority)
// ListNameAuthorityRecords - get all name authority records for given owner
// Returns all authorities if owner set to ""
func (k Keeper) ListNameAuthorityRecords(ctx sdk.Context, owner string) ([]registrytypes.AuthorityEntry, error) {
authorityEntries := []registrytypes.AuthorityEntry{}
err := k.Authorities.Walk(ctx, nil, func(key string, value registrytypes.NameAuthority) (bool, error) {
nameAuthorityRecords[key] = value
// If owner is not empty, skip if authority is not owned by owner
if owner != "" && owner != value.OwnerAddress {
return false, nil
}
authorityEntries = append(authorityEntries, registrytypes.AuthorityEntry{
Name: key,
Entry: &value,
})
// Continue the walk
return false, nil
})
if err != nil {
return map[string]registrytypes.NameAuthority{}, err
return []registrytypes.AuthorityEntry{}, err
}
return nameAuthorityRecords, nil
return authorityEntries, nil
}
// HasNameRecord - checks if a name record exists.

View File

@ -112,10 +112,10 @@ func (qs queryServer) NameRecords(c context.Context, _ *registrytypes.QueryNameR
return &registrytypes.QueryNameRecordsResponse{Names: nameRecords}, nil
}
func (qs queryServer) Whois(c context.Context, request *registrytypes.QueryWhoisRequest) (*registrytypes.QueryWhoisResponse, error) {
func (qs queryServer) Whois(c context.Context, req *registrytypes.QueryWhoisRequest) (*registrytypes.QueryWhoisResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
nameAuthority, err := qs.k.GetNameAuthority(ctx, request.GetName())
nameAuthority, err := qs.k.GetNameAuthority(ctx, req.GetName())
if err != nil {
return nil, err
}
@ -123,6 +123,17 @@ func (qs queryServer) Whois(c context.Context, request *registrytypes.QueryWhois
return &registrytypes.QueryWhoisResponse{NameAuthority: nameAuthority}, nil
}
func (qs queryServer) Authorities(c context.Context, req *registrytypes.QueryAuthoritiesRequest) (*registrytypes.QueryAuthoritiesResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
authorityEntries, err := qs.k.ListNameAuthorityRecords(ctx, req.GetOwner())
if err != nil {
return nil, err
}
return &registrytypes.QueryAuthoritiesResponse{Authorities: authorityEntries}, nil
}
func (qs queryServer) LookupLrn(c context.Context, req *registrytypes.QueryLookupLrnRequest) (*registrytypes.QueryLookupLrnResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
lrn := req.GetLrn()

View File

@ -51,6 +51,14 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
{ProtoField: "name"},
},
},
{
RpcMethod: "Authorities",
Use: "list-authorities [owner]",
Short: "List authorities (optionally by owner)",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "owner", Optional: true},
},
},
{
RpcMethod: "NameRecords",
Use: "names",