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

client: save preimages feature #3143

Merged
merged 96 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
0906767
client: PreimageManager and Preimage storage in MetaDB
gabrocheleau Nov 5, 2023
1409a05
Merge branch 'master' into client/save-preimages
acolytec3 Nov 7, 2023
5e76ea0
common: add getAppliedKey method to StateManagerInterface
gabrocheleau Nov 8, 2023
c2938eb
evm: add preimage reporting
gabrocheleau Nov 8, 2023
3e1984a
statemanager: add getAppliedKey methods to the statemanager implement…
gabrocheleau Nov 8, 2023
76a3876
vm: improve some types
gabrocheleau Nov 8, 2023
63a9e61
vm: implement preimage reporting
gabrocheleau Nov 8, 2023
9f0bf8e
client: add preimage saving
gabrocheleau Nov 8, 2023
d6af5a9
Merge branch 'master' into client/save-preimages
gabrocheleau Nov 8, 2023
fe8022a
evm: update to unprefixedHexToBytes
gabrocheleau Nov 9, 2023
dbbd07f
Merge branch 'master' into client/save-preimages
gabrocheleau Nov 9, 2023
50d85b8
vm: add reportPreimages test to runTx tests
gabrocheleau Nov 9, 2023
6c32301
vm: pass down reportPreimages arg from block to tx
gabrocheleau Nov 9, 2023
b338b99
client: pass down reportPreimages from client to vm through runBlock
gabrocheleau Nov 9, 2023
c9ed40a
client: unify config options naming
gabrocheleau Nov 9, 2023
e0a4baf
client: preimageManager test
gabrocheleau Nov 9, 2023
8787ac5
client: test preimage, wip
gabrocheleau Nov 12, 2023
157debe
common: add todo to gasPrices from 6800
gabrocheleau Dec 6, 2023
88c85af
merge with master
gabrocheleau Dec 10, 2023
5b2cf25
statemanager: fix type issues
gabrocheleau Dec 10, 2023
3c63902
dev accessWitness class on the lines of geth impl
g11tech Dec 13, 2023
7954bb6
integrate accesswitness in evm/vm/runtx flow
g11tech Dec 15, 2023
a603635
plugin the gas schedule for tx origin and destination accesses
g11tech Dec 15, 2023
53af5f2
complete, debug and fix the call and create access charges
g11tech Dec 17, 2023
36a8d36
Merge branch 'master' into client/save-preimages
gabrocheleau Dec 20, 2023
1a34e71
plug the access gas usage on the evm opcode runs
g11tech Dec 26, 2023
1091eb3
debug and fix the code chunk accesses and the poststate setting
g11tech Dec 26, 2023
cae6302
Merge branch 'master' into client/save-preimages
gabrocheleau Dec 26, 2023
3519605
implement access merging and accessed state tracking and traversing
g11tech Dec 27, 2023
1ac77fa
also provide chunkKey for easy reference
g11tech Dec 27, 2023
e2af8de
decode raw accesses to structured ones and debug log them
g11tech Dec 30, 2023
ba47683
debug and add the missing accesses for coinbase, withdrawals
g11tech Dec 30, 2023
2521755
stateManager: implement cache handling in the context of statelessness
gabrocheleau Dec 30, 2023
3a665ff
Merge branch 'client/kaustinen-syncing' of https://github.com/ethereu…
gabrocheleau Dec 30, 2023
0a3ae52
modify poststate to use accesses from the accesswitness to compare an…
g11tech Dec 30, 2023
7979286
stateManager: getComputedValue
gabrocheleau Dec 30, 2023
6b10752
fixes for the getcomputedval fn helper for the code
g11tech Jan 2, 2024
626644e
correctly implement the getcontractcode with partial accessed segment…
g11tech Jan 4, 2024
6c8b3f4
statemanager: tentative fixes & improvements to code cache handling
gabrocheleau Jan 5, 2024
fabfba4
statemanager: checkpoint code cache
gabrocheleau Jan 5, 2024
939cf9b
fix the code chunk comparision in post state verification
g11tech Jan 13, 2024
760822f
rename kaustinen2 local testnet data for
g11tech Jan 13, 2024
4bf589e
fix pre and post state null chunks handling and corresponding get com…
g11tech Jan 13, 2024
7d001e6
setup the client to statelessly execute the verkle blocks randomly fr…
g11tech Jan 14, 2024
cba0448
setup to statelessly run block13 of kaustinen2 in client test spec fo…
g11tech Jan 14, 2024
f299ad6
setup the client kaustinen2 stateless test to test and debug block 16
g11tech Jan 14, 2024
5d24fa8
improve the post state witness mismatch logging to be more comprehens…
g11tech Jan 14, 2024
d4eb8e6
improve chunk verification tracking logging and fix post/prestate key…
g11tech Jan 16, 2024
3b714fa
debug and fix code accesses and overhaul/fix accesscharges on create/…
g11tech Jan 16, 2024
ba54f16
add a more complete matching of generated and provided executed witne…
g11tech Jan 16, 2024
9ac950d
fix return
g11tech Jan 16, 2024
e2e5330
debug, discuss and remove proof of absence charges for contract creat…
g11tech Jan 16, 2024
66bef47
only charge code accesses if accessed from state and when code is wri…
g11tech Jan 18, 2024
fa647f2
Merge branch 'master' into client/save-preimages
gabrocheleau Jan 18, 2024
ec52976
handle bigint treeIndex for the bigint slot and debug/match the prede…
g11tech Jan 19, 2024
1f1cf19
chore: resolve merge conflicts
gabrocheleau Jan 20, 2024
5a51cb8
client: fix kaustinen tests and migrate to new testing framework
gabrocheleau Jan 20, 2024
0a79975
shift kaustinen 2 test to block 13
g11tech Jan 21, 2024
c0c02f9
client: remove stale kaustinen2 test data
gabrocheleau Jan 21, 2024
c21ef84
vm: adjust rewardAccount new common arg ordering and make it optional
gabrocheleau Jan 22, 2024
562fac2
Merge branch 'master' into client/kaustinen-syncing
gabrocheleau Jan 22, 2024
2eb862e
Merge branch 'master' into client/kaustinen-syncing
gabrocheleau Jan 24, 2024
077b83a
Merge branch 'master' into client/kaustinen-syncing
jochem-brouwer Jan 27, 2024
7ddd761
add block12 to the spec list
g11tech Jan 27, 2024
a003c89
move invalid opcode check outside jump analysis
g11tech Jan 27, 2024
89959b7
small cleanup in runtx
g11tech Jan 27, 2024
1c36ec4
chore: merge
gabrocheleau Jan 28, 2024
142c453
chore: merge
gabrocheleau Jan 28, 2024
bb6d298
add preimages spec
g11tech Jan 29, 2024
cdb35be
chore: merge
gabrocheleau Feb 10, 2024
3758656
Merge branch 'master' into client/save-preimages
gabrocheleau Feb 17, 2024
9b8a5bc
build various block scenarios for preimages gen
g11tech Feb 18, 2024
9d1e945
client: revert vmexecution preimages test
gabrocheleau Feb 18, 2024
af803b0
Merge branch 'client/save-preimages' of https://github.com/ethereumjs…
gabrocheleau Feb 18, 2024
6aee56a
client: remove unnecessary boolean explicit comparison
gabrocheleau Feb 18, 2024
58c8ecd
client: remove unused import
gabrocheleau Feb 18, 2024
b7214cd
client: revert re-ordering
gabrocheleau Feb 18, 2024
66b18a5
stateManager: support non keccak256 applied keys
gabrocheleau Feb 18, 2024
80a2503
debug and persist preimages on runWithoutSetHead as well
g11tech Feb 21, 2024
73c034d
add preimages for coinbase,withdrawals to the test and fix the spec t…
g11tech Feb 21, 2024
b4cbeb0
Merge branch 'master' into client/save-preimages
gabrocheleau Feb 24, 2024
8c59302
client: fix preimage tests ts errors
gabrocheleau Feb 24, 2024
3a459fe
client: minor adjustments to preimage test
gabrocheleau Feb 25, 2024
cb76436
client: save preimages helper
gabrocheleau Feb 25, 2024
d128cf6
vm: save preimages at the block level, new applyBlockResult type
gabrocheleau Feb 25, 2024
6bbd14f
common: make getAppliedKey optional
gabrocheleau Feb 26, 2024
0e589de
evm/vm: handle optional getAppliedKey
gabrocheleau Feb 26, 2024
1c413fa
client: add preimage doc
jochem-brouwer Feb 26, 2024
d5a6ca8
trie: export "Path" interface (#3292)
ScottyPoi Feb 26, 2024
92ac258
Snap sync: use zero-element proof for checking validity of final, emp…
scorbajio Feb 26, 2024
e3843bc
Integrate `kzg-wasm` into monorepo (#3294)
acolytec3 Feb 28, 2024
0b366a2
Make trustedSetupPath in Util kzg module optional (#3296)
holgerd77 Feb 29, 2024
1408e29
client: add tx preimages to preimage test cases
gabrocheleau Feb 29, 2024
ad72130
Merge branch 'master' into client/save-preimages
jochem-brouwer Mar 1, 2024
a50ee60
Merge branch 'master' into client/save-preimages
jochem-brouwer Mar 1, 2024
7a0842d
Merge branch 'client/save-preimages' of github.com:ethereumjs/ethereu…
jochem-brouwer Mar 1, 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
6 changes: 2 additions & 4 deletions packages/block/test/testdata/payload-kaustinen.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,7 @@
}
],
"verkleProof": {
"otherStems": [
"0x9444524c2261a7086dd332bb05d79b1c63e74913a97b7b85c680368b19db0e"
],
"otherStems": ["0x9444524c2261a7086dd332bb05d79b1c63e74913a97b7b85c680368b19db0e"],
"depthExtensionPresent": "0x0a101209120808",
"commitmentsByPath": [
"0x1ba24c38c9aff43cf2793ed22a2fdd04282d558bcfad33589b5e4697c0662e45",
Expand Down Expand Up @@ -284,4 +282,4 @@
}
}
}
}
}
10 changes: 10 additions & 0 deletions packages/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,14 @@ export interface ConfigOptions {
snapAvailabilityDepth?: bigint
snapTransitionSafeDepth?: bigint

/**
* Save account keys preimages in the meta db (default: false)
*/
savePreimages?: boolean

/**
* Enables stateless verkle block execution (default: false)
*/
statelessVerkle?: boolean
}

Expand Down Expand Up @@ -431,6 +439,7 @@ export class Config {
// Defaulting to false as experimental as of now
public readonly enableSnapSync: boolean
public readonly useStringValueTrieDB: boolean
public readonly savePreimages: boolean

public readonly statelessVerkle: boolean

Expand Down Expand Up @@ -477,6 +486,7 @@ export class Config {
this.debugCode = options.debugCode ?? Config.DEBUGCODE_DEFAULT
this.mine = options.mine ?? false
this.isSingleNode = options.isSingleNode ?? false
this.savePreimages = options.savePreimages ?? false

if (options.vmProfileBlocks !== undefined || options.vmProfileTxs !== undefined) {
this.vmProfilerOpts = {
Expand Down
27 changes: 27 additions & 0 deletions packages/client/src/execution/preimage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { DBKey, MetaDBManager } from '../util/metaDBManager'

/**
* The `PreImagesManager` saves the preimages of hashed keys. This is necessary for the Verkle transition.
* A "PreImage" of a hash is whatever the input is to the hashed function. So, if one calls `keccak256(X)` with
* output `Y` then `X` is the preimage of `Y`. It thus serves to recover the input to the trapdoor hash function,
* which would otherwise not be feasible.
*/
export class PreimagesManager extends MetaDBManager {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH: I still have my problems to understand what a preimage even is. Can we give this some explicit documentation/explanation? A link to some source also wound't hurt.

Maybe starting here and give the class some line of code docs where an explanation is included.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I would prefer the name PreImagesManager I think 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that Preimage is actually the correct casing. Preimage is the correct mathematical term (i.e. not pre-image). See for e.g. https://en.wiktionary.org/wiki/preimage

/**
* Returns the preimage for a given hashed key
* @param key the hashed key
* @returns the preimage of the hashed key
*/
async getPreimage(key: Uint8Array): Promise<Uint8Array | null> {
return this.get(DBKey.Preimage, key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now realize that this class "only" maps get -> getPreimage and set -> setPreimage 🤔 I'm not super sure if this is necessary, since it is clear what we are trying to get (get(DBKey.Preimage, key)). But not strong feelings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this API relevance is quite minimal, however I do like that it's abstracting away the concept of "DBKey". Also, we could imagine extending this eventually with more complexity (e.g. handling multiple hashing algorithms, deleting/updating in the context of the transition), so I think this creates a better structure to build upon.

}

/**
* Saves a preimage to the db for a given hashed key.
* @param key The hashed key
* @param preimage The preimage to save
*/
async savePreimage(key: Uint8Array, preimage: Uint8Array) {
await this.put(DBKey.Preimage, key, preimage)
}
}
81 changes: 61 additions & 20 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
StatelessVerkleStateManager,
} from '@ethereumjs/statemanager'
import { Trie } from '@ethereumjs/trie'
import { BIGINT_0, BIGINT_1, Lock, ValueEncoding, bytesToHex, equalsBytes } from '@ethereumjs/util'
import {
BIGINT_0,
BIGINT_1,
Lock,
ValueEncoding,
bytesToHex,
equalsBytes,
hexToBytes,
} from '@ethereumjs/util'
import { VM } from '@ethereumjs/vm'

import { Event } from '../types'
Expand All @@ -21,6 +29,7 @@

import { Execution } from './execution'
import { LevelDB } from './level'
import { PreimagesManager } from './preimage'
import { ReceiptsManager } from './receipt'

import type { ExecutionOptions } from './execution'
Expand Down Expand Up @@ -50,6 +59,7 @@
public chainStatus: ChainStatus | null = null

public receiptsManager?: ReceiptsManager
public preimagesManager?: PreimagesManager
private pendingReceipts?: Map<string, TxReceipt[]>
private vmPromise?: Promise<number | null>

Expand Down Expand Up @@ -94,25 +104,34 @@
;(this.vm as any).blockchain = this.chain.blockchain
}

if (this.metaDB && this.config.saveReceipts) {
this.receiptsManager = new ReceiptsManager({
chain: this.chain,
config: this.config,
metaDB: this.metaDB,
})
this.pendingReceipts = new Map()
this.chain.blockchain.events.addListener(
'deletedCanonicalBlocks',
async (blocks, resolve) => {
// Once a block gets deleted from the chain, delete the receipts also
for (const block of blocks) {
await this.receiptsManager?.deleteReceipts(block)
}
if (resolve !== undefined) {
resolve()
if (this.metaDB) {
if (this.config.saveReceipts) {
this.receiptsManager = new ReceiptsManager({
chain: this.chain,
config: this.config,
metaDB: this.metaDB,
})
this.pendingReceipts = new Map()
this.chain.blockchain.events.addListener(
'deletedCanonicalBlocks',
async (blocks, resolve) => {
// Once a block gets deleted from the chain, delete the receipts also
for (const block of blocks) {
await this.receiptsManager?.deleteReceipts(block)
}
if (resolve !== undefined) {
resolve()
}

Check warning on line 124 in packages/client/src/execution/vmexecution.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/execution/vmexecution.ts#L121-L124

Added lines #L121 - L124 were not covered by tests
}
}
)
)
}
if (this.config.savePreimages) {

Check warning on line 128 in packages/client/src/execution/vmexecution.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/execution/vmexecution.ts#L126-L128

Added lines #L126 - L128 were not covered by tests
this.preimagesManager = new PreimagesManager({
chain: this.chain,
config: this.config,
metaDB: this.metaDB,
})
}
}
}

Expand Down Expand Up @@ -407,8 +426,18 @@
if (skipHeaderValidation) {
skipBlockchain = true
}
const reportPreimages = this.config.savePreimages

const result = await vm.runBlock({ clearCache, ...opts, skipHeaderValidation })
const result = await vm.runBlock({
clearCache,
...opts,
skipHeaderValidation,
reportPreimages,
})

if (this.config.savePreimages && result.preimages !== undefined) {
await this.savePreimages(result.preimages)
}
receipts = result.receipts
}
if (receipts !== undefined) {
Expand Down Expand Up @@ -438,6 +467,14 @@
return true
}

async savePreimages(preimages: Map<string, Uint8Array>) {
if (this.preimagesManager !== undefined) {
for (const [key, preimage] of preimages) {
await this.preimagesManager.savePreimage(hexToBytes(key), preimage)
}
}
}

Check warning on line 477 in packages/client/src/execution/vmexecution.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/execution/vmexecution.ts#L476-L477

Added lines #L476 - L477 were not covered by tests
/**
* Sets the chain to a new head block.
* Should only be used after {@link VMExecution.runWithoutSetHead}
Expand Down Expand Up @@ -677,6 +714,7 @@
clearCache,
skipBlockValidation,
skipHeaderValidation: true,
reportPreimages: this.config.savePreimages,

Check warning on line 717 in packages/client/src/execution/vmexecution.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/execution/vmexecution.ts#L717

Added line #L717 was not covered by tests
})
const afterTS = Date.now()
const diffSec = Math.round((afterTS - beforeTS) / 1000)
Expand All @@ -691,6 +729,9 @@
}

await this.receiptsManager?.saveReceipts(block, result.receipts)
if (this.config.savePreimages && result.preimages !== undefined) {
await this.savePreimages(result.preimages)
}

Check warning on line 734 in packages/client/src/execution/vmexecution.ts

View check run for this annotation

Codecov / codecov/patch

packages/client/src/execution/vmexecution.ts#L732-L734

Added lines #L732 - L734 were not covered by tests

txCounter += block.transactions.length
// set as new head block
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export interface ClientOpts {
vmProfileTxs?: boolean
loadBlocksFromRlp?: string
pruneEngineCache?: boolean
savePreimages?: boolean
verkleGenesisStateRoot?: Uint8Array
statelessVerkle?: boolean
engineNewpayloadMaxExecute?: number
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/util/metaDBManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export enum DBKey {
SkeletonBlockHashToNumber,
SkeletonStatus,
SkeletonUnfinalizedBlockByHash,
Preimage,
}

export interface MetaDBManagerOptions {
Expand Down
Loading
Loading