import { NodeKey } from "./node-key.cjs"; import { ProofInnerNode } from "./proof/proof-inner-node.cjs"; //#region src/node.d.ts /** * Interface for loading nodes from storage. * This allows traversal methods to work without depending on the full tree implementation. */ interface NodeGetter { /** * Gets a node by its key (12 bytes for v1, 32 bytes for legacy). * @param nodeKey - The node's database key * @returns The node, or null if not found */ getNode(nodeKey: Uint8Array): Promise; } /** * Node represents a node in the IAVL tree. * * Leaf nodes: * - Have key and value * - Have subtreeHeight = 0 * - Have size = 1 * - Do not have children * * Branch nodes: * - Have key (from leftmost descendant) * - Have left and right children (references or pointers) * - Have subtreeHeight > 0 * - Have size = sum of children sizes */ declare class Node { /** Key for leaf nodes, leftmost descendant key for branch nodes */ key: Uint8Array | null; /** Value for leaf nodes only */ value: Uint8Array | null; /** Cached hash of the node, computed lazily */ hash: Uint8Array | null; /** NodeKey (version + nonce) for this node in the database */ nodeKey: NodeKey | null; /** * Left child node key (12 bytes) or legacy hash (32 bytes) * For v1: 12-byte NodeKey * For legacy: 32-byte hash */ leftNodeKey: Uint8Array | null; /** * Right child node key (12 bytes) or legacy hash (32 bytes) * For v1: 12-byte NodeKey * For legacy: 32-byte hash */ rightNodeKey: Uint8Array | null; /** Cached left child node */ leftNode: Node | null; /** Cached right child node */ rightNode: Node | null; /** Height of the subtree rooted at this node (0 for leaves) */ subtreeHeight: number; /** Number of nodes in the subtree rooted at this node (1 for leaves) */ size: bigint; /** * Whether this node uses legacy format (hash-based child references) * Legacy nodes use 32-byte hashes instead of 12-byte NodeKeys */ isLegacy: boolean; /** * Creates a new leaf node with the given key and value. * * @param key - The key for this leaf node * @param value - The value for this leaf node * @returns A new leaf node */ static newLeafNode(key: Uint8Array, value: Uint8Array): Node; /** * Creates a new branch node with the given children. * * @param left - The left child node * @param right - The right child node * @returns A new branch node */ static newBranchNode(left: Node, right: Node): Node; /** * Checks if this node is a leaf node. * A leaf node has subtreeHeight = 0. * * @returns true if this is a leaf node, false otherwise */ isLeaf(): boolean; /** * Gets the key of this node. * For legacy nodes, returns the hash. * For v1 nodes, returns the NodeKey bytes. * * @returns The node's key as a byte array */ getNodeKey(): Uint8Array; /** * Gets the balance factor of this node. * Balance factor = left.height - right.height * * @returns The balance factor (-2 to +2 for balanced tree) */ getBalanceFactor(): number; /** * Recalculates and updates the height and size of this node based on its children. * This should be called after any modification to child nodes. * * IMPORTANT: This method loads children from storage if needed. * For performance, prefer to call this only after structural changes. * * @param getter - NodeGetter for loading child nodes from storage if needed */ calcHeightAndSize(getter: NodeGetter): Promise; /** * Validates the node structure. * Checks for common invariant violations. * * @returns Error message if invalid, null if valid */ validate(): string | null; /** * Returns a string representation of the node. * Useful for debugging. * * @returns String representation */ toString(): string; /** * Computes and caches the hash of this node. * The hash is computed based on the node type: * - For leaves: SHA256(height || size || version || key || valueHash) * - For branches: SHA256(height || size || version || leftHash || rightHash) * * @param version - The version to use for hash computation * @returns The 32-byte hash of this node */ computeHash(version: bigint): Uint8Array; /** * Recursively computes and caches hashes for this node and all descendants. * This is used when computing the working hash of a tree that has unsaved nodes. * Corresponds to Go's hashWithCount method. * * @param version - Version to use for hash computation * @returns The computed hash */ computeHashWithDescendants(version: bigint): Uint8Array; /** * Builds the byte array to be hashed for this node. * This is the internal implementation that constructs the hash input. * * @param version - The version to use for hash computation * @returns The byte array to be hashed * @private */ private _buildHashInput; /** * Serializes this node to bytes. * The format depends on whether this is a leaf or branch node. * * Leaf format: * height (varint) || size (varint) || key (length-prefixed) || value (length-prefixed) * * Branch format: * height (varint) || size (varint) || key (length-prefixed) || hash (33 bytes) || * mode (varint) || leftNodeKey || rightNodeKey * * @returns Serialized bytes */ toBytes(): Uint8Array; /** * Deserializes a node from bytes. * The nodeKey parameter identifies this node in the database. * * @param nodeKey - The NodeKey bytes (12 bytes for v1, 32 bytes for legacy) * @param bytes - Serialized node bytes * @returns Deserialized node */ static fromBytes(nodeKey: Uint8Array, bytes: Uint8Array): Node; /** * Gets the left child node, loading it from storage if needed. * @param getter - NodeGetter for loading nodes from storage * @returns The left child node, or null if not found */ getLeftNode(getter: NodeGetter): Promise; /** * Gets the right child node, loading it from storage if needed. * @param getter - NodeGetter for loading nodes from storage * @returns The right child node, or null if not found */ getRightNode(getter: NodeGetter): Promise; /** * Creates a clone of this node for copy-on-write operations. * Leaf nodes cannot be cloned (throw error). * Branch nodes are cloned with children loaded from storage if needed. * * IMPORTANT: In JavaScript, we must be careful with reference semantics: * - Primitive fields (subtreeHeight, size) are copied by value * - Object references (key, leftNodeKey, rightNodeKey, leftNode, rightNode) are copied by reference * - The clone shares references to child nodes (copy-on-write semantics) * - hash and nodeKey are set to null (will be recomputed/assigned) * * @param getter - NodeGetter for loading child nodes from storage * @returns A new cloned Node instance * @throws Error if called on a leaf node */ clone(getter: NodeGetter): Promise; /** * Searches for a key in the subtree rooted at this node. * Returns the index and value if found. * * @param getter - NodeGetter for loading nodes from storage * @param key - The key to search for * @returns Object with index (position in tree), value (if found), and error */ get(getter: NodeGetter, key: Uint8Array): Promise<{ index: bigint; value: Uint8Array | null; }>; /** * Gets the key-value pair at the specified index (0-based). * Index represents the position in an in-order traversal. * * @param getter - NodeGetter for loading nodes from storage * @param index - The index to retrieve (0-based) * @returns Object with key and value, or null if index out of bounds */ getByIndex(getter: NodeGetter, index: bigint): Promise<{ key: Uint8Array | null; value: Uint8Array | null; }>; /** * Traverses the subtree in order, calling the callback for each node. * Traversal continues until the callback returns true (stop) or all nodes visited. * * @param getter - NodeGetter for loading nodes from storage * @param ascending - If true, traverse in ascending order; otherwise descending * @param callback - Function called for each node; return true to stop * @returns True if traversal was stopped by callback, false if completed */ traverse(getter: NodeGetter, ascending: boolean, callback: (node: Node) => boolean): Promise; /** * Traverses the entire tree in post-order (LRN: left, right, node). * Post-order traversal visits children before parents, which is required for export. * Based on Go IAVL node.go lines 725-729. * * @param getter - NodeGetter for loading child nodes * @param ascending - If true, traverse in ascending order; otherwise descending * @param callback - Function called for each node; return true to stop * @returns True if traversal was stopped by callback, false if completed */ traversePost(getter: NodeGetter, ascending: boolean, callback: (node: Node) => boolean): Promise; /** * Traverses nodes within a key range, calling the callback for each node. * Supports both ascending and descending order. * * @param getter - NodeGetter for loading nodes from storage * @param start - Start key (inclusive if inclusive=true), or null for no lower bound * @param end - End key (inclusive if inclusive=true), or null for no upper bound * @param ascending - If true, traverse in ascending order; otherwise descending * @param inclusive - If true, include start and end keys; otherwise exclusive * @param callback - Function called for each node; return true to stop * @returns True if traversal was stopped by callback, false if completed */ traverseInRange(getter: NodeGetter, start: Uint8Array | null, end: Uint8Array | null, ascending: boolean, inclusive: boolean, post: boolean, callback: (node: Node) => boolean): Promise; /** * PathToLeaf generates a proof path from this node to the leaf containing the given key. * * If the key does not exist, returns the path to the next leaf left of the key, * except when the key is less than the least item, in which case it returns * a path to the least item. * * The path is ordered ROOT to LEAF, where path[0] is the topmost node and * path[path.length-1] is the leaf's parent. * * Based on Go IAVL v1.3.5 proof.go PathToLeaf (lines 169-173) * * @param getter - NodeGetter for loading nodes from storage * @param key - The key to search for * @returns Object containing the path and the leaf node found * @throws Error if there's an issue loading nodes from storage */ pathToLeaf(getter: NodeGetter, key: Uint8Array): Promise<{ path: ProofInnerNode[]; node: Node; }>; /** * _pathToLeaf is a helper which recursively constructs the PathToLeaf. * As an optimization, the already constructed path is passed in as an argument * and is shared among recursive calls. * * Based on Go IAVL v1.3.5 proof.go pathToLeaf (lines 178-239) * * @param getter - NodeGetter for loading nodes from storage * @param key - The key to search for * @param path - The path being constructed (mutated in place) * @returns The leaf node found * @throws Error if there's an issue loading nodes from storage */ private _pathToLeaf; } //#endregion export { Node, NodeGetter }; //# sourceMappingURL=node.d.cts.map