Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
add valitating for TLD domain & fix to pass test
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiym committed Sep 12, 2023
1 parent 0863f8d commit f521437
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
17 changes: 8 additions & 9 deletions x/registry/keeper/msg_server_register_top_level_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,33 @@ func (suite *KeeperTestSuite) TestRegisterTopLevelDomain() {
creator string
name string
registrationPeriodInYear uint64
domainOwnership types.DomainOwnership
expErr error
fn func()
}{
{
creator: testutil.Alice,
name: "cel",
name: "cel0",
registrationPeriodInYear: 1,
expErr: nil,
fn: func() {},
},
{
creator: testutil.Alice,
name: "cel",
name: "cel1",
registrationPeriodInYear: 4,
expErr: nil,
fn: func() {},
},
{
creator: testutil.Alice,
name: "cel",
name: "cel2",
registrationPeriodInYear: 1,
expErr: sdkerrors.Wrapf(errors.New(fmt.Sprintf("foo.cel")), types.ErrDomainIsAlreadyTaken.Error()),
expErr: sdkerrors.Wrapf(errors.New(fmt.Sprintf("cel2")), types.ErrDomainIsAlreadyTaken.Error()),
fn: func() {
// Register domain once
domain := &types.MsgRegisterTopLevelDomain{
Creator: testutil.Alice,
Name: "cel",
Name: "cel2",
RegistrationPeriodInYear: 1,
}
_, err := suite.msgServer.RegisterTopLevelDomain(suite.ctx, domain)
Expand All @@ -69,7 +68,7 @@ func (suite *KeeperTestSuite) TestRegisterTopLevelDomain() {
_, err := suite.msgServer.RegisterTopLevelDomain(suite.ctx, registerMsg)
fmt.Println("----Case_", i, "---01", err)

if err == nil {
if tc.expErr == nil {
// Evalute if domain is registered
_, found := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, tc.name)
suite.Require().True(found)
Expand All @@ -79,10 +78,10 @@ func (suite *KeeperTestSuite) TestRegisterTopLevelDomain() {
events := sdk.StringifyEvents(suite.ctx.EventManager().ABCIEvents())
eventIndex := len(events) - 1
suite.Require().EqualValues(sdk.StringEvent{
Type: types.EventTypeRegsterDomain,
Type: types.EventTypeRegsterTopLevelDomain,
Attributes: []sdk.Attribute{
{Key: types.AttributeRegisterTopLevelDomainEventName, Value: tc.name},
{Key: types.AttributeRegisterTopLevelDomainEventExpirationDate, Value: events[eventIndex].Attributes[2].Value},
{Key: types.AttributeRegisterTopLevelDomainEventExpirationDate, Value: events[eventIndex].Attributes[1].Value},
},
}, events[eventIndex])
} else {
Expand Down
44 changes: 44 additions & 0 deletions x/registry/keeper/register_top_level_domain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package keeper

import (
// "errors"
// "fmt"
"github.com/mycel-domain/mycel/x/registry/types"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
// sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// Pay TLD registration fee
func (k Keeper) PayTLDRegstrationFee(ctx sdk.Context, payer sdk.AccAddress, domain types.TopLevelDomain, registrationPeriodInYear uint64) (err error) {
// TODO: Pay fee
return nil
}

func (k Keeper) RegisterTopLevelDomain(ctx sdk.Context, domain types.TopLevelDomain, owner sdk.AccAddress, registrationPeriodIYear uint64) (err error) {
// Validate domain
err = k.ValidateTopLevelDomain(ctx, domain)
if err != nil {
return err
}

// Pay TLD registration fee
err = k.PayTLDRegstrationFee(ctx, owner, domain, registrationPeriodIYear)
if err != nil {
return err
}

// Set domain
k.SetTopLevelDomain(ctx, domain)

// Emit event
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeRegsterTopLevelDomain,
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventName, domain.Name),
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventExpirationDate, strconv.FormatInt(domain.ExpirationDate, 10)),
),
)

return err
}
37 changes: 32 additions & 5 deletions x/registry/keeper/validate_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ func (k Keeper) GetIsParentDomainExist(ctx sdk.Context, domain types.SecondLevel
return isParentDomainExist
}

// Get is domain already taken
func (k Keeper) GetIsTopLevelDomainAlreadyTaken(ctx sdk.Context, domain types.TopLevelDomain) (isDomainAlreadyTaken bool) {
_, isDomainAlreadyTaken = k.GetTopLevelDomain(ctx, domain.Name)
return isDomainAlreadyTaken
}

// Validate TLD registration
func (k Keeper) ValidateRegisterTLD(ctx sdk.Context, domain types.SecondLevelDomain) (err error) {
if domain.Parent != "" {
err = sdkerrors.Wrapf(errors.New(domain.Parent),
types.ErrParentDomainMustBeEmpty.Error())
}
func (k Keeper) ValidateRegisterTLD(ctx sdk.Context, domain types.TopLevelDomain) (err error) {
// TODO: Validate TLD
// TODO: Is Staked enough to register TLD
return err
}
Expand Down Expand Up @@ -84,3 +87,27 @@ func (k Keeper) ValidateSecondLevelDomain(ctx sdk.Context, domain types.SecondLe

return err
}

// Validate top-level-domain
func (k Keeper) ValidateTopLevelDomain(ctx sdk.Context, domain types.TopLevelDomain) (err error) {
// Type check
err = domain.Validate()
if err != nil {
return err
}
// Check if domain is already taken
isDomainAlreadyTaken := k.GetIsTopLevelDomainAlreadyTaken(ctx, domain)
if isDomainAlreadyTaken {
err = sdkerrors.Wrapf(errors.New(fmt.Sprintf("%s", domain.Name)),
types.ErrDomainIsAlreadyTaken.Error())
return err
}

// Validate TLD
err = k.ValidateRegisterTLD(ctx, domain)
if err != nil {
return err
}

return err
}

0 comments on commit f521437

Please sign in to comment.