-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
handler.go
117 lines (103 loc) · 3.62 KB
/
handler.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package client
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/evidence"
evidenceexported "github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/exported"
"github.com/cosmos/cosmos-sdk/x/ibc/02-client/types"
ibctmtypes "github.com/cosmos/cosmos-sdk/x/ibc/07-tendermint/types"
localhosttypes "github.com/cosmos/cosmos-sdk/x/ibc/09-localhost/types"
)
// HandleMsgCreateClient defines the sdk.Handler for MsgCreateClient
func HandleMsgCreateClient(ctx sdk.Context, k Keeper, msg exported.MsgCreateClient) (*sdk.Result, error) {
clientType := exported.ClientTypeFromString(msg.GetClientType())
var clientState exported.ClientState
switch clientType {
case 0:
return nil, sdkerrors.Wrap(ErrInvalidClientType, msg.GetClientType())
case exported.Tendermint:
tmMsg, ok := msg.(ibctmtypes.MsgCreateClient)
if !ok {
return nil, sdkerrors.Wrap(ErrInvalidClientType, "Msg is not a Tendermint CreateClient msg")
}
var err error
clientState, err = ibctmtypes.InitializeFromMsg(tmMsg)
if err != nil {
return nil, err
}
case exported.Localhost:
// msg client id is always "localhost"
clientState = localhosttypes.NewClientState(
k.ClientStore(ctx, msg.GetClientID()),
ctx.ChainID(),
ctx.BlockHeight(),
)
default:
return nil, sdkerrors.Wrap(ErrInvalidClientType, msg.GetClientType())
}
_, err := k.CreateClient(
ctx, clientState, msg.GetConsensusState(),
)
if err != nil {
return nil, err
}
attributes := make([]sdk.Attribute, len(msg.GetSigners())+1)
attributes[0] = sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory)
for i, signer := range msg.GetSigners() {
attributes[i+1] = sdk.NewAttribute(sdk.AttributeKeySender, signer.String())
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
EventTypeCreateClient,
sdk.NewAttribute(AttributeKeyClientID, msg.GetClientID()),
sdk.NewAttribute(AttrbuteKeyClientType, msg.GetClientType()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
attributes...,
),
})
return &sdk.Result{
Events: ctx.EventManager().Events().ToABCIEvents(),
}, nil
}
// HandleMsgUpdateClient defines the sdk.Handler for MsgUpdateClient
func HandleMsgUpdateClient(ctx sdk.Context, k Keeper, msg exported.MsgUpdateClient) (*sdk.Result, error) {
clientState, err := k.UpdateClient(ctx, msg.GetClientID(), msg.GetHeader())
if err != nil {
return nil, err
}
attributes := make([]sdk.Attribute, len(msg.GetSigners())+1)
attributes[0] = sdk.NewAttribute(sdk.AttributeKeyModule, AttributeValueCategory)
for i, signer := range msg.GetSigners() {
attributes[i+1] = sdk.NewAttribute(sdk.AttributeKeySender, signer.String())
}
k.Logger(ctx).Info(fmt.Sprintf("client %s updated to height %d", msg.GetClientID(), clientState.GetLatestHeight()))
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
EventTypeUpdateClient,
sdk.NewAttribute(AttributeKeyClientID, msg.GetClientID()),
sdk.NewAttribute(AttrbuteKeyClientType, clientState.ClientType().String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
attributes...,
),
})
return &sdk.Result{
Events: ctx.EventManager().Events().ToABCIEvents(),
}, nil
}
// HandlerClientMisbehaviour defines the Evidence module handler for submitting a
// light client misbehaviour.
func HandlerClientMisbehaviour(k Keeper) evidence.Handler {
return func(ctx sdk.Context, evidence evidenceexported.Evidence) error {
misbehaviour, ok := evidence.(exported.Misbehaviour)
if !ok {
return types.ErrInvalidEvidence
}
return k.CheckMisbehaviourAndUpdateState(ctx, misbehaviour)
}
}