Line data Source code
1 : #include "fd_txncache.h"
2 : #include "fd_txncache_private.h"
3 : #include "../../util/log/fd_log.h"
4 :
5 : struct blockcache {
6 : fd_txncache_blockcache_shmem_t * shmem;
7 :
8 : uint * heads; /* The hash table for the blockhash. Each entry is a pointer to the head of a linked list of
9 : transactions that reference this blockhash. As we add transactions to the bucket, the head
10 : pointer is updated to the new item, and the new item is pointed to the previous head. */
11 : ushort * pages; /* A list of the txnpages containing the transactions for this blockcache. */
12 :
13 : descends_set_t * descends; /* Each fork can descend from other forks in the txncache, and this bit vector contains one
14 : value for each fork in the txncache. If this fork descends from some other fork F, then
15 : the bit at index F in descends[] is set. */
16 : };
17 :
18 : typedef struct blockcache blockcache_t;
19 :
20 : struct fd_txncache_private {
21 : fd_txncache_shmem_t * shmem;
22 :
23 : fd_txncache_blockcache_shmem_t * blockcache_shmem_pool;
24 : blockcache_t * blockcache_pool;
25 : blockhash_map_t * blockhash_map;
26 :
27 : ushort * txnpages_free; /* The index in the txnpages array that is free, for each of the free pages. */
28 :
29 : fd_txncache_txnpage_t * txnpages; /* The actual storage for the transactions. The blockcache points to these
30 : pages when storing transactions. Transaction are grouped into pages of
31 : size 16384 to make certain allocation and deallocation operations faster
32 : (just the pages are acquired/released, rather than each txn). */
33 :
34 : ushort * scratch_pages;
35 : uint * scratch_heads;
36 : fd_txncache_txnpage_t * scratch_txnpage;
37 : };
38 :
39 : FD_FN_CONST ulong
40 204 : fd_txncache_align( void ) {
41 204 : return FD_TXNCACHE_ALIGN;
42 204 : }
43 :
44 : FD_FN_CONST ulong
45 102 : fd_txncache_footprint( ulong max_live_slots ) {
46 102 : ulong max_active_slots = FD_TXNCACHE_MAX_BLOCKHASH_DISTANCE+max_live_slots;
47 :
48 102 : ulong l;
49 102 : l = FD_LAYOUT_INIT;
50 102 : l = FD_LAYOUT_APPEND( l, FD_TXNCACHE_SHMEM_ALIGN, sizeof(fd_txncache_t) );
51 102 : l = FD_LAYOUT_APPEND( l, alignof(blockcache_t), max_active_slots*sizeof(blockcache_t) );
52 102 : return FD_LAYOUT_FINI( l, FD_TXNCACHE_ALIGN );
53 102 : }
54 :
55 : void *
56 : fd_txncache_new( void * ljoin,
57 51 : fd_txncache_shmem_t * shmem ) {
58 51 : if( FD_UNLIKELY( !ljoin ) ) {
59 0 : FD_LOG_WARNING(( "NULL ljoin" ));
60 0 : return NULL;
61 0 : }
62 :
63 51 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)ljoin, fd_txncache_align() ) ) ) {
64 0 : FD_LOG_WARNING(( "misaligned ljoin" ));
65 0 : return NULL;
66 0 : }
67 :
68 51 : ulong max_active_slots = shmem->active_slots_max;
69 51 : ulong blockhash_map_chains = fd_ulong_pow2_up( 2UL*shmem->active_slots_max );
70 :
71 51 : ushort _max_txnpages = fd_txncache_max_txnpages( max_active_slots, shmem->txn_per_slot_max );
72 51 : ushort _max_txnpages_per_blockhash = fd_txncache_max_txnpages_per_blockhash( max_active_slots, shmem->txn_per_slot_max );
73 :
74 51 : ulong _descends_footprint = descends_set_footprint( max_active_slots );
75 51 : if( FD_UNLIKELY( !_descends_footprint ) ) {
76 0 : FD_LOG_WARNING(( "invalid max_active_slots" ));
77 0 : return NULL;
78 0 : }
79 :
80 51 : FD_SCRATCH_ALLOC_INIT( l, shmem );
81 51 : fd_txncache_shmem_t * tc = FD_SCRATCH_ALLOC_APPEND( l, FD_TXNCACHE_SHMEM_ALIGN, sizeof(fd_txncache_shmem_t) );
82 51 : void * _blockhash_map = FD_SCRATCH_ALLOC_APPEND( l, blockhash_map_align(), blockhash_map_footprint( blockhash_map_chains ) );
83 51 : void * _blockcache_pool = FD_SCRATCH_ALLOC_APPEND( l, blockcache_pool_align(), blockcache_pool_footprint( max_active_slots ) );
84 51 : void * _blockcache_pages = FD_SCRATCH_ALLOC_APPEND( l, alignof(ushort), max_active_slots*_max_txnpages_per_blockhash*sizeof(ushort) );
85 51 : void * _blockcache_heads = FD_SCRATCH_ALLOC_APPEND( l, alignof(uint), max_active_slots*shmem->txn_per_slot_max*sizeof(uint) );
86 51 : void * _blockcache_descends = FD_SCRATCH_ALLOC_APPEND( l, descends_set_align(), max_active_slots*_descends_footprint );
87 51 : void * _txnpages_free = FD_SCRATCH_ALLOC_APPEND( l, alignof(ushort), _max_txnpages*sizeof(ushort) );
88 51 : void * _txnpages = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_txncache_txnpage_t), _max_txnpages*sizeof(fd_txncache_txnpage_t) );
89 51 : void * _scratch_pages = FD_SCRATCH_ALLOC_APPEND( l, alignof(ushort), _max_txnpages_per_blockhash*sizeof(ushort) );
90 51 : void * _scratch_heads = FD_SCRATCH_ALLOC_APPEND( l, alignof(uint), shmem->txn_per_slot_max*sizeof(uint) );
91 51 : void * _scratch_txnpage = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_txncache_txnpage_t), sizeof(fd_txncache_txnpage_t) );
92 :
93 51 : FD_SCRATCH_ALLOC_INIT( l2, ljoin );
94 51 : fd_txncache_t * ltc = FD_SCRATCH_ALLOC_APPEND( l2, FD_TXNCACHE_ALIGN, sizeof(fd_txncache_t) );
95 51 : void * _local_blockcache_pool = FD_SCRATCH_ALLOC_APPEND( l2, alignof(blockcache_t), max_active_slots*sizeof(blockcache_t) );
96 :
97 51 : ltc->shmem = tc;
98 :
99 51 : ltc->blockcache_pool = (blockcache_t*)_local_blockcache_pool;
100 51 : ltc->blockcache_shmem_pool = blockcache_pool_join( _blockcache_pool );
101 :
102 8712 : for( ulong i=0UL; i<shmem->active_slots_max; i++ ) {
103 8661 : ltc->blockcache_pool[ i ].pages = (ushort *)_blockcache_pages + i*_max_txnpages_per_blockhash;
104 8661 : ltc->blockcache_pool[ i ].heads = (uint *)_blockcache_heads + i*shmem->txn_per_slot_max;
105 8661 : ltc->blockcache_pool[ i ].descends = descends_set_join( (uchar *)_blockcache_descends + i*_descends_footprint );
106 8661 : ltc->blockcache_pool[ i ].shmem = ltc->blockcache_shmem_pool + i;
107 8661 : FD_TEST( ltc->blockcache_pool[ i ].shmem );
108 8661 : }
109 :
110 51 : FD_TEST( ltc->blockcache_shmem_pool );
111 :
112 51 : ltc->blockhash_map = blockhash_map_join( _blockhash_map );
113 51 : FD_TEST( ltc->blockhash_map );
114 :
115 51 : ltc->txnpages_free = (ushort *)_txnpages_free;
116 51 : ltc->txnpages = (fd_txncache_txnpage_t *)_txnpages;
117 :
118 51 : ltc->scratch_pages = _scratch_pages;
119 51 : ltc->scratch_heads = _scratch_heads;
120 51 : ltc->scratch_txnpage = _scratch_txnpage;
121 :
122 51 : return (void *)ltc;
123 51 : }
124 :
125 : fd_txncache_t *
126 51 : fd_txncache_join( void * ljoin ) {
127 51 : if( FD_UNLIKELY( !ljoin ) ) {
128 0 : FD_LOG_WARNING(( "NULL ljoin" ));
129 0 : return NULL;
130 0 : }
131 :
132 51 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)ljoin, fd_txncache_align() ) ) ) {
133 0 : FD_LOG_WARNING(( "misaligned ljoin" ));
134 0 : return NULL;
135 0 : }
136 :
137 51 : fd_txncache_t * tc = (fd_txncache_t *)ljoin;
138 :
139 51 : return tc;
140 51 : }
141 :
142 : void
143 3588 : fd_txncache_reset( fd_txncache_t * tc ) {
144 3588 : fd_rwlock_write( tc->shmem->lock );
145 :
146 3588 : tc->shmem->root_cnt = 0UL;
147 3588 : root_slist_remove_all( tc->shmem->root_ll, tc->blockcache_shmem_pool );
148 :
149 3588 : tc->shmem->txnpages_free_cnt = tc->shmem->max_txnpages;
150 1198392 : for( ushort i=0; i<tc->shmem->max_txnpages; i++ ) tc->txnpages_free[ i ] = i;
151 :
152 3588 : blockcache_pool_reset( tc->blockcache_shmem_pool );
153 3588 : blockhash_map_reset( tc->blockhash_map );
154 :
155 3588 : fd_rwlock_unwrite( tc->shmem->lock );
156 3588 : }
157 :
158 : FD_FN_PURE static inline ulong
159 : fd_txncache_bucket( fd_txncache_t const * tc,
160 267 : uchar const * txnhash ) {
161 267 : return fd_ulong_hash( FD_LOAD( ulong, txnhash )^tc->shmem->seed )%tc->shmem->txn_per_slot_max;
162 267 : }
163 :
164 : static fd_txncache_txnpage_t *
165 : fd_txncache_ensure_txnpage( fd_txncache_t * tc,
166 63 : blockcache_t * blockcache ) {
167 63 : ushort page_cnt = blockcache->shmem->pages_cnt;
168 63 : if( FD_UNLIKELY( page_cnt>tc->shmem->txnpages_per_blockhash_max ) ) return NULL;
169 :
170 63 : if( FD_LIKELY( page_cnt ) ) {
171 33 : ushort txnpage_idx = blockcache->pages[ page_cnt-1 ];
172 33 : ushort txnpage_free = tc->txnpages[ txnpage_idx ].free;
173 33 : if( FD_LIKELY( txnpage_free ) ) return &tc->txnpages[ txnpage_idx ];
174 33 : }
175 :
176 30 : if( FD_UNLIKELY( page_cnt==tc->shmem->txnpages_per_blockhash_max ) ) return NULL;
177 30 : if( FD_LIKELY( FD_ATOMIC_CAS( &blockcache->pages[ page_cnt ], (ushort)USHORT_MAX, (ushort)(USHORT_MAX-1UL) )==(ushort)USHORT_MAX ) ) {
178 30 : ulong txnpages_free_cnt = tc->shmem->txnpages_free_cnt;
179 30 : for(;;) {
180 30 : if( FD_UNLIKELY( !txnpages_free_cnt ) ) return NULL;
181 30 : ulong old_txnpages_free_cnt = FD_ATOMIC_CAS( &tc->shmem->txnpages_free_cnt, (ushort)txnpages_free_cnt, (ushort)(txnpages_free_cnt-1UL) );
182 30 : if( FD_LIKELY( old_txnpages_free_cnt==txnpages_free_cnt ) ) break;
183 0 : txnpages_free_cnt = old_txnpages_free_cnt;
184 0 : FD_SPIN_PAUSE();
185 0 : }
186 :
187 30 : ushort txnpage_idx = tc->txnpages_free[ txnpages_free_cnt-1UL ];
188 30 : fd_txncache_txnpage_t * txnpage = &tc->txnpages[ txnpage_idx ];
189 30 : txnpage->free = FD_TXNCACHE_TXNS_PER_PAGE;
190 30 : FD_COMPILER_MFENCE();
191 30 : blockcache->pages[ page_cnt ] = txnpage_idx;
192 30 : FD_COMPILER_MFENCE();
193 30 : blockcache->shmem->pages_cnt = (ushort)(page_cnt+1);
194 30 : return txnpage;
195 30 : } else {
196 0 : ushort txnpage_idx = blockcache->pages[ page_cnt ];
197 0 : while( FD_UNLIKELY( txnpage_idx>=USHORT_MAX-1UL ) ) {
198 0 : txnpage_idx = blockcache->pages[ page_cnt ];
199 0 : FD_SPIN_PAUSE();
200 0 : }
201 0 : return &tc->txnpages[ txnpage_idx ];
202 0 : }
203 30 : }
204 :
205 : static int
206 : fd_txncache_insert_txn( fd_txncache_t * tc,
207 : blockcache_t * blockcache,
208 : fd_txncache_txnpage_t * txnpage,
209 : fd_txncache_fork_id_t fork_id,
210 63 : uchar const * txnhash ) {
211 63 : ulong txnpage_idx = (ulong)(txnpage - tc->txnpages);
212 :
213 63 : for(;;) {
214 63 : ushort txnpage_free = txnpage->free;
215 63 : if( FD_UNLIKELY( !txnpage_free ) ) return 0;
216 63 : if( FD_UNLIKELY( FD_ATOMIC_CAS( &txnpage->free, txnpage_free, txnpage_free-1UL )!=txnpage_free ) ) {
217 0 : FD_SPIN_PAUSE();
218 0 : continue;
219 0 : }
220 :
221 63 : ulong txn_idx = FD_TXNCACHE_TXNS_PER_PAGE-txnpage_free;
222 63 : ulong txnhash_offset = blockcache->shmem->txnhash_offset;
223 63 : memcpy( txnpage->txns[ txn_idx ]->txnhash, txnhash+txnhash_offset, 20UL );
224 63 : txnpage->txns[ txn_idx ]->fork_id = fork_id;
225 63 : txnpage->txns[ txn_idx ]->generation = tc->blockcache_pool[ fork_id.val ].shmem->generation;
226 63 : FD_COMPILER_MFENCE();
227 :
228 63 : ulong txn_bucket = fd_txncache_bucket( tc, txnhash+txnhash_offset );
229 63 : for(;;) {
230 63 : uint head = blockcache->heads[ txn_bucket ];
231 63 : txnpage->txns[ txn_idx ]->blockcache_next = head;
232 63 : FD_COMPILER_MFENCE();
233 63 : if( FD_LIKELY( FD_ATOMIC_CAS( &blockcache->heads[ txn_bucket ], head, (uint)(FD_TXNCACHE_TXNS_PER_PAGE*txnpage_idx+txn_idx) )==head ) ) break;
234 0 : FD_SPIN_PAUSE();
235 0 : }
236 :
237 63 : return 1;
238 63 : }
239 63 : }
240 :
241 : fd_txncache_fork_id_t
242 : fd_txncache_attach_child( fd_txncache_t * tc,
243 7248 : fd_txncache_fork_id_t parent_fork_id ) {
244 7248 : fd_rwlock_write( tc->shmem->lock );
245 :
246 7248 : FD_TEST( blockcache_pool_free( tc->blockcache_shmem_pool ) );
247 7248 : ulong idx = blockcache_pool_idx_acquire( tc->blockcache_shmem_pool );
248 :
249 7248 : blockcache_t * fork = &tc->blockcache_pool[ idx ];
250 7248 : fd_txncache_fork_id_t fork_id = { .val = (ushort)idx };
251 :
252 7248 : fork->shmem->generation = tc->shmem->blockcache_generation++;
253 7248 : fork->shmem->child_id = (fd_txncache_fork_id_t){ .val = USHORT_MAX };
254 :
255 7248 : if( FD_LIKELY( parent_fork_id.val==USHORT_MAX ) ) {
256 3588 : FD_TEST( blockcache_pool_free( tc->blockcache_shmem_pool )==blockcache_pool_max( tc->blockcache_shmem_pool )-1UL );
257 3588 : fork->shmem->parent_id = (fd_txncache_fork_id_t){ .val = USHORT_MAX };
258 3588 : fork->shmem->sibling_id = (fd_txncache_fork_id_t){ .val = USHORT_MAX };
259 :
260 3588 : descends_set_null( fork->descends );
261 3588 : root_slist_ele_push_tail( tc->shmem->root_ll, fork->shmem, tc->blockcache_shmem_pool );
262 3660 : } else {
263 3660 : blockcache_t * parent = &tc->blockcache_pool[ parent_fork_id.val ];
264 : /* We might be tempted to freeze the parent here, and it's valid to
265 : do this ordinarily, but not when loading from a snapshot, when
266 : we need to load many transactions into a root parent chain at
267 : once. */
268 3660 : fork->shmem->sibling_id = parent->shmem->child_id;
269 3660 : fork->shmem->parent_id = parent_fork_id;
270 3660 : parent->shmem->child_id = fork_id;
271 :
272 3660 : descends_set_copy( fork->descends, parent->descends );
273 3660 : descends_set_insert( fork->descends, parent_fork_id.val );
274 3660 : }
275 :
276 7248 : fork->shmem->txnhash_offset = 0UL;
277 7248 : fork->shmem->frozen = 0;
278 7248 : memset( fork->heads, 0xFF, tc->shmem->txn_per_slot_max*sizeof(uint) );
279 7248 : fork->shmem->pages_cnt = 0;
280 7248 : memset( fork->pages, 0xFF, tc->shmem->txnpages_per_blockhash_max*sizeof(fork->pages[ 0 ]) );
281 :
282 7248 : fd_rwlock_unwrite( tc->shmem->lock );
283 7248 : return fork_id;
284 7248 : }
285 :
286 : void
287 : fd_txncache_attach_blockhash( fd_txncache_t * tc,
288 : fd_txncache_fork_id_t fork_id,
289 0 : uchar const * blockhash ) {
290 0 : fd_rwlock_write( tc->shmem->lock );
291 :
292 0 : blockcache_t * fork = &tc->blockcache_pool[ fork_id.val ];
293 0 : FD_TEST( !fork->shmem->frozen );
294 0 : fork->shmem->frozen = 1;
295 :
296 0 : memcpy( fork->shmem->blockhash.uc, blockhash, 32UL );
297 :
298 0 : blockhash_map_ele_insert( tc->blockhash_map, fork->shmem, tc->blockcache_shmem_pool );
299 :
300 0 : fd_rwlock_unwrite( tc->shmem->lock );
301 0 : }
302 :
303 : void
304 : fd_txncache_finalize_fork( fd_txncache_t * tc,
305 : fd_txncache_fork_id_t fork_id,
306 : ulong txnhash_offset,
307 3681 : uchar const * blockhash ) {
308 3681 : fd_rwlock_write( tc->shmem->lock );
309 :
310 3681 : blockcache_t * fork = &tc->blockcache_pool[ fork_id.val ];
311 3681 : FD_TEST( fork->shmem->frozen<=1 );
312 3681 : FD_TEST( fork->shmem->frozen>=0 );
313 3681 : fork->shmem->txnhash_offset = txnhash_offset;
314 :
315 3681 : memcpy( fork->shmem->blockhash.uc, blockhash, 32UL );
316 :
317 3681 : if( FD_LIKELY( !fork->shmem->frozen ) ) blockhash_map_ele_insert( tc->blockhash_map, fork->shmem, tc->blockcache_shmem_pool );
318 3681 : fork->shmem->frozen = 2;
319 :
320 3681 : fd_rwlock_unwrite( tc->shmem->lock );
321 3681 : }
322 :
323 : static inline void
324 : remove_blockcache( fd_txncache_t * tc,
325 0 : blockcache_t * blockcache ) {
326 0 : FD_TEST( blockcache->shmem->frozen>=0 );
327 0 : memcpy( tc->txnpages_free+tc->shmem->txnpages_free_cnt, blockcache->pages, blockcache->shmem->pages_cnt*sizeof(tc->txnpages_free[ 0 ]) );
328 0 : tc->shmem->txnpages_free_cnt = (ushort)(tc->shmem->txnpages_free_cnt+blockcache->shmem->pages_cnt);
329 :
330 0 : ulong idx = blockcache_pool_idx( tc->blockcache_shmem_pool, blockcache->shmem );
331 0 : for( ulong i=0UL; i<tc->shmem->active_slots_max; i++ ) descends_set_remove( tc->blockcache_pool[ i ].descends, idx );
332 :
333 0 : if( FD_LIKELY( blockcache->shmem->frozen ) ) blockhash_map_ele_remove_fast( tc->blockhash_map, blockcache->shmem, tc->blockcache_shmem_pool );
334 0 : blockcache->shmem->frozen = -1;
335 0 : blockcache_pool_ele_release( tc->blockcache_shmem_pool, blockcache->shmem );
336 0 : }
337 :
338 : static inline void
339 : remove_children( fd_txncache_t * tc,
340 : blockcache_t const * fork,
341 24 : blockcache_t const * except ) {
342 24 : fd_txncache_fork_id_t sibling_idx = fork->shmem->child_id;
343 48 : while( sibling_idx.val!=USHORT_MAX ) {
344 24 : blockcache_t * sibling = &tc->blockcache_pool[ sibling_idx.val ];
345 :
346 24 : sibling_idx = sibling->shmem->sibling_id;
347 24 : if( FD_UNLIKELY( sibling==except ) ) continue;
348 :
349 0 : remove_children( tc, sibling, except );
350 0 : remove_blockcache( tc, sibling );
351 0 : }
352 24 : }
353 :
354 : void
355 : fd_txncache_cancel_fork( fd_txncache_t * tc,
356 0 : fd_txncache_fork_id_t fork_id ) {
357 0 : fd_rwlock_write( tc->shmem->lock );
358 0 : blockcache_t * fork = &tc->blockcache_pool[ fork_id.val ];
359 0 : FD_TEST( fork->shmem->parent_id.val!=USHORT_MAX );
360 :
361 : /* The soon-to-be-pruned subtree must be unrooted. */
362 0 : fd_txncache_blockcache_shmem_t const * latest_root = root_slist_ele_peek_tail_const( tc->shmem->root_ll, tc->blockcache_shmem_pool );
363 0 : FD_TEST( latest_root );
364 0 : FD_TEST( descends_set_test( fork->descends, blockcache_pool_idx( tc->blockcache_shmem_pool, latest_root ) ) );
365 :
366 0 : remove_children( tc, fork, NULL );
367 0 : remove_blockcache( tc, fork );
368 0 : ushort * fork_id_p = &(tc->blockcache_pool[ fork->shmem->parent_id.val ].shmem->child_id.val);
369 0 : while( *fork_id_p!=fork_id.val ) {
370 0 : fork_id_p = &(tc->blockcache_pool[ *fork_id_p ].shmem->sibling_id.val);
371 0 : }
372 0 : *fork_id_p = fork->shmem->sibling_id.val;
373 0 : fd_rwlock_unwrite( tc->shmem->lock );
374 0 : }
375 :
376 : void
377 : fd_txncache_advance_root( fd_txncache_t * tc,
378 24 : fd_txncache_fork_id_t fork_id ) {
379 24 : fd_rwlock_write( tc->shmem->lock );
380 :
381 24 : blockcache_t * fork = &tc->blockcache_pool[ fork_id.val ];
382 :
383 24 : blockcache_t * parent_fork = &tc->blockcache_pool[ fork->shmem->parent_id.val ];
384 24 : if( FD_UNLIKELY( root_slist_ele_peek_tail( tc->shmem->root_ll, tc->blockcache_shmem_pool )!=parent_fork->shmem ) ) {
385 0 : FD_BASE58_ENCODE_32_BYTES( parent_fork->shmem->blockhash.uc, parent_blockhash_b58 );
386 0 : FD_BASE58_ENCODE_32_BYTES( fork->shmem->blockhash.uc, fork_blockhash_b58 );
387 0 : FD_BASE58_ENCODE_32_BYTES( root_slist_ele_peek_tail( tc->shmem->root_ll, tc->blockcache_shmem_pool )->blockhash.uc, root_blockhash_b58 );
388 0 : FD_LOG_CRIT(( "advancing root from %s to %s but that is not valid, last root is %s",
389 0 : parent_blockhash_b58,
390 0 : fork_blockhash_b58,
391 0 : root_blockhash_b58 ));
392 0 : }
393 :
394 24 : FD_BASE58_ENCODE_32_BYTES( parent_fork->shmem->blockhash.uc, parent_blockhash_b58 );
395 24 : FD_BASE58_ENCODE_32_BYTES( fork->shmem->blockhash.uc, fork_blockhash_b58 );
396 24 : FD_LOG_DEBUG(( "advancing root from %s to %s",
397 24 : parent_blockhash_b58,
398 24 : fork_blockhash_b58 ));
399 :
400 : /* When a fork is rooted, any competing forks can be immediately
401 : removed as they will not be needed again. This includes child
402 : forks of the pruned siblings as well. */
403 24 : remove_children( tc, parent_fork, fork );
404 24 : parent_fork->shmem->child_id = fork_id;
405 :
406 : /* Now, the earliest known rooted fork can likely be removed since its
407 : blockhashes cannot be referenced anymore (they are older than 151
408 : blockhashes away). */
409 24 : tc->shmem->root_cnt++;
410 24 : root_slist_ele_push_tail( tc->shmem->root_ll, fork->shmem, tc->blockcache_shmem_pool );
411 24 : if( FD_LIKELY( tc->shmem->root_cnt>FD_TXNCACHE_MAX_BLOCKHASH_DISTANCE ) ) {
412 0 : fd_txncache_blockcache_shmem_t * old_root_shmem = root_slist_ele_pop_head( tc->shmem->root_ll, tc->blockcache_shmem_pool );
413 0 : FD_TEST( old_root_shmem );
414 0 : blockcache_t * old_root = &tc->blockcache_pool[ blockcache_pool_idx( tc->blockcache_shmem_pool, old_root_shmem ) ];
415 :
416 0 : root_slist_ele_peek_head( tc->shmem->root_ll, tc->blockcache_shmem_pool )->parent_id.val = USHORT_MAX;
417 :
418 0 : remove_blockcache( tc, old_root );
419 0 : tc->shmem->root_cnt--;
420 0 : }
421 :
422 24 : fd_rwlock_unwrite( tc->shmem->lock );
423 24 : }
424 :
425 : static inline blockcache_t *
426 : blockhash_on_fork( fd_txncache_t * tc,
427 : blockcache_t const * fork,
428 267 : uchar const * blockhash ) {
429 267 : fd_txncache_blockcache_shmem_t const * candidate = blockhash_map_ele_query_const( tc->blockhash_map, fd_type_pun_const( blockhash ), NULL, tc->blockcache_shmem_pool );
430 267 : if( FD_UNLIKELY( !candidate ) ) return NULL;
431 :
432 267 : while( candidate ) {
433 267 : ulong candidate_idx = blockcache_pool_idx( tc->blockcache_shmem_pool, candidate );
434 267 : if( FD_LIKELY( descends_set_test( fork->descends, candidate_idx ) ) ) return &tc->blockcache_pool[ candidate_idx ];
435 0 : candidate = blockhash_map_ele_next_const( candidate, NULL, tc->blockcache_shmem_pool );
436 0 : }
437 0 : return NULL;
438 267 : }
439 :
440 : static void
441 : purge_stale_on_blockcache( fd_txncache_t * tc,
442 0 : blockcache_t * blockcache ) {
443 0 : FD_TEST( blockcache->shmem->frozen>=0 );
444 0 : memset( tc->scratch_heads, 0xFF, tc->shmem->txn_per_slot_max*sizeof(tc->scratch_heads[ 0 ]) );
445 0 : memset( tc->scratch_pages, 0xFF, tc->shmem->txnpages_per_blockhash_max*sizeof(tc->scratch_pages[ 0 ]) );
446 0 : ushort scratch_pages_cnt = 0;
447 0 : ushort scratch_txnpage_idx = USHORT_MAX;
448 0 : tc->scratch_txnpage->free = 0;
449 0 : for( ulong i=0UL; i<blockcache->shmem->pages_cnt; i++ ) {
450 0 : ushort curr_txnpage_idx = blockcache->pages[ blockcache->shmem->pages_cnt-i-1UL ];
451 0 : ulong curr_txn_cnt = FD_TXNCACHE_TXNS_PER_PAGE-tc->txnpages[ curr_txnpage_idx ].free;
452 0 : for( ulong j=0UL; j<curr_txn_cnt; j++ ) {
453 0 : fd_txncache_single_txn_t * curr_txn = tc->txnpages[ curr_txnpage_idx ].txns[ curr_txn_cnt-j-1UL ];
454 0 : blockcache_t const * txn_fork = &tc->blockcache_pool[ curr_txn->fork_id.val ];
455 0 : if( FD_LIKELY( txn_fork->shmem->frozen>=0 && txn_fork->shmem->generation==curr_txn->generation ) ) {
456 : /* Valid transaction. Keep. */
457 0 : if( FD_UNLIKELY( !tc->scratch_txnpage->free ) ) {
458 0 : FD_TEST( scratch_txnpage_idx!=curr_txnpage_idx );
459 0 : if( FD_LIKELY( scratch_txnpage_idx!=USHORT_MAX ) ) {
460 0 : fd_txncache_txnpage_t * txnpage = &tc->txnpages[ scratch_txnpage_idx ];
461 0 : memcpy( txnpage, tc->scratch_txnpage, sizeof(*txnpage) );
462 0 : }
463 0 : scratch_txnpage_idx = curr_txnpage_idx;
464 0 : tc->scratch_txnpage->free = FD_TXNCACHE_TXNS_PER_PAGE;
465 0 : tc->scratch_pages[ scratch_pages_cnt ] = scratch_txnpage_idx;
466 0 : scratch_pages_cnt++;
467 0 : }
468 0 : ulong txn_idx = FD_TXNCACHE_TXNS_PER_PAGE-tc->scratch_txnpage->free;
469 0 : memcpy( tc->scratch_txnpage->txns[ txn_idx ], curr_txn, sizeof(*curr_txn) );
470 0 : ulong txn_bucket = fd_txncache_bucket( tc, curr_txn->txnhash );
471 0 : uint head = tc->scratch_heads[ txn_bucket ];
472 0 : tc->scratch_txnpage->txns[ txn_idx ]->blockcache_next = head;
473 0 : ulong txn_gidx = FD_TXNCACHE_TXNS_PER_PAGE*scratch_txnpage_idx+txn_idx;
474 0 : FD_TEST( txn_gidx<UINT_MAX );
475 0 : tc->scratch_heads[ txn_bucket ] = (uint)txn_gidx;
476 0 : tc->scratch_txnpage->free--;
477 0 : } else {
478 : /* Stale transaction. Drop. */
479 0 : continue;
480 0 : }
481 0 : }
482 0 : if( FD_UNLIKELY( curr_txnpage_idx!=scratch_txnpage_idx ) ) {
483 : /* The txnpage is not being used for compaction, free it up. */
484 0 : tc->txnpages_free[ tc->shmem->txnpages_free_cnt ] = curr_txnpage_idx;
485 0 : tc->shmem->txnpages_free_cnt++;
486 0 : }
487 0 : }
488 0 : if( FD_LIKELY( scratch_txnpage_idx!=USHORT_MAX ) ) {
489 0 : fd_txncache_txnpage_t * txnpage = &tc->txnpages[ scratch_txnpage_idx ];
490 0 : memcpy( txnpage, tc->scratch_txnpage, sizeof(*txnpage) );
491 0 : }
492 0 : blockcache->shmem->pages_cnt = scratch_pages_cnt;
493 0 : memcpy( blockcache->pages, tc->scratch_pages, tc->shmem->txnpages_per_blockhash_max*sizeof(blockcache->pages[0]) );
494 0 : memcpy( blockcache->heads, tc->scratch_heads, tc->shmem->txn_per_slot_max*sizeof(blockcache->heads[0]) );
495 0 : }
496 :
497 : static void
498 : purge_stale_on_fork( fd_txncache_t * tc,
499 0 : blockcache_t * fork ) {
500 0 : purge_stale_on_blockcache( tc, fork );
501 :
502 0 : fd_txncache_fork_id_t sibling_idx = fork->shmem->child_id;
503 0 : while( sibling_idx.val!=USHORT_MAX ) {
504 0 : blockcache_t * sibling = &tc->blockcache_pool[ sibling_idx.val ];
505 0 : purge_stale_on_fork( tc, sibling );
506 0 : sibling_idx = sibling->shmem->sibling_id;
507 0 : }
508 0 : }
509 :
510 : static void
511 0 : purge_stale( fd_txncache_t * tc ) {
512 0 : fd_txncache_blockcache_shmem_t * root_shmem = root_slist_ele_peek_head( tc->shmem->root_ll, tc->blockcache_shmem_pool );
513 0 : FD_TEST( root_shmem );
514 0 : blockcache_t * root = &tc->blockcache_pool[ blockcache_pool_idx( tc->blockcache_shmem_pool, root_shmem ) ];
515 0 : ushort free_before = tc->shmem->txnpages_free_cnt;
516 : /* One might think that an optimization here is to stop the descent on
517 : the latest rooted blockcache. There could be no pruned minority
518 : forks from that point on. As a result, there could be no stale
519 : transactions in any blockcache descending from that.
520 : Unfortunately, frontier eviction means that any blockcache in the
521 : fork tree can have stale transactions. */
522 0 : purge_stale_on_fork( tc, root );
523 0 : FD_LOG_NOTICE(( "purge_stale: txnpages_free %hu -> %hu", free_before, tc->shmem->txnpages_free_cnt ));
524 0 : }
525 :
526 : void
527 : fd_txncache_insert( fd_txncache_t * tc,
528 : fd_txncache_fork_id_t fork_id,
529 : uchar const * blockhash,
530 63 : uchar const * txnhash ) {
531 63 : fd_rwlock_read( tc->shmem->lock );
532 :
533 63 : blockcache_t const * fork = &tc->blockcache_pool[ fork_id.val ];
534 63 : FD_TEST( fork->shmem->frozen<=1 );
535 63 : FD_TEST( fork->shmem->frozen>=0 );
536 63 : blockcache_t * blockcache = blockhash_on_fork( tc, fork, blockhash );
537 63 : FD_TEST( blockcache );
538 :
539 63 : for(;;) {
540 63 : fd_txncache_txnpage_t * txnpage = fd_txncache_ensure_txnpage( tc, blockcache );
541 63 : if( FD_UNLIKELY( !txnpage ) ) {
542 : /* Because of sizing invariants when creating the structure, it is
543 : not typically possible to fill it, unless there are stale
544 : transactions from minority forks that were purged floating
545 : around, in which case we can purge them here and try again. */
546 0 : fd_rwlock_unread( tc->shmem->lock );
547 0 : fd_rwlock_write( tc->shmem->lock );
548 0 : if( FD_LIKELY( !fd_txncache_ensure_txnpage( tc, blockcache ) ) ) purge_stale( tc );
549 0 : fd_rwlock_unwrite( tc->shmem->lock );
550 0 : fd_rwlock_read( tc->shmem->lock );
551 0 : continue;
552 0 : }
553 :
554 63 : int success = fd_txncache_insert_txn( tc, blockcache, txnpage, fork_id, txnhash );
555 63 : if( FD_LIKELY( success ) ) break;
556 :
557 0 : FD_SPIN_PAUSE();
558 0 : }
559 :
560 63 : fd_rwlock_unread( tc->shmem->lock );
561 63 : }
562 :
563 : int
564 : fd_txncache_query( fd_txncache_t * tc,
565 : fd_txncache_fork_id_t fork_id,
566 : uchar const * blockhash,
567 204 : uchar const * txnhash ) {
568 204 : fd_rwlock_read( tc->shmem->lock );
569 :
570 204 : blockcache_t const * fork = &tc->blockcache_pool[ fork_id.val ];
571 204 : FD_TEST( fork->shmem->frozen>=0 );
572 204 : blockcache_t const * blockcache = blockhash_on_fork( tc, fork, blockhash );
573 204 : FD_TEST( blockcache );
574 204 : FD_TEST( blockcache->shmem->frozen==2 );
575 :
576 204 : int found = 0;
577 :
578 204 : ulong txnhash_offset = blockcache->shmem->txnhash_offset;
579 204 : ulong head_hash = fd_txncache_bucket( tc, txnhash+txnhash_offset );
580 207 : for( uint head=blockcache->heads[ head_hash ]; head!=UINT_MAX; head=tc->txnpages[ head/FD_TXNCACHE_TXNS_PER_PAGE ].txns[ head%FD_TXNCACHE_TXNS_PER_PAGE ]->blockcache_next ) {
581 3 : fd_txncache_single_txn_t * txn = tc->txnpages[ head/FD_TXNCACHE_TXNS_PER_PAGE ].txns[ head%FD_TXNCACHE_TXNS_PER_PAGE ];
582 :
583 3 : blockcache_t const * txn_fork = &tc->blockcache_pool[ txn->fork_id.val ];
584 3 : int descends = (txn->fork_id.val==fork_id.val || descends_set_test( fork->descends, txn->fork_id.val )) && txn_fork->shmem->frozen>=0 && txn_fork->shmem->generation==txn->generation;
585 3 : if( FD_LIKELY( descends && !memcmp( txnhash+txnhash_offset, txn->txnhash, 20UL ) ) ) {
586 0 : found = 1;
587 0 : break;
588 0 : }
589 3 : }
590 :
591 204 : fd_rwlock_unread( tc->shmem->lock );
592 204 : return found;
593 204 : }
|