-
Notifications
You must be signed in to change notification settings - Fork 2
/
msg_update_metadata.go
62 lines (50 loc) · 1.51 KB
/
msg_update_metadata.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
60
61
62
package keeper
import (
"context"
"cosmossdk.io/orm/types/ormerrors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
v1 "github.com/chora-io/mods/geonode/types/v1"
)
// UpdateMetadata implements the Msg/UpdateMetadata method.
func (k Keeper) UpdateMetadata(ctx context.Context, req *v1.MsgUpdateMetadata) (*v1.MsgUpdateMetadataResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
// get account from curator address
curator, err := sdk.AccAddressFromBech32(req.Curator)
if err != nil {
return nil, err // internal error
}
// get node from node table
node, err := k.ss.NodeTable().Get(ctx, req.Id)
if err != nil {
if ormerrors.NotFound.Is(err) {
return nil, sdkerrors.ErrNotFound.Wrapf("node with id %d: %s", req.Id, err)
}
return nil, err // internal error
}
// get account from account bytes
nodeCurator := sdk.AccAddress(node.Curator)
// verify curator is node curator
if !nodeCurator.Equals(curator) {
return nil, sdkerrors.ErrUnauthorized.Wrapf(
"curator %s: node curator %s", curator, nodeCurator.String(),
)
}
// set new node metadata
node.Metadata = req.NewMetadata
// update node in node table
err = k.ss.NodeTable().Update(ctx, node)
if err != nil {
return nil, err // internal error
}
// emit event
if err = sdkCtx.EventManager().EmitTypedEvent(&v1.EventUpdateMetadata{
Id: node.Id,
}); err != nil {
return nil, err // internal error
}
// return response
return &v1.MsgUpdateMetadataResponse{
Id: node.Id,
}, nil
}