|
| 1 | +import { Address } from '@solana/addresses'; |
| 2 | +import { |
| 3 | + SOLANA_ERROR__INSTRUCTION_ERROR__INSUFFICIENT_FUNDS, |
| 4 | + SOLANA_ERROR__TRANSACTION__FAILED_TO_ESTIMATE_COMPUTE_LIMIT, |
| 5 | + SolanaError, |
| 6 | +} from '@solana/errors'; |
| 7 | +import { AccountRole } from '@solana/instructions'; |
| 8 | +import { Rpc, SimulateTransactionApi } from '@solana/rpc'; |
| 9 | +import { Blockhash } from '@solana/rpc-types'; |
| 10 | +import { ITransactionMessageWithFeePayer, Nonce, TransactionMessage } from '@solana/transaction-messages'; |
| 11 | + |
| 12 | +import { getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT } from '../compute-limit-internal'; |
| 13 | + |
| 14 | +const FOREVER_PROMISE = new Promise(() => { |
| 15 | + /* never resolve */ |
| 16 | +}); |
| 17 | + |
| 18 | +const MOCK_BLOCKHASH_LIFETIME_CONSTRAINT = { |
| 19 | + blockhash: 'GNtuHnNyW68wviopST3ki37Afv7LPphxfSwiHAkX5Q9H' as Blockhash, |
| 20 | + lastValidBlockHeight: 0n, |
| 21 | +} as const; |
| 22 | + |
| 23 | +describe('getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT', () => { |
| 24 | + let sendSimulateTransactionRequest: jest.Mock; |
| 25 | + let mockTransactionMessage: ITransactionMessageWithFeePayer & TransactionMessage; |
| 26 | + let rpc: Rpc<SimulateTransactionApi>; |
| 27 | + let simulateTransaction: jest.Mock; |
| 28 | + beforeEach(() => { |
| 29 | + mockTransactionMessage = { |
| 30 | + feePayer: '7U8VWgTUucttJPt5Bbkt48WknWqRGBfstBt8qqLHnfPT' as Address, |
| 31 | + instructions: [], |
| 32 | + version: 0, |
| 33 | + }; |
| 34 | + sendSimulateTransactionRequest = jest.fn().mockReturnValue(FOREVER_PROMISE); |
| 35 | + simulateTransaction = jest.fn().mockReturnValue({ send: sendSimulateTransactionRequest }); |
| 36 | + rpc = { |
| 37 | + simulateTransaction, |
| 38 | + }; |
| 39 | + }); |
| 40 | + it('aborts the `simulateTransaction` request when aborted', () => { |
| 41 | + const abortController = new AbortController(); |
| 42 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 43 | + abortSignal: abortController.signal, |
| 44 | + rpc, |
| 45 | + transactionMessage: { |
| 46 | + ...mockTransactionMessage, |
| 47 | + lifetimeConstraint: MOCK_BLOCKHASH_LIFETIME_CONSTRAINT, |
| 48 | + }, |
| 49 | + }); |
| 50 | + expect(sendSimulateTransactionRequest).toHaveBeenCalledWith({ |
| 51 | + abortSignal: expect.objectContaining({ aborted: false }), |
| 52 | + }); |
| 53 | + abortController.abort(); |
| 54 | + expect(sendSimulateTransactionRequest).toHaveBeenCalledWith({ |
| 55 | + abortSignal: expect.objectContaining({ aborted: true }), |
| 56 | + }); |
| 57 | + }); |
| 58 | + it('passes the expected basic input to the simulation request', () => { |
| 59 | + const transactionMessage = { |
| 60 | + ...mockTransactionMessage, |
| 61 | + lifetimeConstraint: MOCK_BLOCKHASH_LIFETIME_CONSTRAINT, |
| 62 | + }; |
| 63 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 64 | + commitment: 'finalized', |
| 65 | + minContextSlot: 42n, |
| 66 | + rpc, |
| 67 | + transactionMessage, |
| 68 | + }); |
| 69 | + expect(simulateTransaction).toHaveBeenCalledWith( |
| 70 | + expect.any(String), |
| 71 | + expect.objectContaining({ |
| 72 | + commitment: 'finalized', |
| 73 | + encoding: 'base64', |
| 74 | + minContextSlot: 42n, |
| 75 | + sigVerify: false, |
| 76 | + }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + it('appends a set compute unit limit instruction when one does not exist', async () => { |
| 80 | + expect.assertions(1); |
| 81 | + await jest.isolateModulesAsync(async () => { |
| 82 | + jest.mock('@solana/transactions'); |
| 83 | + const [ |
| 84 | + { compileTransaction }, |
| 85 | + { getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT }, |
| 86 | + ] = await Promise.all([ |
| 87 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 88 | + // @ts-ignore |
| 89 | + import('@solana/transactions'), |
| 90 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 91 | + // @ts-ignore |
| 92 | + import('../compute-limit-internal'), |
| 93 | + ]); |
| 94 | + const transactionMessage = { |
| 95 | + ...mockTransactionMessage, // No `SetComputeUnitLimit` instruction |
| 96 | + lifetimeConstraint: MOCK_BLOCKHASH_LIFETIME_CONSTRAINT, |
| 97 | + }; |
| 98 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 99 | + rpc, |
| 100 | + transactionMessage, |
| 101 | + }); |
| 102 | + expect(compileTransaction).toHaveBeenCalledWith({ |
| 103 | + ...transactionMessage, |
| 104 | + instructions: [ |
| 105 | + ...transactionMessage.instructions, |
| 106 | + { |
| 107 | + data: |
| 108 | + // prettier-ignore |
| 109 | + new Uint8Array([ |
| 110 | + 0x02, // SetComputeUnitLimit instruction inde |
| 111 | + 0xff, 0xff, 0xff, 0xff, // U32::MAX |
| 112 | + ]), |
| 113 | + programAddress: 'ComputeBudget111111111111111111111111111111', |
| 114 | + }, |
| 115 | + ], |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + it('replaces the existing set compute unit limit instruction when one exists', async () => { |
| 120 | + expect.assertions(1); |
| 121 | + await jest.isolateModulesAsync(async () => { |
| 122 | + jest.mock('@solana/transactions'); |
| 123 | + const [ |
| 124 | + { compileTransaction }, |
| 125 | + { getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT }, |
| 126 | + ] = await Promise.all([ |
| 127 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 128 | + // @ts-ignore |
| 129 | + import('@solana/transactions'), |
| 130 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 131 | + // @ts-ignore |
| 132 | + import('../compute-limit-internal'), |
| 133 | + ]); |
| 134 | + const transactionMessage = { |
| 135 | + ...mockTransactionMessage, |
| 136 | + instructions: [ |
| 137 | + { programAddress: '4Kk4nA3F2nWHCcuyT8nR6oF7HQUQHmmzAVD5k8FQPKB2' as Address }, |
| 138 | + { |
| 139 | + data: |
| 140 | + // prettier-ignore |
| 141 | + new Uint8Array([ |
| 142 | + 0x02, // SetComputeUnitLimit instruction inde |
| 143 | + 0x01, 0x02, 0x03, 0x04, // ComputeUnits(u32) |
| 144 | + ]), |
| 145 | + programAddress: 'ComputeBudget111111111111111111111111111111' as Address, |
| 146 | + }, |
| 147 | + { programAddress: '4Kk4nA3F2nWHCcuyT8nR6oF7HQUQHmmzAVD5k8FQPKB2' as Address }, |
| 148 | + ], |
| 149 | + lifetimeConstraint: MOCK_BLOCKHASH_LIFETIME_CONSTRAINT, |
| 150 | + }; |
| 151 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 152 | + rpc, |
| 153 | + transactionMessage, |
| 154 | + }); |
| 155 | + expect(compileTransaction).toHaveBeenCalledWith( |
| 156 | + expect.objectContaining({ |
| 157 | + instructions: [ |
| 158 | + transactionMessage.instructions[0], |
| 159 | + { |
| 160 | + ...transactionMessage.instructions[1], |
| 161 | + data: new Uint8Array([0x02, 0xff, 0xff, 0xff, 0xff]), // Replaced with U32::MAX |
| 162 | + }, |
| 163 | + transactionMessage.instructions[2], |
| 164 | + ], |
| 165 | + }), |
| 166 | + ); |
| 167 | + }); |
| 168 | + }); |
| 169 | + it('does not ask for a replacement blockhash when the transaction message is a durable nonce transaction', () => { |
| 170 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 171 | + rpc, |
| 172 | + transactionMessage: { |
| 173 | + ...mockTransactionMessage, |
| 174 | + instructions: [ |
| 175 | + { |
| 176 | + accounts: [ |
| 177 | + { |
| 178 | + address: '7wJFRFuAE9x5Ptnz2VoBWsfecTCfuuM2sQCpECGypnTU' as Address, |
| 179 | + role: AccountRole.WRITABLE, |
| 180 | + }, |
| 181 | + { |
| 182 | + address: 'SysvarRecentB1ockHashes11111111111111111111' as Address, |
| 183 | + role: AccountRole.READONLY, |
| 184 | + }, |
| 185 | + { |
| 186 | + address: 'HzMoc78z1VNNf9nwD4Czt6CDYEb9LVD8KsVGP46FEmyJ' as Address, |
| 187 | + role: AccountRole.READONLY_SIGNER, |
| 188 | + }, |
| 189 | + ], |
| 190 | + data: new Uint8Array([4, 0, 0, 0]), |
| 191 | + programAddress: '11111111111111111111111111111111' as Address, |
| 192 | + }, |
| 193 | + ], |
| 194 | + lifetimeConstraint: { |
| 195 | + nonce: 'BzAqD6382v5r1pcELoi8HWrBDV4dSL9NGemMn2JYAhxc' as Nonce, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }); |
| 199 | + expect(simulateTransaction).toHaveBeenCalledWith( |
| 200 | + expect.anything(), |
| 201 | + expect.objectContaining({ replaceRecentBlockhash: false }), |
| 202 | + ); |
| 203 | + }); |
| 204 | + it('asks for a replacement blockhash even when the transaction message has a blockhash lifetime', () => { |
| 205 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 206 | + rpc, |
| 207 | + transactionMessage: { |
| 208 | + ...mockTransactionMessage, |
| 209 | + lifetimeConstraint: MOCK_BLOCKHASH_LIFETIME_CONSTRAINT, |
| 210 | + }, |
| 211 | + }); |
| 212 | + expect(simulateTransaction).toHaveBeenCalledWith( |
| 213 | + expect.anything(), |
| 214 | + expect.objectContaining({ replaceRecentBlockhash: true }), |
| 215 | + ); |
| 216 | + }); |
| 217 | + it('asks for a replacement blockhash when the transaction message has no lifetime', () => { |
| 218 | + getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 219 | + rpc, |
| 220 | + transactionMessage: mockTransactionMessage, |
| 221 | + }); |
| 222 | + expect(simulateTransaction).toHaveBeenCalledWith( |
| 223 | + expect.anything(), |
| 224 | + expect.objectContaining({ replaceRecentBlockhash: true }), |
| 225 | + ); |
| 226 | + }); |
| 227 | + it('returns the estimated compute units on success', async () => { |
| 228 | + expect.assertions(1); |
| 229 | + sendSimulateTransactionRequest.mockResolvedValue({ value: { unitsConsumed: 42n } }); |
| 230 | + const estimatePromise = getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 231 | + rpc, |
| 232 | + transactionMessage: mockTransactionMessage, |
| 233 | + }); |
| 234 | + await expect(estimatePromise).resolves.toBe(42); |
| 235 | + }); |
| 236 | + it('caps the estimated compute units to U32::MAX', async () => { |
| 237 | + expect.assertions(1); |
| 238 | + sendSimulateTransactionRequest.mockResolvedValue({ value: { unitsConsumed: 4294967295n /* U32::MAX + 1 */ } }); |
| 239 | + const estimatePromise = getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 240 | + rpc, |
| 241 | + transactionMessage: mockTransactionMessage, |
| 242 | + }); |
| 243 | + await expect(estimatePromise).resolves.toBe(4294967295 /* U32::MAX */); |
| 244 | + }); |
| 245 | + it('throws with the cause when simulation fails', async () => { |
| 246 | + expect.assertions(1); |
| 247 | + const simulationError = new SolanaError(SOLANA_ERROR__INSTRUCTION_ERROR__INSUFFICIENT_FUNDS, { |
| 248 | + index: 42, |
| 249 | + }); |
| 250 | + sendSimulateTransactionRequest.mockRejectedValue(simulationError); |
| 251 | + const estimatePromise = getComputeUnitEstimateForTransactionMessage_INTERNAL_ONLY_DO_NOT_EXPORT({ |
| 252 | + rpc, |
| 253 | + transactionMessage: mockTransactionMessage, |
| 254 | + }); |
| 255 | + await expect(estimatePromise).rejects.toThrow( |
| 256 | + new SolanaError(SOLANA_ERROR__TRANSACTION__FAILED_TO_ESTIMATE_COMPUTE_LIMIT, { |
| 257 | + cause: simulationError, |
| 258 | + }), |
| 259 | + ); |
| 260 | + }); |
| 261 | +}); |
0 commit comments