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

refactor(experimental): add a function to assert a transaction has a blockhash lifetime #1908

Merged
merged 1 commit into from
Nov 30, 2023
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
72 changes: 71 additions & 1 deletion packages/transactions/src/__tests__/blockhash-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import 'test-matchers/toBeFrozenObject';
import { Encoder } from '@solana/codecs-core';
import { getBase58Encoder } from '@solana/codecs-strings';

import { Blockhash, ITransactionWithBlockhashLifetime, setTransactionLifetimeUsingBlockhash } from '../blockhash';
import {
assertIsTransactionWithBlockhashLifetime,
Blockhash,
ITransactionWithBlockhashLifetime,
setTransactionLifetimeUsingBlockhash,
} from '../blockhash';
import { ITransactionWithSignatures } from '../signatures';
import { BaseTransaction } from '../types';

Expand Down Expand Up @@ -109,6 +114,71 @@ describe('assertIsBlockhash()', () => {
});
});

describe('assertIsBlockhashLifetimeTransaction', () => {
beforeEach(() => {
// use real implementation
jest.mocked(getBase58Encoder).mockReturnValue(originalGetBase58Encoder);
});
it('throws for a transaction with no lifetime constraint', () => {
const transaction: BaseTransaction = {
instructions: [],
version: 0,
};
expect(() => assertIsTransactionWithBlockhashLifetime(transaction)).toThrow();
});
it('throws for a transaction with a durable nonce constraint', () => {
const transaction = {
instructions: [],
lifetimeConstraint: {
nonce: 'abcd',
},
version: 0,
} as unknown as BaseTransaction;
expect(() => assertIsTransactionWithBlockhashLifetime(transaction)).toThrow();
});
it('throws for a transaction with a blockhash but no lastValidBlockHeight in lifetimeConstraint', () => {
const transaction = {
instructions: [],
lifetimeConstraint: {
blockhash: '11111111111111111111111111111111',
},
version: 0,
} as unknown as BaseTransaction;
expect(() => assertIsTransactionWithBlockhashLifetime(transaction)).toThrow();
});
it('throws for a transaction with a lastValidBlockHeight but no blockhash in lifetimeConstraint', () => {
const transaction = {
instructions: [],
lifetimeConstraint: {
lastValidBlockHeight: 1234n,
},
version: 0,
} as unknown as BaseTransaction;
expect(() => assertIsTransactionWithBlockhashLifetime(transaction)).toThrow();
});
it('throws for a transaction with a blockhash lifetime but an invalid blockhash value', () => {
const transaction = {
instructions: [],
lifetimeConstraint: {
blockhash: 'not a valid blockhash value',
},
version: 0,
} as unknown as BaseTransaction;
expect(() => assertIsTransactionWithBlockhashLifetime(transaction)).toThrow();
});
it('does not throw for a transaction with a valid blockhash lifetime constraint', () => {
const transaction = {
instructions: [],
lifetimeConstraint: {
blockhash: '11111111111111111111111111111111',
lastValidBlockHeight: 1234n,
},
version: 0,
} as unknown as BaseTransaction;
expect(() => assertIsTransactionWithBlockhashLifetime(transaction)).not.toThrow();
});
});

describe('setTransactionLifetimeUsingBlockhash', () => {
let baseTx: BaseTransaction;
const BLOCKHASH_CONSTRAINT_A = {
Expand Down
48 changes: 41 additions & 7 deletions packages/transactions/src/__typetests__/transaction-typetests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Address } from '@solana/addresses';

import {
appendTransactionInstruction,
assertIsTransactionWithBlockhashLifetime,
assertTransactionIsFullySigned,
Blockhash,
IDurableNonceTransaction,
Expand All @@ -18,7 +19,7 @@ import {
import { createTransaction } from '../create-transaction';
import { ITransactionWithFeePayer, setTransactionFeePayer } from '../fee-payer';
import { CompiledMessage, compileMessage } from '../message';
import { Transaction } from '../types';
import { BaseTransaction, Transaction } from '../types';
import { getUnsignedTransaction } from '../unsigned-transaction';

const mockFeePayer = null as unknown as Address<'feePayer'>;
Expand Down Expand Up @@ -538,10 +539,43 @@ async () => {
} & ITransactionWithFeePayer<'feePayer'> &
ITransactionWithSignatures;

// assertTransactionIsFullySigned
const transaction = {} as Parameters<typeof assertTransactionIsFullySigned>[0];
// @ts-expect-error Should not be fully signed
transaction satisfies IFullySignedTransaction;
assertTransactionIsFullySigned(transaction);
transaction satisfies IFullySignedTransaction;
{
// assertTransactionIsFullySigned
const transaction = {} as Parameters<typeof assertTransactionIsFullySigned>[0];
// @ts-expect-error Should not be fully signed
transaction satisfies IFullySignedTransaction;
assertTransactionIsFullySigned(transaction);
transaction satisfies IFullySignedTransaction;
}

{
// assertIsBlockhashLifetimeTransaction
const transaction = null as unknown as BaseTransaction;
// @ts-expect-error Should not be blockhash lifetime
transaction satisfies ITransactionWithBlockhashLifetime;
// @ts-expect-error Should not satisfy has blockhash
transaction satisfies {
lifetimeConstraint: {
blockhash: Blockhash;
};
};
// @ts-expect-error Should not satisfy has lastValidBlockHeight
transaction satisfies {
lifetimeConstraint: {
lastValidBlockHeight: bigint;
};
};
assertIsTransactionWithBlockhashLifetime(transaction);
transaction satisfies ITransactionWithBlockhashLifetime;
transaction satisfies {
lifetimeConstraint: {
blockhash: Blockhash;
};
};
transaction satisfies {
lifetimeConstraint: {
lastValidBlockHeight: bigint;
};
};
}
};
25 changes: 25 additions & 0 deletions packages/transactions/src/blockhash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@ export function assertIsBlockhash(putativeBlockhash: string): asserts putativeBl
}
}

function isTransactionWithBlockhashLifetime(
transaction: BaseTransaction | (BaseTransaction & ITransactionWithBlockhashLifetime)
): transaction is BaseTransaction & ITransactionWithBlockhashLifetime {
const lifetimeConstraintShapeMatches =
'lifetimeConstraint' in transaction &&
typeof transaction.lifetimeConstraint.blockhash === 'string' &&
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use assertIsBlockhash here, but meh.

typeof transaction.lifetimeConstraint.lastValidBlockHeight === 'bigint';
if (!lifetimeConstraintShapeMatches) return false;
try {
assertIsBlockhash(transaction.lifetimeConstraint.blockhash);
return true;
} catch {
return false;
}
}

export function assertIsTransactionWithBlockhashLifetime(
transaction: BaseTransaction | (BaseTransaction & ITransactionWithBlockhashLifetime)
): asserts transaction is BaseTransaction & ITransactionWithBlockhashLifetime {
if (!isTransactionWithBlockhashLifetime(transaction)) {
// TODO: Coded error.
throw new Error('Transaction does not have a blockhash lifetime');
}
}

export function setTransactionLifetimeUsingBlockhash<TTransaction extends BaseTransaction & IDurableNonceTransaction>(
blockhashLifetimeConstraint: BlockhashLifetimeConstraint,
transaction: TTransaction | (TTransaction & ITransactionWithSignatures)
Expand Down