-
Notifications
You must be signed in to change notification settings - Fork 628
/
client.go
146 lines (110 loc) · 6.3 KB
/
client.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package keeper
import (
"context"
errorsmod "cosmossdk.io/errors"
"github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
"github.com/cosmos/ibc-go/v9/modules/core/exported"
"github.com/cosmos/ibc-go/v9/modules/core/internal/telemetry"
)
// CreateClient generates a new client identifier and invokes the associated light client module in order to
// initialize a new client. An isolated prefixed store will be reserved for this client using the generated
// client identifier. The light client module is responsible for setting any client-specific data in the store
// via the Initialize method. This includes the client state, initial consensus state and any associated
// metadata. The generated client identifier will be returned if a client was successfully initialized.
func (k *Keeper) CreateClient(ctx context.Context, clientType string, clientState, consensusState []byte) (string, error) {
if clientType == exported.Localhost {
return "", errorsmod.Wrapf(types.ErrInvalidClientType, "cannot create client of type: %s", clientType)
}
clientID := k.GenerateClientIdentifier(ctx, clientType)
clientModule, err := k.Route(ctx, clientID)
if err != nil {
return "", err
}
if err := clientModule.Initialize(ctx, clientID, clientState, consensusState); err != nil {
return "", err
}
if status := clientModule.Status(ctx, clientID); status != exported.Active {
return "", errorsmod.Wrapf(types.ErrClientNotActive, "cannot create client (%s) with status %s", clientID, status)
}
initialHeight := clientModule.LatestHeight(ctx, clientID)
k.Logger.Info("client created at height", "client-id", clientID, "height", initialHeight.String())
defer telemetry.ReportCreateClient(clientType)
if err := k.emitCreateClientEvent(ctx, clientID, clientType, initialHeight); err != nil {
return "", err
}
return clientID, nil
}
// UpdateClient updates the consensus state and the state root from a provided header.
func (k *Keeper) UpdateClient(ctx context.Context, clientID string, clientMsg exported.ClientMessage) error {
clientModule, err := k.Route(ctx, clientID)
if err != nil {
return err
}
if status := clientModule.Status(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(types.ErrClientNotActive, "cannot update client (%s) with status %s", clientID, status)
}
if err := clientModule.VerifyClientMessage(ctx, clientID, clientMsg); err != nil {
return err
}
foundMisbehaviour := clientModule.CheckForMisbehaviour(ctx, clientID, clientMsg)
if foundMisbehaviour {
clientModule.UpdateStateOnMisbehaviour(ctx, clientID, clientMsg)
k.Logger.Info("client frozen due to misbehaviour", "client-id", clientID)
clientType := types.MustParseClientIdentifier(clientID)
defer telemetry.ReportUpdateClient(foundMisbehaviour, clientType, clientID)
return k.emitSubmitMisbehaviourEvent(ctx, clientID, clientType)
}
consensusHeights := clientModule.UpdateState(ctx, clientID, clientMsg)
k.Logger.Info("client state updated", "client-id", clientID, "heights", consensusHeights)
clientType := types.MustParseClientIdentifier(clientID)
defer telemetry.ReportUpdateClient(foundMisbehaviour, clientType, clientID)
return k.emitUpdateClientEvent(ctx, clientID, clientType, consensusHeights, k.cdc, clientMsg)
}
// UpgradeClient upgrades the client to a new client state if this new client was committed to
// by the old client at the specified upgrade height
func (k *Keeper) UpgradeClient(ctx context.Context, clientID string, upgradedClient, upgradedConsState, upgradeClientProof, upgradeConsensusStateProof []byte) error {
clientModule, err := k.Route(ctx, clientID)
if err != nil {
return err
}
if status := clientModule.Status(ctx, clientID); status != exported.Active {
return errorsmod.Wrapf(types.ErrClientNotActive, "cannot upgrade client (%s) with status %s", clientID, status)
}
if err := clientModule.VerifyUpgradeAndUpdateState(ctx, clientID, upgradedClient, upgradedConsState, upgradeClientProof, upgradeConsensusStateProof); err != nil {
return errorsmod.Wrapf(err, "cannot upgrade client with ID %s", clientID)
}
latestHeight := clientModule.LatestHeight(ctx, clientID)
k.Logger.Info("client state upgraded", "client-id", clientID, "height", latestHeight.String())
clientType := types.MustParseClientIdentifier(clientID)
defer telemetry.ReportUpgradeClient(clientType, clientID)
return k.emitUpgradeClientEvent(ctx, clientID, clientType, latestHeight)
}
// RecoverClient will invoke the light client module associated with the subject clientID requesting it to
// recover the subject client given a substitute client identifier. The light client implementation
// is responsible for validating the parameters of the substitute (ensuring they match the subject's parameters)
// as well as copying the necessary consensus states from the substitute to the subject client store.
// The substitute must be Active and the subject must not be Active.
func (k *Keeper) RecoverClient(ctx context.Context, subjectClientID, substituteClientID string) error {
clientModule, err := k.Route(ctx, subjectClientID)
if err != nil {
return errorsmod.Wrap(types.ErrRouteNotFound, subjectClientID)
}
if status := clientModule.Status(ctx, subjectClientID); status == exported.Active {
return errorsmod.Wrapf(types.ErrInvalidRecoveryClient, "cannot recover subject client (%s) with status %s", subjectClientID, status)
}
if status := clientModule.Status(ctx, substituteClientID); status != exported.Active {
return errorsmod.Wrapf(types.ErrClientNotActive, "cannot recover client using substitute client (%s) with status %s", substituteClientID, status)
}
subjectLatestHeight := clientModule.LatestHeight(ctx, subjectClientID)
substituteLatestHeight := clientModule.LatestHeight(ctx, substituteClientID)
if subjectLatestHeight.GTE(substituteLatestHeight) {
return errorsmod.Wrapf(types.ErrInvalidHeight, "subject client state latest height is greater or equal to substitute client state latest height (%s >= %s)", subjectLatestHeight, substituteLatestHeight)
}
if err := clientModule.RecoverClient(ctx, subjectClientID, substituteClientID); err != nil {
return err
}
k.Logger.Info("client recovered", "client-id", subjectClientID)
clientType := types.MustParseClientIdentifier(subjectClientID)
defer telemetry.ReportRecoverClient(clientType, subjectClientID)
return k.emitRecoverClientEvent(ctx, subjectClientID, clientType)
}