let recentUnfinalizedBlocks: BlockCursor[] = []
const target = createTarget({
write: async ({read}) => {
for await (const {data, ctx} of read(recentUnfinalizedBlocks[recentUnfinalizedBlocks.length-1])) {
// Track unfinalized blocks from the batch
ctx.state.rollbackChain.forEach((bc) => {
recentUnfinalizedBlocks.push(bc)
})
// Prune finalized blocks
if (ctx.head.finalized) {
recentUnfinalizedBlocks = recentUnfinalizedBlocks.filter(
b => b.number >= ctx.head.finalized!.number
)
}
}
},
fork: async (previousBlocks) => {
const rollbackIndex = findCommonAncestor(recentUnfinalizedBlocks, previousBlocks)
if (rollbackIndex >= 0) {
recentUnfinalizedBlocks.length = rollbackIndex + 1
return recentUnfinalizedBlocks[rollbackIndex]
}
return null
}
})