Skip to content

Commit

Permalink
fix(core-database): fix delete blocks (#3814)
Browse files Browse the repository at this point in the history
  • Loading branch information
rainydio authored Jun 19, 2020
1 parent 4e68514 commit b2fee48
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,31 @@ describe("BlockRepository.deleteBlocks", () => {
const promise = blockRepository.deleteBlocks([block2.data]);
await expect(promise).rejects.toThrow("Removing blocks from the middle");
});

it("should throw when deleting non-continuous chunk (order in reverse)", async () => {
const blockRepository = getCustomRepository(BlockRepository);
await blockRepository.saveBlocks([block1, block2, block3]);
const promise = blockRepository.deleteBlocks([block3.data, block2.data, block1.data]);
await expect(promise).rejects.toThrow("Blocks chunk to delete isn't continuous");
});

it("should throw when deleting non-continuous chunk (missing block)", async () => {
const blockRepository = getCustomRepository(BlockRepository);
await blockRepository.saveBlocks([block1, block2, block3]);
const promise = blockRepository.deleteBlocks([block3.data, block1.data]);
await expect(promise).rejects.toThrow("Blocks chunk to delete isn't continuous");
});

it("should throw when deleted count doesn't match expected", async () => {
const blockRepository = getCustomRepository(BlockRepository);
await blockRepository.saveBlocks([block1, block2, block3]);
const promise = blockRepository.deleteBlocks([
block1.data,
block2.data,
{ ...block3.data, id: "non-existing" },
]);
await expect(promise).rejects.toThrow("Failed to delete all blocks from database");
});
});

describe("BlockRepository.findManyByExpression", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export class Blockchain implements Contracts.Blockchain.Blockchain {

await __removeBlocks(nblocks);

await this.blockRepository.deleteBlocks(removedBlocks);
await this.blockRepository.deleteBlocks(removedBlocks.reverse());

if (this.transactionPool) {
this.transactionPool.readdTransactions(removedTransactions.reverse());
Expand Down
14 changes: 13 additions & 1 deletion packages/core-database/src/repositories/block-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ export class BlockRepository extends AbstractRepository<Block> {
}

public async deleteBlocks(blocks: Interfaces.IBlockData[]): Promise<void> {
const continuousChunk = blocks.every((block, i, arr) => {
return i === 0 ? true : block.height - arr[i - 1].height === 1;
});

if (!continuousChunk) {
throw new Error("Blocks chunk to delete isn't continuous");
}

return this.manager.transaction(async (manager) => {
const lastBlockHeight: number = blocks[blocks.length - 1].height;
const targetBlockHeight: number = blocks[0].height - 1;
Expand All @@ -206,13 +214,17 @@ export class BlockRepository extends AbstractRepository<Block> {
.where("block_id IN (:...blockIds)", { blockIds })
.execute();

await manager
const deleteBlocksResult = await manager
.createQueryBuilder()
.delete()
.from(Block)
.where("id IN (:...blockIds)", { blockIds })
.execute();

if (deleteBlocksResult.affected !== blockIds.length) {
throw new Error("Failed to delete all blocks from database");
}

await manager
.createQueryBuilder()
.delete()
Expand Down

0 comments on commit b2fee48

Please sign in to comment.