feature: Message library

buddies-main-deployment
James Prestwich 4 years ago
parent 64b5ae8fc6
commit a7463ed14c
No known key found for this signature in database
GPG Key ID: 75A7F5C06D747046
  1. 70
      solidity/contracts/Common.sol
  2. 20
      solidity/contracts/Home.sol
  3. 12
      solidity/contracts/Replica.sol

@ -2,6 +2,76 @@
pragma solidity >=0.6.11;
import "@openzeppelin/contracts/cryptography/ECDSA.sol";
import "@summa-tx/memview-sol/contracts/TypedMemView.sol";
library Message {
using TypedMemView for bytes;
using TypedMemView for bytes29;
function formatMessage(
uint32 _origin,
bytes32 _sender,
uint32 _sequence,
uint32 _destination,
bytes32 _recipient,
bytes memory _body
) internal pure returns (bytes memory) {
return
abi.encodePacked(
_origin,
_sender,
_sequence,
_destination,
_recipient,
_body
);
}
function messageHash(
uint32 _origin,
bytes32 _sender,
uint32 _sequence,
uint32 _destination,
bytes32 _recipient,
bytes memory _body
) internal pure returns (bytes32) {
return
keccak256(
formatMessage(
_origin,
_sender,
_sequence,
_destination,
_recipient,
_body
)
);
}
function origin(bytes29 _message) internal pure returns (uint32) {
return uint32(_message.indexUint(0, 4));
}
function sender(bytes29 _message) internal pure returns (bytes32) {
return _message.index(4, 32);
}
function sequence(bytes29 _message) internal pure returns (uint32) {
return uint32(_message.indexUint(32, 4));
}
function destination(bytes29 _message) internal pure returns (uint32) {
return uint32(_message.indexUint(36, 4));
}
function recipient(bytes29 _message) internal pure returns (bytes32) {
return _message.index(40, 32);
}
function body(bytes29 _message) internal pure returns (bytes29) {
return _message.slice(72, _message.len() - 72, 0);
}
}
abstract contract Common {
enum States {ACTIVE, FAILED}

@ -13,7 +13,7 @@ contract Home is MerkleTreeManager, QueueManager, Common {
uint256 constant BOND_SIZE = 50 ether;
event Message(
event Dispatch(
uint32 indexed destination,
uint32 indexed sequence,
// the message is after this root. Some future update will contain it.
@ -49,20 +49,18 @@ contract Home is MerkleTreeManager, QueueManager, Common {
sequences[destination] = sequence;
bytes32 _digest =
keccak256(
abi.encodePacked(
originSLIP44,
bytes32(uint256(uint160(msg.sender))),
sequence,
destination,
recipient,
body
)
Message.messageHash(
originSLIP44,
bytes32(uint256(uint160(msg.sender))),
sequence,
destination,
recipient,
body
);
tree.insert(_digest);
queue.enqueue(root());
emit Message(destination, sequence, current, body);
emit Dispatch(destination, sequence, current, body);
}
function update(

@ -66,6 +66,7 @@ contract ProcessingReplica is Replica, HasZeroHashes {
using MerkleLib for MerkleLib.Tree;
using TypedMemView for bytes;
using TypedMemView for bytes29;
using Message for bytes29;
bytes32 previous; // to smooth over witness invalidation
uint256 lastProcessed;
@ -98,9 +99,8 @@ contract ProcessingReplica is Replica, HasZeroHashes {
{
bytes29 _m = _message.ref(0);
uint32 _destination = uint32(_m.indexUint(36, 4));
uint32 _sequence = uint32(_m.indexUint(72, 4));
require(_destination == ownSLIP44, "other destination");
uint32 _sequence = _m.sequence();
require(_m.destination() == ownSLIP44, "other destination");
require(_sequence == lastProcessed + 1, "out of sequence");
require(
messages[keccak256(_message)] == MessageStatus.Pending,
@ -110,10 +110,10 @@ contract ProcessingReplica is Replica, HasZeroHashes {
messages[_m.keccak()] = MessageStatus.Processed;
// recipient address starts at the 52nd byte. 4 + 36 + 4 + 12
address recipient = _m.indexAddress(52);
// TODO: assembly this to avoid the clone?
bytes memory payload = _m.slice(76, _m.len() - 76, 0).clone();
address recipient = address(uint160(uint256(_m.recipient())));
// TODO: assembly this to avoid the clone?
bytes memory payload = _m.body().clone();
// results intentionally ignored
return recipient.call(payload);
}

Loading…
Cancel
Save