Skip to content

Commit

Permalink
Merge branch 'gnosis:feat/v0.2.0' into feat/add-proofcast-adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmp01 authored Jun 13, 2024
2 parents e56155e + b944a07 commit a69d98f
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 1,202 deletions.
14 changes: 3 additions & 11 deletions packages/evm/contracts/Hashi.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,8 @@ contract Hashi is IHashi {
uint256 threshold,
IAdapter[] calldata adapters
) external view returns (bool) {
if (adapters.length == 0) revert NoAdaptersGiven();
if (threshold > adapters.length || threshold == 0) revert InvalidThreshold(threshold, adapters.length);

bytes32[] memory hashes = new bytes32[](adapters.length);
for (uint256 i = 0; i < adapters.length; ) {
hashes[i] = adapters[i].getHash(domain, id);
unchecked {
++i;
}
}
bytes32[] memory hashes = getHashesFromAdapters(domain, id, adapters);

for (uint256 i = 0; i < hashes.length; ) {
if (i > hashes.length - threshold) break;
Expand Down Expand Up @@ -87,7 +79,7 @@ contract Hashi is IHashi {
}

/// @inheritdoc IHashi
function getHashFromAdapter(uint256 domain, uint256 id, IAdapter adapter) public view returns (bytes32) {
function getHashFromAdapter(uint256 domain, uint256 id, IAdapter adapter) external view returns (bytes32) {
return adapter.getHash(domain, id);
}

Expand All @@ -100,7 +92,7 @@ contract Hashi is IHashi {
if (adapters.length == 0) revert NoAdaptersGiven();
bytes32[] memory hashes = new bytes32[](adapters.length);
for (uint256 i = 0; i < adapters.length; ) {
hashes[i] = getHashFromAdapter(domain, id, adapters[i]);
hashes[i] = adapters[i].getHash(domain, id);
unchecked {
++i;
}
Expand Down
22 changes: 9 additions & 13 deletions packages/evm/contracts/Yaho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ contract Yaho is IYaho, MessageIdCalculator, MessageHashCalculator {
IAdapter[] calldata adapters
) external returns (uint256) {
_checkReportersAndAdapters(threshold, reporters, adapters);
(uint256 messageId, ) = _dispatchMessage(targetChainId, threshold, receiver, data, reporters, adapters);
(uint256 messageId, bytes32 messageHash) = _dispatchMessage(
targetChainId,
threshold,
receiver,
data,
reporters,
adapters
);
_pendingMessageHashes[messageId] = messageHash;
return messageId;
}

Expand Down Expand Up @@ -84,7 +92,6 @@ contract Yaho is IYaho, MessageIdCalculator, MessageHashCalculator {
}

bytes32[] memory reportersReceipts = new bytes32[](reporters.length);
_resetPendingMessageHashesByMessageIds(messageIds);
reportersReceipts = _dispatchMessagesToAdapters(targetChainId, messageIds, messageHashes, reporters, adapters);
return (messageIds, reportersReceipts);
}
Expand Down Expand Up @@ -160,7 +167,6 @@ contract Yaho is IYaho, MessageIdCalculator, MessageHashCalculator {
);
bytes32 messageHash = calculateMessageHash(message);
uint256 messageId = calculateMessageId(block.chainid, address(this), messageHash);
_pendingMessageHashes[messageId] = messageHash;
unchecked {
++currentNonce;
}
Expand All @@ -179,7 +185,6 @@ contract Yaho is IYaho, MessageIdCalculator, MessageHashCalculator {
bytes32[] memory messageHashes = new bytes32[](1);
messageIds[0] = messageId;
messageHashes[0] = messageHash;
_resetPendingMessageHashesByMessageIds(messageIds);
return _dispatchMessagesToAdapters(targetChainId, messageIds, messageHashes, reporters, adapters);
}

Expand All @@ -204,13 +209,4 @@ contract Yaho is IYaho, MessageIdCalculator, MessageHashCalculator {

return reportersReceipts;
}

function _resetPendingMessageHashesByMessageIds(uint256[] memory messageIds) internal {
for (uint256 i = 0; i < messageIds.length; ) {
delete _pendingMessageHashes[messageIds[i]];
unchecked {
++i;
}
}
}
}
56 changes: 28 additions & 28 deletions packages/evm/contracts/adapters/DendrETH/DendrETHAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@ import { SSZ } from "../Telepathy/libraries/SimpleSerialize.sol";
import { BlockHashAdapter } from "../BlockHashAdapter.sol";

contract DendrETHAdapter is BlockHashAdapter {
uint256 public immutable SOURCE_CHAIN_ID;
address public immutable DENDRETH;

error InvalidUpdate();
error BlockHeaderNotAvailable(uint256 slot);
error InvalidBlockNumberProof();
error InvalidBlockHashProof();

address public immutable dendrETHAddress;

constructor(address _dendrETHAddress) {
dendrETHAddress = _dendrETHAddress;
constructor(uint256 sourceChainId, address dendreth) {
SOURCE_CHAIN_ID = sourceChainId;
DENDRETH = dendreth;
}

/// @notice Stores the block header for a given block only if it exists
// in the DendrETH Light Client for the chainId.
// in the DendrETH Light Client for the SOURCE_CHAIN_ID.
function storeBlockHeader(
uint32 _chainId,
uint64 _slot,
uint256 _blockNumber,
bytes32[] calldata _blockNumberProof,
bytes32 _blockHash,
bytes32[] calldata _blockHashProof
uint64 slot,
uint256 blockNumber,
bytes32[] calldata blockNumberProof,
bytes32 blockHash,
bytes32[] calldata blockHashProof
) external {
ILightClient lightClient = ILightClient(dendrETHAddress);
ILightClient lightClient = ILightClient(DENDRETH);

uint256 currentIndex = lightClient.currentIndex();
uint256 i = currentIndex;
bool found = false;

do {
if (_slot == lightClient.optimisticSlots(i)) {
if (slot == lightClient.optimisticSlots(i)) {
found = true;
break;
}
Expand All @@ -45,51 +46,50 @@ contract DendrETHAdapter is BlockHashAdapter {
} while (i != currentIndex);

if (!found) {
revert BlockHeaderNotAvailable(_slot);
revert BlockHeaderNotAvailable(slot);
}

bytes32 blockHeaderRoot = lightClient.optimisticHeaders(i);

if (!SSZ.verifyBlockNumber(_blockNumber, _blockNumberProof, blockHeaderRoot)) {
if (!SSZ.verifyBlockNumber(blockNumber, blockNumberProof, blockHeaderRoot)) {
revert InvalidBlockNumberProof();
}

if (!SSZ.verifyBlockHash(_blockHash, _blockHashProof, blockHeaderRoot)) {
if (!SSZ.verifyBlockHash(blockHash, blockHashProof, blockHeaderRoot)) {
revert InvalidBlockHashProof();
}

_storeHash(uint256(_chainId), _blockNumber, _blockHash);
_storeHash(SOURCE_CHAIN_ID, blockNumber, blockHash);
}

/// @notice Updates DendrETH Light client and stores the given block
// for the update
function storeBlockHeader(
uint32 _chainId,
uint64 _slot,
uint256 _blockNumber,
bytes32[] calldata _blockNumberProof,
bytes32 _blockHash,
bytes32[] calldata _blockHashProof,
uint64 slot,
uint256 blockNumber,
bytes32[] calldata blockNumberProof,
bytes32 blockHash,
bytes32[] calldata blockHashProof,
LightClientUpdate calldata update
) external {
ILightClient lightClient = ILightClient(dendrETHAddress);
ILightClient lightClient = ILightClient(DENDRETH);

lightClient.light_client_update(update);

if (lightClient.optimisticHeaderSlot() != _slot) {
if (lightClient.optimisticHeaderSlot() != slot) {
revert InvalidUpdate();
}

bytes32 blockHeaderRoot = lightClient.optimisticHeaderRoot();

if (!SSZ.verifyBlockNumber(_blockNumber, _blockNumberProof, blockHeaderRoot)) {
if (!SSZ.verifyBlockNumber(blockNumber, blockNumberProof, blockHeaderRoot)) {
revert InvalidBlockNumberProof();
}

if (!SSZ.verifyBlockHash(_blockHash, _blockHashProof, blockHeaderRoot)) {
if (!SSZ.verifyBlockHash(blockHash, blockHashProof, blockHeaderRoot)) {
revert InvalidBlockHashProof();
}

_storeHash(uint256(_chainId), _blockNumber, _blockHash);
_storeHash(SOURCE_CHAIN_ID, blockNumber, blockHash);
}
}
2 changes: 1 addition & 1 deletion packages/evm/contracts/interfaces/IShuSho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface IShuSho {
error CountCannotBeZero();
error DuplicateHashiAddress(IHashi hashi);
error DuplicateOrOutOfOrderAdapters(IAdapter adapterOne, IAdapter adapterTwo);
error DuplicateThreashold(uint256 threshold);
error DuplicateThreshold(uint256 threshold);
error InvalidAdapter(IAdapter adapter);
error InvalidThreshold(uint256 threshold);
error NoAdaptersEnabled(uint256 domain);
Expand Down
Loading

0 comments on commit a69d98f

Please sign in to comment.