Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core-database): revertBlock #3948

Merged
merged 5 commits into from
Aug 13, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore(core-database): getBlocks from database if missing in state store
  • Loading branch information
sebastijankuzner committed Aug 12, 2020
commit 961e955db175de9cc19ddbe513e9fecedda95bc8
11 changes: 9 additions & 2 deletions packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,23 @@ export class DatabaseService {
public async getBlocks(offset: number, limit: number, headersOnly?: boolean): Promise<Interfaces.IBlockData[]> {
// The functions below return matches in the range [start, end], including both ends.
const start: number = offset;
const end: number = offset + limit - 1;
let end: number = offset + limit - 1;

let blocks: Interfaces.IBlockData[] = this.stateStore.getLastBlocksByHeight(start, end, headersOnly);

if (blocks.length !== limit) {
// ! assumes that earlier blocks may be missing
// ! but querying database is unnecessary when later blocks are missing too (aren't forged yet)
blocks = headersOnly

if (blocks.length) {
end = blocks[0].height;
}

const blocksFromDB = headersOnly
? await this.blockRepository.findByHeightRange(start, end)
: await this.blockRepository.findByHeightRangeWithTransactions(start, end);

blocks = [...blocksFromDB, ...blocks];
}

return blocks;
Expand Down