From bb89d4840d9d949a3f6c84750656362efbb0b3a8 Mon Sep 17 00:00:00 2001 From: Trajan0x Date: Sat, 4 Jun 2022 16:03:52 -0400 Subject: [PATCH 1/2] update contracts --- packages/contracts/contracts/Home.sol | 1 + packages/contracts/contracts/libs/Message.sol | 13 +++++++++++-- packages/contracts/test/Home.t.sol | 2 ++ packages/contracts/test/Message.t.sol | 6 ++++++ .../contracts/test/harnesses/MessageHarness.sol | 9 ++++++++- 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/packages/contracts/contracts/Home.sol b/packages/contracts/contracts/Home.sol index 84a3aeafab..951a98a048 100644 --- a/packages/contracts/contracts/Home.sol +++ b/packages/contracts/contracts/Home.sol @@ -185,6 +185,7 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase { _nonce, _destinationDomain, _recipientAddress, + _optimisticSeconds, _messageBody ); // insert the hashed message into the Merkle tree diff --git a/packages/contracts/contracts/libs/Message.sol b/packages/contracts/contracts/libs/Message.sol index d2c2f7b03d..c7ca2e241f 100644 --- a/packages/contracts/contracts/libs/Message.sol +++ b/packages/contracts/contracts/libs/Message.sol @@ -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 @@ -33,6 +33,7 @@ library Message { uint32 _nonce, uint32 _destinationDomain, bytes32 _recipient, + uint32 _optimisticSeconds, bytes memory _messageBody ) internal pure returns (bytes memory) { return @@ -42,6 +43,7 @@ library Message { _nonce, _destinationDomain, _recipient, + _optimisticSeconds, _messageBody ); } @@ -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 @@ -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)); @@ -110,6 +118,7 @@ library Message { nonce(_message), destination(_message), recipient(_message), + optimisticSeconds(_message), TypedMemView.clone(body(_message)) ); } diff --git a/packages/contracts/test/Home.t.sol b/packages/contracts/test/Home.t.sol index 41615bf0a5..54cc3c837b 100644 --- a/packages/contracts/test/Home.t.sol +++ b/packages/contracts/test/Home.t.sol @@ -101,6 +101,7 @@ contract HomeTest is SynapseTestWithUpdaterManager { nonce, remoteDomain, recipient, + optimisticSeconds, messageBody ); bytes32 messageHash = keccak256(abi.encodePacked(message, optimisticSeconds)); @@ -130,6 +131,7 @@ contract HomeTest is SynapseTestWithUpdaterManager { nonce, remoteDomain, recipient, + optimisticSeconds, messageBody ); vm.prank(sender); diff --git a/packages/contracts/test/Message.t.sol b/packages/contracts/test/Message.t.sol index ed460a13d9..183c89f20e 100644 --- a/packages/contracts/test/Message.t.sol +++ b/packages/contracts/test/Message.t.sol @@ -17,6 +17,7 @@ contract MessageTest is Test { bytes32 sender; uint32 nonce; uint32 destinationDomain; + uint32 optimisticSeconds; bytes32 recipient; bytes messageBody; @@ -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"); } @@ -37,6 +39,7 @@ contract MessageTest is Test { nonce, destinationDomain, recipient, + optimisticSeconds, messageBody ); @@ -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)); } @@ -57,6 +61,7 @@ contract MessageTest is Test { nonce, destinationDomain, recipient, + optimisticSeconds, messageBody ); @@ -66,6 +71,7 @@ contract MessageTest is Test { nonce, destinationDomain, recipient, + optimisticSeconds, messageBody ); diff --git a/packages/contracts/test/harnesses/MessageHarness.sol b/packages/contracts/test/harnesses/MessageHarness.sol index 6e1c0cd8cc..93391d632d 100644 --- a/packages/contracts/test/harnesses/MessageHarness.sol +++ b/packages/contracts/test/harnesses/MessageHarness.sol @@ -16,6 +16,7 @@ contract MessageHarness { uint32 _nonce, uint32 _destinationDomain, bytes32 _recipient, + uint32 _optimisticSeconds, bytes memory _messageBody ) public pure returns (bytes memory) { return @@ -25,6 +26,7 @@ contract MessageHarness { _nonce, _destinationDomain, _recipient, + _optimisticSeconds, _messageBody ); } @@ -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) { @@ -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(); } From 7199ae155f4bdec4a224c3adca7a6194be782d7c Mon Sep 17 00:00:00 2001 From: aureliusbtc <82057759+aureliusbtc@users.noreply.github.com> Date: Sat, 4 Jun 2022 16:17:08 -0400 Subject: [PATCH 2/2] fix: change messageHash back to only message --- packages/contracts/contracts/Home.sol | 4 +--- packages/contracts/contracts/ReplicaManager.sol | 10 ++++------ packages/contracts/test/Home.t.sol | 4 +--- packages/contracts/test/ReplicaManager.t.sol | 6 +++--- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/packages/contracts/contracts/Home.sol b/packages/contracts/contracts/Home.sol index 951a98a048..c1a3b5bcc2 100644 --- a/packages/contracts/contracts/Home.sol +++ b/packages/contracts/contracts/Home.sol @@ -65,7 +65,6 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase { uint256 indexed leafIndex, uint64 indexed destinationAndNonce, bytes32 committedRoot, - uint32 optimisticSeconds, bytes message ); @@ -189,7 +188,7 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase { _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()); @@ -200,7 +199,6 @@ contract Home is Version0, QueueManager, MerkleTreeManager, SynapseBase { count() - 1, _destinationAndNonce(_destinationDomain, _nonce), committedRoot, - _optimisticSeconds, _message ); } diff --git a/packages/contracts/contracts/ReplicaManager.sol b/packages/contracts/contracts/ReplicaManager.sol index 3095315146..4f325453c3 100644 --- a/packages/contracts/contracts/ReplicaManager.sol +++ b/packages/contracts/contracts/ReplicaManager.sol @@ -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); } @@ -404,7 +403,6 @@ 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 @@ -412,18 +410,18 @@ contract ReplicaManager is Version0, Initializable, OwnableUpgradeable { 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; } diff --git a/packages/contracts/test/Home.t.sol b/packages/contracts/test/Home.t.sol index 54cc3c837b..517894985b 100644 --- a/packages/contracts/test/Home.t.sol +++ b/packages/contracts/test/Home.t.sol @@ -85,7 +85,6 @@ contract HomeTest is SynapseTestWithUpdaterManager { uint256 indexed leafIndex, uint64 indexed destinationAndNonce, bytes32 committedRoot, - uint32 optimisticSeconds, bytes message ); @@ -104,14 +103,13 @@ contract HomeTest is SynapseTestWithUpdaterManager { 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); diff --git a/packages/contracts/test/ReplicaManager.t.sol b/packages/contracts/test/ReplicaManager.t.sol index 0ad3c589f5..e42e943f14 100644 --- a/packages/contracts/test/ReplicaManager.t.sol +++ b/packages/contracts/test/ReplicaManager.t.sol @@ -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 @@ -144,7 +144,7 @@ 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); @@ -152,7 +152,7 @@ contract ReplicaManagerTest is SynapseTest { 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));