@ethereumjs/trie / Trie
The basic trie interface, use with import { Trie } from '@ethereumjs/trie'
.
- batch
- checkRoot
- checkpoint
- commit
- copy
- createProof
- createReadStream
- database
- del
- findPath
- flushCheckpoints
- fromProof
- get
- hasCheckpoints
- lookupNode
- persistRoot
- put
- revert
- root
- verifyProof
- verifyPrunedIntegrity
- verifyRangeProof
- walkTrie
- create
• new Trie(opts?
)
Creates a new trie.
Name | Type | Description |
---|---|---|
opts? |
TrieOpts |
Options for instantiating the trie Note: in most cases, the static create constructor should be used. It uses the same API but provides sensible defaults |
packages/trie/src/trie/trie.ts:59
• EMPTY_TRIE_ROOT: Buffer
The root for an empty trie
packages/trie/src/trie/trie.ts:45
▸ batch(ops
): Promise
<void
>
The given hash of operations (key additions or deletions) are executed on the trie
(delete operations are only executed on DB with deleteFromDB
set to true
)
Example
const ops = [
{ type: 'del', key: Buffer.from('father') }
, { type: 'put', key: Buffer.from('name'), value: Buffer.from('Yuri Irsenovich Kim') }
, { type: 'put', key: Buffer.from('dob'), value: Buffer.from('16 February 1941') }
, { type: 'put', key: Buffer.from('spouse'), value: Buffer.from('Kim Young-sook') }
, { type: 'put', key: Buffer.from('occupation'), value: Buffer.from('Clown') }
]
await trie.batch(ops)
Name | Type |
---|---|
ops |
BatchDBOp [] |
Promise
<void
>
packages/trie/src/trie/trie.ts:697
▸ checkRoot(root
): Promise
<boolean
>
Checks if a given root exists.
Name | Type |
---|---|
root |
Buffer |
Promise
<boolean
>
packages/trie/src/trie/trie.ts:129
▸ 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
packages/trie/src/trie/trie.ts:927
▸ 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
>
packages/trie/src/trie/trie.ts:936
▸ copy(includeCheckpoints?
): Trie
Returns a copy of the underlying trie.
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. |
packages/trie/src/trie/trie.ts:860
▸ createProof(key
): Promise
<Proof
>
Creates a proof from a trie and key that can be verified using verifyProof.
Name | Type |
---|---|
key |
Buffer |
Promise
<Proof
>
packages/trie/src/trie/trie.ts:737
▸ createReadStream(): TrieReadStream
The data
event is given an Object
that has two properties; the key
and the value
. Both should be Buffers.
Returns a stream of the contents of the trie
packages/trie/src/trie/trie.ts:852
▸ database(db?
): CheckpointDB
Name | Type |
---|---|
db? |
DB |
packages/trie/src/trie/trie.ts:95
▸ del(key
): Promise
<void
>
Deletes a value given a key
from the trie
(delete operations are only executed on DB with deleteFromDB
set to true
)
Name | Type |
---|---|
key |
Buffer |
Promise
<void
>
A Promise that resolves once value is deleted.
packages/trie/src/trie/trie.ts:218
▸ 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 |
Buffer |
undefined |
the search key |
throwIfMissing |
boolean |
false |
if true, throws if any nodes are missing. Used for verifying proofs. (default: false) |
Promise
<Path
>
packages/trie/src/trie/trie.ts:253
▸ flushCheckpoints(): void
Flushes all checkpoints, restoring the initial checkpoint state.
void
packages/trie/src/trie/trie.ts:966
▸ fromProof(proof
): Promise
<void
>
Saves the nodes from a proof into the trie.
Name | Type |
---|---|
proof |
Proof |
Promise
<void
>
packages/trie/src/trie/trie.ts:715
▸ get(key
, throwIfMissing?
): Promise
<null
| Buffer
>
Gets a value given a key
Name | Type | Default value | Description |
---|---|---|---|
key |
Buffer |
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
| Buffer
>
A Promise that resolves to Buffer
if a value was found or null
if no value was found.
packages/trie/src/trie/trie.ts:148
▸ hasCheckpoints(): boolean
Is the trie during a checkpoint phase?
boolean
packages/trie/src/trie/trie.ts:919
▸ lookupNode(node
): Promise
<null
| TrieNode
>
Retrieves a node from db by hash.
Name | Type |
---|---|
node |
Buffer | Buffer [] |
Promise
<null
| TrieNode
>
packages/trie/src/trie/trie.ts:344
▸ persistRoot(): Promise
<void
>
Persists the root hash in the underlying database
Promise
<void
>
packages/trie/src/trie/trie.ts:875
▸ 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 |
---|---|
key |
Buffer |
value |
Buffer |
Promise
<void
>
A Promise that resolves once value is stored.
packages/trie/src/trie/trie.ts:164
▸ revert(): Promise
<void
>
Reverts the trie 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
>
packages/trie/src/trie/trie.ts:952
▸ root(value?
): Buffer
Gets and/or Sets the current root of the trie
Name | Type |
---|---|
value? |
null | Buffer |
Buffer
packages/trie/src/trie/trie.ts:110
▸ verifyProof(rootHash
, key
, proof
): Promise
<null
| Buffer
>
Verifies a proof.
Throws
If proof is found to be invalid.
Name | Type |
---|---|
rootHash |
Buffer |
key |
Buffer |
proof |
Proof |
Promise
<null
| Buffer
>
The value from the key, or null if valid proof of non-existence.
packages/trie/src/trie/trie.ts:753
▸ verifyPrunedIntegrity(): Promise
<boolean
>
Promise
<boolean
>
packages/trie/src/trie/trie.ts:801
▸ verifyRangeProof(rootHash
, firstKey
, lastKey
, keys
, values
, proof
): Promise
<boolean
>
Name | Type |
---|---|
rootHash |
Buffer |
firstKey |
null | Buffer |
lastKey |
null | Buffer |
keys |
Buffer [] |
values |
Buffer [] |
proof |
null | Buffer [] |
Promise
<boolean
>
packages/trie/src/trie/trie.ts:778
▸ walkTrie(root
, onFound
): Promise
<void
>
Walks a trie until finished.
Name | Type | Description |
---|---|---|
root |
Buffer |
|
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 trie.
packages/trie/src/trie/trie.ts:324
▸ Static
create(opts?
): Promise
<Trie
>
Name | Type |
---|---|
opts? |
TrieOpts |
Promise
<Trie
>