-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathIBCStore.sol
97 lines (83 loc) · 3.11 KB
/
IBCStore.sol
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
pragma solidity ^0.8.27;
import
"@openzeppelin-upgradeable/contracts/access/manager/AccessManagedUpgradeable.sol";
import "../02-client/ILightClient.sol";
import "../05-port/IIBCModule.sol";
import "../Types.sol";
library IBCStoreLib {
bytes public constant WASMD_MODULE_STORE_KEY = bytes("wasm");
bytes1 public constant WASMD_CONTRACT_STORE_PREFIX = 0x03;
bytes1 public constant IBC_UNION_COSMWASM_COMMITMENT_PREFIX = 0x00;
uint256 public constant IBC_UNION_EVM_COMMITMENT_SLOT = 0;
}
abstract contract IBCStore is AccessManagedUpgradeable {
// Commitments
// keccak256(IBC-compatible-store-path) => keccak256(IBC-compatible-commitment)
mapping(bytes32 => bytes32) public commitments;
// ClientType -> Address
mapping(string => address) public clientRegistry;
// ClientId -> ClientType
mapping(uint32 => string) public clientTypes;
// ClientId -> Address
mapping(uint32 => address) public clientImpls;
// ConnectionId -> Connection
mapping(uint32 => IBCConnection) public connections;
// ChannelId -> Channel
mapping(uint32 => IBCChannel) public channels;
// ChannelId -> PortId
mapping(uint32 => address) public channelOwner;
// Sequences for identifier
bytes32 constant nextClientSequencePath = keccak256("nextClientSequence");
bytes32 constant nextConnectionSequencePath =
keccak256("nextConnectionSequence");
bytes32 constant nextChannelSequencePath = keccak256("nextChannelSequence");
function getClient(
uint32 clientId
) public view returns (ILightClient) {
return getClientInternal(clientId);
}
function getClientInternal(
uint32 clientId
) internal view returns (ILightClient) {
address clientImpl = clientImpls[clientId];
if (clientImpl == address(0)) {
revert IBCErrors.ErrClientNotFound();
}
return ILightClient(clientImpl);
}
function lookupModuleByChannel(
uint32 channelId
) internal view virtual returns (IIBCModule) {
address module = channelOwner[channelId];
if (module == address(0)) {
revert IBCErrors.ErrModuleNotFound();
}
return IIBCModule(module);
}
function claimChannel(address portId, uint32 channelId) internal {
channelOwner[channelId] = portId;
}
function authenticateChannelOwner(
uint32 channelId
) internal view returns (bool) {
return msg.sender == channelOwner[channelId];
}
function ensureConnectionState(
uint32 connectionId
) internal view returns (uint32) {
IBCConnection storage connection = connections[connectionId];
if (connection.state != IBCConnectionState.Open) {
revert IBCErrors.ErrInvalidConnectionState();
}
return connection.clientId;
}
function ensureChannelState(
uint32 channelId
) internal view returns (IBCChannel storage) {
IBCChannel storage channel = channels[channelId];
if (channel.state != IBCChannelState.Open) {
revert IBCErrors.ErrInvalidChannelState();
}
return channel;
}
}