forked from banthony101/union
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIBCClient.sol
More file actions
134 lines (126 loc) · 4.87 KB
/
Copy pathIBCClient.sol
File metadata and controls
134 lines (126 loc) · 4.87 KB
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
pragma solidity ^0.8.27;
import "./ILightClient.sol";
import "../25-handler/IBCMsgs.sol";
import "../24-host/IBCStore.sol";
import "../24-host/IBCCommitment.sol";
import "../02-client/IIBCClient.sol";
library IBCClientLib {
event RegisterClient(
string indexed clientTypeIndex, string clientType, address clientAddress
);
event CreateClient(
string indexed clientTypeIndex,
string clientType,
uint32 indexed clientId,
string counterpartyChainId
);
event UpdateClient(uint32 indexed clientId, uint64 height);
event Misbehaviour(uint32 indexed clientId);
}
/**
* @dev IBCClient is a router contract that forward calls to clients implementing [ICS-2](https://github.com/cosmos/ibc/tree/main/spec/core/ics-002-client-semantics).
*/
abstract contract IBCClient is IBCStore, IIBCClient {
/**
* @dev registerClient registers a new client type into the client registry
*/
function registerClient(
string calldata clientType,
ILightClient client
) external override restricted {
if (address(clientRegistry[clientType]) != address(0)) {
revert IBCErrors.ErrClientTypeAlreadyExists();
}
clientRegistry[clientType] = address(client);
emit IBCClientLib.RegisterClient(
clientType, clientType, address(client)
);
}
/**
* @dev createClient creates a new client state and populates it with a given consensus state
*/
function createClient(
IBCMsgs.MsgCreateClient calldata msg_
) external override restricted returns (uint32) {
address clientImpl = clientRegistry[msg_.clientType];
if (clientImpl == address(0)) {
revert IBCErrors.ErrClientTypeNotFound();
}
uint32 clientId = generateClientIdentifier();
clientTypes[clientId] = msg_.clientType;
clientImpls[clientId] = clientImpl;
(ConsensusStateUpdate memory update, string memory counterpartyChainId)
= ILightClient(clientImpl).createClient(
msg.sender,
clientId,
msg_.clientStateBytes,
msg_.consensusStateBytes,
msg_.relayer
);
commitments[IBCCommitment.clientStateCommitmentKey(clientId)] =
update.clientStateCommitment;
commitments[IBCCommitment.consensusStateCommitmentKey(
clientId, update.height
)] = update.consensusStateCommitment;
emit IBCClientLib.CreateClient(
msg_.clientType, msg_.clientType, clientId, counterpartyChainId
);
return clientId;
}
/**
* @dev updateClient updates the consensus state and the state root from a provided header
*/
function updateClient(
IBCMsgs.MsgUpdateClient calldata msg_
) external override restricted {
ConsensusStateUpdate memory update = getClientInternal(msg_.clientId)
.updateClient(
msg.sender, msg_.clientId, msg_.clientMessage, msg_.relayer
);
commitments[IBCCommitment.clientStateCommitmentKey(msg_.clientId)] =
update.clientStateCommitment;
commitments[IBCCommitment.consensusStateCommitmentKey(
msg_.clientId, update.height
)] = update.consensusStateCommitment;
emit IBCClientLib.UpdateClient(msg_.clientId, update.height);
}
/**
* @dev forceUpdateClient forcibly updates the consensus state and the state root from a provided header
*/
function forceUpdateClient(
IBCMsgs.MsgForceUpdateClient calldata msg_
) external restricted {
ConsensusStateUpdate memory update = IForceLightClient(
address(getClientInternal(msg_.clientId))
).forceUpdateClient(
msg.sender,
msg_.clientId,
msg_.clientStateBytes,
msg_.consensusStateBytes
);
commitments[IBCCommitment.clientStateCommitmentKey(msg_.clientId)] =
update.clientStateCommitment;
commitments[IBCCommitment.consensusStateCommitmentKey(
msg_.clientId, update.height
)] = update.consensusStateCommitment;
emit IBCClientLib.UpdateClient(msg_.clientId, update.height);
}
/**
* @dev misbehaviour submits a misbehaviour to the client for it to take action if it is correct
*/
function misbehaviour(
IBCMsgs.MsgMisbehaviour calldata msg_
) external override restricted {
getClientInternal(msg_.clientId).misbehaviour(
msg.sender, msg_.clientId, msg_.clientMessage, msg_.relayer
);
emit IBCClientLib.Misbehaviour(msg_.clientId);
}
function generateClientIdentifier() internal returns (uint32) {
uint32 nextClientSequence =
uint32(uint256(commitments[nextClientSequencePath]));
commitments[nextClientSequencePath] =
bytes32(uint256(nextClientSequence + 1));
return nextClientSequence;
}
}