diff --git a/x/registry/keeper/msg_server_register_top_level_domain_test.go b/x/registry/keeper/msg_server_register_top_level_domain_test.go index e1db88f7..8b41a322 100644 --- a/x/registry/keeper/msg_server_register_top_level_domain_test.go +++ b/x/registry/keeper/msg_server_register_top_level_domain_test.go @@ -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) @@ -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) @@ -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 { diff --git a/x/registry/keeper/register_top_level_domain.go b/x/registry/keeper/register_top_level_domain.go new file mode 100644 index 00000000..ef8556a9 --- /dev/null +++ b/x/registry/keeper/register_top_level_domain.go @@ -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 +} diff --git a/x/registry/keeper/validate_registration.go b/x/registry/keeper/validate_registration.go index 36c59f8a..7e54197f 100644 --- a/x/registry/keeper/validate_registration.go +++ b/x/registry/keeper/validate_registration.go @@ -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 } @@ -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 +}