Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into add-kurtosis-to-ci-…
Browse files Browse the repository at this point in the history
…workflows
  • Loading branch information
acolytec3 committed May 29, 2024
2 parents 58d2a6c + ee8e02f commit 667d3e4
Show file tree
Hide file tree
Showing 17 changed files with 290 additions and 72 deletions.
11 changes: 6 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/client/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,12 @@ const args: ClientOpts = yargs
boolean: true,
hidden: true,
})
.option('initialVerkleStateRoot', {
describe:
'Provides an initial stateRoot to start the StatelessVerkleStateManager. This is required to bootstrap verkle witness proof verification, since they depend on the stateRoot of the parent block',
string: true,
coerce: (initialVerkleStateRoot: PrefixedHexString) => hexToBytes(initialVerkleStateRoot),
})
.option('useJsCrypto', {
describe: 'Use pure Javascript cryptography functions',
boolean: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/client/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ export interface ConfigOptions {
statelessVerkle?: boolean
startExecution?: boolean
ignoreStatelessInvalidExecs?: boolean
initialVerkleStateRoot?: Uint8Array

/**
* Enables Prometheus Metrics that can be collected for monitoring client health
Expand Down Expand Up @@ -451,6 +452,7 @@ export class Config {
public readonly statelessVerkle: boolean
public readonly startExecution: boolean
public readonly ignoreStatelessInvalidExecs: boolean
public readonly initialVerkleStateRoot: Uint8Array

public synchronized: boolean
public lastsyncronized?: boolean
Expand Down Expand Up @@ -544,6 +546,7 @@ export class Config {
this.ignoreStatelessInvalidExecs = options.ignoreStatelessInvalidExecs ?? false

this.metrics = options.prometheusMetrics
this.initialVerkleStateRoot = options.initialVerkleStateRoot ?? new Uint8Array()

// Start it off as synchronized if this is configured to mine or as single node
this.synchronized = this.isSingleNode ?? this.mine
Expand Down
6 changes: 5 additions & 1 deletion packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ export class VMExecution extends Execution {
return
}
this.config.logger.info(`Setting up verkleVM`)
const stateManager = await StatelessVerkleStateManager.create()
const stateManager = await StatelessVerkleStateManager.create({
initialStateRoot: this.config.initialVerkleStateRoot,
})
this.verkleVM = await VM.create({
common: this.config.execCommon,
blockchain: this.chain.blockchain,
Expand Down Expand Up @@ -439,6 +441,7 @@ export class VMExecution extends Execution {
const result = await vm.runBlock({
clearCache,
...opts,
parentStateRoot: prevVMStateRoot,
skipHeaderValidation,
reportPreimages,
})
Expand Down Expand Up @@ -733,6 +736,7 @@ export class VMExecution extends Execution {
skipBlockValidation,
skipHeaderValidation: true,
reportPreimages: this.config.savePreimages,
parentStateRoot: parentState,
})
const afterTS = Date.now()
const diffSec = Math.round((afterTS - beforeTS) / 1000)
Expand Down
17 changes: 16 additions & 1 deletion packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ const jsonRpcReceipt = async (
? bytesToHex((receipt as PreByzantiumTxReceipt).stateRoot)
: undefined,
status:
((receipt as PostByzantiumTxReceipt).status as unknown) instanceof Uint8Array
(receipt as PostByzantiumTxReceipt).status !== undefined
? intToHex((receipt as PostByzantiumTxReceipt).status)
: undefined,
blobGasUsed: blobGasUsed !== undefined ? bigIntToHex(blobGasUsed) : undefined,
Expand Down Expand Up @@ -452,6 +452,12 @@ export class Eth {
[validators.rewardPercentiles],
]
)

this.blobBaseFee = middleware(
callWithStackTrace(this.blobBaseFee.bind(this), this._rpcDebug),
0,
[]
)
}

/**
Expand Down Expand Up @@ -1317,4 +1323,13 @@ export class Eth {
reward: rewards.map((r) => r.map(bigIntToHex)),
}
}

/**
*
* @returns the blob base fee for the next/pending block in wei
*/
async blobBaseFee() {
const headBlock = await this._chain.getCanonicalHeadHeader()
return bigIntToHex(headBlock.calcNextBlobGasPrice())
}
}
121 changes: 121 additions & 0 deletions packages/client/test/rpc/eth/blobBaseFee.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { Hardfork } from '@ethereumjs/common'
import { TransactionFactory } from '@ethereumjs/tx'
import {
Address,
BIGINT_0,
BIGINT_256,
blobsToCommitments,
commitmentsToVersionedHashes,
getBlobs,
hexToBytes,
} from '@ethereumjs/util'
import { loadKZG } from 'kzg-wasm'
import { assert, describe, it } from 'vitest'

import genesisJSON from '../../testdata/geth-genesis/eip4844.json'
import { getRpcClient, setupChain } from '../helpers.js'

import type { Chain } from '../../../src/blockchain/chain.js'
import type { VMExecution } from '../../../src/execution/vmexecution.js'
const method = 'eth_blobBaseFee'

const privateKey = hexToBytes('0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8')
const accountAddress = Address.fromPrivateKey(privateKey)
const produceBlockWith4844Tx = async (
execution: VMExecution,
chain: Chain,
blobsCount: number[]
) => {
const kzg = await loadKZG()
// 4844 sample blob
const sampleBlob = getBlobs('hello world')
const commitment = blobsToCommitments(kzg, sampleBlob)
const blobVersionedHash = commitmentsToVersionedHashes(commitment)

const { vm } = execution
const account = await vm.stateManager.getAccount(accountAddress)
let nonce = account?.nonce ?? BIGINT_0
const parentBlock = await chain.getCanonicalHeadBlock()
const vmCopy = await vm.shallowCopy()
// Set block's gas used to max
const blockBuilder = await vmCopy.buildBlock({
parentBlock,
headerData: {
timestamp: parentBlock.header.timestamp + BigInt(1),
},
blockOpts: {
calcDifficultyFromHeader: parentBlock.header,
putBlockIntoBlockchain: false,
},
})
for (let i = 0; i < blobsCount.length; i++) {
const blobVersionedHashes = []
const blobs = []
const kzgCommitments = []
const to = Address.zero()
if (blobsCount[i] > 0) {
for (let blob = 0; blob < blobsCount[i]; blob++) {
blobVersionedHashes.push(...blobVersionedHash)
blobs.push(...sampleBlob)
kzgCommitments.push(...commitment)
}
}
await blockBuilder.addTransaction(
TransactionFactory.fromTxData(
{
type: 3,
gasLimit: 21000,
maxFeePerGas: 0xffffffff,
maxPriorityFeePerGas: BIGINT_256,
nonce,
to,
blobVersionedHashes,
blobs,
kzgCommitments,
maxFeePerBlobGas: BigInt(1000),
},
{ common: vmCopy.common }
).sign(privateKey)
)
nonce++
}

const block = await blockBuilder.build()
await chain.putBlocks([block], true)
await execution.run()
}

describe(method, () => {
it('call', async () => {
const kzg = await loadKZG()
const { server } = await setupChain(genesisJSON, 'post-merge', {
engine: true,
hardfork: Hardfork.Cancun,
customCrypto: {
kzg,
},
})

const rpc = getRpcClient(server)
const res = await rpc.request(method, [])
assert.equal(res.result, '0x1')
})

it('call with more realistic blockchain', async () => {
const kzg = await loadKZG()
const { server, execution, chain } = await setupChain(genesisJSON, 'post-merge', {
engine: true,
hardfork: Hardfork.Cancun,
customCrypto: {
kzg,
},
})

for (let i = 0; i < 10; i++) {
await produceBlockWith4844Tx(execution, chain, [6])
}
const rpc = getRpcClient(server)
const res = await rpc.request(method, [])
assert.equal(res.result, '0x3')
})
})
1 change: 1 addition & 0 deletions packages/client/test/rpc/eth/getTransactionReceipt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe(method, () => {
const res = await rpc.request(method, [bytesToHex(tx.hash())])

assert.equal(res.result.transactionHash, bytesToHex(tx.hash()), 'should return the correct tx')
assert.equal(res.result.status, '0x1', 'transaction result is 1 since succeeded')
})

it('call with unknown tx hash', async () => {
Expand Down
8 changes: 0 additions & 8 deletions packages/evm/src/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Hardfork } from '@ethereumjs/common'
import {
Address,
RIPEMD160_ADDRESS_STRING,
bigIntToHex,
bytesToHex,
bytesToUnprefixedHex,
stripHexPrefix,
Expand Down Expand Up @@ -198,13 +197,6 @@ export class Journal {
const address = new Address(hexToBytes(`0x${addressHex}`))
const account = await this.stateManager.getAccount(address)
if (account === undefined || account.isEmpty()) {
if (this.common.isActivatedEIP(2935)) {
// The history storage address is exempt of state clearing by EIP-158 if the EIP is activated
const addr = bigIntToHex(this.common.param('vm', 'historyStorageAddress')).slice(2)
if (addressHex === addr) {
continue
}
}
await this.deleteAccount(address)
if (this.DEBUG) {
this._debug(`Cleanup touched account address=${address} (>= SpuriousDragon)`)
Expand Down
13 changes: 10 additions & 3 deletions packages/evm/src/opcodes/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1157,9 +1157,16 @@ export const handlers: Map<number, OpHandler> = new Map([
}

const expectedAddress = new Address(setLengthLeft(bigIntToBytes(authority), 20).slice(-20))
const accountNonce = (
(await runState.stateManager.getAccount(expectedAddress)) ?? new Account()
).nonce
const account = (await runState.stateManager.getAccount(expectedAddress)) ?? new Account()

if (account.isContract()) {
// EXTCODESIZE > 0
runState.stack.push(BIGINT_0)
runState.auth = undefined
return
}

const accountNonce = account.nonce

const invokedAddress = setLengthLeft(runState.interpreter._env.address.bytes, 32)
const chainId = setLengthLeft(bigIntToBytes(runState.interpreter.getChainId()), 32)
Expand Down
2 changes: 1 addition & 1 deletion packages/statemanager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"ethereum-cryptography": "^2.1.3",
"js-sdsl": "^4.1.4",
"lru-cache": "10.1.0",
"verkle-cryptography-wasm": "^0.4.1"
"verkle-cryptography-wasm": "^0.4.2"
},
"devDependencies": {
"@ethereumjs/block": "^5.2.0",
Expand Down
Loading

0 comments on commit 667d3e4

Please sign in to comment.