Skip to content

Commit

Permalink
Merge branch 'block-consensus-methods-refactoring' of github.com:ethe…
Browse files Browse the repository at this point in the history
…reumjs/ethereumjs-monorepo into block-consensus-methods-refactoring
  • Loading branch information
scorbajio committed Aug 12, 2024
2 parents a0bb705 + eb89c3c commit 9a02915
Show file tree
Hide file tree
Showing 42 changed files with 742 additions and 818 deletions.
16 changes: 8 additions & 8 deletions packages/block/src/consensus/clique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const CLIQUE_EXTRA_VANITY = 32
export const CLIQUE_EXTRA_SEAL = 65

// This function is not exported in the index file to keep it internal
export function _requireClique(header: BlockHeader, name: string) {
export function requireClique(header: BlockHeader, name: string) {
if (header.common.consensusAlgorithm() !== ConsensusAlgorithm.Clique) {
const msg = header['_errorMsg'](
`BlockHeader.${name}() call only supported for clique PoA networks`,
Expand All @@ -33,7 +33,7 @@ export function _requireClique(header: BlockHeader, name: string) {
* PoA clique signature hash without the seal.
*/
export function cliqueSigHash(header: BlockHeader) {
_requireClique(header, 'cliqueSigHash')
requireClique(header, 'cliqueSigHash')
const raw = header.raw()
raw[12] = header.extraData.subarray(0, header.extraData.length - CLIQUE_EXTRA_SEAL)
return header['keccakFunction'](RLP.encode(raw))
Expand All @@ -44,7 +44,7 @@ export function cliqueSigHash(header: BlockHeader) {
* header (only clique PoA, throws otherwise)
*/
export function cliqueIsEpochTransition(header: BlockHeader): boolean {
_requireClique(header, 'cliqueIsEpochTransition')
requireClique(header, 'cliqueIsEpochTransition')
const epoch = BigInt((header.common.consensusConfig() as CliqueConfig).epoch)
// Epoch transition block if the block number has no
// remainder on the division by the epoch length
Expand All @@ -56,7 +56,7 @@ export function cliqueIsEpochTransition(header: BlockHeader): boolean {
* (only clique PoA, throws otherwise)
*/
export function cliqueExtraVanity(header: BlockHeader): Uint8Array {
_requireClique(header, 'cliqueExtraVanity')
requireClique(header, 'cliqueExtraVanity')
return header.extraData.subarray(0, CLIQUE_EXTRA_VANITY)
}

Expand All @@ -65,7 +65,7 @@ export function cliqueExtraVanity(header: BlockHeader): Uint8Array {
* (only clique PoA, throws otherwise)
*/
export function cliqueExtraSeal(header: BlockHeader): Uint8Array {
_requireClique(header, 'cliqueExtraSeal')
requireClique(header, 'cliqueExtraSeal')
return header.extraData.subarray(-CLIQUE_EXTRA_SEAL)
}

Expand All @@ -78,7 +78,7 @@ export function cliqueExtraSeal(header: BlockHeader): Uint8Array {
* in conjunction with {@link BlockHeader.cliqueIsEpochTransition}
*/
export function cliqueEpochTransitionSigners(header: BlockHeader): Address[] {
_requireClique(header, 'cliqueEpochTransitionSigners')
requireClique(header, 'cliqueEpochTransitionSigners')
if (!cliqueIsEpochTransition(header)) {
const msg = header['_errorMsg']('Signers are only included in epoch transition blocks (clique)')
throw new Error(msg)
Expand All @@ -100,7 +100,7 @@ export function cliqueEpochTransitionSigners(header: BlockHeader): Address[] {
* Returns the signer address
*/
export function cliqueSigner(header: BlockHeader): Address {
_requireClique(header, 'cliqueSigner')
requireClique(header, 'cliqueSigner')
const extraSeal = cliqueExtraSeal(header)
// Reasonable default for default blocks
if (extraSeal.length === 0 || equalsBytes(extraSeal, new Uint8Array(65))) {
Expand All @@ -120,7 +120,7 @@ export function cliqueSigner(header: BlockHeader): Address {
* Method throws if signature is invalid
*/
export function cliqueVerifySignature(header: BlockHeader, signerList: Address[]): boolean {
_requireClique(header, 'cliqueVerifySignature')
requireClique(header, 'cliqueVerifySignature')
const signerAddress = cliqueSigner(header)
const signerFound = signerList.find((signer) => {
return signer.equals(signerAddress)
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
isHexString,
} from '@ethereumjs/util'

import { _requireClique } from './consensus/clique.js'
import { requireClique } from './consensus/clique.js'
import { createBlockFromRpc } from './from-rpc.js'
import {
genRequestsTrieRoot,
Expand Down Expand Up @@ -544,7 +544,7 @@ export function createSealedCliqueBlockExtraData(
;(header.extraData as any) = concatBytes(header.extraData, new Uint8Array(remainingLength))
}

_requireClique(header, 'cliqueSealBlock')
requireClique(header, 'cliqueSealBlock')

const ecSignFunction = header.common.customCrypto?.ecsign ?? ecsign
const signature = ecSignFunction(cliqueSigHash(header), cliqueSigner)
Expand Down
2 changes: 1 addition & 1 deletion packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { keccak256 } from 'ethereum-cryptography/keccak.js'
import {
CLIQUE_EXTRA_SEAL,
CLIQUE_EXTRA_VANITY,
_requireClique,
requireClique,
cliqueIsEpochTransition,
} from './consensus/clique.js'
import { createSealedCliqueBlock } from './constructors.js'
Expand Down
2 changes: 1 addition & 1 deletion packages/blockchain/src/consensus/ethash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class EthashConsensus implements Consensus {
public async genesisInit(): Promise<void> {}
public async setup({ blockchain }: ConsensusOptions): Promise<void> {
this.blockchain = blockchain
this._ethash.cacheDB = this.blockchain!.db as any
this._ethash.cacheDB = this.blockchain.db
}
public async newBlock(): Promise<void> {}
}
54 changes: 27 additions & 27 deletions packages/client/src/execution/vmexecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import {
DBSetHashToNumber,
DBSetTD,
} from '@ethereumjs/blockchain'
import { CacheType, ConsensusType, Hardfork } from '@ethereumjs/common'
import { ConsensusType, Hardfork } from '@ethereumjs/common'
import { MCLBLS, RustBN254 } from '@ethereumjs/evm'
import { getGenesis } from '@ethereumjs/genesis'
import { DefaultStateManager, StatelessVerkleStateManager } from '@ethereumjs/statemanager'
import {
CacheType,
Caches,
DefaultStateManager,
StatelessVerkleStateManager,
} from '@ethereumjs/statemanager'
import { createTrie } from '@ethereumjs/trie'
import {
BIGINT_0,
Expand Down Expand Up @@ -35,7 +40,6 @@ import { ReceiptsManager } from './receipt.js'

import type { ExecutionOptions } from './execution.js'
import type { Block } from '@ethereumjs/block'
import type { Trie } from '@ethereumjs/trie'
import type { PrefixedHexString } from '@ethereumjs/util'
import type { RunBlockOpts, TxReceipt } from '@ethereumjs/vm'

Expand Down Expand Up @@ -160,21 +164,20 @@ export class VMExecution extends Execution {
const stateManager = new DefaultStateManager({
trie,
prefixStorageTrieKeys: this.config.prefixStorageTrieKeys,
accountCacheOpts: {
deactivate: false,
type: CacheType.LRU,
size: this.config.accountCache,
},
storageCacheOpts: {
deactivate: false,
type: CacheType.LRU,
size: this.config.storageCache,
},
codeCacheOpts: {
deactivate: false,
type: CacheType.LRU,
size: this.config.codeCache,
},
caches: new Caches({
account: {
type: CacheType.LRU,
size: this.config.accountCache,
},
storage: {
type: CacheType.LRU,
size: this.config.storageCache,
},
code: {
type: CacheType.LRU,
size: this.config.codeCache,
},
}),
common: this.config.chainCommon,
})

Expand Down Expand Up @@ -1059,25 +1062,22 @@ export class VMExecution extends Execution {

stats() {
if (this._statsVM instanceof DefaultStateManager) {
const sm = this._statsVM.stateManager as any
const disactivatedStats = { size: 0, reads: 0, hits: 0, writes: 0 }
const sm = this._statsVM.stateManager as DefaultStateManager
const deactivatedStats = { size: 0, reads: 0, hits: 0, writes: 0 }
let stats
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
stats = !sm._accountCacheSettings.deactivate ? sm._accountCache.stats() : disactivatedStats
stats = sm['_caches']?.account?.stats() ?? deactivatedStats
this.config.logger.info(
`Account cache stats size=${stats.size} reads=${stats.reads} hits=${stats.hits} writes=${stats.writes}`,
)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
stats = !sm._storageCacheSettings.deactivate ? sm._storageCache.stats() : disactivatedStats
stats = sm['_caches']?.storage?.stats() ?? deactivatedStats
this.config.logger.info(
`Storage cache stats size=${stats.size} reads=${stats.reads} hits=${stats.hits} writes=${stats.writes}`,
)
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
stats = !sm._codeCacheSettings.deactivate ? sm._codeCache.stats() : disactivatedStats
stats = sm['_caches']?.code?.stats() ?? deactivatedStats
this.config.logger.info(
`Code cache stats size=${stats.size} reads=${stats.reads} hits=${stats.hits} writes=${stats.writes}`,
)
const tStats = (sm._trie as Trie).database().stats()
const tStats = sm['_trie'].database().stats()
this.config.logger.info(
`Trie cache stats size=${tStats.size} reads=${tStats.cache.reads} hits=${tStats.cache.hits} ` +
`writes=${tStats.cache.writes} readsDB=${tStats.db.reads} hitsDB=${tStats.db.hits} writesDB=${tStats.db.writes}`,
Expand Down
3 changes: 1 addition & 2 deletions packages/client/src/rpc/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ export class Admin {
return peers?.map((peer) => {
return {
id: peer.id,
// Typescript complains about the typing of `_hello` if we make rlpxPeer possibly null
name: (peer.rlpxPeer as any)['_hello'].clientId ?? null,
name: peer.rlpxPeer?.['_hello']?.clientId ?? null,
protocols: {
eth: {
head: peer.eth?.updatedBestHeader
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/rpc/modules/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import type { EthProtocol } from '../../net/protocol/index.js'
import type { FullEthereumService, Service } from '../../service/index.js'
import type { RpcTx } from '../types.js'
import type { Block, JsonRpcBlock } from '@ethereumjs/block'
import type { Proof } from '@ethereumjs/common'
import type { Log } from '@ethereumjs/evm'
import type { Proof } from '@ethereumjs/statemanager'
import type {
FeeMarketEIP1559Transaction,
LegacyTransaction,
Expand Down
71 changes: 0 additions & 71 deletions packages/common/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,6 @@ export type Proof = {
storageProof: StorageProof[]
}

/*
* Access List types
*/

export type AccessListItem = {
address: PrefixedHexString
storageKeys: PrefixedHexString[]
}

/*
* An Access List as a tuple of [address: Uint8Array, storageKeys: Uint8Array[]]
*/
export type AccessListBytesItem = [Uint8Array, Uint8Array[]]
export type AccessListBytes = AccessListBytesItem[]
export type AccessList = AccessListItem[]

/**
* Authorization list types
*/
export type AuthorizationListItem = {
chainId: PrefixedHexString
address: PrefixedHexString
nonce: PrefixedHexString[]
yParity: PrefixedHexString
r: PrefixedHexString
s: PrefixedHexString
}

// Tuple of [chain_id, address, [nonce], y_parity, r, s]
export type AuthorizationListBytesItem = [
Uint8Array,
Uint8Array,
Uint8Array[],
Uint8Array,
Uint8Array,
Uint8Array,
]
export type AuthorizationListBytes = AuthorizationListBytesItem[]
export type AuthorizationList = AuthorizationListItem[]

/**
* Verkle related
*
Expand Down Expand Up @@ -209,35 +169,4 @@ export interface StateManagerInterface {
*/
clearCaches(): void
shallowCopy(downlevelCaches?: boolean): StateManagerInterface

/*
* Cache properties
*/
_accountCache?: Cache
_storageCache?: Cache
_codeCache?: Cache

_accountCacheSettings?: CacheSettings
_storageCacheSettings?: CacheSettings
_codeCacheSettings?: CacheSettings
}

/**
* Cache related
*/
export enum CacheType {
LRU = 'lru',
ORDERED_MAP = 'ordered_map',
}

export type CacheSettings = {
deactivate: boolean
type: CacheType
size: number
}

interface Cache {
checkpoint(): void
commit(): void
revert(): void
}
2 changes: 1 addition & 1 deletion packages/evm/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ export class Interpreter {
getBlockCoinbase(): bigint {
let coinbase: Address
if (this.common.consensusAlgorithm() === ConsensusAlgorithm.Clique) {
coinbase = this._evm['_optsCached'].cliqueSigner!(this._env.block.header as any)
coinbase = this._evm['_optsCached'].cliqueSigner!(this._env.block.header)
} else {
coinbase = this._env.block.header.coinbase
}
Expand Down
6 changes: 4 additions & 2 deletions packages/evm/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,11 @@ export interface EVMOpts {
profiler?: EVMProfilerOpts

/**
* Must be present if consensus type is clique/poa, else error will be thrown
* When running the EVM with PoA consensus, the `cliqueSigner` function from the `@ethereumjs/block` class
* must be provided along with a `BlockHeader` so that the coinbase can be correctly retrieved when the
* `Interpreter.getBlockCoinbase` method is called.
*/
cliqueSigner?: (header: any) => Address
cliqueSigner?: (header: Block['header']) => Address
}

/**
Expand Down
6 changes: 3 additions & 3 deletions packages/statemanager/src/cache/account.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CacheType } from '@ethereumjs/common'
import { bytesToUnprefixedHex } from '@ethereumjs/util'
import { OrderedMap } from '@js-sdsl/ordered-map'
import debugDefault from 'debug'
import { LRUCache } from 'lru-cache'

import { Cache } from './cache.js'
import { CacheType } from './types.js'

import type { CacheOpts } from './types.js'
import type { Account, Address } from '@ethereumjs/util'
Expand Down Expand Up @@ -68,14 +68,14 @@ export class AccountCache extends Cache {
put(
address: Address,
account: Account | undefined,
couldBeParitalAccount: boolean = false,
couldBePartialAccount: boolean = false,
): void {
const addressHex = bytesToUnprefixedHex(address.bytes)
this._saveCachePreState(addressHex)
const elem = {
accountRLP:
account !== undefined
? couldBeParitalAccount
? couldBePartialAccount
? account.serializeWithPartialInfo()
: account.serialize()
: undefined,
Expand Down
Loading

0 comments on commit 9a02915

Please sign in to comment.