Skip to content

Commit

Permalink
fix load blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
qiweiii committed Sep 8, 2023
1 parent 82aa8c8 commit a995fb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/chopsticks/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const commands = yargs(hideBin(process.argv))
},
resume: {
desc: 'Resume from the lastest block saved in the db, note this will override the block option',
boolean: true,
},
}),
async (argv) => {
Expand Down
21 changes: 15 additions & 6 deletions packages/chopsticks/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ export const setupContext = async (argv: Config, overrideParent = false) => {
await overrideWasm(chain, argv['wasm-override'], at)

// load blocks from db
if (chain.db && argv.resume) {
const blocks = await chain.db.getRepository(BlockEntity).find({ where: {}, order: { number: 'asc' } })
let head
for (const block of blocks) {
head = await chain.loadBlockFromDB(block.number)
if (chain.db) {
if (argv.resume) {
const blocks = await chain.db.getRepository(BlockEntity).find({ where: {}, order: { number: 'asc' } })
// validate the first block in db is chain.head+1 and db blocks are consecutive
const canResume = blocks.every((block, index) => block.number === chain.head.number + index + 1)
if (canResume) {
let head
for (const block of blocks) {
head = await chain.loadBlockFromDB(block.number)
}
await chain.setHead(head)
}
} else {
// starting without resume should clear blocks from db
await chain.db.getRepository(BlockEntity).clear()
}
await chain.setHead(head)
}

return { chain }
Expand Down

0 comments on commit a995fb5

Please sign in to comment.