Skip to content

Commit

Permalink
fix reset canonical header and add fix
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Feb 20, 2023
1 parent 8c92367 commit 7edeab8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,11 @@ export class Blockchain implements BlockchainInterface {
canonicalHead > BigInt(0)
? await this.getTotalDifficulty(header.hash(), canonicalHead)
: header.difficulty

const dbOps: DBOp[] = []
await this._rebuildCanonical(header, dbOps)
await this._deleteCanonicalChainReferences(canonicalHead + BigInt(1), hash, dbOps)
const ops = dbOps.concat(this._saveHeadOps())

await this.dbManager.batch(ops)
await this.checkAndTransitionHardForkByNumber(canonicalHead, td, header.timestamp)
})
Expand Down Expand Up @@ -1079,6 +1081,10 @@ export class Blockchain implements BlockchainInterface {
if (this._headBlockHash?.equals(hash) === true) {
this._headBlockHash = headHash
}
// reset stale headBlock to current canonical
if (this._headHeaderHash?.equals(hash) === true) {
this._headHeaderHash = headHash
}

blockNumber++

Expand Down
7 changes: 7 additions & 0 deletions packages/blockchain/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ tape('blockchain test', (t) => {
st.equal(getBlocks!.length, 5)
st.equal(blocks[0].header.number, getBlocks[0].header.number)
st.ok(isConsecutive(getBlocks!), 'blocks should be consecutive')

let canonicalHeader = await blockchain.getCanonicalHeadHeader()
st.equal(canonicalHeader.number, BigInt(24), 'block 24 should be canonical header')
await blockchain.resetCanonicalHead(BigInt(4))
canonicalHeader = await blockchain.getCanonicalHeadHeader()
st.equal(canonicalHeader.number, BigInt(4), 'block 4 should be new canonical header')

st.end()
})

Expand Down

0 comments on commit 7edeab8

Please sign in to comment.