LCOV - code coverage report
Current view: top level - discof/forest - fd_forest.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 860 0.0 %
Date: 2025-09-20 04:42:25 Functions: 0 38 0.0 %

          Line data    Source code
       1             : #include "fd_forest.h"
       2             : 
       3           0 : static void ver_inc( ulong ** ver ) {
       4           0 :   fd_fseq_update( *ver, fd_fseq_query( *ver ) + 1 );
       5           0 : }
       6             : 
       7           0 : #define VER_INC ulong * ver __attribute__((cleanup(ver_inc))) = fd_forest_ver( forest ); ver_inc( &ver )
       8             : 
       9             : void *
      10           0 : fd_forest_new( void * shmem, ulong ele_max, ulong seed ) {
      11           0 :   FD_TEST( fd_ulong_is_pow2( ele_max ) );
      12             : 
      13           0 :   if( FD_UNLIKELY( !shmem ) ) {
      14           0 :     FD_LOG_WARNING(( "NULL mem" ));
      15           0 :     return NULL;
      16           0 :   }
      17             : 
      18           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_forest_align() ) ) ) {
      19           0 :     FD_LOG_WARNING(( "misaligned mem" ));
      20           0 :     return NULL;
      21           0 :   }
      22             : 
      23           0 :   ulong footprint = fd_forest_footprint( ele_max );
      24           0 :   if( FD_UNLIKELY( !footprint ) ) {
      25           0 :     FD_LOG_WARNING(( "bad ele_max (%lu)", ele_max ));
      26           0 :     return NULL;
      27           0 :   }
      28             : 
      29           0 :   fd_wksp_t * wksp = fd_wksp_containing( shmem );
      30           0 :   if( FD_UNLIKELY( !wksp ) ) {
      31           0 :     FD_LOG_WARNING(( "shmem must be part of a workspace" ));
      32           0 :     return NULL;
      33           0 :   }
      34             : 
      35           0 :   fd_memset( shmem, 0, footprint );
      36           0 :   fd_forest_t * forest;
      37             : 
      38           0 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
      39           0 :   forest          = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_align(),          sizeof(fd_forest_t)                     );
      40           0 :   void * ver      = FD_SCRATCH_ALLOC_APPEND( l, fd_fseq_align(),            fd_fseq_footprint()                     );
      41           0 :   void * pool     = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_pool_align(),     fd_forest_pool_footprint    ( ele_max ) );
      42           0 :   void * ancestry = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_ancestry_align(), fd_forest_ancestry_footprint( ele_max ) );
      43           0 :   void * frontier = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_frontier_align(), fd_forest_frontier_footprint( ele_max ) );
      44           0 :   void * subtrees = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_subtrees_align(), fd_forest_subtrees_footprint( ele_max ) );
      45           0 :   void * orphaned = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_orphaned_align(), fd_forest_orphaned_footprint( ele_max ) );
      46           0 :   void * consumed = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_consumed_align(), fd_forest_consumed_footprint( ele_max ) );
      47           0 :   void * conspool = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_conspool_align(), fd_forest_conspool_footprint( ele_max ) );
      48           0 :   void * deque    = FD_SCRATCH_ALLOC_APPEND( l, fd_forest_deque_align(),    fd_forest_deque_footprint   ( ele_max ) );
      49           0 :   FD_TEST( FD_SCRATCH_ALLOC_FINI( l, fd_forest_align() ) == (ulong)shmem + footprint );
      50             : 
      51           0 :   forest->root           = ULONG_MAX;
      52           0 :   forest->wksp_gaddr     = fd_wksp_gaddr_fast( wksp, forest );
      53           0 :   forest->ver_gaddr      = fd_wksp_gaddr_fast( wksp, fd_fseq_join           ( fd_fseq_new           ( ver,      FD_FOREST_VER_UNINIT ) ) );
      54           0 :   forest->pool_gaddr     = fd_wksp_gaddr_fast( wksp, fd_forest_pool_join    ( fd_forest_pool_new    ( pool,     ele_max              ) ) );
      55           0 :   forest->ancestry_gaddr = fd_wksp_gaddr_fast( wksp, fd_forest_ancestry_join( fd_forest_ancestry_new( ancestry, ele_max, seed        ) ) );
      56           0 :   forest->frontier_gaddr = fd_wksp_gaddr_fast( wksp, fd_forest_frontier_join( fd_forest_frontier_new( frontier, ele_max, seed        ) ) );
      57           0 :   forest->subtrees_gaddr = fd_wksp_gaddr_fast( wksp, fd_forest_subtrees_join( fd_forest_subtrees_new( subtrees, ele_max, seed        ) ) );
      58           0 :   forest->orphaned_gaddr = fd_wksp_gaddr_fast( wksp, fd_forest_orphaned_join( fd_forest_orphaned_new( orphaned, ele_max, seed        ) ) );
      59           0 :   forest->consumed_gaddr = fd_wksp_gaddr_fast( wksp, fd_forest_consumed_join( fd_forest_consumed_new( consumed, ele_max, seed        ) ) );
      60           0 :   forest->conspool_gaddr = fd_wksp_gaddr_fast( wksp, fd_forest_conspool_join( fd_forest_conspool_new( conspool, ele_max              ) ) );
      61           0 :   forest->deque_gaddr    = fd_wksp_gaddr_fast( wksp, fd_forest_deque_join   ( fd_forest_deque_new   ( deque,    ele_max              ) ) );
      62             : 
      63           0 :   FD_COMPILER_MFENCE();
      64           0 :   FD_VOLATILE( forest->magic ) = FD_FOREST_MAGIC;
      65           0 :   FD_COMPILER_MFENCE();
      66             : 
      67           0 :   return shmem;
      68           0 : }
      69             : 
      70             : fd_forest_t *
      71           0 : fd_forest_join( void * shforest ) {
      72           0 :   fd_forest_t * forest = (fd_forest_t *)shforest;
      73             : 
      74           0 :   if( FD_UNLIKELY( !forest ) ) {
      75           0 :     FD_LOG_WARNING(( "NULL forest" ));
      76           0 :     return NULL;
      77           0 :   }
      78             : 
      79           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned((ulong)forest, fd_forest_align() ) ) ) {
      80           0 :     FD_LOG_WARNING(( "misaligned forest" ));
      81           0 :     return NULL;
      82           0 :   }
      83             : 
      84           0 :   fd_wksp_t * wksp = fd_wksp_containing( forest );
      85           0 :   if( FD_UNLIKELY( !wksp ) ) {
      86           0 :     FD_LOG_WARNING(( "forest must be part of a workspace" ));
      87           0 :     return NULL;
      88           0 :   }
      89             : 
      90           0 :   return forest;
      91           0 : }
      92             : 
      93             : void *
      94           0 : fd_forest_leave( fd_forest_t const * forest ) {
      95             : 
      96           0 :   if( FD_UNLIKELY( !forest ) ) {
      97           0 :     FD_LOG_WARNING(( "NULL forest" ));
      98           0 :     return NULL;
      99           0 :   }
     100             : 
     101           0 :   return (void *)forest;
     102           0 : }
     103             : 
     104             : void *
     105           0 : fd_forest_delete( void * forest ) {
     106             : 
     107           0 :   if( FD_UNLIKELY( !forest ) ) {
     108           0 :     FD_LOG_WARNING(( "NULL forest" ));
     109           0 :     return NULL;
     110           0 :   }
     111             : 
     112           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned((ulong)forest, fd_forest_align() ) ) ) {
     113           0 :     FD_LOG_WARNING(( "misaligned forest" ));
     114           0 :     return NULL;
     115           0 :   }
     116             : 
     117             :   // TODO: zero out mem?
     118             : 
     119           0 :   return forest;
     120           0 : }
     121             : 
     122             : 
     123             : static void
     124           0 : consumed_map_insert( fd_forest_t * forest, ulong slot, ulong pool_idx ) {
     125           0 :   fd_forest_consumed_t * consumed = fd_forest_consumed( forest );
     126           0 :   fd_forest_cns_t      * pool     = fd_forest_conspool( forest );
     127           0 :   fd_forest_cns_t      * ele      = fd_forest_conspool_ele_acquire( pool );
     128           0 :   ele->slot            = slot;
     129           0 :   ele->forest_pool_idx = pool_idx;
     130           0 :   fd_forest_consumed_ele_insert( consumed, ele, pool );
     131           0 : }
     132             : 
     133             : fd_forest_t *
     134           0 : fd_forest_init( fd_forest_t * forest, ulong root_slot ) {
     135           0 :   FD_TEST( forest );
     136           0 :   FD_TEST( fd_fseq_query( fd_forest_ver( forest ) ) == FD_FOREST_VER_UNINIT );
     137             : 
     138           0 :   VER_INC;
     139             : 
     140           0 :   fd_forest_blk_t *      pool     = fd_forest_pool( forest );
     141           0 :   ulong                  null     = fd_forest_pool_idx_null( pool );
     142           0 :   fd_forest_frontier_t * frontier = fd_forest_frontier( forest );
     143             : 
     144             :   /* Initialize the root node from a pool element. */
     145             : 
     146           0 :   fd_forest_blk_t * root_ele = fd_forest_pool_ele_acquire( pool );
     147           0 :   root_ele->slot             = root_slot;
     148           0 :   root_ele->parent           = null;
     149           0 :   root_ele->child            = null;
     150           0 :   root_ele->sibling          = null;
     151           0 :   root_ele->buffered_idx     = 0;
     152           0 :   root_ele->complete_idx     = 0;
     153             : 
     154           0 :   fd_forest_blk_idxs_full( root_ele->fecs );
     155           0 :   fd_forest_blk_idxs_full( root_ele->cmpl );
     156             : 
     157           0 :   forest->root = fd_forest_pool_idx( pool, root_ele );
     158           0 :   fd_forest_frontier_ele_insert( frontier, root_ele, pool ); /* cannot fail */
     159           0 :   consumed_map_insert( forest, root_ele->slot, fd_forest_pool_idx( pool, root_ele ) );
     160             : 
     161             :   /* Sanity checks. */
     162             : 
     163           0 :   FD_TEST( root_ele );
     164           0 :   FD_TEST( root_ele == fd_forest_frontier_ele_query( frontier, &root_slot, NULL, pool ));
     165           0 :   FD_TEST( root_ele->slot == root_slot );
     166             : 
     167           0 :   return forest;
     168           0 : }
     169             : 
     170             : static ulong *
     171           0 : fd_forest_deque( fd_forest_t * forest ) {
     172           0 :   return fd_wksp_laddr_fast( fd_forest_wksp( forest ), forest->deque_gaddr );
     173           0 : }
     174             : 
     175             : fd_forest_t *
     176           0 : fd_forest_fini( fd_forest_t * forest ) {
     177           0 :   fd_fseq_update( fd_forest_ver( forest ), FD_FOREST_VER_INVAL );
     178             : 
     179           0 :   fd_forest_blk_t *      pool      = fd_forest_pool( forest );
     180           0 :   ulong                  null      = fd_forest_pool_idx_null( pool );
     181           0 :   fd_forest_ancestry_t * ancestry  = fd_forest_ancestry( forest );
     182           0 :   fd_forest_frontier_t * frontier  = fd_forest_frontier( forest );
     183           0 :   fd_forest_subtrees_t * subtrees  = fd_forest_subtrees( forest );
     184           0 :   fd_forest_orphaned_t * orphaned  = fd_forest_orphaned( forest );
     185           0 :   if( FD_UNLIKELY( !fd_forest_pool_used( pool ) ) ) return forest;
     186             : 
     187           0 :   ulong * q = fd_forest_deque( forest );
     188           0 :   fd_forest_deque_remove_all( q );
     189           0 :   for( fd_forest_ancestry_iter_t iter = fd_forest_ancestry_iter_init( ancestry, pool );
     190           0 :        !fd_forest_ancestry_iter_done( iter, ancestry, pool );
     191           0 :        iter = fd_forest_ancestry_iter_next( iter, ancestry, pool ) ) {
     192           0 :     fd_forest_deque_push_tail( q, fd_forest_ancestry_iter_idx( iter, ancestry, pool ) );
     193           0 :   }
     194           0 :   while( !fd_forest_deque_empty( q ) ) {
     195           0 :     ulong idx = fd_forest_deque_pop_head( q );
     196           0 :     FD_TEST( fd_forest_ancestry_ele_remove( ancestry, &fd_forest_pool_ele( pool, idx )->slot, NULL, pool ) );
     197           0 :     fd_forest_pool_idx_release( pool, idx );
     198           0 :   }
     199           0 :   for( fd_forest_frontier_iter_t iter = fd_forest_frontier_iter_init( frontier, pool );
     200           0 :        !fd_forest_frontier_iter_done( iter, frontier, pool );
     201           0 :        iter = fd_forest_frontier_iter_next( iter, frontier, pool ) ) {
     202           0 :     fd_forest_deque_push_tail( q, fd_forest_frontier_iter_idx( iter, frontier, pool ) );
     203           0 :   }
     204           0 :   while( !fd_forest_deque_empty( q ) ) {
     205           0 :     ulong idx = fd_forest_deque_pop_head( q );
     206           0 :     FD_TEST( fd_forest_frontier_ele_remove( frontier, &fd_forest_pool_ele( pool, idx )->slot, NULL, pool ) );
     207           0 :     fd_forest_pool_idx_release( pool, idx );
     208           0 :   }
     209           0 :   for( fd_forest_subtrees_iter_t iter = fd_forest_subtrees_iter_init( subtrees, pool );
     210           0 :        !fd_forest_subtrees_iter_done( iter, subtrees, pool );
     211           0 :        iter = fd_forest_subtrees_iter_next( iter, subtrees, pool ) ) {
     212           0 :     fd_forest_deque_push_tail( q, fd_forest_subtrees_iter_idx( iter, subtrees, pool ) );
     213           0 :   }
     214           0 :   while( !fd_forest_deque_empty( q ) ) {
     215           0 :     ulong idx = fd_forest_deque_pop_head( q );
     216           0 :     FD_TEST( fd_forest_subtrees_ele_remove( subtrees, &fd_forest_pool_ele( pool, idx )->slot, NULL, pool ) );
     217           0 :     fd_forest_pool_idx_release( pool, idx );
     218           0 :   }
     219           0 :   for( fd_forest_orphaned_iter_t iter = fd_forest_orphaned_iter_init( orphaned, pool );
     220           0 :        !fd_forest_orphaned_iter_done( iter, orphaned, pool );
     221           0 :        iter = fd_forest_orphaned_iter_next( iter, orphaned, pool ) ) {
     222           0 :     fd_forest_deque_push_tail( q, fd_forest_orphaned_iter_idx( iter, orphaned, pool ) );
     223           0 :   }
     224           0 :   while( !fd_forest_deque_empty( q ) ) {
     225           0 :     ulong idx = fd_forest_deque_pop_head( q );
     226           0 :     FD_TEST( fd_forest_orphaned_ele_remove( orphaned, &fd_forest_pool_ele( pool, idx )->slot, NULL, pool ) );
     227           0 :     fd_forest_pool_idx_release( pool, idx );
     228           0 :   }
     229           0 :   forest->root = null;
     230           0 : # if FD_FOREST_USE_HANDHOLDING
     231           0 :   FD_TEST( !fd_forest_pool_used( pool ) );
     232           0 : # endif
     233             : 
     234           0 :   fd_fseq_update( fd_forest_ver( forest ), FD_FOREST_VER_UNINIT );
     235           0 :   return forest;
     236           0 : }
     237             : 
     238             : int
     239           0 : fd_forest_verify( fd_forest_t const * forest ) {
     240           0 :   if( FD_UNLIKELY( !forest ) ) {
     241           0 :     FD_LOG_WARNING(( "NULL forest" ));
     242           0 :     return -1;
     243           0 :   }
     244             : 
     245           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)forest, fd_forest_align() ) ) ) {
     246           0 :     FD_LOG_WARNING(( "misaligned forest" ));
     247           0 :     return -1;
     248           0 :   }
     249             : 
     250           0 :   fd_wksp_t * wksp = fd_wksp_containing( forest );
     251           0 :   if( FD_UNLIKELY( !wksp ) ) {
     252           0 :     FD_LOG_WARNING(( "forest must be part of a workspace" ));
     253           0 :     return -1;
     254           0 :   }
     255             : 
     256           0 :   if( FD_UNLIKELY( forest->magic!=FD_FOREST_MAGIC ) ) {
     257           0 :     FD_LOG_WARNING(( "bad magic" ));
     258           0 :     return -1;
     259           0 :   }
     260             : 
     261           0 :   if( FD_UNLIKELY( fd_fseq_query( fd_forest_ver_const( forest ) ) == ULONG_MAX ) ) {
     262           0 :     FD_LOG_WARNING(( "forest uninitialized or invalid" ));
     263           0 :     return -1;
     264           0 :   }
     265             : 
     266           0 :   fd_forest_blk_t const * pool = fd_forest_pool_const( forest );
     267             : 
     268           0 :   fd_forest_frontier_t const * frontier = fd_forest_frontier_const( forest );
     269           0 :   fd_forest_orphaned_t const * orphaned = fd_forest_orphaned_const( forest );
     270           0 :   fd_forest_ancestry_t const * ancestry = fd_forest_ancestry_const( forest );
     271           0 :   fd_forest_subtrees_t const * subtrees = fd_forest_subtrees_const( forest );
     272             : 
     273           0 :   if( fd_forest_ancestry_verify( ancestry, fd_forest_pool_max( pool ), pool ) == -1 ) return -1;
     274           0 :   if( fd_forest_frontier_verify( frontier, fd_forest_pool_max( pool ), pool ) == -1 ) return -1;
     275           0 :   if( fd_forest_subtrees_verify( subtrees, fd_forest_pool_max( pool ), pool ) == -1 ) return -1;
     276           0 :   if( fd_forest_orphaned_verify( orphaned, fd_forest_pool_max( pool ), pool ) == -1 ) return -1;
     277             : 
     278             :   /* Invariant: elements can only appear in one of the four maps. */
     279           0 :   for( fd_forest_frontier_iter_t iter = fd_forest_frontier_iter_init( frontier, pool ); !fd_forest_frontier_iter_done( iter, frontier, pool ); iter = fd_forest_frontier_iter_next( iter, frontier, pool ) ) {
     280           0 :     fd_forest_blk_t const * ele = fd_forest_frontier_iter_ele_const( iter, frontier, pool );
     281           0 :     if( fd_forest_ancestry_ele_query_const( ancestry, &ele->slot, NULL, pool ) ) return -1;
     282           0 :     if( fd_forest_orphaned_ele_query_const( orphaned, &ele->slot, NULL, pool ) ) return -1;
     283           0 :     if( fd_forest_subtrees_ele_query_const( subtrees, &ele->slot, NULL, pool ) ) return -1;
     284           0 :   }
     285             : 
     286           0 :   for( fd_forest_orphaned_iter_t iter = fd_forest_orphaned_iter_init( orphaned, pool ); !fd_forest_orphaned_iter_done( iter, orphaned, pool ); iter = fd_forest_orphaned_iter_next( iter, orphaned, pool ) ) {
     287           0 :     fd_forest_blk_t const * ele = fd_forest_orphaned_iter_ele_const( iter, orphaned, pool );
     288           0 :     if( fd_forest_ancestry_ele_query_const( ancestry, &ele->slot, NULL, pool ) ) return -1;
     289           0 :     if( fd_forest_frontier_ele_query_const( frontier, &ele->slot, NULL, pool ) ) return -1;
     290           0 :     if( fd_forest_subtrees_ele_query_const( subtrees, &ele->slot, NULL, pool ) ) return -1;
     291           0 :   }
     292             : 
     293           0 :   for( fd_forest_subtrees_iter_t iter = fd_forest_subtrees_iter_init( subtrees, pool ); !fd_forest_subtrees_iter_done( iter, subtrees, pool ); iter = fd_forest_subtrees_iter_next( iter, subtrees, pool ) ) {
     294           0 :     fd_forest_blk_t const * ele = fd_forest_subtrees_iter_ele_const( iter, subtrees, pool );
     295           0 :     if( fd_forest_ancestry_ele_query_const( ancestry, &ele->slot, NULL, pool ) ) return -1;
     296           0 :     if( fd_forest_frontier_ele_query_const( frontier, &ele->slot, NULL, pool ) ) return -1;
     297           0 :     if( fd_forest_orphaned_ele_query_const( orphaned, &ele->slot, NULL, pool ) ) return -1;
     298           0 :   }
     299             : 
     300           0 :   fd_forest_consumed_t const * consumed = fd_forest_consumed_const( forest );
     301           0 :   fd_forest_cns_t const *      conspool = fd_forest_conspool_const( forest );
     302             : 
     303             :   /* from every frontier walk back and verify that there is an ancestor in the consumed map */
     304           0 :   for( fd_forest_frontier_iter_t iter = fd_forest_frontier_iter_init( frontier, pool ); !fd_forest_frontier_iter_done( iter, frontier, pool ); iter = fd_forest_frontier_iter_next( iter, frontier, pool ) ) {
     305           0 :     fd_forest_blk_t const * ele = fd_forest_frontier_iter_ele_const( iter, frontier, pool );
     306           0 :     int found = 0;
     307           0 :     while( FD_LIKELY( ele ) ) {
     308           0 :       if( fd_forest_consumed_ele_query_const( consumed, &ele->slot, NULL, conspool ) ) {
     309           0 :         found = 1;
     310           0 :         break;
     311           0 :       }
     312           0 :       ele = fd_forest_pool_ele_const( pool, ele->parent );
     313           0 :     }
     314           0 :     if( FD_UNLIKELY( !found ) ) return -1;
     315           0 :   }
     316             : 
     317             :   /* Consumed map elements must be in the frontier or ancestry map. */
     318             : 
     319           0 :   for( fd_forest_consumed_iter_t iter = fd_forest_consumed_iter_init( consumed, conspool ); !fd_forest_consumed_iter_done( iter, consumed, conspool ); iter = fd_forest_consumed_iter_next( iter, consumed, conspool ) ) {
     320           0 :     fd_forest_cns_t const * ele = fd_forest_consumed_iter_ele_const( iter, consumed, conspool );
     321           0 :     if( !fd_forest_ancestry_ele_query_const( ancestry, &ele->slot, NULL, pool ) && !fd_forest_frontier_ele_query_const( frontier, &ele->slot, NULL, pool ) ) {
     322           0 :       return -1;
     323           0 :     }
     324           0 :   }
     325             : 
     326           0 :   return 0;
     327           0 : }
     328             : 
     329             : /* remove removes and returns a connected ele from ancestry or frontier
     330             :    maps.  does not remove orphaned ele.  does not unlink ele. */
     331             : 
     332             : static fd_forest_blk_t *
     333           0 : ancestry_frontier_remove( fd_forest_t * forest, ulong slot ) {
     334           0 :   fd_forest_blk_t * pool = fd_forest_pool( forest );
     335           0 :   fd_forest_blk_t * ele  = NULL;
     336           0 :   ele =                  fd_forest_ancestry_ele_remove( fd_forest_ancestry( forest ), &slot, NULL, pool );
     337           0 :   ele = fd_ptr_if( !ele, fd_forest_frontier_ele_remove( fd_forest_frontier( forest ), &slot, NULL, pool ), ele );
     338           0 :   return ele;
     339           0 : }
     340             : 
     341             : static fd_forest_blk_t *
     342           0 : subtrees_orphaned_remove( fd_forest_t * forest, ulong slot ) {
     343           0 :   fd_forest_blk_t * pool = fd_forest_pool( forest );
     344           0 :   fd_forest_blk_t * ele = NULL;
     345           0 :   ele =                  fd_forest_orphaned_ele_remove( fd_forest_orphaned( forest ), &slot, NULL, pool );
     346           0 :   ele = fd_ptr_if( !ele, fd_forest_subtrees_ele_remove( fd_forest_subtrees( forest ), &slot, NULL, pool ), ele );
     347           0 :   return ele;
     348           0 : }
     349             : 
     350             : /* link ele to the tree via its sibling. */
     351             : 
     352             : static void
     353           0 : link_sibling( fd_forest_t * forest, fd_forest_blk_t * sibling, fd_forest_blk_t * ele ) {
     354           0 :   fd_forest_blk_t * pool = fd_forest_pool( forest );
     355           0 :   ulong             null = fd_forest_pool_idx_null( pool );
     356           0 :   while( FD_UNLIKELY( sibling->sibling != null )) sibling = fd_forest_pool_ele( pool, sibling->sibling );
     357           0 :   sibling->sibling = fd_forest_pool_idx( pool, ele );
     358           0 : }
     359             : 
     360             : /* link child to the tree via its parent. */
     361             : 
     362             : static void
     363           0 : link( fd_forest_t * forest, fd_forest_blk_t * parent, fd_forest_blk_t * child ) {
     364           0 :   fd_forest_blk_t * pool = fd_forest_pool( forest );
     365           0 :   ulong             null = fd_forest_pool_idx_null( pool );
     366           0 :   if( FD_LIKELY( parent->child == null ) ) parent->child = fd_forest_pool_idx( pool, child ); /* left-child */
     367           0 :   else link_sibling( forest, fd_forest_pool_ele( pool, parent->child ), child );          /* right-sibling */
     368           0 :   child->parent = fd_forest_pool_idx( pool, parent );
     369           0 : }
     370             : 
     371             : /* advance_consumed_frontier attempts to advance the consumed frontier beginning from slot
     372             :    using BFS.  head is the first element of a linked list representing
     373             :    the BFS queue.  A slot can be advanced if all shreds for the block
     374             :    are received ie. consumed_idx = complete_idx. */
     375             : 
     376             : static void
     377           0 : advance_consumed_frontier( fd_forest_t * forest, ulong slot, ulong parent_slot ) {
     378           0 :   fd_forest_blk_t *      pool     = fd_forest_pool( forest );
     379           0 :   fd_forest_cns_t *      conspool = fd_forest_conspool( forest );
     380           0 :   fd_forest_consumed_t * consumed = fd_forest_consumed( forest );
     381           0 :   ulong                * queue    = fd_forest_deque( forest );
     382             : 
     383           0 :   fd_forest_cns_t * ele;
     384           0 :   ele = fd_forest_consumed_ele_query( consumed, &slot, NULL, conspool );
     385           0 :   ele = fd_ptr_if( !ele, fd_forest_consumed_ele_query( consumed, &parent_slot, NULL, conspool ), ele );
     386           0 :   if( FD_UNLIKELY( !ele ) ) return;
     387             : 
     388           0 : # if FD_FOREST_USE_HANDHOLDING
     389           0 :   FD_TEST( fd_forest_deque_cnt( queue ) == 0 );
     390           0 : # endif
     391             : 
     392             :   /* BFS elements as pool idxs.
     393             :      Invariant: whatever is in the queue, must be in the consumed map. */
     394           0 :   fd_forest_deque_push_tail( queue, ele->forest_pool_idx );
     395           0 :   while( FD_LIKELY( fd_forest_deque_cnt( queue ) ) ) {
     396           0 :     fd_forest_blk_t * head  = fd_forest_pool_ele( pool, fd_forest_deque_pop_head( queue ) );
     397           0 :     fd_forest_blk_t * child = fd_forest_pool_ele( pool, head->child );
     398           0 :     if( FD_LIKELY( child &&
     399           0 :                    head->complete_idx != UINT_MAX &&
     400           0 :                    head->complete_idx == head->buffered_idx &&                                                     /* we've received all the shreds for the slot */
     401           0 :                    0==memcmp( head->cmpl, head->fecs, sizeof(fd_forest_blk_idxs_t) * fd_forest_blk_idxs_word_cnt ) /* AND all the FECs for the slot have been completed */) ) {
     402           0 :       fd_forest_cns_t * cons = fd_forest_consumed_ele_remove( consumed, &head->slot, NULL, conspool );
     403           0 :       fd_forest_conspool_ele_release( conspool, cons );
     404           0 :       while( FD_LIKELY( child ) ) { /* add children to consumed frontier */
     405           0 :         consumed_map_insert( forest, child->slot, fd_forest_pool_idx( pool, child ) );
     406             : 
     407           0 :         fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, child ) );
     408           0 :         child = fd_forest_pool_ele( pool, child->sibling );
     409           0 :       }
     410           0 :     }
     411           0 :   }
     412           0 : }
     413             : 
     414             : static fd_forest_blk_t *
     415           0 : query( fd_forest_t * forest, ulong slot ) {
     416           0 :   fd_forest_blk_t *      pool      = fd_forest_pool( forest );
     417           0 :   fd_forest_ancestry_t * ancestry  = fd_forest_ancestry( forest );
     418           0 :   fd_forest_frontier_t * frontier  = fd_forest_frontier( forest );
     419           0 :   fd_forest_subtrees_t * subtrees  = fd_forest_subtrees( forest );
     420           0 :   fd_forest_orphaned_t * orphaned  = fd_forest_orphaned( forest );
     421             : 
     422           0 :   fd_forest_blk_t * ele = NULL;
     423           0 :   ele =                  fd_forest_ancestry_ele_query( ancestry, &slot, NULL, pool );
     424           0 :   ele = fd_ptr_if( !ele, fd_forest_frontier_ele_query( frontier, &slot, NULL, pool ), ele );
     425           0 :   ele = fd_ptr_if( !ele, fd_forest_subtrees_ele_query( subtrees, &slot, NULL, pool ), ele );
     426           0 :   ele = fd_ptr_if( !ele, fd_forest_orphaned_ele_query( orphaned, &slot, NULL, pool ), ele );
     427           0 :   return ele;
     428           0 : }
     429             : 
     430             : static fd_forest_blk_t *
     431           0 : acquire( fd_forest_t * forest, ulong slot, ulong parent_slot ) {
     432           0 :   fd_forest_blk_t * pool = fd_forest_pool( forest );
     433           0 :   if( FD_UNLIKELY( !fd_forest_pool_free( pool ) ) ) {
     434           0 :     FD_LOG_ERR(( "Firedancer ran out of memory when repairing new blocks. If this happened during catchup, your "
     435           0 :                  "snapshot is likely too old and there are too many blocks to repair. You can fix this by using a more "
     436           0 :                  "recent snapshot (if loading a pre-downloaded snapshot) or rebooting (if downloading the snapshot "
     437           0 :                  "live). If this happened while running live (after catchup), Firedancer got disconnected from the "
     438           0 :                  "cluster and stopped being able to receive shreds. Try rebooting." ));
     439           0 :   }
     440           0 :   fd_forest_blk_t * blk  = fd_forest_pool_ele_acquire( pool );
     441           0 :   ulong             null = fd_forest_pool_idx_null( pool );
     442             : 
     443           0 :   blk->slot        = slot;
     444           0 :   blk->parent_slot = parent_slot;
     445           0 :   blk->next        = null;
     446           0 :   blk->parent      = null;
     447           0 :   blk->child       = null;
     448           0 :   blk->sibling     = null;
     449             : 
     450           0 :   blk->consumed_idx = UINT_MAX;
     451           0 :   blk->buffered_idx = UINT_MAX;
     452           0 :   blk->complete_idx = UINT_MAX;
     453             : 
     454           0 :   fd_forest_blk_idxs_null( blk->fecs ); /* expensive */
     455           0 :   fd_forest_blk_idxs_null( blk->idxs ); /* expensive */
     456           0 :   fd_forest_blk_idxs_null( blk->cmpl ); /* expensive */
     457             : 
     458           0 :   fd_forest_blk_idxs_null( blk->code ); /* FIXME expensive */
     459           0 :   blk->first_shred_ts = 0;
     460           0 :   blk->first_req_ts   = 0;
     461           0 :   blk->turbine_cnt    = 0;
     462           0 :   blk->repair_cnt     = 0;
     463             : 
     464           0 :   return blk;
     465           0 : }
     466             : 
     467             : fd_forest_blk_t *
     468           0 : fd_forest_query( fd_forest_t * forest, ulong slot ) {
     469           0 :   return query( forest, slot );
     470           0 : }
     471             : 
     472             : fd_forest_blk_t *
     473           0 : fd_forest_blk_insert( fd_forest_t * forest, ulong slot, ulong parent_slot ) {
     474           0 : # if FD_FOREST_USE_HANDHOLDING
     475           0 :   FD_TEST( slot > fd_forest_root_slot( forest ) ); /* caller error - inval */
     476           0 : # endif
     477           0 :   fd_forest_blk_t * ele = query( forest, slot );
     478           0 :   if( FD_LIKELY( ele ) ) { return ele; }
     479             : 
     480           0 :   fd_forest_ancestry_t * ancestry = fd_forest_ancestry( forest );
     481           0 :   fd_forest_frontier_t * frontier = fd_forest_frontier( forest );
     482           0 :   fd_forest_subtrees_t * subtrees = fd_forest_subtrees( forest );
     483           0 :   fd_forest_orphaned_t * orphaned = fd_forest_orphaned( forest );
     484           0 :   fd_forest_consumed_t * consumed = fd_forest_consumed( forest );
     485           0 :   fd_forest_cns_t *      conspool = fd_forest_conspool( forest );
     486           0 :   fd_forest_blk_t *      pool     = fd_forest_pool ( forest );
     487           0 :   ulong *                bfs      = fd_forest_deque( forest );
     488             : 
     489           0 :   fd_forest_blk_t * parent = NULL;
     490             : 
     491           0 :   ele = acquire( forest, slot, parent_slot );
     492             : 
     493           0 :   if(        FD_LIKELY  ( parent = fd_forest_ancestry_ele_query ( ancestry, &parent_slot, NULL, pool ) ) ) { /* parent is in ancestry, ele makes new frontier */
     494           0 :     fd_forest_frontier_ele_insert( frontier, ele, pool );
     495           0 :   } else if( FD_UNLIKELY( parent = fd_forest_frontier_ele_remove( frontier, &parent_slot, NULL, pool ) ) ) { /* parent is in frontier, ele makes new frontier */
     496           0 :     fd_forest_ancestry_ele_insert( ancestry, parent, pool );
     497           0 :     fd_forest_frontier_ele_insert( frontier, ele,    pool );
     498           0 :   } else if( FD_UNLIKELY( parent = fd_forest_orphaned_ele_query ( orphaned, &parent_slot, NULL, pool ) ) ) { /* parent is in orphaned, ele makes new orphaned */
     499           0 :     fd_forest_orphaned_ele_insert( orphaned, ele, pool );
     500           0 :   } else if( FD_UNLIKELY( parent = fd_forest_subtrees_ele_query ( subtrees, &parent_slot, NULL, pool ) ) ) { /* parent is in subtrees, ele makes new orphaned */
     501           0 :     fd_forest_orphaned_ele_insert( orphaned, ele, pool );
     502           0 :   } else {                                                                                                   /* parent is not in any map, ele makes new subtree */
     503           0 :     fd_forest_subtrees_ele_insert( subtrees, ele, pool );
     504           0 :   }
     505             : 
     506           0 :   if( FD_LIKELY( parent ) ) link( forest, parent, ele );
     507             : 
     508             :   /* Iterate subtrees and connect ones where the parent slot matches up
     509             :      to the new ele.*/
     510             : 
     511           0 :   for( fd_forest_subtrees_iter_t iter = fd_forest_subtrees_iter_init( subtrees, pool );
     512           0 :        !fd_forest_subtrees_iter_done( iter, subtrees, pool );
     513           0 :        iter = fd_forest_subtrees_iter_next( iter, subtrees, pool ) ) {
     514           0 :     fd_forest_blk_t * orphan = fd_forest_subtrees_iter_ele( iter, subtrees, pool );
     515           0 :     fd_forest_deque_push_tail( bfs, fd_forest_pool_idx( pool, orphan ) );
     516           0 :   }
     517           0 :   while( FD_LIKELY( fd_forest_deque_cnt( bfs ) ) ) {
     518           0 :     fd_forest_blk_t * orphan = fd_forest_pool_ele( pool, fd_forest_deque_pop_head( bfs ) );
     519           0 :     if( FD_UNLIKELY( orphan->parent_slot == ele->slot ) ) {
     520           0 :       link( forest, ele, orphan );
     521           0 :       fd_forest_subtrees_ele_remove( subtrees, &orphan->slot, NULL, pool );
     522           0 :       fd_forest_orphaned_ele_insert( orphaned, orphan,              pool );
     523           0 :     }
     524           0 :   }
     525             : 
     526             :   /* At this point we are in the state where:
     527             : 
     528             :     ele      < in frontier/subtrees/orphaned >
     529             :      |
     530             :     children < all in orphaned >
     531             : 
     532             :     if ele is in frontier, we need to extend the frontier from this child.
     533             :     if ele is in orphaned/subtrees, we are done. don't do anything, */
     534             : 
     535           0 :   if( FD_LIKELY( fd_forest_frontier_ele_query( frontier, &ele->slot, NULL, pool ) ) ) fd_forest_deque_push_tail( bfs, fd_forest_pool_idx( pool, ele ) );
     536           0 :   while( FD_LIKELY( !fd_forest_deque_empty( bfs ) ) ) {
     537           0 :     fd_forest_blk_t * parent = fd_forest_pool_ele( pool, fd_forest_deque_pop_head( bfs ) );
     538           0 :     fd_forest_blk_t * child  = fd_forest_pool_ele( pool, parent->child );
     539           0 :     if( FD_LIKELY( child ) ) {
     540           0 :       fd_forest_frontier_ele_remove( frontier, &parent->slot, NULL, pool );
     541           0 :       fd_forest_ancestry_ele_insert( ancestry, parent,              pool );
     542           0 :     }
     543           0 :     while( FD_LIKELY( child ) ) {
     544           0 :       fd_forest_orphaned_ele_remove( orphaned, &child->slot, NULL, pool );
     545           0 :       fd_forest_frontier_ele_insert( frontier, child,              pool );
     546           0 :       fd_forest_deque_push_tail( bfs, fd_forest_pool_idx( pool, child ) );
     547           0 :       child = fd_forest_pool_ele( pool, child->sibling );
     548           0 :     }
     549           0 :   }
     550             : 
     551           0 :   FD_TEST( fd_forest_deque_empty( bfs ) );
     552           0 :   if( FD_LIKELY( fd_forest_ancestry_ele_query( ancestry, &ele->slot, NULL, pool ) ||
     553           0 :                  fd_forest_frontier_ele_query( frontier, &ele->slot, NULL, pool ) ) ) {
     554             :     /* There is a chance that we connected this ele to the main tree.
     555             :        If this ele doesn't have a parent in the consumed map, add it
     556             :        to the consumed map. */
     557           0 :     fd_forest_blk_t * ancestor = ele;
     558           0 :     while( FD_UNLIKELY( ancestor && !fd_forest_consumed_ele_query( consumed, &ancestor->slot, NULL, conspool ) ) ) {
     559           0 :       ancestor = fd_forest_pool_ele( pool, ancestor->parent );
     560           0 :     }
     561           0 :     if( FD_UNLIKELY( !ancestor ) ) {
     562           0 :       FD_LOG_NOTICE(( "fd_forest: ensure_consumed_reachable: ele %lu is not reachable from consumed frontier, adding myself", ele->slot ));
     563           0 :       consumed_map_insert( forest, ele->slot, fd_forest_pool_idx( pool, ele ) );
     564           0 :     }
     565           0 :   }
     566           0 :   return ele;
     567           0 : }
     568             : 
     569             : fd_forest_blk_t *
     570           0 : fd_forest_data_shred_insert( fd_forest_t * forest, ulong slot, ulong parent_slot FD_PARAM_UNUSED, uint shred_idx, uint fec_set_idx, int slot_complete, int src ) {
     571           0 :   VER_INC;
     572           0 :   fd_forest_blk_t * ele = query( forest, slot );
     573           0 : # if FD_FOREST_USE_HANDHOLDING
     574           0 :   if( FD_UNLIKELY( !ele ) ) FD_LOG_ERR(( "fd_forest: fd_forest_data_shred_insert: ele %lu is not in the forest. data_shred_insert should be preceded by blk_insert", slot ));
     575           0 : # endif
     576           0 :   fd_forest_blk_idxs_insert_if( ele->fecs, fec_set_idx > 0, fec_set_idx - 1 );
     577           0 :   fd_forest_blk_idxs_insert_if( ele->fecs, slot_complete,   shred_idx       );
     578           0 :   ele->complete_idx = fd_uint_if( slot_complete, shred_idx, ele->complete_idx );
     579             : 
     580           0 :   if( !fd_forest_blk_idxs_test( ele->idxs, shred_idx ) ) { /* newly seen shred */
     581           0 :     ele->turbine_cnt += (src==SHRED_SRC_TURBINE);
     582           0 :     ele->repair_cnt  += (src==SHRED_SRC_REPAIR);
     583           0 :   }
     584           0 :   if( FD_UNLIKELY( ele->first_shred_ts == 0 ) ) ele->first_shred_ts = fd_tickcount();
     585             : 
     586           0 :   fd_forest_blk_idxs_insert( ele->idxs, shred_idx );
     587           0 :   while( fd_forest_blk_idxs_test( ele->idxs, ele->buffered_idx + 1U ) ) ele->buffered_idx++;
     588           0 :   advance_consumed_frontier( forest, slot, parent_slot );
     589           0 :   return ele;
     590           0 : }
     591             : 
     592             : fd_forest_blk_t *
     593           0 : fd_forest_fec_insert( fd_forest_t * forest, ulong slot, ulong parent_slot, uint last_shred_idx, uint fec_set_idx, int slot_complete ) {
     594           0 :   VER_INC;
     595             : 
     596           0 :   fd_forest_blk_t * ele = query( forest, slot );
     597           0 : # if FD_FOREST_USE_HANDHOLDING
     598           0 :   if( FD_UNLIKELY( !ele ) ) FD_LOG_ERR(( "fd_forest_fec_insert: ele %lu is not in the forest. fec_insert should be preceded by blk_insert", slot ));
     599           0 : # endif
     600             :   /* It's important that we set the cmpl idx here. If this happens to be
     601             :      the last fec_complete we needed to finish the slot, then we rely on
     602             :      the advance_consumed_frontier call in the below data_shred_insert
     603             :      to move forward the consumed frontier.  */
     604           0 :   fd_forest_blk_idxs_insert( ele->cmpl, last_shred_idx );
     605           0 :   for( uint idx = fec_set_idx; idx <= last_shred_idx; idx++ ) {
     606           0 :     ele = fd_forest_data_shred_insert( forest, slot, parent_slot, idx, fec_set_idx, slot_complete & (idx == last_shred_idx), SHRED_SRC_RECOVERED );
     607           0 :   }
     608           0 :   return ele;
     609           0 : }
     610             : 
     611             : fd_forest_blk_t *
     612           0 : fd_forest_code_shred_insert( fd_forest_t * forest, ulong slot, uint shred_idx ) {
     613           0 :   fd_forest_blk_t * ele  = query( forest, slot );
     614           0 :   if( FD_UNLIKELY( !ele ) ) {
     615           0 :     return NULL;
     616           0 :   }
     617           0 :   if( FD_UNLIKELY( ele->first_shred_ts == 0 ) ) ele->first_shred_ts = fd_tickcount();
     618             : 
     619           0 :   if( FD_UNLIKELY( shred_idx >= fd_forest_blk_idxs_max( ele->code ) ) ) {
     620           0 :     FD_LOG_INFO(( "fd_forest: fd_forest_code_shred_insert: shred_idx %u is greater than max, not tracking.", shred_idx ));
     621           0 :     ele->turbine_cnt += 1;
     622           0 :     return ele;
     623           0 :   }
     624             : 
     625           0 :   if( FD_LIKELY( !fd_forest_blk_idxs_test( ele->code, shred_idx ) ) ) { /* newly seen shred */
     626           0 :     ele->turbine_cnt += 1;
     627           0 :     fd_forest_blk_idxs_insert( ele->code, shred_idx );
     628           0 :   }
     629           0 :   return ele;
     630           0 : }
     631             : 
     632             : void
     633           0 : fd_forest_fec_clear( fd_forest_t * forest, ulong slot, uint fec_set_idx, uint max_shred_idx ) {
     634           0 :   VER_INC;
     635             : 
     636           0 :   if( FD_UNLIKELY( slot <= fd_forest_root_slot( forest ) ) ) {
     637           0 :     FD_LOG_NOTICE(( "fd_forest: fd_forest_fec_clear: slot %lu is <= root slot %lu, ignoring", slot, fd_forest_root_slot( forest ) ));
     638           0 :     return;
     639           0 :   }
     640           0 :   fd_forest_blk_t * ele = query( forest, slot );
     641           0 :   if( FD_UNLIKELY( !ele ) ) return;
     642           0 :   for( uint i=fec_set_idx; i<=fec_set_idx+max_shred_idx; i++ ) {
     643           0 :     fd_forest_blk_idxs_remove( ele->idxs, i );
     644           0 :   }
     645           0 :   if( FD_UNLIKELY( fec_set_idx == 0 ) ) ele->buffered_idx = UINT_MAX;
     646           0 :   else                                  ele->buffered_idx = fd_uint_if( ele->buffered_idx != UINT_MAX, fd_uint_min( ele->buffered_idx, fec_set_idx - 1 ), UINT_MAX );
     647           0 : }
     648             : 
     649             : fd_forest_blk_t const *
     650           0 : fd_forest_publish( fd_forest_t * forest, ulong new_root_slot ) {
     651           0 :   FD_LOG_DEBUG(( "[%s] slot %lu", __func__, new_root_slot ));
     652             : 
     653           0 :   VER_INC;
     654             : 
     655           0 :   fd_forest_ancestry_t * ancestry = fd_forest_ancestry( forest );
     656           0 :   fd_forest_orphaned_t * orphaned = fd_forest_orphaned( forest );
     657           0 :   fd_forest_frontier_t * frontier = fd_forest_frontier( forest );
     658           0 :   fd_forest_subtrees_t * subtrees = fd_forest_subtrees( forest );
     659           0 :   fd_forest_consumed_t * consumed = fd_forest_consumed( forest );
     660           0 :   fd_forest_cns_t *      conspool = fd_forest_conspool( forest );
     661           0 :   fd_forest_blk_t *      pool     = fd_forest_pool( forest );
     662           0 :   ulong                  null     = fd_forest_pool_idx_null( pool );
     663           0 :   ulong *                queue    = fd_forest_deque( forest );
     664             : 
     665           0 :   fd_forest_blk_t * old_root_ele = fd_forest_pool_ele( pool, forest->root );
     666           0 :   fd_forest_blk_t * new_root_ele = query( forest, new_root_slot );
     667             : 
     668           0 : # if FD_FOREST_USE_HANDHOLDING
     669           0 :   if( FD_LIKELY( new_root_ele ) ) {
     670           0 :     FD_TEST( new_root_ele->slot > old_root_ele->slot ); /* caller error - inval */
     671           0 :   }
     672           0 : # endif
     673             : 
     674             :   /* Edge case where if we haven't been getting repairs, and we have a
     675             :      gap between the root and orphans. we publish forward to a slot that
     676             :      we don't have. This only case this should be happening is when we
     677             :      load a second incremental and that incremental slot lives in the
     678             :      gap. In that case this isn't a bug, but we should be treating this
     679             :      new root like the snapshot slot / init root. Should be happening
     680             :      very rarely given a well-functioning repair.  */
     681             : 
     682           0 :   if( FD_UNLIKELY( !new_root_ele ) ) {
     683           0 :     new_root_ele = fd_forest_blk_insert( forest, new_root_slot, 0 );
     684           0 :     new_root_ele->complete_idx = 0;
     685           0 :     new_root_ele->buffered_idx = 0;
     686           0 :     fd_forest_blk_idxs_full( new_root_ele->cmpl );
     687           0 :     fd_forest_blk_idxs_full( new_root_ele->fecs );
     688           0 :     advance_consumed_frontier( forest, new_root_slot, 0 ); /* advances consumed frontier if possible */
     689           0 :   }
     690             : 
     691             :   /* First, remove the previous root, and add it to a FIFO prune queue.
     692             :      head points to the queue head (initialized with old_root_ele). */
     693           0 : # if FD_FOREST_USE_HANDHOLDING
     694           0 :   FD_TEST( fd_forest_deque_cnt( queue ) == 0 );
     695           0 : # endif
     696           0 :   fd_forest_blk_t * head = ancestry_frontier_remove( forest, old_root_ele->slot );
     697           0 :   if( FD_LIKELY( head ) ) fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, head ) );
     698             : 
     699             :   /* Second, BFS down the tree, inserting each ele into the prune queue
     700             :      except for the new root.  Loop invariant: head always descends from
     701             :      old_root_ele and never descends from new_root_ele. */
     702             : 
     703           0 :   while( FD_LIKELY( fd_forest_deque_cnt( queue ) ) ) {
     704           0 :     head = fd_forest_pool_ele( pool, fd_forest_deque_pop_head( queue ) );
     705           0 :     fd_forest_blk_t * child = fd_forest_pool_ele( pool, head->child );
     706           0 :     while( FD_LIKELY( child ) ) {
     707           0 :       if( FD_LIKELY( child != new_root_ele ) ) { /* do not prune new root or descendants */
     708           0 :         child = ancestry_frontier_remove( forest, child->slot );
     709           0 :         fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, child ) );
     710           0 :       }
     711           0 :       child = fd_forest_pool_ele( pool, child->sibling );
     712           0 :     }
     713             : 
     714           0 :     fd_forest_cns_t * cns = NULL;
     715           0 :     if( FD_UNLIKELY( cns = fd_forest_consumed_ele_remove( consumed, &head->slot, NULL, conspool ) ) ) {
     716           0 :       fd_forest_conspool_ele_release( conspool, cns );
     717           0 :     }
     718           0 :     fd_forest_pool_ele_release( pool, head );
     719           0 :   }
     720             : 
     721           0 :   new_root_ele->parent = null; /* unlink new root from parent */
     722           0 :   forest->root         = fd_forest_pool_idx( pool, new_root_ele );
     723             : 
     724           0 :   int new_root_is_orphan = fd_forest_subtrees_ele_query( subtrees, &new_root_ele->slot, NULL, pool ) ||
     725           0 :                            fd_forest_orphaned_ele_query( orphaned, &new_root_ele->slot, NULL, pool );
     726           0 :   if( FD_UNLIKELY( new_root_is_orphan ) ) {
     727             : 
     728             :     /* Extend the frontier from the new root */
     729             : 
     730           0 :     FD_TEST( fd_forest_deque_empty( queue ) );
     731           0 :     fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, new_root_ele ) );
     732           0 :     while( FD_LIKELY( fd_forest_deque_cnt( queue ) ) ) {
     733           0 :       head = fd_forest_pool_ele( pool, fd_forest_deque_pop_head( queue ) );
     734           0 :       subtrees_orphaned_remove( forest, head->slot );
     735             : 
     736           0 :       fd_forest_blk_t * child = fd_forest_pool_ele( pool, head->child );
     737           0 :       if( FD_LIKELY( child ) ) fd_forest_ancestry_ele_insert( ancestry, head, pool );
     738           0 :       else                     fd_forest_frontier_ele_insert( frontier, head, pool );
     739           0 :       while( child ) {
     740           0 :         fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, child ) );
     741           0 :         child = fd_forest_pool_ele( pool, child->sibling );
     742           0 :       }
     743           0 :     }
     744             : 
     745           0 :   }
     746             : 
     747             :   /* If there is nothing on the consumed, we have hit an edge case
     748             :      during catching up where all of our repair frontiers were < the new root.
     749             :      In that case we need to continue repairing from the new root, so
     750             :      add it to the consumed map. */
     751             : 
     752           0 :   if( FD_UNLIKELY( fd_forest_consumed_iter_done( fd_forest_consumed_iter_init( consumed, conspool ), consumed, conspool ) ) ) {
     753           0 :     consumed_map_insert( forest, new_root_ele->slot, fd_forest_pool_idx( pool, new_root_ele ) );
     754           0 :     new_root_ele->complete_idx = 0;
     755           0 :     new_root_ele->buffered_idx = 0;
     756           0 :     fd_forest_blk_idxs_full( new_root_ele->cmpl );
     757           0 :     fd_forest_blk_idxs_full( new_root_ele->fecs );
     758           0 :     advance_consumed_frontier( forest, new_root_ele->slot, 0 );
     759           0 :   }
     760             : 
     761             : 
     762             :   /* Lastly, cleanup orphans if there orphan heads < new_root_slot.
     763             :      First, add any relevant orphans to the prune queue. */
     764             : 
     765           0 :   for( fd_forest_subtrees_iter_t iter = fd_forest_subtrees_iter_init( subtrees, pool );
     766           0 :        !fd_forest_subtrees_iter_done( iter, subtrees, pool );
     767           0 :        iter = fd_forest_subtrees_iter_next( iter, subtrees, pool ) ) {
     768           0 :     fd_forest_blk_t * ele = fd_forest_subtrees_iter_ele( iter, subtrees, pool );
     769           0 :     if( FD_UNLIKELY( ele->slot < new_root_slot ) ) {
     770           0 :       fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, ele ) );
     771           0 :     }
     772           0 :   }
     773             : 
     774             :   /* Now BFS and clean up children of these orphan heads */
     775           0 :   while( FD_UNLIKELY( fd_forest_deque_cnt( queue ) ) ) {
     776           0 :     head = fd_forest_pool_ele( pool, fd_forest_deque_pop_head( queue ) );
     777           0 :     fd_forest_blk_t * child = fd_forest_pool_ele( pool, head->child );
     778           0 :     while( FD_LIKELY( child ) ) {
     779           0 :       if( FD_LIKELY( child != new_root_ele ) ) {
     780           0 :         fd_forest_deque_push_tail( queue, fd_forest_pool_idx( pool, child ) );
     781           0 :       }
     782           0 :       child = fd_forest_pool_ele( pool, child->sibling );
     783           0 :     }
     784           0 :     ulong remove = fd_forest_orphaned_idx_remove( orphaned, &head->slot, null, pool ); /* remove myself */
     785           0 :     remove = fd_ulong_if( remove == null, fd_forest_subtrees_idx_remove( subtrees, &head->slot, null, pool ), remove );
     786           0 :     fd_forest_pool_ele_release( pool, head ); /* free head */
     787           0 :   }
     788           0 :   return new_root_ele;
     789           0 : }
     790             : 
     791             : fd_forest_t *
     792           0 : fd_forest_clear( fd_forest_t * forest ) {
     793           0 :   return forest;
     794           0 : }
     795             : 
     796             : fd_forest_iter_t
     797           0 : fd_forest_iter_init( fd_forest_t * forest ) {
     798             :   /* Find first element. Anything on the frontier. */
     799           0 :   fd_forest_blk_t      const * pool     = fd_forest_pool_const( forest );
     800           0 :   fd_forest_cns_t      const * conspool = fd_forest_conspool_const( forest );
     801           0 :   fd_forest_consumed_t const * consumed = fd_forest_consumed_const( forest );
     802             : 
     803             : 
     804           0 :   fd_forest_consumed_iter_t consumed_iter = fd_forest_consumed_iter_init( consumed, conspool );
     805           0 :   fd_forest_iter_t          repair_iter   = { fd_forest_pool_idx_null( pool ),
     806           0 :                                               UINT_MAX,
     807           0 :                                               fd_fseq_query( fd_forest_ver_const( forest ) ),
     808           0 :                                               consumed_iter };
     809             :   /* Nothing on frontier */
     810             : 
     811           0 :   if( FD_UNLIKELY( fd_forest_consumed_iter_done( consumed_iter, consumed, conspool ) ) ) return repair_iter;
     812             : 
     813             :   /* Populate initial iter shred index */
     814             : 
     815           0 :   fd_forest_cns_t const * ele_ = fd_forest_consumed_iter_ele_const( consumed_iter, consumed, conspool );
     816           0 :   fd_forest_blk_t const * ele  = fd_forest_pool_ele_const( pool, ele_->forest_pool_idx );
     817             : 
     818           0 :   while( ele->complete_idx != UINT_MAX && ele->buffered_idx == ele->complete_idx ) {
     819             :     /* This fork frontier is actually complete, so we can skip it. Also
     820             :        handles edge case where we are calling iter_init right after a
     821             :        forest_init */
     822           0 :     consumed_iter = fd_forest_consumed_iter_next( consumed_iter, consumed, conspool );
     823           0 :     if( FD_UNLIKELY( fd_forest_consumed_iter_done( consumed_iter, consumed, conspool ) ) ) {
     824           0 :       repair_iter.ele_idx   = fd_forest_pool_idx_null( pool );
     825           0 :       repair_iter.shred_idx = UINT_MAX; /* no more elements */
     826           0 :       return repair_iter;
     827           0 :     }
     828           0 :     ele_ = fd_forest_consumed_iter_ele_const( consumed_iter, consumed, conspool );
     829           0 :     ele  = fd_forest_pool_ele_const( pool, ele_->forest_pool_idx );
     830           0 :   }
     831             : 
     832           0 :   repair_iter.ele_idx   = ele_->forest_pool_idx;
     833           0 :   repair_iter.shred_idx = ele->complete_idx == UINT_MAX ? UINT_MAX : ele->buffered_idx + 1;
     834             : 
     835           0 :   return repair_iter;
     836           0 : }
     837             : 
     838             : fd_forest_iter_t
     839           0 : fd_forest_iter_next( fd_forest_iter_t iter, fd_forest_t const * forest ) {
     840           0 :   fd_forest_consumed_t const * consumed = fd_forest_consumed_const( forest );
     841           0 :   fd_forest_cns_t      const * conspool = fd_forest_conspool_const( forest );
     842             : 
     843           0 :   fd_forest_blk_t const      * pool     = fd_forest_pool_const( forest );
     844           0 :   fd_forest_blk_t const      * ele      = fd_forest_pool_ele_const( pool, iter.ele_idx );
     845             : 
     846           0 :   if( iter.frontier_ver != fd_fseq_query( fd_forest_ver_const( forest ) ) ) {
     847             :     /* If the frontier has changed since we started this traversal, we
     848             :        need to reset the iterator. */
     849           0 :     iter.ele_idx   = fd_forest_pool_idx_null( pool ) ;
     850           0 :     iter.shred_idx = UINT_MAX; /* no more elements */
     851           0 :     return iter;
     852           0 :   }
     853             : 
     854           0 :   uint next_shred_idx = iter.shred_idx;
     855           0 :   for(;;) {
     856           0 :     next_shred_idx++;
     857             : 
     858             :     /* Case 1: No more shreds in this slot to request, move to the
     859             :        next one. Wraparound the shred_idx.
     860             : 
     861             :        Case 2: original iter.shred_idx == UINT_MAX (implies prev req
     862             :        was a highest_window_idx request). Also requires moving to next
     863             :        slot and wrapping the shred_idx. */
     864             : 
     865           0 :     if( FD_UNLIKELY( next_shred_idx >= ele->complete_idx || iter.shred_idx == UINT_MAX ) ) {
     866           0 :       iter.ele_idx = ele->child;
     867           0 :       ele          = fd_forest_pool_ele_const( pool, iter.ele_idx );
     868           0 :       if( FD_UNLIKELY( iter.ele_idx == fd_forest_pool_idx_null( pool ) ) ) {
     869           0 :         iter.shred_idx = UINT_MAX; /* no more elements */
     870             : 
     871             :         /* rare and unlikely to happen during a regular run, but if the
     872             :            frontier pool hasn't changed at all since we started this
     873             :            traversal, we can cleanly select the next node in the
     874             :            frontier using the stored frontier iterator. If the frontier
     875             :            has changed though, we should just return done and let the
     876             :            caller reset the iterator. */
     877             : 
     878           0 :         if( FD_UNLIKELY( iter.frontier_ver == fd_fseq_query( fd_forest_ver_const( forest ) ) ) ) {
     879           0 :           iter.head = fd_forest_consumed_iter_next( iter.head, consumed, conspool );
     880           0 :           if( FD_UNLIKELY( !fd_forest_consumed_iter_done( iter.head, consumed, conspool ) ) ) {
     881           0 :             iter.ele_idx   = fd_forest_consumed_iter_ele_const( iter.head, consumed, conspool )->forest_pool_idx;
     882           0 :             ele            = fd_forest_pool_ele_const( pool, iter.ele_idx );
     883           0 :             iter.shred_idx = ele->complete_idx == UINT_MAX ? UINT_MAX : ele->buffered_idx + 1;
     884           0 :           }
     885           0 :         }
     886           0 :         return iter;
     887           0 :       }
     888           0 :       next_shred_idx = ele->buffered_idx + 1;
     889           0 :     }
     890             : 
     891             :     /* Common case - valid shred to request. Note you can't know the
     892             :        ele->complete_idx until you have actually received the slot
     893             :        complete shred, thus the we can do lt instead of leq  */
     894             : 
     895           0 :     if( ele->complete_idx != UINT_MAX &&
     896           0 :         next_shred_idx < ele->complete_idx &&
     897           0 :         !fd_forest_blk_idxs_test( ele->idxs, next_shred_idx ) ) {
     898           0 :       iter.shred_idx = next_shred_idx;
     899           0 :       break;
     900           0 :     }
     901             : 
     902             :     /* Current slot actually needs a highest_window_idx request */
     903             : 
     904           0 :     if( FD_UNLIKELY( ele->complete_idx == UINT_MAX ) ) {
     905           0 :       iter.shred_idx = UINT_MAX;
     906           0 :       break;
     907           0 :     }
     908           0 :   }
     909           0 :   return iter;
     910           0 : }
     911             : 
     912             : int
     913           0 : fd_forest_iter_done( fd_forest_iter_t iter, fd_forest_t const * forest ) {
     914           0 :   fd_forest_blk_t const * pool = fd_forest_pool_const( forest );
     915           0 :   return iter.ele_idx == fd_forest_pool_idx_null( pool ); /* no more elements */
     916           0 : }
     917             : 
     918             : #include <stdio.h>
     919             : 
     920             : static void
     921           0 : preorder( fd_forest_t const * forest, fd_forest_blk_t const * ele ) {
     922           0 :   fd_forest_blk_t const * pool  = fd_forest_pool_const( forest );
     923           0 :   fd_forest_blk_t const * child = fd_forest_pool_ele_const( pool, ele->child );
     924           0 :   printf( "%lu ", ele->slot );
     925           0 :   while( FD_LIKELY( child ) ) {
     926           0 :     preorder( forest, child );
     927           0 :     child = fd_forest_pool_ele_const( pool, child->sibling );
     928           0 :   }
     929           0 : }
     930             : 
     931             : void
     932           0 : fd_forest_preorder_print( fd_forest_t const * forest ) {
     933           0 :   FD_LOG_NOTICE( ( "\n\n[Preorder]" ) );
     934           0 :   preorder( forest, fd_forest_pool_ele_const( fd_forest_pool_const( forest ), forest->root ) );
     935           0 :   printf( "\n\n" );
     936           0 : }
     937             : 
     938             : /* TODO use bit tricks / change */
     939             : static int
     940           0 : num_digits( ulong slot ) {
     941             :   /* using log10 */
     942           0 :   int digits = 0;
     943           0 :   while( slot ) {
     944           0 :     digits++;
     945           0 :     slot /= 10;
     946           0 :   }
     947           0 :   return digits;
     948           0 : }
     949             : 
     950             : static void
     951             : ancestry_print2( fd_forest_t const * forest,
     952             :                  fd_forest_blk_t const    * ele,
     953             :                  fd_forest_blk_t const    * prev,
     954             :                  ulong        last_printed,
     955             :                  int          depth,
     956           0 :                  const char * prefix ) {
     957             : 
     958           0 :   if( FD_UNLIKELY( ele == NULL ) ) return;
     959             : 
     960           0 :   fd_forest_blk_t const * pool = fd_forest_pool_const( forest );
     961           0 :   int digits = num_digits( ele->slot );
     962             : 
     963             :   /* If there is a prefix, this means we are on a fork,  and we need to
     964             :      indent to the correct depth. We do depth - 1 for more satisfying
     965             :      spacing. */
     966           0 :   if( FD_UNLIKELY( strcmp( prefix, "" ) ) ) {
     967           0 :     for( int i = 0; i < depth - 1; i++ ) printf( " " );
     968           0 :     if( depth > 0 ) printf( "%s", prefix );
     969           0 :   }
     970             : 
     971           0 :   if ( FD_UNLIKELY( !prev ) ) { // New interval
     972           0 :     printf("[%lu" , ele->slot );
     973           0 :     last_printed = ele->slot;
     974           0 :     depth       += 1 + digits;
     975           0 :   }
     976             : 
     977           0 :   fd_forest_blk_t const * curr = fd_forest_pool_ele_const( pool, ele->child );
     978             : 
     979             :   /* Cases in which we close the interval:
     980             :      1. the slots are no longer consecutive. no eliding, close bracket
     981             :      2. current ele has multiple children, want to print forks.
     982             :      Maintain last_printed on this fork so that we don't print [a, a]
     983             :      intervals. */
     984             : 
     985           0 :   fd_forest_blk_t const * new_prev = ele;
     986             : 
     987           0 :   if( prev && prev->slot != ele->slot - 1 ) { // non-consecutive, do not elide
     988           0 :     if( last_printed == prev->slot ){
     989           0 :       printf( "] ── [%lu", ele->slot );
     990           0 :       depth += digits + 6;
     991           0 :     } else {
     992           0 :       printf( ", %lu] ── [%lu", prev->slot, ele->slot );
     993           0 :       depth += digits + num_digits(prev->slot ) + 8;
     994           0 :     }
     995           0 :     last_printed = ele->slot;
     996           0 :   } else if( curr && curr->sibling != ULONG_MAX ) { // has multiple children, do not elide
     997           0 :     if( last_printed == ele->slot ){
     998           0 :       printf( "] ── " );
     999           0 :       depth += 5;
    1000           0 :     } else {
    1001           0 :       printf( ", %lu] ── ", ele->slot );
    1002           0 :       depth += digits + 2;
    1003           0 :     }
    1004           0 :     last_printed = ele->slot;
    1005           0 :     new_prev = NULL;
    1006           0 :   }
    1007             : 
    1008           0 :   if( !curr ){ // no children, close bracket, end fork
    1009           0 :     if( last_printed == ele->slot ){
    1010           0 :       printf( "]\n" );
    1011           0 :     } else {
    1012           0 :       printf( ", %lu]\n", ele->slot );
    1013           0 :     }
    1014           0 :     return;
    1015           0 :   }
    1016             : 
    1017           0 :   char new_prefix[512]; /* FIXME size this correctly */
    1018           0 :   new_prefix[0] = '\0'; /* first fork stays on the same line, no prefix */
    1019           0 :   while( curr ) {
    1020           0 :     if( fd_forest_pool_ele_const( pool, curr->sibling ) ) {
    1021           0 :       ancestry_print2( forest, curr, new_prev, last_printed, depth, new_prefix );
    1022           0 :     } else {
    1023           0 :       ancestry_print2( forest, curr, new_prev, last_printed, depth, new_prefix );
    1024           0 :     }
    1025           0 :     curr = fd_forest_pool_ele_const( pool, curr->sibling );
    1026             : 
    1027             :     /* Set up prefix for following iterations */
    1028           0 :     if( curr && curr->sibling != ULONG_MAX ) {
    1029           0 :       sprintf( new_prefix, "├── " ); /* any following forks start on new lines */
    1030           0 :     } else {
    1031           0 :       sprintf( new_prefix, "└── " ); /* any following forks start on new lines */
    1032           0 :     }
    1033           0 :   }
    1034             : 
    1035           0 : }
    1036             : 
    1037             : static void FD_FN_UNUSED
    1038           0 : ancestry_print( fd_forest_t const * forest, fd_forest_blk_t const * ele, int space, const char * prefix ) {
    1039           0 :   fd_forest_blk_t const * pool = fd_forest_pool_const( forest );
    1040           0 : 
    1041           0 :   if( ele == NULL ) return;
    1042           0 : 
    1043           0 :   if( space > 0 ) printf( "\n" );
    1044           0 :   for( int i = 0; i < space; i++ ) printf( " " );
    1045           0 :   if ( ele->complete_idx == UINT_MAX ) printf( "%s%lu (%u/?)", prefix, ele->slot, ele->buffered_idx + 1 );
    1046           0 :   else printf( "%s%lu (%u/%u)", prefix, ele->slot, ele->buffered_idx + 1, ele->complete_idx + 1 );
    1047           0 : 
    1048           0 :   fd_forest_blk_t const * curr = fd_forest_pool_ele_const( pool, ele->child );
    1049           0 : 
    1050           0 :   char new_prefix[1024]; /* FIXME size this correctly */
    1051           0 :   while( curr ) {
    1052           0 :     if( fd_forest_pool_ele_const( pool, curr->sibling ) ) {
    1053           0 :       sprintf( new_prefix, "├── " ); /* branch indicating more siblings follow */
    1054           0 :       ancestry_print( forest, curr, space + 4, new_prefix );
    1055           0 :     } else {
    1056           0 :       sprintf( new_prefix, "└── " ); /* end branch */
    1057           0 :       ancestry_print( forest, curr, space + 4, new_prefix );
    1058           0 :     }
    1059           0 :     curr = fd_forest_pool_ele_const( pool, curr->sibling );
    1060           0 :   }
    1061           0 : }
    1062             : 
    1063             : static void
    1064           0 : ancestry_print3( fd_forest_t const * forest, fd_forest_blk_t const * ele, int space, const char * prefix, fd_forest_blk_t const * prev, int elide ) {
    1065           0 :   fd_forest_blk_t const * pool = fd_forest_pool_const( forest );
    1066             : 
    1067           0 :   if( ele == NULL ) return;
    1068             : 
    1069             :   /* print the slot itself. either we might need to start a new interval, or it may get elided */
    1070           0 :   fd_forest_blk_t const * child = fd_forest_pool_ele_const( pool, ele->child );
    1071             : 
    1072           0 :   if( !elide ) {
    1073           0 :     if( space > 0 ) printf( "\n" );
    1074           0 :     for( int i = 0; i < space; i++ ) printf( " " );
    1075           0 :     printf( "%s", prefix );
    1076           0 :     printf( "%lu", ele->slot );
    1077           0 :   }
    1078             : 
    1079           0 :   if( !child && !elide ) { /* double check these cases arent the same...*/
    1080           0 :     printf( "]" );
    1081           0 :     return;
    1082           0 :   } /* no children, close bracket */
    1083             : 
    1084           0 :   if( !child && elide ) {
    1085           0 :     printf( ", %lu]", ele->slot );
    1086           0 :     return;
    1087           0 :   }
    1088             : 
    1089           0 :   prev = ele;
    1090           0 :   char new_prefix[1024]; /* FIXME size this correctly */
    1091           0 :   int one_child = child && child->sibling == ULONG_MAX;
    1092           0 :   if( one_child &&
    1093           0 :       child->slot != ele->slot + 1 ) { // if I have ONE CHILD and one child is non-consecutive
    1094             : 
    1095           0 :     if( elide ) {
    1096             :       /* current slot wasn't printed, but now that we are branching,
    1097             :          we will want to print the current slot and close the bracket */
    1098           0 :       printf( ", %lu]", ele->slot );
    1099           0 :       space += fd_int_max( num_digits( ele->slot ) + 2, 0 );
    1100           0 :     } else {
    1101           0 :       printf( "]");
    1102           0 :     }
    1103             : 
    1104           0 :     sprintf( new_prefix, "└── [" ); /* end branch */
    1105           0 :     ancestry_print3( forest, child, space + 5, new_prefix, prev, 0 );
    1106           0 :   } else if ( one_child && child->slot == ele->slot + 1 ) {
    1107           0 :     ancestry_print3( forest, child, space, prefix, prev, 1);
    1108           0 :   } else { /* multiple children */
    1109           0 :     if( elide ) {
    1110             :       /* current slot wasn't printed, but now that we are branching,
    1111             :          we will want to print the current slot and close the bracket */
    1112           0 :       printf( ", %lu]", ele->slot );
    1113           0 :       space += fd_int_max( num_digits( ele->slot ) + 2, 0 );
    1114           0 :     } else {
    1115           0 :       printf( "]");
    1116           0 :     }
    1117             : 
    1118           0 :     while( child ) {
    1119           0 :       if( fd_forest_pool_ele_const( pool, child->sibling ) ) {
    1120           0 :         sprintf( new_prefix, "├── [" ); /* branch indicating more siblings follow */
    1121           0 :         ancestry_print3( forest, child, space + 5, new_prefix, prev, 0 );
    1122           0 :       } else {
    1123           0 :         sprintf( new_prefix, "└── [" ); /* end branch */
    1124           0 :         ancestry_print3( forest, child, space + 5, new_prefix, prev, 0 );
    1125           0 :       }
    1126           0 :       child = fd_forest_pool_ele_const( pool, child->sibling );
    1127           0 :     }
    1128           0 :   }
    1129           0 : }
    1130             : 
    1131             : void
    1132           0 : fd_forest_ancestry_print( fd_forest_t const * forest ) {
    1133           0 :   printf(("\n\n[Ancestry]\n" ) );
    1134           0 :   ancestry_print3( forest, fd_forest_pool_ele_const( fd_forest_pool_const( forest ), forest->root ), 0, "[", NULL, 0 );
    1135           0 :   fflush(stdout); /* Ensure ancestry printf output is flushed */
    1136           0 : }
    1137             : 
    1138             : void
    1139           0 : fd_forest_frontier_print( fd_forest_t const * forest ) {
    1140           0 :   printf( "\n\n[Repairing Next]\n" );
    1141           0 :   fd_forest_consumed_t const * consumed = fd_forest_consumed_const( forest );
    1142           0 :   fd_forest_cns_t const *      conspool = fd_forest_conspool_const( forest );
    1143           0 :   fd_forest_blk_t const *      pool     = fd_forest_pool_const( forest );
    1144           0 :   for( fd_forest_consumed_iter_t iter = fd_forest_consumed_iter_init( consumed, conspool );
    1145           0 :        !fd_forest_consumed_iter_done( iter, consumed, conspool );
    1146           0 :        iter = fd_forest_consumed_iter_next( iter, consumed, conspool ) ) {
    1147           0 :     fd_forest_cns_t const * ele = fd_forest_consumed_iter_ele_const( iter, consumed, conspool );
    1148           0 :     fd_forest_blk_t const * ele_ = fd_forest_pool_ele_const( pool, ele->forest_pool_idx );
    1149           0 :     printf("%lu (%u/%u)\n", ele_->slot, ele_->buffered_idx + 1, ele_->complete_idx + 1 );
    1150             :    //ancestry_print( forest, fd_forest_pool_ele_const( fd_forest_pool_const( forest ), fd_forest_pool_idx( pool, ele ) ), 0, "" );
    1151           0 :   }
    1152           0 :   fflush(stdout);
    1153           0 : }
    1154             : 
    1155             : void
    1156           0 : fd_forest_orphaned_print( fd_forest_t const * forest ) {
    1157           0 :   printf( "\n[Orphaned]\n" );
    1158           0 :   fd_forest_subtrees_t const * subtrees = fd_forest_subtrees_const( forest );
    1159           0 :   fd_forest_blk_t const * pool = fd_forest_pool_const( forest );
    1160           0 :   for( fd_forest_subtrees_iter_t iter = fd_forest_subtrees_iter_init( subtrees, pool );
    1161           0 :        !fd_forest_subtrees_iter_done( iter, subtrees, pool );
    1162           0 :        iter = fd_forest_subtrees_iter_next( iter, subtrees, pool ) ) {
    1163           0 :     fd_forest_blk_t const * ele = fd_forest_subtrees_iter_ele_const( iter, subtrees, pool );
    1164           0 :     ancestry_print2( forest, fd_forest_pool_ele_const( fd_forest_pool_const( forest ), fd_forest_pool_idx( pool, ele ) ), NULL, 0, 0, "" );
    1165           0 :   }
    1166           0 :   fflush(stdout);
    1167           0 : }
    1168             : 
    1169             : void
    1170           0 : fd_forest_print( fd_forest_t const * forest ) {
    1171           0 :   if( FD_UNLIKELY( forest->root == ULONG_MAX ) ) return;
    1172           0 :   FD_LOG_NOTICE(("\n\n[Forest]" ) );
    1173           0 :   fd_forest_ancestry_print( forest );
    1174           0 :   fd_forest_frontier_print( forest );
    1175           0 :   fd_forest_orphaned_print( forest );
    1176           0 :   printf("\n");
    1177             :   fflush(stdout);
    1178           0 : }
    1179             : 
    1180             : #undef FD_FOREST_PRINT

Generated by: LCOV version 1.14