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

Commit

Permalink
ExpirationDateInUnixNano -> ExpirationDate:Timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Nov 2, 2023
1 parent 4c9ec36 commit efa78c7
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 94 deletions.
6 changes: 5 additions & 1 deletion proto/mycel/registry/top_level_domain.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package mycel.registry;

import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "cosmos/base/v1beta1/coin.proto";
import "mycel/registry/subdomain_config.proto";
import "mycel/registry/role.proto";
Expand All @@ -10,7 +11,10 @@ option go_package = "github.com/mycel-domain/mycel/x/registry/types";

message TopLevelDomain {
string name = 1;
int64 expirationDateInUnixNano = 2;
google.protobuf.Timestamp expirationDate = 2 [
(gogoproto.stdtime) = true,
(gogoproto.nullable) = false
];
map<string, string> metadata = 3;
SubdomainConfig subdomainConfig = 4;
uint64 subdomainCount = 5;
Expand Down
7 changes: 2 additions & 5 deletions x/registry/keeper/events.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"time"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -11,12 +10,11 @@ import (

// Register top-level-domain event
func EmitRegisterTopLevelDomainEvent(ctx sdk.Context, domain types.TopLevelDomain, fee types.TopLevelDomainFee) {
expirationDate := time.Unix(0, domain.ExpirationDateInUnixNano)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRegisterTopLevelDomain,
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventName, domain.Name),
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventExpirationDate, expirationDate.String()),
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventExpirationDate, domain.ExpirationDate.String()),
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventMaxSubdomainRegistrations, fmt.Sprintf("%d", domain.SubdomainConfig.MaxSubdomainRegistrations)),
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventTotalRegistrationFee, fee.TotalFee.String()),
sdk.NewAttribute(types.AttributeRegisterTopLevelDomainEventBurnWeight, fee.BurnWeight),
Expand Down Expand Up @@ -74,11 +72,10 @@ func EmitWithdrawRegistrationFeeEvent(ctx sdk.Context, msg types.MsgWithdrawRegi

// Extend top-level-domain expiration date event
func EmitExtendTopLevelDomainExpirationDateEvent(ctx sdk.Context, domain types.TopLevelDomain, fee types.TopLevelDomainFee) {
expirationDate := time.Unix(0, domain.ExpirationDateInUnixNano)
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeExtendTopLevelDomainExpirationDate,
sdk.NewAttribute(types.AttributeExtendTopLevelDomainExpirationDateEventDomainName, domain.Name),
sdk.NewAttribute(types.AttributeExtendTopLevelDomainExpirationDateEventExpirationDate, expirationDate.String()),
sdk.NewAttribute(types.AttributeExtendTopLevelDomainExpirationDateEventExpirationDate, domain.ExpirationDate.String()),
sdk.NewAttribute(types.AttributeExtendTopLevelDomainExpirationDateEventTotalRegistrationFee, fee.TotalFee.String()),
sdk.NewAttribute(types.AttributeExtendTopLevelDomainExpirationDateEventBurnWeight, fee.BurnWeight),
sdk.NewAttribute(types.AttributeExtendTopLevelDomainExpirationDateEventRegistrationFeeToBurn, fee.FeeToBurn.String()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package keeper_test

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -51,7 +50,6 @@ func (suite *KeeperTestSuite) TestExtendTopLevelDomain() {
suite.Require().Nil(err)
beforeDomain, found := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, name)
suite.Require().True(found)
beforeExpirationDate := time.Unix(0, beforeDomain.ExpirationDateInUnixNano)

// Run test case function
tc.fn()
Expand All @@ -77,15 +75,12 @@ func (suite *KeeperTestSuite) TestExtendTopLevelDomain() {
suite.Require().Nil(err)
// Evaluate if domain is extended
// Response
registerExpirationDate := time.Unix(0, registerRsp.TopLevelDomain.ExpirationDateInUnixNano)
extendExpirationDate := time.Unix(0, extendRsp.TopLevelDomain.ExpirationDateInUnixNano)
suite.Require().Equal(registerExpirationDate.AddDate(0, 0, int(tc.extensionPeriodInYear)*params.OneYearInDays), extendExpirationDate)
suite.Require().Equal(registerRsp.TopLevelDomain.ExpirationDate.AddDate(0, 0, int(tc.extensionPeriodInYear)*params.OneYearInDays), extendRsp.TopLevelDomain.ExpirationDate)
// Store
afterDomain, found := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, name)
suite.Require().True(found)
expAfterExpirationDate := beforeExpirationDate.AddDate(0, 0, int(tc.extensionPeriodInYear)*params.OneYearInDays)
afterExpirationDate := time.Unix(0, afterDomain.ExpirationDateInUnixNano)
suite.Require().Equal(expAfterExpirationDate, afterExpirationDate)
expAfterExpirationDate := beforeDomain.ExpirationDate.AddDate(0, 0, int(tc.extensionPeriodInYear)*params.OneYearInDays)
suite.Require().Equal(expAfterExpirationDate, afterDomain.ExpirationDate)

// Evalute events
events, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), types.EventTypeExtendTopLevelDomainExpirationDate)
Expand Down
12 changes: 6 additions & 6 deletions x/registry/keeper/msg_server_register_top_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func (k msgServer) RegisterTopLevelDomain(goCtx context.Context, msg *types.MsgR

defaultRegistrationConfig := types.GetDefaultSubdomainConfig(3030)
domain := types.TopLevelDomain{
Name: msg.Name,
ExpirationDateInUnixNano: expirationDate.UnixNano(),
Metadata: nil,
SubdomainConfig: &defaultRegistrationConfig,
AccessControl: accessControl,
TotalWithdrawalAmount: sdk.NewCoins(),
Name: msg.Name,
ExpirationDate: expirationDate,
Metadata: nil,
SubdomainConfig: &defaultRegistrationConfig,
AccessControl: accessControl,
TotalWithdrawalAmount: sdk.NewCoins(),
}

fee, err := k.Keeper.RegisterTopLevelDomain(ctx, domain, msg.Creator, msg.RegistrationPeriodInYear)
Expand Down
6 changes: 2 additions & 4 deletions x/registry/keeper/top_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ func (k Keeper) GetValidTopLevelDomain(ctx sdk.Context, name string) (topLevelDo
}

// Check if domain is not expired
expirationDate := time.Unix(0, topLevelDomain.ExpirationDateInUnixNano)
if ctx.BlockTime().After(expirationDate) && topLevelDomain.ExpirationDateInUnixNano != 0 {
if ctx.BlockTime().After(topLevelDomain.ExpirationDate) && topLevelDomain.ExpirationDate != (time.Time{}) {
return topLevelDomain, errorsmod.Wrapf(types.ErrDomainExpired, "%s", name)
}

Expand Down Expand Up @@ -219,8 +218,7 @@ func (k Keeper) ExtendTopLevelDomainExpirationDate(ctx sdk.Context, creator stri
}

// Update domain store
currentExpirationDate := time.Unix(0, domain.ExpirationDateInUnixNano)
domain.ExtendExpirationDate(currentExpirationDate, extensionPeriodInYear)
domain.ExtendExpirationDate(domain.ExpirationDate, extensionPeriodInYear)
k.SetTopLevelDomain(ctx, *domain)

// Emit event
Expand Down
7 changes: 4 additions & 3 deletions x/registry/keeper/top_level_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"fmt"
"time"

"strconv"
"testing"
Expand Down Expand Up @@ -73,21 +74,21 @@ func (suite *KeeperTestSuite) TestGetValidTopLevelDomain() {
{
topLevelDomain: types.TopLevelDomain{
Name: "test",
ExpirationDateInUnixNano: suite.ctx.BlockTime().AddDate(0, 0, 20).UnixNano(),
ExpirationDate: suite.ctx.BlockTime().AddDate(0, 0, 20),
},
expErr: nil,
},
{
topLevelDomain: types.TopLevelDomain{
Name: "test",
ExpirationDateInUnixNano: 0,
ExpirationDate: time.Time{},
},
expErr: nil,
},
{
topLevelDomain: types.TopLevelDomain{
Name: "test",
ExpirationDateInUnixNano: suite.ctx.BlockTime().AddDate(0, 0, -20).UnixNano(),
ExpirationDate: suite.ctx.BlockTime().AddDate(0, 0, -20),
},
expErr: errorsmod.Wrapf(types.ErrDomainExpired, "test"),
},
Expand Down
9 changes: 4 additions & 5 deletions x/registry/types/top_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ func (topLevelDomain TopLevelDomain) IsEditable(sender string) (isEditable bool,
return isEditable, err
}

func (topLevelDomain *TopLevelDomain) ExtendExpirationDate(from time.Time, extensionPeriodInYear uint64) (expirationDateInUnixNano int64) {
newExpirationDate := from.AddDate(0, 0, params.OneYearInDays*int(extensionPeriodInYear))
expirationDateInUnixNano = newExpirationDate.UnixNano()
topLevelDomain.ExpirationDateInUnixNano = expirationDateInUnixNano
func (topLevelDomain *TopLevelDomain) ExtendExpirationDate(from time.Time, extensionPeriodInYear uint64) (expirationDate time.Time) {
expirationDate = from.AddDate(0, 0, params.OneYearInDays*int(extensionPeriodInYear))
topLevelDomain.ExpirationDate = expirationDate

return expirationDateInUnixNano
return expirationDate
}
Loading

0 comments on commit efa78c7

Please sign in to comment.