Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Simulate with max compute units, not u32::MAX #2728

Merged
merged 2 commits into from
May 22, 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
5 changes: 5 additions & 0 deletions .changeset/chilly-seas-act.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solana/web3.js-experimental": patch
---

Simulate with the maximum quantity of compute units (1.4M) instead of `u32::MAX`
12 changes: 7 additions & 5 deletions packages/library/src/__tests__/compute-limit-internal-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPOR
// prettier-ignore
new Uint8Array([
0x02, // SetComputeUnitLimit instruction inde
0xff, 0xff, 0xff, 0xff, // U32::MAX
0xc0, 0x5c, 0x15, 0x00, // 1,400,000, MAX_COMPUTE_UNITS
]),
programAddress: 'ComputeBudget111111111111111111111111111111',
},
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPOR
transactionMessage.instructions[0],
{
...transactionMessage.instructions[1],
data: new Uint8Array([0x02, 0xff, 0xff, 0xff, 0xff]), // Replaced with U32::MAX
data: new Uint8Array([0x02, 0xc0, 0x5c, 0x15, 0x00]), // Replaced with MAX_COMPUTE_UNITS
},
transactionMessage.instructions[2],
],
Expand Down Expand Up @@ -233,14 +233,16 @@ describe('getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPOR
});
await expect(estimatePromise).resolves.toBe(42);
});
it('caps the estimated compute units to U32::MAX', async () => {
it('caps the estimated compute units to MAX_COMPUTE_UNITS of 1.4M', async () => {
expect.assertions(1);
sendSimulateTransactionRequest.mockResolvedValue({ value: { unitsConsumed: 4294967295n /* U32::MAX + 1 */ } });
sendSimulateTransactionRequest.mockResolvedValue({
value: { unitsConsumed: 1400000n /* MAX_COMPUTE_UNITS */ },
});
const estimatePromise = getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({
rpc,
transactionMessage: mockTransactionMessage,
});
await expect(estimatePromise).resolves.toBe(4294967295 /* U32::MAX */);
await expect(estimatePromise).resolves.toBe(1400000 /* MAX_COMPUTE_UNITS */);
});
it('throws with the cause when simulation fails', async () => {
expect.assertions(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/library/src/compute-limit-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export async function getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_
*/
const existingSetComputeUnitLimitInstructionIndex =
transactionMessage.instructions.findIndex(isSetComputeLimitInstruction);
const maxComputeUnitLimitInstruction = createComputeUnitLimitInstruction(4_294_967_295 /* U32::MAX */);
const maxComputeUnitLimitInstruction = createComputeUnitLimitInstruction(1_400_000 /* MAX_COMPUTE_UNIT_LIMIT */);
if (existingSetComputeUnitLimitInstructionIndex === -1) {
compilableTransactionMessage = appendTransactionMessageInstruction(
maxComputeUnitLimitInstruction,
Expand Down