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

Add Latency to Message Body #39

Merged
merged 2 commits into from
Jun 4, 2022
Merged
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
5 changes: 2 additions & 3 deletions packages/contracts/contracts/Home.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase {
uint256 indexed leafIndex,
uint64 indexed destinationAndNonce,
bytes32 committedRoot,
uint32 optimisticSeconds,
bytes message
);

Expand Down Expand Up @@ -185,10 +184,11 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase {
_nonce,
_destinationDomain,
_recipientAddress,
_optimisticSeconds,
_messageBody
);
// insert the hashed message into the Merkle tree
bytes32 _messageHash = keccak256(abi.encodePacked(_message, _optimisticSeconds));
bytes32 _messageHash = keccak256(_message);
tree.insert(_messageHash);
// enqueue the new Merkle root after inserting the message
queue.enqueue(root());
Expand All @@ -199,7 +199,6 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase {
count() - 1,
_destinationAndNonce(_destinationDomain, _nonce),
committedRoot,
_optimisticSeconds,
_message
);
}
Expand Down
10 changes: 4 additions & 6 deletions packages/contracts/contracts/ReplicaManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,10 @@ contract ReplicaManager is Version0, Initializable, OwnableUpgradeable {
function proveAndProcess(
uint32 _remoteDomain,
bytes memory _message,
uint32 _optimisticSeconds,
bytes32[32] calldata _proof,
uint256 _index
) external {
require(prove(_remoteDomain, _message, _optimisticSeconds, _proof, _index), "!prove");
require(prove(_remoteDomain, _message, _proof, _index), "!prove");
process(_message);
}

Expand Down Expand Up @@ -404,26 +403,25 @@ contract ReplicaManager is Version0, Initializable, OwnableUpgradeable {
* @dev For convenience, we allow proving against any previous root.
* This means that witnesses never need to be updated for the new root
* @param _message Formatted message
* @param _optimisticSeconds Latency period requested by app on Home, included in part of Merkle proof
* @param _proof Merkle proof of inclusion for leaf
* @param _index Index of leaf in home's merkle tree
* @return Returns true if proof was valid and `prove` call succeeded
**/
function prove(
uint32 _remoteDomain,
bytes memory _message,
uint32 _optimisticSeconds,
bytes32[32] calldata _proof,
uint256 _index
) public returns (bool) {
bytes32 _leaf = keccak256(abi.encodePacked(_message, _optimisticSeconds));
uint32 optimisticSeconds = _message.ref(0).optimisticSeconds();
bytes32 _leaf = keccak256(_message);
ReplicaLib.Replica storage replica = allReplicas[activeReplicas[_remoteDomain]];
// ensure that message has not been proven or processed
require(replica.messages[_leaf] == ReplicaLib.MessageStatus.None, "!MessageStatus.None");
// calculate the expected root based on the proof
bytes32 _calculatedRoot = MerkleLib.branchRoot(_leaf, _proof, _index);
// if the root is valid, change status to Proven
if (acceptableRoot(_remoteDomain, _optimisticSeconds, _calculatedRoot)) {
if (acceptableRoot(_remoteDomain, optimisticSeconds, _calculatedRoot)) {
replica.setMessageStatus(_leaf, ReplicaLib.MessageStatus.Processed);
return true;
}
Expand Down
13 changes: 11 additions & 2 deletions packages/contracts/contracts/libs/Message.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ library Message {
using TypedMemView for bytes29;

// Number of bytes in formatted message before `body` field
uint256 internal constant PREFIX_LENGTH = 76;
uint256 internal constant PREFIX_LENGTH = 80;

/**
* @notice Returns formatted (packed) message with provided fields
Expand All @@ -33,6 +33,7 @@ library Message {
uint32 _nonce,
uint32 _destinationDomain,
bytes32 _recipient,
uint32 _optimisticSeconds,
bytes memory _messageBody
) internal pure returns (bytes memory) {
return
Expand All @@ -42,6 +43,7 @@ library Message {
_nonce,
_destinationDomain,
_recipient,
_optimisticSeconds,
_messageBody
);
}
Expand All @@ -62,9 +64,10 @@ library Message {
uint32 _nonce,
uint32 _destination,
bytes32 _recipient,
uint32 _optimisticSeconds,
bytes memory _body
) internal pure returns (bytes32) {
return keccak256(formatMessage(_origin, _sender, _nonce, _destination, _recipient, _body));
return keccak256(formatMessage(_origin, _sender, _nonce, _destination, _recipient, _optimisticSeconds, _body));
}

/// @notice Returns message's origin field
Expand Down Expand Up @@ -92,6 +95,11 @@ library Message {
return _message.index(44, 32);
}

/// @notice Returns the optimistic seconds from the message
function optimisticSeconds(bytes29 _message) internal pure returns (uint32){
return uint32(_message.indexUint(76, 4));
}

/// @notice Returns message's recipient field as an address
function recipientAddress(bytes29 _message) internal pure returns (address) {
return TypeCasts.bytes32ToAddress(recipient(_message));
Expand All @@ -110,6 +118,7 @@ library Message {
nonce(_message),
destination(_message),
recipient(_message),
optimisticSeconds(_message),
TypedMemView.clone(body(_message))
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/test/Home.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ contract HomeTest is SynapseTestWithUpdaterManager {
uint256 indexed leafIndex,
uint64 indexed destinationAndNonce,
bytes32 committedRoot,
uint32 optimisticSeconds,
bytes message
);

Expand All @@ -101,16 +100,16 @@ contract HomeTest is SynapseTestWithUpdaterManager {
nonce,
remoteDomain,
recipient,
optimisticSeconds,
messageBody
);
bytes32 messageHash = keccak256(abi.encodePacked(message, optimisticSeconds));
bytes32 messageHash = keccak256(message);
vm.expectEmit(true, true, true, true);
emit Dispatch(
messageHash,
home.count(),
(uint64(remoteDomain) << 32) | nonce,
home.committedRoot(),
optimisticSeconds,
message
);
vm.prank(sender);
Expand All @@ -130,6 +129,7 @@ contract HomeTest is SynapseTestWithUpdaterManager {
nonce,
remoteDomain,
recipient,
optimisticSeconds,
messageBody
);
vm.prank(sender);
Expand Down
6 changes: 6 additions & 0 deletions packages/contracts/test/Message.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ contract MessageTest is Test {
bytes32 sender;
uint32 nonce;
uint32 destinationDomain;
uint32 optimisticSeconds;
bytes32 recipient;
bytes messageBody;

Expand All @@ -26,6 +27,7 @@ contract MessageTest is Test {
sender = bytes32("AAAA THE SENDOOOOOR");
nonce = 0;
destinationDomain = 2000;
optimisticSeconds = 4;
recipient = bytes32("AAAA THE RECEIVOOOR");
messageBody = bytes("Messagoooor");
}
Expand All @@ -37,6 +39,7 @@ contract MessageTest is Test {
nonce,
destinationDomain,
recipient,
optimisticSeconds,
messageBody
);

Expand All @@ -46,6 +49,7 @@ contract MessageTest is Test {
assertEq(messageHarness.nonce(message), nonce);
assertEq(messageHarness.destination(message), destinationDomain);
assertEq(messageHarness.recipient(message), recipient);
assertEq(messageHarness.optimisticSeconds(message), optimisticSeconds);
assertEq(messageHarness.body(message), (messageBody));
assertEq(messageHarness.leaf(message), keccak256(message));
}
Expand All @@ -57,6 +61,7 @@ contract MessageTest is Test {
nonce,
destinationDomain,
recipient,
optimisticSeconds,
messageBody
);

Expand All @@ -66,6 +71,7 @@ contract MessageTest is Test {
nonce,
destinationDomain,
recipient,
optimisticSeconds,
messageBody
);

Expand Down
6 changes: 3 additions & 3 deletions packages/contracts/test/ReplicaManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ contract ReplicaManagerTest is SynapseTest {
// Relayer relays a new root signed by updater on Home chain
function test_successfulUpdate() public {
bytes memory newMessage = "new root";
bytes32 newRoot = keccak256(abi.encodePacked(newMessage, optimisticSeconds));
bytes32 newRoot = keccak256(newMessage);
assertEq(replicaManager.updater(), vm.addr(updaterPK));
bytes memory sig = signRemoteUpdate(updaterPK, committedRoot, newRoot);
// Root doesn't exist yet
Expand All @@ -144,15 +144,15 @@ contract ReplicaManagerTest is SynapseTest {

function test_updateWithIncorrectSig() public {
bytes memory newMessage = "new root";
bytes32 newRoot = keccak256(abi.encodePacked(newMessage, optimisticSeconds));
bytes32 newRoot = keccak256(newMessage);
bytes memory sig = signRemoteUpdate(fakeUpdaterPK, committedRoot, newRoot);
vm.expectRevert("!updater sig");
replicaManager.update(remoteDomain, committedRoot, newRoot, sig);
}

function test_acceptableRoot() public {
bytes memory newMessage = "new root";
bytes32 newRoot = keccak256(abi.encodePacked(newMessage, optimisticSeconds));
bytes32 newRoot = keccak256(newMessage);
test_successfulUpdate();
vm.warp(block.timestamp + optimisticSeconds + 1);
assertTrue(replicaManager.acceptableRoot(remoteDomain, optimisticSeconds, newRoot));
Expand Down
9 changes: 8 additions & 1 deletion packages/contracts/test/harnesses/MessageHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract MessageHarness {
uint32 _nonce,
uint32 _destinationDomain,
bytes32 _recipient,
uint32 _optimisticSeconds,
bytes memory _messageBody
) public pure returns (bytes memory) {
return
Expand All @@ -25,6 +26,7 @@ contract MessageHarness {
_nonce,
_destinationDomain,
_recipient,
_optimisticSeconds,
_messageBody
);
}
Expand All @@ -45,9 +47,10 @@ contract MessageHarness {
uint32 _nonce,
uint32 _destination,
bytes32 _recipient,
uint32 _optimisticSeconds,
bytes memory _body
) public pure returns (bytes32) {
return Message.messageHash(_origin, _sender, _nonce, _destination, _recipient, _body);
return Message.messageHash(_origin, _sender, _nonce, _destination, _recipient, _optimisticSeconds, _body);
}

function body(bytes memory _message) external view returns (bytes memory) {
Expand Down Expand Up @@ -78,6 +81,10 @@ contract MessageHarness {
return _message.ref(0).recipientAddress();
}

function optimisticSeconds(bytes memory _message) external pure returns (uint32) {
return _message.ref(0).optimisticSeconds();
}

function leaf(bytes memory _message) external view returns (bytes32) {
return _message.ref(0).leaf();
}
Expand Down