Skip to content

Commit

Permalink
Merge pull request #1550 from input-output-hk/fix/fatal-provider-errors
Browse files Browse the repository at this point in the history
fix: fatal provider errors
  • Loading branch information
mkazlauskas authored Dec 20, 2024
2 parents ec6900f + bf4a8b9 commit 7d5f647
Show file tree
Hide file tree
Showing 27 changed files with 394 additions and 516 deletions.
3 changes: 3 additions & 0 deletions packages/tx-construction/src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
{
"path": "../../util/src"
},
{
"path": "../../util-rxjs/src"
},
{
"path": "../../core/src"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/tx-construction/src/tx-builder/TxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ import {
validateValidityInterval
} from './utils';
import { SelectionSkeleton } from '@cardano-sdk/input-selection';
import { coldObservableProvider } from '@cardano-sdk/util-rxjs';
import { contextLogger, deepEquals } from '@cardano-sdk/util';
import { createOutputValidator } from '../output-validation';
import { initializeTx } from './initializeTx';
import { lastValueFrom } from 'rxjs';
import { poll } from '@cardano-sdk/util-rxjs';
import omit from 'lodash/omit.js';
import uniq from 'lodash/uniq.js';

Expand Down Expand Up @@ -499,12 +499,12 @@ export class GenericTxBuilder implements TxBuilder {
allRewardAccounts = uniq([...knownAddresses, ...newAddresses]).map(({ rewardAccount }) => rewardAccount);
}

const rewardAccounts$ = coldObservableProvider({
const rewardAccounts$ = poll({
logger: contextLogger(this.#logger, 'getOrCreateRewardAccounts'),
pollUntil: (rewardAccounts) =>
allRewardAccounts.every((newAccount) => rewardAccounts.some((acct) => acct.address === newAccount)),
provider: this.#dependencies.txBuilderProviders.rewardAccounts,
retryBackoffConfig: { initialInterval: 10, maxInterval: 100, maxRetries: 10 }
retryBackoffConfig: { initialInterval: 10, maxInterval: 100, maxRetries: 10 },
sample: this.#dependencies.txBuilderProviders.rewardAccounts
});

try {
Expand Down
103 changes: 0 additions & 103 deletions packages/util-rxjs/src/coldObservableProvider.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/util-rxjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export * from './passthrough';
export * from './finalizeWithLatest';
export * from './concatAndCombineLatest';
export * from './shareRetryBackoff';
export * from './coldObservableProvider';
export * from './poll';
export * from './types';
79 changes: 79 additions & 0 deletions packages/util-rxjs/src/poll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Logger } from 'ts-log';
import {
NEVER,
Observable,
concat,
defer,
distinctUntilChanged,
from,
mergeMap,
of,
switchMap,
takeUntil,
throwError
} from 'rxjs';
import { RetryBackoffConfig, retryBackoff } from 'backoff-rxjs';
import { strictEquals } from '@cardano-sdk/util';

const POLL_UNTIL_RETRY = Symbol('POLL_UNTIL_RETRY');

export interface PollProps<T> {
sample: () => Promise<T>;
retryBackoffConfig: RetryBackoffConfig;
trigger$?: Observable<unknown>;
equals?: (t1: T, t2: T) => boolean;
combinator?: typeof switchMap;
cancel$?: Observable<unknown>;
pollUntil?: (v: T) => boolean;
logger: Logger;
}

export const poll = <T>({
sample,
retryBackoffConfig,
trigger$ = of(true),
equals = strictEquals,
combinator = switchMap,
cancel$ = NEVER,
pollUntil = () => true,
logger
}: PollProps<T>) =>
trigger$.pipe(
combinator(() =>
defer(() =>
from(sample()).pipe(
mergeMap((v) =>
pollUntil(v)
? of(v)
: // Emit value, but also throw error to force retryBackoff to kick in
concat(
of(v),
throwError(() => POLL_UNTIL_RETRY)
)
)
)
).pipe(
retryBackoff({
...retryBackoffConfig,
shouldRetry: (error) => {
if (error === POLL_UNTIL_RETRY) {
logger.warn('"pollUntil" condition not met, will retry');
return true;
}

logger.error(error);

if (retryBackoffConfig.shouldRetry) {
const shouldRetry = retryBackoffConfig.shouldRetry(error);
logger.debug(`Should retry: ${shouldRetry}`);
return shouldRetry;
}

return true;
}
})
)
),
distinctUntilChanged(equals),
takeUntil(cancel$)
);
138 changes: 0 additions & 138 deletions packages/util-rxjs/test/coldObservableProvider.test.ts

This file was deleted.

Loading

0 comments on commit 7d5f647

Please sign in to comment.