ethereumjs-monorepo

@ethereumjs/statemanager / DefaultStateManager

Class: DefaultStateManager

Default StateManager implementation for the VM.

The state manager abstracts from the underlying data store by providing higher level access to accounts, contract code and storage slots.

The default state manager implementation uses a @ethereumjs/trie trie as a data backend.

Implements

Table of contents

Constructors

Properties

Methods

Constructors

constructor

new DefaultStateManager(opts?)

Instantiate the StateManager interface.

Parameters

Name Type
opts DefaultStateManagerOpts

Defined in

stateManager.ts:192

Properties

common

Readonly common: Common

Defined in

stateManager.ts:173


originalStorageCache

originalStorageCache: OriginalStorageCache

Implementation of

EVMStateManagerInterface.originalStorageCache

Defined in

stateManager.ts:162

Methods

addProofData

addProofData(proof, safe?): Promise<void>

Add proof(s) into an already existing trie

Parameters

Name Type Default value Description
proof Proof | Proof[] undefined The proof(s) retrieved from getProof
safe boolean false -

Returns

Promise<void>

Defined in

stateManager.ts:819


checkpoint

checkpoint(): Promise<void>

Checkpoints the current state of the StateManager instance. State changes that follow can then be committed by calling commit or reverted by calling rollback.

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.checkpoint

Defined in

stateManager.ts:593


clearCaches

clearCaches(): void

Clears all underlying caches

Returns

void

Defined in

stateManager.ts:1148


clearContractStorage

clearContractStorage(address): Promise<void>

Clears all storage entries for the account corresponding to address.

Parameters

Name Type Description
address Address Address to clear the storage of

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.clearContractStorage

Defined in

stateManager.ts:576


commit

commit(): Promise<void>

Commits the current change-set to the instance since the last call to checkpoint.

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.commit

Defined in

stateManager.ts:605


deleteAccount

deleteAccount(address): Promise<void>

Deletes an account from state under the provided address.

Parameters

Name Type Description
address Address Address of the account which should be deleted

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.deleteAccount

Defined in

stateManager.ts:333


dumpStorage

dumpStorage(address): Promise<StorageDump>

Dumps the RLP-encoded storage values for an account specified by address.

Parameters

Name Type Description
address Address The address of the account to return storage for

Returns

Promise<StorageDump>

Implementation of

EVMStateManagerInterface.dumpStorage

Defined in

stateManager.ts:962


dumpStorageRange

dumpStorageRange(address, startKey, limit): Promise<StorageRange>

Dumps a limited number of RLP-encoded storage values for an account specified by address, starting from startKey or greater.

Parameters

Name Type Description
address Address The address of the account to return storage for.
startKey bigint The bigint representation of the smallest storage key that will be returned.
limit number The maximum number of storage values that will be returned.

Returns

Promise<StorageRange>

Implementation of

EVMStateManagerInterface.dumpStorageRange

Defined in

stateManager.ts:995


flush

flush(): Promise<void>

Writes all cache items to the trie

Returns

Promise<void>

Defined in

stateManager.ts:647


generateCanonicalGenesis

generateCanonicalGenesis(initState): Promise<void>

Initializes the provided genesis state into the state trie. Will error if there are uncommitted checkpoints on the instance.

Parameters

Name Type Description
initState any address -> balance | [balance, code, storage]

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.generateCanonicalGenesis

Defined in

stateManager.ts:1051


getAccount

getAccount(address): Promise<undefined | Account>

Gets the account associated with address or undefined if account does not exist

Parameters

Name Type Description
address Address Address of the account to get

Returns

Promise<undefined | Account>

Implementation of

EVMStateManagerInterface.getAccount

Defined in

stateManager.ts:260


getAppliedKey

getAppliedKey(address): Uint8Array

Returns the applied key for a given address Used for saving preimages

Parameters

Name Type Description
address Uint8Array The address to return the applied key

Returns

Uint8Array

Implementation of

EVMStateManagerInterface.getAppliedKey

Defined in

stateManager.ts:1160


getContractCode

getContractCode(address): Promise<Uint8Array>

Gets the code corresponding to the provided address.

Parameters

Name Type Description
address Address Address to get the code for

Returns

Promise<Uint8Array>

Implementation of

EVMStateManagerInterface.getContractCode

Defined in

stateManager.ts:379


getContractStorage

getContractStorage(address, key): Promise<Uint8Array>

Gets the storage value associated with the provided address and key. This method returns the shortest representation of the stored value.

Parameters

Name Type Description
address Address Address of the account to get the storage for
key Uint8Array Key in the account’s storage to get the value for. Must be 32 bytes long.

Returns

Promise<Uint8Array>

Implementation of

EVMStateManagerInterface.getContractStorage

Defined in

stateManager.ts:462


getProof

getProof(address, storageSlots?): Promise<Proof>

Get an EIP-1186 proof

Parameters

Name Type Default value Description
address Address undefined address to get proof of
storageSlots Uint8Array[] [] storage slots to get proof of

Returns

Promise<Proof>

Implementation of

EVMStateManagerInterface.getProof

Defined in

stateManager.ts:707


getStateRoot

getStateRoot(): Promise<Uint8Array>

Gets the state-root of the Merkle-Patricia trie representation of the state of this StateManager. Will error if there are uncommitted checkpoints on the instance.

Returns

Promise<Uint8Array>

Implementation of

EVMStateManagerInterface.getStateRoot

Defined in

stateManager.ts:920


hasStateRoot

hasStateRoot(root): Promise<boolean>

Checks whether there is a state corresponding to a stateRoot

Parameters

Name Type
root Uint8Array

Returns

Promise<boolean>

Implementation of

EVMStateManagerInterface.hasStateRoot

Defined in

stateManager.ts:1087


modifyAccountFields

modifyAccountFields(address, accountFields): Promise<void>

Gets the account associated with address, modifies the given account fields, then saves the account into state. Account fields can include nonce, balance, storageRoot, and codeHash.

Parameters

Name Type Description
address Address Address of the account to modify
accountFields Partial<Pick<Account, "nonce" | "balance" | "storageRoot" | "codeHash">> Object containing account fields and values to modify

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.modifyAccountFields

Defined in

stateManager.ts:317


putAccount

putAccount(address, account): Promise<void>

Saves an account into state under the provided address.

Parameters

Name Type Description
address Address Address under which to store account
account undefined | Account The account to store or undefined if to be deleted

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.putAccount

Defined in

stateManager.ts:284


putContractCode

putContractCode(address, value): Promise<void>

Adds value to the state trie as code, and sets codeHash on the account corresponding to address to reference this.

Parameters

Name Type Description
address Address Address of the account to add the code for
value Uint8Array The value of the code

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.putContractCode

Defined in

stateManager.ts:356


putContractStorage

putContractStorage(address, key, value): Promise<void>

Adds value to the state trie for the account corresponding to address at the provided key.

Parameters

Name Type Description
address Address Address to set a storage value for
key Uint8Array Key to set the value at. Must be 32 bytes long.
value Uint8Array Value to set at key for account corresponding to address. Cannot be more than 32 bytes. Leading zeros are stripped. If it is a empty or filled with zeros, deletes the value.

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.putContractStorage

Defined in

stateManager.ts:549


revert

revert(): Promise<void>

Reverts the current change-set to the instance since the last call to checkpoint.

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.revert

Defined in

stateManager.ts:627


setStateRoot

setStateRoot(stateRoot, clearCache?): Promise<void>

Sets the state of the instance to that represented by the provided stateRoot. Will error if there are uncommitted checkpoints on the instance or if the state root does not exist in the state trie.

Parameters

Name Type Default value Description
stateRoot Uint8Array undefined The state-root to reset the instance to
clearCache boolean true -

Returns

Promise<void>

Implementation of

EVMStateManagerInterface.setStateRoot

Defined in

stateManager.ts:932


shallowCopy

shallowCopy(downlevelCaches?): DefaultStateManager

Copies the current instance of the StateManager at the last fully committed point, i.e. as if all current checkpoints were reverted.

Caches are downleveled (so: adopted for short-term usage) by default.

This means in particular:

  1. For caches instantiated as an LRU cache type the copy() method will instantiate with an ORDERED_MAP cache instead, since copied instantances are mostly used in short-term usage contexts and LRU cache instantation would create a large overhead here.
  2. The underlying trie object is initialized with 0 cache size

Both adoptions can be deactivated by setting downlevelCaches to false.

Cache values are generally not copied along regardless of the downlevelCaches setting.

Parameters

Name Type Default value
downlevelCaches boolean true

Returns

DefaultStateManager

Implementation of

EVMStateManagerInterface.shallowCopy

Defined in

stateManager.ts:1113


verifyProof

verifyProof(proof): Promise<boolean>

Verify an EIP-1186 proof. Throws if proof is invalid, otherwise returns true.

Parameters

Name Type Description
proof Proof the proof to prove

Returns

Promise<boolean>

Defined in

stateManager.ts:842


fromProof

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

Create a StateManager and initialize this with proof(s) gotten previously from getProof This generates a (partial) StateManager where one can retrieve all items from the proof

Parameters

Name Type Default value Description
proof Proof | Proof[] undefined Either a proof retrieved from getProof, or an array of those proofs
safe boolean false Whether or not to verify that the roots of the proof items match the reported roots
opts DefaultStateManagerOpts {} a dictionary of StateManager opts

Returns

Promise<DefaultStateManager>

A new DefaultStateManager with elements from the given proof included in its backing state trie

Defined in

stateManager.ts:760