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