Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: multihop #84

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions packages/evm/contracts/Bouncer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.20;

import { IAdapter } from "./interfaces/IAdapter.sol";
import { IReporter } from "./interfaces/IReporter.sol";
import { IYaho } from "./interfaces/IYaho.sol";
import { IBouncer } from "./interfaces/IBouncer.sol";
import { HopDecoder } from "./libraries/HopDecoder.sol";

contract Bouncer is IBouncer {
address public immutable YAHO;
address public immutable YARU;

constructor(address yaho, address yaru) {
YAHO = yaho;
YARU = yaru;
}

function onMessage(
uint256,
uint256 sourceChainId,
address sender,
uint256 threshold,
IAdapter[] calldata adapters,
bytes calldata data
) external returns (bytes memory) {
if (msg.sender != YARU) revert NotYaru();

(
,
uint8 hopsNonce,
,
,
uint256 nextChainId,
address receiver,
uint256 expectedSourceChainId,
address expectedSender,
uint256 expectedThreshold,
bytes32 expectedAdaptersHash,
uint256 nextThreshold,
IReporter[] memory nextReporters,
IAdapter[] memory nextAdapters,
bytes memory message
) = HopDecoder.decodeCurrentHop(data);

if (sourceChainId != expectedSourceChainId) revert InvalidSourceChainId();
if (sender != expectedSender) revert InvalidSender();
if (threshold < expectedThreshold) revert InvalidThreshold();
if (keccak256(abi.encodePacked(adapters)) != expectedAdaptersHash) revert InvalidAdapters();

bytes memory dataWithUpdatedNonce = abi.encodePacked(
data[:7 + message.length],
bytes1(hopsNonce + 1),
data[7 + 1 + message.length:]
);
IYaho(YAHO).dispatchMessage(
nextChainId,
nextThreshold,
receiver,
dataWithUpdatedNonce,
nextReporters,
nextAdapters
);

return abi.encodePacked(true);
}
}
15 changes: 15 additions & 0 deletions packages/evm/contracts/interfaces/IBouncer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.0;

import { IJushin } from "./IJushin.sol";

/**
* @title IBouncer
*/
interface IBouncer is IJushin {
error InvalidAdapters();
error InvalidSender();
error InvalidSourceChainId();
error InvalidThreshold();
error NotYaru();
}
97 changes: 97 additions & 0 deletions packages/evm/contracts/libraries/HopDecoder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
pragma solidity ^0.8.20;

import { IAdapter } from "../interfaces/IAdapter.sol";
import { IReporter } from "../interfaces/IReporter.sol";

library HopDecoder {
function decodeCurrentHop(
bytes calldata data
)
internal
pure
returns (
bytes4 magic,
uint8 hopsNonce,
uint8 hopsCount,
bytes8 chainProtocol,
uint256 chainId,
address receiver,
uint256 expectedSourceChainId,
address expectedSender,
uint32 expectedThreshold,
bytes32 expectedAdaptersHash,
uint32 nextThreshold,
IReporter[] memory nextReporters,
IAdapter[] memory nextAdapters,
bytes memory message
)
{
magic = bytes4(data[:4]);

uint24 messageLength = uint24(bytes3(data[4:7]));
message = data[7:7 + messageLength];

uint256 a = 7 + messageLength;
uint256 b = a + 1;
uint256 c = b + 1;

hopsNonce = uint8(bytes1(data[a:b]));
hopsCount = uint8(bytes1(data[b:c]));

uint32 hopBytesToSkip = 0;
for (uint256 k = 0; k < hopsNonce; ) {
hopBytesToSkip += uint32(bytes4(data[c + k * 4:c + (k * 4) + 4]));
unchecked {
++k;
}
}

uint256 hopStart = c + hopBytesToSkip + ((hopsNonce + 1) * 4);
uint256 d = hopStart + 8;
uint256 e = hopStart + 24;
uint256 f = hopStart + 56;
uint256 g = hopStart + 72;
uint256 h = hopStart + 104;
uint256 i = hopStart + 108;
uint256 l = hopStart + 140;
uint256 m = hopStart + 144;
uint256 n = hopStart + 148;

chainProtocol = bytes8(data[hopStart:d]);
chainId = uint128(bytes16(data[d:e]));
receiver = address(uint160(uint256(bytes32(data[e:f]))));
expectedSourceChainId = uint128(bytes16(data[f:g]));
expectedSender = address(uint160(uint256(bytes32(data[g:h]))));
expectedThreshold = uint32(bytes4(data[h:i]));
expectedAdaptersHash = bytes32(data[i:l]);
nextThreshold = uint32(bytes4(data[l:m]));

uint32 nextReportersLength = uint32(bytes4(data[m:n]));
nextReporters = new IReporter[](nextReportersLength);
for (uint256 k = 0; k < nextReportersLength; ) {
nextReporters[k] = IReporter(address(uint160(uint256(bytes32(data[n + (k * 32):n + (k * 32) + 32])))));
unchecked {
++k;
}
}

uint256 o = n + (nextReportersLength * 32);
uint256 p = o + 4;
uint32 nextAdaptersLength = uint32(bytes4(data[o:p]));
nextAdapters = new IAdapter[](nextAdaptersLength);
for (uint256 k = 0; k < nextAdaptersLength; ) {
nextAdapters[k] = IAdapter(address(uint160(uint256(bytes32(data[p + (k * 32):p + (k * 32) + 32])))));
unchecked {
++k;
}
}
}

function decodeHeaderAndMessage(
bytes calldata data
) internal pure returns (bytes memory header, bytes memory message) {
uint24 messageLength = uint24(bytes3(data[4:7]));
message = data[7:7 + messageLength];
header = data[7 + messageLength:];
}
}
52 changes: 52 additions & 0 deletions packages/evm/contracts/test/HopReceiver.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.20;

import { IJushin } from "../interfaces/IJushin.sol";
import { IAdapter } from "../interfaces/IAdapter.sol";
import { HopDecoder } from "../libraries/HopDecoder.sol";

contract HopReceiver is IJushin {
address public yaru;
uint256 public expectedSourceChainId;
address public expectedSender;
uint256 public expectedThreshold;
bytes32 public expectedAdaptersHash;
bytes32 public expectedHeaderHash;

event MessageReceived(bytes message);

function setConfigs(
address yaru_,
uint256 expectedSourceChainId_,
address expectedSender_,
uint256 expectedThreshold_,
bytes32 expectedAdaptersHash_,
bytes32 expectedHeaderHash_
) external {
yaru = yaru_;
expectedSourceChainId = expectedSourceChainId_;
expectedSender = expectedSender_;
expectedThreshold = expectedThreshold_;
expectedAdaptersHash = expectedAdaptersHash_;
expectedHeaderHash = expectedHeaderHash_;
}

function onMessage(
uint256,
uint256 sourceChainId,
address sender,
uint256 threshold,
IAdapter[] calldata adapters,
bytes calldata data
) external returns (bytes memory) {
require(msg.sender == yaru, "!yaru");
require(sourceChainId == expectedSourceChainId, "!expectedSourceChainId");
require(sender == expectedSender, "!expectedSender");
require(threshold == expectedThreshold, "!expectedThreshold");
require(keccak256(abi.encodePacked(adapters)) == expectedAdaptersHash, "!expectedAdaptersHash");
(bytes memory header, bytes memory message) = HopDecoder.decodeHeaderAndMessage(data);
require(keccak256(header) == expectedHeaderHash, "!expectedHeaderHash");
emit MessageReceived(message);
return abi.encodePacked(true);
}
}
Loading
Loading