Line data Source code
1 : #ifndef HEADER_fd_src_flamenco_runtime_fd_runtime_stack_tmpl_h 2 : #define HEADER_fd_src_flamenco_runtime_fd_runtime_stack_tmpl_h 3 : 4 : /* The fd_runtime_stack_t container instantiations and constructors. 5 : fd_runtime_stack.h only declares the map types; include this header 6 : to use the maps or to size/create a runtime stack. */ 7 : 8 : #include "fd_runtime_stack.h" 9 : #include "../../util/fd_hash32.h" 10 : 11 : #define MAP_NAME fd_vote_rewards_map 12 : #define MAP_KEY_T fd_pubkey_t 13 : #define MAP_ELE_T fd_vote_rewards_t 14 240 : #define MAP_KEY pubkey 15 247 : #define MAP_KEY_EQ(k0,k1) (!memcmp( k0, k1, sizeof(fd_pubkey_t) )) 16 556 : #define MAP_KEY_HASH(key,seed) (fd_hash32( (key)->uc, (seed) )) 17 347 : #define MAP_NEXT next 18 1693 : #define MAP_IDX_T uint 19 : #include "../../util/tmpl/fd_map_chain.c" 20 : 21 : #define MAP_NAME fd_stake_accum_map 22 : #define MAP_KEY_T fd_pubkey_t 23 : #define MAP_ELE_T fd_stake_accum_t 24 275 : #define MAP_KEY pubkey 25 351 : #define MAP_KEY_EQ(k0,k1) (!memcmp( k0, k1, sizeof(fd_pubkey_t) )) 26 898 : #define MAP_KEY_HASH(key,seed) (fd_hash32( (key)->uc, (seed) )) 27 414 : #define MAP_NEXT next 28 1433 : #define MAP_IDX_T uint 29 : #include "../../util/tmpl/fd_map_chain.c" 30 : 31 : FD_FN_CONST static inline ulong 32 84 : fd_runtime_stack_align( void ) { 33 84 : return 128UL; 34 84 : } 35 : 36 : /* stake_accum and bpf_migration share a region, see fd_runtime_stack_t */ 37 : 38 : FD_FN_CONST static inline ulong 39 60 : fd_runtime_stack_private_stake_accum_align( void ) { 40 60 : return fd_ulong_max( 128UL, alignof(fd_runtime_stack_bpf_migration_t) ); 41 60 : } 42 : 43 : FD_FN_CONST static inline ulong 44 36 : fd_runtime_stack_private_stake_accum_footprint( ulong max_staked_vote_accounts ) { 45 36 : return fd_ulong_max( sizeof(fd_stake_accum_t) * max_staked_vote_accounts, sizeof(fd_runtime_stack_bpf_migration_t) ); 46 36 : } 47 : 48 : FD_FN_PURE static inline ulong 49 : fd_runtime_stack_footprint( ulong max_vote_accounts, 50 : ulong max_staked_vote_accounts, 51 24 : ulong max_stake_accounts ) { 52 24 : ulong vote_chain_cnt = fd_vote_rewards_map_chain_cnt_est( max_vote_accounts ); 53 24 : ulong stake_chain_cnt = fd_stake_accum_map_chain_cnt_est( max_staked_vote_accounts ); 54 24 : ulong l = FD_LAYOUT_INIT; 55 24 : l = FD_LAYOUT_APPEND( l, alignof(fd_runtime_stack_t), sizeof(fd_runtime_stack_t) ); 56 24 : l = FD_LAYOUT_APPEND( l, alignof(fd_vote_stake_weight_t), sizeof(fd_vote_stake_weight_t) * max_vote_accounts ); 57 24 : l = FD_LAYOUT_APPEND( l, alignof(fd_stake_weight_t), sizeof(fd_stake_weight_t) * max_vote_accounts ); 58 24 : l = FD_LAYOUT_APPEND( l, 128UL, sizeof(fd_vote_rewards_t) * max_vote_accounts ); 59 24 : l = FD_LAYOUT_APPEND( l, fd_vote_rewards_map_align(), fd_vote_rewards_map_footprint( vote_chain_cnt ) ); 60 24 : l = FD_LAYOUT_APPEND( l, fd_runtime_stack_private_stake_accum_align(), fd_runtime_stack_private_stake_accum_footprint( max_staked_vote_accounts ) ); 61 24 : l = FD_LAYOUT_APPEND( l, fd_stake_accum_map_align(), fd_stake_accum_map_footprint( stake_chain_cnt ) ); 62 24 : l = FD_LAYOUT_APPEND( l, alignof(fd_calculated_stake_result_t), sizeof(fd_calculated_stake_result_t) * max_stake_accounts ); 63 24 : return FD_LAYOUT_FINI( l, fd_runtime_stack_align() ); 64 24 : } 65 : 66 : static inline void * 67 : fd_runtime_stack_new( void * shmem, 68 : ulong max_vote_accounts, 69 : ulong max_staked_vote_accounts, 70 : ulong max_stake_accounts, 71 12 : ulong seed ) { 72 12 : if( FD_UNLIKELY( !shmem ) ) return NULL; 73 12 : ulong vote_chain_cnt = fd_vote_rewards_map_chain_cnt_est( max_vote_accounts ); 74 12 : ulong stake_chain_cnt = fd_stake_accum_map_chain_cnt_est( max_staked_vote_accounts ); 75 12 : FD_SCRATCH_ALLOC_INIT( l, shmem ); 76 12 : fd_runtime_stack_t * runtime_stack = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_runtime_stack_t), sizeof(fd_runtime_stack_t) ); 77 12 : fd_vote_stake_weight_t * stake_weights = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_vote_stake_weight_t), sizeof(fd_vote_stake_weight_t) * max_vote_accounts ); 78 12 : fd_stake_weight_t * id_weights = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_stake_weight_t), sizeof(fd_stake_weight_t) * max_vote_accounts ); 79 12 : fd_vote_rewards_t * vote_ele = FD_SCRATCH_ALLOC_APPEND( l, 128UL, sizeof(fd_vote_rewards_t) * max_vote_accounts ); 80 12 : void * vote_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_vote_rewards_map_align(), fd_vote_rewards_map_footprint( vote_chain_cnt ) ); 81 12 : fd_stake_accum_t * stake_accum = FD_SCRATCH_ALLOC_APPEND( l, fd_runtime_stack_private_stake_accum_align(), fd_runtime_stack_private_stake_accum_footprint( max_staked_vote_accounts ) ); 82 12 : void * stake_accum_map_mem = FD_SCRATCH_ALLOC_APPEND( l, fd_stake_accum_map_align(), fd_stake_accum_map_footprint( stake_chain_cnt ) ); 83 12 : fd_calculated_stake_result_t * stake_results = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_calculated_stake_result_t), sizeof(fd_calculated_stake_result_t) * max_stake_accounts ); 84 12 : if( FD_UNLIKELY( FD_SCRATCH_ALLOC_FINI( l, fd_runtime_stack_align() )!=(ulong)shmem + fd_runtime_stack_footprint( max_vote_accounts, max_staked_vote_accounts, max_stake_accounts ) ) ) { 85 0 : FD_LOG_WARNING(( "fd_runtime_stack_new: bad layout" )); 86 0 : return NULL; 87 0 : } 88 : 89 12 : runtime_stack->max_vote_accounts = max_vote_accounts; 90 12 : runtime_stack->max_staked_vote_accounts = max_staked_vote_accounts; 91 12 : runtime_stack->max_stake_accounts = max_stake_accounts; 92 12 : runtime_stack->stakes.stake_weights = stake_weights; 93 12 : runtime_stack->stakes.id_weights = id_weights; 94 12 : runtime_stack->stakes.vote_ele = vote_ele; 95 12 : runtime_stack->stakes.stake_results = stake_results; 96 12 : runtime_stack->stakes.stake_accum = stake_accum; 97 12 : runtime_stack->bpf_migration = (fd_runtime_stack_bpf_migration_t *)stake_accum; 98 : 99 12 : runtime_stack->stakes.stake_accum_map = fd_stake_accum_map_join( fd_stake_accum_map_new( stake_accum_map_mem, stake_chain_cnt, seed ) ); 100 12 : if( FD_UNLIKELY( !runtime_stack->stakes.stake_accum_map ) ) { 101 0 : FD_LOG_WARNING(( "fd_runtime_stack_new: bad map" )); 102 0 : return NULL; 103 0 : } 104 : 105 12 : runtime_stack->stakes.vote_map = fd_vote_rewards_map_join( fd_vote_rewards_map_new( vote_map_mem, vote_chain_cnt, seed ) ); 106 12 : if( FD_UNLIKELY( !runtime_stack->stakes.vote_map ) ) { 107 0 : FD_LOG_WARNING(( "fd_runtime_stack_new: bad map" )); 108 0 : return NULL; 109 0 : } 110 : 111 12 : return shmem; 112 12 : } 113 : 114 : FD_FN_CONST static inline fd_runtime_stack_t * 115 12 : fd_runtime_stack_join( void * shruntime_stack ) { 116 12 : return (fd_runtime_stack_t *)shruntime_stack; 117 12 : } 118 : 119 : #endif /* HEADER_fd_src_flamenco_runtime_fd_runtime_stack_tmpl_h */