@feelyourprotocol/statemanager 8141.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +373 -0
- package/README.md +331 -0
- package/dist/cjs/cache/account.d.ts +85 -0
- package/dist/cjs/cache/account.d.ts.map +1 -0
- package/dist/cjs/cache/account.js +252 -0
- package/dist/cjs/cache/account.js.map +1 -0
- package/dist/cjs/cache/cache.d.ts +23 -0
- package/dist/cjs/cache/cache.d.ts.map +1 -0
- package/dist/cjs/cache/cache.js +31 -0
- package/dist/cjs/cache/cache.js.map +1 -0
- package/dist/cjs/cache/caches.d.ts +19 -0
- package/dist/cjs/cache/caches.d.ts.map +1 -0
- package/dist/cjs/cache/caches.js +107 -0
- package/dist/cjs/cache/caches.js.map +1 -0
- package/dist/cjs/cache/code.d.ts +87 -0
- package/dist/cjs/cache/code.d.ts.map +1 -0
- package/dist/cjs/cache/code.js +258 -0
- package/dist/cjs/cache/code.js.map +1 -0
- package/dist/cjs/cache/index.d.ts +7 -0
- package/dist/cjs/cache/index.d.ts.map +1 -0
- package/dist/cjs/cache/index.js +23 -0
- package/dist/cjs/cache/index.js.map +1 -0
- package/dist/cjs/cache/originalStorageCache.d.ts +21 -0
- package/dist/cjs/cache/originalStorageCache.d.ts.map +1 -0
- package/dist/cjs/cache/originalStorageCache.js +52 -0
- package/dist/cjs/cache/originalStorageCache.js.map +1 -0
- package/dist/cjs/cache/storage.d.ts +101 -0
- package/dist/cjs/cache/storage.d.ts.map +1 -0
- package/dist/cjs/cache/storage.js +337 -0
- package/dist/cjs/cache/storage.js.map +1 -0
- package/dist/cjs/cache/types.d.ts +36 -0
- package/dist/cjs/cache/types.d.ts.map +1 -0
- package/dist/cjs/cache/types.js +8 -0
- package/dist/cjs/cache/types.js.map +1 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/merkleStateManager.d.ts +260 -0
- package/dist/cjs/merkleStateManager.d.ts.map +1 -0
- package/dist/cjs/merkleStateManager.js +616 -0
- package/dist/cjs/merkleStateManager.js.map +1 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/proof/index.d.ts +3 -0
- package/dist/cjs/proof/index.d.ts.map +1 -0
- package/dist/cjs/proof/index.js +19 -0
- package/dist/cjs/proof/index.js.map +1 -0
- package/dist/cjs/proof/merkle.d.ts +40 -0
- package/dist/cjs/proof/merkle.d.ts.map +1 -0
- package/dist/cjs/proof/merkle.js +182 -0
- package/dist/cjs/proof/merkle.js.map +1 -0
- package/dist/cjs/proof/rpc.d.ts +10 -0
- package/dist/cjs/proof/rpc.d.ts.map +1 -0
- package/dist/cjs/proof/rpc.js +20 -0
- package/dist/cjs/proof/rpc.js.map +1 -0
- package/dist/cjs/rpcStateManager.d.ts +162 -0
- package/dist/cjs/rpcStateManager.d.ts.map +1 -0
- package/dist/cjs/rpcStateManager.js +313 -0
- package/dist/cjs/rpcStateManager.js.map +1 -0
- package/dist/cjs/simpleStateManager.d.ts +54 -0
- package/dist/cjs/simpleStateManager.d.ts.map +1 -0
- package/dist/cjs/simpleStateManager.js +125 -0
- package/dist/cjs/simpleStateManager.js.map +1 -0
- package/dist/cjs/statefulBinaryTreeStateManager.d.ts +69 -0
- package/dist/cjs/statefulBinaryTreeStateManager.d.ts.map +1 -0
- package/dist/cjs/statefulBinaryTreeStateManager.js +576 -0
- package/dist/cjs/statefulBinaryTreeStateManager.js.map +1 -0
- package/dist/cjs/types.d.ts +92 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/util.d.ts +4 -0
- package/dist/cjs/util.d.ts.map +1 -0
- package/dist/cjs/util.js +21 -0
- package/dist/cjs/util.js.map +1 -0
- package/dist/esm/cache/account.d.ts +85 -0
- package/dist/esm/cache/account.d.ts.map +1 -0
- package/dist/esm/cache/account.js +248 -0
- package/dist/esm/cache/account.js.map +1 -0
- package/dist/esm/cache/cache.d.ts +23 -0
- package/dist/esm/cache/cache.d.ts.map +1 -0
- package/dist/esm/cache/cache.js +27 -0
- package/dist/esm/cache/cache.js.map +1 -0
- package/dist/esm/cache/caches.d.ts +19 -0
- package/dist/esm/cache/caches.d.ts.map +1 -0
- package/dist/esm/cache/caches.js +103 -0
- package/dist/esm/cache/caches.js.map +1 -0
- package/dist/esm/cache/code.d.ts +87 -0
- package/dist/esm/cache/code.d.ts.map +1 -0
- package/dist/esm/cache/code.js +254 -0
- package/dist/esm/cache/code.js.map +1 -0
- package/dist/esm/cache/index.d.ts +7 -0
- package/dist/esm/cache/index.d.ts.map +1 -0
- package/dist/esm/cache/index.js +7 -0
- package/dist/esm/cache/index.js.map +1 -0
- package/dist/esm/cache/originalStorageCache.d.ts +21 -0
- package/dist/esm/cache/originalStorageCache.d.ts.map +1 -0
- package/dist/esm/cache/originalStorageCache.js +48 -0
- package/dist/esm/cache/originalStorageCache.js.map +1 -0
- package/dist/esm/cache/storage.d.ts +101 -0
- package/dist/esm/cache/storage.d.ts.map +1 -0
- package/dist/esm/cache/storage.js +333 -0
- package/dist/esm/cache/storage.js.map +1 -0
- package/dist/esm/cache/types.d.ts +36 -0
- package/dist/esm/cache/types.d.ts.map +1 -0
- package/dist/esm/cache/types.js +5 -0
- package/dist/esm/cache/types.js.map +1 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +8 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/merkleStateManager.d.ts +260 -0
- package/dist/esm/merkleStateManager.d.ts.map +1 -0
- package/dist/esm/merkleStateManager.js +612 -0
- package/dist/esm/merkleStateManager.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/proof/index.d.ts +3 -0
- package/dist/esm/proof/index.d.ts.map +1 -0
- package/dist/esm/proof/index.js +3 -0
- package/dist/esm/proof/index.js.map +1 -0
- package/dist/esm/proof/merkle.d.ts +40 -0
- package/dist/esm/proof/merkle.d.ts.map +1 -0
- package/dist/esm/proof/merkle.js +175 -0
- package/dist/esm/proof/merkle.js.map +1 -0
- package/dist/esm/proof/rpc.d.ts +10 -0
- package/dist/esm/proof/rpc.d.ts.map +1 -0
- package/dist/esm/proof/rpc.js +17 -0
- package/dist/esm/proof/rpc.js.map +1 -0
- package/dist/esm/rpcStateManager.d.ts +162 -0
- package/dist/esm/rpcStateManager.d.ts.map +1 -0
- package/dist/esm/rpcStateManager.js +308 -0
- package/dist/esm/rpcStateManager.js.map +1 -0
- package/dist/esm/simpleStateManager.d.ts +54 -0
- package/dist/esm/simpleStateManager.d.ts.map +1 -0
- package/dist/esm/simpleStateManager.js +121 -0
- package/dist/esm/simpleStateManager.js.map +1 -0
- package/dist/esm/statefulBinaryTreeStateManager.d.ts +69 -0
- package/dist/esm/statefulBinaryTreeStateManager.d.ts.map +1 -0
- package/dist/esm/statefulBinaryTreeStateManager.js +572 -0
- package/dist/esm/statefulBinaryTreeStateManager.js.map +1 -0
- package/dist/esm/types.d.ts +92 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/types.js.map +1 -0
- package/dist/esm/util.d.ts +4 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +18 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/tsconfig.prod.cjs.tsbuildinfo +1 -0
- package/dist/tsconfig.prod.esm.tsbuildinfo +1 -0
- package/package.json +74 -0
- package/src/cache/account.ts +277 -0
- package/src/cache/cache.ts +35 -0
- package/src/cache/caches.ts +125 -0
- package/src/cache/code.ts +277 -0
- package/src/cache/index.ts +6 -0
- package/src/cache/originalStorageCache.ts +57 -0
- package/src/cache/storage.ts +369 -0
- package/src/cache/types.ts +38 -0
- package/src/index.ts +7 -0
- package/src/merkleStateManager.ts +737 -0
- package/src/proof/index.ts +2 -0
- package/src/proof/merkle.ts +264 -0
- package/src/proof/rpc.ts +24 -0
- package/src/rpcStateManager.ts +381 -0
- package/src/simpleStateManager.ts +154 -0
- package/src/statefulBinaryTreeStateManager.ts +789 -0
- package/src/types.ts +103 -0
- package/src/util.ts +28 -0
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Account, EthereumJSErrorWithoutCode, bytesToHex } from '@feelyourprotocol/util'
|
|
2
|
+
import { keccak_256 } from '@noble/hashes/sha3.js'
|
|
3
|
+
|
|
4
|
+
import { OriginalStorageCache } from './cache/originalStorageCache.ts'
|
|
5
|
+
import { modifyAccountFields } from './util.ts'
|
|
6
|
+
|
|
7
|
+
import type { AccountFields, Common, StateManagerInterface } from '@feelyourprotocol/common'
|
|
8
|
+
import type { Address, PrefixedHexString } from '@feelyourprotocol/util'
|
|
9
|
+
import type { SimpleStateManagerOpts } from './index.ts'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Simple and dependency-free state manager for basic state access use cases
|
|
13
|
+
* where a merkle-patricia or binary tree backed state manager is too heavy-weight.
|
|
14
|
+
*
|
|
15
|
+
* This state manager comes with the basic state access logic for
|
|
16
|
+
* accounts, storage and code (put* and get* methods) as well as a simple
|
|
17
|
+
* implementation of checkpointing but lacks methods implementations of
|
|
18
|
+
* state root related logic as well as some other non-core functions.
|
|
19
|
+
*
|
|
20
|
+
* Functionality provided is sufficient to be used for simple EVM use
|
|
21
|
+
* cases and the state manager is used as default there.
|
|
22
|
+
*
|
|
23
|
+
* For a more full fledged and MPT-backed state manager implementation
|
|
24
|
+
* have a look at the [`@feelyourprotocol/statemanager` package docs](https://github.com/ethereumjs/ethereumjs-monorepo/blob/master/packages/statemanager/docs/README.md).
|
|
25
|
+
*/
|
|
26
|
+
export class SimpleStateManager implements StateManagerInterface {
|
|
27
|
+
public accountStack: Map<PrefixedHexString, Account | undefined>[] = []
|
|
28
|
+
public codeStack: Map<PrefixedHexString, Uint8Array>[] = []
|
|
29
|
+
public storageStack: Map<string, Uint8Array>[] = []
|
|
30
|
+
|
|
31
|
+
originalStorageCache: {
|
|
32
|
+
get(address: Address, key: Uint8Array): Promise<Uint8Array>
|
|
33
|
+
clear(): void
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public readonly common?: Common
|
|
37
|
+
|
|
38
|
+
constructor(opts: SimpleStateManagerOpts = {}) {
|
|
39
|
+
this.checkpointSync()
|
|
40
|
+
this.originalStorageCache = new OriginalStorageCache(this.getStorage.bind(this))
|
|
41
|
+
this.common = opts.common
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected topAccountStack() {
|
|
45
|
+
return this.accountStack[this.accountStack.length - 1]
|
|
46
|
+
}
|
|
47
|
+
protected topCodeStack() {
|
|
48
|
+
return this.codeStack[this.codeStack.length - 1]
|
|
49
|
+
}
|
|
50
|
+
protected topStorageStack() {
|
|
51
|
+
return this.storageStack[this.storageStack.length - 1]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Synchronous version of checkpoint() to allow to call from constructor
|
|
55
|
+
protected checkpointSync() {
|
|
56
|
+
const newTopA = new Map(this.topAccountStack())
|
|
57
|
+
for (const [address, account] of newTopA) {
|
|
58
|
+
const accountCopy =
|
|
59
|
+
account !== undefined
|
|
60
|
+
? Object.assign(Object.create(Object.getPrototypeOf(account)), account)
|
|
61
|
+
: undefined
|
|
62
|
+
newTopA.set(address, accountCopy)
|
|
63
|
+
}
|
|
64
|
+
this.accountStack.push(newTopA)
|
|
65
|
+
this.codeStack.push(new Map(this.topCodeStack()))
|
|
66
|
+
this.storageStack.push(new Map(this.topStorageStack()))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async getAccount(address: Address): Promise<Account | undefined> {
|
|
70
|
+
return this.topAccountStack().get(address.toString())
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async putAccount(address: Address, account?: Account | undefined): Promise<void> {
|
|
74
|
+
this.topAccountStack().set(address.toString(), account)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async deleteAccount(address: Address): Promise<void> {
|
|
78
|
+
this.topAccountStack().set(address.toString(), undefined)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async modifyAccountFields(address: Address, accountFields: AccountFields): Promise<void> {
|
|
82
|
+
await modifyAccountFields(this, address, accountFields)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async getCode(address: Address): Promise<Uint8Array> {
|
|
86
|
+
return this.topCodeStack().get(address.toString()) ?? new Uint8Array(0)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async putCode(address: Address, value: Uint8Array): Promise<void> {
|
|
90
|
+
this.topCodeStack().set(address.toString(), value)
|
|
91
|
+
if ((await this.getAccount(address)) === undefined) {
|
|
92
|
+
await this.putAccount(address, new Account())
|
|
93
|
+
}
|
|
94
|
+
await this.modifyAccountFields(address, {
|
|
95
|
+
codeHash: (this.common?.customCrypto.keccak256 ?? keccak_256)(value),
|
|
96
|
+
})
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async getCodeSize(address: Address): Promise<number> {
|
|
100
|
+
const contractCode = await this.getCode(address)
|
|
101
|
+
return contractCode.length
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async getStorage(address: Address, key: Uint8Array): Promise<Uint8Array> {
|
|
105
|
+
return (
|
|
106
|
+
this.topStorageStack().get(`${address.toString()}_${bytesToHex(key)}`) ?? new Uint8Array(0)
|
|
107
|
+
)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise<void> {
|
|
111
|
+
this.topStorageStack().set(`${address.toString()}_${bytesToHex(key)}`, value)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async clearStorage(): Promise<void> {}
|
|
115
|
+
|
|
116
|
+
async checkpoint(): Promise<void> {
|
|
117
|
+
this.checkpointSync()
|
|
118
|
+
}
|
|
119
|
+
async commit(): Promise<void> {
|
|
120
|
+
this.accountStack.splice(-2, 1)
|
|
121
|
+
this.codeStack.splice(-2, 1)
|
|
122
|
+
this.storageStack.splice(-2, 1)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async revert(): Promise<void> {
|
|
126
|
+
this.accountStack.pop()
|
|
127
|
+
this.codeStack.pop()
|
|
128
|
+
this.storageStack.pop()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async flush(): Promise<void> {}
|
|
132
|
+
clearCaches(): void {}
|
|
133
|
+
|
|
134
|
+
shallowCopy(): StateManagerInterface {
|
|
135
|
+
const copy = new SimpleStateManager({ common: this.common })
|
|
136
|
+
for (let i = 0; i < this.accountStack.length; i++) {
|
|
137
|
+
copy.accountStack.push(new Map(this.accountStack[i]))
|
|
138
|
+
copy.codeStack.push(new Map(this.codeStack[i]))
|
|
139
|
+
copy.storageStack.push(new Map(this.storageStack[i]))
|
|
140
|
+
}
|
|
141
|
+
return copy
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// State root functionality not implemented
|
|
145
|
+
getStateRoot(): Promise<Uint8Array> {
|
|
146
|
+
throw EthereumJSErrorWithoutCode('Method not implemented.')
|
|
147
|
+
}
|
|
148
|
+
setStateRoot(): Promise<void> {
|
|
149
|
+
throw EthereumJSErrorWithoutCode('Method not implemented.')
|
|
150
|
+
}
|
|
151
|
+
hasStateRoot(): Promise<boolean> {
|
|
152
|
+
throw EthereumJSErrorWithoutCode('Method not implemented.')
|
|
153
|
+
}
|
|
154
|
+
}
|