|
| 1 | +#include "fd_blockhashes.h" |
| 2 | + |
| 3 | +fd_blockhashes_t * |
| 4 | +fd_blockhashes_init( fd_blockhashes_t * mem, |
| 5 | + ulong seed ) { |
| 6 | + if( FD_UNLIKELY( !mem ) ) { |
| 7 | + FD_LOG_WARNING(( "NULL mem" )); |
| 8 | + return NULL; |
| 9 | + } |
| 10 | + FD_TEST( fd_blockhash_deq_join( fd_blockhash_deq_new( &mem->d ) ) ); |
| 11 | + memset( mem->d.deque, 0x5a, sizeof(fd_blockhash_info_t) * FD_BLOCKHASHES_MAX ); |
| 12 | + FD_TEST( fd_blockhash_map_join( fd_blockhash_map_new( mem, FD_BLOCKHASH_MAP_CHAIN_MAX, seed ) ) ); |
| 13 | + return mem; |
| 14 | +} |
| 15 | + |
| 16 | +fd_blockhashes_t * |
| 17 | +fd_blockhashes_recover( fd_blockhashes_t * blockhashes, |
| 18 | + fd_hash_hash_age_pair_t const * ages, |
| 19 | + ulong age_cnt, |
| 20 | + ulong seed ) { |
| 21 | + FD_TEST( fd_blockhashes_init( blockhashes, seed ) ); |
| 22 | + if( FD_UNLIKELY( !age_cnt || age_cnt>FD_BLOCKHASHES_MAX ) ) { |
| 23 | + FD_LOG_WARNING(( "Corrupt snapshot: blockhash queue age count %lu is out of range [1,%d)", age_cnt, FD_BLOCKHASHES_MAX )); |
| 24 | + } |
| 25 | + |
| 26 | + /* For depressing reasons, the ages array is not sorted when ingested |
| 27 | + from a snapshot. The hash_index field is also not validated. |
| 28 | + Firedancer assumes that the sequence of hash_index numbers is |
| 29 | + gapless and does not wrap around. */ |
| 30 | + |
| 31 | + ulong seq_min = ULONG_MAX-1; |
| 32 | + for( ulong i=0UL; i<age_cnt; i++ ) { |
| 33 | + seq_min = fd_ulong_min( seq_min, ages[i].val.hash_index ); |
| 34 | + } |
| 35 | + ulong seq_max; |
| 36 | + if( FD_UNLIKELY( __builtin_uaddl_overflow( seq_min, age_cnt, &seq_max ) ) ) { |
| 37 | + FD_LOG_WARNING(( "Corrupt snapshot: blockhash queue sequence number wraparound (seq_min=%lu age_cnt=%lu)", seq_min, age_cnt )); |
| 38 | + return NULL; |
| 39 | + } |
| 40 | + |
| 41 | + /* Reset */ |
| 42 | + |
| 43 | + for( ulong i=0UL; i<age_cnt; i++ ) { |
| 44 | + fd_blockhash_info_t * ele = fd_blockhash_deq_push_tail_nocopy( blockhashes->d.deque ); |
| 45 | + memset( ele, 0, sizeof(fd_blockhash_info_t) ); |
| 46 | + } |
| 47 | + |
| 48 | + /* Load hashes */ |
| 49 | + |
| 50 | + for( ulong i=0UL; i<age_cnt; i++ ) { |
| 51 | + fd_hash_hash_age_pair_t const * elem = &ages[i]; |
| 52 | + ulong idx; |
| 53 | + if( FD_UNLIKELY( __builtin_usubl_overflow( elem->val.hash_index, seq_min, &idx ) ) ) { |
| 54 | + FD_LOG_WARNING(( "Corrupt snapshot: gap in blockhash queue (seq=[%lu,%lu) idx=%lu)", |
| 55 | + seq_min, seq_max, elem->val.hash_index )); |
| 56 | + return NULL; |
| 57 | + } |
| 58 | + fd_blockhash_info_t * info = &blockhashes->d.deque[ idx ]; |
| 59 | + if( FD_UNLIKELY( info->exists ) ) { |
| 60 | + FD_LOG_HEXDUMP_NOTICE(( "info", info, sizeof(fd_blockhash_info_t) )); |
| 61 | + FD_LOG_WARNING(( "Corrupt snapshot: duplicate blockhash queue index %lu", idx )); |
| 62 | + return NULL; |
| 63 | + } |
| 64 | + info->exists = 1; |
| 65 | + info->hash = elem->key; |
| 66 | + info->fee_calculator = elem->val.fee_calculator; |
| 67 | + fd_blockhash_map_idx_insert( blockhashes->map, idx, blockhashes->d.deque ); |
| 68 | + } |
| 69 | + |
| 70 | + return blockhashes; |
| 71 | +} |
| 72 | + |
| 73 | +static void |
| 74 | +fd_blockhashes_pop_old( fd_blockhashes_t * blockhashes ) { |
| 75 | + if( FD_UNLIKELY( fd_blockhash_deq_empty( blockhashes->d.deque ) ) ) return; |
| 76 | + fd_blockhash_info_t * info = fd_blockhash_deq_pop_head_nocopy( blockhashes->d.deque ); |
| 77 | + info->exists = 0; |
| 78 | + fd_blockhash_map_ele_remove( blockhashes->map, &info->hash, NULL, blockhashes->d.deque ); |
| 79 | +} |
| 80 | + |
| 81 | +void |
| 82 | +fd_blockhashes_pop_new( fd_blockhashes_t * blockhashes ) { |
| 83 | + if( FD_UNLIKELY( fd_blockhash_deq_empty( blockhashes->d.deque ) ) ) return; |
| 84 | + fd_blockhash_info_t * info = fd_blockhash_deq_pop_tail_nocopy( blockhashes->d.deque ); |
| 85 | + info->exists = 0; |
| 86 | + fd_blockhash_map_ele_remove( blockhashes->map, &info->hash, NULL, blockhashes->d.deque ); |
| 87 | +} |
| 88 | + |
| 89 | +fd_blockhash_info_t * |
| 90 | +fd_blockhashes_push_new( fd_blockhashes_t * blockhashes, |
| 91 | + fd_hash_t const * hash ) { |
| 92 | + if( FD_UNLIKELY( fd_blockhash_deq_full( blockhashes->d.deque ) ) ) { |
| 93 | + fd_blockhashes_pop_old( blockhashes ); |
| 94 | + } |
| 95 | + if( FD_UNLIKELY( fd_blockhash_map_idx_query( blockhashes->map, hash, ULONG_MAX, blockhashes->d.deque )!=ULONG_MAX ) ) { |
| 96 | + char bh_cstr[ FD_BASE58_ENCODED_32_SZ ]; fd_base58_encode_32( hash->uc, NULL, bh_cstr ); |
| 97 | + FD_LOG_CRIT(( "Attempted to register duplicate blockhash %s", bh_cstr )); |
| 98 | + } |
| 99 | + |
| 100 | + fd_blockhash_info_t * info = fd_blockhash_deq_push_tail_nocopy( blockhashes->d.deque ); |
| 101 | + *info = (fd_blockhash_info_t) { .hash = *hash, .exists = 1 }; |
| 102 | + |
| 103 | + fd_blockhash_map_ele_insert( blockhashes->map, info, blockhashes->d.deque ); |
| 104 | + |
| 105 | + return info; |
| 106 | +} |
| 107 | + |
| 108 | +fd_blockhash_info_t * |
| 109 | +fd_blockhashes_push_old( fd_blockhashes_t * blockhashes, |
| 110 | + fd_hash_t const * hash ) { |
| 111 | + if( FD_UNLIKELY( fd_blockhash_deq_full( blockhashes->d.deque ) ) ) { |
| 112 | + return NULL; |
| 113 | + } |
| 114 | + if( FD_UNLIKELY( fd_blockhash_map_idx_query( blockhashes->map, hash, ULONG_MAX, blockhashes->d.deque )!=ULONG_MAX ) ) { |
| 115 | + char bh_cstr[ FD_BASE58_ENCODED_32_SZ ]; fd_base58_encode_32( hash->uc, NULL, bh_cstr ); |
| 116 | + FD_LOG_CRIT(( "Attempted to register duplicate blockhash %s", bh_cstr )); |
| 117 | + } |
| 118 | + |
| 119 | + fd_blockhash_info_t * info = fd_blockhash_deq_push_head_nocopy( blockhashes->d.deque ); |
| 120 | + *info = (fd_blockhash_info_t) { .hash = *hash, .exists = 1 }; |
| 121 | + |
| 122 | + fd_blockhash_map_ele_insert( blockhashes->map, info, blockhashes->d.deque ); |
| 123 | + |
| 124 | + return info; |
| 125 | +} |
| 126 | + |
| 127 | + |
| 128 | +FD_FN_PURE int |
| 129 | +fd_blockhashes_check_age( fd_blockhashes_t const * blockhashes, |
| 130 | + fd_hash_t const * blockhash, |
| 131 | + ulong max_age ) { |
| 132 | + ulong const idx = fd_blockhash_map_idx_query_const( blockhashes->map, blockhash, ULONG_MAX, blockhashes->d.deque ); |
| 133 | + if( FD_UNLIKELY( idx==ULONG_MAX ) ) return 0; |
| 134 | + /* Derive distance from tail (end) */ |
| 135 | + ulong const max = fd_blockhash_deq_max( blockhashes->d.deque ); |
| 136 | + ulong const end = (blockhashes->d.end - 1) & (max-1); |
| 137 | + ulong const age = end + fd_ulong_if( idx<=end, 0UL, max ) - idx; |
| 138 | + return age<=max_age; |
| 139 | +} |
0 commit comments