LCOV - code coverage report
Current view: top level - flamenco/runtime - fd_runtime_const.h (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 10 20 50.0 %
Date: 2026-02-22 05:44:04 Functions: 0 0 -

          Line data    Source code
       1             : #ifndef HEADER_fd_src_flamenco_runtime_fd_runtime_const_h
       2             : #define HEADER_fd_src_flamenco_runtime_fd_runtime_const_h
       3             : 
       4             : #include "../leaders/fd_leaders.h"
       5             : #include "../types/fd_types.h"
       6             : #include "../../ballet/txn/fd_txn.h" /* for fd_acct_addr_t */
       7             : #include "../vm/fd_vm_base.h" /* fd_vm_trace_t */
       8             : 
       9             : FD_PROTOTYPES_BEGIN
      10             : 
      11             : /* All of the variable bounds in the bank should be deteremined by the
      12             :    max number of vote accounts and stake accounts that the system
      13             :    supports. These are not protocol-level bounds, but rather bounds
      14             :    that are used to determine the max amount of memory that various
      15             :    data structures require. */
      16             : 
      17          51 : #define FD_RUNTIME_MAX_VOTE_ACCOUNTS  (40200UL)   /* ~40k vote accounts */
      18             : 
      19     1338885 : #define FD_RUNTIME_MAX_STAKE_ACCOUNTS (3000000UL) /* 3M stake accounts */
      20             : 
      21             : #define FD_RUNTIME_SLOTS_PER_EPOCH    (432000UL)  /* 432k slots per epoch */
      22             : 
      23             : /* Maximum amount of writable accounts per transaction
      24             :    https://github.com/anza-xyz/agave/blob/v3.0.8/runtime/src/bank.rs#L2946 */
      25         828 : #define FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_TRANSACTION (64UL)
      26             : 
      27             : /* The initial block id hash is a dummy value for the initial block id
      28             :    as one is not provided in snapshots.  This does not have an
      29             :    equivalent in Agave.
      30             : 
      31             :    TODO: This should be removed in favor of repairing the last shred of
      32             :    the snapshot slot to get the actual block id of the snapshot slot. */
      33             : 
      34           0 : #define FD_RUNTIME_INITIAL_BLOCK_ID (0xF17EDA2CE7B1DUL)
      35             : 
      36             : /* FD_RUNTIME_ACC_SZ_MAX is the protocol level hardcoded size limit of a
      37             :    Solana account. */
      38             : 
      39         975 : #define FD_RUNTIME_ACC_SZ_MAX (10UL<<20) /* 10MiB */
      40             : 
      41             : /* FD_RUNTIME_WRITABLE_ACCOUNTS_MAX is the protocol level hardcoded
      42             :    limit of writable accounts per transaction. */
      43             : 
      44             : #define FD_RUNTIME_WRITABLE_ACCOUNTS_MAX (64UL)
      45             : 
      46             : /* Genesis creation times for major Solana clusters */
      47             : 
      48           0 : #define FD_RUNTIME_GENESIS_CREATION_TIME_MAINNET (1584368940UL)
      49           0 : #define FD_RUNTIME_GENESIS_CREATION_TIME_TESTNET (1580834132UL)
      50           0 : #define FD_RUNTIME_GENESIS_CREATION_TIME_DEVNET  (1597081016UL)
      51             : 
      52             : 
      53             : /* FeeStructure constants. Bank is always initialized with
      54             :    `FeeStructure::default()`
      55             :    https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/runtime/src/bank.rs#L1859
      56             :    https://github.com/anza-xyz/solana-sdk/blob/badc2c40071e6e7f7a8e8452b792b66613c5164c/fee-structure/src/lib.rs#L100 */
      57          81 : #define FD_RUNTIME_FEE_STRUCTURE_LAMPORTS_PER_SIGNATURE (5000UL)
      58             : 
      59             : /* Various constant values used by the runtime. */
      60             : 
      61         162 : #define MICRO_LAMPORTS_PER_LAMPORT (1000000UL)
      62             : 
      63             : #define DEFAULT_HASHES_PER_TICK  (12500)
      64             : #define UPDATED_HASHES_PER_TICK2 (17500)
      65             : #define UPDATED_HASHES_PER_TICK3 (27500)
      66             : #define UPDATED_HASHES_PER_TICK4 (47500)
      67             : #define UPDATED_HASHES_PER_TICK5 (57500)
      68             : #define UPDATED_HASHES_PER_TICK6 (62500)
      69             : 
      70           0 : #define SECONDS_PER_YEAR ((double)(365.242199 * 24.0 * 60.0 * 60.0))
      71             : 
      72             : /* https://github.com/anza-xyz/agave/blob/0d34a1a160129c4293dac248e14231e9e773b4ce/program-runtime/src/compute_budget.rs#L139 */
      73             : #define FD_MAX_INSTRUCTION_TRACE_LENGTH (64UL)
      74             : /* https://github.com/anza-xyz/agave/blob/f70ab5598ccd86b216c3928e4397bf4a5b58d723/compute-budget/src/compute_budget.rs#L13 */
      75           0 : #define FD_MAX_INSTRUCTION_STACK_DEPTH  (5UL)
      76             : 
      77             : 
      78           0 : #define FD_RUNTIME_VM_TRACE_EVENT_MAX      (128UL<<20)
      79           0 : #define FD_RUNTIME_VM_TRACE_EVENT_DATA_MAX (2048UL)
      80             : 
      81           0 : #define FD_RUNTIME_VM_TRACE_STATIC_FOOTPRINT (FD_RUNTIME_VM_TRACE_EVENT_MAX + sizeof(fd_vm_trace_t))
      82           0 : #define FD_RUNTIME_VM_TRACE_STATIC_ALIGN     (8UL)
      83             : 
      84             : /* Maximum CPI instruction data size. 10 KiB was chosen to ensure that
      85             :    CPI instructions are not more limited than transaction instructions
      86             :    if the size of transactions is doubled in the future.
      87             :    https://github.com/anza-xyz/agave/blob/v3.1.1/transaction-context/src/lib.rs#L33 */
      88             : #define FD_RUNTIME_CPI_MAX_INSTR_DATA_LEN (10240UL)
      89             : 
      90             : /* The bpf loader's serialization footprint is bounded in the worst case
      91             :    by 64 unique writable accounts which are each 10MiB in size (bounded
      92             :    by the amount of transaction accounts).  We can also have up to
      93             :    FD_BPF_INSTR_ACCT_MAX (255) referenced accounts in an instruction.
      94             : 
      95             :    - 8 bytes for the account count
      96             :    For each account:
      97             :      If duplicated:
      98             :        - 8 bytes for each duplicated account
      99             :     If not duplicated:
     100             :      - header for each unique account (96 bytes)
     101             :        - 1 account idx byte
     102             :        - 1 is_signer byte
     103             :        - 1 is_writable byte
     104             :        - 1 executable byte
     105             :        - 4 bytes for the original data length
     106             :        - 32 bytes for the key
     107             :        - 32 bytes for the owner
     108             :        - 8 bytes for the lamports
     109             :        - 8 bytes for the data length
     110             :        - 8 bytes for the rent epoch
     111             :      - 10MiB for the data (10485760 bytes)
     112             :      - 10240 bytes for resizing the data
     113             :      - 0 padding bytes because this is already 8 byte aligned
     114             :    - 8 bytes for instruction data length
     115             :    - 1232 bytes for the instruction data (TXN_MTU)
     116             :    - 32 bytes for the program id
     117             : 
     118             :   So the total footprint is:
     119             :   8 header bytes +
     120             :   192 duplicate accounts (256 instr accounts - 64 unique accounts) * 8 bytes     = 1536      duplicate account bytes +
     121             :   64 unique accounts * (96 header bytes + 10485760 bytes + 10240 resizing bytes) = 671750144 unique account bytes +
     122             :   8 + 1232 + 32                                                                  = 1272 bytes trailer bytes + program id = 671751416 bytes
     123             :   Total footprint: 671752960 bytes
     124             : 
     125             :   This is a reasonably tight-ish upper bound on the input region
     126             :   footprint for a single instruction at a single stack depth.  In
     127             :   reality the footprint would be slightly smaller because the
     128             :   instruction data can't be equal to the transaction MTU.
     129             :  */
     130         588 : #define MAX_PERMITTED_DATA_INCREASE (10240UL) // 10KB
     131         492 : #define FD_BPF_ALIGN_OF_U128        (8UL)
     132         684 : #define FD_ACCOUNT_REC_ALIGN        (8UL)
     133             : /* https://github.com/anza-xyz/sbpf/blob/v0.12.2/src/ebpf.rs#L37-L38 */
     134             : #define FD_RUNTIME_EBPF_HOST_ALIGN  (16UL)
     135             : 
     136             : /* FD_INSTR_ACCT_MAX is the maximum number of accounts that can
     137             :    be referenced by a single instruction.
     138             : 
     139             :    This is different from FD_BPF_INSTR_ACCT_MAX, which is enforced by the
     140             :    BPF serializer. It is possible to pass in more than FD_BPF_INSTR_ACCT_MAX
     141             :    instruction accounts in a transaction (for example mainnet transaction)
     142             :    3eDdfZE6HswPxFKrtnQPsEmTkyL1iP57gRPEXwaqNGAqF1paGXCYYMwh7z4uQDUMgFor742sikVSQZW1gFRDhPNh).
     143             : 
     144             :    A transaction like this will be loaded and sanitized, but will fail in the
     145             :    bpf serialization stage. It is also possible to invoke a native program with
     146             :    more than FD_BPF_INSTR_ACCT_MAX instruction accounts that will execute successfully.
     147             : 
     148             :    Therefore we need to derive a bound from a worst-case transaction: one that
     149             :    has the maximum possible number of instruction accounts at the expense of
     150             :    everything else. This is a legacy transaction with a single account address,
     151             :    a single signature, a single instruction with empty data and as many
     152             :    instruction accounts as possible.
     153             : 
     154             :    Therefore, the maximum number of instruction accounts is:
     155             :      (MTU - fixed overhead) / (size of instruction account)
     156             :    = (MTU
     157             :        - signature count (1 byte, value=1)
     158             :        - signature (64 bytes)
     159             :        - signature count in header (1 byte)
     160             :        - readonly signed count (1 byte)
     161             :        - readonly unsigned count (1 byte)
     162             :        - account count (1 byte, compact-u16 value=1)
     163             :        - 1 account address (32 bytes)
     164             :        - recent blockhash (32 bytes)
     165             :        - instruction count (1 byte, compact-u16 value=1)
     166             :        - program id index (1 byte)
     167             :        - instruction account count (2 bytes)
     168             :        - data len (1 byte, value=0)
     169             :    = 1232 - 1 - 64 - 1 - 1 - 1 - 1 - 32 - 32 - 1 - 1 - 2 - 1
     170             :    = 1094
     171             : 
     172             :    TODO: SIMD-406 (https://github.com/solana-foundation/solana-improvement-documents/pull/406)
     173             :    limits the number of instruction accounts to 255 in transaction sanitization.
     174             : 
     175             :    Once the corresponding feature gate has been activated, we can reduce
     176             :    FD_INSTR_ACCT_MAX to 255. We cannot reduce this before as this would cause
     177             :    the result of the get_processed_sibling_instruction syscall to diverge from
     178             :    Agave. */
     179           6 : #define FD_INSTR_ACCT_MAX           (1094UL)
     180             : 
     181             : /* FD_BPF_INSTR_ACCT_MAX is the maximum number of accounts that
     182             :    an instruction that goes through the bpf loader serializer can reference.
     183             : 
     184             :    The BPF loader has a lower limit for the number of instruction accounts
     185             :    than is enforced in transaction sanitization.
     186             : 
     187             :    TODO: remove this limit once SIMD-406 is activated, as we can then use the
     188             :    same limit everywhere.
     189             : 
     190             :    https://github.com/anza-xyz/agave/blob/v3.1.4/transaction-context/src/lib.rs#L30-L32 */
     191             : #define FD_BPF_INSTR_ACCT_MAX       (255UL)
     192             : 
     193             : #define FD_BPF_LOADER_UNIQUE_ACCOUNT_FOOTPRINT(direct_mapping)                                                                                              \
     194             :                                               (1UL                         /* dup byte          */                                                        + \
     195             :                                                sizeof(uchar)               /* is_signer         */                                                        + \
     196             :                                                sizeof(uchar)               /* is_writable       */                                                        + \
     197             :                                                sizeof(uchar)               /* executable        */                                                        + \
     198             :                                                sizeof(uint)                /* original_data_len */                                                        + \
     199             :                                                sizeof(fd_pubkey_t)         /* key               */                                                        + \
     200             :                                                sizeof(fd_pubkey_t)         /* owner             */                                                        + \
     201             :                                                sizeof(ulong)               /* lamports          */                                                        + \
     202             :                                                sizeof(ulong)               /* data len          */                                                        + \
     203             :                                                (direct_mapping ? FD_BPF_ALIGN_OF_U128 : FD_ULONG_ALIGN_UP( FD_RUNTIME_ACC_SZ_MAX, FD_BPF_ALIGN_OF_U128 )) + \
     204             :                                                MAX_PERMITTED_DATA_INCREASE                                                                                + \
     205             :                                                sizeof(ulong))              /* rent_epoch        */
     206             : #define FD_BPF_LOADER_DUPLICATE_ACCOUNT_FOOTPRINT (8UL) /* 1 dup byte + 7 bytes for padding */
     207             : 
     208             : #define FD_BPF_LOADER_INPUT_REGION_FOOTPRINT(account_lock_limit, direct_mapping)                                                                      \
     209             :                                               (FD_ULONG_ALIGN_UP( (sizeof(ulong)         /* acct_cnt       */                                       + \
     210             :                                                                    account_lock_limit*FD_BPF_LOADER_UNIQUE_ACCOUNT_FOOTPRINT(direct_mapping)        + \
     211             :                                                                    (FD_BPF_INSTR_ACCT_MAX-account_lock_limit)*FD_BPF_LOADER_DUPLICATE_ACCOUNT_FOOTPRINT + \
     212             :                                                                    sizeof(ulong)         /* instr data len */                                       + \
     213             :                                                                    FD_TXN_MTU            /* No instr data  */                                       + \
     214             :                                                                    sizeof(fd_pubkey_t)), /* program id     */                                          \
     215             :                                                                    FD_RUNTIME_EBPF_HOST_ALIGN ))
     216             : 
     217             : 
     218             : 
     219             : #define BPF_LOADER_SERIALIZATION_FOOTPRINT (671752960UL)
     220             : FD_STATIC_ASSERT( BPF_LOADER_SERIALIZATION_FOOTPRINT==FD_BPF_LOADER_INPUT_REGION_FOOTPRINT(64UL, 0), bpf_loader_serialization_footprint );
     221             : 
     222             : 
     223             : /* Some vote instruction types are dynamically sized:
     224             :     - tower_sync_switch                (contains deque of fd_vote_lockout_t)
     225             :     - tower_sync                       (contains deque of fd_vote_lockout_t)
     226             :     - compact_vote_state_update_switch (vector of fd_lockout_offset_t)
     227             :     - compact_vote_state_update        (vector of fd_lockout_offset_t)
     228             :     - authorize_checked_with_seed      (char vector of current_authority_derived_key_seed)
     229             :     - authorize_with_seed              (char vector of current_authority_derived_key_seed)
     230             :     - update_vote_state_switch         (contains deque of fd_vote_lockout_t)
     231             :     - update_vote_state                (contains deque of fd_vote_lockout_t)
     232             :     - vote_switch                      (deque of slot numbers)
     233             :     - vote                             (deque of slot numbers)
     234             :    All other vote instruction types are statically sized.
     235             : 
     236             :    A loose bound on the max amount of encoded fd_vote_lockout_t
     237             :    possible is 1232 bytes/(12 bytes/per lockout) = 102 lockouts.  So
     238             :    the worst case bound for the deque of fd_vote_lockout is
     239             :    32 + (102 * sizeof(fd_vote_lockout_t)) = 1644 bytes.
     240             : 
     241             :    The worst case vector of fd_lockout_offset_t is one where each
     242             :    encoded element is 2 bytes.  This means that we can have 1232/2 =
     243             :    616 elements.  They are represented as being 16 bytes each, so the
     244             :    total footprint would be 9856 bytes.
     245             : 
     246             :    The deque of slot numbers is a vector of ulong, which is 8 bytes.
     247             :    So the worst case is 1232 bytes/8 bytes = 154 elements.  So, the
     248             :    total footprint is 32 + (154 * 8 bytes) = 1264 bytes.
     249             : 
     250             :    The worst case char vector is 1232 bytes as each element is 1 byte
     251             :    up to the txn MTU.
     252             : 
     253             :    With this, that means that the compact_vote_state_update_switch
     254             :    can have the largest worst case footprint where the struct is
     255             :    104 bytes (sizeof(fd_compact_vote_state_update_switch_t) + the
     256             :    worst case lockout vector of 616 elements. */
     257             : #define FD_LOCKOUT_OFFSET_FOOTPRINT   (9856UL)
     258             : #define FD_VOTE_INSTRUCTION_FOOTPRINT (sizeof(fd_vote_instruction_t) + FD_LOCKOUT_OFFSET_FOOTPRINT)
     259             : 
     260             : /* TODO: This is the value as generated by fd_types bincode decoding of
     261             :    fd_vote_state_versioned_t.  This should eventually be replaced. */
     262             : #define FD_VOTE_STATE_VERSIONED_FOOTPRINT (9248UL)
     263             : 
     264             : /* The footprint of a fd_vote_authorized_voters_t struct is defined as a
     265             :    fd_vote_authorized_voters_t followed by a pool and then a treap. */
     266             : #define FD_AUTHORIZED_VOTERS_ALIGN     (128UL)
     267             : #define FD_AUTHORIZED_VOTERS_FOOTPRINT (4888UL)
     268             : 
     269             : /* TODO: These footprints are currently overprovisioned due to test
     270             :    fixtures which currently violate protocol invariants. */
     271             : 
     272             : /* The footprint of the landed votes is determined by a deque with max
     273             :    cnt of 31.  The footprint is as follows:
     274             :    alignof(DEQUE_T) == alignof(fd_landed_vote_t) == 8
     275             :    sizeof(DEQUE_T)  == sizeof(fd_landed_vote_t)  == 24
     276             :    return fd_ulong_align_up( fd_ulong_align_up( 32UL, alignof(DEQUE_T) ) + sizeof(DEQUE_T)*max, alignof(DEQUE_(private_t)) );
     277             :    return fd_ulong_align_up( fd_ulong_align_up( 32UL, 8UL ) )            + 24UL*31UL, 8UL );
     278             :    return fd_ulong_align_up( 32UL + 744, 8UL ) == 776 */
     279             : #define FD_LANDED_VOTES_ALIGN     (32UL)
     280             : #define FD_LANDED_VOTES_FOOTPRINT (FD_VOTE_STATE_VERSIONED_FOOTPRINT)
     281             : 
     282             : /* The calculation for the landed votes footprint is the same as the
     283             :    calculation for the landed votes but the sizeof(fd_vote_lockout_t)
     284             :    is 16 bytes:
     285             :    return fd_ulong_align_up( 32UL + 16UL * 31UL, 8UL ) == 528UL */
     286             : #define FD_VOTE_LOCKOUTS_ALIGN     (32UL)
     287             : #define FD_VOTE_LOCKOUTS_FOOTPRINT (FD_VOTE_STATE_VERSIONED_FOOTPRINT)
     288             : 
     289             : static const FD_FN_UNUSED fd_account_meta_t FD_ACCOUNT_META_DEFAULT = {0};
     290             : 
     291             : FD_PROTOTYPES_END
     292             : 
     293             : #endif /* HEADER_fd_src_flamenco_runtime_fd_runtime_const_h */

Generated by: LCOV version 1.14