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

Commit

Permalink
impl updateTextRecord message
Browse files Browse the repository at this point in the history
  • Loading branch information
taryune committed Dec 14, 2023
1 parent fcba715 commit bf67c97
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 1 deletion.
12 changes: 12 additions & 0 deletions x/registry/keeper/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ func EmitUpdateDnsRecordEvent(ctx sdk.Context, msg types.MsgUpdateDnsRecord) {
)
}

// Update dns record event
func EmitUpdateTextRecordEvent(ctx sdk.Context, msg types.MsgUpdateTextRecord) {
ctx.EventManager().EmitEvent(
sdk.NewEvent(types.EventTypeUpdateTextRecord,
sdk.NewAttribute(types.AttributeUpdateTextRecordEventDomainName, msg.Name),
sdk.NewAttribute(types.AttributeUpdateTextRecordEventDomainParent, msg.Parent),
sdk.NewAttribute(types.AttributeUpdateTextRecordEventKey, msg.Key),
sdk.NewAttribute(types.AttributeUpdateTextRecordEventValue, msg.Value),
),
)
}

// Withdraw fees event
func EmitWithdrawRegistrationFeeEvent(ctx sdk.Context, msg types.MsgWithdrawRegistrationFee, fee sdk.Coins) {
ctx.EventManager().EmitEvent(
Expand Down
21 changes: 20 additions & 1 deletion x/registry/keeper/msg_server_update_text_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,34 @@ package keeper
import (
"context"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/mycel-domain/mycel/x/registry/types"
)

func (k msgServer) UpdateTextRecord(goCtx context.Context, msg *types.MsgUpdateTextRecord) (*types.MsgUpdateTextRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
domain, isFound := k.Keeper.GetSecondLevelDomain(ctx, msg.Name, msg.Parent)
if !isFound {
return nil, errorsmod.Wrapf(types.ErrSecondLevelDomainNotFound, "%s.%s", msg.Name, msg.Parent)
}

// Check if the domain is owned by the creator
isEditable, err := domain.IsRecordEditable(msg.Creator)
if !isEditable {
return nil, err
}

err = domain.UpdateTextRecord(msg.Key, msg.Value)
if err != nil {
return nil, err
}
k.Keeper.SetSecondLevelDomain(ctx, domain)

// Emit event
EmitUpdateTextRecordEvent(ctx, *msg)

return &types.MsgUpdateTextRecordResponse{}, nil
}
107 changes: 107 additions & 0 deletions x/registry/keeper/msg_server_update_text_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package keeper_test

import (
"fmt"

errorsmod "cosmossdk.io/errors"

"github.com/mycel-domain/mycel/testutil"
"github.com/mycel-domain/mycel/x/registry/types"
)

func (suite *KeeperTestSuite) TestUpdateTextRecord() {
testCases := []struct {
creator string
name string
parent string
key string
value string
expErr error
fn func()
}{
{
creator: testutil.Alice,
name: "foo",
parent: "cel",
key: "com.github",
value: "mycel-domain",
expErr: nil,
fn: func() {},
},
{
creator: testutil.Alice,
name: "foo",
parent: "cel",
key: "ETHEREUM_MAINNET_MAINNET",
value: "mycel-domain",
expErr: errorsmod.Wrapf(types.ErrInvalidTextRecordKey, "%s", "ETHEREUM_MAINNET_MAINNET"),
fn: func() {},
},
{
creator: testutil.Alice,
name: "hoge",
parent: "fuga",
key: "com.github",
value: "mycel-domain",
expErr: errorsmod.Wrapf(types.ErrSecondLevelDomainNotFound, "hoge.fuga"),
fn: func() {},
},
{
creator: testutil.Bob,
name: "foo",
parent: "cel",
key: "com.github",
value: "mycel-domain",
expErr: errorsmod.Wrapf(types.ErrSecondLevelDomainNotEditable, "%s", testutil.Bob),
fn: func() {},
},
}
for i, tc := range testCases {
suite.Run(fmt.Sprintf("Case %d", i), func() {
suite.SetupTest()

// Register domain
domain := &types.MsgRegisterSecondLevelDomain{
Creator: testutil.Alice,
Name: "foo",
Parent: "cel",
RegistrationPeriodInYear: 1,
}
_, err := suite.msgServer.RegisterSecondLevelDomain(suite.ctx, domain)
suite.Require().Nil(err)
// Run test case function
tc.fn()

// Update dns record
msgUpdateRecord := &types.MsgUpdateTextRecord{
Creator: tc.creator,
Name: tc.name,
Parent: tc.parent,
Key: tc.key,
Value: tc.value,
}
_, err = suite.msgServer.UpdateTextRecord(suite.ctx, msgUpdateRecord)

if tc.expErr == nil {
// Check if the record is updated
suite.Require().Nil(err)
res, _ := suite.app.RegistryKeeper.GetSecondLevelDomain(suite.ctx, domain.Name, domain.Parent)
suite.Require().Equal(tc.value, res.GetTextRecord(tc.key))
// Evalute events
events, found := testutil.FindEventsByType(suite.ctx.EventManager().Events(), types.EventTypeUpdateTextRecord)
suite.Require().True(found)

for _, event := range events {
suite.Require().Equal(tc.name, event.Attributes[0].Value)
suite.Require().Equal(tc.parent, event.Attributes[1].Value)
suite.Require().Equal(tc.key, event.Attributes[2].Value)
suite.Require().Equal(tc.value, event.Attributes[3].Value)
}
} else {
suite.Require().EqualError(err, tc.expErr.Error())
}

})
}

}
1 change: 1 addition & 0 deletions x/registry/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var (
ErrInvalidWalletRecordType = errorsmod.Register(ModuleName, 1201, "invalid wallet record type")
ErrInvalidDnsRecordValue = errorsmod.Register(ModuleName, 1202, "invalid dns record value")
ErrInvalidDnsRecordType = errorsmod.Register(ModuleName, 1203, "invalid dns record type")
ErrInvalidTextRecordKey = errorsmod.Register(ModuleName, 1204, "invalid text record key, this key is reserved")
)

// withdraw sentinel errors
Expand Down
10 changes: 10 additions & 0 deletions x/registry/types/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ const (
AttributeUpdateDnsRecordEventValue = "value"
)

// Update text record event
const (
EventTypeUpdateTextRecord = "update-text-record"

AttributeUpdateTextRecordEventDomainName = "name"
AttributeUpdateTextRecordEventDomainParent = "parent"
AttributeUpdateTextRecordEventKey = "key"
AttributeUpdateTextRecordEventValue = "value"
)

// Withdraw fees event
const (
EventTypeWithdrawRegistrationFee = "withdraw-registration-fees"
Expand Down
37 changes: 37 additions & 0 deletions x/registry/types/secend_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ func (secondLevelDomain *SecondLevelDomain) GetDnsRecord(dnsRecordType string) s
return ""
}

func (secondLevelDomain *SecondLevelDomain) GetTextRecord(key string) string {
for _, rec := range secondLevelDomain.Records {
if rec.GetTextRecord() != nil && rec.GetTextRecord().Key == key {
return rec.GetTextRecord().Value
}
}
return ""
}

func GetWalletAddressFormat(walletRecordType string) (walletAddressFormat string, err error) {
// Validate wallet record type
err = ValidateWalletRecordType(walletRecordType)
Expand Down Expand Up @@ -137,6 +146,34 @@ func (secondLevelDomain *SecondLevelDomain) UpdateDnsRecord(dnsRecordType string
return err
}

func (secondLevelDomain *SecondLevelDomain) UpdateTextRecord(key string, value string) (err error) {
err = ValidateTextRecordKey(key)
if err != nil {
return err
}
textRecord := &TextRecord{
Key: key,
Value: value,
}

record := &Record{
Record: &Record_TextRecord{TextRecord: textRecord},
}

updated := false
for i, rec := range secondLevelDomain.Records {
if rec.GetTextRecord() != nil && rec.GetTextRecord().Key == key {
secondLevelDomain.Records[i] = record
updated = true
break
}
}
if !updated {
secondLevelDomain.Records = append(secondLevelDomain.Records, record)
}
return err
}

func (secondLevelDomain SecondLevelDomain) IsRecordEditable(sender string) (isEditable bool, err error) {
role := secondLevelDomain.GetRole(sender)
if role == DomainRole_NO_ROLE {
Expand Down
28 changes: 28 additions & 0 deletions x/registry/types/second_level_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,34 @@ func TestDomainUpdateDnsRecord(t *testing.T) {
}
}

func TestDomainUpdateTextRecord(t *testing.T) {
testCases := []struct {
key string
value string
expErr error
}{
{
key: "key1",
value: "value1",
},
{
key: "ETHEREUM_MAINNET_MAINNET",
value: "value2",
expErr: errorsmod.Wrapf(ErrInvalidTextRecordKey, "ETHEREUM_MAINNET_MAINNET"),
},
}
for _, tc := range testCases {
domain := SecondLevelDomain{Name: "foo", Parent: "myc"}
err := domain.UpdateTextRecord(tc.key, tc.value)
if tc.expErr == nil {
require.Nil(t, err)
require.Equal(t, tc.value, domain.GetTextRecord(tc.key))
} else {
require.EqualError(t, err, tc.expErr.Error())

Check failure on line 227 in x/registry/types/second_level_domain_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with -s standard -s default -s prefix(github.com/mycel-domain/mycel) -s blank -s dot --custom-order (gci)
}
}
}

func TestGetRoleSLD(t *testing.T) {
testCases := []struct {
domain SecondLevelDomain
Expand Down
12 changes: 12 additions & 0 deletions x/registry/types/validate_second_level_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,15 @@ func ValidateDnsRecordType(dnsRecordType string) (err error) {
}
return err
}

func ValidateTextRecordKey(textRecordKey string) (err error) {
_, isFound := NetworkName_value[textRecordKey]
if isFound {
err = errorsmod.Wrapf(ErrInvalidTextRecordKey, "%s", textRecordKey)
}
_, isFound = DnsRecordType_value[textRecordKey]
if isFound {
err = errorsmod.Wrapf(ErrInvalidTextRecordKey, "%s", textRecordKey)
}
return err
}

0 comments on commit bf67c97

Please sign in to comment.