/** * NodeKey represents a key of a node in the database. * Based on the Go iavl node.go NodeKey implementation. * * Format: 12 bytes total * - Bytes 0-7: version (int64, big-endian) * - Bytes 8-11: nonce (uint32, big-endian) */ /** * NodeKey represents a unique identifier for a node in the IAVL tree. * * The NodeKey consists of: * - version: The tree version when this node was created * - nonce: A unique nonce to distinguish multiple nodes in the same version * * The root node of each version has nonce = 1. */ export class NodeKey { /** Tree version when this node was created */ private readonly _version: bigint; /** Unique nonce for this node within the version */ private readonly _nonce: number; /** * Creates a new NodeKey. * * @param version - Tree version (int64) * @param nonce - Unique nonce (uint32) */ constructor(version: bigint | number, nonce: number) { this._version = typeof version === "bigint" ? version : BigInt(version); this._nonce = nonce >>> 0; // Ensure uint32 } /** * Gets the version. */ get version(): bigint { return this._version; } /** * Gets the nonce. */ get nonce(): number { return this._nonce; } /** * Serializes the NodeKey to a 12-byte Uint8Array. * * Format: * - Bytes 0-7: version (big-endian uint64) * - Bytes 8-11: nonce (big-endian uint32) * * @returns 12-byte representation of the NodeKey */ toBytes(): Uint8Array { const bytes = new Uint8Array(12); // Write version (8 bytes, big-endian) const version = this._version; bytes[0] = Number((version >> 56n) & 0xFFn); bytes[1] = Number((version >> 48n) & 0xFFn); bytes[2] = Number((version >> 40n) & 0xFFn); bytes[3] = Number((version >> 32n) & 0xFFn); bytes[4] = Number((version >> 24n) & 0xFFn); bytes[5] = Number((version >> 16n) & 0xFFn); bytes[6] = Number((version >> 8n) & 0xFFn); bytes[7] = Number(version & 0xFFn); // Write nonce (4 bytes, big-endian) const nonce = this._nonce; bytes[8] = (nonce >> 24) & 0xFF; bytes[9] = (nonce >> 16) & 0xFF; bytes[10] = (nonce >> 8) & 0xFF; bytes[11] = nonce & 0xFF; return bytes; } /** * Deserializes a NodeKey from a 12-byte Uint8Array. * * @param bytes - 12-byte array containing the serialized NodeKey * @returns A new NodeKey instance * @throws {Error} If bytes is not exactly 12 bytes */ static fromBytes(bytes: Uint8Array): NodeKey { if (bytes.length !== 12) { throw new Error(`NodeKey must be exactly 12 bytes, got ${bytes.length}`); } // Read version (8 bytes, big-endian) const version = (BigInt(bytes[0]) << 56n) | (BigInt(bytes[1]) << 48n) | (BigInt(bytes[2]) << 40n) | (BigInt(bytes[3]) << 32n) | (BigInt(bytes[4]) << 24n) | (BigInt(bytes[5]) << 16n) | (BigInt(bytes[6]) << 8n) | BigInt(bytes[7]); // Read nonce (4 bytes, big-endian) const nonce = (bytes[8] << 24) | (bytes[9] << 16) | (bytes[10] << 8) | bytes[11]; return new NodeKey(version, nonce >>> 0); // >>> 0 ensures uint32 } /** * Creates a root node key for a given version. * Root nodes always have nonce = 1. * * @param version - Tree version * @returns A new NodeKey for the root node */ static rootKey(version: bigint | number): NodeKey { return new NodeKey(version, 1); } /** * Compares this NodeKey with another for equality. * * @param other - Another NodeKey to compare * @returns true if both version and nonce are equal */ equals(other: NodeKey): boolean { return this._version === other._version && this._nonce === other._nonce; } /** * Compares this NodeKey with another for ordering. * * @param other - Another NodeKey to compare * @returns negative if this < other, 0 if equal, positive if this > other */ compare(other: NodeKey): number { // First compare versions if (this._version < other._version) { return -1; } if (this._version > other._version) { return 1; } // If versions are equal, compare nonces if (this._nonce < other._nonce) { return -1; } if (this._nonce > other._nonce) { return 1; } return 0; } /** * Returns a string representation of the NodeKey. * * @returns String in format "v{version}n{nonce}" */ toString(): string { return `v${this._version}n${this._nonce}`; } } /** * Creates a root NodeKey for the given version. * This is a convenience function for NodeKey.rootKey(). * * @param version - Tree version * @returns A new NodeKey with nonce = 1 */ export function getRootKey(version: bigint | number): Uint8Array { return NodeKey.rootKey(version).toBytes(); }