Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various Verkle Fixes #3650

Merged
merged 59 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
a847f86
scaffolding
acolytec3 Aug 28, 2024
107c883
broken WIP
acolytec3 Aug 28, 2024
78f1990
partial implementations
acolytec3 Aug 29, 2024
9cf5af7
fix getAccount
acolytec3 Aug 29, 2024
1419c9d
add todo
acolytec3 Aug 29, 2024
bb25fea
make trie.get accept suffixes
acolytec3 Aug 29, 2024
8c0605c
clean up reference
acolytec3 Aug 30, 2024
ef37d62
Add reserved bytes to encodeBasicData function
acolytec3 Aug 31, 2024
adad631
FIx encoding again
acolytec3 Sep 3, 2024
e69001c
spelling [no ci]
acolytec3 Sep 3, 2024
053fdea
change param to account [no ci]
acolytec3 Sep 3, 2024
762a694
Add support for basic account delete
acolytec3 Sep 3, 2024
293c86a
implement chunkify code
acolytec3 Sep 3, 2024
f6bbd1f
Add putCode
acolytec3 Sep 3, 2024
1d148ed
Move code to helpers
acolytec3 Sep 3, 2024
daa5faf
getCode and getCodeSize
acolytec3 Sep 4, 2024
036fa67
Start work on tests
acolytec3 Sep 4, 2024
d4e10dd
Update magic numbers to constants
acolytec3 Sep 4, 2024
e006237
Make get/putCode work
acolytec3 Sep 4, 2024
680ec51
Fix various get/putCode bugs
acolytec3 Sep 4, 2024
c7047fd
add get/putstorage
acolytec3 Sep 4, 2024
9161e02
export SFVKSM [no ci]
acolytec3 Sep 5, 2024
31fa232
Merge remote-tracking branch 'origin/master' into statefulVerkleState…
acolytec3 Sep 5, 2024
15e1d76
add commit/flush/revert
acolytec3 Sep 5, 2024
78ba172
Tests for caching
acolytec3 Sep 5, 2024
1f5e39f
make cspell happy
acolytec3 Sep 5, 2024
95b51e3
lint
acolytec3 Sep 5, 2024
04be4c6
add back missing method from interface
acolytec3 Sep 5, 2024
8b7d4b1
Merge remote-tracking branch 'origin/master' into statefulVerkleState…
acolytec3 Sep 6, 2024
3dc9346
Update packages/util/test/verkle.spec.ts
gabrocheleau Sep 8, 2024
f9d0825
Apply suggestions from code review
gabrocheleau Sep 8, 2024
1a8bd79
address some feedback
acolytec3 Sep 8, 2024
76316cb
Merge remote-tracking branch 'origin/master' into statefulVerkleState…
acolytec3 Sep 8, 2024
54c30c5
Update types and add test
acolytec3 Sep 6, 2024
f6e89ec
FIx commitment format
acolytec3 Sep 6, 2024
f4922c3
Update verkle crypto and add proof test
acolytec3 Sep 9, 2024
9f94d7d
add max chunks constant
acolytec3 Sep 9, 2024
36ac903
delete account in put if no account
acolytec3 Sep 9, 2024
6af9a3f
spelling
acolytec3 Sep 9, 2024
0133cc1
fix basic data encoding offsets
acolytec3 Sep 9, 2024
7c346df
remove console log
acolytec3 Sep 9, 2024
b97e813
Fix suffix logic
acolytec3 Sep 9, 2024
45b64c7
wasm update
acolytec3 Sep 10, 2024
f4396d7
update interface
acolytec3 Sep 10, 2024
fb1ba09
Merge remote-tracking branch 'origin/master' into proofs-again
acolytec3 Sep 10, 2024
c9e6f71
update package lock
acolytec3 Sep 10, 2024
03942e8
update verkle crypto wasm again
acolytec3 Sep 13, 2024
5273d53
Merge remote-tracking branch 'origin/master' into proofs-again
acolytec3 Sep 19, 2024
c74211a
console logs
acolytec3 Sep 20, 2024
20f5deb
maybe another fix
acolytec3 Sep 23, 2024
e82d77a
various fixes
acolytec3 Sep 24, 2024
313d92e
Merge remote-tracking branch 'origin/master' into proofs-again
acolytec3 Sep 24, 2024
ffab34a
update tests
acolytec3 Sep 24, 2024
60045b5
Remove obsolete proof helpers
acolytec3 Sep 24, 2024
2f31f54
add second test
acolytec3 Sep 24, 2024
fb2ebcd
add explanatory comment for leaf marker
acolytec3 Sep 24, 2024
918c8b5
Add safeguards to leafnode.create
acolytec3 Sep 24, 2024
ed68d14
use correct randomBytes
acolytec3 Sep 24, 2024
c712e56
Merge remote-tracking branch 'origin/master' into proofs-again
acolytec3 Sep 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add get/putstorage
  • Loading branch information
acolytec3 committed Sep 4, 2024
commit c7047fdfd45236a10f6e3bc8c25c30a41bfb38ca
57 changes: 51 additions & 6 deletions packages/statemanager/src/statefulVerkleStateManager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Common, Mainnet } from '@ethereumjs/common'
import { RLP } from '@ethereumjs/rlp'
import {
Account,
type Address,
Expand All @@ -8,6 +9,7 @@ import {
VERKLE_CODE_OFFSET,
VERKLE_NODE_WIDTH,
VerkleLeafType,
bytesToBigInt,
chunkifyCode,
createAccountFromRLP,
createPartialAccount,
Expand All @@ -17,7 +19,10 @@ import {
generateChunkSuffixes,
generateCodeStems,
getVerkleStem,
getVerkleTreeKeyForStorageSlot,
hexToBytes,
short,
unpadBytes,
} from '@ethereumjs/util'
import { VerkleTree } from '@ethereumjs/verkle'
import debugDefault from 'debug'
Expand Down Expand Up @@ -305,15 +310,55 @@ export class StatefulVerkleStateManager implements StateManagerInterface {
if (accountBytes === undefined) return 0
return decodeVerkleLeafBasicData(accountBytes).codeSize
}
getStorage(address: Address, key: Uint8Array): Promise<Uint8Array> {
throw new Error('Method not implemented.')
getStorage = async (address: Address, key: Uint8Array): Promise<Uint8Array> => {
if (key.length !== 32) {
throw new Error('Storage key must be 32 bytes long')
}
const cachedValue = this._caches?.storage?.get(address, key)
if (cachedValue !== undefined) {
const decoded = RLP.decode(cachedValue ?? new Uint8Array(0)) as Uint8Array
return decoded
}

const account = await this.getAccount(address)
if (!account) {
return new Uint8Array()
}
const storageKey = await getVerkleTreeKeyForStorageSlot(
address,
bytesToBigInt(key, true),
this.verkleCrypto,
)
const value = await this._trie.get(storageKey.slice(0, 31), [storageKey[31]])

this._caches?.storage?.put(address, key, value[0] ?? hexToBytes('0x80'))
const decoded = RLP.decode(value[0] ?? new Uint8Array(0)) as Uint8Array
return decoded
}
putStorage(address: Address, key: Uint8Array, value: Uint8Array): Promise<void> {
throw new Error('Method not implemented.')

putStorage = async (address: Address, key: Uint8Array, value: Uint8Array): Promise<void> => {
value = unpadBytes(value)
this._caches?.storage?.put(address, key, RLP.encode(value))
if (this._caches?.storage === undefined) {
const storageKey = await getVerkleTreeKeyForStorageSlot(
address,
bytesToBigInt(key, true),
this.verkleCrypto,
)
await this._trie.put(storageKey.slice(0, 31), [storageKey[31]], [RLP.encode(value)])
}
}
clearStorage(address: Address): Promise<void> {
throw new Error('Method not implemented.')

clearStorage = async (address: Address): Promise<void> => {
// TODO: Determine if it's possible to clear the actual slots in the trie
// since the EIP doesn't seem to state how to handle this
// The main concern I have is that we have no way of identifying all storage slots
// for a given account so we can't correctly update the trie's root hash
// (since presumably "clearStorage" would imply writing over all of the storage slots with zeros)
// Also, do we still need a storageRoot? - presumably not since we don't have separate storage tries
this._caches?.storage?.clearStorage(address)
}

checkpoint = async (): Promise<void> => {
this._trie.checkpoint()
this._caches?.checkpoint()
Expand Down
24 changes: 20 additions & 4 deletions packages/statemanager/test/statefulVerkleStateManager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { createAccount, createAddressFromString, matchingBytesLength } from '@ethereumjs/util'
import {
Account,
bigIntToBytes,
createAccount,
createAddressFromString,
hexToBytes,
matchingBytesLength,
setLengthLeft,
} from '@ethereumjs/util'
import { createVerkleTree } from '@ethereumjs/verkle'
import { hexToBytes } from 'ethereum-cryptography/utils'
import { loadVerkleCrypto } from 'verkle-cryptography-wasm'
import { assert, beforeAll, describe, it } from 'vitest'

import { StatefulVerkleStateManager } from '../src/statefulVerkleStateManager.js'

import type { VerkleCrypto } from '@ethereumjs/util'
import type { PrefixedHexString, VerkleCrypto } from '@ethereumjs/util'

describe('Verkle Tree API tests', () => {
let verkleCrypto: VerkleCrypto
Expand Down Expand Up @@ -40,7 +47,7 @@ describe('Verkle Tree API tests', () => {
const retrievedBigByteCode = await sm.getCode(address)
assert.deepEqual(bigByteCode, retrievedBigByteCode)
const reallyBigByteCode = hexToBytes(
(await import('./testdata/biggestContractEver.json')).default.bytecode,
(await import('./testdata/biggestContractEver.json')).default.bytecode as PrefixedHexString,
)
// Biggest mainnet contract - 0x10C621008B210C3A5d0385e458B48af05BF4Ec88 (supposedly anyway)
await sm.putCode(address, reallyBigByteCode)
Expand All @@ -51,4 +58,13 @@ describe('Verkle Tree API tests', () => {
reallyBigByteCode.length,
)
})
it('should put and get storage', async () => {
const trie = await createVerkleTree()
const sm = new StatefulVerkleStateManager({ trie, verkleCrypto })
const address = createAddressFromString('0x9e5ef720fa2cdfa5291eb7e711cfd2e62196f4b3')
await sm.putAccount(address, new Account(0n, 1n))
await sm.putStorage(address, setLengthLeft(bigIntToBytes(0n), 32), hexToBytes('0x1'))
const retrievedValue = await sm.getStorage(address, setLengthLeft(bigIntToBytes(0n), 32))
assert.deepEqual(retrievedValue, hexToBytes('0x1'))
})
})
Loading