All files / src/TxSubmit NodeTxSubmitProvider.ts

90.24% Statements 37/41
69.56% Branches 16/23
100% Functions 9/9
90% Lines 36/40

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 11435x 35x                                               35x 35x 2x 1x 1x                   1x 1x                 35x 16x 16x 16x 16x     16x 16x 16x 16x       6x 5x     35x 6x   2x 1x   1x 1x       6x               6x       6x 2x         2x 2x     2x 2x 1x         1x            
import { EmptyError, firstValueFrom } from 'rxjs';
import {
  GeneralCardanoNodeError,
  GeneralCardanoNodeErrorCode,
  HandleOwnerChangeError,
  HandleProvider,
  HealthCheckResponse,
  ProviderError,
  ProviderFailure,
  SubmitTxArgs,
  TxSubmissionError,
  TxSubmitProvider
} from '@cardano-sdk/core';
import { InMemoryCache } from '../InMemoryCache';
import { Logger } from 'ts-log';
import { ObservableCardanoNode } from '@cardano-sdk/projection';
import { WithLogger } from '@cardano-sdk/util';
 
type ObservableTxSubmitter = Pick<ObservableCardanoNode, 'healthCheck$' | 'submitTx'>;
export type NodeTxSubmitProviderProps = WithLogger & {
  handleProvider?: HandleProvider;
  cardanoNode: ObservableTxSubmitter;
  healthCheckCache: InMemoryCache;
};
 
const emptyMessage = 'ObservableCardanoNode observable completed without emitting';
const toProviderError = (error: unknown) => {
  if (error instanceof TxSubmissionError) {
    throw new ProviderError(ProviderFailure.BadRequest, error);
  } else Iif (error instanceof GeneralCardanoNodeError) {
    throw new ProviderError(
      error.code === GeneralCardanoNodeErrorCode.ConnectionFailure
        ? ProviderFailure.ConnectionFailure
        : error.code === GeneralCardanoNodeErrorCode.ServerNotReady
        ? ProviderFailure.ServerUnavailable
        : ProviderFailure.Unknown,
      error
    );
  }
  if (error instanceof EmptyError) {
    throw new ProviderError(
      ProviderFailure.ServerUnavailable,
      new GeneralCardanoNodeError(GeneralCardanoNodeErrorCode.ServerNotReady, null, emptyMessage)
    );
  }
  throw new ProviderError(ProviderFailure.Unknown, error);
};
 
/** Submit transactions to an ObservableCardanoNode. Validates handle resolutions against a HandleProvider. */
export class NodeTxSubmitProvider implements TxSubmitProvider {
  #logger: Logger;
  #cardanoNode: ObservableTxSubmitter;
  #handleProvider?: HandleProvider;
  #healthCheckCache: InMemoryCache;
 
  constructor({ handleProvider, cardanoNode, logger, healthCheckCache }: NodeTxSubmitProviderProps) {
    this.#handleProvider = handleProvider;
    this.#cardanoNode = cardanoNode;
    this.#logger = logger;
    this.#healthCheckCache = healthCheckCache;
  }
 
  async submitTx({ signedTransaction, context }: SubmitTxArgs): Promise<void> {
    await this.#throwIfHandleResolutionConflict(context);
    await firstValueFrom(this.#cardanoNode.submitTx(signedTransaction)).catch(toProviderError);
  }
 
  async #checkHealth(): Promise<HealthCheckResponse> {
    const [cardanoNodeHealth, handleProviderHealth] = await Promise.all([
      firstValueFrom(this.#cardanoNode.healthCheck$).catch((error): HealthCheckResponse => {
        if (error instanceof EmptyError) {
          return { ok: false, reason: emptyMessage };
        }
        this.#logger.error('Unexpected healtcheck error', error);
        return { ok: false, reason: 'Internal error' };
      }),
      this.#handleProvider?.healthCheck()
    ]);
    return {
      localNode: cardanoNodeHealth.localNode,
      ok: cardanoNodeHealth.ok && (!handleProviderHealth || handleProviderHealth.ok),
      reason: cardanoNodeHealth.reason || handleProviderHealth?.reason
    };
  }
 
  async healthCheck(): Promise<HealthCheckResponse> {
    return this.#healthCheckCache.get('ogmios_cardano_node', () => this.#checkHealth());
  }
 
  async #throwIfHandleResolutionConflict(context: SubmitTxArgs['context']): Promise<void> {
    if (context?.handleResolutions && context.handleResolutions.length > 0) {
      Iif (!this.#handleProvider) {
        this.#logger.debug('No handle provider: bypassing handle validation');
        return;
      }
 
      const handleInfoList = await this.#handleProvider.resolveHandles({
        handles: context.handleResolutions.map((hndRes) => hndRes.handle)
      });
 
      for (const [index, handleInfo] of handleInfoList.entries()) {
        if (!handleInfo || handleInfo.cardanoAddress !== context.handleResolutions[index].cardanoAddress) {
          const handleOwnerChangeError = new HandleOwnerChangeError(
            context.handleResolutions[index].handle,
            context.handleResolutions[index].cardanoAddress,
            handleInfo ? handleInfo.cardanoAddress : null
          );
          throw new ProviderError(ProviderFailure.Conflict, handleOwnerChangeError);
        }
      }
    }
  }
}