Skip to content

Commit

Permalink
Add getBlockHeader
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastijankuzner committed Dec 3, 2024
1 parent db4f6b2 commit 6e12e52
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/contracts/source/contracts/database.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Block, Commit, Transaction } from "./crypto/index.js";
import { Block, BlockHeader, Commit, Transaction } from "./crypto/index.js";

export interface State {
height: number;
Expand All @@ -22,6 +22,9 @@ export interface DatabaseService {
getBlockById(id: string): Promise<Block | undefined>;
findBlocks(start: number, end: number): Promise<Block[]>;

getBlockHeader(height: number): Promise<BlockHeader | undefined>;
getBlockHeaderById(id: string): Promise<BlockHeader | undefined>;

getTransactionById(id: string): Promise<Transaction | undefined>;

addCommit(block: Commit): void;
Expand Down
39 changes: 39 additions & 0 deletions packages/database/source/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
@inject(Identifiers.Cryptography.Block.Factory)
private readonly blockFactory!: Contracts.Crypto.BlockFactory;

@inject(Identifiers.Cryptography.Block.Deserializer)
private readonly blockDeserializer!: Contracts.Crypto.BlockDeserializer;

@inject(Identifiers.Cryptography.Transaction.Factory)
private readonly transactionFactory!: Contracts.Crypto.TransactionFactory;

Expand Down Expand Up @@ -138,6 +141,31 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return undefined;
}

public async getBlockHeader(height: number): Promise<Contracts.Crypto.BlockHeader | undefined> {
const bytes = this.#readBlockHeaderBytes(height);

if (bytes) {
return await this.blockDeserializer.deserializeHeader(bytes);
}

return undefined;
}

public async getBlockHeaderById(id: string): Promise<Contracts.Crypto.BlockHeader | undefined> {
const height = this.#getHeightById(id);

if (height === undefined) {
return undefined;
}

const bytes = this.#readBlockHeaderBytes(height);
if (bytes) {
return await this.blockDeserializer.deserializeHeader(bytes);
}

return undefined;
}

public async findBlocks(start: number, end: number): Promise<Contracts.Crypto.Block[]> {
return await this.#map<Contracts.Crypto.Block>(
await this.findCommitBuffers(start, end),
Expand Down Expand Up @@ -273,6 +301,17 @@ export class DatabaseService implements Contracts.Database.DatabaseService {
return Buffer.concat([blockBuffer, ...transactions]);
}

#readBlockHeaderBytes(height: number): Buffer | undefined {
if (this.#commitCache.has(height)) {
return Buffer.from(this.#commitCache.get(height)!.serialized, "hex").subarray(
this.proofSize(),
this.proofSize() + this.headerSize(),
);
}

return this.blockStorage.get(height);
}

async #map<T>(data: unknown[], callback: (...arguments_: any[]) => Promise<T>): Promise<T[]> {
const result: T[] = [];

Expand Down

0 comments on commit 6e12e52

Please sign in to comment.