All files / test/ChainHistory/fixtures FixtureBuilder.ts

88.98% Statements 105/118
58.33% Branches 21/36
100% Functions 13/13
90.72% Lines 88/97

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 1722x 2x 2x     2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                 2x 2x 2x     2x 2x             2x 2x 2x 2x           2x           2x   2x     2x 6x   4x     2x       2x 137x 8x 8x     8x           2x   2x       4x 4x 4x 4x   4x     11x         21x   21x 21x 16x   16x 16x 16x 16x 16x 16x 16x 16x   16x   16x 16x 16x 16x 16x 16x 16x   16x     21x   21x 21x   21x     34x       1x 1x 1x       1x 1x 3x       5x 5x     5x 5x   5x     13x      
import * as Queries from './queries';
import { Cardano } from '@cardano-sdk/core';
import { DB_MAX_SAFE_INTEGER } from '../../../src/ChainHistory/DbSyncChainHistory/queries';
import { Logger } from 'ts-log';
import { Pool, QueryResult } from 'pg';
import { Range, bufferToHexString } from '@cardano-sdk/util';
 
export enum TxWith {
  AuxiliaryData = 'auxiliaryData',
  PoolRetireCertificate = 'poolRetireCertificate',
  PoolUpdateCertificate = 'poolUpdateCertificate',
  StakeRegistrationCertificate = 'stakeRegistrationCertificate',
  StakeDeregistrationCertificate = 'stakeDeregistrationCertificate',
  DelegationCertificate = 'delegationCertificate',
  MirCertificate = 'mirCertificate',
  CollateralInput = 'collateralInput',
  Mint = 'mint',
  MultiAsset = 'multiAsset',
  Redeemer = 'redeemer',
  Withdrawal = 'withdrawal',
  CollateralOutput = 'collateralOutput',
  ProposalProcedures = 'proposalProcedures',
  VotingProcedure = 'votingProcedures',
  ScriptReference = 'scriptReference'
}
 
export type AddressesInBlockRange = {
  addresses: Set<Cardano.PaymentAddress>;
  blockRange: Range<Cardano.BlockNo>;
  txInRangeCount: number;
};
 
export class ChainHistoryFixtureBuilder {
  #db: Pool;
  #logger: Logger;
 
  constructor(db: Pool, logger: Logger) {
    this.#db = db;
    this.#logger = logger;
  }
 
  public async getAddressesWithSomeInBlockRange(
    desiredQty: number,
    blockRange: Range<Cardano.BlockNo>
  ): Promise<AddressesInBlockRange> {
    const lowerBound = blockRange?.lowerBound ?? 0;
    const upperBound = blockRange?.upperBound ?? DB_MAX_SAFE_INTEGER;
    const txIds = new Set<bigint>();
    const addressesInBlockRange = {
      addresses: new Set<Cardano.PaymentAddress>(),
      blockRange: { lowerBound: Cardano.BlockNo(DB_MAX_SAFE_INTEGER), upperBound: Cardano.BlockNo(0) },
      txInRangeCount: 0
    };
 
    this.#logger.debug(`About to find transactions of addresses since block ${lowerBound} and before ${upperBound}`);
 
    const results: QueryResult<{
      address: string;
      block_no: number;
      tx_id: bigint;
    }> = await this.#db.query(Queries.transactionInBlockRange, [lowerBound, upperBound]);
 
    Iif (results.rows.length === 0) throw new Error(`No transactions found in range [${lowerBound} -> ${upperBound}].`);
 
    // Collect ony the requested amount of addresses and drop the excess.
    for (const { address } of results.rows) {
      if (addressesInBlockRange.addresses.size >= desiredQty) break;
 
      addressesInBlockRange.addresses.add(address as unknown as Cardano.PaymentAddress);
    }
 
    Iif (results.rows.length < desiredQty) {
      this.#logger.warn(`${desiredQty} addresses desired, only ${results.rows.length} results found`);
    }
 
    for (const { address, block_no, tx_id } of results.rows) {
      if (addressesInBlockRange.addresses.has(address as unknown as Cardano.PaymentAddress)) {
        txIds.add(tx_id);
        addressesInBlockRange.blockRange.lowerBound = Cardano.BlockNo(
          Math.min(addressesInBlockRange.blockRange.lowerBound, block_no)
        );
        addressesInBlockRange.blockRange.upperBound = Cardano.BlockNo(
          Math.max(addressesInBlockRange.blockRange.upperBound, block_no)
        );
      }
    }
 
    addressesInBlockRange.txInRangeCount = txIds.size;
 
    return addressesInBlockRange;
  }
 
  public async getBlockHashes(desiredQty: number): Promise<Cardano.BlockId[]> {
    this.#logger.debug(`About to fetch up to the last ${desiredQty} blocks`);
    const result: QueryResult<{ hash: Buffer }> = await this.#db.query(Queries.latestBlockHashes, [desiredQty]);
    const resultsQty = result.rows.length;
    Iif (result.rows.length === 0) {
      throw new Error('No blocks found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} blocks desired, only ${resultsQty} results found`);
    }
    return result.rows.map(({ hash }) => bufferToHexString(hash) as unknown as Cardano.BlockId);
  }
 
  // eslint-disable-next-line sonarjs/cognitive-complexity,complexity
  public async getTxHashes(desiredQty: number, options?: { with?: TxWith[] }): Promise<Cardano.TransactionId[]> {
    this.#logger.debug(`About to fetch up to the last ${desiredQty} transactions`);
 
    let query = Queries.latestTxHashes;
    if (options?.with) {
      query = Queries.beginLatestTxHashes;
 
      if (options.with.includes(TxWith.MultiAsset)) query += Queries.latestTxHashesWithMultiAsset;
      if (options.with.includes(TxWith.AuxiliaryData)) query += Queries.latestTxHashesWithAuxiliaryData;
      if (options.with.includes(TxWith.Mint)) query += Queries.latestTxHashesWithMint;
      if (options.with.includes(TxWith.Redeemer)) query += Queries.latestTxHashesWithRedeemer;
      if (options.with.includes(TxWith.CollateralInput)) query += Queries.latestTxHashesWithCollateral;
      Iif (options.with.includes(TxWith.PoolRetireCertificate)) query += Queries.latestTxHashesWithPoolRetireCerts;
      Iif (options.with.includes(TxWith.PoolUpdateCertificate)) query += Queries.latestTxHashesWithPoolUpdateCerts;
      Iif (options.with.includes(TxWith.StakeRegistrationCertificate))
        query += Queries.latestTxHashesWithStakeRegistrationCerts;
      Iif (options.with.includes(TxWith.StakeDeregistrationCertificate))
        query += Queries.latestTxHashesWithStakeDeregistrationCerts;
      if (options.with.includes(TxWith.DelegationCertificate)) query += Queries.latestTxHashesWithDelegationCerts;
      Iif (options.with.includes(TxWith.MirCertificate)) query += Queries.latestTxHashesWithMirCerts;
      if (options.with.includes(TxWith.Withdrawal)) query += Queries.latestTxHashesWithWithdrawal;
      if (options.with.includes(TxWith.CollateralOutput)) query += Queries.latestTxHashesWithCollateralOutput;
      if (options.with.includes(TxWith.ProposalProcedures)) query += Queries.latestTxHashesWithProposalProcedures;
      if (options.with.includes(TxWith.VotingProcedure)) query += Queries.latestTxHashesWithVotingProcedures;
      if (options.with.includes(TxWith.ScriptReference)) query += Queries.latestTxHashesWithScriptReference;
 
      query += Queries.endLatestTxHashes;
    }
 
    const result: QueryResult<{ tx_hash: Buffer }> = await this.#db.query(query, [desiredQty]);
 
    const resultsQty = result.rows.length;
    Iif (result.rows.length === 0) {
      throw new Error('No transactions found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} transactions desired, only ${resultsQty} results found`);
    }
    return result.rows.map(({ tx_hash }) => bufferToHexString(tx_hash) as unknown as Cardano.TransactionId);
  }
 
  public async getMultiAssetTxOutIds(desiredQty: number) {
    this.#logger.debug('About to fetch genesis multiasset tx out ids');
    const result: QueryResult<{ tx_out_id: string }> = await this.#db.query(Queries.findMultiAssetTxOut, [desiredQty]);
    return result.rows.map(({ tx_out_id }) => BigInt(tx_out_id));
  }
 
  public async getGenesisAddresses() {
    this.#logger.debug('About to fetch genesis addresses');
    const result: QueryResult<{ address: string }> = await this.#db.query(Queries.genesisUtxoAddresses);
    return result.rows.map(({ address }) => address as unknown as Cardano.PaymentAddress);
  }
 
  public async getDistinctAddresses(desiredQty: number): Promise<Cardano.PaymentAddress[]> {
    this.#logger.debug(`About to fetch up to the last ${desiredQty} distinct addresses`);
    const result: QueryResult<{ address: string }> = await this.#db.query(Queries.latestDistinctAddresses, [
      desiredQty
    ]);
    const resultsQty = result.rows.length;
    Iif (result.rows.length === 0) {
      throw new Error('No addresses found');
    } else Iif (resultsQty < desiredQty) {
      this.#logger.warn(`${desiredQty} distinct addresses desired, only ${resultsQty} results found`);
    }
    return result.rows.map(({ address }) => address as unknown as Cardano.PaymentAddress);
  }
}