Line data Source code
1 : #include "fd_pack_rebate_sum.h" 2 : #include "fd_pack.h" 3 : 4 : static const fd_acct_addr_t null_addr = { 0 }; 5 : 6 : #define MAP_NAME rmap 7 24003 : #define MAP_T fd_pack_rebate_entry_t 8 105993 : #define MAP_LG_SLOT_CNT 13 9 52536 : #define MAP_KEY_T fd_acct_addr_t 10 61134 : #define MAP_KEY_NULL null_addr 11 : #if FD_HAS_AVX 12 52536 : # define MAP_KEY_INVAL(k) _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) ) 13 : #else 14 : # define MAP_KEY_INVAL(k) MAP_KEY_EQUAL(k, null_addr) 15 : #endif 16 24273 : #define MAP_KEY_EQUAL(k0,k1) (!memcmp((k0).b,(k1).b, FD_TXN_ACCT_ADDR_SZ)) 17 : #define MAP_KEY_EQUAL_IS_SLOW 1 18 : #define MAP_MEMOIZE 0 19 28296 : #define MAP_KEY_HASH(key) ((uint)fd_hash( 132132, (key).b, 32UL )) 20 0 : #define MAP_MOVE(d,s) (__extension__({ FD_LOG_CRIT(( "Tried to move a map value" )); (d)=(s); })) 21 : 22 : #include "../../util/tmpl/fd_map.c" 23 : 24 : 25 : void * 26 6 : fd_pack_rebate_sum_new( void * mem ) { 27 6 : fd_pack_rebate_sum_t * s = (fd_pack_rebate_sum_t *)mem; 28 : 29 6 : s->total_cost_rebate = 0UL; 30 6 : s->vote_cost_rebate = 0UL; 31 6 : s->data_bytes_rebate = 0UL; 32 6 : s->microblock_cnt_rebate = 0UL; 33 6 : s->ib_result = 0; 34 6 : s->writer_cnt = 0U; 35 : 36 6 : rmap_new( s->map ); 37 : 38 : /* Not a good place to put this, but there's not really a better place 39 : for it either. The compiler should eliminate it. */ 40 6 : FD_TEST( rmap_footprint()==sizeof(s->map) ); 41 6 : return mem; 42 6 : } 43 : 44 42 : #define HEADROOM (FD_PACK_REBATE_SUM_CAPACITY-MAX_TXN_PER_MICROBLOCK*FD_TXN_ACCT_ADDR_MAX) 45 : 46 : ulong 47 : fd_pack_rebate_sum_add_txn( fd_pack_rebate_sum_t * s, 48 : fd_txn_p_t const * txns, 49 : fd_acct_addr_t const * const * adtl_writable, 50 42 : ulong txn_cnt ) { 51 : /* See end of function for this equation */ 52 42 : if( FD_UNLIKELY( txn_cnt==0UL ) ) return (ulong)((fd_int_max( 0, (int)s->writer_cnt - (int)HEADROOM ) + 1636) / 1637); 53 : 54 33 : int is_initializer_bundle = 1; 55 33 : int ib_success = 1; 56 33 : int any_in_block = 0; 57 : 58 204 : for( ulong i=0UL; i<txn_cnt; i++ ) { 59 171 : fd_txn_p_t const * txn = txns+i; 60 171 : ulong rebated_cus = txn->bank_cu.rebated_cus; 61 171 : int in_block = !!(txn->flags & FD_TXN_P_FLAGS_EXECUTE_SUCCESS); 62 : 63 : /* For IB purposes, treat AlreadyProcessed (7) as success. If one 64 : transaction is an initializer bundle, they all must be, so it's 65 : unclear if the first line should be an |= or an &=, but &= seems 66 : more right. */ 67 171 : is_initializer_bundle &= !!(txn->flags & FD_TXN_P_FLAGS_INITIALIZER_BUNDLE); 68 171 : ib_success &= in_block | ((txn->flags&FD_TXN_P_FLAGS_RESULT_MASK)==(7U<<24)); 69 171 : any_in_block |= in_block; 70 : 71 171 : s->total_cost_rebate += rebated_cus; 72 171 : s->vote_cost_rebate += fd_ulong_if( txn->flags & FD_TXN_P_FLAGS_IS_SIMPLE_VOTE, rebated_cus, 0UL ); 73 171 : s->data_bytes_rebate += fd_ulong_if( !in_block, txn->payload_sz, 0UL ); 74 : 75 171 : if( FD_UNLIKELY( rebated_cus==0UL ) ) continue; 76 : 77 171 : fd_acct_addr_t const * accts = fd_txn_get_acct_addrs( TXN(txn), txn->payload ); 78 171 : for( fd_txn_acct_iter_t iter=fd_txn_acct_iter_init( TXN(txn), FD_TXN_ACCT_CAT_WRITABLE & FD_TXN_ACCT_CAT_IMM ); 79 264 : iter!=fd_txn_acct_iter_end(); iter=fd_txn_acct_iter_next( iter ) ) { 80 : 81 93 : ulong j=fd_txn_acct_iter_idx( iter ); 82 : 83 93 : fd_pack_rebate_entry_t * in_table = rmap_query( s->map, accts[j], NULL ); 84 93 : if( FD_UNLIKELY( !in_table ) ) { 85 66 : in_table = rmap_insert( s->map, accts[j] ); 86 66 : in_table->rebate_cus = 0UL; 87 66 : s->inserted[ s->writer_cnt++ ] = in_table; 88 66 : } 89 93 : in_table->rebate_cus += rebated_cus; 90 93 : } 91 171 : accts = adtl_writable[i]; 92 171 : if( FD_LIKELY( txn->flags & FD_TXN_P_FLAGS_SANITIZE_SUCCESS ) ) { 93 12063 : for( ulong j=0UL; j<(ulong)TXN(txn)->addr_table_adtl_writable_cnt; j++ ) { 94 11922 : fd_pack_rebate_entry_t * in_table = rmap_query( s->map, accts[j], NULL ); 95 11922 : if( FD_UNLIKELY( !in_table ) ) { 96 11916 : in_table = rmap_insert( s->map, accts[j] ); 97 11916 : in_table->rebate_cus = 0UL; 98 11916 : s->inserted[ s->writer_cnt++ ] = in_table; 99 11916 : } 100 11922 : in_table->rebate_cus += rebated_cus; 101 11922 : } 102 141 : } 103 171 : FD_TEST( s->writer_cnt<=FD_PACK_REBATE_SUM_CAPACITY ); 104 171 : } 105 : 106 33 : int is_bundle = txns->flags & FD_TXN_P_FLAGS_BUNDLE; /* can't mix bundle and non-bundle */ 107 33 : ulong microblock_cnt_rebate = fd_ulong_if( any_in_block, 0UL, fd_ulong_if( is_bundle, txn_cnt, 1UL ) ); 108 33 : s->microblock_cnt_rebate += microblock_cnt_rebate; 109 33 : s->data_bytes_rebate += microblock_cnt_rebate*48UL; /* microblock headers */ 110 : 111 33 : if( FD_UNLIKELY( is_initializer_bundle & (s->ib_result!=-1) ) ) { /* if in -1 state, stay. Shouldn't be possible */ 112 6 : s->ib_result = fd_int_if( ib_success, 1, -1 ); 113 6 : } 114 : 115 : /* We want to make sure that we have enough capacity to insert 31*128 116 : addresses without hitting 5k. Thus, if x is the current value of 117 : writer_cnt, we need to call report at least y times to ensure 118 : x-y*1637 <= 5*1024-31*128 119 : y >= (x-1152)/1637 120 : but y is an integer, so y >= ceiling( (x-1152)/1637 ) */ 121 33 : return (ulong)((fd_int_max( 0, (int)s->writer_cnt - (int)HEADROOM ) + 1636) / 1637); 122 33 : } 123 : 124 : 125 : ulong 126 : fd_pack_rebate_sum_report( fd_pack_rebate_sum_t * s, 127 45 : fd_pack_rebate_t * out ) { 128 45 : if( FD_UNLIKELY( (s->ib_result==0) & (s->total_cost_rebate==0UL) & (s->writer_cnt==0U) ) ) return 0UL; 129 36 : out->total_cost_rebate = s->total_cost_rebate; s->total_cost_rebate = 0UL; 130 36 : out->vote_cost_rebate = s->vote_cost_rebate; s->vote_cost_rebate = 0UL; 131 36 : out->data_bytes_rebate = s->data_bytes_rebate; s->data_bytes_rebate = 0UL; 132 36 : out->microblock_cnt_rebate = s->microblock_cnt_rebate; s->microblock_cnt_rebate = 0UL; 133 36 : out->ib_result = s->ib_result; s->ib_result = 0; 134 : 135 36 : out->writer_cnt = 0U; 136 36 : ulong writer_cnt = fd_ulong_min( s->writer_cnt, 1637UL ); 137 12018 : for( ulong i=0UL; i<writer_cnt; i++ ) { 138 11982 : fd_pack_rebate_entry_t * e = s->inserted[ --(s->writer_cnt) ]; 139 11982 : out->writer_rebates[ out->writer_cnt++ ] = *e; 140 11982 : rmap_remove( s->map, e ); 141 11982 : } 142 : 143 36 : return sizeof(*out)-sizeof(fd_pack_rebate_entry_t)+(out->writer_cnt)*sizeof(fd_pack_rebate_entry_t); 144 45 : } 145 : 146 : void 147 0 : fd_pack_rebate_sum_clear( fd_pack_rebate_sum_t * s ) { 148 0 : s->total_cost_rebate = 0UL; 149 0 : s->vote_cost_rebate = 0UL; 150 0 : s->data_bytes_rebate = 0UL; 151 0 : s->microblock_cnt_rebate = 0UL; 152 0 : s->ib_result = 0; 153 : 154 0 : ulong writer_cnt = s->writer_cnt; 155 0 : for( ulong i=0UL; i<writer_cnt; i++ ) { 156 0 : fd_pack_rebate_entry_t * e = s->inserted[ --(s->writer_cnt) ]; 157 0 : rmap_remove( s->map, e ); 158 0 : } 159 0 : }