Skip to content

Commit

Permalink
RNS Primary Names (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Sep 11, 2024
2 parents 02299dc + a963e0c commit 8ca23e1
Show file tree
Hide file tree
Showing 21 changed files with 1,799 additions and 190 deletions.
10 changes: 10 additions & 0 deletions proto/canine_chain/rns/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ service Query {
rpc AllInits(QueryAllInits) returns (QueryAllInitsResponse) {
option (google.api.http).get = "/jackal/canine-chain/rns/init";
}

// Queries a list of Init items.
rpc PrimaryName(QueryPrimaryName) returns (QueryPrimaryNameResponse) {
option (google.api.http).get = "/jackal/canine-chain/rns/primary/{owner}";
}
}

message QueryParams {}
Expand All @@ -78,6 +83,11 @@ message QueryName { string name = 1; }

message QueryNameResponse { Names name = 1 [ (gogoproto.nullable) = false ]; } // TODO: Check if this breaks everything to change it to Name

message QueryPrimaryName { string owner = 1; }

message QueryPrimaryNameResponse { Names name = 1 [ (gogoproto.nullable) = false ]; }


message QueryListOwnedNames {
string address = 1;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
Expand Down
20 changes: 20 additions & 0 deletions proto/canine_chain/rns/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ option go_package = "github.com/jackalLabs/canine-chain/x/rns/types";

service Msg {
rpc Register(MsgRegister) returns (MsgRegisterResponse);
rpc RegisterName(MsgRegisterName) returns (MsgRegisterNameResponse);
rpc Bid(MsgBid) returns (MsgBidResponse);
rpc AcceptBid(MsgAcceptBid) returns (MsgAcceptBidResponse);
rpc CancelBid(MsgCancelBid) returns (MsgCancelBidResponse);
Expand All @@ -19,8 +20,10 @@ service Msg {
rpc DelRecord(MsgDelRecord) returns (MsgDelRecordResponse);
rpc Init(MsgInit) returns (MsgInitResponse);
rpc Update(MsgUpdate) returns (MsgUpdateResponse);
rpc MakePrimary(MsgMakePrimary) returns (MsgMakePrimaryResponse);
}

// Deprecated! Use MsgRegisterName instead
message MsgRegister {
string creator = 1;
string name = 2;
Expand All @@ -30,6 +33,16 @@ message MsgRegister {

message MsgRegisterResponse {}

message MsgRegisterName {
string creator = 1;
string name = 2;
int64 years = 3;
string data = 4;
bool setPrimary = 5;
}

message MsgRegisterNameResponse {}

message MsgUpdate {
string creator = 1;
string name = 2;
Expand All @@ -38,6 +51,13 @@ message MsgUpdate {

message MsgUpdateResponse {}

message MsgMakePrimary {
string creator = 1;
string name = 2;
}

message MsgMakePrimaryResponse {}

message MsgBid {
string creator = 1;
string name = 2;
Expand Down
8 changes: 7 additions & 1 deletion x/rns/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
ctx = ctx.WithEventManager(sdk.NewEventManager())

switch msg := msg.(type) {
case *types.MsgRegister: //nolint:typecheck
case *types.MsgRegister:
res, err := msgServer.Register(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgRegisterName:
res, err := msgServer.RegisterName(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgBid:
res, err := msgServer.Bid(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
Expand Down Expand Up @@ -53,6 +56,9 @@ func NewHandler(k keeper.Keeper) sdk.Handler {
case *types.MsgUpdate:
res, err := msgServer.Update(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
case *types.MsgMakePrimary:
res, err := msgServer.MakePrimary(sdk.WrapSDKContext(ctx), msg)
return sdk.WrapServiceResult(ctx, res, err)
default:
errMsg := fmt.Sprintf("unrecognized %s message type: %T", types.ModuleName, msg)
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, errMsg)
Expand Down
2 changes: 1 addition & 1 deletion x/rns/keeper/bidding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (suite *KeeperTestSuite) TestMsgAcceptBid() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, nameAddress, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, nameAddress.String(), TestName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, nameAddress.String(), TestName, "{}", 2, true)
suite.Require().NoError(err)

bidderBalBefore := suite.bankKeeper.GetAllBalances(suite.ctx, address)
Expand Down
14 changes: 14 additions & 0 deletions x/rns/keeper/grpc_query_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,17 @@ func (k Keeper) Name(c context.Context, req *types.QueryName) (*types.QueryNameR

return &types.QueryNameResponse{Name: val}, nil
}

func (k Keeper) PrimaryName(c context.Context, req *types.QueryPrimaryName) (*types.QueryPrimaryNameResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)

n, found := k.GetPrimaryName(ctx, req.Owner)
if !found {
return nil, status.Error(codes.NotFound, "user does not have a primary name set")
}

return &types.QueryPrimaryNameResponse{Name: n}, nil
}
6 changes: 3 additions & 3 deletions x/rns/keeper/msg_server_basics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (suite *KeeperTestSuite) TestMsgTrasnfer() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, owner, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, owner.String(), successfulName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, owner.String(), successfulName, "{}", 2, true)
suite.Require().NoError(err)

cases := []struct {
Expand Down Expand Up @@ -218,7 +218,7 @@ func (suite *KeeperTestSuite) TestMsgTrasnfer() {
preRun: func() *types.MsgTransfer {
freeName := "freeBi.jkl"
blockHeight := suite.ctx.BlockHeight()
err := suite.rnsKeeper.RegisterName(suite.ctx, owner.String(), freeName, "{}", 2)
err := suite.rnsKeeper.RegisterRNSName(suite.ctx, owner.String(), freeName, "{}", 2, true)
suite.Require().NoError(err)
name, _ := suite.rnsKeeper.GetNames(suite.ctx, "freeBi", "jkl")
name.Locked = blockHeight + 1
Expand Down Expand Up @@ -276,7 +276,7 @@ func (suite *KeeperTestSuite) TestMsgUpdate() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, owner, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, owner.String(), successfulName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, owner.String(), successfulName, "{}", 2, true)
suite.Require().NoError(err)

const testdata = "{\"test\":\"test\"}"
Expand Down
8 changes: 4 additions & 4 deletions x/rns/keeper/msg_server_bidding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (suite *KeeperTestSuite) TestMsgAddBid() {
suite.Require().NoError(err)

suite.rnsKeeper.SetInit(suite.ctx, types.Init{Address: nameOwner.String(), Complete: true})
err = suite.rnsKeeper.RegisterName(suite.ctx, nameOwner.String(), nuggieName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, nameOwner.String(), nuggieName, "{}", 2, true)
suite.Require().NoError(err)

_, _ = msgSrvr.List(sdk.WrapSDKContext(suite.ctx), &types.MsgList{Creator: nameOwner.String(), Name: nuggieName, Price: sdk.NewInt64Coin("ujkl", 200)})
Expand Down Expand Up @@ -111,7 +111,7 @@ func (suite *KeeperTestSuite) TestMsgAcceptOneBid() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, bidder, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, nameOwner.String(), TestName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, nameOwner.String(), TestName, "{}", 2, true)
suite.Require().NoError(err)

err = suite.rnsKeeper.AddBid(suite.ctx, bidder.String(), TestName, "1000ujkl")
Expand Down Expand Up @@ -150,7 +150,7 @@ func (suite *KeeperTestSuite) TestMsgAcceptOneBid() {
preRun: func() *types.MsgAcceptBid {
freeName := "freeBi.jkl"
blockHeight := suite.ctx.BlockHeight()
err := suite.rnsKeeper.RegisterName(suite.ctx, nameOwner.String(), freeName, "{}", 2)
err := suite.rnsKeeper.RegisterRNSName(suite.ctx, nameOwner.String(), freeName, "{}", 2, true)
suite.Require().NoError(err)
name, _ := suite.rnsKeeper.GetNames(suite.ctx, "freeBi", "jkl")
name.Locked = blockHeight + 1
Expand Down Expand Up @@ -210,7 +210,7 @@ func (suite *KeeperTestSuite) TestMsgCancelOneBid() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, bidder, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, nameOwner.String(), TestName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, nameOwner.String(), TestName, "{}", 2, true)
suite.Require().NoError(err)

err = suite.rnsKeeper.AddBid(suite.ctx, bidder.String(), TestName, "1000ujkl")
Expand Down
2 changes: 1 addition & 1 deletion x/rns/keeper/msg_server_buy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (suite *KeeperTestSuite) TestBuyMsg() {
fullName := rnsName + "." + rnsTLD // "Nuggie.jkl"

keeper.SetInit(suite.ctx, types.Init{Address: nameOwner.String(), Complete: true})
err = suite.rnsKeeper.RegisterName(suite.ctx, nameOwner.String(), fullName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, nameOwner.String(), fullName, "{}", 2, true)
suite.Require().NoError(err)
originalNames, found := keeper.GetNames(suite.ctx, rnsName, rnsTLD)
suite.Require().True(found)
Expand Down
2 changes: 1 addition & 1 deletion x/rns/keeper/msg_server_delist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (suite *KeeperTestSuite) TestDelistMsg() {
// Init rns account and register rns
rnsName := "Nuggie.jkl"
suite.rnsKeeper.SetInit(suite.ctx, types.Init{Address: nameOwner.String(), Complete: true})
err = suite.rnsKeeper.RegisterName(suite.ctx, nameOwner.String(), rnsName, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, nameOwner.String(), rnsName, "{}", 2, true)
suite.Require().NoError(err)

keeper := suite.rnsKeeper
Expand Down
4 changes: 2 additions & 2 deletions x/rns/keeper/msg_server_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (suite *KeeperTestSuite) TestMsgAddRecord() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, owner, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, owner.String(), "BiPhan.jkl", "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, owner.String(), "BiPhan.jkl", "{}", 2, true)
suite.Require().NoError(err)

cases := []struct {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (suite *KeeperTestSuite) TestMsgDelRecord() {
err = suite.bankKeeper.SendCoinsFromModuleToAccount(suite.ctx, types.ModuleName, owner, coins)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, owner.String(), "BiPhan.jkl", "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, owner.String(), "BiPhan.jkl", "{}", 2, true)
suite.Require().NoError(err)

_, _ = msgSrvr.AddRecord(context, types.NewMsgAddRecord(owner.String(), "BiPhan.jkl", "app", owner.String(), "{}"))
Expand Down
33 changes: 31 additions & 2 deletions x/rns/keeper/msg_server_register.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/jackalLabs/canine-chain/v4/x/rns/types"
)

func (k Keeper) RegisterName(ctx sdk.Context, sender string, nm string, data string, years int64) error {
func (k Keeper) RegisterRNSName(ctx sdk.Context, sender string, nm string, data string, years int64, primary bool) error {
nm = strings.ToLower(nm)
name, tld, err := GetNameAndTLD(nm)
if err != nil {
Expand Down Expand Up @@ -81,14 +81,43 @@ func (k Keeper) RegisterName(ctx sdk.Context, sender string, nm string, data str
// Write whois information to the store
k.SetNames(ctx, newWhois)

_, hasPrimary := k.GetPrimaryName(ctx, newWhois.Value)

if primary || !hasPrimary {
k.SetPrimaryName(ctx, newWhois.Value, newWhois.Name, newWhois.Tld)
}

return nil
}

func (k msgServer) MakePrimary(goCtx context.Context, msg *types.MsgMakePrimary) (*types.MsgMakePrimaryResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

nm := strings.ToLower(msg.Name)
name, tld, err := GetNameAndTLD(nm)
if err != nil {
return nil, err
}

k.SetPrimaryName(ctx, msg.Creator, name, tld)

return &types.MsgMakePrimaryResponse{}, err
}

// Register is Deprecated! Use RegisterName instead.
func (k msgServer) Register(goCtx context.Context, msg *types.MsgRegister) (*types.MsgRegisterResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Try getting a name from the store

err := k.RegisterName(ctx, msg.Creator, msg.Name, msg.Data, msg.Years)
err := k.RegisterRNSName(ctx, msg.Creator, msg.Name, msg.Data, msg.Years, false)

return &types.MsgRegisterResponse{}, err
}

func (k msgServer) RegisterName(goCtx context.Context, msg *types.MsgRegisterName) (*types.MsgRegisterNameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

err := k.RegisterRNSName(ctx, msg.Creator, msg.Name, msg.Data, msg.Years, msg.SetPrimary)

return &types.MsgRegisterNameResponse{}, err
}
28 changes: 28 additions & 0 deletions x/rns/keeper/names.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"fmt"
"strings"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -10,6 +11,33 @@ import (
"github.com/jackalLabs/canine-chain/v4/x/rns/types"
)

func (k Keeper) SetPrimaryName(ctx sdk.Context, owner, name, tld string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PrimaryNameKeyPrefix))
store.Set(types.PrimaryNameKey(
owner,
), []byte(fmt.Sprintf("%s.%s", name, tld)))
}

func (k Keeper) GetPrimaryName(
ctx sdk.Context,
owner string,
) (val types.Names, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.PrimaryNameKeyPrefix))

b := store.Get(types.PrimaryNameKey(
owner,
))
if b == nil {
return val, false
}

n := string(b)

nameString := strings.Split(n, ".")

return k.GetNames(ctx, nameString[0], nameString[1])
}

// SetNames set a specific names in the store from its index
func (k Keeper) SetNames(ctx sdk.Context, names types.Names) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.NamesKeyPrefix))
Expand Down
8 changes: 6 additions & 2 deletions x/rns/keeper/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ func (suite *KeeperTestSuite) TestMsgRegisterName() {
beforebal := suite.bankKeeper.GetAllBalances(suite.ctx, address)
amt := beforebal.AmountOf("ujkl")

err = suite.rnsKeeper.RegisterName(suite.ctx, address.String(), name, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, address.String(), name, "{}", 2, true)
suite.Require().NoError(err)

primName, f := suite.rnsKeeper.GetPrimaryName(suite.ctx, address.String())
suite.Require().Equal(true, f)
suite.Require().Equal(name, primName.GetDisplay())

nameReq := types.QueryName{
Name: name,
}
Expand All @@ -52,7 +56,7 @@ func (suite *KeeperTestSuite) TestMsgRegisterName() {
_, err = suite.queryClient.Name(suite.ctx.Context(), &nameReq)
suite.Require().NoError(err)

err = suite.rnsKeeper.RegisterName(suite.ctx, address.String(), capname, "{}", 2) // adding time to registration
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, address.String(), capname, "{}", 2, true) // adding time to registration
suite.Require().NoError(err)

afterbal = suite.bankKeeper.GetAllBalances(suite.ctx, address)
Expand Down
2 changes: 1 addition & 1 deletion x/rns/keeper/transfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (suite *KeeperTestSuite) TestMsgTransfer() {
beforebal := suite.bankKeeper.GetAllBalances(suite.ctx, address)
amt := beforebal.AmountOf("ujkl")

err = suite.rnsKeeper.RegisterName(suite.ctx, address.String(), name, "{}", 2)
err = suite.rnsKeeper.RegisterRNSName(suite.ctx, address.String(), name, "{}", 2, true)
suite.Require().NoError(err)

nameReq := types.QueryName{
Expand Down
15 changes: 14 additions & 1 deletion x/rns/types/key_names.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var _ binary.ByteOrder

const (
// NamesKeyPrefix is the prefix to retrieve all Names
NamesKeyPrefix = "Names/value/"
NamesKeyPrefix = "Names/value/"
PrimaryNameKeyPrefix = "PrimaryName/value/"
)

// NamesKey returns the store key to retrieve a Names from the index fields
Expand All @@ -25,3 +26,15 @@ func NamesKey(

return key
}

func PrimaryNameKey(
owner string,
) []byte {
var key []byte

indexBytes := []byte(owner)
key = append(key, indexBytes...)
key = append(key, []byte("/")...)

return key
}
Loading

0 comments on commit 8ca23e1

Please sign in to comment.