Skip to content

Commit bbb140b

Browse files
committed
debug and fix skeleton tests
1 parent e640849 commit bbb140b

File tree

2 files changed

+44
-40
lines changed

2 files changed

+44
-40
lines changed

packages/client/src/service/skeleton.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,6 @@ export class Skeleton extends MetaDBManager {
233233
return this.started > 0
234234
}
235235

236-
async isLastAnnouncement(): Promise<boolean> {
237-
const subchain0 = this.status.progress.subchains[0]
238-
if (subchain0 !== undefined) {
239-
return this.getBlock(subchain0.head + BIGINT_1) !== undefined
240-
} else {
241-
return true
242-
}
243-
}
244-
245236
/**
246237
* Try fast forwarding the chain head to the number
247238
*/
@@ -316,7 +307,7 @@ export class Skeleton extends MetaDBManager {
316307
} else if (lastchain.head >= number) {
317308
// Check if its duplicate announcement, if not trim the head and let the match run
318309
// post this if block
319-
const mayBeDupBlock = await this.getBlock(number)
310+
const mayBeDupBlock = await this.getBlock(number, true)
320311
if (mayBeDupBlock !== undefined && equalsBytes(mayBeDupBlock.header.hash(), head.hash())) {
321312
this.config.logger.debug(
322313
`Skeleton duplicate ${force ? 'setHead' : 'announcement'} tail=${lastchain.tail} head=${
@@ -359,7 +350,7 @@ export class Skeleton extends MetaDBManager {
359350
return true
360351
}
361352
}
362-
const parent = await this.getBlock(number - BIGINT_1)
353+
const parent = await this.getBlock(number - BIGINT_1, true)
363354
if (parent === undefined || !equalsBytes(parent.hash(), head.header.parentHash)) {
364355
if (force) {
365356
this.config.logger.warn(
@@ -818,7 +809,7 @@ export class Skeleton extends MetaDBManager {
818809
const syncedBlock = await this.getBlock(
819810
syncedHeight,
820811
// need to debug why this flag causes to return undefined when chain gets synced
821-
//, true
812+
true,
822813
)
823814
if (
824815
syncedBlock !== undefined &&
@@ -859,7 +850,7 @@ export class Skeleton extends MetaDBManager {
859850
async headHash(): Promise<Uint8Array | undefined> {
860851
const subchain = this.bounds()
861852
if (subchain !== undefined) {
862-
const headBlock = await this.getBlock(subchain.head)
853+
const headBlock = await this.getBlock(subchain.head, true)
863854
if (headBlock) {
864855
return headBlock.hash()
865856
}
@@ -998,7 +989,7 @@ export class Skeleton extends MetaDBManager {
998989
} else {
999990
// Critical error, we expect new incoming blocks to extend the canonical
1000991
// subchain which is the [0]'th
1001-
const tailBlock = await this.getBlock(this.status.progress.subchains[0].tail)
992+
const tailBlock = await this.getBlock(this.status.progress.subchains[0].tail, true)
1002993
this.config.logger.warn(
1003994
`Blocks don't extend canonical subchain tail=${
1004995
this.status.progress.subchains[0].tail
@@ -1112,7 +1103,7 @@ export class Skeleton extends MetaDBManager {
11121103
for (let i = 0; i < maxItems; i++) {
11131104
const tailBlock = await this.getBlockByHash(next)
11141105

1115-
if (tailBlock === undefined) {
1106+
if (tailBlock === undefined || tailBlock.header.number <= BIGINT_1) {
11161107
break
11171108
} else {
11181109
blocks.push(tailBlock)
@@ -1202,7 +1193,7 @@ export class Skeleton extends MetaDBManager {
12021193
while (!this.status.canonicalHeadReset && canonicalHead < subchain.head) {
12031194
// Get next block
12041195
const number = canonicalHead + BIGINT_1
1205-
const block = await this.getBlock(number)
1196+
const block = await this.getBlock(number, true)
12061197

12071198
if (block === undefined) {
12081199
// This can happen
@@ -1381,10 +1372,12 @@ export class Skeleton extends MetaDBManager {
13811372
* Gets a block from the skeleton or canonical db by number.
13821373
*/
13831374
async getBlock(number: bigint, onlyCanonical = false): Promise<Block | undefined> {
1384-
return this.chain.blockchain.dbManager.getBlock(number, {
1385-
fcUed: onlyCanonical,
1386-
linked: this.status.linked,
1387-
})
1375+
return this.chain.blockchain.dbManager
1376+
.getBlock(number, {
1377+
fcUed: onlyCanonical,
1378+
linked: this.status.linked,
1379+
})
1380+
.catch((_e) => undefined)
13881381
}
13891382

13901383
/**
@@ -1395,7 +1388,7 @@ export class Skeleton extends MetaDBManager {
13951388
onlyCanonical: boolean = false,
13961389
): Promise<Block | undefined> {
13971390
return this.chain.blockchain.dbManager.getBlock(hash, {
1398-
fcUed: !onlyCanonical,
1391+
fcUed: onlyCanonical,
13991392
linked: this.status.linked,
14001393
})
14011394
}

packages/client/test/sync/skeleton.spec.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,23 @@ type Subchain = {
2525
}
2626

2727
const common = new Common({ chain: Mainnet })
28-
const block49 = createBlock({ header: { number: 49 } }, { common })
29-
const block49B = createBlock({ header: { number: 49, extraData: utf8ToBytes('B') } }, { common })
30-
const block50 = createBlock({ header: { number: 50, parentHash: block49.hash() } }, { common })
28+
const block49 = createBlock({ header: { number: 49 } }, { common, setHardfork: true })
29+
const block49B = createBlock(
30+
{ header: { number: 49, extraData: utf8ToBytes('B') } },
31+
{ common, setHardfork: true },
32+
)
33+
const block50 = createBlock(
34+
{ header: { number: 50, parentHash: block49.hash() } },
35+
{ common, setHardfork: true },
36+
)
3137
const block50B = createBlock(
3238
{ header: { number: 50, parentHash: block49.hash(), gasLimit: 999 } },
33-
{ common },
39+
{ common, setHardfork: true },
40+
)
41+
const block51 = createBlock(
42+
{ header: { number: 51, parentHash: block50.hash() } },
43+
{ common, setHardfork: true },
3444
)
35-
const block51 = createBlock({ header: { number: 51, parentHash: block50.hash() } }, { common })
3645

3746
describe('[Skeleton]/ startup scenarios ', () => {
3847
it('starts the chain when starting the skeleton', async () => {
@@ -594,20 +603,20 @@ describe('[Skeleton] / setHead', async () => {
594603
'canonical height should change when setHead is set with force=true',
595604
)
596605

597-
// unlink the skeleton for the below check to check all blocks cleared
606+
// unlink the skeleton for the below check and still find the blocks in blokchain
598607
skeleton['status'].linked = false
599608
for (const block of [block1, block2, block3, block4, block5]) {
600609
assert.equal(
601-
(await skeleton.getBlock(block.header.number, true))?.hash(),
602-
undefined,
603-
`skeleton block number=${block.header.number} should be cleaned up after filling canonical chain`,
610+
(await skeleton.getBlock(block.header.number, true))?.hash() !== undefined,
611+
true,
612+
`skeleton block number=${block.header.number} should be available even afer filling canonical chain`,
604613
)
605614
assert.equal(
606-
(await skeleton.getBlockByHash(block.hash(), true))?.hash(),
607-
undefined,
615+
(await skeleton.getBlockByHash(block.hash(), true))?.hash() !== undefined,
616+
true,
608617
`skeleton block hash=${short(
609618
block.hash(),
610-
)} should be cleaned up after filling canonical chain`,
619+
)} should be available even after filling canonical chain`,
611620
)
612621
}
613622
})
@@ -677,16 +686,16 @@ describe('[Skeleton] / setHead', async () => {
677686
skeleton['status'].linked = false
678687
for (const block of [block3, block4, block5]) {
679688
assert.equal(
680-
(await skeleton.getBlock(block.header.number, true))?.hash(),
681-
undefined,
682-
`skeleton block number=${block.header.number} should be cleaned up after filling canonical chain`,
689+
(await skeleton.getBlock(block.header.number, true))?.hash() !== undefined,
690+
true,
691+
`skeleton block number=${block.header.number} should still be available after filling canonical chain`,
683692
)
684693
assert.equal(
685-
(await skeleton.getBlockByHash(block.hash(), true))?.hash(),
686-
undefined,
694+
(await skeleton.getBlockByHash(block.hash(), true))?.hash() !== undefined,
695+
true,
687696
`skeleton block hash=${short(
688697
block.hash(),
689-
)} should be cleaned up after filling canonical chain`,
698+
)} should still be available after filling canonical chain`,
690699
)
691700
}
692701
// restore linkedStatus
@@ -707,9 +716,9 @@ describe('[Skeleton] / setHead', async () => {
707716

708717
await skeleton.setHead(block41, false)
709718
await skeleton.setHead(block51, false)
710-
711719
// should link the chains including the 41, 51 block backfilled from the unfinalized
712720
await skeleton.forkchoiceUpdate(block61)
721+
713722
assert.equal(
714723
skeleton['status'].progress.subchains[0]?.head,
715724
BigInt(6),
@@ -856,7 +865,9 @@ describe('[Skeleton] / setHead', async () => {
856865
await skeleton.open()
857866

858867
await skeleton.initSync(block4InvalidPoS)
868+
// the following putBlocks will fail ad block3 is pow block
859869
await skeleton.putBlocks([block3PoW, block2])
870+
860871
assert.equal(chain.blocks.height, BigInt(0), 'canonical height should be at genesis')
861872
await skeleton.putBlocks([block1])
862873
await wait(200)

0 commit comments

Comments
 (0)