LCOV - code coverage report
Current view: top level - discof/poh - fd_poh_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 174 0.0 %
Date: 2026-09-17 04:28:31 Functions: 0 10 0.0 %

          Line data    Source code
       1             : #include "fd_poh.h"
       2             : #include "fd_poh_tile.h"
       3             : #include "../replay/fd_replay_tile.h"
       4             : #include "../../util/pod/fd_pod.h"
       5             : #include "../../disco/tiles.h"
       6             : #include "../../disco/fd_clock_tile.h"
       7             : #include "../../discof/fd_startup.h"
       8             : #include <time.h>
       9             : #include "generated/fd_poh_tile_seccomp.h"
      10             : 
      11           0 : #define IN_KIND_REPLAY (0)
      12           0 : #define IN_KIND_PACK   (1)
      13           0 : #define IN_KIND_EXECLE (2)
      14             : 
      15             : struct fd_poh_in {
      16             :   fd_wksp_t * mem;
      17             :   ulong       chunk0;
      18             :   ulong       wmark;
      19             :   ulong       mtu;
      20             : };
      21             : 
      22             : typedef struct fd_poh_in fd_poh_in_t;
      23             : 
      24             : struct fd_poh_tile {
      25             :   fd_poh_t poh[1];
      26             : 
      27             :   /* There's a race condition ... let's say two execles A and B, execle
      28             :      A processes some transactions, then releases the account locks, and
      29             :      sends the microblock to PoH to be stamped.  Pack now re-packs the
      30             :      same accounts with a new microblock, sends to execle B, execle B
      31             :      executes and sends the microblock to PoH, and this all happens fast
      32             :      enough that PoH picks the 2nd block to stamp before the 1st.  The
      33             :      accounts database changes now are misordered with respect to PoH so
      34             :      replay could fail.
      35             : 
      36             :      To prevent this race, we order all microblocks and only process
      37             :      them in PoH in the order they are produced by pack.  This is a
      38             :      little bit over-strict, we just need to ensure that microblocks
      39             :      with conflicting accounts execute in order, but this is easiest to
      40             :      implement for now. */
      41             :   uint expect_pack_idx;
      42             : 
      43             :   ulong in_cnt;
      44             :   ulong idle_cnt;
      45             : 
      46             :   fd_startup_gate_t startup_gate[1];
      47             : 
      48             :   int in_kind[ 64 ];
      49             :   fd_poh_in_t in[ 64 ];
      50             : 
      51             :   fd_poh_out_t shred_out[ 1 ];
      52             :   fd_poh_out_t replay_out[ 1 ];
      53             : };
      54             : 
      55             : typedef struct fd_poh_tile fd_poh_tile_t;
      56             : 
      57             : FD_FN_CONST static inline ulong
      58           0 : scratch_align( void ) {
      59           0 :   return 128UL;
      60           0 : }
      61             : 
      62             : FD_FN_PURE static inline ulong
      63           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      64           0 :   (void)tile;
      65           0 :   ulong l = FD_LAYOUT_INIT;
      66           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_poh_tile_t), sizeof(fd_poh_tile_t) );
      67           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      68           0 : }
      69             : 
      70             : static inline void
      71           0 : during_housekeeping( fd_poh_tile_t * ctx ) {
      72           0 :   if( FD_UNLIKELY( fd_clock_tile_recal_due( ctx->poh->clock ) ) ) {
      73           0 :     fd_clock_tile_recal( ctx->poh->clock );
      74           0 :   }
      75           0 : }
      76             : 
      77             : static inline void
      78             : after_credit( fd_poh_tile_t *     ctx,
      79             :               fd_stem_context_t * stem,
      80             :               int *               opt_poll_in,
      81           0 :               int *               charge_busy ) {
      82           0 :   if( FD_UNLIKELY( !fd_startup_gate_idle( ctx->startup_gate ) ) ) return;
      83             : 
      84           0 :   ctx->idle_cnt++;
      85           0 :   if( FD_LIKELY( ctx->idle_cnt>=2UL*ctx->in_cnt || fd_poh_must_tick( ctx->poh ) || fd_poh_must_publish_skipped_tick( ctx->poh ) ) ) {
      86             :     /* We would like to fully drain input links to the best of our
      87             :        knowledge, before we spend cycles on hashing.  That is, we would
      88             :        like to assert that all input links have stayed empty since the
      89             :        last time we polled.  Given an arbitrary input link L, the worst
      90             :        case is when L is at idx 0 in the input link shuffle the last
      91             :        time we polled a frag from it, but then link L ends up at idx
      92             :        in_cnt-1 in the subsequent input link shuffle.  So strictly
      93             :        speaking we will need to have observed 2*in_cnt-1 consecutive
      94             :        empty in links to be able to assert that link L has been empty
      95             :        since the last time we polled it.
      96             : 
      97             :        Except that when we are leader and the hashcnt is right before a
      98             :        tick boundary, poh must advance to the tick boundary and produce
      99             :        the tick.  Otherwise, a tick will be skipped if a microblock
     100             :        mixin happens.  Additionally, when there are pending skipped
     101             :        ticks to be published, we should do that before processing any
     102             :        incoming microblocks. */
     103           0 :     fd_poh_advance( ctx->poh, stem, opt_poll_in, charge_busy );
     104           0 :     ctx->idle_cnt = 0UL;
     105           0 :   }
     106           0 : }
     107             : 
     108             : /* ....
     109             : 
     110             :     1. replay -> (pack, poh) ... start packing for slot
     111             :     2. if slot in progress -> pack -> poh (abandon_packing) for old slot
     112             :     3. pack free to start packing
     113             :     4. if poh slot in progress, refuse replay frag ... until see abandon_packing
     114             :     5. poh must process pack frags in order
     115             :     6. when poh sees done_packing/abandon_packing, return poh -> replay saying execle unused now */
     116             : 
     117             : static int
     118             : before_frag( fd_poh_tile_t * ctx,
     119             :              ulong           in_idx,
     120             :              ulong           seq FD_PARAM_UNUSED,
     121           0 :              ulong           sig ) {
     122           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY ) )
     123           0 :     return sig!=REPLAY_SIG_RESET && sig!=REPLAY_SIG_BECAME_LEADER && sig!=REPLAY_SIG_WFS_DONE;
     124           0 :   return 0;
     125           0 : }
     126             : 
     127             : static inline int
     128             : returnable_frag( fd_poh_tile_t *     ctx,
     129             :                  ulong               in_idx,
     130             :                  ulong               seq,
     131             :                  ulong               sig,
     132             :                  ulong               chunk,
     133             :                  ulong               sz,
     134             :                  ulong               ctl,
     135             :                  ulong               tsorig,
     136             :                  ulong               tspub,
     137           0 :                  fd_stem_context_t * stem ) {
     138           0 :   (void)seq;
     139           0 :   (void)ctl;
     140           0 :   (void)tsorig;
     141           0 :   (void)tspub;
     142             : 
     143           0 :   fd_startup_gate_busy( ctx->startup_gate );
     144             : 
     145             :   /* TODO: Pack has a workaround for Frankendancer that sequences bank
     146             :      release to manage lifetimes, but it's not needed in Firedancer so
     147             :      we just drop it.  We shouldn't send it at all in future. */
     148           0 :   if( FD_UNLIKELY( sig==FD_PACK_MSG_DONE_DRAINING && ctx->in_kind[ in_idx ]==IN_KIND_PACK ) ) {
     149           0 :     ctx->idle_cnt = 0UL;
     150           0 :     return 0;
     151           0 :   }
     152             : 
     153             :   /* Pack periodically publishes a tighter microblock bound over the
     154             :      pack_poh link. */
     155           0 :   if( FD_UNLIKELY( sig==FD_PACK_MSG_REDUCE_MB_BOUND && ctx->in_kind[ in_idx ]==IN_KIND_PACK ) ) {
     156           0 :     ctx->idle_cnt = 0UL;
     157           0 :     if( FD_UNLIKELY( !fd_poh_have_leader_bank( ctx->poh ) ) ) return 0; /* must have become leader first */
     158           0 :     FD_TEST( sz==sizeof(ulong) );
     159           0 :     ulong const * new_max = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     160           0 :     fd_poh_update_max_microblocks( ctx->poh, *new_max );
     161           0 :     return 0;
     162           0 :   }
     163             : 
     164           0 :   if( FD_UNLIKELY( sig==REPLAY_SIG_WFS_DONE && ctx->in_kind[ in_idx ]==IN_KIND_REPLAY ) ) {
     165           0 :     fd_poh_wfs_done( ctx->poh );
     166           0 :     ctx->idle_cnt = 0UL;
     167           0 :     return 0;
     168           0 :   }
     169             : 
     170           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
     171           0 :     FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->in[ in_idx ].chunk0, ctx->in[ in_idx ].wmark ));
     172             : 
     173             :   /* There's a race condition where we might receive microblocks from
     174             :      execles (or pack's done_packing, when pack ends the block on a
     175             :      reset) before we have learned what the leader bank is from replay
     176             :      (the become_leader message makes it from replay->pack->execle->poh)
     177             :      before it just makes it from replay->poh.  This is rare but
     178             :      violates invariants in poh, so we simply do not process any
     179             :      transactions for mixin until we have learned what the leader bank
     180             :      is.  become_leader always precedes the reset on the replay link, so
     181             :      these holds always drain. */
     182           0 :   if( FD_UNLIKELY( ( ctx->in_kind[ in_idx ]==IN_KIND_EXECLE || ctx->in_kind[ in_idx ]==IN_KIND_PACK ) && !fd_poh_have_leader_bank( ctx->poh ) ) ) return 1;
     183             : 
     184           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY && fd_poh_have_leader_bank( ctx->poh ) ) ) return 1;
     185             :   /* If prior leaders skipped, it might happen that replay tells us to
     186             :      become leader, but poh is still hashing through the skipped slots
     187             :      and could not yet mixin any microblocks.  In this case, we hold
     188             :      the microblocks and do not mixin them yet until we have hashed
     189             :      through to the actual leader slot.
     190             : 
     191             :      It might actually be allowed by the protocol to mixin earlier, but
     192             :      that really doesn't seem like a good idea.
     193             : 
     194             :      It's fine to block pack/execles on hashing here, because they we
     195             :      are going to have the wait for the full block to timeout once it
     196             :      starts. */
     197           0 :   if( FD_UNLIKELY( ( ctx->in_kind[ in_idx ]==IN_KIND_EXECLE || ctx->in_kind[ in_idx ]==IN_KIND_PACK ) && fd_poh_hashing_to_leader_slot( ctx->poh ) ) ) return 1;
     198             :   /* If prior leaders skipped, it might happen that replay tells us to
     199             :      become leader, but we haven't published the skipped ticks yet.
     200             : 
     201             :      Skipped ticks need to be published before any microblocks, so we
     202             :      hold the microblocks and do not mixin them yet until we have
     203             :      published any skipped ticks.
     204             : 
     205             :      It's fine to block pack/execles here, because the skipped ticks
     206             :      will be published in the immediate after_credit iterations. */
     207           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EXECLE && fd_poh_must_publish_skipped_tick( ctx->poh ) ) ) return 1;
     208           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EXECLE || ctx->in_kind[ in_idx ]==IN_KIND_PACK ) ) {
     209           0 :     uint pack_idx = (uint)fd_disco_execle_sig_pack_idx( sig );
     210           0 :     if( FD_UNLIKELY( ((int)(pack_idx-ctx->expect_pack_idx))<0L ) ) FD_LOG_ERR(( "received out of order pack_idx %u (expecting %u)", pack_idx, ctx->expect_pack_idx ));
     211           0 :     if( FD_UNLIKELY( pack_idx!=ctx->expect_pack_idx ) ) return 1;
     212           0 :     ctx->expect_pack_idx++;
     213           0 :   }
     214             : 
     215           0 :   switch( ctx->in_kind[ in_idx ] ) {
     216           0 :     case IN_KIND_PACK: {
     217           0 :       fd_done_packing_t const * done_packing = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     218           0 :       fd_poh_done_packing( ctx->poh, stem, done_packing );
     219           0 :       break;
     220           0 :     }
     221           0 :     case IN_KIND_REPLAY: {
     222           0 :       if( FD_LIKELY( sig==REPLAY_SIG_BECAME_LEADER ) ) {
     223           0 :         fd_became_leader_t const * became_leader = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     224           0 :         fd_poh_begin_leader( ctx->poh, became_leader->slot, became_leader->hashcnt_per_tick, became_leader->ticks_per_slot, became_leader->tick_duration_ns, became_leader->max_microblocks_in_slot, became_leader->slot_start_ns );
     225           0 :       } else if( sig==REPLAY_SIG_RESET ) {
     226           0 :         fd_poh_reset_t const * reset = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     227           0 :         fd_poh_reset( ctx->poh, stem, reset->timestamp, reset->hashcnt_per_tick, reset->ticks_per_slot, reset->tick_duration_ns, reset->completed_slot, reset->completed_blockhash, reset->next_leader_slot, reset->max_microblocks_in_slot, reset->completed_cmr );
     228           0 :         ctx->poh->wfs_paused = reset->wfs_paused;
     229           0 :       }
     230           0 :       break;
     231           0 :     }
     232           0 :     case IN_KIND_EXECLE: {
     233           0 :       ulong target_slot = fd_disco_execle_sig_slot( sig );
     234           0 :       FD_TEST( sz>=sizeof(fd_microblock_trailer_t) && (sz-sizeof(fd_microblock_trailer_t))%sizeof(fd_txn_p_t)==0UL );
     235           0 :       ulong txn_cnt = (sz-sizeof(fd_microblock_trailer_t))/sizeof(fd_txn_p_t);
     236           0 :       fd_txn_p_t const * txns = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     237           0 :       fd_microblock_trailer_t const * trailer = fd_type_pun_const( (uchar const*)txns+sz-sizeof(fd_microblock_trailer_t) );
     238             : 
     239           0 :       fd_leader_txn_timing_rec_t timing = {
     240           0 :         .dispatched_ticks = trailer->exec_start_ticks,
     241           0 :         .replayed_ticks   = trailer->exec_end_ticks,
     242           0 :       };
     243           0 :       fd_poh1_mixin( ctx->poh, stem, target_slot, trailer->hash, txn_cnt, txns, &timing );
     244           0 :       break;
     245           0 :     }
     246           0 :     default: {
     247           0 :       FD_LOG_ERR(( "unexpected input kind %d", ctx->in_kind[ in_idx ] ));
     248           0 :       break;
     249           0 :     }
     250           0 :   }
     251             : 
     252           0 :   ctx->idle_cnt = 0UL;
     253           0 :   return 0;
     254           0 : }
     255             : 
     256             : static inline fd_poh_out_t
     257             : out1( fd_topo_t const *      topo,
     258             :       fd_topo_tile_t const * tile,
     259           0 :       char const *           name ) {
     260           0 :   ulong idx = ULONG_MAX;
     261             : 
     262           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
     263           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
     264           0 :     if( !strcmp( link->name, name ) ) {
     265           0 :       if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
     266           0 :       idx = i;
     267           0 :     }
     268           0 :   }
     269             : 
     270           0 :   if( FD_UNLIKELY( idx==ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had no output link named %s", tile->name, tile->kind_id, name ));
     271             : 
     272           0 :   void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
     273           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
     274           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
     275             : 
     276           0 :   return (fd_poh_out_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
     277           0 : }
     278             : 
     279             : static void
     280             : unprivileged_init( fd_topo_t const *      topo,
     281           0 :                    fd_topo_tile_t const * tile ) {
     282           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     283             : 
     284           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     285           0 :   fd_poh_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_poh_tile_t ), sizeof( fd_poh_tile_t ) );
     286             : 
     287           0 :   ctx->expect_pack_idx = 0UL;
     288             : 
     289           0 :   ctx->in_cnt   = tile->in_cnt;
     290           0 :   ctx->idle_cnt = 0UL;
     291             : 
     292           0 :   FD_CHECK_ERR( tile->in_cnt<=sizeof(ctx->in)/sizeof(ctx->in[0]), "too many input links" );
     293             : 
     294           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     295           0 :     fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
     296           0 :     fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     297             : 
     298           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     299           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     300           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     301           0 :     ctx->in[ i ].mtu    = link->mtu;
     302             : 
     303           0 :     if(      !strcmp( link->name, "replay_out" ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
     304           0 :     else if( !strcmp( link->name, "pack_poh"   ) ) ctx->in_kind[ i ] = IN_KIND_PACK;
     305           0 :     else if( !strcmp( link->name, "execle_poh" ) ) ctx->in_kind[ i ] = IN_KIND_EXECLE;
     306           0 :     else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
     307           0 :   }
     308             : 
     309           0 :   *ctx->shred_out = out1( topo, tile, "poh_shred" );
     310           0 :   *ctx->replay_out = out1( topo, tile, "poh_replay" );
     311             : 
     312           0 :   void * timing_tables = NULL;
     313           0 :   ulong ldr_tt_obj_id = fd_pod_query_ulong( topo->props, "ldr_tt", ULONG_MAX );
     314           0 :   if( FD_LIKELY( ldr_tt_obj_id!=ULONG_MAX ) ) timing_tables = fd_topo_obj_laddr( topo, ldr_tt_obj_id );
     315             : 
     316           0 :   FD_TEST( fd_poh_join( fd_poh_new( ctx->poh ), ctx->shred_out, ctx->replay_out, timing_tables, tile->poh.max_txn_per_slot ) );
     317             : 
     318           0 :   fd_clock_tile_init( ctx->poh->clock );
     319             : 
     320           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     321           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     322           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     323             : 
     324           0 :   fd_startup_gate_init( ctx->startup_gate, topo, tile->in_cnt );
     325           0 : }
     326             : 
     327             : static ulong
     328             : populate_allowed_seccomp( fd_topo_t const *      topo,
     329             :                           fd_topo_tile_t const * tile,
     330             :                           ulong                  out_cnt,
     331           0 :                           struct sock_filter *   out ) {
     332           0 :   (void)topo;
     333           0 :   (void)tile;
     334             : 
     335           0 :   populate_sock_filter_policy_fd_poh_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     336           0 :   return sock_filter_policy_fd_poh_tile_instr_cnt;
     337           0 : }
     338             : 
     339             : static ulong
     340             : populate_allowed_fds( fd_topo_t const *      topo,
     341             :                       fd_topo_tile_t const * tile,
     342             :                       ulong                  out_fds_cnt,
     343           0 :                       int *                  out_fds ) {
     344           0 :   (void)topo;
     345           0 :   (void)tile;
     346             : 
     347           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     348             : 
     349           0 :   ulong out_cnt = 0UL;
     350           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     351           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     352           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     353           0 :   return out_cnt;
     354           0 : }
     355             : 
     356             : /* One tick, one microblock */
     357           0 : #define STEM_BURST (2UL)
     358             : 
     359             : /* See explanation in fd_pack */
     360           0 : #define STEM_LAZY  (128L*3000L)
     361             : 
     362           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_poh_tile_t
     363           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_poh_tile_t)
     364             : 
     365           0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
     366           0 : #define STEM_CALLBACK_AFTER_CREDIT        after_credit
     367           0 : #define STEM_CALLBACK_BEFORE_FRAG         before_frag
     368           0 : #define STEM_CALLBACK_RETURNABLE_FRAG     returnable_frag
     369             : 
     370             : #include "../../disco/stem/fd_stem.c"
     371             : 
     372             : fd_topo_run_tile_t fd_tile_poh = {
     373             :   .name                     = "poh",
     374             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     375             :   .populate_allowed_fds     = populate_allowed_fds,
     376             :   .scratch_align            = scratch_align,
     377             :   .scratch_footprint        = scratch_footprint,
     378             :   .privileged_init          = NULL,
     379             :   .unprivileged_init        = unprivileged_init,
     380             :   .run                      = stem_run,
     381             : };

Generated by: LCOV version 1.14