Skip to content

Commit

Permalink
add and use optimistic number to hash key/dbop to avoid conflict with…
Browse files Browse the repository at this point in the history
… number to ahash and rebuild canonical while backfilling
  • Loading branch information
g11tech committed Aug 14, 2024
1 parent c7f6c87 commit ee7b200
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,13 @@ export class Blockchain implements BlockchainInterface {
if (isGenesis) {
if (equalsBytes(this.genesisBlock.hash(), block.hash())) {
// Try to re-put the existing genesis block, accept this
optimistic = false
return
}
throw new Error(
'Cannot put a different genesis block than current blockchain genesis: create a new Blockchain',
)
// genesis block is not optimistic
optimistic = false
}

if (block.common.chainId() !== this.common.chainId()) {
Expand Down Expand Up @@ -410,10 +410,10 @@ export class Blockchain implements BlockchainInterface {
if (optimistic) {
dbOps = dbOps.concat(DBSetBlockOrHeader(item))
dbOps.push(DBSetHashToNumber(blockHash, blockNumber))
dbOps.push(DBOp.set(DBTarget.OptimisticNumberToHash, blockHash, { blockNumber }))
await this.dbManager.batch(dbOps)
} else {
const currentTd = { header: BIGINT_0, block: BIGINT_0 }

// set total difficulty in the current context scope
if (this._headHeaderHash) {
currentTd.header = await this.getTotalDifficulty(this._headHeaderHash)
Expand Down
5 changes: 5 additions & 0 deletions packages/blockchain/src/db/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const TD_SUFFIX = utf8ToBytes('t')
* headerPrefix + number + numSuffix -> hash
*/
const NUM_SUFFIX = utf8ToBytes('n')
const OPTIMISTIC_NUM_SUFFIX = utf8ToBytes('o')

/**
* blockHashPrefix + hash -> number
Expand Down Expand Up @@ -55,6 +56,9 @@ const bodyKey = (n: bigint, hash: Uint8Array) => concatBytes(BODY_PREFIX, bytesB

const numberToHashKey = (n: bigint) => concatBytes(HEADER_PREFIX, bytesBE8(n), NUM_SUFFIX)

const optimisticNumberToHashKey = (n: bigint) =>
concatBytes(HEADER_PREFIX, bytesBE8(n), OPTIMISTIC_NUM_SUFFIX)

const hashToNumberKey = (hash: Uint8Array) => concatBytes(BLOCK_HASH_PREFIX, hash)

/**
Expand All @@ -69,5 +73,6 @@ export {
headerKey,
HEADS_KEY,
numberToHashKey,
optimisticNumberToHashKey,
tdKey,
}
18 changes: 16 additions & 2 deletions packages/blockchain/src/db/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ export class DBManager {
* Fetches a block (header and body) given a block id,
* which can be either its hash or its number.
*/
async getBlock(blockId: Uint8Array | bigint | number): Promise<Block | undefined> {
async getBlock(
blockId: Uint8Array | bigint | number,
optimistic: boolean = false,
): Promise<Block | undefined> {
if (typeof blockId === 'number' && Number.isInteger(blockId)) {
blockId = BigInt(blockId)
}
Expand All @@ -97,7 +100,13 @@ export class DBManager {
number = await this.hashToNumber(blockId)
} else if (typeof blockId === 'bigint') {
number = blockId
hash = await this.numberToHash(blockId)
if (optimistic) {
hash = await this.optimisticNumberToHash(blockId)
}
// hash will be undefined if it no optimistic lookup was done or if that was not successful
if (hash === undefined) {
hash = await this.numberToHash(blockId)
}
} else {
throw new Error('Unknown blockId type')
}
Expand Down Expand Up @@ -190,6 +199,11 @@ export class DBManager {
return value
}

async optimisticNumberToHash(blockNumber: bigint): Promise<Uint8Array | undefined> {
const value = await this.get(DBTarget.OptimisticNumberToHash, { blockNumber })
return value
}

/**
* Fetches a key from the db. If `opts.cache` is specified
* it first tries to load from cache, and on cache miss will
Expand Down
7 changes: 7 additions & 0 deletions packages/blockchain/src/db/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
hashToNumberKey,
headerKey,
numberToHashKey,
optimisticNumberToHashKey,
tdKey,
} from './constants.js'

Expand All @@ -25,6 +26,7 @@ export enum DBTarget {
CliqueSignerStates,
CliqueVotes,
CliqueBlockSigners,
OptimisticNumberToHash,
}

/**
Expand Down Expand Up @@ -88,6 +90,11 @@ export class DBOp {
this.cacheString = 'numberToHash'
break
}
case DBTarget.OptimisticNumberToHash: {
this.baseDBOp.key = optimisticNumberToHashKey(key!.blockNumber!)
this.cacheString = 'optimisticNumberToHash'
break
}
case DBTarget.TotalDifficulty: {
this.baseDBOp.key = tdKey(key!.blockNumber!, key!.blockHash!)
this.cacheString = 'td'
Expand Down

0 comments on commit ee7b200

Please sign in to comment.