Skip to content

Commit

Permalink
feat: implemented 02-client
Browse files Browse the repository at this point in the history
  • Loading branch information
srdtrk committed Jul 23, 2024
1 parent 2b41eed commit 794605c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
53 changes: 53 additions & 0 deletions src/ICS02Client.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25;

import { IICS02Client } from "./interfaces/IICS02Client.sol";
import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { IBCIdentifiers } from "./utils/IBCIdentifiers.sol";
import { ILightClient } from "./interfaces/ILightClient.sol";
import { IICS02ClientErrors } from "./errors/IICS02ClientErrors.sol";

contract ICS02Client is IICS02Client, IICS02ClientErrors {
/// @dev clientId => counterpartyInfo
mapping(string => CounterpartyInfo) private counterpartyInfos;
/// @dev clientId => client
mapping(string => ILightClient) private clients;
/// @dev clientType => nextClientSeq
mapping(string => uint32) private nextClientSeq;

/// @notice Generates the next client identifier
/// @param clientType The client type
/// @return The next client identifier
function getNextClientId(string calldata clientType) private returns (string memory) {
if (!IBCIdentifiers.validateClientType(clientType)) {
revert IBCInvalidClientType(clientType);
}

uint32 seq = nextClientSeq[clientType];
nextClientSeq[clientType] = seq + 1;
return string.concat(clientType, "-", Strings.toString(seq));
}

function getCounterparty(string calldata clientId) external view returns (CounterpartyInfo memory) {
return counterpartyInfos[clientId];
}

function getClient(string calldata clientId) external view returns (ILightClient) {
return clients[clientId];
}

function addClient(string calldata clientType, CounterpartyInfo calldata counterpartyInfo, address client) external returns (string memory) {
string memory clientId = getNextClientId(clientType);
clients[clientId] = ILightClient(client);
counterpartyInfos[clientId] = counterpartyInfo;
return clientId;
}

function updateClient(string calldata clientId, bytes calldata updateMsg) external returns (ILightClient.UpdateResult) {
return clients[clientId].updateClient(updateMsg);
}

function submitMisbehaviour(string calldata clientId, bytes calldata misbehaviourMsg) external {
clients[clientId].misbehaviour(misbehaviourMsg);
}
}
2 changes: 2 additions & 0 deletions src/ICS26Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { IBCIdentifiers } from "./utils/IBCIdentifiers.sol";
import { IIBCAppCallbacks } from "./msgs/IIBCAppCallbacks.sol";
import { ICS24Host } from "./utils/ICS24Host.sol";

/// @title IBC Eureka Router
/// @notice ICS26Router is the router for the IBC Eureka protocol
contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors {
mapping(string => IIBCApp) private apps;
IICS02Client private ics02Client;
Expand Down
7 changes: 7 additions & 0 deletions src/errors/IICS02ClientErrors.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25;

interface IICS02ClientErrors {
/// @param clientType client type
error IBCInvalidClientType(string clientType);
}
17 changes: 4 additions & 13 deletions src/interfaces/IICS02Client.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,23 @@ interface IICS02Client is IICS02ClientMsgs {
/// @return The counterparty client information
function getCounterparty(string calldata clientId) external view returns (CounterpartyInfo memory);

/// @notice Returns the creator of the client given the client identifier.
/// @param clientId The client identifier
/// @return The address of the client creator
function getCreator(string calldata clientId) external view returns (address);

/// @notice Returns the address of the client contract given the client identifier.
/// @param clientId The client identifier
/// @return The address of the client contract
function getClient(string calldata clientId) external view returns (ILightClient);

/// @notice Adds a client to the client router.
/// @param clientType The client type, e.g., "07-tendermint".
/// @param counterpartyInfo The counterparty client information
/// @param client The address of the client contract
/// @return The client identifier
function addClient(string calldata clientType, address client) external returns (string memory);

/// @notice Adds a counterparty to the client router.
/// @param clientId The client identifier
/// @param counterpartyInfo The counterparty client information
function addCounterparty(string calldata clientId, CounterpartyInfo calldata counterpartyInfo) external;
function addClient(string calldata clientType, CounterpartyInfo calldata counterpartyInfo, address client) external returns (string memory);

/// @notice Updates the client given the client identifier.
/// @param clientId The client identifier
/// @param updateMsg The update message
/// @return The result of the update operation
function routeUpdateClient(
function updateClient(
string calldata clientId,
bytes calldata updateMsg
)
Expand All @@ -47,5 +38,5 @@ interface IICS02Client is IICS02ClientMsgs {
/// @notice Submits misbehaviour to the client with the given client identifier.
/// @param clientId The client identifier
/// @param misbehaviourMsg The misbehaviour message
function routeMisbehaviour(string calldata clientId, bytes calldata misbehaviourMsg) external;
function submitMisbehaviour(string calldata clientId, bytes calldata misbehaviourMsg) external;
}
8 changes: 8 additions & 0 deletions src/utils/IBCIdentifiers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,12 @@ library IBCIdentifiers {
}
return true;
}

/// @notice validateClientType checks if the client type is allowed
function validateClientType(string memory clientType) internal pure returns (bool) {
if (keccak256(bytes(clientType)) == keccak256("07-tendermint")) {
return true;
}
return false;
}
}

0 comments on commit 794605c

Please sign in to comment.