LCOV - code coverage report
Current view: top level - discof/poh - fd_poh_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 135 0.0 %
Date: 2025-10-13 04:42:14 Functions: 0 8 0.0 %

          Line data    Source code
       1             : #include "fd_poh.h"
       2             : #include "generated/fd_poh_tile_seccomp.h"
       3             : #include "fd_poh_tile.h"
       4             : #include "../replay/fd_replay_tile.h"
       5             : #include "../../disco/tiles.h"
       6             : 
       7           0 : #define IN_KIND_REPLAY (0)
       8           0 : #define IN_KIND_PACK   (1)
       9           0 : #define IN_KIND_BANK   (2)
      10             : 
      11             : struct fd_poh_in {
      12             :   fd_wksp_t * mem;
      13             :   ulong       chunk0;
      14             :   ulong       wmark;
      15             :   ulong       mtu;
      16             : };
      17             : 
      18             : typedef struct fd_poh_in fd_poh_in_t;
      19             : 
      20             : struct fd_poh_tile {
      21             :   fd_poh_t poh[1];
      22             : 
      23             :   /* There's a race condition ... let's say two banks A and B, bank A
      24             :      processes some transactions, then releases the account locks, and
      25             :      sends the microblock to PoH to be stamped.  Pack now re-packs the
      26             :      same accounts with a new microblock, sends to bank B, bank B
      27             :      executes and sends the microblock to PoH, and this all happens fast
      28             :      enough that PoH picks the 2nd block to stamp before the 1st.  The
      29             :      accounts database changes now are misordered with respect to PoH so
      30             :      replay could fail.
      31             : 
      32             :      To prevent this race, we order all microblocks and only process
      33             :      them in PoH in the order they are produced by pack.  This is a
      34             :      little bit over-strict, we just need to ensure that microblocks
      35             :      with conflicting accounts execute in order, but this is easiest to
      36             :      implement for now. */
      37             :   uint expect_pack_idx;
      38             : 
      39             :   ulong in_cnt;
      40             :   ulong idle_cnt;
      41             : 
      42             :   int in_kind[ 64 ];
      43             :   fd_poh_in_t in[ 64 ];
      44             : 
      45             :   fd_poh_out_t shred_out[ 1 ];
      46             :   fd_poh_out_t replay_out[ 1 ];
      47             : };
      48             : 
      49             : typedef struct fd_poh_tile fd_poh_tile_t;
      50             : 
      51             : FD_FN_CONST static inline ulong
      52           0 : scratch_align( void ) {
      53           0 :   return 128UL;
      54           0 : }
      55             : 
      56             : FD_FN_PURE static inline ulong
      57           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      58           0 :   (void)tile;
      59           0 :   ulong l = FD_LAYOUT_INIT;
      60           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_poh_tile_t), sizeof(fd_poh_tile_t) );
      61           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      62           0 : }
      63             : 
      64             : static inline void
      65             : after_credit( fd_poh_tile_t *     ctx,
      66             :               fd_stem_context_t * stem,
      67             :               int *               opt_poll_in,
      68           0 :               int *               charge_busy ) {
      69           0 :   ctx->idle_cnt++;
      70           0 :   if( FD_UNLIKELY( ctx->idle_cnt>=2UL*ctx->in_cnt ) ) {
      71             :     /* We would like to fully drain input links to the best of our
      72             :        knowledge, before we spend cycles on hashing.  That is, we would
      73             :        like to assert that all input links have stayed empty since the
      74             :        last time we polled.  Given an arbitrary input link L, the worst
      75             :        case is when L is at idx 0 in the input link shuffle the last
      76             :        time we polled a frag from it, but then link L ends up at idx
      77             :        in_cnt-1 in the subsequent input link shuffle.  So strictly
      78             :        speaking we will need to have observed 2*in_cnt-1 consecutive
      79             :        empty in links to be able to assert that link L has been empty
      80             :        since the last time we polled it. */
      81           0 :     fd_poh_advance( ctx->poh, stem, opt_poll_in, charge_busy );
      82           0 :     ctx->idle_cnt = 0UL;
      83           0 :   }
      84           0 : }
      85             : 
      86             : /* ....
      87             : 
      88             :     1. replay -> (pack, poh) ... start packing for slot
      89             :     2. if slot in progress -> pack -> poh (abandon_packing) for old slot
      90             :     3. pack free to start packing
      91             :     4. if poh slot in progress, refuse replay frag ... until see abandon_packing
      92             :     5. poh must process pack frags in order
      93             :     6. when poh sees done_packing/abandon_packing, return poh -> replay saying bank unused now */
      94             : 
      95             : static inline int
      96             : returnable_frag( fd_poh_tile_t *     ctx,
      97             :                  ulong               in_idx,
      98             :                  ulong               seq,
      99             :                  ulong               sig,
     100             :                  ulong               chunk,
     101             :                  ulong               sz,
     102             :                  ulong               ctl,
     103             :                  ulong               tsorig,
     104             :                  ulong               tspub,
     105           0 :                  fd_stem_context_t * stem ) {
     106           0 :   (void)seq;
     107           0 :   (void)ctl;
     108           0 :   (void)tsorig;
     109           0 :   (void)tspub;
     110             : 
     111             :   /* TODO: Pack has a workaround for Frankendancer that sequences bank
     112             :      release to manage lifetimes, but it's not needed in Firedancer so
     113             :      we just drop it.  We shouldn't send it at all in future. */
     114           0 :   if( FD_UNLIKELY( sig==ULONG_MAX && ctx->in_kind[ in_idx ]==IN_KIND_PACK ) ) {
     115           0 :     ctx->idle_cnt = 0UL;
     116           0 :     return 0;
     117           0 :   }
     118             : 
     119           0 :   if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
     120           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 ));
     121             : 
     122           0 :   if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_REPLAY && fd_poh_have_leader_bank( ctx->poh ) ) ) return 1;
     123           0 :   if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_BANK || ctx->in_kind[ in_idx ]==IN_KIND_PACK ) ) {
     124           0 :     uint pack_idx = (uint)fd_disco_bank_sig_pack_idx( sig );
     125           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 ));
     126           0 :     if( FD_UNLIKELY( pack_idx!=ctx->expect_pack_idx ) ) return 1;
     127           0 :     ctx->expect_pack_idx++;
     128           0 :   }
     129             : 
     130           0 :   switch( ctx->in_kind[ in_idx ] ) {
     131           0 :     case IN_KIND_PACK: {
     132           0 :       fd_done_packing_t const * done_packing = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     133           0 :       fd_poh_done_packing( ctx->poh, done_packing->microblocks_in_slot );
     134           0 :       break;
     135           0 :     }
     136           0 :     case IN_KIND_REPLAY: {
     137           0 :       if( FD_LIKELY( sig==REPLAY_SIG_BECAME_LEADER ) ) {
     138           0 :         fd_became_leader_t const * became_leader = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     139           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 );
     140           0 :       } else if( sig==REPLAY_SIG_RESET ) {
     141           0 :         fd_poh_reset_t const * reset = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     142           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_block_id );
     143           0 :       }
     144           0 :       break;
     145           0 :     }
     146           0 :     case IN_KIND_BANK: {
     147           0 :       ulong target_slot = fd_disco_bank_sig_slot( sig );
     148           0 :       if( FD_UNLIKELY( fd_poh_hashing_to_leader_slot( ctx->poh ) ) ) {
     149             :         /* If we are skipping to a leader slot, we can't process bank
     150             :            microblocks until we are actually ready to mix them in, which
     151             :            is when we have hashed through the slots being skipped. */
     152           0 :         return 1;
     153           0 :       }
     154             : 
     155           0 :       ulong txn_cnt = (sz-sizeof(fd_microblock_trailer_t))/sizeof(fd_txn_p_t);
     156           0 :       fd_txn_p_t const * txns = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
     157           0 :       fd_microblock_trailer_t const * trailer = fd_type_pun_const( (uchar const*)txns+sz-sizeof(fd_microblock_trailer_t) );
     158           0 :       fd_poh1_mixin( ctx->poh, stem, target_slot, trailer->hash, txn_cnt, txns );
     159           0 :       break;
     160           0 :     }
     161           0 :     default: {
     162           0 :       FD_LOG_ERR(( "unexpected input kind %d", ctx->in_kind[ in_idx ] ));
     163           0 :       break;
     164           0 :     }
     165           0 :   }
     166             : 
     167           0 :   ctx->idle_cnt = 0UL;
     168           0 :   return 0;
     169           0 : }
     170             : 
     171             : static inline fd_poh_out_t
     172             : out1( fd_topo_t const *      topo,
     173             :       fd_topo_tile_t const * tile,
     174           0 :       char const *           name ) {
     175           0 :   ulong idx = ULONG_MAX;
     176             : 
     177           0 :   for( ulong i=0UL; i<tile->out_cnt; i++ ) {
     178           0 :     fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
     179           0 :     if( !strcmp( link->name, name ) ) {
     180           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 ));
     181           0 :       idx = i;
     182           0 :     }
     183           0 :   }
     184             : 
     185           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 ));
     186             : 
     187           0 :   void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
     188           0 :   ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
     189           0 :   ulong wmark  = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
     190             : 
     191           0 :   return (fd_poh_out_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
     192           0 : }
     193             : 
     194             : static void
     195             : unprivileged_init( fd_topo_t *      topo,
     196           0 :                    fd_topo_tile_t * tile ) {
     197           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     198             : 
     199           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     200           0 :   fd_poh_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_poh_tile_t ), sizeof( fd_poh_tile_t ) );
     201             : 
     202           0 :   ctx->expect_pack_idx = 0UL;
     203             : 
     204           0 :   ctx->in_cnt   = tile->in_cnt;
     205           0 :   ctx->idle_cnt = 0UL;
     206             : 
     207           0 :   for( ulong i=0UL; i<tile->in_cnt; i++ ) {
     208           0 :     fd_topo_link_t * link = &topo->links[ tile->in_link_id[ i ] ];
     209           0 :     fd_topo_wksp_t * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
     210             : 
     211           0 :     ctx->in[ i ].mem    = link_wksp->wksp;
     212           0 :     ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
     213           0 :     ctx->in[ i ].wmark  = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
     214           0 :     ctx->in[ i ].mtu    = link->mtu;
     215             : 
     216           0 :     if(      !strcmp( link->name, "replay_out" ) ) ctx->in_kind[ i ] = IN_KIND_REPLAY;
     217           0 :     else if( !strcmp( link->name, "pack_poh"   ) ) ctx->in_kind[ i ] = IN_KIND_PACK;
     218           0 :     else if( !strcmp( link->name, "bank_poh"   ) ) ctx->in_kind[ i ] = IN_KIND_BANK;
     219           0 :     else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
     220           0 :   }
     221             : 
     222           0 :   *ctx->shred_out = out1( topo, tile, "poh_shred" );
     223           0 :   *ctx->replay_out = out1( topo, tile, "poh_replay" );
     224             : 
     225           0 :   FD_TEST( fd_poh_join( fd_poh_new( ctx->poh ), ctx->shred_out, ctx->replay_out ) );
     226             : 
     227           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     228           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     229           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     230           0 : }
     231             : 
     232             : static ulong
     233             : populate_allowed_seccomp( fd_topo_t const *      topo,
     234             :                           fd_topo_tile_t const * tile,
     235             :                           ulong                  out_cnt,
     236           0 :                           struct sock_filter *   out ) {
     237           0 :   (void)topo;
     238           0 :   (void)tile;
     239             : 
     240           0 :   populate_sock_filter_policy_fd_poh_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     241           0 :   return sock_filter_policy_fd_poh_tile_instr_cnt;
     242           0 : }
     243             : 
     244             : static ulong
     245             : populate_allowed_fds( fd_topo_t const *      topo,
     246             :                       fd_topo_tile_t const * tile,
     247             :                       ulong                  out_fds_cnt,
     248           0 :                       int *                  out_fds ) {
     249           0 :   (void)topo;
     250           0 :   (void)tile;
     251             : 
     252           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     253             : 
     254           0 :   ulong out_cnt = 0UL;
     255           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     256           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     257           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     258           0 :   return out_cnt;
     259           0 : }
     260             : 
     261             : /* One tick, one microblock, one slot ended */
     262           0 : #define STEM_BURST (3UL)
     263             : 
     264             : /* See explanation in fd_pack */
     265           0 : #define STEM_LAZY  (128L*3000L)
     266             : 
     267           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_poh_tile_t
     268           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_poh_tile_t)
     269             : 
     270           0 : #define STEM_CALLBACK_AFTER_CREDIT    after_credit
     271           0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
     272             : 
     273             : #include "../../disco/stem/fd_stem.c"
     274             : 
     275             : fd_topo_run_tile_t fd_tile_poh = {
     276             :   .name                     = "poh",
     277             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     278             :   .populate_allowed_fds     = populate_allowed_fds,
     279             :   .scratch_align            = scratch_align,
     280             :   .scratch_footprint        = scratch_footprint,
     281             :   .privileged_init          = NULL,
     282             :   .unprivileged_init        = unprivileged_init,
     283             :   .run                      = stem_run,
     284             : };

Generated by: LCOV version 1.14