Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fatal provider errors #1550

Merged
merged 3 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
mirceahasegan marked this conversation as resolved.
Show resolved Hide resolved
}

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

This file was deleted.

Loading
Loading