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(wallet): base wallet will now immediately throw if tx is outside validity interval [LW-12315] #1606

Merged
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
31 changes: 31 additions & 0 deletions packages/wallet/src/Wallets/BaseWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ import {
EpochInfo,
EraSummary,
HandleProvider,
OutsideOfValidityIntervalData,
ProviderError,
ProviderFailure,
RewardAccountInfoProvider,
RewardsProvider,
Serialization,
TxSubmissionError,
TxSubmissionErrorCode,
TxSubmitProvider,
UtxoProvider
} from '@cardano-sdk/core';
Expand Down Expand Up @@ -697,6 +702,32 @@ export class BaseWallet implements ObservableWallet {
{ mightBeAlreadySubmitted }: SubmitTxOptions = {}
): Promise<Cardano.TransactionId> {
this.#logger.debug(`Submitting transaction ${outgoingTx.id}`);

// TODO: Workaround while we resolve LW-12394
const { validityInterval } = outgoingTx.body;
if (validityInterval?.invalidHereafter) {
const slot = await firstValueFrom(this.tip$.pipe(map((tip) => tip.slot)));

if (slot >= validityInterval?.invalidHereafter) {
const data: OutsideOfValidityIntervalData = {
currentSlot: slot,
validityInterval: {
invalidBefore: validityInterval?.invalidBefore,
invalidHereafter: validityInterval?.invalidHereafter
}
};

throw new ProviderError(
ProviderFailure.BadRequest,
new TxSubmissionError(
TxSubmissionErrorCode.OutsideOfValidityInterval,
data,
'Not submitting transaction due to validity interval'
)
);
}
}

this.#newTransactions.submitting$.next(outgoingTx);
try {
await this.txSubmitProvider.submitTx({
Expand Down
13 changes: 13 additions & 0 deletions packages/wallet/test/PersonalWallet/methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,19 @@ describe('BaseWallet methods', () => {
expect(await txPending).toEqual(outgoingTx);
});

it('throws if transaction is outside validity interval', async () => {
const draftTx = await wallet.initializeTx(props);
draftTx.body.validityInterval = {
invalidHereafter: Cardano.Slot(1_000_000)
};
const tx = await wallet.finalizeTx({ tx: draftTx });

await expect(wallet.submitTx(tx)).rejects.toThrow();

const txInFlight = await firstValueFrom(wallet.transactions.outgoing.inFlight$);
expect(txInFlight).toEqual([]);
});

it('does not re-serialize the transaction to compute transaction id', async () => {
// This transaction produces a different ID when round-tripping serialisation
const cbor = Serialization.TxCBOR(
Expand Down
51 changes: 51 additions & 0 deletions packages/wallet/test/services/TransactionsTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,57 @@ describe('TransactionsTracker', () => {
});
});

// TODO: Will be useful for LW-12394 investigation
it('emits timeout for transactions outside of the validity interval as failed$', async () => {
const outgoingTx = toOutgoingTx(queryTransactionsResult.pageResults[0]);

createTestScheduler().run(({ cold, hot, expectObservable }) => {
const tip$ = hot<Cardano.Tip>('-a---|', {
a: {
blockNo: Cardano.BlockNo(1),
hash: '' as Cardano.BlockId,
slot: Cardano.Slot(outgoingTx.body.validityInterval!.invalidHereafter! * 2)
}
});
const failedTx = { ...outgoingTx, id: 'x' as Cardano.TransactionId };
const submitting$ = cold('-a---|', { a: failedTx });
const pending$ = cold('-----|', { a: failedTx });
const transactionsSource$ = cold<Cardano.HydratedTx[]>('a--b-|', { a: [], b: [queryTransactionsResult.pageResults[0]] });
const failedToSubmit$ = hot<FailedTx>('-----|', { a: { ...failedTx, reason: TransactionFailure.FailedToSubmit } });
const signed$ = hot<WitnessedTx>('----|', {});
const transactionsTracker = createTransactionsTracker(
{
addresses$,
chainHistoryProvider,
historicalTransactionsFetchLimit,
inFlightTransactionsStore,
logger,
newTransactions: {
failedToSubmit$,
pending$,
signed$,
submitting$
},
retryBackoffConfig,
signedTransactionsStore,
tip$,
transactionsHistoryStore: transactionsStore
},
{
rollback$: NEVER,
transactionsSource$
}
);
expectObservable(transactionsTracker.outgoing.submitting$).toBe('-a---|', { a: failedTx });
expectObservable(transactionsTracker.outgoing.pending$).toBe('-----|');
expectObservable(transactionsTracker.outgoing.inFlight$).toBe('a(bc)|', { a: [], b: [failedTx], c: [] });
expectObservable(transactionsTracker.outgoing.onChain$).toBe('-----|', { a: [failedTx] });
expectObservable(transactionsTracker.outgoing.failed$).toBe('-a---|', {
a: { reason: TransactionFailure.Timeout, ...failedTx }
});
});
});

it('emits at all relevant observable properties on transaction that failed to submit and merges reemit failures', async () => {
const outgoingTx = toOutgoingTx(queryTransactionsResult.pageResults[0]);
const outgoingTxReemit = toOutgoingTx(queryTransactionsResult.pageResults[1]);
Expand Down
Loading