diff --git a/app/entities/compute-unit/lib/__tests__/compute-units-schedule.spec.ts b/app/entities/compute-unit/lib/__tests__/compute-units-schedule.spec.ts index e6c9890fe..6cc3c2537 100644 --- a/app/entities/compute-unit/lib/__tests__/compute-units-schedule.spec.ts +++ b/app/entities/compute-unit/lib/__tests__/compute-units-schedule.spec.ts @@ -1,11 +1,15 @@ import { makeBlockTransaction } from '@entities/block-data/__fixtures__/block-builders'; import { address } from '@solana/kit'; -import { ComputeBudgetProgram } from '@solana/web3.js'; +import { ComputeBudgetProgram, type ParsedInstruction, PublicKey } from '@solana/web3.js'; import { Cluster } from '@utils/cluster'; import { alloc, writeUint32LE } from '@/app/shared/lib/bytes'; -import { estimateRequestedComputeUnits, getReservedComputeUnits } from '../compute-units-schedule'; +import { + estimateRequestedComputeUnits, + estimateRequestedComputeUnitsForParsedTransaction, + getReservedComputeUnits, +} from '../compute-units-schedule'; describe('getReservedComputeUnits', () => { describe('mainnet', () => { @@ -65,6 +69,14 @@ describe('getReservedComputeUnits', () => { ).toEqual(200_000); }); + it.each([ + 'Stake11111111111111111111111111111111111111', + 'Config1111111111111111111111111111111111111', + 'AddressLookupTab1e1111111111111111111111111', + ])('should return default compute units for %s, which runs as a Core BPF program', programId => { + expect(getReservedComputeUnits({ cluster: Cluster.Mainnet, epoch: 1097n, programId })).toEqual(200_000); + }); + it('should handle feature gate program migration correctly', () => { // Before migration (epoch 753), feature gate is builtin expect( @@ -139,6 +151,30 @@ describe('getReservedComputeUnits', () => { }); }); + describe('Core BPF migrations', () => { + it.each([ + ['mainnet', Cluster.Mainnet, 'AddressLookupTab1e1111111111111111111111111', 762n], + ['mainnet', Cluster.Mainnet, 'Stake11111111111111111111111111111111111111', 823n], + ['devnet', Cluster.Devnet, 'AddressLookupTab1e1111111111111111111111111', 849n], + ['devnet', Cluster.Devnet, 'Stake11111111111111111111111111111111111111', 917n], + ['testnet', Cluster.Testnet, 'AddressLookupTab1e1111111111111111111111111', 756n], + ['testnet', Cluster.Testnet, 'Stake11111111111111111111111111111111111111', 813n], + ])('should keep the minimal reserve on %s for %s until its migration', (_name, cluster, programId, epoch) => { + expect(getReservedComputeUnits({ cluster, epoch: epoch - 1n, programId })).toEqual(3_000); + expect(getReservedComputeUnits({ cluster, epoch, programId })).toEqual(200_000); + }); + + it('should return default compute units for Config, which was already Core BPF at epoch 759', () => { + expect( + getReservedComputeUnits({ + cluster: Cluster.Mainnet, + epoch: 759n, + programId: 'Config1111111111111111111111111111111111111', + }), + ).toEqual(200_000); + }); + }); + describe('custom cluster', () => { it('should always use most recent configuration', () => { expect( @@ -304,6 +340,17 @@ describe('estimateRequestedComputeUnits', () => { expect(estimateRequestedComputeUnits(tx, 759n, Cluster.Mainnet)).toEqual(203_000); }); + // Mainnet 5aKTtG79yJycua2vNZQbWWsvaEv6qzsxmrVGsoSYQPVRhy9LpvnixFwm8ntFuapeV5ice5V9xLoUzta3oZg8ybfM logs + // `Stake11111111111111111111111111111111111111 consumed 12186 of 202850` after the System instruction used 150. + it('should reserve the default for a Stake instruction', () => { + const tx = createMockTransaction([ + { data: new Uint8Array([4, 0, 0, 0]), programId: '11111111111111111111111111111111' }, + { data: new Uint8Array([1, 0, 0, 0]), programId: 'Stake11111111111111111111111111111111111111' }, + ]); + + expect(estimateRequestedComputeUnits(tx, 1097n, Cluster.Mainnet)).toEqual(203_000); + }); + it('should handle feature gate program correctly across epochs', () => { const tx = createMockTransaction([ { @@ -439,3 +486,33 @@ describe('estimateRequestedComputeUnits', () => { }); }); }); + +describe('estimateRequestedComputeUnitsForParsedTransaction', () => { + // Mainnet 5PjwEDEdNSLv7uYTe3cjoyRcqDA7PGVnGXq1LoH1LDEy17ymV4ePvPfZy3Gz8CLnS3LiPdyu9LhAUQUqkY45NRgf logs + // `Stake11111111111111111111111111111111111111 consumed 16053 of 200000 compute units`. + it('should reserve the default for an RPC-parsed Stake instruction', () => { + const delegate: ParsedInstruction = { + parsed: { + info: { + clockSysvar: 'SysvarC1ock11111111111111111111111111111111', + stakeAccount: '4otjzF3rD3ExDJw3UL4zhdUsiNXJKKT5Gnp1sadRYmvN', + stakeAuthority: '59L2oxymiQQ9Hvhh92nt8Y7nDYjsauFkdb3SybdnsG6h', + stakeConfigAccount: 'StakeConfig11111111111111111111111111111111', + stakeHistorySysvar: 'SysvarStakeHistory1111111111111111111111111', + voteAccount: 'H74qox1GASBWd94FWMyy6GVAbRVLf9SAMgbJ1tzSUAst', + }, + type: 'delegate', + }, + program: 'stake', + programId: new PublicKey('Stake11111111111111111111111111111111111111'), + }; + + expect( + estimateRequestedComputeUnitsForParsedTransaction( + { message: { instructions: [delegate] } }, + 1097n, + Cluster.Mainnet, + ), + ).toEqual(200_000); + }); +}); diff --git a/app/entities/compute-unit/lib/compute-units-schedule.ts b/app/entities/compute-unit/lib/compute-units-schedule.ts index 734262b1b..40a38003d 100644 --- a/app/entities/compute-unit/lib/compute-units-schedule.ts +++ b/app/entities/compute-unit/lib/compute-units-schedule.ts @@ -21,12 +21,15 @@ const BASE58_ENCODER = getBase58Encoder(); * Built-in programs that have minimal reserved compute units (3k) * This list is based on the feature gate C9oAhLxDBm3ssWtJx1yBGzPY55r2rArHmN1pbQn6HogH * Source: https://solana.com/docs/references/feature-gates/reserve-minimal-cus-for-builtins + * Config is not in the list: it was already a Core BPF program on every cluster when this feature activated. + * Stake and Address Lookup Table leave it when their own Core BPF migrations activate (see below). */ +const STAKE_PROGRAM = 'Stake11111111111111111111111111111111111111'; +const ADDRESS_LOOKUP_TABLE_PROGRAM = 'AddressLookupTab1e1111111111111111111111111'; const BUILTIN_PROGRAMS_3K: readonly string[] = [ '11111111111111111111111111111111', // System Program - 'Stake11111111111111111111111111111111111111', // Stake Program - 'Config1111111111111111111111111111111111111', // Config Program - 'AddressLookupTab1e1111111111111111111111111', // Address Lookup Table Program + STAKE_PROGRAM, // Stake Program + ADDRESS_LOOKUP_TABLE_PROGRAM, // Address Lookup Table Program 'BPFLoaderUpgradeab1e11111111111111111111111', // BPF Loader Upgradeable 'BPFLoader1111111111111111111111111111111111', // BPF Loader 'BPFLoader2111111111111111111111111111111111', // BPF Loader 2 @@ -65,6 +68,11 @@ const DEFAULT_COMPUTE_UNITS = 200_000; const MINIMAL_BUILTIN_COMPUTE_UNITS = 3_000; const MAX_COMPUTE_UNITS = 1_400_000; +function reserveMinimalUnitsFor(builtins: readonly string[]) { + return (programId: string) => + builtins.includes(programId) ? MINIMAL_BUILTIN_COMPUTE_UNITS : DEFAULT_COMPUTE_UNITS; +} + /** * Compute unit reserve configurations by epoch * Add new configurations here as features are activated @@ -87,13 +95,34 @@ const COMPUTE_UNIT_RESERVE_CONFIGS: readonly ComputeUnitReserveConfig[] = [ }, description: 'Built-in programs use minimal compute units', featureAccount: 'C9oAhLxDBm3ssWtJx1yBGzPY55r2rArHmN1pbQn6HogH', - getReservedUnits: (programId: string) => { - if (BUILTIN_PROGRAMS_3K.includes(programId)) { - return MINIMAL_BUILTIN_COMPUTE_UNITS; - } - // Feature gate program is already BPF at this point, uses default - return DEFAULT_COMPUTE_UNITS; + // Feature gate program is already BPF at this point, uses default + getReservedUnits: reserveMinimalUnitsFor(BUILTIN_PROGRAMS_3K), + }, + { + activations: { + [Cluster.Mainnet]: 762, + [Cluster.Devnet]: 849, + [Cluster.Testnet]: 756, + }, + description: 'Address Lookup Table program migrated to Core BPF', + featureAccount: 'C97eKZygrkU4JxJsZdjgbUY7iQR7rKTr4NyDWo2E5pRm', + getReservedUnits: reserveMinimalUnitsFor( + BUILTIN_PROGRAMS_3K.filter(programId => programId !== ADDRESS_LOOKUP_TABLE_PROGRAM), + ), + }, + { + activations: { + [Cluster.Mainnet]: 823, + [Cluster.Devnet]: 917, + [Cluster.Testnet]: 813, }, + description: 'Stake program migrated to Core BPF', + featureAccount: '6M4oQ6eXneVhtLoiAr4yRYQY43eVLjrKbiDZDJc892yk', + getReservedUnits: reserveMinimalUnitsFor( + BUILTIN_PROGRAMS_3K.filter( + programId => programId !== ADDRESS_LOOKUP_TABLE_PROGRAM && programId !== STAKE_PROGRAM, + ), + ), }, ];