Skip to content

Commit

Permalink
Freeze instructions and lifetime in transaction message objects (#2607)
Browse files Browse the repository at this point in the history
* freeze the instructions array when we append/prepend instructions

* freeze the instructions array when we create a transaction message

* freeze the instructions array and lifetime when adding a durable nonce

* freeze the lifetime constraint when adding a blockhash

* freeze instructions when decompiling a transaction message

* add a changeset for transaction-messages
  • Loading branch information
mcintyre94 authored May 5, 2024
1 parent 62e8580 commit 3d90241
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 42 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-radios-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/transaction-messages": patch
---

Freeze the instructions and lifetimeConstraint fields within transaction messages
7 changes: 7 additions & 0 deletions packages/transaction-messages/src/__tests__/blockhash-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,11 @@ describe('setTransactionMessageLifetimeUsingBlockhash', () => {
);
expect(txWithBlockhashLifetimeConstraint).toBeFrozenObject();
});
it('freezes the blockhash constraint', () => {
const txWithBlockhashLifetimeConstraint = setTransactionMessageLifetimeUsingBlockhash(
BLOCKHASH_CONSTRAINT_A,
baseTx,
);
expect(txWithBlockhashLifetimeConstraint.lifetimeConstraint).toBeFrozenObject();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ describe('createTransactionMessage', () => {
const tx = createTransactionMessage({ version: 0 });
expect(tx).toBeFrozenObject();
});
it('freezes the instructions array', () => {
const tx = createTransactionMessage({ version: 0 });
expect(tx.instructions).toBeFrozenObject();
});
});
221 changes: 221 additions & 0 deletions packages/transaction-messages/src/__tests__/decompile-message-test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import '@solana/test-matchers/toBeFrozenObject';

import { Address } from '@solana/addresses';
import {
SOLANA_ERROR__TRANSACTION__FAILED_TO_DECOMPILE_ADDRESS_LOOKUP_TABLE_CONTENTS_MISSING,
Expand Down Expand Up @@ -40,6 +42,23 @@ describe('decompileTransactionMessage', () => {
});
});

it('freezes the blockhash lifetime constraint', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 0,
numReadonlySignerAccounts: 0,
numSignerAccounts: 1,
},
instructions: [],
lifetimeToken: blockhash,
staticAccounts: [feePayer],
version: 0,
};

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

it('converts a transaction with version legacy', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
Expand Down Expand Up @@ -140,6 +159,42 @@ describe('decompileTransactionMessage', () => {
expect(transaction.instructions).toStrictEqual([expectedInstruction]);
});

it('freezes the instruction accounts', () => {
const programAddress = 'HZMKVnRrWLyQLwPLTTLKtY7ET4Cf7pQugrTr9eTBrpsf' as Address;

const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 2, // 1 passed into instruction + 1 program
numReadonlySignerAccounts: 1,
numSignerAccounts: 3, // fee payer + 2 passed into instruction
},
instructions: [
{
accountIndices: [1, 2, 3, 4],
data: new Uint8Array([0, 1, 2, 3, 4]),
programAddressIndex: 5,
},
],
lifetimeToken: blockhash,
staticAccounts: [
// writable signers
feePayer,
'H4RdPRWYk3pKw2CkNznxQK6J6herjgQke2pzFJW4GC6x' as Address,
// read-only signers
'G35QeFd4jpXWfRkuRKwn8g4vYrmn8DWJ5v88Kkpd8z1V' as Address,
// writable non-signers
'3LeBzRE9Yna5zi9R8vdT3MiNQYuEp4gJgVyhhwmqfCtd' as Address,
// read-only non-signers
'8kud9bpNvfemXYdTFjs5cZ8fZinBkx8JAnhVmRwJZk5e' as Address,
programAddress,
],
version: 0,
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.instructions[0].accounts).toBeFrozenObject();
});

it('converts a transaction with multiple instructions', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
Expand Down Expand Up @@ -194,6 +249,46 @@ describe('decompileTransactionMessage', () => {
lastValidBlockHeight: 100n,
});
});

it('freezes the instructions within the transaction', () => {
const programAddress = 'HZMKVnRrWLyQLwPLTTLKtY7ET4Cf7pQugrTr9eTBrpsf' as Address;

const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 1,
// fee payer
numReadonlySignerAccounts: 0,
numSignerAccounts: 1, // program address
},
instructions: [{ programAddressIndex: 1 }],
lifetimeToken: blockhash,
staticAccounts: [feePayer, programAddress],
version: 0,
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.instructions[0]).toBeFrozenObject();
});

it('freezes the instructions array', () => {
const programAddress = 'HZMKVnRrWLyQLwPLTTLKtY7ET4Cf7pQugrTr9eTBrpsf' as Address;

const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 1,
// fee payer
numReadonlySignerAccounts: 0,
numSignerAccounts: 1, // program address
},
instructions: [{ programAddressIndex: 1 }],
lifetimeToken: blockhash,
staticAccounts: [feePayer, programAddress],
version: 0,
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.instructions).toBeFrozenObject();
});
});

describe('for a transaction with a durable nonce lifetime', () => {
Expand Down Expand Up @@ -266,6 +361,42 @@ describe('decompileTransactionMessage', () => {
expect(transaction.lifetimeConstraint).toStrictEqual({ nonce });
});

it('freezes the nonce lifetime constraint', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 2, // recent blockhashes sysvar, system program
numReadonlySignerAccounts: 0, // nonce authority already added as fee payer
numSignerAccounts: 1, // fee payer and nonce authority are the same account
},
instructions: [
{
accountIndices: [
1, // nonce account address
3, // recent blockhashes sysvar
0, // nonce authority address
],
data: new Uint8Array([4, 0, 0, 0]),
programAddressIndex: 2,
},
],
lifetimeToken: nonce,
staticAccounts: [
// writable signers
nonceAuthorityAddress,
// no read-only signers
// writable non-signers
nonceAccountAddress,
// read-only non-signers
systemProgramAddress,
recentBlockhashesSysvarAddress,
],
version: 0,
};

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

it('converts a transaction with one instruction which is advance nonce (fee payer is not nonce authority)', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
Expand Down Expand Up @@ -405,6 +536,96 @@ describe('decompileTransactionMessage', () => {
expect(transaction.instructions).toStrictEqual(expectedInstructions);
expect(transaction.lifetimeConstraint).toStrictEqual({ nonce });
});

it('freezes the instructions within the transaction', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 4, // recent blockhashes sysvar, system program, 2 other program addresses
numReadonlySignerAccounts: 0, // nonce authority already added as fee payer
numSignerAccounts: 1, // fee payer and nonce authority are the same account
},
instructions: [
{
accountIndices: [
1, // nonce account address
3, // recent blockhashes sysvar
0, // nonce authority address
],
data: new Uint8Array([4, 0, 0, 0]),
programAddressIndex: 2,
},
{
accountIndices: [0, 1],
data: new Uint8Array([1, 2, 3, 4]),
programAddressIndex: 4,
},
{ programAddressIndex: 5 },
],
lifetimeToken: nonce,
staticAccounts: [
// writable signers
nonceAuthorityAddress,
// no read-only signers
// writable non-signers
nonceAccountAddress,
// read-only non-signers
systemProgramAddress,
recentBlockhashesSysvarAddress,
'3hpECiFPtnyxoWqWqcVyfBUDhPKSZXWDduNXFywo8ncP' as Address,
'Cmqw16pVQvmW1b7Ek1ioQ5Ggf1PaoXi5XxsK9iVSbRKC' as Address,
],
version: 0,
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.instructions[0]).toBeFrozenObject();
expect(transaction.instructions[1]).toBeFrozenObject();
expect(transaction.instructions[2]).toBeFrozenObject();
});

it('freezes the instructions array', () => {
const compiledTransaction: CompiledTransactionMessage = {
header: {
numReadonlyNonSignerAccounts: 4, // recent blockhashes sysvar, system program, 2 other program addresses
numReadonlySignerAccounts: 0, // nonce authority already added as fee payer
numSignerAccounts: 1, // fee payer and nonce authority are the same account
},
instructions: [
{
accountIndices: [
1, // nonce account address
3, // recent blockhashes sysvar
0, // nonce authority address
],
data: new Uint8Array([4, 0, 0, 0]),
programAddressIndex: 2,
},
{
accountIndices: [0, 1],
data: new Uint8Array([1, 2, 3, 4]),
programAddressIndex: 4,
},
{ programAddressIndex: 5 },
],
lifetimeToken: nonce,
staticAccounts: [
// writable signers
nonceAuthorityAddress,
// no read-only signers
// writable non-signers
nonceAccountAddress,
// read-only non-signers
systemProgramAddress,
recentBlockhashesSysvarAddress,
'3hpECiFPtnyxoWqWqcVyfBUDhPKSZXWDduNXFywo8ncP' as Address,
'Cmqw16pVQvmW1b7Ek1ioQ5Ggf1PaoXi5XxsK9iVSbRKC' as Address,
],
version: 0,
};

const transaction = decompileTransactionMessage(compiledTransaction);
expect(transaction.instructions).toBeFrozenObject();
});
});

describe('for a transaction with address lookup tables', () => {
Expand Down
70 changes: 56 additions & 14 deletions packages/transaction-messages/src/__tests__/durable-nonce-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('setTransactionMessageLifetimeUsingDurableNonce', () => {
);
expect(durableNonceTxWithConstraintA).toHaveProperty('lifetimeConstraint', { nonce: NONCE_CONSTRAINT_A.nonce });
});
it('appends an `AdvanceNonceAccount` instruction', () => {
it('prepends an `AdvanceNonceAccount` instruction', () => {
const durableNonceTxWithConstraintA = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_A,
baseTx,
Expand All @@ -199,6 +199,13 @@ describe('setTransactionMessageLifetimeUsingDurableNonce', () => {
baseTx.instructions[0],
]);
});
it('freezes the prepended `AdvanceNonceAccount` instruction', () => {
const durableNonceTxWithConstraintA = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_A,
baseTx,
);
expect(durableNonceTxWithConstraintA.instructions[0]).toBeFrozenObject();
});
describe('given a transaction with an advance nonce account instruction but no nonce lifetime constraint', () => {
it('does not modify an `AdvanceNonceAccount` instruction if the existing one matches the constraint added', () => {
const instruction = createMockAdvanceNonceAccountInstruction(NONCE_CONSTRAINT_A);
Expand All @@ -213,19 +220,39 @@ describe('setTransactionMessageLifetimeUsingDurableNonce', () => {
);
expect(durableNonceTxWithConstraintA.instructions).toEqual([instruction, baseTx.instructions[0]]);
});
it('replaces an `AdvanceNonceAccount` instruction if the existing one does not match the constraint added', () => {
const transaction: BaseTransactionMessage = {
...baseTx,
instructions: [createMockAdvanceNonceAccountInstruction(NONCE_CONSTRAINT_B), baseTx.instructions[0]],
};
const durableNonceTxWithConstraintA = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_A,
transaction,
);
expect(durableNonceTxWithConstraintA.instructions).toEqual([
createMockAdvanceNonceAccountInstruction(NONCE_CONSTRAINT_A),
baseTx.instructions[0],
]);
describe('when the existing `AdvanceNonceAccount` instruction does not match the constraint added', () => {
it('replaces the existing instruction', () => {
const transaction: BaseTransactionMessage = {
...baseTx,
instructions: [
createMockAdvanceNonceAccountInstruction(NONCE_CONSTRAINT_B),
baseTx.instructions[0],
],
};
const durableNonceTxWithConstraintA = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_A,
transaction,
);
expect(durableNonceTxWithConstraintA.instructions).toEqual([
createMockAdvanceNonceAccountInstruction(NONCE_CONSTRAINT_A),
baseTx.instructions[0],
]);
});

it('freezes the replacement instruction', () => {
const transaction: BaseTransactionMessage = {
...baseTx,
instructions: [
createMockAdvanceNonceAccountInstruction(NONCE_CONSTRAINT_B),
baseTx.instructions[0],
],
};
const durableNonceTxWithConstraintA = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_A,
transaction,
);
expect(durableNonceTxWithConstraintA.instructions[0]).toBeFrozenObject();
});
});
});
describe('given a durable nonce transaction', () => {
Expand Down Expand Up @@ -260,6 +287,13 @@ describe('setTransactionMessageLifetimeUsingDurableNonce', () => {
durableNonceTxWithConstraintA.instructions[1],
]);
});
it('freezes the replacement advance nonce account instruction', () => {
const durableNonceTxWithConstraintB = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_B,
durableNonceTxWithConstraintA,
);
expect(durableNonceTxWithConstraintB.instructions[0]).toBeFrozenObject();
});
it('returns the original transaction when trying to set the same durable nonce constraint again', () => {
const txWithSameNonceLifetimeConstraint = setTransactionMessageLifetimeUsingDurableNonce(
NONCE_CONSTRAINT_A,
Expand All @@ -272,4 +306,12 @@ describe('setTransactionMessageLifetimeUsingDurableNonce', () => {
const durableNonceTx = setTransactionMessageLifetimeUsingDurableNonce(NONCE_CONSTRAINT_A, baseTx);
expect(durableNonceTx).toBeFrozenObject();
});
it('freezes the instructions array', () => {
const durableNonceTx = setTransactionMessageLifetimeUsingDurableNonce(NONCE_CONSTRAINT_A, baseTx);
expect(durableNonceTx.instructions).toBeFrozenObject();
});
it('freezes the nonce lifetime', () => {
const durableNonceTx = setTransactionMessageLifetimeUsingDurableNonce(NONCE_CONSTRAINT_A, baseTx);
expect(durableNonceTx.lifetimeConstraint).toBeFrozenObject();
});
});
Loading

0 comments on commit 3d90241

Please sign in to comment.