The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/solidity/optics-core/contracts/Home.sol

120 lines
3.1 KiB

// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
import "./Common.sol";
import "./Merkle.sol";
import "./Queue.sol";
import "./Sortition.sol";
contract Home is MerkleTreeManager, QueueManager, Common {
using QueueLib for QueueLib.Queue;
using MerkleLib for MerkleLib.Tree;
mapping(uint32 => uint32) public sequences;
ISortition sortition;
4 years ago
event Dispatch(
feature: adds untested ProverSync struct to optics-core (#78) * feature: basic ProverSync setup * prog: basic setup for linking enqueuings to roots * refactor: modifies Dispatch event to contain treeSize field and condenses destination and sequence into single field * feature: adds leaf_by_tree_size methods to Home abi and logic for updating local tree in prover_sync * format: runs cargo fmt * prog: comments out unused code from previous design * fix: replaces slip44 references with domain * refactor: moves interval to parameter instead of ProverSync struct field * fix: deletes commented out code from NewLeaf design * refactor: modifies ProverSync error handling to bubble up ProverSyncError type * fix: removes slip44 reference * refactor: adds incremental merkle tree to ProverSync and batch updates prover on successful incremental update * fmt: runs cargo fmt * fix: fixes raw_message_by_sequence filtering after destination and sequence event fields were combined to single field in Home * fix: simplifies error handling, adds retry counter for failed api calls, and clones incremental merkle * fix: renames leaves_by_tree_size to leaves_by_tree_index * fix/docs: fixes Home Dispatch event to use leafIndex instead of treeSize and fixes docs to reflect change * fix: changes num_retries to u32 * refactor: cleans up error handling * docs: adds comment about why leafIndex is count() - 1 * fix: deletes old NewLeaf event from Home * fmt: runs cargo fmt * fix: removes num_retries field * refactor: simplifies destination_and_sequence util * fix: fixes typo in warning message * refactor: derives transparent errors * docs: improves comments for prover-sync/incremental merkle behavior * fix: restarts poll_updates loop if prover root was updated while we were building leaf vector from incremental merkle * feature: adds ProverSync::new function * fix: ProverSync stops polling updates on invalid local_root * fmt: runs cargo fmt
4 years ago
uint256 indexed leafIndex,
uint64 indexed destinationAndSequence,
bytes32 indexed leaf,
bytes message
);
event ImproperUpdate();
constructor(uint32 _originDomain, address _sortition)
4 years ago
payable
MerkleTreeManager()
QueueManager()
Common(_originDomain, address(0), bytes32(0))
{
sortition = ISortition(_sortition);
updater = ISortition(_sortition).current();
}
function fail() internal override {
_setFailed();
sortition.slash(msg.sender);
}
function calcDestinationAndSequence(uint32 _destination, uint32 _sequence)
internal
pure
returns (uint64)
{
feature: adds untested ProverSync struct to optics-core (#78) * feature: basic ProverSync setup * prog: basic setup for linking enqueuings to roots * refactor: modifies Dispatch event to contain treeSize field and condenses destination and sequence into single field * feature: adds leaf_by_tree_size methods to Home abi and logic for updating local tree in prover_sync * format: runs cargo fmt * prog: comments out unused code from previous design * fix: replaces slip44 references with domain * refactor: moves interval to parameter instead of ProverSync struct field * fix: deletes commented out code from NewLeaf design * refactor: modifies ProverSync error handling to bubble up ProverSyncError type * fix: removes slip44 reference * refactor: adds incremental merkle tree to ProverSync and batch updates prover on successful incremental update * fmt: runs cargo fmt * fix: fixes raw_message_by_sequence filtering after destination and sequence event fields were combined to single field in Home * fix: simplifies error handling, adds retry counter for failed api calls, and clones incremental merkle * fix: renames leaves_by_tree_size to leaves_by_tree_index * fix/docs: fixes Home Dispatch event to use leafIndex instead of treeSize and fixes docs to reflect change * fix: changes num_retries to u32 * refactor: cleans up error handling * docs: adds comment about why leafIndex is count() - 1 * fix: deletes old NewLeaf event from Home * fmt: runs cargo fmt * fix: removes num_retries field * refactor: simplifies destination_and_sequence util * fix: fixes typo in warning message * refactor: derives transparent errors * docs: improves comments for prover-sync/incremental merkle behavior * fix: restarts poll_updates loop if prover root was updated while we were building leaf vector from incremental merkle * feature: adds ProverSync::new function * fix: ProverSync stops polling updates on invalid local_root * fmt: runs cargo fmt
4 years ago
return (uint64(_destination) << 32) & _sequence;
}
function enqueue(
uint32 destination,
bytes32 recipient,
bytes memory body
) external notFailed {
uint32 sequence = sequences[destination] + 1;
sequences[destination] = sequence;
bytes memory _message =
Message.formatMessage(
originDomain,
bytes32(uint256(uint160(msg.sender))),
sequence,
destination,
recipient,
body
);
bytes32 _leaf = keccak256(_message);
tree.insert(_leaf);
queue.enqueue(root());
feature: adds untested ProverSync struct to optics-core (#78) * feature: basic ProverSync setup * prog: basic setup for linking enqueuings to roots * refactor: modifies Dispatch event to contain treeSize field and condenses destination and sequence into single field * feature: adds leaf_by_tree_size methods to Home abi and logic for updating local tree in prover_sync * format: runs cargo fmt * prog: comments out unused code from previous design * fix: replaces slip44 references with domain * refactor: moves interval to parameter instead of ProverSync struct field * fix: deletes commented out code from NewLeaf design * refactor: modifies ProverSync error handling to bubble up ProverSyncError type * fix: removes slip44 reference * refactor: adds incremental merkle tree to ProverSync and batch updates prover on successful incremental update * fmt: runs cargo fmt * fix: fixes raw_message_by_sequence filtering after destination and sequence event fields were combined to single field in Home * fix: simplifies error handling, adds retry counter for failed api calls, and clones incremental merkle * fix: renames leaves_by_tree_size to leaves_by_tree_index * fix/docs: fixes Home Dispatch event to use leafIndex instead of treeSize and fixes docs to reflect change * fix: changes num_retries to u32 * refactor: cleans up error handling * docs: adds comment about why leafIndex is count() - 1 * fix: deletes old NewLeaf event from Home * fmt: runs cargo fmt * fix: removes num_retries field * refactor: simplifies destination_and_sequence util * fix: fixes typo in warning message * refactor: derives transparent errors * docs: improves comments for prover-sync/incremental merkle behavior * fix: restarts poll_updates loop if prover root was updated while we were building leaf vector from incremental merkle * feature: adds ProverSync::new function * fix: ProverSync stops polling updates on invalid local_root * fmt: runs cargo fmt
4 years ago
// leafIndex is count() - 1 since new leaf has already been inserted
emit Dispatch(
count() - 1,
calcDestinationAndSequence(destination, sequence),
_leaf,
_message
);
}
function update(
bytes32 _oldRoot,
bytes32 _newRoot,
bytes memory _signature
) external notFailed {
if (improperUpdate(_oldRoot, _newRoot, _signature)) return;
while (true) {
bytes32 next = queue.dequeue();
if (next == _newRoot) break;
}
current = _newRoot;
emit Update(originDomain, _oldRoot, _newRoot, _signature);
}
function improperUpdate(
bytes32 _oldRoot,
bytes32 _newRoot,
bytes memory _signature
) public notFailed returns (bool) {
require(Common.checkSig(_oldRoot, _newRoot, _signature), "bad sig");
require(_oldRoot == current, "not a current update");
if (!queue.contains(_newRoot)) {
fail();
emit ImproperUpdate();
return true;
}
return false;
}
function suggestUpdate()
external
view
returns (bytes32 _current, bytes32 _new)
{
if (queue.length() != 0) {
_current = current;
_new = queue.lastItem();
}
}
}