@ethereumjs/binarytree / BinaryTree
Defined in: binaryTree.ts:39
The basic binary tree interface, use with import { BinaryTree } from '@ethereumjs/binarytree'.
A BinaryTree object can be created with the constructor method:
new BinaryTree(
opts):BinaryTree
Defined in: binaryTree.ts:60
Creates a new binary tree.
Options for instantiating the binary tree
Note: in most cases, the static createBinaryTree constructor should be used. It uses the same API but provides sensible defaults
BinaryTree
EMPTY_TREE_ROOT:
Uint8Array
Defined in: binaryTree.ts:44
The root for an empty tree
checkpoint():
void
Defined in: binaryTree.ts:667
Creates a checkpoint that can later be reverted to or committed.
After this is called, all changes can be reverted until commit is called.
void
checkRoot(
root):Promise<boolean>
Defined in: binaryTree.ts:132
Checks if a given root exists.
Uint8Array
Promise<boolean>
commit():
Promise<void>
Defined in: binaryTree.ts:676
Commits a checkpoint to disk, if current checkpoint is not nested. If nested, only sets the parent checkpoint as current checkpoint.
Promise<void>
If not during a checkpoint phase
createBinaryProof(
key):Promise<Uint8Array<ArrayBufferLike>[]>
Defined in: binaryTree.ts:600
Creates a proof from a tree and key that can be verified using verifyBinaryProof.
Uint8Array
a 32 byte binary tree key (31 byte stem + 1 byte suffix)
Promise<Uint8Array<ArrayBufferLike>[]>
createReadStream():
any
Defined in: binaryTree.ts:618
The data event is given an Object that has two properties; the key and the value. Both should be Uint8Arrays.
any
Returns a stream of the contents of the tree
createRootNode():
Promise<void>
Defined in: binaryTree.ts:553
Create empty root node for initializing an empty tree.
Promise<void>
del(
stem,suffixes):Promise<void>
Defined in: binaryTree.ts:545
Deletes a given key from the tree.
Uint8Array
the stem of the stem node to delete from
number[]
the suffixes to delete
Promise<void>
A Promise that resolves once the key is deleted.
findPath(
keyInBytes):Promise<Path>
Defined in: binaryTree.ts:432
Tries to find a path to the node for the given key.
It returns a Path object containing:
node: the found node (if any),stack: an array of tuples [node, path] representing the nodes encountered,remaining: the bits of the key that were not matched.Uint8Array
the search key as a byte array.
Promise<Path>
A Promise that resolves to a Path object.
flushCheckpoints():
void
Defined in: binaryTree.ts:706
Flushes all checkpoints, restoring the initial checkpoint state.
void
get(
stem,suffixes):Promise<(Uint8Array<ArrayBufferLike> |null)[]>
Defined in: binaryTree.ts:152
Gets values at a given binary tree stem and set of suffixes
Uint8Array
the stem of the stem node where we’re seeking values
number[]
an array of suffixes corresponding to the values desired
Promise<(Uint8Array<ArrayBufferLike> | null)[]>
A Promise that resolves to an array of Uint8Arrays or null depending on if values were found.
If the stem is not found, will return an empty array.
hasCheckpoints():
boolean
Defined in: binaryTree.ts:659
Is the tree during a checkpoint phase?
boolean
persistRoot():
Promise<void>
Defined in: binaryTree.ts:650
Persists the root hash in the underlying database
Promise<void>
put(
stem,suffixes,values):Promise<void>
Defined in: binaryTree.ts:182
Stores a given value at the given key or performs a deletion if value is null.
Uint8Array
the stem (must be 31 bytes) to store the value at.
number[]
array of suffixes at which to store individual values.
(Uint8Array<ArrayBufferLike> | null)[]
the value(s) to store (or null for deletion).
Promise<void>
A Promise that resolves once the value is stored.
revert():
Promise<void>
Defined in: binaryTree.ts:692
Reverts the tree to the state it was at when checkpoint was first called.
If during a nested checkpoint, sets root to most recent checkpoint, and sets
parent checkpoint as current.
Promise<void>
root(
value?):Uint8Array
Defined in: binaryTree.ts:98
Gets and/or Sets the current root of the tree
Uint8Array<ArrayBufferLike> | null
Uint8Array
saveStack(
putStack):Promise<void>
Defined in: binaryTree.ts:585
Saves a stack of nodes to the database.
[Uint8Array<ArrayBufferLike>, BinaryNode | null][]
an array of tuples of keys (the partial path of the node in the trie) and nodes (BinaryNodes)
Promise<void>
shallowCopy(
includeCheckpoints?):BinaryTree
Defined in: binaryTree.ts:634
Returns a copy of the underlying tree.
Note on db: the copy will create a reference to the same underlying database.
Note on cache: for memory reasons a copy will not recreate a new LRU cache but initialize with cache being deactivated.
boolean = true
If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db.
BinaryTree
updateBranch(
stemNode,nearestNode,pathToNode,pathToParent):object[] |undefined
Defined in: binaryTree.ts:368
Helper method for updating or creating the parent internal node for a given stem node. If the nearest node is a stem node with a different stem, a new internal node is created to branch at the first differing bit. If the nearest node is an internal node, its child reference is updated.
The child stem node that will be referenced by the new/updated internal node.
The nearest node to the new stem node.
number[]
The path (in bits) to nearestNode as known from the trie.
number[]
object[] | undefined
An array of nodes and their partial paths from the new stem node to the branch parent node
or undefined if no changes were made.
withLock<
T>(operation):Promise<T>
Defined in: binaryTree.ts:120
Runs operation while holding the tree’s internal lock, releasing the
lock when the returned promise settles. Use this to keep other lock-aware
operations (e.g. commit/revert) from interleaving with a multi-step
read sequence, such as reading values at several roots via root()/get().
T
() => Promise<T>
Promise<T>