Skip to content

Commit

Permalink
feat: add optional CBOR representation to GET_TRANSACTION
Browse files Browse the repository at this point in the history
  • Loading branch information
iccicci committed Nov 29, 2024
1 parent 7259cf4 commit 671636e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ Payload contains [protocol parameters](https://docs.blockfrost.io/#tag/cardano--

### GET_TRANSACTION

Returns information about a specified transaction.
Returns information about a specified transaction, optionally with CBOR representation.

Input message:

Expand All @@ -645,6 +645,7 @@ Input message:
"command": "GET_TRANSACTION";
"params": {
"txId": string; // transaction id
"cbor":? boolean;
}
}
```
Expand Down Expand Up @@ -686,6 +687,7 @@ Payload contains [transaction data](https://docs.blockfrost.io/#tag/cardano--tra
asset_mint_or_burn_count: number;
redeemer_count: number;
valid_contract: boolean;
cbor?: string;
};
}
```
Expand Down
8 changes: 2 additions & 6 deletions src/methods/get-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@ import { prepareMessage } from '../utils/message.js';
import { fetchTransactionData } from '../utils/transaction.js';
import { MessageId } from '../types/message.js';

export default async (id: MessageId, clientId: string, txId: string): Promise<string> => {
const data = await fetchTransactionData(txId);
const message = prepareMessage({ id, clientId, data });

return message;
};
export default async (id: MessageId, clientId: string, txId: string, cbor?: boolean) =>
prepareMessage({ id, clientId, data: await fetchTransactionData(txId, cbor) });
2 changes: 1 addition & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ wss.on('connection', async (ws: Server.Ws) => {

case 'GET_TRANSACTION': {
validators.GET_TRANSACTION(params);
response = await getTransaction(id, clientId, params.txId);
response = await getTransaction(id, clientId, params.txId, params.cbor);

break;
}
Expand Down
1 change: 1 addition & 0 deletions src/types/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type Messages = BaseMessage &
command: 'GET_TRANSACTION';
params: {
txId: string;
cbor?: boolean;
};
}
| {
Expand Down
1 change: 1 addition & 0 deletions src/utils/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const schemas: { [k in Validators]: { properties: unknown; required?: string[] }
GET_TRANSACTION: {
properties: {
txId: { type: 'string' },
cbor: { type: 'boolean' },
},
required: ['txId'],
},
Expand Down
15 changes: 12 additions & 3 deletions test/unit/tests/utils/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ describe('transaction utils', () => {
}

test(
'no deadlock in fetchTransactionData (BUG)',
'no deadlock in fetchTransactionData',
{ timeout: 30000 },
async () => {
// Mock /txs/:txHash responses
nock(/.+blockfrost.io/)
Expand Down Expand Up @@ -162,13 +163,22 @@ describe('transaction utils', () => {
});
});

// Mock txs/:id/cbor responses
nock(/.+blockfrost.io/)
.persist()
.get(/api\/v0\/txs\/.*\/cbor/)
.delay(1000)
.reply(200, (_uri, _body, cb) => {
cb(null, { cbor: '0123456789abcdef0123456789abcdef0123456789abcdef' });
});

let promises = [];

// run thousands of fetchTransactionData
// (promise limiter concurrency set to 500 by default)
for (let i = 0; i < 5000; i++) {
promises.push(
transactionUtils.fetchTransactionData(`txHash-${i}`).then(data => {
transactionUtils.fetchTransactionData(`txHash-${i}`, Math.random() > 0.3).then(data => {
// console.log(`Fetched txHash-${i}`);
return data;
}),
Expand All @@ -194,7 +204,6 @@ describe('transaction utils', () => {
expect(txs[i].hash).toBe(`txHash-${i}`);
}
},
{ timeout: 15000 },
);

test(
Expand Down

0 comments on commit 671636e

Please sign in to comment.