Skip to content

Commit

Permalink
Merge branch 'master' into client/migrate-test-files
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Sep 11, 2024
2 parents ef4fee1 + 0041c53 commit eed8d1a
Show file tree
Hide file tree
Showing 12 changed files with 391 additions and 313 deletions.
41 changes: 26 additions & 15 deletions packages/blockchain/src/consensus/clique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,13 @@ export class CliqueConsensus implements Consensus {
*/
public _cliqueLatestBlockSigners: CliqueLatestBlockSigners = []

DEBUG: boolean // Guard for debug logs
constructor() {
// Skip DEBUG calls unless 'ethjs' included in environmental DEBUG variables
// Additional window check is to prevent vite browser bundling (and potentially other) to break
this.DEBUG =
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false

this.algorithm = ConsensusAlgorithm.Clique
}

Expand Down Expand Up @@ -220,7 +226,7 @@ export class CliqueConsensus implements Consensus {
cliqueEpochTransitionSigners(genesisBlock.header),
]
await this.cliqueUpdateSignerStates(genesisSignerState)
debug(`[Block 0] Genesis block -> update signer states`)
this.DEBUG && debug(`[Block 0] Genesis block -> update signer states`)
await this.cliqueUpdateVotes()
}

Expand Down Expand Up @@ -270,7 +276,7 @@ export class CliqueConsensus implements Consensus {
let i = 0
try {
for (const signer of this.cliqueActiveSigners(signerState[0])) {
debug(`Clique signer [${i}]: ${signer} (block: ${signerState[0]})`)
this.DEBUG && debug(`Clique signer [${i}]: ${signer} (block: ${signerState[0]})`)
i++
}
// eslint-disable-next-line no-empty
Expand Down Expand Up @@ -344,7 +350,8 @@ export class CliqueConsensus implements Consensus {
this._cliqueLatestVotes = this._cliqueLatestVotes.filter(
(vote) => !vote[1][1].equals(beneficiary),
)
debug(`[Block ${header.number}] Clique majority consensus (AUTH ${beneficiary})`)
this.DEBUG &&
debug(`[Block ${header.number}] Clique majority consensus (AUTH ${beneficiary})`)
}
// DROP vote
votes = this._cliqueLatestVotes.filter((vote) => {
Expand Down Expand Up @@ -378,27 +385,31 @@ export class CliqueConsensus implements Consensus {
// Discard votes from removed signer and for removed signer
(vote) => !vote[1][0].equals(beneficiary) && !vote[1][1].equals(beneficiary),
)
debug(`[Block ${header.number}] Clique majority consensus (DROP ${beneficiary})`)
this.DEBUG &&
debug(`[Block ${header.number}] Clique majority consensus (DROP ${beneficiary})`)
}
if (round === 1) {
// Always add the latest vote to the history no matter if already voted
// the same vote or not
this._cliqueLatestVotes.push(latestVote)
debug(
`[Block ${header.number}] New clique vote: ${signer} -> ${beneficiary} ${
equalsBytes(nonce, CLIQUE_NONCE_AUTH) ? 'AUTH' : 'DROP'
}`,
)
this.DEBUG &&
debug(
`[Block ${header.number}] New clique vote: ${signer} -> ${beneficiary} ${
equalsBytes(nonce, CLIQUE_NONCE_AUTH) ? 'AUTH' : 'DROP'
}`,
)
}
if (consensus) {
if (round === 1) {
debug(
`[Block ${header.number}] Clique majority consensus on existing votes -> update signer states`,
)
this.DEBUG &&
debug(
`[Block ${header.number}] Clique majority consensus on existing votes -> update signer states`,
)
} else {
debug(
`[Block ${header.number}] Clique majority consensus on new vote -> update signer states`,
)
this.DEBUG &&
debug(
`[Block ${header.number}] Clique majority consensus on new vote -> update signer states`,
)
}
const newSignerState: CliqueSignerState = [header.number, activeSigners]
await this.cliqueUpdateSignerStates(newSignerState)
Expand Down
47 changes: 25 additions & 22 deletions packages/client/src/sync/fetcher/accountfetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
const origin = this.getOrigin(syncRange)
const limit = this.getLimit(syncRange)

this.debug(
`Account fetcher instantiated root=${short(this.root)} origin=${short(origin)} limit=${short(
limit,
)} destroyWhenDone=${this.destroyWhenDone}`,
)
this.DEBUG &&
this.debug(
`Account fetcher instantiated root=${short(this.root)} origin=${short(origin)} limit=${short(
limit,
)} destroyWhenDone=${this.destroyWhenDone}`,
)
}

async blockingFetch(): Promise<boolean> {
Expand Down Expand Up @@ -302,11 +303,12 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
origin: Uint8Array,
{ accounts, proof }: { accounts: AccountData[]; proof: Uint8Array[] },
): Promise<boolean> {
this.debug(
`verifyRangeProof accounts:${accounts.length} first=${bytesToHex(
accounts[0].hash,
)} last=${short(accounts[accounts.length - 1].hash)}`,
)
this.DEBUG &&
this.debug(
`verifyRangeProof accounts:${accounts.length} first=${bytesToHex(
accounts[0].hash,
)} last=${short(accounts[accounts.length - 1].hash)}`,
)

for (let i = 0; i < accounts.length - 1; i++) {
// ensure the range is monotonically increasing
Expand Down Expand Up @@ -382,7 +384,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>

if (this.highestKnownHash && compareBytes(limit, this.highestKnownHash) < 0) {
// skip this job and don't rerequest it if it's limit is lower than the highest known key hash
this.debug(`skipping request with limit lower than highest known hash`)
this.DEBUG && this.debug(`skipping request with limit lower than highest known hash`)
return Object.assign([], [{ skipped: true }], { completed: true })
}

Expand Down Expand Up @@ -415,12 +417,12 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
// if proof is false, reject corrupt peer
if (isMissingRightRange !== false) return undefined
} catch (e) {
this.debug(e)
this.DEBUG && this.debug(e)
// if proof is false, reject corrupt peer
return undefined
}

this.debug(`Data for last range has been received`)
this.DEBUG && this.debug(`Data for last range has been received`)
// response contains empty object so that task can be terminated in store phase and not reenqueued
return Object.assign([], [Object.create(null)], { completed: true })
}
Expand All @@ -436,11 +438,12 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
// Check if there is any pending data to be synced to the right
let completed: boolean
if (isMissingRightRange && this.isMissingRightRange(limit, rangeResult)) {
this.debug(
`Peer ${peerInfo} returned missing right range account=${bytesToHex(
rangeResult.accounts[rangeResult.accounts.length - 1].hash,
)} limit=${bytesToHex(limit)}`,
)
this.DEBUG &&
this.debug(
`Peer ${peerInfo} returned missing right range account=${bytesToHex(
rangeResult.accounts[rangeResult.accounts.length - 1].hash,
)} limit=${bytesToHex(limit)}`,
)
completed = false
} else {
completed = true
Expand Down Expand Up @@ -487,15 +490,15 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
* @param result fetch result
*/
async store(result: AccountData[]): Promise<void> {
this.debug(`Stored ${result.length} accounts in account trie`)
this.DEBUG && this.debug(`Stored ${result.length} accounts in account trie`)

if (JSON.stringify(result[0]) === JSON.stringify({ skipped: true })) {
// return without storing to skip this task
return
}
if (JSON.stringify(result[0]) === JSON.stringify(Object.create(null))) {
// TODO fails to handle case where there is a proof of non existence and returned accounts for last requested range
this.debug('Final range received with no elements remaining to the right')
this.DEBUG && this.debug('Final range received with no elements remaining to the right')

await this.accountTrie.persistRoot()
this.snapFetchersCompleted(AccountFetcher, this.accountTrie.root())
Expand Down Expand Up @@ -579,7 +582,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
debugStr += ` limit=${short(
setLengthLeft(bigIntToBytes(startedWith + pushedCount - BIGINT_1), 32),
)}`
this.debug(`Created new tasks num=${tasks.length} ${debugStr}`)
this.DEBUG && this.debug(`Created new tasks num=${tasks.length} ${debugStr}`)
return tasks
}

Expand All @@ -602,7 +605,7 @@ export class AccountFetcher extends Fetcher<JobTask, AccountData[], AccountData>
const origin = this.getOrigin(pendingRange)
const limit = this.getLimit(pendingRange)

this.debug(`Fetcher pending with origin=${short(origin)} limit=${short(limit)}`)
this.DEBUG && this.debug(`Fetcher pending with origin=${short(origin)} limit=${short(limit)}`)
const tasks = this.tasks()
for (const task of tasks) {
this.enqueueTask(task)
Expand Down
50 changes: 28 additions & 22 deletions packages/client/src/sync/fetcher/blockfetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
})
if (!Array.isArray(headersResult) || headersResult[1].length === 0) {
// Catch occasional null or empty responses
this.debug(`Peer ${peerInfo} returned no headers for blocks=${blocksRange}`)
this.DEBUG && this.debug(`Peer ${peerInfo} returned no headers for blocks=${blocksRange}`)
return []
}
const headers = headersResult[1]
Expand All @@ -64,13 +64,14 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
bodiesResult[1].length === 0
) {
// Catch occasional null or empty responses
this.debug(`Peer ${peerInfo} returned no bodies for blocks=${blocksRange}`)
this.DEBUG && this.debug(`Peer ${peerInfo} returned no bodies for blocks=${blocksRange}`)
return []
}
const bodies = bodiesResult[1]
this.debug(
`Requested blocks=${blocksRange} from ${peerInfo} (received: ${headers.length} headers / ${bodies.length} bodies)`,
)
this.DEBUG &&
this.debug(
`Requested blocks=${blocksRange} from ${peerInfo} (received: ${headers.length} headers / ${bodies.length} bodies)`,
)
const blocks: Block[] = []
for (const [i, [txsData, unclesData, withdrawalsData]] of bodies.entries()) {
const header = headers[i]
Expand All @@ -81,9 +82,10 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
!equalsBytes(header.withdrawalsRoot, KECCAK256_RLP) &&
(withdrawalsData?.length ?? 0) === 0)
) {
this.debug(
`Requested block=${headers[i].number}} from peer ${peerInfo} missing non-empty txs=${txsData.length} or uncles=${unclesData.length} or withdrawals=${withdrawalsData?.length}`,
)
this.DEBUG &&
this.debug(
`Requested block=${headers[i].number}} from peer ${peerInfo} missing non-empty txs=${txsData.length} or uncles=${unclesData.length} or withdrawals=${withdrawalsData?.length}`,
)
return []
}
const values: BlockBytes = [headers[i].raw(), txsData, unclesData]
Expand All @@ -99,9 +101,10 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
await block.validateData(false, false)
blocks.push(block)
}
this.debug(
`Returning blocks=${blocksRange} from ${peerInfo} (received: ${headers.length} headers / ${bodies.length} bodies)`,
)
this.DEBUG &&
this.debug(
`Returning blocks=${blocksRange} from ${peerInfo} (received: ${headers.length} headers / ${bodies.length} bodies)`,
)
return blocks
}

Expand All @@ -120,7 +123,8 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
} else if (result.length > 0 && result.length < job.task.count) {
// Save partial result to re-request missing items.
job.partialResult = result
this.debug(`Partial result received=${result.length} expected=${job.task.count}`)
this.DEBUG &&
this.debug(`Partial result received=${result.length} expected=${job.task.count}`)
}
}
return
Expand All @@ -133,18 +137,20 @@ export class BlockFetcher extends BlockFetcherBase<Block[], Block> {
async store(blocks: Block[]) {
try {
const num = await this.chain.putBlocks(blocks)
this.debug(
`Fetcher results stored in blockchain (blocks num=${blocks.length} first=${
blocks[0]?.header.number
} last=${blocks[blocks.length - 1]?.header.number})`,
)
this.DEBUG &&
this.debug(
`Fetcher results stored in blockchain (blocks num=${blocks.length} first=${
blocks[0]?.header.number
} last=${blocks[blocks.length - 1]?.header.number})`,
)
this.config.events.emit(Event.SYNC_FETCHED_BLOCKS, blocks.slice(0, num))
} catch (e: any) {
this.debug(
`Error storing fetcher results in blockchain (blocks num=${blocks.length} first=${
blocks[0]?.header.number
} last=${blocks[blocks.length - 1]?.header.number}): ${e}`,
)
this.DEBUG &&
this.debug(
`Error storing fetcher results in blockchain (blocks num=${blocks.length} first=${
blocks[0]?.header.number
} last=${blocks[blocks.length - 1]?.header.number}): ${e}`,
)
throw e
}
}
Expand Down
36 changes: 22 additions & 14 deletions packages/client/src/sync/fetcher/blockfetcherbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,25 @@ export abstract class BlockFetcherBase<JobResult, StorageItem> extends Fetcher<
count: bigint

protected reverse: boolean
protected DEBUG: boolean

/**
* Create new block fetcher
*/
constructor(options: BlockFetcherOptions) {
super(options)

this.DEBUG =
typeof window === 'undefined' ? (process?.env?.DEBUG?.includes('ethjs') ?? false) : false

this.chain = options.chain
this.first = options.first
this.count = options.count
this.reverse = options.reverse ?? false
this.debug(
`Block fetcher instantiated interval=${this.interval} first=${this.first} count=${this.count} reverse=${this.reverse} destroyWhenDone=${this.destroyWhenDone}`,
)
this.DEBUG &&
this.debug(
`Block fetcher instantiated interval=${this.interval} first=${this.first} count=${this.count} reverse=${this.reverse} destroyWhenDone=${this.destroyWhenDone}`,
)
}

/**
Expand Down Expand Up @@ -92,7 +97,7 @@ export abstract class BlockFetcherBase<JobResult, StorageItem> extends Fetcher<
}

debugStr += ` count=${pushedCount} reverse=${this.reverse}`
this.debug(`Created new tasks num=${tasks.length} ${debugStr}`)
this.DEBUG && this.debug(`Created new tasks num=${tasks.length} ${debugStr}`)
return tasks
}

Expand All @@ -104,18 +109,20 @@ export abstract class BlockFetcherBase<JobResult, StorageItem> extends Fetcher<
this.count > BIGINT_0 &&
this.processed - this.finished < this.config.maxFetcherRequests
) {
this.debug(
`Fetcher pending with first=${this.first} count=${this.count} reverse=${this.reverse}`,
)
this.DEBUG &&
this.debug(
`Fetcher pending with first=${this.first} count=${this.count} reverse=${this.reverse}`,
)
const tasks = this.tasks(this.first, this.count)
for (const task of tasks) {
this.enqueueTask(task)
}
this.debug(`Enqueued num=${tasks.length} tasks`)
this.DEBUG && this.debug(`Enqueued num=${tasks.length} tasks`)
} else {
this.debug(
`No new tasks enqueued in=${this.in.length} count=${this.count} processed=${this.processed} finished=${this.finished}`,
)
this.DEBUG &&
this.debug(
`No new tasks enqueued in=${this.in.length} count=${this.count} processed=${this.processed} finished=${this.finished}`,
)
}
}

Expand Down Expand Up @@ -198,9 +205,10 @@ export abstract class BlockFetcherBase<JobResult, StorageItem> extends Fetcher<
)
}
}
this.debug(
`Enqueued tasks by number list num=${numberList.length} min=${min} bulkRequest=${bulkRequest} ${updateHeightStr}`,
)
this.DEBUG &&
this.debug(
`Enqueued tasks by number list num=${numberList.length} min=${min} bulkRequest=${bulkRequest} ${updateHeightStr}`,
)
if (this.in.length === 0) {
this.nextTasks()
}
Expand Down
Loading

0 comments on commit eed8d1a

Please sign in to comment.