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.
132 lines
4.3 KiB
132 lines
4.3 KiB
3 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
pragma solidity >=0.6.11;
|
||
|
|
||
|
// ============ Internal Imports ============
|
||
|
import {IValidatorManager} from "../interfaces/IValidatorManager.sol";
|
||
3 years ago
|
import {Outbox} from "./Outbox.sol";
|
||
3 years ago
|
// ============ External Imports ============
|
||
|
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
|
||
|
import {ECDSA} from "@openzeppelin/contracts/cryptography/ECDSA.sol";
|
||
|
|
||
|
/**
|
||
|
* @title ValidatorManager
|
||
|
* @author Celo Labs Inc.
|
||
|
* @notice MVP version of contract that will manage Validator selection and
|
||
|
* rotataion.
|
||
|
*/
|
||
|
contract ValidatorManager is IValidatorManager, Ownable {
|
||
|
// Mapping of domain -> validator address.
|
||
|
mapping(uint32 => address) public validators;
|
||
|
|
||
|
// ============ Events ============
|
||
|
|
||
|
/**
|
||
3 years ago
|
* @notice Emitted when a validator is enrolled
|
||
|
* @param domain The domain for which the validator is being enrolled
|
||
3 years ago
|
* @param validator The address of the validator
|
||
|
*/
|
||
3 years ago
|
event ValidatorEnrolled(uint32 indexed domain, address indexed validator);
|
||
3 years ago
|
|
||
|
/**
|
||
|
* @notice Emitted when proof of an improper checkpoint is submitted,
|
||
|
* which sets the contract to FAILED state
|
||
|
* @param root Root of the improper checkpoint
|
||
|
* @param index Index of the improper checkpoint
|
||
|
* @param signature Signature on `root` and `index`
|
||
|
*/
|
||
|
event ImproperCheckpoint(
|
||
3 years ago
|
address indexed outbox,
|
||
3 years ago
|
uint32 indexed domain,
|
||
|
address indexed validator,
|
||
|
bytes32 root,
|
||
|
uint256 index,
|
||
|
bytes signature
|
||
|
);
|
||
|
|
||
|
// ============ Constructor ============
|
||
|
|
||
|
constructor() Ownable() {}
|
||
|
|
||
|
// ============ External Functions ============
|
||
|
|
||
|
/**
|
||
3 years ago
|
* @notice Enroll a validator for the given domain
|
||
3 years ago
|
* @dev only callable by trusted owner
|
||
|
* @param _domain The domain for which the validator is being set
|
||
|
* @param _validator The address of the validator
|
||
|
*/
|
||
3 years ago
|
function enrollValidator(uint32 _domain, address _validator)
|
||
3 years ago
|
external
|
||
|
onlyOwner
|
||
|
{
|
||
|
validators[_domain] = _validator;
|
||
3 years ago
|
emit ValidatorEnrolled(_domain, _validator);
|
||
3 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Check if an Checkpoint is an Improper Checkpoint;
|
||
3 years ago
|
* if so, set the provided Outbox contract to FAILED state.
|
||
3 years ago
|
*
|
||
|
* An Improper Checkpoint is an checkpoint that was not previously checkpointed.
|
||
3 years ago
|
* @param _outbox Address of the Outbox contract to set to FAILED.
|
||
3 years ago
|
* @param _root Merkle root of the improper checkpoint
|
||
|
* @param _index Index root of the improper checkpoint
|
||
|
* @param _signature Validator signature on `_root` and `_index`
|
||
|
* @return TRUE if checkpoint was an Improper Checkpoint (implying Validator was slashed)
|
||
|
*/
|
||
|
function improperCheckpoint(
|
||
3 years ago
|
address _outbox,
|
||
3 years ago
|
bytes32 _root,
|
||
|
uint256 _index,
|
||
|
bytes memory _signature
|
||
|
) external returns (bool) {
|
||
3 years ago
|
uint32 _domain = Outbox(_outbox).localDomain();
|
||
3 years ago
|
require(
|
||
|
isValidatorSignature(_domain, _root, _index, _signature),
|
||
|
"!validator sig"
|
||
|
);
|
||
3 years ago
|
require(Outbox(_outbox).checkpoints(_root) != _index, "!improper");
|
||
|
Outbox(_outbox).fail();
|
||
3 years ago
|
emit ImproperCheckpoint(
|
||
3 years ago
|
_outbox,
|
||
3 years ago
|
_domain,
|
||
|
validators[_domain],
|
||
|
_root,
|
||
|
_index,
|
||
|
_signature
|
||
|
);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// ============ Public Functions ============
|
||
|
|
||
|
/**
|
||
|
* @notice Checks that signature was signed by Validator
|
||
3 years ago
|
* @param _domain Domain of Outbox contract
|
||
3 years ago
|
* @param _root Merkle root
|
||
|
* @param _index Corresponding leaf index
|
||
|
* @param _signature Signature on `_root` and `_index`
|
||
|
* @return TRUE iff signature is valid signed by validator
|
||
|
**/
|
||
|
function isValidatorSignature(
|
||
|
uint32 _domain,
|
||
|
bytes32 _root,
|
||
|
uint256 _index,
|
||
|
bytes memory _signature
|
||
|
) public view override returns (bool) {
|
||
|
bytes32 _digest = keccak256(
|
||
|
abi.encodePacked(domainHash(_domain), _root, _index)
|
||
|
);
|
||
|
_digest = ECDSA.toEthSignedMessageHash(_digest);
|
||
|
return (ECDSA.recover(_digest, _signature) == validators[_domain]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @notice Hash of domain concatenated with "OPTICS"
|
||
|
* @param _domain the domain to hash
|
||
|
*/
|
||
|
function domainHash(uint32 _domain) public pure returns (bytes32) {
|
||
|
return keccak256(abi.encodePacked(_domain, "OPTICS"));
|
||
|
}
|
||
|
}
|