Skip to content

Commit 8d0f6ac

Browse files
committed
fixup! feat: add web socket based transactions by addresses provider
1 parent bcb5752 commit 8d0f6ac

File tree

3 files changed

+59
-35
lines changed

3 files changed

+59
-35
lines changed

packages/cardano-services/src/WsServer/requests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import { Cardano, CardanoNode, CardanoNodeUtil } from '@cardano-sdk/core';
44
import { Pool } from 'pg';
55
import { ProtocolParamsModel } from '../NetworkInfo/DbSyncNetworkInfoProvider/types';
6+
import { partialTransactionsByIds, transactionsByIds } from './transactions';
67
import { toProtocolParams } from '../NetworkInfo/DbSyncNetworkInfoProvider/mappers';
7-
import { transactionsByIds } from './transactions';
88

99
// Workaround for @types/pg
1010
declare module 'pg' {
@@ -142,7 +142,7 @@ SELECT DISTINCT tx_id FROM (
142142

143143
if (partialTxIds)
144144
for (let i = 0; i < partialTxIds.length; i += 100)
145-
action!(undefined, await transactionsByIds(partialTxIds.slice(i, i + 100), db));
145+
action!(undefined, await partialTransactionsByIds(partialTxIds.slice(i, i + 100), db));
146146

147147
return chunks.flat();
148148
};

packages/cardano-services/src/WsServer/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ export class CardanoWsServer extends WsProvider {
220220
};
221221

222222
const loadTransactions = async () => {
223-
const addressesMap = new Map<Cardano.PaymentAddress, true>();
224-
for (const ws of this.wss.clients) for (const address of ws.addresses) addressesMap.set(address, true);
225-
const addresses = [...addressesMap.keys()];
223+
const addressesSet = new Set<Cardano.PaymentAddress>();
224+
for (const ws of this.wss.clients) for (const address of ws.addresses) addressesSet.add(address);
225+
const addresses = [...addressesSet];
226226

227227
if (debugLog) this.logger.debug(`Transactions for notification ${notification} ${JSON.stringify(addresses)}`);
228228

packages/cardano-services/src/WsServer/transactions.ts

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,24 @@ const mapActions = (actions: ActionModel[]) =>
369369

370370
const mapAsset = (asset: [Cardano.AssetId, string]) => [asset[0], BigInt(asset[1])] as const;
371371

372+
/**
373+
* Extracts the _compound certificates_ from two lists of _base certificates_.
374+
* This is required because **db-sync** stores _compound certificates_ splitting them in _base certificate records_.
375+
*
376+
* As an example:
377+
*
378+
* `stake_vote_deleg_cert` certificates are stored as a `stake_delegation` certificate record plus a `vote_deleg_cert`
379+
* certificate record splitting `stake_vote_deleg_cert` properties: the `pool_keyhash` is stored in the first record
380+
* and the `drep` is stored in the latter record.
381+
*
382+
* To make the lower amount of queries as possible, we perform one single query on each certificate table, next we use
383+
* this function to merge them results.
384+
*
385+
* @param certs1 The first list _base certificates_.
386+
* @param certs2 The second list of _base certificates_.
387+
* @param merge The function to merge two _base certificates_ into a _compound certificate_.
388+
* @returns The two lists of _base certificates_ (pruned by the ones used to create the _compound certificates_) and the list of _compound certificates_.
389+
*/
372390
const compound = <
373391
C1 extends Cardano.HydratedCertificate,
374392
C2 extends Cardano.HydratedCertificate,
@@ -382,16 +400,21 @@ const compound = <
382400
const result: (readonly [number, C3])[] = [];
383401
const foundIndexes2: number[] = [];
384402

403+
// Iterate over certificates in the first list.
385404
for (const c1 of certs1) {
405+
// Check if in the second list there is a certificate with the same certificate index: i.e. they are two sources of the same _compound certificate_.
386406
const c2index = certs2.findIndex((c2) => c1[0] === c2[0]);
387407

408+
// In negative case, push the certificate from the first list in the return first list.
388409
if (c2index === -1) result1.push(c1);
410+
// In affirmative case, push the merged certificate in the return merged list.
389411
else {
390412
foundIndexes2.push(c2index);
391413
result.push([c1[0], merge(c1[1], certs2[c2index][1])]);
392414
}
393415
}
394416

417+
// Finally create the return second list filtering the second list from the ones used to create the return merged list.
395418
const result2 = certs2.filter((_, c2index) => !foundIndexes2.includes(c2index));
396419

397420
return [result1, result2, result] as const;
@@ -693,36 +716,37 @@ const vDele = [[null]] as unknown as TxModel['vDele'];
693716
const votes = [{}] as VoteModel[];
694717
const withdrawals = [{}] as TxModel['withdrawals'];
695718

696-
export const transactionsByIds = async (ids: string[], db: Pool, onlyUtxos?: boolean) => {
697-
const result = await db.query<{ tx: TxModel }>({
698-
...(onlyUtxos ? { name: 'get_utxos', text: getUtxos } : { name: 'get_txs', text: getTransactions }),
699-
values: [ids]
700-
});
701-
702-
const rows = onlyUtxos
703-
? result.rows.map(({ tx }) => ({
704-
tx: {
705-
...tx,
706-
actions,
707-
collateral,
708-
collaterals,
709-
commReg,
710-
commRet,
711-
deRep,
712-
metadata,
713-
mint,
714-
redeemers,
715-
reg,
716-
sDele,
717-
spReg,
718-
spRetire,
719-
unReg,
720-
vDele,
721-
votes,
722-
withdrawals
723-
}
724-
}))
725-
: result.rows;
719+
export const transactionsByIds = async (ids: string[], db: Pool) => {
720+
const { rows } = await db.query<{ tx: TxModel }>({ name: 'get_txs', text: getTransactions, values: [ids] });
726721

727722
return rows.map(({ tx }) => mapTx(tx));
728723
};
724+
725+
export const partialTransactionsByIds = async (ids: string[], db: Pool) => {
726+
const { rows } = await db.query<{ tx: TxModel }>({ name: 'get_utxos', text: getUtxos, values: [ids] });
727+
728+
return rows
729+
.map(({ tx }) => ({
730+
tx: {
731+
...tx,
732+
actions,
733+
collateral,
734+
collaterals,
735+
commReg,
736+
commRet,
737+
deRep,
738+
metadata,
739+
mint,
740+
redeemers,
741+
reg,
742+
sDele,
743+
spReg,
744+
spRetire,
745+
unReg,
746+
vDele,
747+
votes,
748+
withdrawals
749+
}
750+
}))
751+
.map(({ tx }) => mapTx(tx));
752+
};

0 commit comments

Comments
 (0)