-
Notifications
You must be signed in to change notification settings - Fork 17
/
msg_server_addel_redcord.go
59 lines (45 loc) · 1.34 KB
/
msg_server_addel_redcord.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package keeper
import (
"context"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/jackalLabs/canine-chain/x/rns/types"
)
func (k msgServer) DelRecord(goCtx context.Context, msg *types.MsgDelRecord) (*types.MsgDelRecordResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
mname := strings.ToLower(msg.Name)
n, tld, err := GetNameAndTLD(mname)
if err != nil {
return nil, err
}
sub, n, hasSub := GetSubdomain(n)
if !hasSub {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, "name does not contain records")
}
val, found := k.GetNames(ctx, n, tld)
if !found {
return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, "cannot find name")
}
if ctx.BlockHeight() > val.Expires {
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "name does not exist or has expired")
}
if msg.Creator != val.Value {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "you do not own this name")
}
removed := false
dms := []*types.Names{}
for _, domain := range val.Subdomains {
if domain.Name != sub {
dms = append(dms, domain)
continue
}
removed = true
}
if !removed {
return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "record does not exist for this name")
}
val.Subdomains = dms
k.SetNames(ctx, val)
return &types.MsgDelRecordResponse{}, nil
}