Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 20 additions & 6 deletions src/cardano.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export type TxEvent = GenericTxEvent<cardano.Tx>;
export type MempoolEvent = GenericTxInMempoolEvent<cardano.Tx>;
export type TxHash = Uint8Array;
export type TxCbor = Uint8Array;
export type Block = {
parsedBlock: cardano.Block;
nativeBytes: Uint8Array;
};

function toMempoolEvent(txInMempool: submit.TxInMempool): MempoolEvent {
return {
Expand All @@ -60,6 +64,16 @@ function anyChainToBlock(msg: sync.AnyChainBlock) {
return msg.chain.case == "cardano" ? msg.chain.value : null;
}

function anyChainToBlockWithBytes(msg: sync.AnyChainBlock): Block | null {
if (msg.chain.case == "cardano") {
return {
parsedBlock: msg.chain.value,
nativeBytes: msg.nativeBytes
};
}
return null;
}

function pointToBlockRef(p: ChainPoint) {
return new sync.BlockRef({
index: BigInt(p.slot),
Expand Down Expand Up @@ -146,13 +160,13 @@ export class SyncClient {
return blockRefToPoint(res.tip!);
}

async fetchBlock(p: ChainPoint): Promise<cardano.Block> {
async fetchBlock(p: ChainPoint): Promise<Block> {
const req = pointToBlockRef(p);
const res = await this.inner.fetchBlock({ ref: [req] });
return anyChainToBlock(res.block[0])!;
return anyChainToBlockWithBytes(res.block[0])!;
}

async fetchHistory(p: ChainPoint | undefined, maxItems = 1): Promise<cardano.Block> {
async fetchHistory(p: ChainPoint | undefined, maxItems = 1): Promise<Block[]> {
const req = new sync.DumpHistoryRequest({
startToken: p ? new sync.BlockRef({
index: BigInt(p.slot),
Expand All @@ -167,9 +181,9 @@ export class SyncClient {
throw new Error("No block history found for the provided ChainPoint.");
}

const block = anyChainToBlock(res.block[0]);

return block!;
return res.block
.map(anyChainToBlockWithBytes)
.filter((block): block is Block => block !== null);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {
SubmitClient as CardanoSubmitClient,
WatchClient as CardanoWatchClient,
ChainPoint as CardanoChainPoint,
Block as CardanoBlock,
Utxo as CardanoUtxo,
TipEvent as CardanoTipEvent,
TxEvent as CardanoTxEvent,
Expand Down
24 changes: 17 additions & 7 deletions test/index.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,17 @@ describe("SyncClient", () => {
expect(tip.hash.length).toBeGreaterThan(0);
});
test("fetchBlock", async () => {
const block = await syncClient.fetchBlock({
const blockWithBytes = await syncClient.fetchBlock({
slot: 85213090,
hash: 'e50842b1cc3ac813cb88d1533c3dea0f92e0ea945f53487c1d960c2210d0c3ba',
});

expect(blockWithBytes.nativeBytes).toBeInstanceOf(Uint8Array);
expect(blockWithBytes.nativeBytes.length).toBeGreaterThan(0);

expect({
body: block.body?.toJson(),
header: block.header?.toJson()
body: blockWithBytes.parsedBlock.body?.toJson(),
header: blockWithBytes.parsedBlock.header?.toJson()
}).toEqual({
body: {},
header: {
Expand All @@ -447,14 +450,21 @@ describe("SyncClient", () => {
});
});
test("fetchHistory", async () => {
const block = await syncClient.fetchHistory({
const blocks = await syncClient.fetchHistory({
slot: 85213090,
hash: 'e50842b1cc3ac813cb88d1533c3dea0f92e0ea945f53487c1d960c2210d0c3ba',
});
}, 1);

expect(Array.isArray(blocks)).toBe(true);
expect(blocks.length).toBe(1);

const firstBlock = blocks[0];
expect(firstBlock.nativeBytes).toBeInstanceOf(Uint8Array);
expect(firstBlock.nativeBytes.length).toBeGreaterThan(0);

expect({
body: block.body?.toJson(),
header: block.header?.toJson()
body: firstBlock.parsedBlock.body?.toJson(),
header: firstBlock.parsedBlock.header?.toJson()
}).toEqual({
body: {},
header: {
Expand Down