From 64b5ae8fc61658ca76a5a632338d90ae7a16a68e Mon Sep 17 00:00:00 2001 From: James Prestwich Date: Sun, 17 Jan 2021 13:18:53 -0800 Subject: [PATCH] feature: current in Common. Event for Message --- solidity/contracts/Common.sol | 10 ++++++++-- solidity/contracts/Home.sol | 24 +++++++++++++++++++++--- solidity/contracts/Replica.sol | 16 +++++++++------- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/solidity/contracts/Common.sol b/solidity/contracts/Common.sol index 0f4d6182d..9319eb8ff 100644 --- a/solidity/contracts/Common.sol +++ b/solidity/contracts/Common.sol @@ -7,10 +7,11 @@ abstract contract Common { enum States {ACTIVE, FAILED} uint32 public immutable originSLIP44; - address public updater; bytes32 public immutable DOMAIN_HASH; + address public updater; States public state; + bytes32 public current; event Update( bytes32 indexed _oldRoot, @@ -24,9 +25,14 @@ abstract contract Common { bytes _signature2 ); - constructor(uint32 _originSLIP44, address _updater) { + constructor( + uint32 _originSLIP44, + address _updater, + bytes32 _current + ) { originSLIP44 = _originSLIP44; updater = _updater; + current = _current; DOMAIN_HASH = keccak256(abi.encodePacked(_originSLIP44, "OPTICS")); state = States.ACTIVE; } diff --git a/solidity/contracts/Home.sol b/solidity/contracts/Home.sol index 1eed2c779..1c3dbb615 100644 --- a/solidity/contracts/Home.sol +++ b/solidity/contracts/Home.sol @@ -9,20 +9,32 @@ contract Home is MerkleTreeManager, QueueManager, Common { using QueueLib for QueueLib.Queue; using MerkleLib for MerkleLib.Tree; + mapping(uint32 => uint32) public sequences; + uint256 constant BOND_SIZE = 50 ether; + event Message( + uint32 indexed destination, + uint32 indexed sequence, + // the message is after this root. Some future update will contain it. + bytes32 indexed current, + bytes message + ); event ImproperUpdate(); - constructor(uint32 _originSLIP44, address _updater) + constructor( + uint32 _originSLIP44, + address _updater, + bytes32 _current + ) payable MerkleTreeManager() QueueManager() - Common(_originSLIP44, _updater) + Common(_originSLIP44, _updater, _current) { require(msg.value >= BOND_SIZE, "insufficient bond"); } - // TODO function fail() internal override { _setFailed(); msg.sender.transfer(address(this).balance / 2); @@ -33,11 +45,15 @@ contract Home is MerkleTreeManager, QueueManager, Common { bytes32 recipient, bytes memory body ) external notFailed { + uint32 sequence = sequences[destination] + 1; + sequences[destination] = sequence; + bytes32 _digest = keccak256( abi.encodePacked( originSLIP44, bytes32(uint256(uint160(msg.sender))), + sequence, destination, recipient, body @@ -46,6 +62,7 @@ contract Home is MerkleTreeManager, QueueManager, Common { tree.insert(_digest); queue.enqueue(root()); + emit Message(destination, sequence, current, body); } function update( @@ -67,6 +84,7 @@ contract Home is MerkleTreeManager, QueueManager, Common { bytes memory _signature ) public notFailed returns (bool) { require(Common.checkSig(_newRoot, _oldRoot, _signature), "bad sig"); + require(_oldRoot == current, "Not a current update"); if (!queue.contains(_newRoot)) { fail(); emit ImproperUpdate(); diff --git a/solidity/contracts/Replica.sol b/solidity/contracts/Replica.sol index 4eb30b648..676b96912 100644 --- a/solidity/contracts/Replica.sol +++ b/solidity/contracts/Replica.sol @@ -9,7 +9,6 @@ abstract contract Replica is Common { uint32 public immutable ownSLIP44; uint256 public optimisticSeconds; - bytes32 current; bytes32 pending; uint256 confirmAt; @@ -20,11 +19,11 @@ abstract contract Replica is Common { uint32 _ownSLIP44, address _updater, uint256 _optimisticSeconds, - bytes32 _start - ) Common(_originSLIP44, _updater) { + bytes32 _current + ) Common(_originSLIP44, _updater, _current) { ownSLIP44 = _ownSLIP44; optimisticSeconds = _optimisticSeconds; - current = _start; + current = _current; } function fail() internal override { @@ -93,7 +92,10 @@ contract ProcessingReplica is Replica, HasZeroHashes { function _beforeUpdate() internal override {} - function process(bytes calldata _message) public { + function process(bytes memory _message) + public + returns (bool, bytes memory) + { bytes29 _m = _message.ref(0); uint32 _destination = uint32(_m.indexUint(36, 4)); @@ -113,7 +115,7 @@ contract ProcessingReplica is Replica, HasZeroHashes { bytes memory payload = _m.slice(76, _m.len() - 76, 0).clone(); // results intentionally ignored - recipient.call(payload); + return recipient.call(payload); } function prove( @@ -134,7 +136,7 @@ contract ProcessingReplica is Replica, HasZeroHashes { bytes32 leaf, bytes32[32] calldata proof, uint256 index, - bytes calldata message + bytes memory message ) external { require(prove(leaf, proof, index), "!prove"); process(message);