RAILGUN Reloaded
    Preparing search index...

    Sparse Merkle Tree implementation. Extends the base Merkle Tree, but provides the convenience of appending nodes at the end of the tree.

    Hierarchy (View Summary)

    Index
    • Internal method to get a reference to the buffer slice for a node at a given index

      Parameters

      • index: number

        element-wise index

      Returns Uint8Array<ArrayBufferLike>

      subarray of bytesPerElement aligned to a multiple of the node size

    • Internal method to set a node at a given

      Parameters

      • index: number

        element-wise index

      • value: Readonly<Uint8Array>

        bytesPerElement-sized buffer to copy

      Returns void

    • Get a reference to the leaf node at a given index

      Parameters

      • index: number

        Zero-based index of the leaf node. Out of bounds indices will throw an error

      Returns Readonly<Uint8Array>

      Read-only zero-copy leaf node

    • Insert nodes at a given index of the tree. Supports either a single node or an array of nodes, efficiently updating parents. Note only up to size elements can be filled, overfilling will cause an assertion error.

      Parameters

      • index: number

        start index of first element

      • nodes: Readonly<Uint8Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike>[]>

        single element or sequence of consecutive elements

      Returns number

      index after last element inserted. Useful for chaining inserts

    • Generate an inclusion proof for a given index. Returns an object with the index, root and path elements, where path elements are the first sibling and uncles of the node up to the root.

      Parameters

      • index: number

        Leaf index to prove

      Returns MerkleProof

      Merkle proof for the given leaf index

    • Append nodes at the end of the tree

      Parameters

      • nodes: Uint8Array<ArrayBufferLike> | Uint8Array<ArrayBufferLike>[]

        Either a single node or list of nodes to append

      Returns number

      new length of the tree

    LEAF_LEVEL: number = 0

    Level of leaf nodes in the tree. All leaf nodes are at this level, while all internal nodes are at higher levels.

    buf: Uint8Array

    Underlying buffer holding all tree nodes

    3-arity hash function deriving parents. First argument is output buffer to be populated with the hash digest

    bytesPerElement: number

    Bytes per node

    capacity: number

    Total number of nodes in the tree

    _rootIndex: number

    Cached index of root node

    size: number

    Total number of leaf nodes in the tree

    depth: number

    Depth of the tree

    length: number

    Number of filled leaves in the tree

    create: (params: CreateParams) => SparseMerkleTree

    Create a new SparseMerkleTree instance with the given parameters, allocating memory to contain all nodes. Initializes the tree with the zero element. Returns an instance to use

    Type Declaration

      • (params: CreateParams): SparseMerkleTree
      • Parameters

        • params: CreateParams

          Creation parameters

          • depth

            Depth of the tree, excluding the root node. Must be at least 1

          • hashFn

            Hash function to use for internal nodes. Must follow the InplaceMerkleHashFn signature

          • zeroElement

            Zero element to use for leaf nodes. Internal nodes are calculated up to the root. Must be at least 1 byte

          • length

            Number of filled leaves in the tree. Defaults to 0

        Returns SparseMerkleTree

        Instance of MerkleTree

    from: (
        obj: Serialized,
        params: Omit<ConstructorParams, keyof Serialized>,
    ) => SparseMerkleTree

    Create a new SparseMerkleTree instance from a serialized object and global parameters

    Type Declaration

      • (
            obj: Serialized,
            params: Omit<ConstructorParams, keyof Serialized>,
        ): SparseMerkleTree
      • Parameters

        • obj: Serialized

          Serialized object of the merkle tree

        • params: Omit<ConstructorParams, keyof Serialized>

          Additional system parameters

        Returns SparseMerkleTree

        Instance