Skip to main content

Chronicle

Git Source

Inherits: Ownable

Author: sina.eth

The core Chronicle smart contract.

The Chronicle smart contract tracks a merkle mountain range and enforces that any newly posted merkle root is consistent with the previous root. TODO(sina) use custom errors instead of requires.

State Variables​

rootCache​

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

param root The root hash for the checkpoint.

return treeSize The tree size corresponding to the root.

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

Returns 0 if the root hash is not in the mapping.

mapping(bytes32 => uint256) public rootCache;

currentRoot​

The current root hash.

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

bytes32 public currentRoot;

Functions​

constructor​

Emits an {Ownable.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 returns (bytes32, uint256);

Returns

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

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(
uint256 index,
bytes32 leaf,
bytes32[] calldata leftRange,
bytes32[] calldata rightRange,
bytes32 targetRoot
) external view;

Parameters

NameTypeDescription
indexuint256The index of the leaf to be verified in the tree.
leafbytes32The leaf to be verified.
leftRangebytes32[]The left range of the proof.
rightRangebytes32[]The right range of the proof.
targetRootbytes32The root of the tree the proof is being verified against.

safeVerifyProof​

seedHeight=

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(
uint256 index,
bytes32 leaf,
bytes32[] calldata leftRange,
bytes32[] calldata rightRange,
bytes32 targetRoot
) external view returns (bool isValid);

Parameters

NameTypeDescription
indexuint256The index of the leaf to be verified in the tree.
leafbytes32The leaf to be verified.
leftRangebytes32[]The left range of the proof.
rightRangebytes32[]The right range of the proof.
targetRootbytes32The root of the tree the proof is being verified against.

Returns

NameTypeDescription
isValidboolWhether the proof is valid.

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 onlyOwner;

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.

Events​

RootUpdated​

Emitted when the root is updated.

event RootUpdated(bytes32 indexed newRoot, uint256 indexed newSize);