Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

LibBinaryMerkleTree

Git Source

State Variables

LOG2_MAX_DATA_BLOCK_SIZE

Log2 of the maximum data block size.

The data block must still be smaller than the drive. We limit the size of data blocks because of the block gas limit.

uint256 constant LOG2_MAX_DATA_BLOCK_SIZE = 12

Functions

merkleRootAfterReplacement

Compute the root of a Merkle tree after replacing one of its nodes.

Level of node is deduced by the length of the siblings array.

Raises an InvalidNodeIndex error if an invalid node index is provided.

function merkleRootAfterReplacement(
    bytes32[] calldata sibs,
    uint256 nodeIndex,
    bytes32 node,
    function(bytes32, bytes32) pure returns (bytes32) nodeFromChildren
) internal pure returns (bytes32);

Parameters

NameTypeDescription
sibsbytes32[]The siblings of the node in bottom-up order
nodeIndexuint256The index of the node
nodebytes32The new node
nodeFromChildrenfunction (bytes32, bytes32) pure returns (bytes32)The function that computes nodes from their children

Returns

NameTypeDescription
<none>bytes32The root hash of the new Merkle tree

merkleRoot

Get the Merkle root of a byte array.

Data blocks are right-padded with zeros if necessary.

leafFromDataAt receives the data, the block index, and the block size

function merkleRoot(
    bytes memory data,
    uint256 log2DriveSize,
    uint256 log2DataBlockSize,
    function(bytes memory, uint256, uint256) pure returns (bytes32) leafFromDataAt,
    function(bytes32, bytes32) pure returns (bytes32) nodeFromChildren
) internal pure returns (bytes32);

Parameters

NameTypeDescription
databytesThe byte array
log2DriveSizeuint256The log2 of the drive size
log2DataBlockSizeuint256The log2 of the data block size
leafFromDataAtfunction (bytes memory, uint256, uint256) pure returns (bytes32)The function that computes leaves from data blocks
nodeFromChildrenfunction (bytes32, bytes32) pure returns (bytes32)The function that computes nodes from their children