Skip to content

Commit

Permalink
Simulate with max compute units, not u32::MAX (#2728)
Browse files Browse the repository at this point in the history
#### Problem

Looking through recent updates to the monorepo, this caught my eye: anza-xyz/agave#885

The simulation utility sets `u32::MAX`, which will fail once that feature is enabled.

#### Solution

Set `MAX_COMPUTE_UNITS`, hardcoded to 1,400,000, instead of `u32::MAX`.
  • Loading branch information
joncinque authored May 22, 2024
1 parent 3496637 commit f1e9ac2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
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

0 comments on commit f1e9ac2

Please sign in to comment.