import { ChangeSet } from "./generated/changeset.cjs"; import { ExportNode } from "./export/types.cjs"; import { Exporter } from "./export/exporter.cjs"; import { CommitmentProof } from "./generated/cosmos/ics23/v1/proofs.cjs"; import { TreeIterator } from "./iterator/iterator.cjs"; import { Node, NodeGetter } from "./node.cjs"; import { Logger } from "winston"; //#region src/immutable-tree.d.ts /** * ImmutableTree represents a read-only snapshot of the tree at a specific version. */ declare class ImmutableTree { /** Root node of the tree */ readonly root: Node | null; /** NodeDB instance for loading nodes (forward reference using NodeGetter interface) */ readonly ndb: NodeGetter; /** Version of this tree snapshot */ readonly version: bigint; /** Whether to skip fast storage upgrade (use traditional IAVL node storage only) */ readonly skipFastStorageUpgrade: boolean; /** Logger instance for diagnostics */ readonly logger: Logger | null; /** * Creates a new ImmutableTree instance. * * @param root - Root node of the tree (null for empty tree) * @param ndb - NodeDB instance for loading nodes * @param version - Version number of this tree snapshot * @param skipFastStorageUpgrade - Whether to skip fast storage (default: false) * @param logger - Optional logger instance */ constructor(root: Node | null, ndb: NodeGetter, version: bigint, skipFastStorageUpgrade?: boolean, logger?: Logger | null); /** * Returns the version of the tree. * * @returns Version number */ getVersion(): bigint; /** * Returns the root node of the tree. * * @returns Root node or null if tree is empty */ getRoot(): Node | null; /** * Returns whether the tree is empty. * * @returns true if tree has no nodes */ isEmpty(): boolean; /** * Returns the number of leaf nodes in the tree. * * @returns Total number of key-value pairs */ size(): bigint; /** * Returns the height of the tree. * Height is defined as the number of edges in the longest path from root to leaf. * An empty tree has height 0, a single node has height 0, two levels have height 1. * * @returns Tree height */ height(): number; /** * Returns the root hash of the tree. * Computes the hash if it hasn't been computed yet. * Returns null for empty trees. * * @returns Root hash or null for empty tree */ hash(): Uint8Array | null; /** * Returns whether a key exists in the tree. * * @param key - The key to check * @returns true if key exists, false otherwise */ has(key: Uint8Array): Promise; /** * Creates a shallow clone of this ImmutableTree. * The clone shares the same root node and NodeDB reference. * Used for creating snapshots (e.g., lastSaved in MutableTree). * * @returns A new ImmutableTree instance with same root/version/ndb */ clone(): ImmutableTree; /** * Returns the value for a given key. * The returned value must not be modified, since it may point to data stored within IAVL. * * Employs fast storage optimization when available for better performance. * This matches Go IAVL's Get() implementation (immutable_tree.go:180-216). * * @param key - The key to look up * @returns The value, or null if key doesn't exist */ get(key: Uint8Array): Promise; /** * Returns the index and value of the specified key if it exists, or the next index otherwise. * The returned value must not be modified, since it may point to data stored within IAVL. * * The index is the index in the list of leaf nodes sorted lexicographically by key. * The leftmost leaf has index 0, its neighbor has index 1, and so on. * * @param key - The key to look up * @returns Object with index and value (value is null if key doesn't exist) */ getWithIndex(key: Uint8Array): Promise<{ index: bigint; value: Uint8Array | null; }>; /** * Gets the key and value at the specified index. * Index represents position in the sorted list of keys (0-based). * * @param index - The index to look up (0 = leftmost leaf) * @returns Object with key and value, or null if index is out of bounds */ getByIndex(index: bigint): Promise<{ key: Uint8Array; value: Uint8Array; } | null>; /** * Creates an iterator over the tree in ascending order. * * @param start - Start key (null to start from beginning) * @param end - End key (null for no limit) * @returns TreeIterator instance */ iterator(start?: Uint8Array | null, end?: Uint8Array | null): TreeIterator; /** * Creates an iterator over the tree in descending order. * * @param start - Start key (null to start from end) * @param end - End key (null for no limit) * @returns TreeIterator instance */ reverseIterator(start?: Uint8Array | null, end?: Uint8Array | null): TreeIterator; /** * Iterates over all keys in the tree, calling a function for each key-value pair. * * Corresponds to Go's ImmutableTree.Iterate() * * @param fn - Function to call for each key-value pair. Return true to stop iteration. * @returns True if iteration was stopped early, false if completed */ iterate(fn: (key: Uint8Array, value: Uint8Array) => boolean): Promise; /** * Iterates over a range of keys, calling a function for each key-value pair. * End key is exclusive (non-inclusive). * * Corresponds to Go's ImmutableTree.IterateRange() * * @param start - Start key (null to start from beginning) * @param end - End key (exclusive, null for no limit) * @param ascending - True for ascending order, false for descending * @param fn - Function to call for each key-value pair. Return true to stop iteration. * @returns True if iteration was stopped early, false if completed */ iterateRange(start: Uint8Array | null, end: Uint8Array | null, ascending: boolean, fn: (key: Uint8Array, value: Uint8Array) => boolean): Promise; /** * Iterates over a range of keys, calling a function for each key-value-version triple. * End key is inclusive. * * Corresponds to Go's ImmutableTree.IterateRangeInclusive() * * @param start - Start key (null to start from beginning) * @param end - End key (inclusive, null for no limit) * @param ascending - True for ascending order, false for descending * @param fn - Function to call for each key-value-version triple. Return true to stop iteration. * @returns True if iteration was stopped early, false if completed */ iterateRangeInclusive(start: Uint8Array | null, end: Uint8Array | null, ascending: boolean, fn: (key: Uint8Array, value: Uint8Array, version: bigint) => boolean): Promise; /** * Get Membership Proof generates an ICS23 existence proof for a key. * * Returns a CommitmentProof that the given key (and its value) exists in the IAVL tree. * If the key doesn't exist in the tree, this will return an error. * * The proof can be verified by a light client using the ICS23 verification library * with the tree's root hash. * * Based on Go IAVL v1.3.5 proof_ics23.go GetMembershipProof (lines 14-25) * * @param key - The key to generate a proof for * @returns CommitmentProof containing an ExistenceProof * @throws Error if key doesn't exist in the tree */ getMembershipProof(key: Uint8Array): Promise; /** * Get Non-Membership Proof generates an ICS23 non-existence proof for a key. * * Returns a CommitmentProof that the given key doesn't exist in the IAVL tree. * If the key exists in the tree, this will return an error. * * The proof includes existence proofs for the keys immediately to the left * and right of where the queried key would be if it existed. If the keys * are adjacent and neither equals the queried key, then the queried key * cannot exist. * * Based on Go IAVL v1.3.5 proof_ics23.go GetNonMembershipProof (lines 42-89) * * @param key - The key to generate a non-existence proof for * @returns CommitmentProof containing a NonExistenceProof * @throws Error if key exists in the tree */ getNonMembershipProof(key: Uint8Array): Promise; /** * Create Existence Proof generates an ICS23 ExistenceProof for a key. * * This is an internal helper method that: * 1. Calls root.pathToLeaf() to get the proof path * 2. Converts the path to ICS23 format using convertLeafOp/convertInnerOps * 3. Returns an ExistenceProof with the key, value, leaf op, and inner ops * * The returned proof can be used to verify that a key-value pair exists * in the tree by hashing up from the leaf to the root. * * Based on Go IAVL v1.3.5 proof_ics23.go createExistenceProof (lines 100-113) * * @param key - The key to generate a proof for * @returns ExistenceProof for the key * @throws Error if key doesn't exist (returns proof for adjacent key) */ private createExistenceProof; /** * Verify Membership verifies an ICS23 membership proof for a key-value pair. * * Returns true if the proof is valid and proves that the key-value pair exists * in the tree at the committed root hash. * * Based on Go IAVL v1.3.5 proof_ics23.go VerifyMembership (lines 28-36) * * @param proof - The commitment proof to verify * @param key - The key being proven * @returns true if proof is valid, false otherwise */ verifyMembership(proof: CommitmentProof, key: Uint8Array): Promise; /** * Verify Non-Membership verifies an ICS23 non-membership proof for a key. * * Returns true if the proof is valid and proves that the key does not exist * in the tree at the committed root hash. * * Based on Go IAVL v1.3.5 proof_ics23.go VerifyNonMembership (lines 92-96) * * @param proof - The commitment proof to verify * @param key - The key being proven absent * @returns true if proof is valid, false otherwise */ verifyNonMembership(proof: CommitmentProof, key: Uint8Array): boolean; /** * Get Proof gets the proof for the given key. * Returns a membership proof if the key exists, or a non-membership proof if it doesn't. * * Based on Go IAVL v1.3.5 proof_ics23.go GetProof (lines 178-192) * * @param key - The key to generate a proof for * @returns CommitmentProof (either membership or non-membership) * @throws Error if root is null */ getProof(key: Uint8Array): Promise; /** * Verify Proof checks if the proof is correct for the given key. * Automatically determines if it's a membership or non-membership proof and verifies accordingly. * * Based on Go IAVL v1.3.5 proof_ics23.go VerifyProof (lines 195-200) * * @param proof - The proof to verify * @param key - The key the proof is for * @returns True if proof is valid, false otherwise */ verifyProof(proof: CommitmentProof, key: Uint8Array): Promise; /** * Export creates an Exporter for this tree. * Exported nodes can be imported into an empty tree with MutableTree.import(). * Nodes are exported depth-first post-order (LRN), this order must be preserved * when importing in order to recreate the same tree structure. * * Based on Go IAVL immutable_tree.go lines 159-161. * * @returns An Exporter that yields nodes in post-order * @throws Error if tree is nil or has no root * * @example * ```typescript * const exporter = tree.export(); * for await (const node of exporter) { * console.log(`Key: ${node.key}, Version: ${node.version}`); * } * ``` */ export(): Exporter; /** * Create a compressed exporter for this tree. The compressed exporter * applies delta encoding to reduce export size: * - Branch keys are skipped (can be derived during import) * - Leaf keys are delta-encoded compared to previous leaf * - Branch node versions are delta-encoded compared to max version in children * * @returns An async iterable that yields compressed export nodes * * @example * ```typescript * const exporter = tree.exportCompressed(); * for await (const node of exporter) { * // Process compressed node * } * ``` */ exportCompressed(): AsyncIterable; /** * Traverses state changes between a range of versions. * For each version in the range, extracts the changes from the previous version * and calls the callback function with the version number and the ChangeSet. * * This is useful for extracting a stream of changes across multiple versions, * for example to build a changelog or to replicate state changes. * * Based on Go IAVL v1.3.5 immutable_tree.go TraverseStateChanges * * @param startVersion - First version to extract changes for * @param endVersion - Last version to extract changes for * @param fn - Callback function to receive changes for each version * @returns Promise that resolves when traversal is complete * * @example * ```typescript * await tree.traverseStateChanges(1n, 10n, async (version, changeSet) => { * console.log(`Version ${version}: ${changeSet.pairs.length} changes`); * // Return true to stop iteration * return false; * }); * ``` */ traverseStateChanges(startVersion: bigint, endVersion: bigint, fn: (version: bigint, changeSet: ChangeSet) => Promise): Promise; /** * Returns a string representation of all leaves in the tree for debugging. * Format: "Tree{key1: value1, key2: value2, ...}" * * Corresponds to Go's ImmutableTree.String() * * Note: In TypeScript, this must be async because tree traversal requires * database access. This differs from Go where String() is synchronous. * * @returns Promise of string representation showing all key-value pairs */ toString(): Promise; /** * Returns a visual representation of the tree structure for debugging. * Each line represents a node, with indentation showing depth. * * Corresponds to Go's ImmutableTree.RenderShape() * * @param indent - String to use for each level of indentation (default: " ") * @param encoder - Optional custom encoder function for node IDs * @returns Array of strings representing the tree structure */ renderShape(indent?: string, encoder?: (id: Uint8Array, depth: number, isLeaf: boolean) => string): Promise; /** * Recursively renders a node and its children. * Helper for renderShape(). * * @param node - Node to render * @param indent - Indentation string * @param depth - Current depth in tree * @param encoder - Function to encode node IDs * @returns Array of strings for this node and its subtree */ private renderNode; } //#endregion export { ImmutableTree }; //# sourceMappingURL=immutable-tree.d.cts.map