Ethereumjs-Webserver

@ethereumjs/trie / Trie

Class: Trie

The basic trie interface, use with import { Trie } from '@ethereumjs/trie'.

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new Trie(opts?)

Creates a new trie.

Parameters

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

Defined in

packages/trie/src/trie.ts:99

Properties

EMPTY_TRIE_ROOT

EMPTY_TRIE_ROOT: Uint8Array

The root for an empty trie

Defined in

packages/trie/src/trie.ts:80


walkTrieIterable

walkTrieIterable: (…args: [nodeHash: Uint8Array, currentKey: number[], onFound: OnFound, filter: NodeFilter, visited: Set<string>]) => AsyncIterable<{ currentKey: number[] ; node: TrieNode }>

Type declaration

▸ (…args): AsyncIterable<{ currentKey: number[] ; node: TrieNode }>

Parameters
Name Type
...args [nodeHash: Uint8Array, currentKey: number[], onFound: OnFound, filter: NodeFilter, visited: Set<string>]
Returns

AsyncIterable<{ currentKey: number[] ; node: TrieNode }>

Defined in

packages/trie/src/trie.ts:566

Methods

batch

batch(ops, skipKeyTransform?): 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: 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 trie.batch(ops)

Parameters

Name Type
ops BatchDBOp<Uint8Array, Uint8Array>[]
skipKeyTransform? boolean

Returns

Promise<void>

Defined in

packages/trie/src/trie.ts:967


checkRoot

checkRoot(root): Promise<boolean>

Checks if a given root exists.

Parameters

Name Type
root Uint8Array

Returns

Promise<boolean>

Defined in

packages/trie/src/trie.ts:305


checkpoint

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.

Returns

void

Defined in

packages/trie/src/trie.ts:1277


commit

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

Returns

Promise<void>

Defined in

packages/trie/src/trie.ts:1287


createAsyncReadStream

createAsyncReadStream(): ReadableStream<any>

Use asynchronous iteration over the chunks in a web stream using the for await…of syntax.

Returns

ReadableStream<any>

Returns a web stream of the contents of the trie

Defined in

packages/trie/src/trie.ts:1182


createProof

createProof(key): Promise<Proof>

Creates a proof from a trie and key that can be verified using verifyProof.

Parameters

Name Type
key Uint8Array

Returns

Promise<Proof>

Defined in

packages/trie/src/trie.ts:1035


createReadStream

createReadStream(): TrieReadStream

The data event is given an Object that has two properties; the key and the value. Both should be Uint8Arrays.

Deprecated

Use createAsyncReadStream

Returns

TrieReadStream

Returns a stream of the contents of the trie

Defined in

packages/trie/src/trie.ts:1174


database

database(db?, valueEncoding?): CheckpointDB

Parameters

Name Type
db? DB<string, string | Uint8Array>
valueEncoding? ValueEncoding

Returns

CheckpointDB

Defined in

packages/trie/src/trie.ts:272


del

del(key, skipKeyTransform?): Promise<void>

Deletes a value given a key from the trie (delete operations are only executed on DB with deleteFromDB set to true)

Parameters

Name Type Default value
key Uint8Array undefined
skipKeyTransform boolean false

Returns

Promise<void>

A Promise that resolves once value is deleted.

Defined in

packages/trie/src/trie.ts:406


findPath

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.

Parameters

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)

Returns

Promise<Path>

Defined in

packages/trie/src/trie.ts:446


flushCheckpoints

flushCheckpoints(): void

Flushes all checkpoints, restoring the initial checkpoint state.

Returns

void

Defined in

packages/trie/src/trie.ts:1319


fromProof

fromProof(proof): Promise<void>

Saves the nodes from a proof into the trie. A static version of this function exists with the same name.

Deprecated

Use updateFromProof

Parameters

Name Type
proof Proof

Returns

Promise<void>

Defined in

packages/trie/src/trie.ts:986


get

get(key, throwIfMissing?): Promise<null | Uint8Array>

Gets a value given a key

Parameters

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)

Returns

Promise<null | Uint8Array>

A Promise that resolves to Uint8Array if a value was found or null if no value was found.

Defined in

packages/trie/src/trie.ts:324


hasCheckpoints

hasCheckpoints(): boolean

Is the trie during a checkpoint phase?

Returns

boolean

Defined in

packages/trie/src/trie.ts:1269


lookupNode

lookupNode(node): Promise<TrieNode>

Retrieves a node from db by hash.

Parameters

Name Type
node Uint8Array | Uint8Array[]

Returns

Promise<TrieNode>

Defined in

packages/trie/src/trie.ts:615


persistRoot

persistRoot(): Promise<void>

Persists the root hash in the underlying database

Returns

Promise<void>

Defined in

packages/trie/src/trie.ts:1216


put

put(key, value, skipKeyTransform?): 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)

Parameters

Name Type Default value
key Uint8Array undefined
value null | Uint8Array undefined
skipKeyTransform boolean false

Returns

Promise<void>

A Promise that resolves once value is stored.

Defined in

packages/trie/src/trie.ts:342


revert

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.

Returns

Promise<void>

Defined in

packages/trie/src/trie.ts:1303


root

root(value?): Uint8Array

Gets and/or Sets the current root of the trie

Parameters

Name Type
value? null | Uint8Array

Returns

Uint8Array

Defined in

packages/trie/src/trie.ts:287


saveStack

saveStack(key, stack, opStack): Promise<void>

Saves a stack of nodes to the database.

Parameters

Name Type Description
key Nibbles the key. Should follow the stack
stack TrieNode[] a stack of nodes to the value given by the key
opStack BatchDBOp<Uint8Array, Uint8Array>[] a stack of levelup operations to commit at the end of this function

Returns

Promise<void>

Defined in

packages/trie/src/trie.ts:881


shallowCopy

shallowCopy(includeCheckpoints?, opts?): Trie

Returns a copy of the underlying trie.

Note on db: the copy will create a reference to the same underlying database.

Note on cache: for memory reasons a copy will by default not recreate a new LRU cache but initialize with cache being deactivated. This behavior can be overwritten by explicitly setting cacheSize as an option on the method.

Parameters

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.
opts? TrieShallowCopyOpts undefined -

Returns

Trie

Defined in

packages/trie/src/trie.ts:1199


updateFromProof

updateFromProof(proof, shouldVerifyRoot?): Promise<undefined | Uint8Array>

Updates a trie from a proof

Parameters

Name Type Default value Description
proof Proof undefined The proof
shouldVerifyRoot boolean false If true, verifies that the root key of the proof matches the trie root. Throws if this is not the case.

Returns

Promise<undefined | Uint8Array>

The root of the proof

Defined in

packages/trie/src/trie.ts:1005


verifyProof

verifyProof(rootHash, key, proof): Promise<null | Uint8Array>

Verifies a proof. A static version of this function exists with the same name.

Throws

If proof is found to be invalid.

Parameters

Name Type
rootHash Uint8Array
key Uint8Array
proof Proof

Returns

Promise<null | Uint8Array>

The value from the key, or null if valid proof of non-existence.

Defined in

packages/trie/src/trie.ts:1053


verifyPrunedIntegrity

verifyPrunedIntegrity(): Promise<boolean>

Returns

Promise<boolean>

Defined in

packages/trie/src/trie.ts:1119


verifyRangeProof

verifyRangeProof(rootHash, firstKey, lastKey, keys, values, proof): Promise<boolean>

verifyRangeProof

Parameters

Name Type
rootHash Uint8Array
firstKey null | Uint8Array
lastKey null | Uint8Array
keys Uint8Array[]
values Uint8Array[]
proof null | Uint8Array[]

Returns

Promise<boolean>

Defined in

packages/trie/src/trie.ts:1096


walkAllNodes

walkAllNodes(onFound): Promise<void>

Executes a callback for each node in the trie.

Parameters

Name Type Description
onFound OnFound callback to call when a node is found.

Returns

Promise<void>

Resolves when finished walking trie.

Defined in

packages/trie/src/trie.ts:573


walkAllValueNodes

walkAllValueNodes(onFound): Promise<void>

Executes a callback for each value node in the trie.

Parameters

Name Type Description
onFound OnFound callback to call when a node is found.

Returns

Promise<void>

Resolves when finished walking trie.

Defined in

packages/trie/src/trie.ts:584


walkTrie

walkTrie(root, onFound): Promise<void>

Walks a trie until finished.

Parameters

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.

Returns

Promise<void>

Resolves when finished walking trie.

Defined in

packages/trie/src/trie.ts:562


create

Static create(opts?): Promise<Trie>

Parameters

Name Type
opts? TrieOpts

Returns

Promise<Trie>

Defined in

packages/trie/src/trie.ts:190


createFromProof

Static createFromProof(proof, trieOpts?): Promise<Trie>

Create a trie from a given proof

Parameters

Name Type Description
proof Proof proof to create trie from
trieOpts? TrieOpts trie opts to be applied to returned trie

Returns

Promise<Trie>

new trie created from given proof

Defined in

packages/trie/src/trie.ts:156


fromProof

Static fromProof(proof, opts?): Promise<Trie>

Static version of fromProof function with the same behavior.

Deprecated

Use updateFromProof

Parameters

Name Type
proof Proof
opts? TrieOpts

Returns

Promise<Trie>

Defined in

packages/trie/src/trie.ts:236


verifyProof

Static verifyProof(key, proof, opts?): Promise<null | Uint8Array>

Static version of verifyProof function with the same behavior.

Throws

If proof is found to be invalid.

Parameters

Name Type Description
key Uint8Array  
proof Proof  
opts? TrieOpts Trie options

Returns

Promise<null | Uint8Array>

The value from the key, or null if valid proof of non-existence.

Defined in

packages/trie/src/trie.ts:256


verifyRangeProof

Static verifyRangeProof(rootHash, firstKey, lastKey, keys, values, proof, opts?): Promise<boolean>

Static version of verifyRangeProof function with the same behavior

Parameters

Name Type
rootHash Uint8Array
firstKey null | Uint8Array
lastKey null | Uint8Array
keys Uint8Array[]
values Uint8Array[]
proof null | Uint8Array[]
opts? TrieOpts

Returns

Promise<boolean>

Defined in

packages/trie/src/trie.ts:167