2022-04-05 07:09:27 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-13 12:03:42 +00:00
|
|
|
|
2022-04-05 07:09:27 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
"github.com/tharsis/ethermint/x/nameservice/types"
|
|
|
|
)
|
|
|
|
|
2022-04-13 12:03:42 +00:00
|
|
|
// BondIDAttributeName denotes the record bond ID.
|
|
|
|
const BondIDAttributeName = "bondId"
|
|
|
|
|
|
|
|
// ExpiryTimeAttributeName denotes the record expiry time.
|
|
|
|
const ExpiryTimeAttributeName = "expiryTime"
|
|
|
|
|
2022-04-05 07:09:27 +00:00
|
|
|
type Querier struct {
|
|
|
|
Keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ types.QueryServer = Querier{}
|
|
|
|
|
|
|
|
func (q Querier) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
params := q.Keeper.GetParams(ctx)
|
|
|
|
return &types.QueryParamsResponse{Params: ¶ms}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 12:03:42 +00:00
|
|
|
func (q Querier) ListRecords(c context.Context, req *types.QueryListRecordsRequest) (*types.QueryListRecordsResponse, error) {
|
2022-04-05 07:09:27 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2022-04-13 12:03:42 +00:00
|
|
|
attributes := req.GetAttributes()
|
2022-04-14 03:56:12 +00:00
|
|
|
all := req.GetAll()
|
2022-04-13 12:03:42 +00:00
|
|
|
records := []types.Record{}
|
|
|
|
|
|
|
|
if len(attributes) > 0 {
|
|
|
|
records = q.Keeper.MatchRecords(ctx, func(record *types.RecordType) bool {
|
2022-04-14 03:56:12 +00:00
|
|
|
return MatchOnAttributes(record, attributes, all)
|
2022-04-13 12:03:42 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
records = q.Keeper.ListRecords(ctx)
|
|
|
|
}
|
|
|
|
|
2022-04-05 07:09:27 +00:00
|
|
|
return &types.QueryListRecordsResponse{Records: records}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) GetRecord(c context.Context, req *types.QueryRecordByIdRequest) (*types.QueryRecordByIdResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
id := req.GetId()
|
|
|
|
if !q.Keeper.HasRecord(ctx, id) {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "Record not found.")
|
|
|
|
}
|
|
|
|
record := q.Keeper.GetRecord(ctx, id)
|
|
|
|
return &types.QueryRecordByIdResponse{Record: record}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) GetRecordByBondId(c context.Context, req *types.QueryRecordByBondIdRequest) (*types.QueryRecordByBondIdResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
records := q.recordKeeper.QueryRecordsByBond(ctx, req.GetId())
|
|
|
|
return &types.QueryRecordByBondIdResponse{Records: records}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) GetNameServiceModuleBalance(c context.Context, _ *types.GetNameServiceModuleBalanceRequest) (*types.GetNameServiceModuleBalanceResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
balances := q.Keeper.GetModuleBalances(ctx)
|
|
|
|
return &types.GetNameServiceModuleBalanceResponse{
|
|
|
|
Balances: balances,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) ListNameRecords(c context.Context, _ *types.QueryListNameRecordsRequest) (*types.QueryListNameRecordsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
nameRecords := q.Keeper.ListNameRecords(ctx)
|
|
|
|
return &types.QueryListNameRecordsResponse{Names: nameRecords}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) Whois(c context.Context, request *types.QueryWhoisRequest) (*types.QueryWhoisResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
nameAuthority := q.Keeper.GetNameAuthority(ctx, request.GetName())
|
|
|
|
return &types.QueryWhoisResponse{NameAuthority: nameAuthority}, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 07:37:38 +00:00
|
|
|
func (q Querier) LookupCrn(c context.Context, req *types.QueryLookupCrn) (*types.QueryLookupCrnResponse, error) {
|
2022-04-05 07:09:27 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2022-04-20 07:37:38 +00:00
|
|
|
crn := req.GetCrn()
|
|
|
|
if !q.Keeper.HasNameRecord(ctx, crn) {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "CRN not found.")
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
2022-04-20 07:37:38 +00:00
|
|
|
nameRecord := q.Keeper.GetNameRecord(ctx, crn)
|
2022-04-05 07:09:27 +00:00
|
|
|
if nameRecord == nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "name record not found.")
|
|
|
|
}
|
2022-04-20 07:37:38 +00:00
|
|
|
return &types.QueryLookupCrnResponse{Name: nameRecord}, nil
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 07:37:38 +00:00
|
|
|
func (q Querier) ResolveCrn(c context.Context, req *types.QueryResolveCrn) (*types.QueryResolveCrnResponse, error) {
|
2022-04-05 07:09:27 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2022-04-20 07:37:38 +00:00
|
|
|
crn := req.GetCrn()
|
|
|
|
record := q.Keeper.ResolveCRN(ctx, crn)
|
2022-04-05 07:09:27 +00:00
|
|
|
if record == nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "record not found.")
|
|
|
|
}
|
2022-04-20 07:37:38 +00:00
|
|
|
return &types.QueryResolveCrnResponse{Record: record}, nil
|
2022-04-05 07:09:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) GetRecordExpiryQueue(c context.Context, _ *types.QueryGetRecordExpiryQueue) (*types.QueryGetRecordExpiryQueueResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
records := q.Keeper.GetRecordExpiryQueue(ctx)
|
|
|
|
return &types.QueryGetRecordExpiryQueueResponse{Records: records}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q Querier) GetAuthorityExpiryQueue(c context.Context, _ *types.QueryGetAuthorityExpiryQueue) (*types.QueryGetAuthorityExpiryQueueResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
authorities := q.Keeper.GetAuthorityExpiryQueue(ctx)
|
|
|
|
return &types.QueryGetAuthorityExpiryQueueResponse{Authorities: authorities}, nil
|
|
|
|
}
|
2022-04-13 12:03:42 +00:00
|
|
|
|
|
|
|
func matchOnRecordField(record *types.RecordType, attr *types.QueryListRecordsRequest_KeyValueInput) (fieldFound bool, matched bool) {
|
|
|
|
fieldFound = false
|
|
|
|
matched = true
|
|
|
|
|
|
|
|
switch attr.Key {
|
|
|
|
case BondIDAttributeName:
|
|
|
|
{
|
|
|
|
fieldFound = true
|
|
|
|
if record.BondId != attr.Value.GetString_() {
|
|
|
|
matched = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ExpiryTimeAttributeName:
|
|
|
|
{
|
|
|
|
fieldFound = true
|
|
|
|
if record.ExpiryTime != attr.Value.GetString_() {
|
|
|
|
matched = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:56:12 +00:00
|
|
|
func MatchOnAttributes(record *types.RecordType, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) bool {
|
2022-04-13 12:03:42 +00:00
|
|
|
// Filter deleted records.
|
|
|
|
if record.Deleted {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:56:12 +00:00
|
|
|
// If ONLY named records are requested, check for that condition first.
|
|
|
|
if !all && len(record.Names) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-13 12:03:42 +00:00
|
|
|
recAttrs := record.Attributes
|
|
|
|
|
|
|
|
for _, attr := range attributes {
|
|
|
|
// First try matching on record struct fields.
|
|
|
|
fieldFound, matched := matchOnRecordField(record, attr)
|
|
|
|
|
|
|
|
if fieldFound {
|
|
|
|
if !matched {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
recAttrVal, recAttrFound := recAttrs[attr.Key]
|
|
|
|
if !recAttrFound {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:34:52 +00:00
|
|
|
if attr.Value.Type == "int" {
|
2022-04-13 12:03:42 +00:00
|
|
|
recAttrValInt, ok := recAttrVal.(int)
|
|
|
|
if !ok || int(attr.Value.GetInt()) != recAttrValInt {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:34:52 +00:00
|
|
|
if attr.Value.Type == "float" {
|
|
|
|
recAttrValFloat, ok := recAttrVal.(float64)
|
|
|
|
if !ok || attr.Value.GetFloat() != recAttrValFloat {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.Value.Type == "string" {
|
2022-04-13 12:03:42 +00:00
|
|
|
recAttrValString, ok := recAttrVal.(string)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.Value.GetString_() != recAttrValString {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 12:34:52 +00:00
|
|
|
if attr.Value.Type == "boolean" {
|
|
|
|
recAttrValBool, ok := recAttrVal.(bool)
|
|
|
|
if !ok || attr.Value.GetBoolean() != recAttrValBool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr.Value.Type == "reference" {
|
|
|
|
obj, ok := recAttrVal.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
// Attr value is not an object.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := obj["/"].(string); !ok {
|
|
|
|
// Attr value is not a reference.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
recAttrValRefID := obj["/"].(string)
|
|
|
|
if recAttrValRefID != attr.Value.GetReference().GetId() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Handle arrays.
|
2022-04-13 12:03:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|