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/core/contracts/Outbox.sol

193 lines
6.3 KiB

// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.6.11;
// ============ Internal Imports ============
import {Version0} from "./Version0.sol";
import {Common} from "./Common.sol";
import {MerkleLib} from "../libs/Merkle.sol";
import {Message} from "../libs/Message.sol";
import {MerkleTreeManager} from "./Merkle.sol";
import {IOutbox} from "../interfaces/IOutbox.sol";
/**
* @title Outbox
* @author Celo Labs Inc.
* @notice Accepts messages to be dispatched to remote chains,
* constructs a Merkle tree of the messages,
* and accepts signatures from a bonded Validator
* which notarize the Merkle tree roots.
* Accepts submissions of fraudulent signatures
* by the Validator and slashes the Validator in this case.
*/
contract Outbox is IOutbox, Version0, MerkleTreeManager, Common {
// ============ Libraries ============
using MerkleLib for MerkleLib.Tree;
// ============ Constants ============
// Maximum bytes per message = 2 KiB
// (somewhat arbitrarily set to begin)
uint256 public constant MAX_MESSAGE_BODY_BYTES = 2 * 2**10;
// ============ Enums ============
// States:
// 0 - UnInitialized - before initialize function is called
// note: the contract is initialized at deploy time, so it should never be in this state
// 1 - Active - as long as the contract has not become fraudulent
// 2 - Failed - after a valid fraud proof has been submitted;
// contract will no longer accept updates or new messages
enum States {
UnInitialized,
Active,
Failed
}
// ============ Public Storage Variables ============
// Current state of contract
States public state;
// ============ Upgrade Gap ============
// gap for upgrade safety
uint256[49] private __GAP;
// ============ Events ============
4 years ago
/**
* @notice Emitted when a new message is dispatched via Abacus
* @param messageHash Hash of message; the leaf inserted to the Merkle tree for the message
* @param leafIndex Index of message's leaf in merkle tree
* @param destination Destination domain
* @param message Raw bytes of message
*/
event Dispatch(
bytes32 indexed messageHash,
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,
uint32 indexed destination,
bytes message
);
event Fail();
// ============ Constructor ============
constructor(uint32 _localDomain) Common(_localDomain) {} // solhint-disable-line no-empty-blocks
// ============ Initializer ============
function initialize(address _validatorManager) public initializer {
__Common_initialize(_validatorManager);
state = States.Active;
}
// ============ Modifiers ============
/**
* @notice Ensures that contract state != FAILED when the function is called
*/
modifier notFailed() {
require(state != States.Failed, "failed state");
_;
}
// ============ External Functions ============
/**
* @notice Dispatch the message it to the destination domain & recipient
* @dev Format the message, insert its hash into Merkle tree,
* and emit `Dispatch` event with message information.
* @param _destinationDomain Domain of destination chain
* @param _recipientAddress Address of recipient on destination chain as bytes32
* @param _messageBody Raw bytes content of message
* @return The leaf index of the dispatched message's hash in the Merkle tree.
*/
function dispatch(
uint32 _destinationDomain,
bytes32 _recipientAddress,
bytes memory _messageBody
) external override notFailed returns (uint256) {
require(_messageBody.length <= MAX_MESSAGE_BODY_BYTES, "msg too long");
// The leaf has not been inserted yet at this point1
uint256 _leafIndex = count();
// format the message into packed bytes
bytes memory _message = Message.formatMessage(
localDomain,
bytes32(uint256(uint160(msg.sender))),
_destinationDomain,
_recipientAddress,
_messageBody
);
// insert the hashed message into the Merkle tree
bytes32 _messageHash = keccak256(
abi.encodePacked(_message, _leafIndex)
);
tree.insert(_messageHash);
// Emit Dispatch event with message information
emit Dispatch(_messageHash, _leafIndex, _destinationDomain, _message);
return _leafIndex;
}
/**
* @notice Checkpoints the latest root and index.
* Validators are expected to sign this checkpoint so that it can be
* relayed to the Inbox contracts. Checkpoints for a single message (i.e.
* count = 1) are disallowed since they make checkpoint tracking more
* difficult.
* @dev emits Checkpoint event
*/
function checkpoint() external override notFailed {
uint256 count = count();
require(count > 1, "!count");
bytes32 root = root();
_checkpoint(root, count - 1);
}
/**
* @notice Set contract state to FAILED.
Multisig validator manager contracts & tooling (#334) * All contracts working together, need to separate local vs remote, clean up natspec, and add tests * Create Inbox/Outbox specific MultisigValidatorManagers * New Inbox.sol tests * MultisigValidatorManager tests * Comment for getCheckpointSignatures * Fix build * InboxMultisigValidatorManager tests * OutboxMultisigValidatorManager tests * Prettier * Test rust-produced domain hashes * Delete old ValidatorManager, rm IValidatorManager reference in Common.sol * rm validatorManager test, rm unused AbacusDeployment * Rm test/index.ts * Modify TestAbacusDeploy to work with new validator managers * Add inbox / outbox multisig validator managers to core contracts * Update core test addresses * Ensure validator set unenrolling does not violate a quorum threshold * Prettier * self nits * All builds passing, fixed invariant checker * nits * Minor fixes * rm IValidatorManager * Nit * ValidatorManager -> validator manager in comments * more moving away from ValidatorManager * some more... * Make domainHash internal as _domainHash, publicly expose in TestMultisigValidatorManager * Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * Revert "Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol" This reverts commit cf54d4b7656e9c830662e122f71a0bce4c49f1da. * some natspec fixes * PR comments, mostly small renames * Add backticks to natspec * Apply suggestions from code review Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * PR comments * Rename quorumThreshold -> threshold * Add isValidator and validatorCount * Add validatorCount to enroll/unenroll events * Remove multisig from sdk / deploy names * Rename InboxMultisig... and OutboxMultisig... to Inbox... and Outbox... * Better checking, check threshold * rm validator managers from TestAbacusDeploy * clean up comments * PR comments Co-authored-by: Yorke Rhodes <yorke@useabacus.network>
3 years ago
* @dev Called by the validator manager when fraud is proven.
*/
Multisig validator manager contracts & tooling (#334) * All contracts working together, need to separate local vs remote, clean up natspec, and add tests * Create Inbox/Outbox specific MultisigValidatorManagers * New Inbox.sol tests * MultisigValidatorManager tests * Comment for getCheckpointSignatures * Fix build * InboxMultisigValidatorManager tests * OutboxMultisigValidatorManager tests * Prettier * Test rust-produced domain hashes * Delete old ValidatorManager, rm IValidatorManager reference in Common.sol * rm validatorManager test, rm unused AbacusDeployment * Rm test/index.ts * Modify TestAbacusDeploy to work with new validator managers * Add inbox / outbox multisig validator managers to core contracts * Update core test addresses * Ensure validator set unenrolling does not violate a quorum threshold * Prettier * self nits * All builds passing, fixed invariant checker * nits * Minor fixes * rm IValidatorManager * Nit * ValidatorManager -> validator manager in comments * more moving away from ValidatorManager * some more... * Make domainHash internal as _domainHash, publicly expose in TestMultisigValidatorManager * Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * Revert "Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol" This reverts commit cf54d4b7656e9c830662e122f71a0bce4c49f1da. * some natspec fixes * PR comments, mostly small renames * Add backticks to natspec * Apply suggestions from code review Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * PR comments * Rename quorumThreshold -> threshold * Add isValidator and validatorCount * Add validatorCount to enroll/unenroll events * Remove multisig from sdk / deploy names * Rename InboxMultisig... and OutboxMultisig... to Inbox... and Outbox... * Better checking, check threshold * rm validator managers from TestAbacusDeploy * clean up comments * PR comments Co-authored-by: Yorke Rhodes <yorke@useabacus.network>
3 years ago
function fail() external override onlyValidatorManager {
// set contract to FAILED
state = States.Failed;
emit Fail();
}
Multisig validator manager contracts & tooling (#334) * All contracts working together, need to separate local vs remote, clean up natspec, and add tests * Create Inbox/Outbox specific MultisigValidatorManagers * New Inbox.sol tests * MultisigValidatorManager tests * Comment for getCheckpointSignatures * Fix build * InboxMultisigValidatorManager tests * OutboxMultisigValidatorManager tests * Prettier * Test rust-produced domain hashes * Delete old ValidatorManager, rm IValidatorManager reference in Common.sol * rm validatorManager test, rm unused AbacusDeployment * Rm test/index.ts * Modify TestAbacusDeploy to work with new validator managers * Add inbox / outbox multisig validator managers to core contracts * Update core test addresses * Ensure validator set unenrolling does not violate a quorum threshold * Prettier * self nits * All builds passing, fixed invariant checker * nits * Minor fixes * rm IValidatorManager * Nit * ValidatorManager -> validator manager in comments * more moving away from ValidatorManager * some more... * Make domainHash internal as _domainHash, publicly expose in TestMultisigValidatorManager * Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * Revert "Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol" This reverts commit cf54d4b7656e9c830662e122f71a0bce4c49f1da. * some natspec fixes * PR comments, mostly small renames * Add backticks to natspec * Apply suggestions from code review Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * PR comments * Rename quorumThreshold -> threshold * Add isValidator and validatorCount * Add validatorCount to enroll/unenroll events * Remove multisig from sdk / deploy names * Rename InboxMultisig... and OutboxMultisig... to Inbox... and Outbox... * Better checking, check threshold * rm validator managers from TestAbacusDeploy * clean up comments * PR comments Co-authored-by: Yorke Rhodes <yorke@useabacus.network>
3 years ago
/**
* @notice Returns whether the provided root and index are a known
* checkpoint.
* @param _root The merkle root.
* @param _index The index.
* @return TRUE iff `_root` and `_index` are a known checkpoint.
*/
function isCheckpoint(bytes32 _root, uint256 _index)
external
view
override
returns (bool)
{
// Checkpoints are zero-indexed, but checkpoints of index 0 are disallowed
Multisig validator manager contracts & tooling (#334) * All contracts working together, need to separate local vs remote, clean up natspec, and add tests * Create Inbox/Outbox specific MultisigValidatorManagers * New Inbox.sol tests * MultisigValidatorManager tests * Comment for getCheckpointSignatures * Fix build * InboxMultisigValidatorManager tests * OutboxMultisigValidatorManager tests * Prettier * Test rust-produced domain hashes * Delete old ValidatorManager, rm IValidatorManager reference in Common.sol * rm validatorManager test, rm unused AbacusDeployment * Rm test/index.ts * Modify TestAbacusDeploy to work with new validator managers * Add inbox / outbox multisig validator managers to core contracts * Update core test addresses * Ensure validator set unenrolling does not violate a quorum threshold * Prettier * self nits * All builds passing, fixed invariant checker * nits * Minor fixes * rm IValidatorManager * Nit * ValidatorManager -> validator manager in comments * more moving away from ValidatorManager * some more... * Make domainHash internal as _domainHash, publicly expose in TestMultisigValidatorManager * Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * Revert "Update solidity/core/contracts/validator-manager/InboxMultisigValidatorManager.sol" This reverts commit cf54d4b7656e9c830662e122f71a0bce4c49f1da. * some natspec fixes * PR comments, mostly small renames * Add backticks to natspec * Apply suggestions from code review Co-authored-by: Yorke Rhodes <yorke@useabacus.network> * PR comments * Rename quorumThreshold -> threshold * Add isValidator and validatorCount * Add validatorCount to enroll/unenroll events * Remove multisig from sdk / deploy names * Rename InboxMultisig... and OutboxMultisig... to Inbox... and Outbox... * Better checking, check threshold * rm validator managers from TestAbacusDeploy * clean up comments * PR comments Co-authored-by: Yorke Rhodes <yorke@useabacus.network>
3 years ago
return _index > 0 && checkpoints[_root] == _index;
}
// ============ Internal Functions ============
/**
* @notice Internal utility function that combines
* `_destination` and `_nonce`.
* @dev Both destination and nonce should be less than 2^32 - 1
* @param _destination Domain of destination chain
* @param _nonce Current nonce for given destination chain
* @return Returns (`_destination` << 32) & `_nonce`
*/
function _destinationAndNonce(uint32 _destination, uint32 _nonce)
internal
pure
returns (uint64)
{
return (uint64(_destination) << 32) | _nonce;
}
}