8000 Allow compiled messages with no lifetime by lorisleiva · Pull Request #587 · anza-xyz/kit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow compiled messages with no lifetime #587

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

Closed
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
6 changes: 6 additions & 0 deletions .changeset/fancy-cameras-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@solana/transaction-messages': minor
'@solana/kit': minor
---

Allow `CompiledTransactionMessages` to have no lifetime constraints
3 changes: 3 additions & 0 deletions examples/deserialize-transaction/src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ const decompiledTransactionMessage = await decompileTransactionMessageFetchingLo
log.info(`[step 3] The transaction fee payer is ${decompiledTransactionMessage.feePayer.address}`);

// And the lifetime constraint:
if (!('lifetimeConstraint' in decompiledTransactionMessage)) {
throw new Error('We expect a lifetime');
}
Comment on lines +318 to +320
Copy link
Member Author

Choose a reason for hiding this comment

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

This showcases how this PR affects the developer experience. Here we know there will always be a lifetime constraint since we're fetching a transaction that landed so we need to assert the correct type.

log.info(decompiledTransactionMessage.lifetimeConstraint, '[step 3] The transaction lifetime constraint');

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export async function decompileTransactionMessageFetchingLookupTables(
compiledTransactionMessage: CompiledTransactionMessage,
rpc: Rpc<GetMultipleAccountsApi>,
config?: DecompileTransactionMessageFetchingLookupTablesConfig,
): Promise<BaseTransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithLifetime> {
): Promise<
| (BaseTransactionMessage & TransactionMessageWithFeePayer)
| (BaseTransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithLifetime)
> {
const lookupTables =
'addressTableLookups' in compiledTransactionMessage &&
compiledTransactionMessage.addressTableLookups !== undefined &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { AccountLookupMeta, AccountMeta, AccountRole, Instruction } from '@solan
import { CompiledTransactionMessage } from '../compile';
import { decompileTransactionMessage } from '../decompile-message';
import { Nonce } from '../durable-nonce';
import { TransactionMessageWithLifetime } from '../lifetime';

describe('decompileTransactionMessage', () => {
const U64_MAX = 2n ** 64n - 1n;
Expand All @@ -36,7 +37,7 @@ describe('decompileTransactionMessage', () => {

expect(transaction.version).toBe(0);
expect(transaction.feePayer.address).toEqual(feePayer);
expect(transaction.lifetimeConstraint).toEqual({
expect((transaction as TransactionMessageWithLifetime).lifetimeConstraint).toEqual({
blockhash,
lastValidBlockHeight: U64_MAX,
});
Expand All @@ -56,7 +57,7 @@ describe('decompileTransactionMessage', () => {
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.lifetimeConstraint).toBeFrozenObject();
expect((transaction as TransactionMessageWithLifetime).lifetimeConstraint).toBeFrozenObject();
});

it('converts a transaction with version legacy', () => {
Expand Down Expand Up @@ -244,7 +245,7 @@ describe('decompileTransactionMessage', () => {
};

const transaction = decompileTransactionMessage(compiledTransaction, { lastValidBlockHeight: 100n });
expect(transaction.lifetimeConstraint).toEqual({
expect((transaction as TransactionMessageWithLifetime).lifetimeConstraint).toEqual({
blockhash,
lastValidBlockHeight: 100n,
});
Expand Down Expand Up @@ -358,7 +359,7 @@ describe('decompileTransactionMessage', () => {

expect(transaction.instructions).toStrictEqual([expectedInstruction]);
expect(transaction.feePayer.address).toStrictEqual(nonceAuthorityAddress);
expect(transaction.lifetimeConstraint).toStrictEqual({ nonce });
expect((transaction as TransactionMessageWithLifetime).lifetimeConstraint).toStrictEqual({ nonce });
});

it('freezes the nonce lifetime constraint', () => {
Expand Down Expand Up @@ -394,7 +395,7 @@ describe('decompileTransactionMessage', () => {
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.lifetimeConstraint).toBeFrozenObject();
expect((transaction as TransactionMessageWithLifetime).lifetimeConstraint).toBeFrozenObject();
});

it('converts a transaction with one instruction which is advance nonce (fee payer is not nonce authority)', () => {
Expand Down Expand Up @@ -534,7 +535,7 @@ describe('decompileTransactionMessage', () => {
];

expect(transaction.instructions).toStrictEqual(expectedInstructions);
expect(transaction.lifetimeConstraint).toStrictEqual({ nonce });
expect((transaction as TransactionMessageWithLifetime).lifetimeConstraint).toStrictEqual({ nonce });
});

it('freezes the instructions within the transaction', () => {
Expand Down
15 changes: 13 additions & 2 deletions packages/transaction-messages/src/codecs/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ function getPreludeStructEncoderTuple() {
['version', getTransactionVersionEncoder()],
['header', getMessageHeaderEncoder()],
['staticAccounts', getArrayEncoder(getAddressEncoder(), { size: getShortU16Encoder() })],
['lifetimeToken', fixEncoderSize(getBase58Encoder(), 32)],
[
'lifetimeToken',
transformEncoder(
fixEncoderSize(getBase58Encoder(), 32),
(lifetimeToken: string | undefined): string => lifetimeToken ?? '11111111111111111111111111111111',
),
],
['instructions', getArrayEncoder(getInstructionEncoder(), { size: getShortU16Encoder() })],
] as const;
}
Expand All @@ -59,7 +65,12 @@ function getPreludeStructDecoderTuple() {
['version', getTransactionVersionDecoder() as Decoder<number>],
['header', getMessageHeaderDecoder()],
['staticAccounts', getArrayDecoder(getAddressDecoder(), { size: getShortU16Decoder() })],
['lifetimeToken', fixDecoderSize(getBase58Decoder(), 32)],
[
'lifetimeToken',
transformDecoder(fixDecoderSize(getBase58Decoder(), 32), (lifetimeToken: string): string | undefined =>
lifetimeToken === '11111111111111111111111111111111' ? undefined : lifetimeToken,
),
],
['instructions', getArrayDecoder(getInstructionDecoder(), { size: getShortU16Decoder() })],
['addressTableLookups', getAddressTableLookupArrayDecoder()],
] as const;
Expand Down
14 changes: 3 additions & 11 deletions packages/transaction-messages/src/compile/message.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { TransactionMessageWithBlockhashLifetime } from '../blockhash';
import { TransactionMessageWithFeePayer } from '../fee-payer';
import { TransactionMessageWithLifetime } from '../lifetime';
import { BaseTransactionMessage } from '../transaction-message';
Expand All @@ -23,7 +22,7 @@ type BaseCompiledTransactionMessage = Readonly<{
* In the case of a transaction message with a nonce lifetime constraint, this will be the value
* of the nonce itself. In all other cases this will be a recent blockhash.
*/
lifetimeToken: ReturnType<typeof getCompiledLifetimeToken>;
lifetimeToken?: ReturnType<typeof getCompiledLifetimeToken>;
/** A list of addresses indicating which accounts to load */
staticAccounts: ReturnType<typeof getCompiledStaticAccounts>;
}>;
Expand All @@ -49,11 +48,6 @@ type VersionedCompiledTransactionMessage = BaseCompiledTransactionMessage &
version: number;
}>;

const EMPTY_BLOCKHASH_LIFETIME_CONSTRAINT = {
blockhash: '11111111111111111111111111111111',
lastValidBlockHeight: 0n,
} as TransactionMessageWithBlockhashLifetime['lifetimeConstraint'];

/**
* Converts the type of transaction message data structure that you create in your application to
* the type of transaction message data structure that can be encoded for execution on the network.
Expand All @@ -79,16 +73,14 @@ export function compileTransactionMessage(
transactionMessage.instructions,
);
const orderedAccounts = getOrderedAccountsFromAddressMap(addressMap);
const lifetimeConstraint =
(transactionMessage as Partial<TransactionMessageWithLifetime>).lifetimeConstraint ??
EMPTY_BLOCKHASH_LIFETIME_CONSTRAINT;
const lifetimeConstraint = (transactionMessage as Partial<TransactionMessageWithLifetime>).lifetimeConstraint;
return {
...(transactionMessage.version !== 'legacy'
? { addressTableLookups: getCompiledAddressTableLookups(orderedAccounts) }
: null),
...(lifetimeConstraint ? { lifetimeToken: getCompiledLifetimeToken(lifetimeConstraint) } : null),
header: getCompiledMessageHeader(orderedAccounts),
instructions: getCompiledInstructions(transactionMessage.instructions, orderedAccounts),
lifetimeToken: getCompiledLifetimeToken(lifetimeConstraint),
staticAccounts: getCompiledStaticAccounts(orderedAccounts),
version: transactionMessage.version,
};
Expand Down
24 changes: 15 additions & 9 deletions packages/transaction-messages/src/decompile-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ export type DecompileTransactionMessageConfig = {
export function decompileTransactionMessage(
compiledTransactionMessage: CompiledTransactionMessage,
config?: DecompileTransactionMessageConfig,
): BaseTransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithLifetime {
):
| (BaseTransactionMessage & TransactionMessageWithFeePayer)
| (BaseTransactionMessage & TransactionMessageWithFeePayer & TransactionMessageWithLifetime) {
const feePayer = compiledTransactionMessage.staticAccounts[0];
if (!feePayer) {
throw new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_FEE_PAYER_MISSING);
Expand All @@ -239,11 +241,13 @@ export function decompileTransactionMessage(
);

const firstInstruction = instructions[0];
const lifetimeConstraint = getLifetimeConstraint(
compiledTransactionMessage.lifetimeToken,
firstInstruction,
config?.lastValidBlockHeight,
);
const lifetimeConstraint = compiledTransactionMessage.lifetimeToken
? getLifetimeConstraint(
compiledTransactionMessage.lifetimeToken,
firstInstruction,
config?.lastValidBlockHeight,
)
: undefined;

return pipe(
createTransactionMessage({ version: compiledTransactionMessage.version as TransactionVersion }),
Expand All @@ -253,9 +257,11 @@ export function decompileTransactionMessage(
(acc, instruction) => appendTransactionMessageInstruction(instruction, acc),
m as BaseTransactionMessage & TransactionMessageWithFeePayer,
),
m =>
'blockhash' in lifetimeConstraint
m => {
if (!lifetimeConstraint) return m;
return 'blockhash' in lifetimeConstraint
? setTransactionMessageLifetimeUsingBlockhash(lifetimeConstraint, m)
: setTransactionMessageLifetimeUsingDurableNonce(lifetimeConstraint, m),
: setTransactionMessageLifetime 3FF9 UsingDurableNonce(lifetimeConstraint, m);
},
);
}
0