Skip to content

Commit

Permalink
blockchain -> PoA: reactivated total difficulty for PoA still being a…
Browse files Browse the repository at this point in the history
… quality indicator and being present as an devp2p ETH status value
  • Loading branch information
holgerd77 committed Feb 8, 2021
1 parent 0ccef76 commit 1de3d5f
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions packages/blockchain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,10 @@ export default class Blockchain implements BlockchainInterface {
*/
private async _putBlockOrHeader(item: Block | BlockHeader) {
await this.runWithLock<void>(async () => {
const block = item instanceof BlockHeader ? new Block(item) : item
const block =
item instanceof BlockHeader
? new Block(item, undefined, undefined, { common: this._common })
: item
const isGenesis = block.isGenesis()

// we cannot overwrite the Genesis block after initializing the Blockchain
Expand Down Expand Up @@ -841,22 +844,41 @@ export default class Blockchain implements BlockchainInterface {
}
}

if (this._common.consensusAlgorithm() === 'ethash') {
// set total difficulty in the current context scope
if (this._headHeaderHash) {
currentTd.header = await this.getTotalDifficulty(this._headHeaderHash)
}
if (this._headBlockHash) {
currentTd.block = await this.getTotalDifficulty(this._headBlockHash)
if (this._common.consensusAlgorithm() === 'clique') {
// validate checkpoint signers towards active signers on epoch transition blocks
if (header.cliqueIsEpochTransition()) {
// note: keep votes on epoch transition blocks in case of reorgs.
// only active (non-stale) votes will counted (if vote.blockNumber >= lastEpochBlockNumber)

const checkpointSigners = header.cliqueEpochTransitionSigners()
const activeSigners = this.cliqueActiveSigners()
for (const [i, cSigner] of checkpointSigners.entries()) {
if (!activeSigners[i] || !activeSigners[i].equals(cSigner)) {
throw new Error(
`checkpoint signer not found in active signers list at index ${i}: ${cSigner.toString()}`
)
}
}
}
}

// calculate the total difficulty of the new block
const parentTd = await this.getTotalDifficulty(header.parentHash, blockNumber.subn(1))
td.iadd(parentTd)
// set total difficulty in the current context scope
if (this._headHeaderHash) {
currentTd.header = await this.getTotalDifficulty(this._headHeaderHash)
}
if (this._headBlockHash) {
currentTd.block = await this.getTotalDifficulty(this._headBlockHash)
}

// save total difficulty to the database
dbOps = dbOps.concat(DBSetTD(td, blockNumber, blockHash))
// calculate the total difficulty of the new block
let parentTd = new BN(0)
if (!block.isGenesis()) {
parentTd = await this.getTotalDifficulty(header.parentHash, blockNumber.subn(1))
}
td.iadd(parentTd)

// save total difficulty to the database
dbOps = dbOps.concat(DBSetTD(td, blockNumber, blockHash))

// save header/block to the database
dbOps = dbOps.concat(DBSetBlockOrHeader(block))
Expand All @@ -879,24 +901,9 @@ export default class Blockchain implements BlockchainInterface {

// Clique: update signer votes and state
if (this._common.consensusAlgorithm() === 'clique') {
if (header.cliqueIsEpochTransition()) {
// note: keep votes on epoch transition blocks in case of reorgs.
// only active (non-stale) votes will counted (if vote.blockNumber >= lastEpochBlockNumber)

// validate checkpoint signers towards active signers
const checkpointSigners = header.cliqueEpochTransitionSigners()
const activeSigners = this.cliqueActiveSigners()
for (const [i, cSigner] of checkpointSigners.entries()) {
if (!activeSigners[i] || !activeSigners[i].equals(cSigner)) {
throw new Error(
`checkpoint signer not found in active signers list at index ${i}: ${cSigner.toString()}`
)
}
}
} else {
if (!header.cliqueIsEpochTransition()) {
await this.cliqueUpdateVotes(header)
}

await this.cliqueUpdateLatestBlockSigners(header)
}

Expand Down

0 comments on commit 1de3d5f

Please sign in to comment.