@ethereumjs/verkle / VerkleTree
The basic verkle tree interface, use with import { VerkleTree } from '@ethereumjs/verkle'
.
• new VerkleTree(opts?
)
Creates a new verkle tree.
Name | Type | Description |
---|---|---|
opts? |
VerkleTreeOpts |
Options for instantiating the verkle tree Note: in most cases, the static create constructor should be used. It uses the same API but provides sensible defaults |
• EMPTY_TREE_ROOT: Uint8Array
The root for an empty tree
▸ batch(ops
): Promise
<void
>
The given hash of operations (key additions or deletions) are executed on the tree
(delete operations are only executed on DB with deleteFromDB
set to true
)
Example
const ops = [
{ type: 'del', key: Uint8Array.from('father') }
, { type: 'put', key: Uint8Array.from('name'), value: Uint8Array.from('Yuri Irsenovich Kim') }
, { type: 'put', key: Uint8Array.from('dob'), value: Uint8Array.from('16 February 1941') }
, { type: 'put', key: Uint8Array.from('spouse'), value: Uint8Array.from('Kim Young-sook') }
, { type: 'put', key: Uint8Array.from('occupation'), value: Uint8Array.from('Clown') }
]
await tree.batch(ops)
Name | Type |
---|---|
ops |
BatchDBOp <Uint8Array , Uint8Array >[] |
Promise
<void
>
▸ checkRoot(root
): Promise
<boolean
>
Checks if a given root exists.
Name | Type |
---|---|
root |
Uint8Array |
Promise
<boolean
>
▸ checkpoint(): void
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
▸ commit(): Promise
<void
>
Commits a checkpoint to disk, if current checkpoint is not nested. If nested, only sets the parent checkpoint as current checkpoint.
Throws
If not during a checkpoint phase
Promise
<void
>
▸ createProof(key
): Promise
<Proof
>
Creates a proof from a tree and key that can be verified using verifyProof.
Name | Type |
---|---|
key |
Uint8Array |
Promise
<Proof
>
▸ createReadStream(): any
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
▸ database(db?
): CheckpointDB
Name | Type |
---|---|
db? |
DB <Uint8Array , Uint8Array > |
▸ findLeafNode(key
, throwIfMissing?
): Promise
<null
| LeafNode
>
Tries to find the leaf node leading up to the given key, or null if not found.
Name | Type | Default value | Description |
---|---|---|---|
key |
Uint8Array |
undefined |
the search key |
throwIfMissing |
boolean |
false |
if true, throws if any nodes are missing. Used for verifying proofs. (default: false) |
Promise
<null
| LeafNode
>
▸ findPath(key
, throwIfMissing?
): Promise
<Path
>
Tries to find a path to the node for the given key.
It returns a stack
of nodes to the closest node.
Name | Type | Default value | Description |
---|---|---|---|
key |
Uint8Array |
undefined |
the search key |
throwIfMissing |
boolean |
false |
if true, throws if any nodes are missing. Used for verifying proofs. (default: false) |
Promise
<Path
>
▸ flushCheckpoints(): void
Flushes all checkpoints, restoring the initial checkpoint state.
void
▸ fromProof(proof
): Promise
<void
>
Saves the nodes from a proof into the tree.
Name | Type |
---|---|
proof |
Proof |
Promise
<void
>
▸ get(key
, throwIfMissing?
): Promise
<null
| Uint8Array
>
Gets a value given a key
Name | Type | Default value | Description |
---|---|---|---|
key |
Uint8Array |
undefined |
the key to search for |
throwIfMissing |
boolean |
false |
if true, throws if any nodes are missing. Used for verifying proofs. (default: false) |
Promise
<null
| Uint8Array
>
A Promise that resolves to Uint8Array
if a value was found or null
if no value was found.
▸ hasCheckpoints(): boolean
Is the tree during a checkpoint phase?
boolean
▸ lookupNode(node
): Promise
<null
| VerkleNode
>
Retrieves a node from db by hash.
Name | Type |
---|---|
node |
Uint8Array | Uint8Array [] |
Promise
<null
| VerkleNode
>
▸ persistRoot(): Promise
<void
>
Persists the root hash in the underlying database
Promise
<void
>
▸ put(key
, value
): Promise
<void
>
Stores a given value
at the given key
or do a delete if value
is empty
(delete operations are only executed on DB with deleteFromDB
set to true
)
Name | Type | Description |
---|---|---|
key |
Uint8Array |
the key to store the value at |
value |
Uint8Array |
the value to store |
Promise
<void
>
A Promise that resolves once value is stored.
▸ revert(): Promise
<void
>
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
Gets and/or Sets the current root of the tree
Name | Type |
---|---|
value? |
null | Uint8Array |
Uint8Array
▸ saveStack(key
, stack
, opStack
): Promise
<void
>
Saves a stack of nodes to the database.
Name | Type | Description |
---|---|---|
key |
Uint8Array |
the key. Should follow the stack |
stack |
VerkleNode [] |
a stack of nodes to the value given by the key |
opStack |
PutBatch <Uint8Array , Uint8Array >[] |
a stack of levelup operations to commit at the end of this function |
Promise
<void
>
▸ shallowCopy(includeCheckpoints?
): VerkleTree
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.
Name | Type | Default value | Description |
---|---|---|---|
includeCheckpoints |
boolean |
true |
If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db. |
▸ verifyProof(rootHash
, key
, proof
): Promise
<null
| Uint8Array
>
Verifies a proof.
Throws
If proof is found to be invalid.
Name | Type |
---|---|
rootHash |
Uint8Array |
key |
Uint8Array |
proof |
Proof |
Promise
<null
| Uint8Array
>
The value from the key, or null if valid proof of non-existence.
▸ walkTree(root
, onFound
): Promise
<void
>
Walks a tree until finished.
Name | Type | Description |
---|---|---|
root |
Uint8Array |
|
onFound |
FoundNodeFunction |
callback to call when a node is found. This schedules new tasks. If no tasks are available, the Promise resolves. |
Promise
<void
>
Resolves when finished walking tree.
▸ Static
create(opts?
): Promise
<VerkleTree
>
Name | Type |
---|---|
opts? |
VerkleTreeOpts |
Promise
<VerkleTree
>