Skip to content

Commit d7a9ff2

Browse files
am1r021holgerd77
andauthored
Statemanager proof function tree shake (#3672)
* Move proof functions from Merkle sm over to being standalone functions * Remove unneeded parameter from fromMerkleStateProof * Export merkle sm proof functions * Refactor tests to use new standalone sm proof functions * stateManager: Fix linting issues * Make debugger a class field * Move proof functions from verkle sm over to being standalone functions * Remove getProof and verifyProof for the stateless verkle sm * Remove unused accountExists function on RPCSm * Remove accountExists function on RPCStateeManager * Move getProof function of RPCStateManager over to being standalone * Export proof functions from statemanager package * Fix test * Fix and reembed example * Fix getProof rpc method by checking which proof function to use for given sm * Remove getProof and verifyProof from sm interface and fix errors * Fix linting issues * Fix lint issue --------- Co-authored-by: Holger Drewes <[email protected]>
1 parent 766bd8f commit d7a9ff2

17 files changed

+454
-385
lines changed

packages/client/src/rpc/modules/eth.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { createBlock } from '@ethereumjs/block'
22
import { Hardfork } from '@ethereumjs/common'
3+
import {
4+
MerkleStateManager,
5+
StatelessVerkleStateManager,
6+
getMerkleStateProof,
7+
getVerkleStateProof,
8+
} from '@ethereumjs/statemanager'
39
import {
410
Capability,
511
createBlob4844TxFromSerializedNetworkWrapper,
@@ -1246,14 +1252,19 @@ export class Eth {
12461252

12471253
const vm = await this._vm.shallowCopy()
12481254

1249-
if (!('getProof' in vm.stateManager)) {
1250-
throw new Error('getProof RPC method not supported with the StateManager provided')
1251-
}
12521255
await vm.stateManager.setStateRoot(block.header.stateRoot)
12531256

12541257
const address = createAddressFromString(addressHex)
12551258
const slots = slotsHex.map((slotHex) => setLengthLeft(hexToBytes(slotHex), 32))
1256-
const proof = await vm.stateManager.getProof!(address, slots)
1259+
let proof: Proof
1260+
if (vm.stateManager instanceof MerkleStateManager) {
1261+
proof = await getMerkleStateProof(vm.stateManager, address, slots)
1262+
} else if (vm.stateManager instanceof StatelessVerkleStateManager) {
1263+
proof = await getVerkleStateProof(vm.stateManager, address, slots)
1264+
} else {
1265+
throw new Error('getProof RPC method not supported with the StateManager provided')
1266+
}
1267+
12571268
for (const p of proof.storageProof) {
12581269
p.key = bigIntToHex(BigInt(p.key))
12591270
}

packages/common/src/interfaces.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ export interface StateManagerInterface {
150150
* on usage (check for existence)
151151
*/
152152
// Client RPC
153-
getProof?(address: Address, storageSlots: Uint8Array[]): Promise<Proof>
154153
dumpStorage?(address: Address): Promise<StorageDump>
155154
dumpStorageRange?(address: Address, startKey: bigint, limit: number): Promise<StorageRange>
156155

@@ -169,7 +168,6 @@ export interface StateManagerInterface {
169168
executionWitness?: VerkleExecutionWitness | null,
170169
accessWitness?: AccessWitnessInterface,
171170
): void
172-
verifyVerkleProof?(): boolean
173171
verifyPostState?(): boolean
174172
checkChunkWitnessPresent?(contract: Address, programCounter: number): Promise<boolean>
175173
getAppliedKey?(address: Uint8Array): Uint8Array // only for preimages

packages/statemanager/README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ It also includes a checkpoint/revert/commit mechanism to either persist or rever
3939
```ts
4040
// ./examples/basicUsage.ts
4141

42-
import { DefaultStateManager } from '@ethereumjs/statemanager'
42+
import { MerkleStateManager } from '@ethereumjs/statemanager'
4343
import { Account, Address, hexToBytes } from '@ethereumjs/util'
4444

4545
const main = async () => {
46-
const stateManager = new DefaultStateManager()
46+
const stateManager = new MerkleStateManager()
4747
const address = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
4848
const account = new Account(BigInt(0), BigInt(1000))
4949
await stateManager.checkpoint()
@@ -106,12 +106,17 @@ See below example for common usage:
106106
```ts
107107
// ./examples/fromProofInstantiation.ts
108108

109-
import { DefaultStateManager } from '@ethereumjs/statemanager'
109+
import {
110+
MerkleStateManager,
111+
getMerkleStateProof,
112+
fromMerkleStateProof,
113+
addMerkleStateProofData,
114+
} from '@ethereumjs/statemanager'
110115
import { Address, hexToBytes } from '@ethereumjs/util'
111116

112117
const main = async () => {
113118
// setup `stateManager` with some existing address
114-
const stateManager = new DefaultStateManager()
119+
const stateManager = new MerkleStateManager()
115120
const contractAddress = new Address(hexToBytes('0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b'))
116121
const byteCode = hexToBytes('0x67ffffffffffffffff600160006000fb')
117122
const storageKey1 = hexToBytes(
@@ -127,12 +132,15 @@ const main = async () => {
127132
await stateManager.putStorage(contractAddress, storageKey1, storageValue1)
128133
await stateManager.putStorage(contractAddress, storageKey2, storageValue2)
129134

130-
const proof = await stateManager.getProof(contractAddress)
131-
const proofWithStorage = await stateManager.getProof(contractAddress, [storageKey1, storageKey2])
132-
const partialStateManager = await DefaultStateManager.fromProof(proof)
135+
const proof = await getMerkleStateProof(stateManager, contractAddress)
136+
const proofWithStorage = await getMerkleStateProof(stateManager, contractAddress, [
137+
storageKey1,
138+
storageKey2,
139+
])
140+
const partialStateManager = await fromMerkleStateProof(proof)
133141

134142
// To add more proof data, use `addProofData`
135-
await partialStateManager.addProofData(proofWithStorage)
143+
await addMerkleStateProofData(partialStateManager, proofWithStorage)
136144
console.log(await partialStateManager.getCode(contractAddress)) // contract bytecode is not included in proof
137145
console.log(await partialStateManager.getStorage(contractAddress, storageKey1), storageValue1) // should match
138146
console.log(await partialStateManager.getStorage(contractAddress, storageKey2), storageValue2) // should match
@@ -197,7 +205,7 @@ const main = async () => {
197205
const state = new RPCStateManager({ provider, blockTag })
198206
const evm = await createEVM({ blockchain, stateManager: state }) // note that evm is ready to run BLOCKHASH opcodes (over RPC)
199207
} catch (e) {
200-
console.log(e.message) // fetch would fail because provider url is not real. please replace provider with a valid rpc url string.
208+
console.log(e.message) // fetch would fail because provider url is not real. please replace provider with a valid RPC url string.
201209
}
202210
}
203211
void main()

packages/statemanager/examples/fromProofInstantiation.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { MerkleStateManager } from '@ethereumjs/statemanager'
1+
import {
2+
MerkleStateManager,
3+
addMerkleStateProofData,
4+
fromMerkleStateProof,
5+
getMerkleStateProof,
6+
} from '@ethereumjs/statemanager'
27
import { Address, hexToBytes } from '@ethereumjs/util'
38

49
const main = async () => {
@@ -19,12 +24,15 @@ const main = async () => {
1924
await stateManager.putStorage(contractAddress, storageKey1, storageValue1)
2025
await stateManager.putStorage(contractAddress, storageKey2, storageValue2)
2126

22-
const proof = await stateManager.getProof(contractAddress)
23-
const proofWithStorage = await stateManager.getProof(contractAddress, [storageKey1, storageKey2])
24-
const partialStateManager = await MerkleStateManager.fromProof(proof)
27+
const proof = await getMerkleStateProof(stateManager, contractAddress)
28+
const proofWithStorage = await getMerkleStateProof(stateManager, contractAddress, [
29+
storageKey1,
30+
storageKey2,
31+
])
32+
const partialStateManager = await fromMerkleStateProof(proof)
2533

2634
// To add more proof data, use `addProofData`
27-
await partialStateManager.addProofData(proofWithStorage)
35+
await addMerkleStateProofData(partialStateManager, proofWithStorage)
2836
console.log(await partialStateManager.getCode(contractAddress)) // contract bytecode is not included in proof
2937
console.log(await partialStateManager.getStorage(contractAddress, storageKey1), storageValue1) // should match
3038
console.log(await partialStateManager.getStorage(contractAddress, storageKey2), storageValue2) // should match

packages/statemanager/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './accessWitness.js'
22
export * from './cache/index.js'
33
export * from './merkleStateManager.js'
4+
export * from './proofs/index.js'
45
export * from './rpcStateManager.js'
56
export * from './simpleStateManager.js'
67
export * from './statefulVerkleStateManager.js'

0 commit comments

Comments
 (0)