Line data Source code
1 : #include "fd_cost_tracker.h"
2 : #include "fd_bank.h"
3 :
4 : #define POOL_NAME fd_account_cost_pool
5 9 : #define POOL_T fd_account_cost_t
6 963840 : #define POOL_NEXT next_
7 : #include "../../util/tmpl/fd_pool.c"
8 :
9 : #define MAP_NAME fd_account_cost_map
10 : #define MAP_KEY_T fd_pubkey_t
11 : #define MAP_ELE_T fd_account_cost_t
12 0 : #define MAP_KEY account
13 0 : #define MAP_KEY_EQ(k0,k1) (fd_pubkey_eq( k0, k1 ))
14 0 : #define MAP_KEY_HASH(key,seed) (fd_hash( seed, key, sizeof(fd_pubkey_t) ))
15 0 : #define MAP_NEXT next_
16 : #include "../../util/tmpl/fd_map_chain.c"
17 :
18 : static inline fd_account_cost_t *
19 0 : fd_cost_tracker_account_cost_pool_get( fd_cost_tracker_t const * cost_tracker ) {
20 0 : return fd_account_cost_pool_join( (uchar *)cost_tracker + cost_tracker->pool_offset_ );
21 0 : }
22 :
23 : static inline fd_account_cost_map_t *
24 0 : fd_cost_tracker_account_cost_map_get( fd_cost_tracker_t const * cost_tracker ) {
25 0 : return fd_account_cost_map_join( (uchar *)cost_tracker + cost_tracker->map_offset_ );
26 0 : }
27 :
28 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L313-L321 */
29 : FD_FN_PURE static inline ulong
30 0 : get_instructions_data_cost( fd_exec_txn_ctx_t const * txn_ctx ) {
31 0 : ulong total_instr_data_sz = 0UL;
32 0 : for( ushort i=0; i<TXN( &txn_ctx->txn )->instr_cnt; i++ ) {
33 0 : total_instr_data_sz += TXN( &txn_ctx->txn )->instr[ i ].data_sz;
34 0 : }
35 0 : return total_instr_data_sz / FD_PACK_INV_COST_PER_INSTR_DATA_BYTE;
36 0 : }
37 :
38 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L152-L187 */
39 : FD_FN_PURE static inline ulong
40 0 : get_signature_cost( fd_exec_txn_ctx_t const * txn_ctx ) {
41 0 : fd_txn_t const * txn = TXN( &txn_ctx->txn );
42 0 : void const * payload = txn_ctx->txn.payload;
43 0 : fd_acct_addr_t const * accounts = fd_txn_get_acct_addrs( txn, payload );
44 :
45 : /* Compute signature counts (both normal + precompile)
46 : TODO: Factor this logic out into a shared function that can be used
47 : both here and in fd_pack_cost.h */
48 0 : ulong signature_cost = fd_ulong_sat_mul( txn->signature_cnt, FD_PACK_COST_PER_SIGNATURE );
49 0 : ulong num_secp256k1_instruction_signatures = 0UL;
50 0 : ulong num_ed25519_instruction_signatures = 0UL;
51 0 : ulong num_secp256r1_instruction_signatures = 0UL;
52 :
53 0 : for( ushort i=0; i<txn->instr_cnt; i++ ) {
54 0 : fd_txn_instr_t const * instr = &txn->instr[ i ];
55 0 : if( instr->data_sz==0UL ) continue;
56 :
57 0 : fd_acct_addr_t const * prog_id = accounts + instr->program_id;
58 0 : uchar const * instr_data = fd_txn_get_instr_data( instr, payload );
59 :
60 0 : if( fd_memeq( prog_id, fd_solana_ed25519_sig_verify_program_id.key, sizeof(fd_pubkey_t) ) ) {
61 0 : num_ed25519_instruction_signatures += (ulong)instr_data[ 0 ];
62 0 : } else if( fd_memeq( prog_id, fd_solana_keccak_secp_256k_program_id.key, sizeof(fd_pubkey_t) ) ) {
63 0 : num_secp256k1_instruction_signatures += (ulong)instr_data[ 0 ];
64 0 : } else if( fd_memeq( prog_id, fd_solana_secp256r1_program_id.key, sizeof(fd_pubkey_t) ) ) {
65 0 : num_secp256r1_instruction_signatures += (ulong)instr_data[ 0 ];
66 0 : }
67 0 : }
68 :
69 : /* No direct permalink, just factored out for readability */
70 0 : ulong secp256k1_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_SECP256K1_SIGNATURE, num_secp256k1_instruction_signatures );
71 :
72 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L155-L160 */
73 0 : ulong ed25519_verify_cost;
74 0 : if( FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, ed25519_precompile_verify_strict ) ) {
75 0 : ed25519_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_ED25519_SIGNATURE, num_ed25519_instruction_signatures );
76 0 : } else {
77 0 : ed25519_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_NON_STRICT_ED25519_SIGNATURE, num_ed25519_instruction_signatures );
78 0 : }
79 :
80 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L162-L167 */
81 0 : ulong secp256r1_verify_cost = 0UL;
82 0 : if( FD_FEATURE_ACTIVE_BANK( txn_ctx->bank, enable_secp256r1_precompile ) ) {
83 0 : secp256r1_verify_cost = fd_ulong_sat_mul( FD_PACK_COST_PER_SECP256R1_SIGNATURE, num_secp256r1_instruction_signatures );
84 0 : }
85 :
86 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L169-L186 */
87 0 : return fd_ulong_sat_add( signature_cost,
88 0 : fd_ulong_sat_add( secp256k1_verify_cost,
89 0 : fd_ulong_sat_add( ed25519_verify_cost,
90 0 : secp256r1_verify_cost ) ) );
91 0 : }
92 :
93 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L190-L192 */
94 : FD_FN_PURE static inline ulong
95 0 : get_write_lock_cost( ulong num_write_locks ) {
96 0 : return fd_ulong_sat_mul( num_write_locks, FD_WRITE_LOCK_UNITS );
97 0 : }
98 :
99 : /* Loop through all instructions here and deserialize the instruction data to try to determine any
100 : system program allocations done.
101 :
102 : https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L367-L386 */
103 : static inline ulong
104 0 : calculate_allocated_accounts_data_size( fd_exec_txn_ctx_t const * txn_ctx ) {
105 0 : fd_txn_t const * txn = TXN( &txn_ctx->txn );
106 0 : void const * payload = txn_ctx->txn.payload;
107 :
108 0 : ulong allocated_accounts_data_size = 0UL;
109 0 : for( ushort i=0; i<txn->instr_cnt; i++ ) {
110 0 : fd_txn_instr_t const * instr = &txn->instr[ i ];
111 0 : fd_acct_addr_t const * accounts = fd_txn_get_acct_addrs( txn, payload );
112 0 : fd_acct_addr_t const * prog_id = accounts + instr->program_id;
113 0 : uchar const * instr_data = fd_txn_get_instr_data( instr, payload );
114 :
115 0 : if( instr->data_sz==0UL || !fd_memeq( prog_id, &fd_solana_system_program_id, sizeof(fd_pubkey_t) ) ) continue;
116 :
117 0 : fd_bincode_decode_ctx_t ctx = {
118 0 : .data = instr_data,
119 0 : .dataend = instr_data + instr->data_sz,
120 0 : };
121 :
122 0 : ulong total_sz = 0UL;
123 0 : int err = fd_system_program_instruction_decode_footprint( &ctx, &total_sz );
124 0 : if( FD_UNLIKELY( err ) ) continue;
125 :
126 0 : uchar buf[total_sz];
127 0 : fd_system_program_instruction_t * instruction = fd_system_program_instruction_decode( buf, &ctx );
128 0 : if( FD_UNLIKELY( !instruction ) ) continue;
129 :
130 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L330-L346 */
131 0 : ulong space = 0UL;
132 :
133 0 : switch( instruction->discriminant ) {
134 0 : case fd_system_program_instruction_enum_create_account: {
135 0 : space = instruction->inner.create_account.space;
136 0 : break;
137 0 : }
138 0 : case fd_system_program_instruction_enum_create_account_with_seed: {
139 0 : space = instruction->inner.create_account_with_seed.space;
140 0 : break;
141 0 : }
142 0 : case fd_system_program_instruction_enum_allocate: {
143 0 : space = instruction->inner.allocate;
144 0 : break;
145 0 : }
146 0 : case fd_system_program_instruction_enum_allocate_with_seed: {
147 0 : space = instruction->inner.allocate_with_seed.space;
148 0 : break;
149 0 : }
150 0 : }
151 :
152 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L373-L380 */
153 0 : if( FD_UNLIKELY( space>FD_RUNTIME_ACC_SZ_MAX ) ) return 0UL;
154 :
155 0 : allocated_accounts_data_size = fd_ulong_sat_add( allocated_accounts_data_size, space );
156 0 : }
157 :
158 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L396-L397 */
159 0 : return fd_ulong_min( 2UL*FD_RUNTIME_ACC_SZ_MAX, allocated_accounts_data_size );
160 0 : }
161 :
162 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L123-L149 */
163 : static inline fd_transaction_cost_t
164 : calculate_non_vote_transaction_cost( fd_exec_txn_ctx_t const * txn_ctx,
165 : ulong loaded_accounts_data_size_cost,
166 0 : ulong data_bytes_cost ) {
167 :
168 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L132 */
169 0 : ulong signature_cost = get_signature_cost( txn_ctx );
170 :
171 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L133 */
172 0 : ulong write_lock_cost = get_write_lock_cost( fd_txn_account_cnt( TXN( &txn_ctx->txn ), FD_TXN_ACCT_CAT_WRITABLE ) );
173 :
174 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L135-L136 */
175 0 : ulong allocated_accounts_data_size = calculate_allocated_accounts_data_size( txn_ctx );
176 :
177 0 : return (fd_transaction_cost_t){ .discriminant = fd_transaction_cost_enum_transaction,
178 0 : .inner = {
179 0 : .transaction = {
180 0 : .signature_cost = signature_cost,
181 0 : .write_lock_cost = write_lock_cost,
182 0 : .data_bytes_cost = data_bytes_cost,
183 0 : .programs_execution_cost = fd_ulong_sat_sub( txn_ctx->compute_budget_details.compute_unit_limit,
184 0 : txn_ctx->compute_budget_details.compute_meter ),
185 0 : .loaded_accounts_data_size_cost = loaded_accounts_data_size_cost,
186 0 : .allocated_accounts_data_size = allocated_accounts_data_size,
187 0 : }
188 0 : }
189 0 : };
190 0 : }
191 :
192 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L26-L42 */
193 : FD_FN_PURE static inline ulong
194 0 : transaction_cost_sum( fd_transaction_cost_t const * txn_cost ) {
195 0 : switch( txn_cost->discriminant ) {
196 0 : case fd_transaction_cost_enum_simple_vote: {
197 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L38 */
198 0 : return FD_PACK_SIMPLE_VOTE_COST;
199 0 : }
200 0 : case fd_transaction_cost_enum_transaction: {
201 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/transaction_cost.rs#L164-L171 */
202 0 : fd_usage_cost_details_t const * usage_cost = &txn_cost->inner.transaction;
203 0 : ulong cost = 0UL;
204 :
205 0 : cost = fd_ulong_sat_add( cost, usage_cost->signature_cost );
206 0 : cost = fd_ulong_sat_add( cost, usage_cost->write_lock_cost );
207 0 : cost = fd_ulong_sat_add( cost, usage_cost->data_bytes_cost );
208 0 : cost = fd_ulong_sat_add( cost, usage_cost->programs_execution_cost );
209 0 : cost = fd_ulong_sat_add( cost, usage_cost->loaded_accounts_data_size_cost );
210 :
211 0 : return cost;
212 0 : }
213 0 : default: {
214 0 : __builtin_unreachable();
215 0 : }
216 0 : }
217 0 : }
218 :
219 : FD_FN_PURE static inline ulong
220 0 : get_allocated_accounts_data_size( fd_transaction_cost_t const * txn_cost ) {
221 0 : switch( txn_cost->discriminant ) {
222 0 : case fd_transaction_cost_enum_simple_vote:
223 0 : return 0UL;
224 0 : case fd_transaction_cost_enum_transaction:
225 0 : return txn_cost->inner.transaction.allocated_accounts_data_size;
226 0 : default:
227 0 : __builtin_unreachable();
228 0 : }
229 0 : }
230 :
231 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L277-L322 */
232 : static inline int
233 : would_fit( fd_cost_tracker_t const * cost_tracker,
234 : fd_exec_txn_ctx_t const * txn_ctx,
235 0 : fd_transaction_cost_t const * tx_cost ) {
236 :
237 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L281 */
238 0 : ulong cost = transaction_cost_sum( tx_cost );
239 :
240 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L283-L288 */
241 0 : if( fd_transaction_cost_is_simple_vote( tx_cost ) ) {
242 0 : if( FD_UNLIKELY( fd_ulong_sat_add( cost_tracker->vote_cost, cost )>cost_tracker->vote_cost_limit ) ) {
243 0 : return FD_COST_TRACKER_ERROR_WOULD_EXCEED_VOTE_MAX_LIMIT;
244 0 : }
245 0 : }
246 :
247 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L290-L293 */
248 0 : if( FD_UNLIKELY( fd_ulong_sat_add( cost_tracker->block_cost, cost )>cost_tracker->block_cost_limit ) ) {
249 0 : return FD_COST_TRACKER_ERROR_WOULD_EXCEED_BLOCK_MAX_LIMIT;
250 0 : }
251 :
252 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L295-L298 */
253 0 : if( FD_UNLIKELY( cost>cost_tracker->account_cost_limit ) ) {
254 0 : return FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT;
255 0 : }
256 :
257 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L300-L301 */
258 0 : ulong allocated_accounts_data_size = fd_ulong_sat_add( cost_tracker->allocated_accounts_data_size,
259 0 : get_allocated_accounts_data_size( tx_cost ) );
260 :
261 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L303-L304 */
262 0 : if( FD_UNLIKELY( allocated_accounts_data_size>FD_MAX_BLOCK_ACCOUNTS_DATA_SIZE_DELTA ) ) {
263 0 : return FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_DATA_BLOCK_LIMIT;
264 0 : }
265 :
266 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L308-L319 */
267 0 : fd_account_cost_map_t * map = fd_cost_tracker_account_cost_map_get( cost_tracker );
268 0 : if( FD_UNLIKELY( !map ) ) {
269 0 : FD_LOG_CRIT(( "failed to get account cost map" ));
270 0 : }
271 :
272 0 : fd_account_cost_t * pool = fd_cost_tracker_account_cost_pool_get( cost_tracker );
273 0 : if( FD_UNLIKELY( !pool ) ) {
274 0 : FD_LOG_CRIT(( "failed to get account cost pool" ));
275 0 : }
276 :
277 :
278 0 : for( ulong i=0UL; i<txn_ctx->accounts_cnt; i++ ) {
279 0 : if( !fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, (ushort)i ) ) continue;
280 :
281 0 : fd_pubkey_t const * writable_acc = &txn_ctx->account_keys[i];
282 :
283 0 : fd_account_cost_t const * chained_cost = fd_account_cost_map_ele_query_const( map, writable_acc, NULL, pool );
284 0 : if( chained_cost ) {
285 0 : if( FD_UNLIKELY( fd_ulong_sat_add( chained_cost->cost, cost )>cost_tracker->account_cost_limit ) ) {
286 0 : return FD_COST_TRACKER_ERROR_WOULD_EXCEED_ACCOUNT_MAX_LIMIT;
287 0 : }
288 0 : }
289 0 : }
290 :
291 0 : return FD_COST_TRACKER_SUCCESS;
292 0 : }
293 :
294 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L352-L372 */
295 : static inline void
296 : add_transaction_execution_cost( fd_cost_tracker_t * cost_tracker,
297 : fd_exec_txn_ctx_t const * txn_ctx,
298 : fd_transaction_cost_t const * tx_cost,
299 0 : ulong adjustment ) {
300 :
301 0 : fd_account_cost_map_t * map = fd_cost_tracker_account_cost_map_get( cost_tracker );
302 0 : if( FD_UNLIKELY( !map ) ) {
303 0 : FD_LOG_CRIT(( "failed to get account cost map" ));
304 0 : }
305 :
306 0 : fd_account_cost_t * pool = fd_cost_tracker_account_cost_pool_get( cost_tracker );
307 0 : if( FD_UNLIKELY( !pool ) ) {
308 0 : FD_LOG_CRIT(( "failed to get account cost pool" ));
309 0 : }
310 :
311 0 : for( ulong i=0UL; i<txn_ctx->accounts_cnt; i++ ) {
312 0 : if( !fd_exec_txn_ctx_account_is_writable_idx( txn_ctx, (ushort)i ) ) continue;
313 :
314 0 : fd_pubkey_t const * writable_acc = &txn_ctx->account_keys[i];
315 :
316 0 : fd_account_cost_t * account_cost = fd_account_cost_map_ele_query( map, writable_acc, NULL, pool );
317 0 : if( account_cost==NULL ) {
318 :
319 0 : if( FD_UNLIKELY( !fd_account_cost_pool_free( pool ) ) ) {
320 0 : FD_LOG_CRIT(( "There are no free accounts in account costs pool" ));
321 0 : }
322 :
323 0 : account_cost = fd_account_cost_pool_ele_acquire( pool );
324 0 : if( FD_UNLIKELY( !account_cost ) ) {
325 0 : FD_LOG_CRIT(( "Failed to acquire account cost" ));
326 0 : }
327 0 : account_cost->account = *writable_acc;
328 0 : account_cost->cost = adjustment;
329 :
330 0 : fd_account_cost_map_ele_insert( map, account_cost, pool );
331 0 : } else {
332 0 : account_cost->cost = fd_ulong_sat_add( account_cost->cost, adjustment );
333 0 : }
334 0 : }
335 :
336 0 : cost_tracker->block_cost = fd_ulong_sat_add( cost_tracker->block_cost, adjustment );
337 0 : if( fd_transaction_cost_is_simple_vote( tx_cost ) ) {
338 0 : cost_tracker->vote_cost = fd_ulong_sat_add( cost_tracker->vote_cost, adjustment );
339 0 : }
340 0 : }
341 :
342 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L325-L335 */
343 : static inline void
344 : add_transaction_cost( fd_cost_tracker_t * cost_tracker,
345 : fd_exec_txn_ctx_t const * txn_ctx,
346 0 : fd_transaction_cost_t const * tx_cost ) {
347 : /* Note: We purposely omit signature counts updates since they're not relevant to cost calculations right now. */
348 0 : cost_tracker->allocated_accounts_data_size += get_allocated_accounts_data_size( tx_cost );
349 0 : add_transaction_execution_cost( cost_tracker, txn_ctx, tx_cost, transaction_cost_sum( tx_cost ) );
350 0 : }
351 :
352 : ulong
353 12 : fd_cost_tracker_footprint( void ) {
354 12 : ulong map_chain_cnt = fd_account_cost_map_chain_cnt_est( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT );
355 :
356 12 : ulong l = FD_LAYOUT_INIT;
357 12 : l = FD_LAYOUT_APPEND( l, fd_cost_tracker_align(), sizeof(fd_cost_tracker_t) );
358 12 : l = FD_LAYOUT_APPEND( l, fd_account_cost_pool_align(), fd_account_cost_pool_footprint( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT ) );
359 12 : l = FD_LAYOUT_APPEND( l, fd_account_cost_map_align(), fd_account_cost_map_footprint( map_chain_cnt ) );
360 12 : return FD_LAYOUT_FINI( l, fd_cost_tracker_align() );
361 12 : }
362 :
363 : ulong
364 87 : fd_cost_tracker_align( void ) {
365 : /* The align of the struct should be the max of the underlying data
366 : structures in the cost tracker. In this case, this is the map
367 : and the pool. */
368 87 : return fd_ulong_max( fd_ulong_max( fd_account_cost_map_align(), fd_account_cost_pool_align() ), alignof(fd_cost_tracker_t) );
369 87 : }
370 :
371 : void *
372 : fd_cost_tracker_new( void * mem,
373 : fd_features_t const * features,
374 : ulong slot,
375 9 : ulong seed ) {
376 :
377 9 : if( FD_UNLIKELY( !mem ) ) {
378 3 : FD_LOG_WARNING(( "NULL mem" ));
379 3 : return NULL;
380 3 : }
381 :
382 6 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, fd_cost_tracker_align() ) ) ) {
383 0 : FD_LOG_WARNING(( "misaligned mem" ));
384 0 : return NULL;
385 0 : }
386 :
387 6 : if( FD_UNLIKELY( !features ) ) {
388 3 : FD_LOG_WARNING(( "NULL features" ));
389 3 : return NULL;
390 3 : }
391 :
392 3 : ulong map_chain_cnt = fd_account_cost_map_chain_cnt_est( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT );
393 :
394 3 : FD_SCRATCH_ALLOC_INIT( l, mem );
395 3 : fd_cost_tracker_t * cost_tracker = FD_SCRATCH_ALLOC_APPEND( l, fd_cost_tracker_align(), sizeof(fd_cost_tracker_t) );
396 3 : void * pool_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_account_cost_pool_align(), fd_account_cost_pool_footprint( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT ) );
397 3 : void * map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_account_cost_map_align(), fd_account_cost_map_footprint( map_chain_cnt ) );
398 :
399 3 : if( FD_UNLIKELY( FD_SCRATCH_ALLOC_FINI( l, fd_cost_tracker_align() )!=(ulong)mem+fd_cost_tracker_footprint() ) ) {
400 0 : FD_LOG_WARNING(( "fd_cost_tracker_new: bad layout" ));
401 0 : return NULL;
402 0 : }
403 :
404 3 : if( FD_UNLIKELY( !fd_account_cost_pool_join( fd_account_cost_pool_new( pool_mem, FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT ) ) ) ) {
405 0 : FD_LOG_WARNING(( "failed to allocate memory for cost tracker accounts pool" ));
406 0 : return NULL;
407 0 : }
408 :
409 3 : if( FD_UNLIKELY( !fd_account_cost_map_join( fd_account_cost_map_new( map_mem, map_chain_cnt, seed ) ) ) ) {
410 0 : FD_LOG_WARNING(( "failed to allocate memory for cost tracker accounts map" ));
411 0 : return NULL;
412 0 : }
413 :
414 3 : cost_tracker->pool_offset_ = (ulong)pool_mem - (ulong)mem;
415 3 : cost_tracker->map_offset_ = (ulong)map_mem - (ulong)mem;
416 :
417 : /* Initialize the limits depending on the active feature set */
418 :
419 3 : if( FD_FEATURE_ACTIVE( slot, features, raise_block_limits_to_100m ) ) {
420 3 : cost_tracker->block_cost_limit = FD_MAX_BLOCK_UNITS_SIMD_0286;
421 3 : cost_tracker->vote_cost_limit = FD_MAX_VOTE_UNITS;
422 3 : cost_tracker->account_cost_limit = FD_MAX_WRITABLE_ACCOUNT_UNITS;
423 3 : } else if( FD_FEATURE_ACTIVE( slot, features, raise_block_limits_to_60m ) ) {
424 0 : cost_tracker->block_cost_limit = FD_MAX_BLOCK_UNITS_SIMD_0256;
425 0 : cost_tracker->vote_cost_limit = FD_MAX_VOTE_UNITS;
426 0 : cost_tracker->account_cost_limit = FD_MAX_WRITABLE_ACCOUNT_UNITS;
427 0 : } else {
428 0 : cost_tracker->block_cost_limit = FD_MAX_BLOCK_UNITS_SIMD_0207;
429 0 : cost_tracker->vote_cost_limit = FD_MAX_VOTE_UNITS;
430 0 : cost_tracker->account_cost_limit = FD_MAX_WRITABLE_ACCOUNT_UNITS;
431 0 : }
432 :
433 : /* https://github.com/anza-xyz/agave/blob/v3.0.1/runtime/src/bank.rs#L4059-L4066 */
434 3 : if( FD_FEATURE_ACTIVE( slot, features, raise_account_cu_limit ) ) {
435 3 : cost_tracker->account_cost_limit = fd_ulong_sat_mul( cost_tracker->block_cost_limit, 40UL ) / 100UL;
436 3 : }
437 :
438 : /* Initialize the current accumulated values */
439 :
440 3 : cost_tracker->block_cost = 0UL;
441 3 : cost_tracker->vote_cost = 0UL;
442 3 : cost_tracker->allocated_accounts_data_size = 0UL;
443 :
444 3 : FD_COMPILER_MFENCE();
445 3 : FD_VOLATILE( cost_tracker->magic ) = FD_COST_TRACKER_MAGIC;
446 3 : FD_COMPILER_MFENCE();
447 :
448 3 : return mem;
449 3 : }
450 :
451 : fd_cost_tracker_t *
452 9 : fd_cost_tracker_join( void * mem ) {
453 9 : if( FD_UNLIKELY( !mem ) ) {
454 3 : FD_LOG_WARNING(( "NULL mem" ));
455 3 : return NULL;
456 3 : }
457 :
458 6 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)mem, fd_cost_tracker_align() ) ) ) {
459 0 : FD_LOG_WARNING(( "misaligned mem" ));
460 0 : return NULL;
461 0 : }
462 :
463 6 : fd_cost_tracker_t * cost_tracker = (fd_cost_tracker_t *)mem;
464 :
465 6 : if( FD_UNLIKELY( cost_tracker->magic!=FD_COST_TRACKER_MAGIC ) ) {
466 3 : FD_LOG_WARNING(( "Invalid cost tracker magic" ));
467 3 : return NULL;
468 3 : }
469 :
470 3 : ulong map_chain_cnt = fd_account_cost_map_chain_cnt_est( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT );
471 3 : FD_SCRATCH_ALLOC_INIT( l, cost_tracker );
472 3 : cost_tracker = FD_SCRATCH_ALLOC_APPEND( l, fd_cost_tracker_align(), sizeof(fd_cost_tracker_t) );
473 3 : void * pool_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_account_cost_pool_align(), fd_account_cost_pool_footprint( FD_RUNTIME_MAX_WRITABLE_ACCOUNTS_PER_SLOT ) );
474 3 : void * map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_account_cost_map_align(), fd_account_cost_map_footprint( map_chain_cnt ) );
475 :
476 3 : if( FD_UNLIKELY( !fd_account_cost_pool_join( pool_mem ) ) ) {
477 0 : FD_LOG_WARNING(( "failed to join cost tracker accounts pool" ));
478 0 : return NULL;
479 0 : }
480 :
481 3 : if( FD_UNLIKELY( !fd_account_cost_map_join( map_mem ) ) ) {
482 0 : FD_LOG_WARNING(( "failed to join cost tracker accounts map" ));
483 0 : return NULL;
484 0 : }
485 :
486 3 : return cost_tracker;
487 3 : }
488 :
489 : int
490 : fd_cost_tracker_calculate_cost_and_add( fd_cost_tracker_t * cost_tracker,
491 0 : fd_exec_txn_ctx_t const * txn_ctx ) {
492 0 : if( FD_UNLIKELY( !txn_ctx ) ) {
493 0 : return FD_COST_TRACKER_SUCCESS;
494 0 : }
495 :
496 : /* https://github.com/anza-xyz/agave/blob/v2.1.0/cost-model/src/cost_model.rs#L83-L85 */
497 0 : fd_transaction_cost_t txn_cost;
498 0 : if( fd_txn_is_simple_vote_transaction( TXN( &txn_ctx->txn ), txn_ctx->txn.payload ) ) {
499 0 : txn_cost = (fd_transaction_cost_t){ .discriminant = fd_transaction_cost_enum_simple_vote };
500 0 : } else {
501 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L78-L81 */
502 0 : ulong loaded_accounts_data_size_cost = fd_cost_tracker_calculate_loaded_accounts_data_size_cost( txn_ctx );
503 :
504 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L82-L83 */
505 0 : ulong instructions_data_cost = get_instructions_data_cost( txn_ctx );
506 :
507 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_model.rs#L85-L93 */
508 0 : txn_cost = calculate_non_vote_transaction_cost( txn_ctx, loaded_accounts_data_size_cost, instructions_data_cost );
509 0 : }
510 :
511 : /* https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L167 */
512 0 : int err = would_fit( cost_tracker, txn_ctx, &txn_cost );
513 0 : if( FD_UNLIKELY( err ) ) return err;
514 :
515 : /* We don't need `updated_costliest_account_cost` since it seems to be
516 : for a different use case other than validating block cost limits.
517 : https://github.com/anza-xyz/agave/blob/v2.2.0/cost-model/src/cost_tracker.rs#L168 */
518 0 : add_transaction_cost( cost_tracker, txn_ctx, &txn_cost );
519 0 : return FD_COST_TRACKER_SUCCESS;
520 0 : }
|