@@ -54,7 +54,7 @@ func (k Keeper) CreateClient(
5454}
5555
5656// UpdateClient updates the consensus state and the state root from a provided header.
57- func (k Keeper ) UpdateClient (ctx sdk.Context , clientID string , header exported.ClientMessage ) error {
57+ func (k Keeper ) UpdateClient (ctx sdk.Context , clientID string , clientMsg exported.ClientMessage ) error {
5858 clientState , found := k .GetClientState (ctx , clientID )
5959 if ! found {
6060 return sdkerrors .Wrapf (types .ErrClientNotFound , "cannot update client with ID %s" , clientID )
@@ -66,54 +66,21 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.C
6666 return sdkerrors .Wrapf (types .ErrClientNotActive , "cannot update client (%s) with status %s" , clientID , status )
6767 }
6868
69- // Any writes made in CheckHeaderAndUpdateState are persisted on both valid updates and misbehaviour updates.
70- // Light client implementations are responsible for writing the correct metadata (if any) in either case.
71- newClientState , newConsensusState , err := clientState .CheckHeaderAndUpdateState (ctx , k .cdc , clientStore , header )
72- if err != nil {
73- return sdkerrors .Wrapf (err , "cannot update client with ID %s" , clientID )
74- }
75-
76- // emit the full header in events
77- var (
78- headerStr string
79- consensusHeight exported.Height
80- )
81- if header != nil {
82- // Marshal the Header as an Any and encode the resulting bytes to hex.
83- // This prevents the event value from containing invalid UTF-8 characters
84- // which may cause data to be lost when JSON encoding/decoding.
85- headerStr = hex .EncodeToString (types .MustMarshalClientMessage (k .cdc , header ))
86- // set default consensus height with header height
87- consensusHeight = header .GetHeight ()
88-
69+ if err := clientState .VerifyClientMessage (ctx , k .cdc , clientStore , clientMsg ); err != nil {
70+ return err
8971 }
9072
91- // set new client state regardless of if update is valid update or misbehaviour
92- k .SetClientState (ctx , clientID , newClientState )
93- // If client state is not frozen after clientState CheckHeaderAndUpdateState,
94- // then update was valid. Write the update state changes, and set new consensus state.
95- // Else the update was proof of misbehaviour and we must emit appropriate misbehaviour events.
96- if status := newClientState .Status (ctx , clientStore , k .cdc ); status != exported .Frozen {
97- // if update is not misbehaviour then update the consensus state
98- k .SetClientConsensusState (ctx , clientID , header .GetHeight (), newConsensusState )
99-
100- k .Logger (ctx ).Info ("client state updated" , "client-id" , clientID , "height" , consensusHeight .String ())
73+ // Marshal the ClientMessage as an Any and encode the resulting bytes to hex.
74+ // This prevents the event value from containing invalid UTF-8 characters
75+ // which may cause data to be lost when JSON encoding/decoding.
76+ clientMsgStr := hex .EncodeToString (types .MustMarshalClientMessage (k .cdc , clientMsg ))
10177
102- defer func () {
103- telemetry .IncrCounterWithLabels (
104- []string {"ibc" , "client" , "update" },
105- 1 ,
106- []metrics.Label {
107- telemetry .NewLabel (types .LabelClientType , clientState .ClientType ()),
108- telemetry .NewLabel (types .LabelClientID , clientID ),
109- telemetry .NewLabel (types .LabelUpdateType , "msg" ),
110- },
111- )
112- }()
78+ // set default consensus height with header height
79+ consensusHeight := clientMsg .GetHeight ()
11380
114- // emitting events in the keeper emits for both begin block and handler client updates
115- EmitUpdateClientEvent ( ctx , clientID , newClientState , consensusHeight , headerStr )
116- } else {
81+ foundMisbehaviour := clientState . CheckForMisbehaviour ( ctx , k . cdc , clientStore , clientMsg )
82+ if foundMisbehaviour {
83+ clientState . UpdateStateOnMisbehaviour ( ctx , k . cdc , clientStore , clientMsg )
11784
11885 k .Logger (ctx ).Info ("client frozen due to misbehaviour" , "client-id" , clientID )
11986
@@ -129,9 +96,30 @@ func (k Keeper) UpdateClient(ctx sdk.Context, clientID string, header exported.C
12996 )
13097 }()
13198
132- EmitSubmitMisbehaviourEventOnUpdate (ctx , clientID , newClientState , consensusHeight , headerStr )
99+ EmitSubmitMisbehaviourEventOnUpdate (ctx , clientID , clientState .ClientType (), consensusHeight , clientMsgStr )
100+
101+ return nil
133102 }
134103
104+ clientState .UpdateState (ctx , k .cdc , clientStore , clientMsg )
105+
106+ k .Logger (ctx ).Info ("client state updated" , "client-id" , clientID , "height" , consensusHeight .String ())
107+
108+ defer func () {
109+ telemetry .IncrCounterWithLabels (
110+ []string {"ibc" , "client" , "update" },
111+ 1 ,
112+ []metrics.Label {
113+ telemetry .NewLabel (types .LabelClientType , clientState .ClientType ()),
114+ telemetry .NewLabel (types .LabelClientID , clientID ),
115+ telemetry .NewLabel (types .LabelUpdateType , "msg" ),
116+ },
117+ )
118+ }()
119+
120+ // emitting events in the keeper emits for both begin block and handler client updates
121+ EmitUpdateClientEvent (ctx , clientID , clientState .ClientType (), consensusHeight , clientMsgStr )
122+
135123 return nil
136124}
137125
0 commit comments