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 : /* FD_RUNTIME_MAX_{STAKE,VOTE}_ACCOUNTS are the maximum number of stake 12 : and vote accounts that the system supports: anything larger will 13 : result in a crash. The bounds were set with the intention of making a 14 : dos vector to mint stake/vote accounts financially infeasible. A 15 : reasonable value to guard against this attack is roughly 550,000 SOL. 16 : 17 : For vote accounts, the limit is set to 19,000,000 because the rent 18 : exempt reserve of creating a valid vote account is ~0.03 SOL. For 19 : each vote account, it also must be staked. Each stake account has a 20 : rent exempt value of ~0.022 SOL. This means the cost of minting 20M 21 : vote accounts is: 22 : 19,000,000 accounts * 0.02685 SOL = 510,150 SOL. 23 : 19,000,000 accounts * 0.00228 SOL = 43,320 SOL. 24 : Total cost: 553,470 SOL. 25 : In reality, the cost is slightly higher because of transaction fees 26 : and various CU costs to create the vote and stake accounts. 27 : 28 : For stake accounts, the limit is set to 241M because the rent exempt 29 : reserve of creating a valid stake account is 30 : 241,000,000 accounts * 0.00228 SOL = 549,480 SOL. 31 : If you just consider the transaction fee of 0.000005 per account 32 : 241,000,000 * 0.000005 = 1,205 SOL. 33 : This brings our total cost to 550,685 SOL. */ 34 : 35 0 : #define FD_RUNTIME_MAX_VOTE_ACCOUNTS (19000000UL) 36 0 : #define FD_RUNTIME_MAX_STAKE_ACCOUNTS (241000000UL) 37 : 38 : /* The expected stake and vote account values are based on observed 39 : values on mainnet and testnet allowing for some growth. These are 40 : chosen to size various caches and maps: they are not intended to be 41 : exact as they are not consensus critical values. */ 42 : 43 60 : #define FD_RUNTIME_EXPECTED_STAKE_ACCOUNTS (2000000UL) 44 60 : #define FD_RUNTIME_EXPECTED_VOTE_ACCOUNTS (16384UL) 45 : 46 : #define FD_RUNTIME_SLOTS_PER_EPOCH (432000UL) /* 432k slots per epoch */ 47 : 48 24 : #define FD_RUNTIME_MAX_VOTE_ACCOUNTS_VAT (2000UL) 49 : 50 : /* Maximum amount of writable accounts per transaction 51 : https://github.com/anza-xyz/agave/blob/v3.0.8/runtime/src/bank.rs#L2946 */ 52 48 : #define FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_TRANSACTION (64UL) 53 : 54 : /* FD_RUNTIME_ACC_SZ_MAX is the protocol level hardcoded size limit of a 55 : Solana account. */ 56 : 57 662354 : #define FD_RUNTIME_ACC_SZ_MAX (10UL<<20) /* 10MiB */ 58 : 59 : /* FD_RUNTIME_WRITABLE_ACCOUNTS_MAX is the protocol level hardcoded 60 : limit of writable accounts per transaction. */ 61 : 62 : #define FD_RUNTIME_WRITABLE_ACCOUNTS_MAX (64UL) 63 : 64 : /* Genesis creation times for major Solana clusters */ 65 : 66 0 : #define FD_RUNTIME_GENESIS_CREATION_TIME_MAINNET (1584368940UL) 67 0 : #define FD_RUNTIME_GENESIS_CREATION_TIME_TESTNET (1580834132UL) 68 0 : #define FD_RUNTIME_GENESIS_CREATION_TIME_DEVNET (1597081016UL) 69 : 70 : /* FeeStructure constants. Bank is always initialized with 71 : `FeeStructure::default()` 72 : https://github.com/anza-xyz/agave/blob/v3.1.0-beta.0/runtime/src/bank.rs#L1859 73 : https://github.com/anza-xyz/solana-sdk/blob/badc2c40071e6e7f7a8e8452b792b66613c5164c/fee-structure/src/lib.rs#L100 */ 74 4101 : #define FD_RUNTIME_FEE_STRUCTURE_LAMPORTS_PER_SIGNATURE (5000UL) 75 : 76 : /* Various constant values used by the runtime. */ 77 : 78 8202 : #define MICRO_LAMPORTS_PER_LAMPORT (1000000UL) 79 : 80 : #define DEFAULT_HASHES_PER_TICK (12500) 81 : #define UPDATED_HASHES_PER_TICK2 (17500) 82 : #define UPDATED_HASHES_PER_TICK3 (27500) 83 : #define UPDATED_HASHES_PER_TICK4 (47500) 84 : #define UPDATED_HASHES_PER_TICK5 (57500) 85 : #define UPDATED_HASHES_PER_TICK6 (62500) 86 : 87 5528 : #define SECONDS_PER_YEAR ((double)(365.242199 * 24.0 * 60.0 * 60.0)) 88 : 89 : /* https://github.com/anza-xyz/agave/blob/0d34a1a160129c4293dac248e14231e9e773b4ce/program-runtime/src/compute_budget.rs#L139 */ 90 : #define FD_MAX_INSTRUCTION_TRACE_LENGTH (64UL) 91 : /* https://github.com/anza-xyz/agave/blob/f70ab5598ccd86b216c3928e4397bf4a5b58d723/compute-budget/src/compute_budget.rs#L13 */ 92 6808 : #define FD_MAX_INSTRUCTION_STACK_DEPTH (5UL) 93 : 94 : 95 0 : #define FD_RUNTIME_VM_TRACE_EVENT_MAX (128UL<<20) 96 0 : #define FD_RUNTIME_VM_TRACE_EVENT_DATA_MAX (2048UL) 97 : 98 0 : #define FD_RUNTIME_VM_TRACE_STATIC_FOOTPRINT (FD_RUNTIME_VM_TRACE_EVENT_MAX + sizeof(fd_vm_trace_t)) 99 0 : #define FD_RUNTIME_VM_TRACE_STATIC_ALIGN (8UL) 100 : 101 : /* Maximum CPI instruction data size. 10 KiB was chosen to ensure that 102 : CPI instructions are not more limited than transaction instructions 103 : if the size of transactions is doubled in the future. 104 : https://github.com/anza-xyz/agave/blob/v3.1.1/transaction-context/src/lib.rs#L33 */ 105 : #define FD_RUNTIME_CPI_MAX_INSTR_DATA_LEN (10240UL) 106 : 107 : /* The bpf loader's serialization footprint is bounded in the worst case 108 : by 64 unique writable accounts which are each 10MiB in size (bounded 109 : by the amount of transaction accounts). We can also have up to 110 : FD_BPF_INSTR_ACCT_MAX (255) referenced accounts in an instruction. 111 : 112 : - 8 bytes for the account count 113 : For each account: 114 : If duplicated: 115 : - 8 bytes for each duplicated account 116 : If not duplicated: 117 : - header for each unique account (96 bytes) 118 : - 1 account idx byte 119 : - 1 is_signer byte 120 : - 1 is_writable byte 121 : - 1 executable byte 122 : - 4 bytes for the original data length 123 : - 32 bytes for the key 124 : - 32 bytes for the owner 125 : - 8 bytes for the lamports 126 : - 8 bytes for the data length 127 : - 8 bytes for the rent epoch 128 : - 10MiB for the data (10485760 bytes) 129 : - 10240 bytes for resizing the data 130 : - 0 padding bytes because this is already 8 byte aligned 131 : - 8 bytes for instruction data length 132 : - 10240 bytes for the instruction data (CPI_MAX_INSTR_DATA_LEN) 133 : - 32 bytes for the program id 134 : 135 : So the total footprint is: 136 : 8 header bytes + 137 : 191 duplicate accounts (255 instr accounts - 64 unique accounts) * 8 bytes = 1528 duplicate account bytes + 138 : 64 unique accounts * (96 header bytes + 10485760 bytes + 10240 resizing bytes) = 671750144 unique account bytes + 139 : 8 + 10240 + 32 = 10280 trailer bytes 140 : Subtotal: 671761960 bytes, aligned up to 16 = 671761968 bytes 141 : 142 : This is a reasonably tight-ish upper bound on the input region 143 : footprint for a single instruction at a single stack depth. */ 144 102250 : #define MAX_PERMITTED_DATA_INCREASE (10240UL) // 10KB 145 52183 : #define FD_BPF_ALIGN_OF_U128 (8UL) 146 324042 : #define FD_ACCOUNT_REC_ALIGN (8UL) 147 : /* https://github.com/anza-xyz/sbpf/blob/v0.12.2/src/ebpf.rs#L37-L38 */ 148 : #define FD_RUNTIME_EBPF_HOST_ALIGN (16UL) 149 : 150 : /* FD_INSTR_ACCT_MAX is the maximum number of accounts that can 151 : be referenced by a single instruction. 152 : 153 : This is different from FD_BPF_INSTR_ACCT_MAX, which is enforced by the 154 : BPF serializer. It is possible to pass in more than FD_BPF_INSTR_ACCT_MAX 155 : instruction accounts in a transaction (for example mainnet transaction) 156 : 3eDdfZE6HswPxFKrtnQPsEmTkyL1iP57gRPEXwaqNGAqF1paGXCYYMwh7z4uQDUMgFor742sikVSQZW1gFRDhPNh). 157 : 158 : A transaction like this will be loaded and sanitized, but will fail in the 159 : bpf serialization stage. It is also possible to invoke a native program with 160 : more than FD_BPF_INSTR_ACCT_MAX instruction accounts that will execute successfully. 161 : 162 : Therefore we need to derive a bound from a worst-case transaction: one that 163 : has the maximum possible number of instruction accounts at the expense of 164 : everything else. This is a legacy transaction with a single account address, 165 : a single signature, a single instruction with empty data and as many 166 : instruction accounts as possible. 167 : 168 : Therefore, the maximum number of instruction accounts is: 169 : (MTU - fixed overhead) / (size of instruction account) 170 : = (MTU 171 : - signature count (1 byte, value=1) 172 : - signature (64 bytes) 173 : - signature count in header (1 byte) 174 : - readonly signed count (1 byte) 175 : - readonly unsigned count (1 byte) 176 : - account count (1 byte, compact-u16 value=1) 177 : - 1 account address (32 bytes) 178 : - recent blockhash (32 bytes) 179 : - instruction count (1 byte, compact-u16 value=1) 180 : - program id index (1 byte) 181 : - instruction account count (2 bytes) 182 : - data len (1 byte, value=0) 183 : = 1232 - 1 - 64 - 1 - 1 - 1 - 1 - 32 - 32 - 1 - 1 - 2 - 1 184 : = 1094 185 : 186 : TODO: SIMD-406 (https://github.com/solana-foundation/solana-improvement-documents/pull/406) 187 : limits the number of instruction accounts to 255 in transaction sanitization. 188 : 189 : Once the corresponding feature gate has been activated, we can reduce 190 : FD_INSTR_ACCT_MAX to 255. We cannot reduce this before as this would cause 191 : the result of the get_processed_sibling_instruction syscall to diverge from 192 : Agave. */ 193 0 : #define FD_INSTR_ACCT_MAX (1094UL) 194 : 195 : /* FD_BPF_INSTR_ACCT_MAX is the maximum number of accounts that 196 : an instruction that goes through the bpf loader serializer can reference. 197 : 198 : The BPF loader has a lower limit for the number of instruction accounts 199 : than is enforced in transaction sanitization. 200 : 201 : TODO: remove this limit once SIMD-406 is activated, as we can then use the 202 : same limit everywhere. 203 : 204 : https://github.com/anza-xyz/agave/blob/v3.1.4/transaction-context/src/lib.rs#L30-L32 */ 205 : #define FD_BPF_INSTR_ACCT_MAX (255UL) 206 : 207 : #define FD_BPF_LOADER_UNIQUE_ACCOUNT_FOOTPRINT(direct_mapping) \ 208 : (1UL /* dup byte */ + \ 209 : sizeof(uchar) /* is_signer */ + \ 210 : sizeof(uchar) /* is_writable */ + \ 211 : sizeof(uchar) /* executable */ + \ 212 : sizeof(uint) /* original_data_len */ + \ 213 : sizeof(fd_pubkey_t) /* key */ + \ 214 : sizeof(fd_pubkey_t) /* owner */ + \ 215 : sizeof(ulong) /* lamports */ + \ 216 : sizeof(ulong) /* data len */ + \ 217 : (direct_mapping ? FD_BPF_ALIGN_OF_U128 : FD_ULONG_ALIGN_UP( FD_RUNTIME_ACC_SZ_MAX, FD_BPF_ALIGN_OF_U128 )) + \ 218 : MAX_PERMITTED_DATA_INCREASE + \ 219 : sizeof(ulong)) /* rent_epoch */ 220 : #define FD_BPF_LOADER_DUPLICATE_ACCOUNT_FOOTPRINT (8UL) /* 1 dup byte + 7 bytes for padding */ 221 : 222 : #define FD_BPF_LOADER_INPUT_REGION_FOOTPRINT(account_lock_limit, direct_mapping) \ 223 : (FD_ULONG_ALIGN_UP( (sizeof(ulong) /* acct_cnt */ + \ 224 : account_lock_limit*FD_BPF_LOADER_UNIQUE_ACCOUNT_FOOTPRINT(direct_mapping) + \ 225 : (FD_BPF_INSTR_ACCT_MAX-account_lock_limit)*FD_BPF_LOADER_DUPLICATE_ACCOUNT_FOOTPRINT + \ 226 : sizeof(ulong) /* instr data len */ + \ 227 : FD_RUNTIME_CPI_MAX_INSTR_DATA_LEN /* instr data */ + \ 228 : sizeof(fd_pubkey_t)), /* program id */ \ 229 : FD_RUNTIME_EBPF_HOST_ALIGN )) 230 : 231 : 232 : 233 : #define BPF_LOADER_SERIALIZATION_FOOTPRINT (671761968UL) 234 : FD_STATIC_ASSERT( BPF_LOADER_SERIALIZATION_FOOTPRINT==FD_BPF_LOADER_INPUT_REGION_FOOTPRINT(64UL, 0), bpf_loader_serialization_footprint ); 235 : 236 : 237 : /* Some vote instruction types are dynamically sized: 238 : - tower_sync_switch (contains deque of fd_vote_lockout_t) 239 : - tower_sync (contains deque of fd_vote_lockout_t) 240 : - compact_vote_state_update_switch (vector of fd_lockout_offset_t) 241 : - compact_vote_state_update (vector of fd_lockout_offset_t) 242 : - authorize_checked_with_seed (char vector of current_authority_derived_key_seed) 243 : - authorize_with_seed (char vector of current_authority_derived_key_seed) 244 : - update_vote_state_switch (contains deque of fd_vote_lockout_t) 245 : - update_vote_state (contains deque of fd_vote_lockout_t) 246 : - vote_switch (deque of slot numbers) 247 : - vote (deque of slot numbers) 248 : All other vote instruction types are statically sized. 249 : 250 : A loose bound on the max amount of encoded fd_vote_lockout_t 251 : possible is 1232 bytes/(12 bytes/per lockout) = 102 lockouts. So 252 : the worst case bound for the deque of fd_vote_lockout is 253 : 32 + (102 * sizeof(fd_vote_lockout_t)) = 1644 bytes. 254 : 255 : The worst case vector of fd_lockout_offset_t is one where each 256 : encoded element is 2 bytes. This means that we can have 1232/2 = 257 : 616 elements. They are represented as being 16 bytes each, so the 258 : total footprint would be 9856 bytes. 259 : 260 : The deque of slot numbers is a vector of ulong, which is 8 bytes. 261 : So the worst case is 1232 bytes/8 bytes = 154 elements. So, the 262 : total footprint is 32 + (154 * 8 bytes) = 1264 bytes. 263 : 264 : The worst case char vector is 1232 bytes as each element is 1 byte 265 : up to the txn MTU. 266 : 267 : With this, that means that the compact_vote_state_update_switch 268 : can have the largest worst case footprint where the struct is 269 : 104 bytes (sizeof(fd_compact_vote_state_update_switch_t) + the 270 : worst case lockout vector of 616 elements. */ 271 : #define FD_LOCKOUT_OFFSET_FOOTPRINT (9856UL) 272 : #define FD_VOTE_INSTRUCTION_FOOTPRINT (sizeof(fd_vote_instruction_t) + FD_LOCKOUT_OFFSET_FOOTPRINT) 273 : 274 : /* This footprint is obtained by taking the max of the fd_types decode 275 : footprints for the following vote state versions and associated 276 : bounds: 277 : - all Option<T> fields are Some(T) 278 : - votes: max capacity of 32 279 : - authorized_voters: max capacity of 5 280 : - epoch_credits: max capacity of 64 281 : 282 : v1_14_11: 4496 283 : v3: 4752 284 : v4: 4752 285 : 286 : The authorized_voters min capacity is 5 287 : (FD_VOTE_AUTHORIZED_VOTERS_MIN) to account for the transient 288 : push-before-pop in get_and_cache_authorized_voter_for_epoch before 289 : purge. */ 290 : #define FD_VOTE_STATE_VERSIONED_FOOTPRINT (4752UL) 291 : 292 : /* The footprint of a fd_vote_authorized_voters_t struct is defined as a 293 : fd_vote_authorized_voters_t followed by a pool and then a treap. */ 294 : #define FD_AUTHORIZED_VOTERS_ALIGN (128UL) 295 : #define FD_AUTHORIZED_VOTERS_FOOTPRINT (4888UL) 296 : 297 : /* TODO: These footprints are currently overprovisioned due to test 298 : fixtures which currently violate protocol invariants. */ 299 : 300 : /* The footprint of the landed votes is determined by a deque with max 301 : cnt of 31. The footprint is as follows: 302 : alignof(DEQUE_T) == alignof(fd_landed_vote_t) == 8 303 : sizeof(DEQUE_T) == sizeof(fd_landed_vote_t) == 24 304 : return fd_ulong_align_up( fd_ulong_align_up( 32UL, alignof(DEQUE_T) ) + sizeof(DEQUE_T)*max, alignof(DEQUE_(private_t)) ); 305 : return fd_ulong_align_up( fd_ulong_align_up( 32UL, 8UL ) ) + 24UL*31UL, 8UL ); 306 : return fd_ulong_align_up( 32UL + 744, 8UL ) == 776 */ 307 : #define FD_LANDED_VOTES_ALIGN (32UL) 308 : #define FD_LANDED_VOTES_FOOTPRINT (FD_VOTE_STATE_VERSIONED_FOOTPRINT) 309 : 310 : /* The calculation for the landed votes footprint is the same as the 311 : calculation for the landed votes but the sizeof(fd_vote_lockout_t) 312 : is 16 bytes: 313 : return fd_ulong_align_up( 32UL + 16UL * 31UL, 8UL ) == 528UL */ 314 : #define FD_VOTE_LOCKOUTS_ALIGN (32UL) 315 : #define FD_VOTE_LOCKOUTS_FOOTPRINT (FD_VOTE_STATE_VERSIONED_FOOTPRINT) 316 : 317 : #define FD_EPOCH_CREDITS_MAX (64UL) 318 : 319 : static const FD_FN_UNUSED fd_account_meta_t FD_ACCOUNT_META_DEFAULT = {0}; 320 : 321 : FD_PROTOTYPES_END 322 : 323 : #endif /* HEADER_fd_src_flamenco_runtime_fd_runtime_const_h */