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

Commit

Permalink
add test and fix logic
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Nov 2, 2023
1 parent 127269f commit 85a80f4
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (k msgServer) ExtendTopLevelDomainExpirationDate(goCtx context.Context, msg
return nil, err
}

fee, err := k.Keeper.ExtendTopLevelDomainExpirationDate(ctx, msg.Creator, domain, msg.ExtensionPeriodInYear)
fee, err := k.Keeper.ExtendTopLevelDomainExpirationDate(ctx, msg.Creator, &domain, msg.ExtensionPeriodInYear)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,125 @@
package keeper_test

import (
"fmt"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/mycel-domain/mycel/app/params"
"github.com/mycel-domain/mycel/testutil"
furnacetypes "github.com/mycel-domain/mycel/x/furnace/types"
"github.com/mycel-domain/mycel/x/registry/types"

errorsmod "cosmossdk.io/errors"
)

func (suite *KeeperTestSuite) TestExtendTopLevelDomain() {
name := "hoge"

testCases := []struct {
creator string
extensionPeriodInYear uint64
expErr error
fn func()
}{
{
creator: testutil.Alice,
extensionPeriodInYear: 1,
expErr: nil,
fn: func() {},
},
{
creator: testutil.Bob,
extensionPeriodInYear: 1,
expErr: errorsmod.Wrapf(types.ErrDomainNotEditable, "%s", testutil.Bob),
fn: func() {},
},
}

for i, tc := range testCases {
suite.Run(fmt.Sprintf("Case %d", i), func() {
suite.SetupTest()

// Register domain
registerMsg := &types.MsgRegisterTopLevelDomain{
Creator: testutil.Alice,
Name: name,
RegistrationPeriodInYear: 1,
}
registerRsp, err := suite.msgServer.RegisterTopLevelDomain(suite.ctx, registerMsg)
suite.Require().Nil(err)
beforeDomain, found := suite.app.RegistryKeeper.GetTopLevelDomain(suite.ctx, name)
suite.Require().True(found)
beforeExpirationDate := time.Unix(0, beforeDomain.ExpirationDate)
_ = beforeExpirationDate

// Run test case function
tc.fn()

// Before balances
furnaceAddress := authtypes.NewModuleAddress(furnacetypes.ModuleName)
beforeFurnaceBalance := suite.app.BankKeeper.GetAllBalances(suite.ctx, furnaceAddress)
beforeTreasuryBalance := suite.app.DistrKeeper.GetFeePool(suite.ctx).CommunityPool

// Extend domain
extendMsg := &types.MsgExtendTopLevelDomainExpirationDate{
Creator: tc.creator,
Name: name,
ExtensionPeriodInYear: tc.extensionPeriodInYear,
}
extendRsp, err := suite.msgServer.ExtendTopLevelDomainExpirationDate(suite.ctx, extendMsg)

// After balances
afterFurnaceBalance := suite.app.BankKeeper.GetAllBalances(suite.ctx, furnaceAddress)
afterTreasuryBalance := suite.app.DistrKeeper.GetFeePool(suite.ctx).CommunityPool

if tc.expErr == nil {
suite.Require().Nil(err)
// Evaluate if domain is extended
// Response
registerExpirationDate := time.Unix(0, registerRsp.TopLevelDomain.ExpirationDate)
extendExpirationDate := time.Unix(0, extendRsp.TopLevelDomain.ExpirationDate)
suite.Require().Equal(registerExpirationDate.AddDate(0, 0, int(tc.extensionPeriodInYear)*params.OneYearInDays), extendExpirationDate)
// 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.ExpirationDate)
suite.Require().Equal(expAfterExpirationDate, afterExpirationDate)

// Evalute events
events, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), types.EventTypeExtendTopLevelDomainExpirationDate)
suite.Require().True(found)
for _, event := range events {
suite.Require().Equal(name, event.Attributes[0].Value)

// Check if the extension fee is correct
total, err := sdk.ParseCoinsNormalized(event.Attributes[2].Value)
suite.Require().Nil(err)
toBurn, err := sdk.ParseCoinNormalized(event.Attributes[4].Value)
suite.Require().Nil(err)
toTreasury, err := sdk.ParseCoinNormalized(event.Attributes[5].Value)
suite.Require().Nil(err)

// Check if the total is equal to the sum of toBurn and toTreasury
if total.Len() == 1 {
suite.Require().Equal(total, sdk.NewCoins(toBurn.Add(toTreasury)))

} else {
suite.Require().Equal(total, sdk.NewCoins(toBurn, toTreasury))
}

// Check if the furnace balance is increased
expectedFurnaceBalance := beforeFurnaceBalance.Add(toBurn)
suite.Require().Equal(expectedFurnaceBalance, afterFurnaceBalance)
expectedTreasuryBalance := beforeTreasuryBalance.Add(sdk.NewDecCoinFromCoin(toTreasury))
suite.Require().Equal(expectedTreasuryBalance, afterTreasuryBalance)
}

} else {
suite.Require().EqualError(err, tc.expErr.Error())
}
})
}
}
17 changes: 6 additions & 11 deletions x/registry/keeper/top_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func (k Keeper) RegisterTopLevelDomain(ctx sdk.Context, domain types.TopLevelDom
}

// Extend top level domain expirationDate
func (k Keeper) ExtendTopLevelDomainExpirationDate(ctx sdk.Context, creator string, domain types.TopLevelDomain, extensionPeriodInYear uint64) (fee types.TopLevelDomainFee, err error) {
func (k Keeper) ExtendTopLevelDomainExpirationDate(ctx sdk.Context, creator string, domain *types.TopLevelDomain, extensionPeriodInYear uint64) (fee types.TopLevelDomainFee, err error) {
creatorAddress, err := sdk.AccAddressFromBech32(creator)
if err != nil {
return types.TopLevelDomainFee{}, err
Expand All @@ -213,23 +213,18 @@ func (k Keeper) ExtendTopLevelDomainExpirationDate(ctx sdk.Context, creator stri
}

// Pay TLD extend fee
fee, err = k.PayTopLevelDomainFee(ctx, creatorAddress, domain, extensionPeriodInYear)
fee, err = k.PayTopLevelDomainFee(ctx, creatorAddress, *domain, extensionPeriodInYear)
if err != nil {
return types.TopLevelDomainFee{}, err
}

// Update domain store
currentTime := ctx.BlockTime()
expirationDate := currentTime.AddDate(0, 0, params.OneYearInDays*int(extensionPeriodInYear))
domain.ExpirationDate = expirationDate.UnixNano()
k.SetTopLevelDomain(ctx, domain)

// Set domain
domain.ExpirationDate = ctx.BlockTime().AddDate(0, 0, params.OneYearInDays*int(extensionPeriodInYear)).UnixNano()
k.SetTopLevelDomain(ctx, domain)
currentExpirationDate := time.Unix(0, domain.ExpirationDate)
domain.ExtendExpirationDate(currentExpirationDate, extensionPeriodInYear)
k.SetTopLevelDomain(ctx, *domain)

// Emit event
EmitExtendTopLevelDomainExpirationDateEvent(ctx, domain, fee)
EmitExtendTopLevelDomainExpirationDateEvent(ctx, *domain, fee)

return fee, err
}

0 comments on commit 85a80f4

Please sign in to comment.