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: add data within relayMessagesToAdapters and dispatchMessagesToAdapters #27

Closed
wants to merge 2 commits into from
Closed
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
46 changes: 29 additions & 17 deletions packages/evm/contracts/Yaho.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,56 @@ contract Yaho is IMessageDispatcher, MessageHashCalculator {

/// @dev Relays hashes of the given messageIds to the given adapters.
/// @param messageIds Array of IDs of the message hashes to relay to the given adapters.
/// @param adapters Array of relay adapter addresses to which hashes should be relayed.
/// @param messageRelays Array of message relays addresses to which hashes should be relayed.
/// @param destinationAdapters Array of oracle adapter addresses to receive hashes.
/// @param data Array of bytes used as optional parameters to pass to the MessageRelay.
/// @return adapterReciepts Reciepts from each of the relay adapters.
function relayMessagesToAdapters(
uint256[] memory messageIds,
address[] memory adapters,
address[] memory destinationAdapters
address[] memory messageRelays,
address[] memory destinationAdapters,
bytes[] calldata data
) external payable returns (bytes32[] memory) {
if (messageIds.length == 0) revert NoMessageIdsGiven(address(this));
if (adapters.length == 0) revert NoAdaptersGiven(address(this));
if (adapters.length != destinationAdapters.length) revert UnequalArrayLengths(address(this));
bytes32[] memory adapterReciepts = new bytes32[](adapters.length);
for (uint256 i = 0; i < adapters.length; i++) {
adapterReciepts[i] = IMessageRelay(adapters[i]).relayMessages(messageIds, destinationAdapters[i]);
if (messageRelays.length == 0) revert NoAdaptersGiven(address(this));
if (messageRelays.length != destinationAdapters.length) revert UnequalArrayLengths(address(this));
bytes32[] memory adapterReciepts = new bytes32[](messageRelays.length);
for (uint256 i = 0; i < messageRelays.length; i++) {
adapterReciepts[i] = IMessageRelay(messageRelays[i]).relayMessages(
messageIds,
destinationAdapters[i],
data[i]
);
}
return adapterReciepts;
}

/// @dev Dispatches an array of messages and relays their hashes to an array of relay adapters.
/// @param messages An array of Messages to be dispatched.
/// @param adapters Array of relay adapter addresses to which hashes should be relayed.
/// @param messageRelays Array of message relays addresses to which hashes should be relayed.
/// @param destinationAdapters Array of oracle adapter addresses to receive hashes.
/// @return messageIds An array of message IDs corresponding to the dispatched messages.
/// @return adapterReciepts Reciepts from each of the relay adapters.
/// @param data Array of bytes used as optional parameters to pass to the MessageRelay.
/// @return adapterReciepts Reciepts from each of the relay.
function dispatchMessagesToAdapters(
Message[] memory messages,
address[] memory adapters,
address[] memory destinationAdapters
Message[] calldata messages,
address[] calldata messageRelays,
address[] calldata destinationAdapters,
bytes[] calldata data
) external payable returns (bytes32[] memory messageIds, bytes32[] memory) {
if (adapters.length == 0) revert NoAdaptersGiven(address(this));
if (messageRelays.length == 0) revert NoAdaptersGiven(address(this));
messageIds = dispatchMessages(messages);
uint256[] memory uintIds = new uint256[](messageIds.length);
for (uint256 i = 0; i < messageIds.length; i++) {
uintIds[i] = uint256(messageIds[i]);
}
bytes32[] memory adapterReciepts = new bytes32[](adapters.length);
for (uint256 i = 0; i < adapters.length; i++) {
adapterReciepts[i] = IMessageRelay(adapters[i]).relayMessages(uintIds, destinationAdapters[i]);
bytes32[] memory adapterReciepts = new bytes32[](messageRelays.length);
for (uint256 i = 0; i < messageRelays.length; i++) {
adapterReciepts[i] = IMessageRelay(messageRelays[i]).relayMessages(
uintIds,
destinationAdapters[i],
data[i]
);
}
return (messageIds, adapterReciepts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,27 @@ contract AMBMessageRelay is IMessageRelay {
yaho = _yaho;
}

function relayMessages(uint256[] memory messageIds, address ambAdapter) public payable returns (bytes32 receipt) {
function relayMessages(
uint256[] calldata messageIds,
address ambAdapter,
bytes calldata data
) public payable returns (bytes32 receipt) {
bytes32[] memory hashes = new bytes32[](messageIds.length);
for (uint256 i = 0; i < messageIds.length; i++) {
uint256 id = messageIds[i];
hashes[i] = yaho.hashes(id);
emit MessageRelayed(address(this), messageIds[i]);
}
bytes memory data = abi.encodeCall(AMBAdapter.storeHashes, (messageIds, hashes));
receipt = amb.requireToPassMessage(ambAdapter, data, 0);

uint256 gas = 350000; // NOTE: 350000 is the default value
if (data.length > 0) {
(gas) = abi.decode(data, (uint256));
}

receipt = amb.requireToPassMessage(
ambAdapter,
abi.encodeCall(AMBAdapter.storeHashes, (messageIds, hashes)),
gas
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ contract SygmaMessageRelayer is SygmaReporter, IMessageRelay {
@param messageIds IDs of the messages to pass over the Sygma bridge.
@param sygmaAdapter Address of the Sygma adapter on the target chain.
*/
function relayMessages(uint256[] memory messageIds, address sygmaAdapter) public payable returns (bytes32) {
function relayMessages(
uint256[] calldata messageIds,
address sygmaAdapter,
bytes calldata
) public payable returns (bytes32) {
bytes32[] memory hashes = new bytes32[](messageIds.length);
for (uint256 i = 0; i < messageIds.length; i++) {
uint256 id = messageIds[i];
Expand Down
6 changes: 5 additions & 1 deletion packages/evm/contracts/interfaces/IMessageRelay.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@
pragma solidity ^0.8.17;

interface IMessageRelay {
function relayMessages(uint256[] memory messageIds, address adapter) external payable returns (bytes32 receipts);
function relayMessages(
uint256[] memory messageIds,
address adapter,
bytes calldata data
) external payable returns (bytes32 receipts);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IMessageRelay } from "../interfaces/IMessageRelay.sol";
contract MockMessageRelay is IMessageRelay {
uint256 public count;

function relayMessages(uint256[] memory, address) external payable returns (bytes32 receipts) {
function relayMessages(uint256[] calldata, address, bytes calldata) external payable returns (bytes32 receipts) {
receipts = bytes32(count);
count++;
}
Expand Down
7 changes: 6 additions & 1 deletion packages/evm/test/00_E2E.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ describe("End-to-end tests", function () {
const ambAdapter = await AMBAdapter.deploy(amb.address, ambMessageRelay.address, BYTES32_DOMAIN_ID)

//// dispatch messages
await yaho.dispatchMessagesToAdapters([message_1, message_2], [ambMessageRelay.address], [ambAdapter.address])
await yaho.dispatchMessagesToAdapters(
[message_1, message_2],
[ambMessageRelay.address],
[ambAdapter.address],
["0x", "0x"],
)

// execute messages
const response = await yaru.callStatic.executeMessages(
Expand Down
9 changes: 6 additions & 3 deletions packages/evm/test/04_Yaho.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ describe("Yaho", function () {
it("reverts if no message IDs are given", async function () {
const { yaho, messageRelay } = await setup()
await yaho.dispatchMessages([MESSAGE_1, MESSAGE_2])
await expect(yaho.relayMessagesToAdapters([], [messageRelay.address], [yaho.address, yaho.address]))
await expect(yaho.relayMessagesToAdapters([], [messageRelay.address], [yaho.address, yaho.address], ["0x"]))
.to.be.revertedWithCustomError(yaho, "NoMessageIdsGiven")
.withArgs(yaho.address)
})
it("reverts if no adapters are given", async function () {
const { yaho } = await setup()
await yaho.dispatchMessages([MESSAGE_1, MESSAGE_2])
await expect(yaho.relayMessagesToAdapters([ID_ZERO], [], [yaho.address, yaho.address]))
await expect(yaho.relayMessagesToAdapters([ID_ZERO], [], [yaho.address, yaho.address], []))
.to.be.revertedWithCustomError(yaho, "NoAdaptersGiven")
.withArgs(yaho.address)
})
Expand All @@ -93,6 +93,7 @@ describe("Yaho", function () {
[ID_ZERO, ID_ONE],
[messageRelay.address, messageRelay.address],
[yaho.address, yaho.address],
["0x", "0x"],
)
expect(receipts[0]).to.equal(ID_ZERO)
expect(receipts[1]).to.equal(ID_ONE)
Expand All @@ -102,7 +103,7 @@ describe("Yaho", function () {
describe("dispatchMessagesToAdapters()", function () {
it("reverts if no adapters are given", async function () {
const { yaho } = await setup()
await expect(yaho.dispatchMessagesToAdapters([MESSAGE_1, MESSAGE_2], [], [yaho.address, yaho.address]))
await expect(yaho.dispatchMessagesToAdapters([MESSAGE_1, MESSAGE_2], [], [yaho.address, yaho.address], []))
.to.be.revertedWithCustomError(yaho, "NoAdaptersGiven")
.withArgs(yaho.address)
})
Expand All @@ -113,12 +114,14 @@ describe("Yaho", function () {
[MESSAGE_1, MESSAGE_2],
[messageRelay.address, messageRelay.address],
[yaho.address, yaho.address],
["0x", "0x"],
),
)
const [messageIds, receipts] = await yaho.callStatic.dispatchMessagesToAdapters(
[MESSAGE_1, MESSAGE_2],
[messageRelay.address, messageRelay.address],
[yaho.address, yaho.address],
["0x", "0x"],
)
expect(messageIds[0]).to.equal(ID_TWO)
expect(receipts[0]).to.equal(ID_TWO)
Expand Down
8 changes: 4 additions & 4 deletions packages/evm/test/06_HashiZodiacModule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe("HashiModule", function () {
}

// dispatch message
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address])
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address], ["0x"])
// execute messages
await expect(yaru.executeMessages([message], [ID_ZERO], [wallet.address], [ambAdapter.address])).to.be.reverted
})
Expand All @@ -237,7 +237,7 @@ describe("HashiModule", function () {
}

// dispatch message
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address])
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address], ["0x"])
// execute messages
await expect(yaru.executeMessages([message], [ID_ZERO], [wallet.address], [ambAdapter.address])).to.be.reverted
})
Expand All @@ -254,7 +254,7 @@ describe("HashiModule", function () {
}

// dispatch message
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address])
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address], ["0x"])

await expect(yaru.executeMessages([message], [ID_ZERO], [wallet.address], [ambAdapter.address])).to.be.reverted
})
Expand All @@ -270,7 +270,7 @@ describe("HashiModule", function () {
const pingCount = await pingPong.count()

// dispatch message
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address])
await yaho.dispatchMessagesToAdapters([message], [ambMessageRelay.address], [ambAdapter.address], ["0x"])
// execute messages
await yaru.executeMessages([message], [ID_ZERO], [wallet.address], [ambAdapter.address])

Expand Down
8 changes: 4 additions & 4 deletions packages/evm/test/adapters/AMB/03_AMBMessageRelay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const setup = async () => {
}
}

describe("AMBMessageRelayer", function () {
describe("AMBMessageRelay", function () {
describe("Deploy", function () {
it("Successfully deploys contract", async function () {
const { ambMessageRelay } = await setup()
Expand All @@ -46,12 +46,12 @@ describe("AMBMessageRelayer", function () {
describe("relayMessages()", function () {
it("Relays message hashes over AMB", async function () {
const { ambMessageRelay, ambAdapter } = await setup()
const receipt = await ambMessageRelay.relayMessages([0, 1], ambAdapter.address)
const receipt = await ambMessageRelay.relayMessages([0, 1], ambAdapter.address, "0x")
await expect(receipt).to.emit(ambMessageRelay, "MessageRelayed").withArgs(ambMessageRelay.address, 0)
})
it("Reports headers to AMB", async function () {
const { ambMessageRelay, amb, ambAdapter, message_1, yaho, wallet } = await setup()
const receipt = await ambMessageRelay.relayMessages([0, 1], ambAdapter.address)
const receipt = await ambMessageRelay.relayMessages([0, 1], ambAdapter.address, "0x")
await expect(receipt).to.emit(ambMessageRelay, "MessageRelayed").withArgs(ambMessageRelay.address, 0)
await expect(receipt).to.emit(ambMessageRelay, "MessageRelayed").withArgs(ambMessageRelay.address, 1)
const hash0 = await yaho.calculateHash(network.config.chainId, 0, yaho.address, wallet.address, message_1)
Expand All @@ -61,7 +61,7 @@ describe("AMBMessageRelayer", function () {
})
it("Returns receipt", async function () {
const { ambMessageRelay, ambAdapter } = await setup()
const receipt = await ambMessageRelay.callStatic.relayMessages([0, 1], ambAdapter.address)
const receipt = await ambMessageRelay.callStatic.relayMessages([0, 1], ambAdapter.address, "0x")
expect(receipt).is.not.null
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ describe("SygmaMessageRelayer", function () {
[hash0, hash1],
sygmaAdapter.address,
)
expect(await sygmaMessageRelayer.callStatic.relayMessages([0, 1], sygmaAdapter.address)).to.equal(
expect(await sygmaMessageRelayer.callStatic.relayMessages([0, 1], sygmaAdapter.address, "0x")).to.equal(
"0x0000000000000000000000000000000000000000000000000000000000000001",
)
await expect(sygmaMessageRelayer.relayMessages([0, 1], sygmaAdapter.address))
await expect(sygmaMessageRelayer.relayMessages([0, 1], sygmaAdapter.address, "0x"))
.to.emit(sygmaMessageRelayer, "MessageRelayed")
.withArgs(sygmaMessageRelayer.address, 0)
.and.to.emit(sygmaMessageRelayer, "MessageRelayed")
Expand Down
2 changes: 1 addition & 1 deletion packages/evm/test/adapters/Sygma/04_Sygma_E2E.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe("SygmaMessageRelayer End-to-End", function () {
const pingCount = await pingPong.count()

// dispatch message
await yaho.dispatchMessagesToAdapters([message], [sygmaMessageRelayer.address], [sygmaAdapter.address])
await yaho.dispatchMessagesToAdapters([message], [sygmaMessageRelayer.address], [sygmaAdapter.address], ["0x"])
// execute messages
await yaru.executeMessages([message], [ID_ZERO], [wallet.address], [sygmaAdapter.address])

Expand Down
6 changes: 3 additions & 3 deletions packages/evm/test/test/00_NaiveTokenBridge.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe("Naive Token Bridge", function () {

const messageId = await tokenBridgeOne.callStatic.bridgeTokens(erc20Token.address, wallet.address, AMOUNT)
await tokenBridgeOne.bridgeTokens(erc20Token.address, wallet.address, AMOUNT)
await yaho.relayMessagesToAdapters([messageId], [ambMessageRelay.address], [ambAdapter.address])
await yaho.relayMessagesToAdapters([messageId], [ambMessageRelay.address], [ambAdapter.address], ["0x"])

const call = await tokenBridgeTwo.populateTransaction.mintTokens(
erc20Token.address,
Expand Down Expand Up @@ -114,7 +114,7 @@ describe("Naive Token Bridge", function () {

const messageIdOne = await tokenBridgeOne.callStatic.bridgeTokens(erc20Token.address, wallet.address, AMOUNT)
await tokenBridgeOne.bridgeTokens(erc20Token.address, wallet.address, AMOUNT)
await yaho.relayMessagesToAdapters([messageIdOne], [ambMessageRelay.address], [ambAdapter.address])
await yaho.relayMessagesToAdapters([messageIdOne], [ambMessageRelay.address], [ambAdapter.address], ["0x"])

const callOne = await tokenBridgeTwo.populateTransaction.mintTokens(
erc20Token.address,
Expand All @@ -139,7 +139,7 @@ describe("Naive Token Bridge", function () {
const messageIdTwo = await tokenBridgeTwo.callStatic.bridgeTokens(twinToken.address, wallet.address, AMOUNT)
await tokenBridgeTwo.bridgeTokens(twinToken.address, wallet.address, AMOUNT)

await yaho.relayMessagesToAdapters([messageIdTwo], [ambMessageRelay.address], [ambAdapter.address])
await yaho.relayMessagesToAdapters([messageIdTwo], [ambMessageRelay.address], [ambAdapter.address], ["0x"])

const callTwo = await tokenBridgeOne.populateTransaction.releaseTokens(erc20Token.address, wallet.address, AMOUNT)
const messageTwo = {
Expand Down
Loading