Skip to content

Commit

Permalink
Statemanager doc update (#3675)
Browse files Browse the repository at this point in the history
* Change DefaultStateManager to MerkleStateManager

* Fix mispelling

* Reword and add example to example description

* Update example

* Update and reembed example
  • Loading branch information
scorbajio committed Sep 18, 2024
1 parent d7a9ff2 commit f315fc0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions packages/statemanager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ The `StateManager` provides high-level access and manipulation methods to and fo
This library includes several different implementations that all implement the `StateManager` interface which is accepted by the `vm` library. These include:

- [`SimpleStateManager`](./src/simpleStateManager.ts) -a minimally functional (and dependency minimized) version of the state manager suitable for most basic EVM bytecode operations
- [`DefaultStateManager`](./src//stateManager.ts) - a Merkle-Patricia Trie-based `DefaultStateManager` implementation that is used by the `@ethereumjs/client` and `@ethereumjs/vm`
- [`MerkleStateManager`](./src//stateManager.ts) - a Merkle-Patricia Trie-based `MerkleStateManager` implementation that is used by the `@ethereumjs/client` and `@ethereumjs/vm`
- [`RPCStateManager`](./src/rpcStateManager.ts) - a light-weight implementation that sources state and history data from an external JSON-RPC provider
- [`StatelessVerkleStateManager`](./src/statelessVerkleStateManager.ts) - an experimental implementation of a "stateless" state manager that uses Verkle proofs to provide necessary state access for processing verkle-trie based blocks

It also includes a checkpoint/revert/commit mechanism to either persist or revert state changes and provides a sophisticated caching mechanism under the hood to reduce the need reading state accesses from disk.

### `DefaultStateManager`
### `MerkleStateManager`

#### Usage example

Expand Down Expand Up @@ -69,7 +69,7 @@ There are now two cache options available: an unbounded cache (`CacheType.ORDERE

Caches now "survive" a flush operation and especially long-lived usage scenarios will benefit from increased performance by a growing and more "knowing" cache leading to less and less trie reads.

Have a loot at the extended `CacheOptions` on how to use and leverage the new cache system.
Have a look at the extended `CacheOptions` on how to use and leverage the new cache system.

### `SimpleStateManager`

Expand All @@ -95,11 +95,20 @@ const main = async () => {
void main()
```

### `DefaultStateManager` -> Proofs
### `MerkleStateManager` -> Proofs

#### Instantiating from a Proof

The `DefaultStateManager` has a static constructor `fromProof` that accepts one or more [EIP-1186](https://eips.ethereum.org/EIPS/eip-1186) [proofs](./src/stateManager.ts) and will instantiate a `DefaultStateManager` with a partial trie containing the state provided by the proof(s). Be aware that this constructor accepts the `StateManagerOpts` dictionary as a third parameter (i.e. `stateManager.fromProof(proof, safe, opts)`). Therefore, if you need to use a customized trie (e.g. one that does not use key hashing) or specify caching options, you can pass them in here. If you do instantiate a trie and pass it into the `fromProof` constructor, you also need to instantiate the trie using the corresponding `fromProof` constructor to ensure the state root matches when the proof data is added to the trie. See [this test](./test/stateManager.spec.ts#L287-L288) for more details.
The `MerkleStateManager` has a standalone constructor function `fromMerkleStateProof` that accepts one or more [EIP-1186](https://eips.ethereum.org/EIPS/eip-1186) [proofs](./src/stateManager.ts) and will instantiate a `MerkleStateManager` with a partial trie containing the state provided by the proof(s). Be aware that this constructor accepts the `StateManagerOpts` dictionary as a third parameter (i.e. `fromMerkleStateProof(proof, safe, opts)`).

Therefore, if you need to use a customized trie (e.g. one that does not use key hashing) or specify caching options, you can pass them in here. If you do instantiate a trie and pass it into the `createTrieFromProof` constructor, you also need to instantiate the trie using the corresponding `createStateManagerFromProof` constructor to ensure the state root matches when the proof data is added to the trie, consider an example:

```ts
const newTrie = await createTrieFromProof(proof, { useKeyHashing: false })
const partialSM = await fromMerkleStateProof([proof], true, {
trie: newTrie,
})
```

See below example for common usage:

Expand All @@ -108,9 +117,9 @@ See below example for common usage:

import {
MerkleStateManager,
getMerkleStateProof,
fromMerkleStateProof,
addMerkleStateProofData,
fromMerkleStateProof,
getMerkleStateProof,
} from '@ethereumjs/statemanager'
import { Address, hexToBytes } from '@ethereumjs/util'

Expand Down Expand Up @@ -139,7 +148,7 @@ const main = async () => {
])
const partialStateManager = await fromMerkleStateProof(proof)

// To add more proof data, use `addProofData`
// To add more proof data, use `addMerkleStateProofData`
await addMerkleStateProofData(partialStateManager, proofWithStorage)
console.log(await partialStateManager.getCode(contractAddress)) // contract bytecode is not included in proof
console.log(await partialStateManager.getStorage(contractAddress, storageKey1), storageValue1) // should match
Expand Down
2 changes: 1 addition & 1 deletion packages/statemanager/examples/fromProofInstantiation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const main = async () => {
])
const partialStateManager = await fromMerkleStateProof(proof)

// To add more proof data, use `addProofData`
// To add more proof data, use `addMerkleStateProofData`
await addMerkleStateProofData(partialStateManager, proofWithStorage)
console.log(await partialStateManager.getCode(contractAddress)) // contract bytecode is not included in proof
console.log(await partialStateManager.getStorage(contractAddress, storageKey1), storageValue1) // should match
Expand Down

0 comments on commit f315fc0

Please sign in to comment.