Skip to content

Latest commit

 

History

History
598 lines (342 loc) · 13.2 KB

File metadata and controls

598 lines (342 loc) · 13.2 KB

@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/trie.ts:59

Properties

EMPTY_TRIE_ROOT

EMPTY_TRIE_ROOT: Buffer

The root for an empty trie

Defined in

packages/trie/src/trie/trie.ts:45

Methods

batch

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)

Parameters

Name Type
ops BatchDBOp[]

Returns

Promise<void>

Defined in

packages/trie/src/trie/trie.ts:697


checkRoot

checkRoot(root): Promise<boolean>

Checks if a given root exists.

Parameters

Name Type
root Buffer

Returns

Promise<boolean>

Defined in

packages/trie/src/trie/trie.ts:129


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/trie.ts:927


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/trie.ts:936


copy

copy(includeCheckpoints?): Trie

Returns a copy of the underlying trie.

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.

Returns

Trie

Defined in

packages/trie/src/trie/trie.ts:860


createProof

createProof(key): Promise<Proof>

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

Parameters

Name Type
key Buffer

Returns

Promise<Proof>

Defined in

packages/trie/src/trie/trie.ts:737


createReadStream

createReadStream(): TrieReadStream

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

Returns

TrieReadStream

Returns a stream of the contents of the trie

Defined in

packages/trie/src/trie/trie.ts:852


database

database(db?): CheckpointDB

Parameters

Name Type
db? DB

Returns

CheckpointDB

Defined in

packages/trie/src/trie/trie.ts:95


del

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)

Parameters

Name Type
key Buffer

Returns

Promise<void>

A Promise that resolves once value is deleted.

Defined in

packages/trie/src/trie/trie.ts:218


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 Buffer 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/trie.ts:253


flushCheckpoints

flushCheckpoints(): void

Flushes all checkpoints, restoring the initial checkpoint state.

Returns

void

Defined in

packages/trie/src/trie/trie.ts:966


fromProof

fromProof(proof): Promise<void>

Saves the nodes from a proof into the trie.

Parameters

Name Type
proof Proof

Returns

Promise<void>

Defined in

packages/trie/src/trie/trie.ts:715


get

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

Gets a value given a key

Parameters

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)

Returns

Promise<null | Buffer>

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

Defined in

packages/trie/src/trie/trie.ts:148


hasCheckpoints

hasCheckpoints(): boolean

Is the trie during a checkpoint phase?

Returns

boolean

Defined in

packages/trie/src/trie/trie.ts:919


lookupNode

lookupNode(node): Promise<null | TrieNode>

Retrieves a node from db by hash.

Parameters

Name Type
node Buffer | Buffer[]

Returns

Promise<null | TrieNode>

Defined in

packages/trie/src/trie/trie.ts:344


persistRoot

persistRoot(): Promise<void>

Persists the root hash in the underlying database

Returns

Promise<void>

Defined in

packages/trie/src/trie/trie.ts:875


put

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)

Parameters

Name Type
key Buffer
value Buffer

Returns

Promise<void>

A Promise that resolves once value is stored.

Defined in

packages/trie/src/trie/trie.ts:164


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/trie.ts:952


root

root(value?): Buffer

Gets and/or Sets the current root of the trie

Parameters

Name Type
value? null | Buffer

Returns

Buffer

Defined in

packages/trie/src/trie/trie.ts:110


verifyProof

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

Verifies a proof.

Throws

If proof is found to be invalid.

Parameters

Name Type
rootHash Buffer
key Buffer
proof Proof

Returns

Promise<null | Buffer>

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

Defined in

packages/trie/src/trie/trie.ts:753


verifyPrunedIntegrity

verifyPrunedIntegrity(): Promise<boolean>

Returns

Promise<boolean>

Defined in

packages/trie/src/trie/trie.ts:801


verifyRangeProof

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

verifyRangeProof

Parameters

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

Returns

Promise<boolean>

Defined in

packages/trie/src/trie/trie.ts:778


walkTrie

walkTrie(root, onFound): Promise<void>

Walks a trie until finished.

Parameters

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.

Returns

Promise<void>

Resolves when finished walking trie.

Defined in

packages/trie/src/trie/trie.ts:324


create

Static create(opts?): Promise<Trie>

Parameters

Name Type
opts? TrieOpts

Returns

Promise<Trie>

Defined in

packages/trie/src/trie/trie.ts:75