Witness

Git Source (opens in a new tab)

Inherits: IWitness, OwnableRoles

Author: sina.eth

The core Witness smart contract.

The Witness smart contract tracks a merkle mountain range and enforces that any newly posted merkle root is consistent with the previous root.

State Variables

UPDATER_ROLE

uint256 public constant UPDATER_ROLE = _ROLE_0;

currentRoot

The current root hash.

This is the root hash of the most recently accepted update.

bytes32 public currentRoot;

_rootInfo

mapping(bytes32 root => RootInfo cache) internal _rootInfo;

Functions

rootInfo

A Mapping of checkpointed root hashes to their corresponding tree data.

function rootInfo(bytes32 root) public view virtual returns (RootInfo memory);

Parameters

NameTypeDescription
rootbytes32The root hash for the checkpoint.

Returns

NameTypeDescription
<none>RootInfoinfo The RootInfo struct containing info about the root hash checkpoint.

rootCache

A mapping of checkpointed root hashes to their corresponding tree sizes.

This mapping is used to keep track of the tree size corresponding to when the contract accepted a given root hash.

function rootCache(bytes32 root) public view virtual returns (uint256);

Parameters

NameTypeDescription
rootbytes32The root hash for the checkpoint.

Returns

NameTypeDescription
<none>uint256treeSize The tree size corresponding to the root.

constructor

Emits an {OwnableRoles.OwnershipTransferred} event.

constructor(address owner);

Parameters

NameTypeDescription
owneraddressThe address that should be set as the initial contract owner.

getCurrentTreeState

Helper util to get the current tree state.

function getCurrentTreeState() external view virtual returns (bytes32, uint256);

Returns

NameTypeDescription
<none>bytes32currentRoot The current root of the tree.
<none>uint256treeSize The current size of the tree.

getLastUpdateTime

Helper util to get the last block.timestamp the tree was updated.

function getLastUpdateTime() external view virtual returns (uint256);

Returns

NameTypeDescription
<none>uint256timestamp The block.timestamp the update was made.

getLastUpdateBlock

Helper util to get the last block.number the tree was updated.

function getLastUpdateBlock() external view virtual returns (uint256);

Returns

NameTypeDescription
<none>uint256block The block.timestamp the update was made.

verifyProof

Verifies a proof for a given leaf. Throws an error if the proof is invalid.

*Notes:

  • For invalid proofs, this method will throw with an error indicating why the proof failed to validate.
  • The proof must validate against a checkpoint the contract has previously accepted.*
function verifyProof(Proof calldata proof) external view virtual;

Parameters

NameTypeDescription
proofProofThe proof to be verified.

safeVerifyProof

Verifies a proof for a given leaf, returning a boolean instead of throwing for invalid proofs.

This method is a wrapper around verifyProof that catches any errors and returns false instead. The params and logic are otherwise the same as verifyProof.

function safeVerifyProof(Proof calldata proof) external view returns (bool isValid);

Parameters

NameTypeDescription
proofProofThe proof to be verified.

updateTreeRoot

Updates the tree root to a larger tree.

*Emits a {RootUpdated} event. Notes:

  • A range proof is verified to ensure the new root is consistent with the previous root.
  • Roots are stored in storage for easier retrieval in the future, along with the treeSize they correspond to. Requirements:
  • msg.sender must be the contract owner.
  • newSize must be greater than the current tree size.
  • oldRange must correspond to the current tree root and size.
  • size check must pass on newRange. After these checks are verified, the new root is calculated based on oldRange and newRange.*
function updateTreeRoot(
    uint256 newSize,
    bytes32[] calldata oldRange,
    bytes32[] calldata newRange
)
    external
    virtual
    onlyRoles(UPDATER_ROLE);

Parameters

NameTypeDescription
newSizeuint256The size of the updated tree.
oldRangebytes32[]A compact range representing the current root.
newRangebytes32[]A compact range representing the diff between oldRange and the new root's coverage.

fallback

Used for L2 calldata optimization. For efficiency, this function will directly return the results, terminating the context. If called internally, it must be called at the end of the function.

fallback() external virtual;