Skip to content

Commit a69d63f

Browse files
committed
add and use optimistic number to hash key/dbop to avoid conflict with number to ahash and rebuild canonical while backfilling
1 parent 3b24f64 commit a69d63f

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

packages/blockchain/src/blockchain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,13 @@ export class Blockchain implements BlockchainInterface {
362362
if (isGenesis) {
363363
if (equalsBytes(this.genesisBlock.hash(), block.hash())) {
364364
// Try to re-put the existing genesis block, accept this
365+
optimistic = false
365366
return
366367
}
367368
throw new Error(
368369
'Cannot put a different genesis block than current blockchain genesis: create a new Blockchain',
369370
)
370371
// genesis block is not optimistic
371-
optimistic = false
372372
}
373373

374374
if (block.common.chainId() !== this.common.chainId()) {
@@ -410,10 +410,10 @@ export class Blockchain implements BlockchainInterface {
410410
if (optimistic) {
411411
dbOps = dbOps.concat(DBSetBlockOrHeader(item))
412412
dbOps.push(DBSetHashToNumber(blockHash, blockNumber))
413+
dbOps.push(DBOp.set(DBTarget.OptimisticNumberToHash, blockHash, { blockNumber }))
413414
await this.dbManager.batch(dbOps)
414415
} else {
415416
const currentTd = { header: BIGINT_0, block: BIGINT_0 }
416-
417417
// set total difficulty in the current context scope
418418
if (this._headHeaderHash) {
419419
currentTd.header = await this.getTotalDifficulty(this._headHeaderHash)

packages/blockchain/src/db/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const TD_SUFFIX = utf8ToBytes('t')
2828
* headerPrefix + number + numSuffix -> hash
2929
*/
3030
const NUM_SUFFIX = utf8ToBytes('n')
31+
const OPTIMISTIC_NUM_SUFFIX = utf8ToBytes('o')
3132

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

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

59+
const optimisticNumberToHashKey = (n: bigint) =>
60+
concatBytes(HEADER_PREFIX, bytesBE8(n), OPTIMISTIC_NUM_SUFFIX)
61+
5862
const hashToNumberKey = (hash: Uint8Array) => concatBytes(BLOCK_HASH_PREFIX, hash)
5963

6064
/**
@@ -69,5 +73,6 @@ export {
6973
headerKey,
7074
HEADS_KEY,
7175
numberToHashKey,
76+
optimisticNumberToHashKey,
7277
tdKey,
7378
}

packages/blockchain/src/db/manager.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ export class DBManager {
8484
* Fetches a block (header and body) given a block id,
8585
* which can be either its hash or its number.
8686
*/
87-
async getBlock(blockId: Uint8Array | bigint | number): Promise<Block | undefined> {
87+
async getBlock(
88+
blockId: Uint8Array | bigint | number,
89+
optimistic: boolean = false,
90+
): Promise<Block | undefined> {
8891
if (typeof blockId === 'number' && Number.isInteger(blockId)) {
8992
blockId = BigInt(blockId)
9093
}
@@ -97,7 +100,13 @@ export class DBManager {
97100
number = await this.hashToNumber(blockId)
98101
} else if (typeof blockId === 'bigint') {
99102
number = blockId
100-
hash = await this.numberToHash(blockId)
103+
if (optimistic) {
104+
hash = await this.optimisticNumberToHash(blockId)
105+
}
106+
// hash will be undefined if it no optimistic lookup was done or if that was not successful
107+
if (hash === undefined) {
108+
hash = await this.numberToHash(blockId)
109+
}
101110
} else {
102111
throw new Error('Unknown blockId type')
103112
}
@@ -190,6 +199,11 @@ export class DBManager {
190199
return value
191200
}
192201

202+
async optimisticNumberToHash(blockNumber: bigint): Promise<Uint8Array | undefined> {
203+
const value = await this.get(DBTarget.OptimisticNumberToHash, { blockNumber })
204+
return value
205+
}
206+
193207
/**
194208
* Fetches a key from the db. If `opts.cache` is specified
195209
* it first tries to load from cache, and on cache miss will

packages/blockchain/src/db/operation.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
hashToNumberKey,
99
headerKey,
1010
numberToHashKey,
11+
optimisticNumberToHashKey,
1112
tdKey,
1213
} from './constants.js'
1314

@@ -25,6 +26,7 @@ export enum DBTarget {
2526
CliqueSignerStates,
2627
CliqueVotes,
2728
CliqueBlockSigners,
29+
OptimisticNumberToHash,
2830
}
2931

3032
/**
@@ -88,6 +90,11 @@ export class DBOp {
8890
this.cacheString = 'numberToHash'
8991
break
9092
}
93+
case DBTarget.OptimisticNumberToHash: {
94+
this.baseDBOp.key = optimisticNumberToHashKey(key!.blockNumber!)
95+
this.cacheString = 'optimisticNumberToHash'
96+
break
97+
}
9198
case DBTarget.TotalDifficulty: {
9299
this.baseDBOp.key = tdKey(key!.blockNumber!, key!.blockHash!)
93100
this.cacheString = 'td'

0 commit comments

Comments
 (0)