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