LCOV - code coverage report
Current view: top level - discoh/guih - fd_guih.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 1873 0.0 %
Date: 2026-07-13 05:28:40 Functions: 0 47 0.0 %

          Line data    Source code
       1             : #include "fd_guih.h"
       2             : #include "fd_guih_printf.h"
       3             : #include "fd_guih_metrics.h"
       4             : 
       5             : #include "../../disco/metrics/fd_metrics.h"
       6             : #include "../../discof/gossip/fd_gossip_tile.h"
       7             : #include "../../discoh/plugin/fd_plugin.h"
       8             : #include "../../disco/bundle/fd_bundle_tile.h"
       9             : 
      10             : #include "../../ballet/base58/fd_base58.h"
      11             : #include "../../third_party/cjson/cJSON.h"
      12             : #include "../../disco/genesis/fd_genesis_cluster.h"
      13             : #include "../../disco/pack/fd_pack.h"
      14             : #include "../../disco/pack/fd_pack_cost.h"
      15             : 
      16             : #include <stdio.h>
      17             : 
      18             : FD_FN_CONST ulong
      19           0 : fd_guih_align( void ) {
      20           0 :   return 128UL;
      21           0 : }
      22             : 
      23             : ulong
      24           0 : fd_guih_footprint( ulong tile_cnt ) {
      25           0 :   FD_TEST( tile_cnt && tile_cnt <=FD_TOPO_MAX_TILES );
      26             : 
      27           0 :   ulong l = FD_LAYOUT_INIT;
      28           0 :   l = FD_LAYOUT_APPEND( l, fd_guih_align(),                sizeof(fd_guih_t) );
      29           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_guih_tile_timers_t), FD_GUIH_TILE_TIMER_SNAP_CNT * tile_cnt * sizeof(fd_guih_tile_timers_t) );
      30           0 :   l = FD_LAYOUT_APPEND( l, alignof(fd_guih_tile_timers_t), FD_GUIH_LEADER_CNT * FD_GUIH_TILE_TIMER_LEADER_DOWNSAMPLE_CNT * tile_cnt * sizeof(fd_guih_tile_timers_t) );
      31           0 :   l = FD_LAYOUT_APPEND( l, fd_guih_rate_deque_align(),     fd_guih_rate_deque_footprint() ); /* ingress_maxq */
      32           0 :   l = FD_LAYOUT_APPEND( l, fd_guih_rate_deque_align(),     fd_guih_rate_deque_footprint() ); /* egress_maxq  */
      33           0 :   return FD_LAYOUT_FINI( l, fd_guih_align() );
      34           0 : }
      35             : 
      36             : void *
      37             : fd_guih_new( void *                shmem,
      38             :             fd_http_server_t *    http,
      39             :             char const *          version,
      40             :             char const *          cluster,
      41             :             uchar const *         identity_key,
      42             :             int                   has_vote_key,
      43             :             uchar const *         vote_key,
      44             :             int                   is_full_client,
      45             :             int                   snapshots_enabled FD_PARAM_UNUSED,
      46             :             int                   is_voting,
      47             :             int                   schedule_strategy,
      48             :             char const *          wfs_expected_bank_hash_cstr FD_PARAM_UNUSED,
      49             :             ushort                expected_shred_version,
      50             :             fd_topo_t const *     topo,
      51           0 :             long                  now ) {
      52             : 
      53           0 :   if( FD_UNLIKELY( !shmem ) ) {
      54           0 :     FD_LOG_WARNING(( "NULL shmem" ));
      55           0 :     return NULL;
      56           0 :   }
      57             : 
      58           0 :   if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_guih_align() ) ) ) {
      59           0 :     FD_LOG_WARNING(( "misaligned shmem" ));
      60           0 :     return NULL;
      61           0 :   }
      62             : 
      63           0 :   if( FD_UNLIKELY( topo->tile_cnt>FD_TOPO_MAX_TILES ) ) {
      64           0 :     FD_LOG_WARNING(( "too many tiles" ));
      65           0 :     return NULL;
      66           0 :   }
      67             : 
      68           0 :   ulong tile_cnt = topo->tile_cnt;
      69             : 
      70           0 :   FD_SCRATCH_ALLOC_INIT( l, shmem );
      71           0 :   fd_guih_t *             gui                  = FD_SCRATCH_ALLOC_APPEND( l, fd_guih_align(),                sizeof(fd_guih_t) );
      72           0 :   fd_guih_tile_timers_t * tile_timers_snap_mem = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_guih_tile_timers_t), FD_GUIH_TILE_TIMER_SNAP_CNT * tile_cnt * sizeof(fd_guih_tile_timers_t) );
      73           0 :   fd_guih_tile_timers_t * leader_tt_mem        = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_guih_tile_timers_t), FD_GUIH_LEADER_CNT * FD_GUIH_TILE_TIMER_LEADER_DOWNSAMPLE_CNT * tile_cnt * sizeof(fd_guih_tile_timers_t) );
      74           0 :   void *                 ingress_maxq_mem     = FD_SCRATCH_ALLOC_APPEND( l, fd_guih_rate_deque_align(),     fd_guih_rate_deque_footprint() );
      75           0 :   void *                 egress_maxq_mem      = FD_SCRATCH_ALLOC_APPEND( l, fd_guih_rate_deque_align(),     fd_guih_rate_deque_footprint() );
      76             : 
      77           0 :   gui->http     = http;
      78           0 :   gui->topo     = topo;
      79           0 :   gui->tile_cnt = tile_cnt;
      80             : 
      81           0 :   gui->summary.tile_timers_snap = tile_timers_snap_mem;
      82           0 :   gui->summary.ingress_maxq     = fd_guih_rate_deque_join( fd_guih_rate_deque_new( ingress_maxq_mem ) );
      83           0 :   gui->summary.egress_maxq      = fd_guih_rate_deque_join( fd_guih_rate_deque_new( egress_maxq_mem  ) );
      84             : 
      85           0 :   gui->summary.network_stats_has_prev = 0;
      86           0 :   gui->summary.net_rate_ema_ready     = 0;
      87           0 :   gui->summary.net_rate_prev_ts       = 0L;
      88           0 :   fd_memset( gui->summary.ingress_ema, 0, sizeof(gui->summary.ingress_ema) );
      89           0 :   fd_memset( gui->summary.egress_ema,  0, sizeof(gui->summary.egress_ema)  );
      90             : 
      91           0 :   for( ulong i=0UL; i<FD_GUIH_LEADER_CNT; i++ ) gui->leader_slots[ i ]->tile_timers = leader_tt_mem + i * FD_GUIH_TILE_TIMER_LEADER_DOWNSAMPLE_CNT * tile_cnt;
      92             : 
      93           0 :   gui->leader_slot = ULONG_MAX;
      94           0 :   gui->summary.schedule_strategy = schedule_strategy;
      95             : 
      96             : 
      97           0 :   gui->next_sample_400millis = now;
      98           0 :   gui->next_sample_100millis = now;
      99           0 :   gui->next_sample_50millis  = now;
     100           0 :   gui->next_sample_25millis  = now;
     101           0 :   gui->next_sample_10millis  = now;
     102             : 
     103           0 :   memcpy( gui->summary.identity_key->uc, identity_key, 32UL );
     104           0 :   fd_base58_encode_32( identity_key, NULL, gui->summary.identity_key_base58 );
     105           0 :   gui->summary.identity_key_base58[ FD_BASE58_ENCODED_32_SZ-1UL ] = '\0';
     106             : 
     107           0 :   if( FD_LIKELY( has_vote_key ) ) {
     108           0 :     gui->summary.has_vote_key = 1;
     109           0 :     memcpy( gui->summary.vote_key->uc, vote_key, 32UL );
     110           0 :     fd_base58_encode_32( vote_key, NULL, gui->summary.vote_key_base58 );
     111           0 :     gui->summary.vote_key_base58[ FD_BASE58_ENCODED_32_SZ-1UL ] = '\0';
     112           0 :   } else {
     113           0 :     gui->summary.has_vote_key = 0;
     114           0 :     memset( gui->summary.vote_key_base58, 0, sizeof(gui->summary.vote_key_base58) );
     115           0 :   }
     116             : 
     117           0 :   gui->summary.is_full_client                = is_full_client;
     118           0 :   gui->summary.version                       = version;
     119           0 :   gui->summary.cluster                       = cluster;
     120           0 :   gui->summary.startup_time_nanos            = gui->next_sample_400millis;
     121           0 :   gui->summary.expected_shred_version        = expected_shred_version;
     122           0 :   gui->summary.wfs_enabled          = 0;
     123           0 :   gui->summary.wfs_bank_hash[ 0UL ] = '\0';
     124             : 
     125           0 :   gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_INITIALIZING;
     126           0 :   gui->summary.startup_progress.startup_got_full_snapshot              = 0;
     127           0 :   gui->summary.startup_progress.startup_full_snapshot_slot             = 0;
     128           0 :   gui->summary.startup_progress.startup_incremental_snapshot_slot      = 0;
     129           0 :   gui->summary.startup_progress.startup_waiting_for_supermajority_slot = ULONG_MAX;
     130           0 :   gui->summary.startup_progress.startup_ledger_max_slot                = ULONG_MAX;
     131             : 
     132           0 :   gui->summary.identity_account_balance      = 0UL;
     133           0 :   gui->summary.vote_account_balance          = 0UL;
     134           0 :   gui->summary.estimated_slot_duration_nanos = 0UL;
     135             : 
     136           0 :   gui->summary.vote_distance = 0UL;
     137           0 :   gui->summary.vote_state = is_voting ? FD_GUIH_VOTE_STATE_VOTING : FD_GUIH_VOTE_STATE_NON_VOTING;
     138             : 
     139           0 :   gui->summary.sock_tile_cnt   = fd_topo_tile_name_cnt( gui->topo, "sock"   );
     140           0 :   gui->summary.net_tile_cnt    = fd_topo_tile_name_cnt( gui->topo, "net"    );
     141           0 :   gui->summary.quic_tile_cnt   = fd_topo_tile_name_cnt( gui->topo, "quic"   );
     142           0 :   gui->summary.verify_tile_cnt = fd_topo_tile_name_cnt( gui->topo, "verify" );
     143           0 :   gui->summary.resolh_tile_cnt = fd_topo_tile_name_cnt( gui->topo, "resolh" );
     144           0 :   gui->summary.resolv_tile_cnt = fd_topo_tile_name_cnt( gui->topo, "resolv" );
     145           0 :   gui->summary.bank_tile_cnt   = fd_topo_tile_name_cnt( gui->topo, "bank"   );
     146           0 :   gui->summary.execle_tile_cnt = fd_topo_tile_name_cnt( gui->topo, "execle" );
     147           0 :   gui->summary.execrp_tile_cnt = fd_topo_tile_name_cnt( gui->topo, "execrp" );
     148           0 :   gui->summary.shred_tile_cnt  = fd_topo_tile_name_cnt( gui->topo, "shred"  );
     149             : 
     150           0 :   gui->summary.slot_rooted                   = ULONG_MAX;
     151           0 :   gui->summary.slot_optimistically_confirmed = ULONG_MAX;
     152           0 :   gui->summary.slot_completed                = ULONG_MAX;
     153           0 :   gui->summary.slot_estimated                = ULONG_MAX;
     154           0 :   gui->summary.slot_caught_up                = ULONG_MAX;
     155           0 :   gui->summary.slot_repair                   = ULONG_MAX;
     156           0 :   gui->summary.slot_turbine                  = ULONG_MAX;
     157           0 :   gui->summary.slot_reset                    = ULONG_MAX;
     158           0 :   gui->summary.slot_storage                  = ULONG_MAX;
     159           0 :   gui->summary.active_fork_cnt               = 1UL;
     160             : 
     161           0 :   for( ulong i=0UL; i < (FD_GUIH_REPAIR_SLOT_HISTORY_SZ+1UL); i++ )  gui->summary.slots_max_repair[ i ].slot  = ULONG_MAX;
     162           0 :   for( ulong i=0UL; i < (FD_GUIH_TURBINE_SLOT_HISTORY_SZ+1UL); i++ ) gui->summary.slots_max_turbine[ i ].slot = ULONG_MAX;
     163             : 
     164           0 :   for( ulong i=0UL; i < FD_GUIH_TURBINE_RECV_TIMESTAMPS; i++ ) gui->turbine_slots[ i ].slot = ULONG_MAX;
     165             : 
     166           0 :   gui->summary.estimated_tps_history_idx = 0UL;
     167           0 :   memset( gui->summary.estimated_tps_history, 0, sizeof(gui->summary.estimated_tps_history) );
     168             : 
     169           0 :   memset( gui->summary.txn_waterfall_reference, 0, sizeof(gui->summary.txn_waterfall_reference) );
     170           0 :   memset( gui->summary.txn_waterfall_current,   0, sizeof(gui->summary.txn_waterfall_current) );
     171             : 
     172           0 :   memset( gui->summary.tile_stats_reference, 0, sizeof(gui->summary.tile_stats_reference) );
     173           0 :   memset( gui->summary.tile_stats_current, 0, sizeof(gui->summary.tile_stats_current) );
     174             : 
     175           0 :   gui->summary.progcache_history_idx = 0UL;
     176           0 :   memset( gui->summary.progcache_hits_history,    0, sizeof(gui->summary.progcache_hits_history) );
     177           0 :   memset( gui->summary.progcache_lookups_history, 0, sizeof(gui->summary.progcache_lookups_history) );
     178           0 :   gui->summary.progcache_hits_1min    = 0UL;
     179           0 :   gui->summary.progcache_lookups_1min = 0UL;
     180             : 
     181           0 :   memset( gui->summary.tile_timers_snap,            0, tile_cnt * sizeof(fd_guih_tile_timers_t) );
     182           0 :   memset( gui->summary.tile_timers_snap + tile_cnt, 0, tile_cnt * sizeof(fd_guih_tile_timers_t) );
     183           0 :   gui->summary.tile_timers_snap_idx    = 2UL;
     184             : 
     185           0 :   memset( gui->summary.scheduler_counts_snap[ 0 ], 0, sizeof(gui->summary.scheduler_counts_snap[ 0 ]) );
     186           0 :   memset( gui->summary.scheduler_counts_snap[ 1 ], 0, sizeof(gui->summary.scheduler_counts_snap[ 1 ]) );
     187           0 :   gui->summary.scheduler_counts_snap_idx    = 2UL;
     188             : 
     189           0 :   for( ulong i=0UL; i<FD_GUIH_SLOTS_CNT;  i++ ) gui->slots[ i ]->slot             = ULONG_MAX;
     190           0 :   for( ulong i=0UL; i<FD_GUIH_LEADER_CNT; i++ ) gui->leader_slots[ i ]->slot      = ULONG_MAX;
     191           0 :   gui->leader_slots_cnt      = 0UL;
     192             : 
     193           0 :   gui->tower_cnt = 0UL;
     194             : 
     195           0 :   gui->block_engine.has_block_engine = 0;
     196             : 
     197           0 :   gui->epoch.has_epoch[ 0 ] = 0;
     198           0 :   gui->epoch.has_epoch[ 1 ] = 0;
     199             : 
     200           0 :   gui->gossip.peer_cnt               = 0UL;
     201           0 :   gui->vote_account.vote_account_cnt = 0UL;
     202           0 :   gui->validator_info.info_cnt       = 0UL;
     203             : 
     204           0 :   gui->pack_txn_idx = 0UL;
     205             : 
     206           0 :   gui->shreds.leader_shred_cnt      = 0UL;
     207           0 :   gui->shreds.staged_next_broadcast = 0UL;
     208           0 :   gui->shreds.staged_head           = 0UL;
     209           0 :   gui->shreds.staged_tail           = 0UL;
     210           0 :   gui->shreds.history_tail          = 0UL;
     211           0 :   gui->shreds.history_slot          = ULONG_MAX;
     212           0 :   gui->summary.catch_up_repair_sz   = 0UL;
     213           0 :   gui->summary.catch_up_turbine_sz  = 0UL;
     214           0 :   gui->summary.late_votes_sz        = 0UL;
     215             : 
     216           0 :   return gui;
     217           0 : }
     218             : 
     219             : fd_guih_t *
     220           0 : fd_guih_join( void * shmem ) {
     221           0 :   return (fd_guih_t *)shmem;
     222           0 : }
     223             : 
     224             : void
     225             : fd_guih_set_identity( fd_guih_t *    gui,
     226           0 :                      uchar const * identity_pubkey ) {
     227           0 :   memcpy( gui->summary.identity_key->uc, identity_pubkey, 32UL );
     228           0 :   fd_base58_encode_32( identity_pubkey, NULL, gui->summary.identity_key_base58 );
     229           0 :   gui->summary.identity_key_base58[ FD_BASE58_ENCODED_32_SZ-1UL ] = '\0';
     230             : 
     231           0 :   fd_guih_printf_identity_key( gui );
     232           0 :   fd_http_server_ws_broadcast( gui->http );
     233           0 : }
     234             : 
     235             : void
     236             : fd_guih_ws_open( fd_guih_t * gui,
     237             :                 ulong      ws_conn_id,
     238           0 :                 long now ) {
     239           0 :   void (* printers[] )( fd_guih_t * gui ) = {
     240           0 :     fd_guih_printf_startup_progress,
     241           0 :     fd_guih_printf_version,
     242           0 :     fd_guih_printf_cluster,
     243           0 :     fd_guih_printf_commit_hash,
     244           0 :     fd_guih_printf_identity_key,
     245           0 :     fd_guih_printf_vote_key,
     246           0 :     fd_guih_printf_startup_time_nanos,
     247           0 :     fd_guih_printf_vote_state,
     248           0 :     fd_guih_printf_vote_distance,
     249           0 :     fd_guih_printf_turbine_slot,
     250           0 :     fd_guih_printf_repair_slot,
     251           0 :     fd_guih_printf_slot_caught_up,
     252           0 :     fd_guih_printf_tps_history,
     253           0 :     fd_guih_printf_tiles,
     254           0 :     fd_guih_printf_schedule_strategy,
     255           0 :     fd_guih_printf_identity_balance,
     256           0 :     fd_guih_printf_vote_balance,
     257           0 :     fd_guih_printf_estimated_slot_duration_nanos,
     258           0 :     fd_guih_printf_root_slot,
     259           0 :     fd_guih_printf_storage_slot,
     260           0 :     fd_guih_printf_reset_slot,
     261           0 :     fd_guih_printf_active_fork_cnt,
     262           0 :     fd_guih_printf_optimistically_confirmed_slot,
     263           0 :     fd_guih_printf_completed_slot,
     264           0 :     fd_guih_printf_estimated_slot,
     265           0 :     fd_guih_printf_live_tile_timers,
     266           0 :     fd_guih_printf_live_tile_metrics,
     267           0 :     fd_guih_printf_catch_up_history,
     268           0 :     fd_guih_printf_vote_latency_history,
     269           0 :     fd_guih_printf_late_votes_history,
     270           0 :     fd_guih_printf_health
     271           0 :   };
     272             : 
     273           0 :   ulong printers_len = sizeof(printers) / sizeof(printers[0]);
     274           0 :   for( ulong i=0UL; i<printers_len; i++ ) {
     275           0 :     printers[ i ]( gui );
     276           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     277           0 :   }
     278             : 
     279           0 :   if( FD_LIKELY( gui->block_engine.has_block_engine ) ) {
     280           0 :     fd_guih_printf_block_engine( gui );
     281           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     282           0 :   }
     283             : 
     284           0 :   for( ulong i=0UL; i<2UL; i++ ) {
     285           0 :     if( FD_LIKELY( gui->epoch.has_epoch[ i ] ) ) {
     286           0 :       fd_guih_printf_skip_rate( gui, i );
     287           0 :       FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     288           0 :       fd_guih_printf_epoch( gui, i );
     289           0 :       FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     290           0 :     }
     291           0 :   }
     292             : 
     293           0 :   ulong epoch_idx = fd_guih_current_epoch_idx( gui );
     294           0 :   if( FD_LIKELY( epoch_idx!=ULONG_MAX ) ) {
     295           0 :     fd_guih_printf_skipped_history( gui, epoch_idx );
     296           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     297           0 :     fd_guih_printf_skipped_history_cluster( gui, epoch_idx );
     298           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     299           0 :   }
     300             : 
     301             :   /* Print peers last because it's the largest message and would
     302             :      block other information. */
     303           0 :   fd_guih_printf_peers_all( gui );
     304           0 :   FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     305             : 
     306             :   /* rebroadcast 10s of historical shred data */
     307           0 :   if( FD_LIKELY( gui->shreds.staged_next_broadcast!=ULONG_MAX ) ) {
     308           0 :     fd_guih_printf_shred_rebroadcast( gui, now-(long)(10*1e9) );
     309           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
     310           0 :   }
     311           0 : }
     312             : 
     313             : static void
     314           0 : fd_guih_tile_timers_snap( fd_guih_t * gui ) {
     315           0 :   fd_guih_tile_timers_t * cur = gui->summary.tile_timers_snap + gui->summary.tile_timers_snap_idx * gui->tile_cnt;
     316           0 :   gui->summary.tile_timers_snap_idx = (gui->summary.tile_timers_snap_idx+1UL)%FD_GUIH_TILE_TIMER_SNAP_CNT;
     317           0 :   for( ulong i=0UL; i<gui->topo->tile_cnt; i++ ) {
     318           0 :     fd_topo_tile_t const * tile = &gui->topo->tiles[ i ];
     319           0 :     if ( FD_UNLIKELY( !tile->metrics ) ) {
     320             :       /* bench tiles might not have been booted initially.
     321             :          This check shouldn't be necessary if all tiles barrier after boot. */
     322             :       // TODO(FIXME) this probably isn't the right fix but it makes fddev bench work for now
     323           0 :       return;
     324           0 :     }
     325           0 :     volatile ulong const * tile_metrics = fd_metrics_tile( tile->metrics );
     326             : 
     327           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_CAUGHT_UP_HOUSEKEEPING_IDX    ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_HOUSEKEEPING )    ];
     328           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_PROCESSING_HOUSEKEEPING_IDX   ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_PROCESSING_HOUSEKEEPING )   ];
     329           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_BACKPRESSURE_HOUSEKEEPING_IDX ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_BACKPRESSURE_HOUSEKEEPING ) ];
     330           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_CAUGHT_UP_PREFRAG_IDX         ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_PREFRAG )         ];
     331           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_PROCESSING_PREFRAG_IDX        ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_PROCESSING_PREFRAG )        ];
     332           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_BACKPRESSURE_PREFRAG_IDX      ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_BACKPRESSURE_PREFRAG )      ];
     333           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_CAUGHT_UP_POSTFRAG_IDX        ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_CAUGHT_UP_POSTFRAG )        ];
     334           0 :     cur[ i ].timers[ FD_METRICS_ENUM_TILE_REGIME_V_PROCESSING_POSTFRAG_IDX       ] = tile_metrics[ MIDX( COUNTER, TILE, REGIME_DURATION_NANOS_PROCESSING_POSTFRAG )       ];
     335             : 
     336           0 :     cur[ i ].sched_timers[ FD_METRICS_ENUM_CPU_REGIME_V_WAIT_IDX   ] = tile_metrics[ MIDX( COUNTER, TILE, CPU_DURATION_NANOS_WAIT )   ];
     337           0 :     cur[ i ].sched_timers[ FD_METRICS_ENUM_CPU_REGIME_V_USER_IDX   ] = tile_metrics[ MIDX( COUNTER, TILE, CPU_DURATION_NANOS_USER )   ];
     338           0 :     cur[ i ].sched_timers[ FD_METRICS_ENUM_CPU_REGIME_V_SYSTEM_IDX ] = tile_metrics[ MIDX( COUNTER, TILE, CPU_DURATION_NANOS_SYSTEM ) ];
     339           0 :     cur[ i ].sched_timers[ FD_METRICS_ENUM_CPU_REGIME_V_IDLE_IDX   ] = tile_metrics[ MIDX( COUNTER, TILE, CPU_DURATION_NANOS_IDLE )   ];
     340             : 
     341           0 :     cur[ i ].in_backp  = (int)tile_metrics[ MIDX(GAUGE, TILE, IN_BACKPRESSURE) ];
     342           0 :     cur[ i ].status    = (uchar)tile_metrics[ MIDX( GAUGE, TILE, STATUS ) ];
     343           0 :     cur[ i ].heartbeat = tile_metrics[ MIDX( GAUGE, TILE, HEARTBEAT_TIMESTAMP_NANOS ) ];
     344           0 :     cur[ i ].backp_cnt = tile_metrics[ MIDX( COUNTER, TILE, BACKPRESSURE ) ];
     345           0 :     cur[ i ].nvcsw     = tile_metrics[ MIDX( COUNTER, TILE, CONTEXT_SWITCH_VOLUNTARY ) ];
     346           0 :     cur[ i ].nivcsw    = tile_metrics[ MIDX( COUNTER, TILE, CONTEXT_SWITCH_INVOLUNTARY ) ];
     347           0 :     cur[ i ].minflt    = tile_metrics[ MIDX( COUNTER, TILE, PAGE_FAULT_MINOR ) ];
     348           0 :     cur[ i ].majflt    = tile_metrics[ MIDX( COUNTER, TILE, PAGE_FAULT_MAJOR ) ];
     349           0 :     cur[ i ].last_cpu  = (ushort)tile_metrics[ MIDX( GAUGE, TILE, LAST_CPU ) ];
     350           0 :     cur[ i ].interrupts     = tile_metrics[ MIDX( COUNTER, TILE, IRQ_PREEMPTED ) ];
     351           0 :     cur[ i ].tlb_shootdowns = tile_metrics[ MIDX( COUNTER, TILE, TLB_SHOOTDOWN ) ];
     352           0 :     cur[ i ].timer_ticks    = tile_metrics[ MIDX( COUNTER, TILE, TIMER_TICK ) ];
     353           0 :   }
     354           0 : }
     355             : 
     356             : static void
     357           0 : fd_guih_scheduler_counts_snap( fd_guih_t * gui, long now ) {
     358           0 :   ulong pack_tile_idx = fd_topo_find_tile( gui->topo, "pack", 0UL );
     359           0 :   if( FD_UNLIKELY( pack_tile_idx==ULONG_MAX ) ) return;
     360             : 
     361           0 :   fd_guih_scheduler_counts_t * cur = gui->summary.scheduler_counts_snap[ gui->summary.scheduler_counts_snap_idx ];
     362           0 :   gui->summary.scheduler_counts_snap_idx = (gui->summary.scheduler_counts_snap_idx+1UL)%FD_GUIH_SCHEDULER_COUNT_SNAP_CNT;
     363             : 
     364           0 :   fd_topo_tile_t const * pack = &gui->topo->tiles[ fd_topo_find_tile( gui->topo, "pack", 0UL ) ];
     365           0 :   volatile ulong const * pack_metrics = fd_metrics_tile( pack->metrics );
     366             : 
     367           0 :   cur->sample_time_ns = now;
     368             : 
     369           0 :   cur->regular     = pack_metrics[ MIDX( GAUGE, PACK, TXN_AVAILABLE_REGULAR ) ];
     370           0 :   cur->votes       = pack_metrics[ MIDX( GAUGE, PACK, TXN_AVAILABLE_VOTES ) ];
     371           0 :   cur->conflicting = pack_metrics[ MIDX( GAUGE, PACK, TXN_AVAILABLE_CONFLICTING ) ];
     372           0 :   cur->bundles     = pack_metrics[ MIDX( GAUGE, PACK, TXN_AVAILABLE_BUNDLES ) ];
     373           0 : }
     374             : 
     375             : static void
     376           0 : fd_guih_estimated_tps_snap( fd_guih_t * gui ) {
     377           0 :   ulong vote_failed     = 0UL;
     378           0 :   ulong vote_success    = 0UL;
     379           0 :   ulong nonvote_success = 0UL;
     380           0 :   ulong nonvote_failed  = 0UL;
     381             : 
     382           0 :   if( FD_LIKELY( gui->summary.slot_completed==ULONG_MAX ) ) return;
     383           0 :   for( ulong i=0UL; i<fd_ulong_min( gui->summary.slot_completed+1UL, FD_GUIH_SLOTS_CNT ); i++ ) {
     384           0 :     ulong _slot = gui->summary.slot_completed-i;
     385           0 :     fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, _slot );
     386           0 :     if( FD_UNLIKELY( !slot ) ) break; /* Slot no longer exists, no TPS. */
     387           0 :     if( FD_UNLIKELY( slot->completed_time==LONG_MAX ) ) continue; /* Slot is on this fork but was never completed, must have been in root path on boot. */
     388           0 :     if( FD_UNLIKELY( slot->completed_time+FD_GUIH_TPS_HISTORY_WINDOW_DURATION_SECONDS*1000L*1000L*1000L<gui->next_sample_400millis ) ) break; /* Slot too old. */
     389           0 :     if( FD_UNLIKELY( slot->skipped ) ) continue; /* Skipped slots don't count to TPS. */
     390           0 :     if( FD_UNLIKELY( slot->vote_failed==UINT_MAX ) ) continue; /* Slot transaction counts not yet populated. */
     391           0 :     vote_failed     += slot->vote_failed;
     392           0 :     vote_success    += slot->vote_success;
     393           0 :     nonvote_success += slot->nonvote_success;
     394           0 :     nonvote_failed  += slot->nonvote_failed;
     395           0 :   }
     396             : 
     397           0 :   gui->summary.estimated_tps_history[ gui->summary.estimated_tps_history_idx ].vote_failed = vote_failed;
     398           0 :   gui->summary.estimated_tps_history[ gui->summary.estimated_tps_history_idx ].vote_success = vote_success;
     399           0 :   gui->summary.estimated_tps_history[ gui->summary.estimated_tps_history_idx ].nonvote_success = nonvote_success;
     400           0 :   gui->summary.estimated_tps_history[ gui->summary.estimated_tps_history_idx ].nonvote_failed = nonvote_failed;
     401           0 :   gui->summary.estimated_tps_history_idx = (gui->summary.estimated_tps_history_idx+1UL) % FD_GUIH_TPS_HISTORY_SAMPLE_CNT;
     402           0 : }
     403             : 
     404             : static void
     405             : fd_guih_network_stats_snap( fd_guih_t *               gui,
     406           0 :                            fd_guih_network_stats_t * cur ) {
     407           0 :   fd_topo_t const * topo = gui->topo;
     408           0 :   ulong gossvf_tile_cnt = fd_topo_tile_name_cnt( topo, "gossvf" );
     409           0 :   ulong gossip_tile_cnt = fd_topo_tile_name_cnt( topo, "gossip" );
     410           0 :   ulong shred_tile_cnt  = fd_topo_tile_name_cnt( topo, "shred" );
     411           0 :   ulong net_tile_cnt    = fd_topo_tile_name_cnt( topo, "net" );
     412           0 :   ulong quic_tile_cnt   = fd_topo_tile_name_cnt( topo, "quic" );
     413             : 
     414           0 :   cur->in.gossip   = fd_guih_metrics_gossip_total_ingress_bytes( topo, gossvf_tile_cnt );
     415           0 :   cur->out.gossip  = fd_guih_metrics_gossip_total_egress_bytes( topo, gossip_tile_cnt );
     416           0 :   cur->in.turbine  = fd_guih_metrics_sum_tiles_counter( topo, "shred", shred_tile_cnt, MIDX( COUNTER, SHRED, SHRED_TURBINE_RX_BYTES ) );
     417             : 
     418           0 :   cur->out.turbine = 0UL;
     419           0 :   cur->out.repair  = 0UL;
     420           0 :   cur->out.rserve  = 0UL; /* rserve is not part of frankendancer */
     421           0 :   cur->out.tpu     = 0UL;
     422           0 :   for( ulong i=0UL; i<net_tile_cnt; i++ ) {
     423           0 :     ulong net_tile_idx = fd_topo_find_tile( topo, "net", i );
     424           0 :     if( FD_UNLIKELY( net_tile_idx==ULONG_MAX ) ) continue;
     425           0 :     fd_topo_tile_t const * net = &topo->tiles[ net_tile_idx ];
     426           0 :     for( ulong j=0UL; j<net->in_cnt; j++ ) {
     427           0 :       if( FD_UNLIKELY( !strcmp( topo->links[ net->in_link_id[ j ] ].name, "shred_net" ) ) ) {
     428           0 :           cur->out.turbine += fd_metrics_link_in( net->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_CONSUMED_BYTES_OFF ];
     429           0 :       }
     430             : 
     431           0 :       if( FD_UNLIKELY( !strcmp( topo->links[ net->in_link_id[ j ] ].name, "repair_net" ) ) ) {
     432           0 :           cur->out.repair += fd_metrics_link_in( net->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_CONSUMED_BYTES_OFF ];
     433           0 :       }
     434             : 
     435           0 :       if( FD_UNLIKELY( !strcmp( topo->links[ net->in_link_id[ j ] ].name, "send_net" ) ) ) {
     436           0 :           cur->out.tpu += fd_metrics_link_in( net->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_CONSUMED_BYTES_OFF ];
     437           0 :       }
     438           0 :     }
     439           0 :   }
     440             : 
     441           0 :   cur->in.repair = fd_guih_metrics_sum_tiles_counter( topo, "shred", shred_tile_cnt, MIDX( COUNTER, SHRED, SHRED_REPAIR_RX_BYTES ) );
     442           0 :   ulong repair_tile_idx = fd_topo_find_tile( topo, "repair", 0UL );
     443           0 :   if( FD_LIKELY( repair_tile_idx!=ULONG_MAX ) ) {
     444           0 :     fd_topo_tile_t const * repair = &topo->tiles[ repair_tile_idx ];
     445             : 
     446           0 :     for( ulong i=0UL; i<repair->in_cnt; i++ ) {
     447           0 :       if( FD_UNLIKELY( !strcmp( topo->links[ repair->in_link_id[ i ] ].name, "net_repair" ) ) ) {
     448           0 :           cur->in.repair += fd_metrics_link_in( repair->metrics, i )[ FD_METRICS_COUNTER_LINK_FRAG_CONSUMED_BYTES_OFF ];
     449           0 :       }
     450           0 :     }
     451           0 :   }
     452             : 
     453           0 :   cur->in.rserve = 0UL; /* rserve is not part of frankendancer */
     454             : 
     455           0 :   cur->in.tpu = 0UL;
     456           0 :   for( ulong i=0UL; i<quic_tile_cnt; i++ ) {
     457           0 :     ulong quic_tile_idx = fd_topo_find_tile( topo, "quic", i );
     458           0 :     if( FD_UNLIKELY( quic_tile_idx==ULONG_MAX ) ) continue;
     459           0 :     fd_topo_tile_t const * quic = &topo->tiles[ quic_tile_idx ];
     460           0 :     volatile ulong * quic_metrics = fd_metrics_tile( quic->metrics );
     461           0 :     cur->in.tpu += quic_metrics[ MIDX( COUNTER, QUIC, PKT_RX_BYTES ) ];
     462           0 :   }
     463             : 
     464           0 :   ulong bundle_tile_idx = fd_topo_find_tile( topo, "bundle", 0UL );
     465           0 :   if( FD_LIKELY( bundle_tile_idx!=ULONG_MAX ) ) {
     466           0 :     fd_topo_tile_t const * bundle = &topo->tiles[ bundle_tile_idx ];
     467           0 :     volatile ulong * bundle_metrics = fd_metrics_tile( bundle->metrics );
     468           0 :     cur->in.tpu += bundle_metrics[ MIDX( COUNTER, BUNDLE, PROTOBUF_RX_BYTES ) ];
     469           0 :   }
     470             : 
     471           0 :   ulong metric_tile_idx = fd_topo_find_tile( topo, "metric", 0UL );
     472           0 :   if( FD_LIKELY( metric_tile_idx!=ULONG_MAX ) ) {
     473           0 :     fd_topo_tile_t const * metric = &topo->tiles[ metric_tile_idx ];
     474           0 :     volatile ulong * metric_metrics = fd_metrics_tile( metric->metrics );
     475           0 :     cur->in.metric  = metric_metrics[ MIDX( COUNTER, METRIC, BYTES_READ ) ];
     476           0 :     cur->out.metric = metric_metrics[ MIDX( COUNTER, METRIC, BYTES_WRITTEN ) ];
     477           0 :   } else {
     478           0 :     cur->in.metric  = 0UL;
     479           0 :     cur->out.metric = 0UL;
     480           0 :   }
     481           0 : }
     482             : 
     483             : static void
     484             : fd_guih_network_rate_max_update( fd_guih_t * gui,
     485           0 :                                 long       now ) {
     486           0 :   fd_guih_network_stats_t * cur  = gui->summary.network_stats_current;
     487           0 :   fd_guih_network_stats_t * prev = gui->summary.network_stats_prev;
     488             : 
     489             :   /* On the first sample we have no previous value. */
     490           0 :   if( FD_UNLIKELY( !gui->summary.network_stats_has_prev ) ) {
     491           0 :     *prev = *cur;
     492           0 :     gui->summary.network_stats_has_prev = 1;
     493           0 :     gui->summary.net_rate_prev_ts       = now;
     494           0 :     return;
     495           0 :   }
     496             : 
     497           0 :   ulong d_in[ FD_GUIH_NET_PROTO_CNT ];
     498           0 :   d_in[ 0 ] = fd_ulong_sat_sub( cur->in.turbine, prev->in.turbine );
     499           0 :   d_in[ 1 ] = fd_ulong_sat_sub( cur->in.gossip,  prev->in.gossip  );
     500           0 :   d_in[ 2 ] = fd_ulong_sat_sub( cur->in.tpu,     prev->in.tpu     );
     501           0 :   d_in[ 3 ] = fd_ulong_sat_sub( cur->in.repair,  prev->in.repair  );
     502           0 :   d_in[ 4 ] = fd_ulong_sat_sub( cur->in.rserve,  prev->in.rserve  );
     503           0 :   d_in[ 5 ] = fd_ulong_sat_sub( cur->in.metric,  prev->in.metric  );
     504             : 
     505           0 :   ulong d_out[ FD_GUIH_NET_PROTO_CNT ];
     506           0 :   d_out[ 0 ] = fd_ulong_sat_sub( cur->out.turbine, prev->out.turbine );
     507           0 :   d_out[ 1 ] = fd_ulong_sat_sub( cur->out.gossip,  prev->out.gossip  );
     508           0 :   d_out[ 2 ] = fd_ulong_sat_sub( cur->out.tpu,     prev->out.tpu     );
     509           0 :   d_out[ 3 ] = fd_ulong_sat_sub( cur->out.repair,  prev->out.repair  );
     510           0 :   d_out[ 4 ] = fd_ulong_sat_sub( cur->out.rserve,  prev->out.rserve  );
     511           0 :   d_out[ 5 ] = fd_ulong_sat_sub( cur->out.metric,  prev->out.metric  );
     512             : 
     513             :   /* Compute per-protocol instantaneous bytes/sec rate and feed the EMA. */
     514           0 :   long dt_ns = now - gui->summary.net_rate_prev_ts;
     515           0 :   if( FD_LIKELY( dt_ns>0L ) ) {
     516           0 :     double dt_sec = (double)dt_ns / 1.0e9;
     517             : 
     518           0 :     for( ulong i=0UL; i<FD_GUIH_NET_PROTO_CNT; i++ ) {
     519           0 :       double rate_in  = (double)d_in[ i ]  / dt_sec;
     520           0 :       double rate_out = (double)d_out[ i ] / dt_sec;
     521             : 
     522           0 :       if( FD_UNLIKELY( !gui->summary.net_rate_ema_ready ) ) {
     523           0 :         gui->summary.ingress_ema[ i ] = rate_in;
     524           0 :         gui->summary.egress_ema[ i ]  = rate_out;
     525           0 :       } else {
     526           0 :         gui->summary.ingress_ema[ i ] = fd_guih_ema( gui->summary.net_rate_prev_ts, now, rate_in,  gui->summary.ingress_ema[ i ], FD_GUIH_NETWORK_EMA_HALF_LIFE_NS );
     527           0 :         gui->summary.egress_ema[ i ]  = fd_guih_ema( gui->summary.net_rate_prev_ts, now, rate_out, gui->summary.egress_ema[ i ],  FD_GUIH_NETWORK_EMA_HALF_LIFE_NS );
     528           0 :       }
     529           0 :     }
     530           0 :     gui->summary.net_rate_ema_ready = 1;
     531           0 :   }
     532           0 :   gui->summary.net_rate_prev_ts = now;
     533             : 
     534             :   /* Track max total EMA in a rolling 5-minute window using monotonic
     535             :      deques.
     536             : 
     537             :      Invariant: deque entries are strictly decreasing in value from
     538             :      head to tail.  The head is always the current window maximum.
     539             : 
     540             :      Insert:  pop tail entries whose value <= new value (they can
     541             :               never become the maximum), then push the new entry.
     542             :      Expire:  pop head entries older than 5 minutes. */
     543           0 :   if( FD_LIKELY( gui->summary.net_rate_ema_ready ) ) {
     544           0 :     double sum_in  = 0.0;
     545           0 :     double sum_out = 0.0;
     546           0 :     for( ulong i=0UL; i<FD_GUIH_NET_PROTO_CNT; i++ ) {
     547           0 :       sum_in  += gui->summary.ingress_ema[ i ];
     548           0 :       sum_out += gui->summary.egress_ema[ i ];
     549           0 :     }
     550             : 
     551           0 :     while( !fd_guih_rate_deque_empty( gui->summary.ingress_maxq ) && fd_guih_rate_deque_peek_head_const( gui->summary.ingress_maxq )->ts_nanos<now-FD_GUIH_NET_RATE_MAX_WINDOW_NS ) {
     552           0 :       fd_guih_rate_deque_pop_head( gui->summary.ingress_maxq );
     553           0 :     }
     554           0 :     while( !fd_guih_rate_deque_empty( gui->summary.ingress_maxq ) && fd_guih_rate_deque_peek_tail_const( gui->summary.ingress_maxq )->value<=sum_in ) {
     555           0 :       fd_guih_rate_deque_pop_tail( gui->summary.ingress_maxq );
     556           0 :     }
     557           0 :     if( FD_UNLIKELY( fd_guih_rate_deque_full( gui->summary.ingress_maxq ) ) ) {
     558           0 :       fd_guih_rate_deque_pop_tail( gui->summary.ingress_maxq );
     559           0 :     }
     560           0 :     fd_guih_rate_deque_push_tail( gui->summary.ingress_maxq, (fd_guih_rate_entry_t){ .ts_nanos=now, .value=sum_in } );
     561             : 
     562           0 :     while( !fd_guih_rate_deque_empty( gui->summary.egress_maxq ) && fd_guih_rate_deque_peek_head_const( gui->summary.egress_maxq )->ts_nanos<now-FD_GUIH_NET_RATE_MAX_WINDOW_NS ) {
     563           0 :       fd_guih_rate_deque_pop_head( gui->summary.egress_maxq );
     564           0 :     }
     565           0 :     while( !fd_guih_rate_deque_empty( gui->summary.egress_maxq ) && fd_guih_rate_deque_peek_tail_const( gui->summary.egress_maxq )->value<=sum_out ) {
     566           0 :       fd_guih_rate_deque_pop_tail( gui->summary.egress_maxq );
     567           0 :     }
     568           0 :     if( FD_UNLIKELY( fd_guih_rate_deque_full( gui->summary.egress_maxq ) ) ) {
     569           0 :       fd_guih_rate_deque_pop_tail( gui->summary.egress_maxq );
     570           0 :     }
     571           0 :     fd_guih_rate_deque_push_tail( gui->summary.egress_maxq, (fd_guih_rate_entry_t){ .ts_nanos=now, .value=sum_out } );
     572           0 :   }
     573             : 
     574           0 :   *prev = *cur;
     575           0 : }
     576             : 
     577             : /* Snapshot all of the data from metrics to construct a view of the
     578             :    transaction waterfall.
     579             : 
     580             :    Tiles are sampled in reverse pipeline order: this helps prevent data
     581             :    discrepancies where a later tile has "seen" more transactions than an
     582             :    earlier tile, which shouldn't typically happen. */
     583             : 
     584             : static void
     585             : fd_guih_txn_waterfall_snap( fd_guih_t *               gui,
     586           0 :                            fd_guih_txn_waterfall_t * cur ) {
     587           0 :   memset( cur, 0, sizeof(fd_guih_txn_waterfall_t) );
     588           0 :   fd_topo_t const * topo = gui->topo;
     589             : 
     590           0 :   for( ulong i=0UL; i<gui->summary.bank_tile_cnt; i++ ) {
     591           0 :     fd_topo_tile_t const * bank = &topo->tiles[ fd_topo_find_tile( topo, "bank", i ) ];
     592             : 
     593           0 :     volatile ulong const * bank_metrics = fd_metrics_tile( bank->metrics );
     594           0 :     cur->out.block_success += bank_metrics[ MIDX( COUNTER, BANK, TXN_EXECUTED_SUCCESS ) ];
     595             : 
     596           0 :     cur->out.block_fail +=
     597           0 :         bank_metrics[ MIDX( COUNTER, BANK, TXN_EXECUTED_FAILED ) ]
     598           0 :       + bank_metrics[ MIDX( COUNTER, BANK, TXN_FEE_ONLY        ) ];
     599             : 
     600           0 :     cur->out.bank_invalid +=
     601           0 :         bank_metrics[ MIDX( COUNTER, BANK, TXN_LOAD_ADDRESS_TABLE_ACCOUNT_UNINITIALIZED ) ]
     602           0 :       + bank_metrics[ MIDX( COUNTER, BANK, TXN_LOAD_ADDRESS_TABLE_ACCOUNT_NOT_FOUND ) ]
     603           0 :       + bank_metrics[ MIDX( COUNTER, BANK, TXN_LOAD_ADDRESS_TABLE_INVALID_ACCOUNT_OWNER ) ]
     604           0 :       + bank_metrics[ MIDX( COUNTER, BANK, TXN_LOAD_ADDRESS_TABLE_INVALID_ACCOUNT_DATA ) ]
     605           0 :       + bank_metrics[ MIDX( COUNTER, BANK, TXN_LOAD_ADDRESS_TABLE_INVALID_LOOKUP_INDEX  ) ];
     606             : 
     607           0 :     cur->out.bank_invalid +=
     608           0 :         bank_metrics[ MIDX( COUNTER, BANK, TXN_PROCESSING_FAILED ) ];
     609           0 :   }
     610             : 
     611           0 :   for( ulong i=0UL; i<gui->summary.execle_tile_cnt; i++ ) {
     612           0 :     fd_topo_tile_t const * execle = &topo->tiles[ fd_topo_find_tile( topo, "execle", i ) ];
     613             : 
     614           0 :     volatile ulong const * execle_metrics = fd_metrics_tile( execle->metrics );
     615             : 
     616           0 :     cur->out.block_success += execle_metrics[ MIDX( COUNTER, EXECLE, TXN_LANDED_LANDED_SUCCESS ) ];
     617           0 :     cur->out.block_fail    +=
     618           0 :         execle_metrics[ MIDX( COUNTER, EXECLE, TXN_LANDED_LANDED_FEES_ONLY ) ]
     619           0 :       + execle_metrics[ MIDX( COUNTER, EXECLE, TXN_LANDED_LANDED_FAILED ) ];
     620           0 :     cur->out.bank_invalid  += execle_metrics[ MIDX( COUNTER, EXECLE, TXN_LANDED_UNLANDED ) ];
     621             : 
     622           0 :     cur->out.bank_nonce_already_advanced += execle_metrics[ MIDX( COUNTER, EXECLE, TXN_RESULT_NONCE_ALREADY_ADVANCED ) ];
     623           0 :     cur->out.bank_nonce_advance_failed   += execle_metrics[ MIDX( COUNTER, EXECLE, TXN_RESULT_NONCE_ADVANCE_FAILED ) ];
     624           0 :     cur->out.bank_nonce_wrong_blockhash  += execle_metrics[ MIDX( COUNTER, EXECLE, TXN_RESULT_NONCE_WRONG_BLOCKHASH ) ];
     625           0 :   }
     626             : 
     627           0 :   ulong pack_tile_idx = fd_topo_find_tile( topo, "pack", 0UL );
     628           0 :   if( pack_tile_idx!=ULONG_MAX ) {
     629           0 :     fd_topo_tile_t const * pack = &topo->tiles[ pack_tile_idx ];
     630           0 :     volatile ulong const * pack_metrics = fd_metrics_tile( pack->metrics );
     631             : 
     632           0 :     cur->out.pack_invalid_bundle =
     633           0 :         pack_metrics[ MIDX( COUNTER, PACK, TXN_PARTIAL_BUNDLE ) ]
     634           0 :       + pack_metrics[ MIDX( COUNTER, PACK, BUNDLE_CRANK_RESULT_INSERTION_FAILED ) ]
     635           0 :       + pack_metrics[ MIDX( COUNTER, PACK, BUNDLE_CRANK_RESULT_CREATION_FAILED ) ];
     636             : 
     637           0 :     cur->out.pack_invalid =
     638           0 :         pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_INSTR_ACCT_CNT ) ]
     639           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_NONCE_CONFLICT ) ]
     640           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_BUNDLE_BLACKLIST ) ]
     641           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_INVALID_NONCE ) ]
     642           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_WRITE_SYSVAR ) ]
     643           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_ESTIMATION_FAIL ) ]
     644           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_DUPLICATE_ACCOUNT ) ]
     645           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_TOO_MANY_ACCOUNTS ) ]
     646           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_TOO_LARGE ) ]
     647           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_ADDR_LUT ) ]
     648           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_UNAFFORDABLE ) ]
     649           0 :       + pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_DUPLICATE ) ]
     650           0 :       - pack_metrics[ MIDX( COUNTER, PACK, BUNDLE_CRANK_RESULT_INSERTION_FAILED ) ]; /* so we don't double count this, since its already accounted for in invalid_bundle */
     651             : 
     652           0 :     cur->out.pack_expired = pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_EXPIRED ) ] +
     653           0 :                             pack_metrics[ MIDX( COUNTER, PACK, TXN_EXPIRED ) ] +
     654           0 :                             pack_metrics[ MIDX( COUNTER, PACK, TXN_DELETED ) ] +
     655           0 :                             pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_NONCE_PRIORITY ) ];
     656             : 
     657           0 :     cur->out.pack_already_executed = pack_metrics[ MIDX( COUNTER, PACK, TXN_ALREADY_EXECUTED ) ];
     658             : 
     659           0 :     cur->out.pack_leader_slow = pack_metrics[ MIDX( COUNTER, PACK, TXN_INSERTED_PRIORITY ) ];
     660             : 
     661           0 :     cur->out.pack_wait_full =
     662           0 :         pack_metrics[ MIDX( COUNTER, PACK, TXN_EXTRA_DROPPED ) ];
     663             : 
     664           0 :     cur->out.pack_retained = pack_metrics[ MIDX( GAUGE, PACK, TXN_AVAILABLE ) ];
     665             : 
     666           0 :     ulong inserted_to_extra = pack_metrics[ MIDX( COUNTER, PACK, TXN_EXTRA_INSERTED ) ];
     667           0 :     ulong inserted_from_extra = pack_metrics[ MIDX( COUNTER, PACK, TXN_EXTRA_RETRIEVED ) ]
     668           0 :                                 + pack_metrics[ MIDX( COUNTER, PACK, TXN_EXTRA_DROPPED ) ];
     669           0 :     cur->out.pack_retained += fd_ulong_if( inserted_to_extra>=inserted_from_extra, inserted_to_extra-inserted_from_extra, 0UL );
     670             : 
     671           0 :     cur->in.pack_cranked =
     672           0 :         pack_metrics[ MIDX( COUNTER, PACK, BUNDLE_CRANK_RESULT_INSERTED ) ]
     673           0 :       + pack_metrics[ MIDX( COUNTER, PACK, BUNDLE_CRANK_RESULT_INSERTION_FAILED ) ]
     674           0 :       + pack_metrics[ MIDX( COUNTER, PACK, BUNDLE_CRANK_RESULT_CREATION_FAILED ) ];
     675           0 :   }
     676             : 
     677           0 :   for( ulong i=0UL; i<gui->summary.resolh_tile_cnt; i++ ) {
     678           0 :     fd_topo_tile_t const * resolv = &topo->tiles[ fd_topo_find_tile( topo, "resolh", i ) ];
     679           0 :     volatile ulong const * resolv_metrics = fd_metrics_tile( resolv->metrics );
     680             : 
     681           0 :     cur->out.resolv_no_ledger += resolv_metrics[ MIDX( COUNTER, RESOLH, TXN_NO_BANK ) ];
     682           0 :     cur->out.resolv_expired += resolv_metrics[ MIDX( COUNTER, RESOLH, BLOCKHASH_EXPIRED ) ]
     683           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLH, TXN_BUNDLE_PEER_FAILED  ) ];
     684           0 :     cur->out.resolv_lut_failed += resolv_metrics[ MIDX( COUNTER, RESOLH, LUT_RESOLVED_ACCOUNT_NOT_FOUND ) ]
     685           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLH, LUT_RESOLVED_INVALID_ACCOUNT_OWNER ) ]
     686           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLH, LUT_RESOLVED_INVALID_ACCOUNT_DATA ) ]
     687           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLH, LUT_RESOLVED_ACCOUNT_UNINITIALIZED ) ]
     688           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLH, LUT_RESOLVED_INVALID_LOOKUP_INDEX ) ];
     689           0 :     cur->out.resolv_ancient += resolv_metrics[ MIDX( COUNTER, RESOLH, STASH_OPERATION_OVERRUN ) ];
     690             : 
     691           0 :     ulong inserted_to_resolv = resolv_metrics[ MIDX( COUNTER, RESOLH, STASH_OPERATION_INSERTED ) ];
     692           0 :     ulong removed_from_resolv = resolv_metrics[ MIDX( COUNTER, RESOLH, STASH_OPERATION_OVERRUN ) ]
     693           0 :                               + resolv_metrics[ MIDX( COUNTER, RESOLH, STASH_OPERATION_PUBLISHED ) ]
     694           0 :                               + resolv_metrics[ MIDX( COUNTER, RESOLH, STASH_OPERATION_REMOVED ) ];
     695           0 :     cur->out.resolv_retained += fd_ulong_if( inserted_to_resolv>=removed_from_resolv, inserted_to_resolv-removed_from_resolv, 0UL );
     696           0 :   }
     697             : 
     698           0 :   for( ulong i=0UL; i<gui->summary.resolv_tile_cnt; i++ ) {
     699           0 :     fd_topo_tile_t const * resolv = &topo->tiles[ fd_topo_find_tile( topo, "resolv", i ) ];
     700           0 :     volatile ulong const * resolv_metrics = fd_metrics_tile( resolv->metrics );
     701             : 
     702           0 :     cur->out.resolv_no_ledger += resolv_metrics[ MIDX( COUNTER, RESOLV, TXN_NO_BANK ) ];
     703           0 :     cur->out.resolv_expired += resolv_metrics[ MIDX( COUNTER, RESOLV, BLOCKHASH_EXPIRED ) ]
     704           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLV, TXN_BUNDLE_PEER_FAILED  ) ];
     705           0 :     cur->out.resolv_lut_failed += resolv_metrics[ MIDX( COUNTER, RESOLV, LUT_RESOLVED_ACCOUNT_NOT_FOUND ) ]
     706           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLV, LUT_RESOLVED_INVALID_ACCOUNT_OWNER ) ]
     707           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLV, LUT_RESOLVED_INVALID_ACCOUNT_DATA ) ]
     708           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLV, LUT_RESOLVED_ACCOUNT_UNINITIALIZED ) ]
     709           0 :                                 + resolv_metrics[ MIDX( COUNTER, RESOLV, LUT_RESOLVED_INVALID_LOOKUP_INDEX ) ];
     710           0 :     cur->out.resolv_ancient += resolv_metrics[ MIDX( COUNTER, RESOLV, STASH_OPERATION_OVERRUN ) ];
     711             : 
     712           0 :     ulong inserted_to_resolv = resolv_metrics[ MIDX( COUNTER, RESOLV, STASH_OPERATION_INSERTED ) ];
     713           0 :     ulong removed_from_resolv = resolv_metrics[ MIDX( COUNTER, RESOLV, STASH_OPERATION_OVERRUN ) ]
     714           0 :                               + resolv_metrics[ MIDX( COUNTER, RESOLV, STASH_OPERATION_PUBLISHED ) ]
     715           0 :                               + resolv_metrics[ MIDX( COUNTER, RESOLV, STASH_OPERATION_REMOVED ) ];
     716           0 :     cur->out.resolv_retained += fd_ulong_if( inserted_to_resolv>=removed_from_resolv, inserted_to_resolv-removed_from_resolv, 0UL );
     717           0 :   }
     718             : 
     719           0 :   ulong dedup_tile_idx = fd_topo_find_tile( topo, "dedup", 0UL );
     720           0 :   if( FD_UNLIKELY( dedup_tile_idx!=ULONG_MAX ) ) {
     721           0 :     fd_topo_tile_t const * dedup = &topo->tiles[ dedup_tile_idx ];
     722           0 :     volatile ulong const * dedup_metrics = fd_metrics_tile( dedup->metrics );
     723             : 
     724           0 :     cur->out.dedup_duplicate = dedup_metrics[ MIDX( COUNTER, DEDUP, TXN_RESULT_DEDUP_FAILURE ) ]
     725           0 :                              + dedup_metrics[ MIDX( COUNTER, DEDUP, TXN_RESULT_BUNDLE_PEER_FAILURE ) ];
     726           0 :   }
     727             : 
     728           0 :   for( ulong i=0UL; i<gui->summary.verify_tile_cnt; i++ ) {
     729           0 :     fd_topo_tile_t const * verify = &topo->tiles[ fd_topo_find_tile( topo, "verify", i ) ];
     730           0 :     volatile ulong const * verify_metrics = fd_metrics_tile( verify->metrics );
     731             : 
     732           0 :     for( ulong j=0UL; j<gui->summary.quic_tile_cnt; j++ ) {
     733             :       /* TODO: Not precise... even if 1 frag gets skipped, it could have been for this verify tile. */
     734           0 :       cur->out.verify_overrun += fd_metrics_link_in( verify->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_POLLING_OVERRUN_OFF ] / gui->summary.verify_tile_cnt;
     735           0 :       cur->out.verify_overrun += fd_metrics_link_in( verify->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_READING_OVERRUN_OFF ];
     736           0 :     }
     737             : 
     738           0 :     cur->out.verify_failed    += verify_metrics[ MIDX( COUNTER, VERIFY, TXN_RESULT_VERIFY_FAILURE ) ] +
     739           0 :                                  verify_metrics[ MIDX( COUNTER, VERIFY, TXN_RESULT_BUNDLE_PEER_FAILURE ) ];
     740           0 :     cur->out.verify_parse     += verify_metrics[ MIDX( COUNTER, VERIFY, TXN_RESULT_PARSE_FAILURE ) ];
     741           0 :     cur->out.verify_duplicate += verify_metrics[ MIDX( COUNTER, VERIFY, TXN_RESULT_DEDUP_FAILURE ) ];
     742           0 :   }
     743             : 
     744           0 :   for( ulong i=0UL; i<gui->summary.quic_tile_cnt; i++ ) {
     745           0 :     fd_topo_tile_t const * quic = &topo->tiles[ fd_topo_find_tile( topo, "quic", i ) ];
     746           0 :     volatile ulong * quic_metrics = fd_metrics_tile( quic->metrics );
     747             : 
     748           0 :     cur->out.tpu_udp_invalid  += quic_metrics[ MIDX( COUNTER, QUIC, LEGACY_TXN_UNDERSIZE   ) ];
     749           0 :     cur->out.tpu_udp_invalid  += quic_metrics[ MIDX( COUNTER, QUIC, LEGACY_TXN_OVERSIZE    ) ];
     750           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_UNDERSIZE          ) ];
     751           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_OVERSIZE           ) ];
     752           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, TXN_OVERSIZE           ) ];
     753           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_CRYPTO_FAILED      ) ];
     754           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_NO_CONN            ) ];
     755           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_SRC_INVALID        ) ];
     756           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_NET_HEADER_INVALID ) ];
     757           0 :     cur->out.tpu_quic_invalid += quic_metrics[ MIDX( COUNTER, QUIC, PKT_HEADER_INVALID     ) ];
     758           0 :     cur->out.quic_abandoned   += quic_metrics[ MIDX( COUNTER, QUIC, TXN_ABANDONED          ) ];
     759           0 :     cur->out.quic_frag_drop   += quic_metrics[ MIDX( COUNTER, QUIC, TXN_OVERRUN            ) ];
     760             : 
     761           0 :     for( ulong j=0UL; j<gui->summary.net_tile_cnt; j++ ) {
     762             :       /* TODO: Not precise... net frags that were skipped might not have been destined for QUIC tile */
     763             :       /* TODO: Not precise... even if 1 frag gets skipped, it could have been for this QUIC tile */
     764           0 :       cur->out.quic_overrun += fd_metrics_link_in( quic->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_POLLING_OVERRUN_OFF ] / gui->summary.quic_tile_cnt;
     765           0 :       cur->out.quic_overrun += fd_metrics_link_in( quic->metrics, j )[ FD_METRICS_COUNTER_LINK_FRAG_READING_OVERRUN_OFF ];
     766           0 :     }
     767           0 :   }
     768             : 
     769           0 :   for( ulong i=0UL; i<gui->summary.net_tile_cnt; i++ ) {
     770           0 :     fd_topo_tile_t const * net = &topo->tiles[ fd_topo_find_tile( topo, "net", i ) ];
     771           0 :     volatile ulong * net_metrics = fd_metrics_tile( net->metrics );
     772             : 
     773           0 :     cur->out.net_overrun += net_metrics[ MIDX( COUNTER, NET, XDP_RX_RING_FULL ) ];
     774           0 :     cur->out.net_overrun += net_metrics[ MIDX( COUNTER, NET, XDP_RX_OTHER_DROPPED ) ];
     775           0 :     cur->out.net_overrun += net_metrics[ MIDX( COUNTER, NET, XDP_RX_FILL_RING_EMPTY ) ];
     776           0 :   }
     777             : 
     778           0 :   ulong bundle_txns_received = 0UL;
     779           0 :   ulong bundle_tile_idx = fd_topo_find_tile( topo, "bundle", 0UL );
     780           0 :   if( FD_LIKELY( bundle_tile_idx!=ULONG_MAX ) ) {
     781           0 :     fd_topo_tile_t const * bundle = &topo->tiles[ bundle_tile_idx ];
     782           0 :     volatile ulong const * bundle_metrics = fd_metrics_tile( bundle->metrics );
     783             : 
     784           0 :     bundle_txns_received = bundle_metrics[ MIDX( COUNTER, BUNDLE, TXN_RX ) ];
     785           0 :   }
     786             : 
     787           0 :   if( dedup_tile_idx!=ULONG_MAX ) {
     788           0 :     fd_topo_tile_t const * dedup = &topo->tiles[ dedup_tile_idx ];
     789           0 :     volatile ulong const * dedup_metrics = fd_metrics_tile( dedup->metrics );
     790           0 :     cur->in.gossip = dedup_metrics[ MIDX( COUNTER, DEDUP, VOTE_GOSSIP_RX ) ];
     791           0 :   }
     792             : 
     793           0 :   cur->in.quic     = cur->out.tpu_quic_invalid +
     794           0 :                      cur->out.quic_overrun +
     795           0 :                      cur->out.quic_frag_drop +
     796           0 :                      cur->out.quic_abandoned +
     797           0 :                      cur->out.net_overrun;
     798           0 :   cur->in.udp      = cur->out.tpu_udp_invalid;
     799           0 :   cur->in.block_engine = bundle_txns_received;
     800           0 :   for( ulong i=0UL; i<gui->summary.quic_tile_cnt; i++ ) {
     801           0 :     fd_topo_tile_t const * quic = &topo->tiles[ fd_topo_find_tile( topo, "quic", i ) ];
     802           0 :     volatile ulong * quic_metrics = fd_metrics_tile( quic->metrics );
     803             : 
     804           0 :     cur->in.quic += quic_metrics[ MIDX( COUNTER, QUIC, TXN_RX_QUIC_FAST ) ];
     805           0 :     cur->in.quic += quic_metrics[ MIDX( COUNTER, QUIC, TXN_RX_QUIC_FRAG ) ];
     806           0 :     cur->in.udp  += quic_metrics[ MIDX( COUNTER, QUIC, TXN_RX_UDP       ) ];
     807           0 :   }
     808           0 : }
     809             : 
     810             : static void
     811             : fd_guih_tile_stats_snap( fd_guih_t *                     gui,
     812             :                         fd_guih_txn_waterfall_t const * waterfall,
     813             :                         fd_guih_tile_stats_t *          stats,
     814           0 :                         long                           now ) {
     815           0 :   memset( stats, 0, sizeof(fd_guih_tile_stats_t) );
     816           0 :   fd_topo_t const * topo = gui->topo;
     817             : 
     818           0 :   stats->sample_time_nanos = now;
     819             : 
     820           0 :   for( ulong i=0UL; i<gui->summary.net_tile_cnt; i++ ) {
     821           0 :     fd_topo_tile_t const * net = &topo->tiles[ fd_topo_find_tile( topo, "net", i ) ];
     822           0 :     volatile ulong * net_metrics = fd_metrics_tile( net->metrics );
     823             : 
     824           0 :     stats->net_in_rx_bytes  += net_metrics[ MIDX( COUNTER, NET, PKT_RX_BYTES ) ];
     825           0 :     stats->net_out_tx_bytes += net_metrics[ MIDX( COUNTER, NET, PKT_TX_BYTES ) ];
     826           0 :   }
     827             : 
     828           0 :   for( ulong i=0UL; i<gui->summary.sock_tile_cnt; i++ ) {
     829           0 :     fd_topo_tile_t const * sock = &topo->tiles[ fd_topo_find_tile( topo, "sock", i ) ];
     830           0 :     volatile ulong * sock_metrics = fd_metrics_tile( sock->metrics );
     831             : 
     832           0 :     stats->net_in_rx_bytes  += sock_metrics[ MIDX( COUNTER, SOCK, PKT_RX_BYTES ) ];
     833           0 :     stats->net_out_tx_bytes += sock_metrics[ MIDX( COUNTER, SOCK, PKT_TX_BYTES ) ];
     834           0 :   }
     835             : 
     836           0 :   for( ulong i=0UL; i<gui->summary.quic_tile_cnt; i++ ) {
     837           0 :     fd_topo_tile_t const * quic = &topo->tiles[ fd_topo_find_tile( topo, "quic", i ) ];
     838           0 :     volatile ulong * quic_metrics = fd_metrics_tile( quic->metrics );
     839             : 
     840           0 :     stats->quic_conn_cnt += quic_metrics[ MIDX( GAUGE, QUIC, CONN_IN_USE ) ];
     841           0 :   }
     842             : 
     843           0 :   ulong bundle_tile_idx = fd_topo_find_tile( topo, "bundle", 0UL );
     844           0 :   if( FD_LIKELY( bundle_tile_idx!=ULONG_MAX ) ) {
     845           0 :     fd_topo_tile_t const * bundle = &topo->tiles[ bundle_tile_idx ];
     846           0 :     volatile ulong * bundle_metrics = fd_metrics_tile( bundle->metrics );
     847           0 :     stats->bundle_rtt_smoothed_nanos = bundle_metrics[ MIDX( GAUGE, BUNDLE, RTT_SMOOTHED_NANOS ) ];
     848             : 
     849           0 :     fd_histf_new( &stats->bundle_rx_delay_hist, FD_MHIST_MIN( BUNDLE, MESSAGE_RX_DELAY_NANOS ), FD_MHIST_MAX( BUNDLE, MESSAGE_RX_DELAY_NANOS ) );
     850           0 :     stats->bundle_rx_delay_hist.sum = bundle_metrics[ MIDX( HISTOGRAM, BUNDLE, MESSAGE_RX_DELAY_NANOS ) + FD_HISTF_BUCKET_CNT ];
     851           0 :     for( ulong b=0; b<FD_HISTF_BUCKET_CNT; b++ ) stats->bundle_rx_delay_hist.counts[ b ] = bundle_metrics[ MIDX( HISTOGRAM, BUNDLE, MESSAGE_RX_DELAY_NANOS ) + b ];
     852           0 :   }
     853             : 
     854           0 :   stats->verify_drop_cnt = waterfall->out.verify_duplicate +
     855           0 :                            waterfall->out.verify_parse +
     856           0 :                            waterfall->out.verify_failed;
     857           0 :   stats->verify_total_cnt = waterfall->in.gossip +
     858           0 :                             waterfall->in.quic +
     859           0 :                             waterfall->in.udp -
     860           0 :                             waterfall->out.net_overrun -
     861           0 :                             waterfall->out.tpu_quic_invalid -
     862           0 :                             waterfall->out.tpu_udp_invalid -
     863           0 :                             waterfall->out.quic_abandoned -
     864           0 :                             waterfall->out.quic_frag_drop -
     865           0 :                             waterfall->out.quic_overrun -
     866           0 :                             waterfall->out.verify_overrun;
     867           0 :   stats->dedup_drop_cnt = waterfall->out.dedup_duplicate;
     868           0 :   stats->dedup_total_cnt = stats->verify_total_cnt -
     869           0 :                            waterfall->out.verify_duplicate -
     870           0 :                             waterfall->out.verify_parse -
     871           0 :                             waterfall->out.verify_failed;
     872             : 
     873           0 :   ulong pack_tile_idx = fd_topo_find_tile( topo, "pack", 0UL );
     874           0 :   if( pack_tile_idx!=ULONG_MAX ) {
     875           0 :     fd_topo_tile_t const * pack  = &topo->tiles[ pack_tile_idx ];
     876           0 :     volatile ulong const * pack_metrics = fd_metrics_tile( pack->metrics );
     877           0 :     stats->pack_buffer_cnt      = pack_metrics[ MIDX( GAUGE, PACK, TXN_AVAILABLE ) ];
     878           0 :     stats->pack_buffer_capacity = pack->pack.max_pending_transactions;
     879           0 :   }
     880             : 
     881           0 :   stats->bank_txn_exec_cnt = waterfall->out.block_fail + waterfall->out.block_success;
     882           0 : }
     883             : 
     884             : static inline int
     885           0 : fd_guih_ephemeral_slots_contains( fd_guih_ephemeral_slot_t * slots, ulong slots_sz, ulong slot ) {
     886           0 :   for( ulong i=0UL; i<slots_sz; i++ ) {
     887           0 :     if( FD_UNLIKELY( slots[ i ].slot==ULONG_MAX ) ) break;
     888           0 :     if( FD_UNLIKELY( slots[ i ].slot==slot ) ) return 1;
     889           0 :   }
     890           0 :   return 0;
     891           0 : }
     892             : 
     893             : #define SORT_NAME fd_guih_ephemeral_slot_sort
     894           0 : #define SORT_KEY_T fd_guih_ephemeral_slot_t
     895           0 : #define SORT_BEFORE(a,b) fd_int_if( (a).slot==ULONG_MAX, 0, fd_int_if( (b).slot==ULONG_MAX, 1, fd_int_if( (a).slot==(b).slot, (a).timestamp_arrival_nanos>(b).timestamp_arrival_nanos, (a).slot>(b).slot ) ) )
     896             : #include "../../util/tmpl/fd_sort.c"
     897             : 
     898             : static inline void
     899           0 : fd_guih_try_insert_ephemeral_slot( fd_guih_ephemeral_slot_t * slots, ulong slots_sz, ulong slot, long now ) {
     900           0 :   int already_present = 0;
     901           0 :   for( ulong i=0UL; i<slots_sz; i++ ) {
     902             :     /* evict any slots older than 4.8 seconds */
     903           0 :     if( FD_UNLIKELY( slots[ i ].slot!=ULONG_MAX && now-slots[ i ].timestamp_arrival_nanos>4800000000L ) ) {
     904           0 :       slots[ i ].slot = ULONG_MAX;
     905           0 :       continue;
     906           0 :     }
     907             : 
     908             :     /* if we've already seen this slot, just update the timestamp */
     909           0 :     if( FD_UNLIKELY( slots[ i ].slot==slot ) ) {
     910           0 :       slots[ i ].timestamp_arrival_nanos = now;
     911           0 :       already_present = 1;
     912           0 :     }
     913           0 :   }
     914           0 :   if( FD_LIKELY( already_present ) ) return;
     915             : 
     916             :   /* Insert the new slot number, evicting a smaller slot if necessary */
     917           0 :   slots[ slots_sz ].timestamp_arrival_nanos = now;
     918           0 :   slots[ slots_sz ].slot = slot;
     919           0 :   fd_guih_ephemeral_slot_sort_insert( slots, slots_sz+1UL );
     920           0 : }
     921             : 
     922             : static inline void
     923           0 : fd_guih_try_insert_run_length_slot( ulong * slots, ulong capacity, ulong * slots_sz, ulong slot ) {
     924             :   /* catch up history is run-length encoded */
     925           0 :   ulong range_idx = fd_sort_up_ulong_split( slots, *slots_sz, slot );
     926           0 :   if( FD_UNLIKELY( range_idx<(*slots_sz)-1UL              && range_idx%2UL==0UL && slots[ range_idx ]<=slot && slots[ range_idx+1UL ]>=slot ) ) return;
     927           0 :   if( FD_UNLIKELY( range_idx<(*slots_sz) && range_idx>0UL && range_idx%2UL==1UL && slots[ range_idx-1UL ]<=slot && slots[ range_idx ]>=slot ) ) return;
     928             : 
     929           0 :   slots[ (*slots_sz)++ ] = slot;
     930           0 :   slots[ (*slots_sz)++ ] = slot;
     931             : 
     932           0 :   fd_sort_up_ulong_insert( slots, (*slots_sz) );
     933             : 
     934             :   /* colesce ranges */
     935           0 :   ulong removed = 0UL;
     936           0 :   for( ulong i=1UL; i<(*slots_sz)-1UL; i+=2 ) {
     937           0 :     if( FD_UNLIKELY( slots[ i ]+1UL==slots[ i+1UL ] ) ) {
     938           0 :       slots[ i ]     = ULONG_MAX;
     939           0 :       slots[ i+1UL ] = ULONG_MAX;
     940           0 :       removed += 2;
     941           0 :     }
     942           0 :   }
     943             : 
     944           0 :   if( FD_UNLIKELY( (*slots_sz)>=removed+capacity-2UL && (*slots_sz)>=4UL ) ) {
     945             :     /* We are at capacity, start coalescing earlier intervals. */
     946           0 :     slots[ 1 ] = ULONG_MAX;
     947           0 :     slots[ 2 ] = ULONG_MAX;
     948           0 :     removed += 2;
     949           0 :   }
     950             : 
     951           0 :   fd_sort_up_ulong_insert( slots, (*slots_sz) );
     952           0 :   (*slots_sz) -= removed;
     953           0 : }
     954             : 
     955             : void
     956           0 : fd_guih_handle_repair_slot( fd_guih_t * gui, ulong slot, long now ) {
     957           0 :   int was_sent = fd_guih_ephemeral_slots_contains( gui->summary.slots_max_repair, FD_GUIH_REPAIR_SLOT_HISTORY_SZ, slot );
     958           0 :   fd_guih_try_insert_ephemeral_slot( gui->summary.slots_max_repair, FD_GUIH_REPAIR_SLOT_HISTORY_SZ, slot, now );
     959             : 
     960           0 :   if( FD_UNLIKELY( !was_sent && slot!=gui->summary.slot_repair ) ) {
     961           0 :     gui->summary.slot_repair = slot;
     962             : 
     963           0 :     fd_guih_printf_repair_slot( gui );
     964           0 :     fd_http_server_ws_broadcast( gui->http );
     965             : 
     966           0 :     if( FD_UNLIKELY( gui->summary.slot_caught_up==ULONG_MAX ) ) fd_guih_try_insert_run_length_slot( gui->summary.catch_up_repair, FD_GUIH_REPAIR_CATCH_UP_HISTORY_SZ, &gui->summary.catch_up_repair_sz, slot );
     967           0 :   }
     968           0 : }
     969             : 
     970             : int
     971           0 : fd_guih_poll( fd_guih_t * gui, long now ) {
     972           0 :   if( FD_LIKELY( now>gui->next_sample_400millis ) ) {
     973           0 :     fd_guih_estimated_tps_snap( gui );
     974           0 :     fd_guih_printf_estimated_tps( gui );
     975           0 :     fd_http_server_ws_broadcast( gui->http );
     976             : 
     977           0 :     gui->next_sample_400millis += 400L*1000L*1000L;
     978           0 :     return 1;
     979           0 :   }
     980             : 
     981           0 :   if( FD_LIKELY( now>gui->next_sample_100millis ) ) {
     982           0 :     fd_guih_txn_waterfall_snap( gui, gui->summary.txn_waterfall_current );
     983           0 :     fd_guih_printf_live_txn_waterfall( gui, gui->summary.txn_waterfall_reference, gui->summary.txn_waterfall_current, 0UL /* TODO: REAL NEXT LEADER SLOT */ );
     984           0 :     fd_http_server_ws_broadcast( gui->http );
     985             : 
     986           0 :     fd_guih_network_stats_snap( gui, gui->summary.network_stats_current );
     987           0 :     fd_guih_network_rate_max_update( gui, now );
     988           0 :     fd_guih_printf_live_network_metrics( gui, gui->summary.network_stats_current );
     989           0 :     fd_http_server_ws_broadcast( gui->http );
     990             : 
     991           0 :     *gui->summary.tile_stats_reference = *gui->summary.tile_stats_current;
     992           0 :     fd_guih_tile_stats_snap( gui, gui->summary.txn_waterfall_current, gui->summary.tile_stats_current, now );
     993           0 :     fd_guih_printf_live_tile_stats( gui, gui->summary.tile_stats_reference, gui->summary.tile_stats_current );
     994           0 :     fd_http_server_ws_broadcast( gui->http );
     995             : 
     996           0 :     ulong bundle_tile_idx = fd_topo_find_tile( gui->topo, "bundle", 0UL );
     997           0 :     if( FD_LIKELY( bundle_tile_idx!=ULONG_MAX ) ) {
     998           0 :       volatile ulong const * bundle_metrics = fd_metrics_tile( gui->topo->tiles[ bundle_tile_idx ].metrics );
     999           0 :       int cur_state = (int)bundle_metrics[ MIDX( GAUGE, BUNDLE, STATE ) ];
    1000           0 :       if( FD_UNLIKELY( cur_state != gui->block_engine.status ) ) {
    1001           0 :         gui->block_engine.status = cur_state;
    1002           0 :         fd_guih_printf_block_engine( gui );
    1003           0 :         fd_http_server_ws_broadcast( gui->http );
    1004           0 :       }
    1005           0 :     }
    1006             : 
    1007           0 :     fd_guih_printf_health( gui );
    1008           0 :     fd_http_server_ws_broadcast( gui->http );
    1009             : 
    1010           0 :     gui->next_sample_100millis += 100L*1000L*1000L;
    1011           0 :     return 1;
    1012           0 :   }
    1013             : 
    1014           0 :   if( FD_LIKELY( now>gui->next_sample_50millis ) ) {
    1015             :     /* We get the repair slot from the sampled metric after catching up
    1016             :        and from incoming shred data before catchup. This makes the
    1017             :        catchup progress bar look complete while also keeping the
    1018             :        overview slots vis correct.  TODO: do this properly using frags
    1019             :        sent over a link */
    1020           0 :     if( FD_LIKELY( gui->summary.slot_caught_up!=ULONG_MAX ) ) {
    1021           0 :       fd_topo_tile_t const * repair = &gui->topo->tiles[ fd_topo_find_tile( gui->topo, "repair", 0UL ) ];
    1022           0 :       volatile ulong const * repair_metrics = fd_metrics_tile( repair->metrics );
    1023           0 :       ulong slot = repair_metrics[ MIDX( GAUGE, REPAIR, SLOT_HIGHEST_REPAIRED ) ];
    1024           0 :       fd_guih_handle_repair_slot( gui, slot, now );
    1025           0 :     }
    1026             : 
    1027           0 :     gui->next_sample_50millis += 50L*1000L*1000L;
    1028           0 :     return 1;
    1029           0 :   }
    1030             : 
    1031           0 :   if( FD_LIKELY( now>gui->next_sample_25millis ) ) {
    1032           0 :     fd_guih_tile_timers_snap( gui );
    1033             : 
    1034           0 :     fd_guih_printf_live_tile_timers( gui );
    1035           0 :     fd_http_server_ws_broadcast( gui->http );
    1036             : 
    1037           0 :     fd_guih_printf_live_tile_metrics( gui );
    1038           0 :     fd_http_server_ws_broadcast( gui->http );
    1039             : 
    1040           0 :     gui->next_sample_25millis += (long)(25*1000L*1000L);
    1041           0 :     return 1;
    1042           0 :   }
    1043             : 
    1044             : 
    1045           0 :   if( FD_LIKELY( now>gui->next_sample_10millis ) ) {
    1046           0 :     fd_guih_scheduler_counts_snap( gui, now );
    1047             : 
    1048           0 :     fd_guih_printf_server_time_nanos( gui, now );
    1049           0 :     fd_http_server_ws_broadcast( gui->http );
    1050             : 
    1051           0 :     gui->next_sample_10millis += 10L*1000L*1000L;
    1052           0 :     return 1;
    1053           0 :   }
    1054             : 
    1055           0 :   return 0;
    1056           0 : }
    1057             : 
    1058             : static void
    1059             : fd_guih_handle_gossip_update( fd_guih_t *    gui,
    1060           0 :                              uchar const * msg ) {
    1061             :   /* `gui->gossip.peer_cnt` is guaranteed to be in [0, FD_GUIH_MAX_PEER_CNT], because
    1062             :   `peer_cnt` is FD_TEST-ed to be less than or equal FD_GUIH_MAX_PEER_CNT.
    1063             :   For every new peer that is added an existing peer will be removed or was still free.
    1064             :   And adding a new peer is done at most `peer_cnt` times. */
    1065           0 :   ulong peer_cnt = FD_LOAD( ulong, msg );
    1066             : 
    1067           0 :   FD_TEST( peer_cnt<=FD_GUIH_MAX_PEER_CNT );
    1068             : 
    1069           0 :   ulong added_cnt = 0UL;
    1070           0 :   ulong added[ FD_GUIH_MAX_PEER_CNT ] = {0};
    1071             : 
    1072           0 :   ulong update_cnt = 0UL;
    1073           0 :   ulong updated[ FD_GUIH_MAX_PEER_CNT ] = {0};
    1074             : 
    1075           0 :   ulong removed_cnt = 0UL;
    1076           0 :   fd_pubkey_t removed[ FD_GUIH_MAX_PEER_CNT ] = {0};
    1077             : 
    1078           0 :   uchar const * data = msg + sizeof(ulong);
    1079           0 :   for( ulong i=0UL; i<gui->gossip.peer_cnt; i++ ) {
    1080           0 :     int found = 0;
    1081           0 :     for( ulong j=0UL; j<peer_cnt; j++ ) {
    1082           0 :       if( FD_UNLIKELY( !memcmp( gui->gossip.peers[ i ].pubkey, data+j*(58UL+12UL*6UL), 32UL ) ) ) {
    1083           0 :         found = 1;
    1084           0 :         break;
    1085           0 :       }
    1086           0 :     }
    1087             : 
    1088           0 :     if( FD_UNLIKELY( !found ) ) {
    1089           0 :       fd_memcpy( removed[ removed_cnt++ ].uc, gui->gossip.peers[ i ].pubkey->uc, 32UL );
    1090           0 :       if( FD_LIKELY( i+1UL!=gui->gossip.peer_cnt ) ) {
    1091           0 :         gui->gossip.peers[ i ] = gui->gossip.peers[ gui->gossip.peer_cnt-1UL ];
    1092           0 :         i--;
    1093           0 :       }
    1094           0 :       gui->gossip.peer_cnt--;
    1095           0 :     }
    1096           0 :   }
    1097             : 
    1098           0 :   ulong before_peer_cnt = gui->gossip.peer_cnt;
    1099           0 :   for( ulong i=0UL; i<peer_cnt; i++ ) {
    1100           0 :     int found = 0;
    1101           0 :     ulong found_idx = 0;
    1102           0 :     for( ulong j=0UL; j<gui->gossip.peer_cnt; j++ ) {
    1103           0 :       if( FD_UNLIKELY( !memcmp( gui->gossip.peers[ j ].pubkey, data+i*(58UL+12UL*6UL), 32UL ) ) ) {
    1104           0 :         found_idx = j;
    1105           0 :         found = 1;
    1106           0 :         break;
    1107           0 :       }
    1108           0 :     }
    1109             : 
    1110           0 :     if( FD_UNLIKELY( !found ) ) {
    1111           0 :       fd_memcpy( gui->gossip.peers[ gui->gossip.peer_cnt ].pubkey->uc, data+i*(58UL+12UL*6UL), 32UL );
    1112           0 :       gui->gossip.peers[ gui->gossip.peer_cnt ].wallclock = FD_LOAD( ulong, data+i*(58UL+12UL*6UL)+32UL );
    1113           0 :       gui->gossip.peers[ gui->gossip.peer_cnt ].shred_version = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+40UL );
    1114           0 :       gui->gossip.peers[ gui->gossip.peer_cnt ].has_version = FD_LOAD( uchar, data+i*(58UL+12UL*6UL)+42UL );
    1115           0 :       if( FD_LIKELY( gui->gossip.peers[ gui->gossip.peer_cnt ].has_version ) ) {
    1116           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].version.major = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+43UL );
    1117           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].version.minor = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+45UL );
    1118           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].version.patch = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+47UL );
    1119           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].version.has_commit = FD_LOAD( uchar, data+i*(58UL+12UL*6UL)+49UL );
    1120           0 :         if( FD_LIKELY( gui->gossip.peers[ gui->gossip.peer_cnt ].version.has_commit ) ) {
    1121           0 :           gui->gossip.peers[ gui->gossip.peer_cnt ].version.commit = FD_LOAD( uint, data+i*(58UL+12UL*6UL)+50UL );
    1122           0 :         }
    1123           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].version.feature_set = FD_LOAD( uint, data+i*(58UL+12UL*6UL)+54UL );
    1124           0 :       }
    1125             : 
    1126           0 :       for( ulong j=0UL; j<12UL; j++ ) {
    1127           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].sockets[ j ].ipv4 = FD_LOAD( uint, data+i*(58UL+12UL*6UL)+58UL+j*6UL );
    1128           0 :         gui->gossip.peers[ gui->gossip.peer_cnt ].sockets[ j ].port = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+58UL+j*6UL+4UL );
    1129           0 :       }
    1130             : 
    1131           0 :       gui->gossip.peer_cnt++;
    1132           0 :     } else {
    1133           0 :       int peer_updated = gui->gossip.peers[ found_idx ].shred_version!=FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+40UL ) ||
    1134             :                          // gui->gossip.peers[ found_idx ].wallclock!=FD_LOAD( ulong, data+i*(58UL+12UL*6UL)+32UL ) ||
    1135           0 :                          gui->gossip.peers[ found_idx ].has_version!=FD_LOAD( uchar, data+i*(58UL+12UL*6UL)+42UL );
    1136             : 
    1137           0 :       if( FD_LIKELY( !peer_updated && gui->gossip.peers[ found_idx ].has_version ) ) {
    1138           0 :         peer_updated = gui->gossip.peers[ found_idx ].version.major!=FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+43UL ) ||
    1139           0 :                         gui->gossip.peers[ found_idx ].version.minor!=FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+45UL ) ||
    1140           0 :                         gui->gossip.peers[ found_idx ].version.patch!=FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+47UL ) ||
    1141           0 :                         gui->gossip.peers[ found_idx ].version.has_commit!=FD_LOAD( uchar, data+i*(58UL+12UL*6UL)+49UL ) ||
    1142           0 :                         (gui->gossip.peers[ found_idx ].version.has_commit && gui->gossip.peers[ found_idx ].version.commit!=FD_LOAD( uint, data+i*(58UL+12UL*6UL)+50UL )) ||
    1143           0 :                         gui->gossip.peers[ found_idx ].version.feature_set!=FD_LOAD( uint, data+i*(58UL+12UL*6UL)+54UL );
    1144           0 :       }
    1145             : 
    1146           0 :       if( FD_LIKELY( !peer_updated ) ) {
    1147           0 :         for( ulong j=0UL; j<12UL; j++ ) {
    1148           0 :           peer_updated = gui->gossip.peers[ found_idx ].sockets[ j ].ipv4!=FD_LOAD( uint, data+i*(58UL+12UL*6UL)+58UL+j*6UL ) ||
    1149           0 :                           gui->gossip.peers[ found_idx ].sockets[ j ].port!=FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+58UL+j*6UL+4UL );
    1150           0 :           if( FD_LIKELY( peer_updated ) ) break;
    1151           0 :         }
    1152           0 :       }
    1153             : 
    1154           0 :       if( FD_UNLIKELY( peer_updated ) ) {
    1155           0 :         updated[ update_cnt++ ] = found_idx;
    1156           0 :         gui->gossip.peers[ found_idx ].shred_version = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+40UL );
    1157           0 :         gui->gossip.peers[ found_idx ].wallclock = FD_LOAD( ulong, data+i*(58UL+12UL*6UL)+32UL );
    1158           0 :         gui->gossip.peers[ found_idx ].has_version = FD_LOAD( uchar, data+i*(58UL+12UL*6UL)+42UL );
    1159           0 :         if( FD_LIKELY( gui->gossip.peers[ found_idx ].has_version ) ) {
    1160           0 :           gui->gossip.peers[ found_idx ].version.major = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+43UL );
    1161           0 :           gui->gossip.peers[ found_idx ].version.minor = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+45UL );
    1162           0 :           gui->gossip.peers[ found_idx ].version.patch = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+47UL );
    1163           0 :           gui->gossip.peers[ found_idx ].version.has_commit = FD_LOAD( uchar, data+i*(58UL+12UL*6UL)+49UL );
    1164           0 :           if( FD_LIKELY( gui->gossip.peers[ found_idx ].version.has_commit ) ) {
    1165           0 :             gui->gossip.peers[ found_idx ].version.commit = FD_LOAD( uint, data+i*(58UL+12UL*6UL)+50UL );
    1166           0 :           }
    1167           0 :           gui->gossip.peers[ found_idx ].version.feature_set = FD_LOAD( uint, data+i*(58UL+12UL*6UL)+54UL );
    1168           0 :         }
    1169             : 
    1170           0 :         for( ulong j=0UL; j<12UL; j++ ) {
    1171           0 :           gui->gossip.peers[ found_idx ].sockets[ j ].ipv4 = FD_LOAD( uint, data+i*(58UL+12UL*6UL)+58UL+j*6UL );
    1172           0 :           gui->gossip.peers[ found_idx ].sockets[ j ].port = FD_LOAD( ushort, data+i*(58UL+12UL*6UL)+58UL+j*6UL+4UL );
    1173           0 :         }
    1174           0 :       }
    1175           0 :     }
    1176           0 :   }
    1177             : 
    1178           0 :   added_cnt = gui->gossip.peer_cnt - before_peer_cnt;
    1179           0 :   for( ulong i=before_peer_cnt; i<gui->gossip.peer_cnt; i++ ) added[ i-before_peer_cnt ] = i;
    1180             : 
    1181           0 :   fd_guih_printf_peers_gossip_update( gui, updated, update_cnt, removed, removed_cnt, added, added_cnt );
    1182           0 :   fd_http_server_ws_broadcast( gui->http );
    1183           0 : }
    1184             : 
    1185             : static void
    1186             : fd_guih_handle_vote_account_update( fd_guih_t *    gui,
    1187           0 :                                    uchar const * msg ) {
    1188             :   /* See fd_guih_handle_gossip_update for why `gui->vote_account.vote_account_cnt`
    1189             :   is guaranteed to be in [0, FD_GUIH_MAX_PEER_CNT]. */
    1190           0 :   ulong peer_cnt = FD_LOAD( ulong, msg );
    1191             : 
    1192           0 :   FD_TEST( peer_cnt<=FD_GUIH_MAX_PEER_CNT );
    1193             : 
    1194           0 :   ulong added_cnt = 0UL;
    1195           0 :   ulong added[ FD_GUIH_MAX_PEER_CNT ] = {0};
    1196             : 
    1197           0 :   ulong update_cnt = 0UL;
    1198           0 :   ulong updated[ FD_GUIH_MAX_PEER_CNT ] = {0};
    1199             : 
    1200           0 :   ulong removed_cnt = 0UL;
    1201           0 :   fd_pubkey_t removed[ FD_GUIH_MAX_PEER_CNT ] = {0};
    1202             : 
    1203           0 :   uchar const * data = msg + sizeof(ulong);
    1204           0 :   for( ulong i=0UL; i<gui->vote_account.vote_account_cnt; i++ ) {
    1205           0 :     int found = 0;
    1206           0 :     for( ulong j=0UL; j<peer_cnt; j++ ) {
    1207           0 :       if( FD_UNLIKELY( !memcmp( gui->vote_account.vote_accounts[ i ].vote_account, data+j*112UL, 32UL ) ) ) {
    1208           0 :         found = 1;
    1209           0 :         break;
    1210           0 :       }
    1211           0 :     }
    1212             : 
    1213           0 :     if( FD_UNLIKELY( !found ) ) {
    1214           0 :       fd_memcpy( removed[ removed_cnt++ ].uc, gui->vote_account.vote_accounts[ i ].vote_account->uc, 32UL );
    1215           0 :       if( FD_LIKELY( i+1UL!=gui->vote_account.vote_account_cnt ) ) {
    1216           0 :         gui->vote_account.vote_accounts[ i ] = gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt-1UL ];
    1217           0 :         i--;
    1218           0 :       }
    1219           0 :       gui->vote_account.vote_account_cnt--;
    1220           0 :     }
    1221           0 :   }
    1222             : 
    1223           0 :   ulong before_peer_cnt = gui->vote_account.vote_account_cnt;
    1224           0 :   for( ulong i=0UL; i<peer_cnt; i++ ) {
    1225           0 :     int found = 0;
    1226           0 :     ulong found_idx;
    1227           0 :     for( ulong j=0UL; j<gui->vote_account.vote_account_cnt; j++ ) {
    1228           0 :       if( FD_UNLIKELY( !memcmp( gui->vote_account.vote_accounts[ j ].vote_account, data+i*112UL, 32UL ) ) ) {
    1229           0 :         found_idx = j;
    1230           0 :         found = 1;
    1231           0 :         break;
    1232           0 :       }
    1233           0 :     }
    1234             : 
    1235           0 :     if( FD_UNLIKELY( !found ) ) {
    1236           0 :       fd_memcpy( gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].vote_account->uc, data+i*112UL, 32UL );
    1237           0 :       fd_memcpy( gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].pubkey->uc, data+i*112UL+32UL, 32UL );
    1238             : 
    1239           0 :       gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].activated_stake = FD_LOAD( ulong, data+i*112UL+64UL );
    1240           0 :       gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].last_vote = FD_LOAD( ulong, data+i*112UL+72UL );
    1241           0 :       gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].root_slot = FD_LOAD( ulong, data+i*112UL+80UL );
    1242           0 :       gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].epoch_credits = FD_LOAD( ulong, data+i*112UL+88UL );
    1243           0 :       gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].commission = FD_LOAD( uchar, data+i*112UL+96UL );
    1244           0 :       gui->vote_account.vote_accounts[ gui->vote_account.vote_account_cnt ].delinquent = FD_LOAD( uchar, data+i*112UL+97UL );
    1245             : 
    1246           0 :       gui->vote_account.vote_account_cnt++;
    1247           0 :     } else {
    1248           0 :       int peer_updated =
    1249           0 :         memcmp( gui->vote_account.vote_accounts[ found_idx ].pubkey->uc, data+i*112UL+32UL, 32UL ) ||
    1250           0 :         gui->vote_account.vote_accounts[ found_idx ].activated_stake != FD_LOAD( ulong, data+i*112UL+64UL ) ||
    1251             :         // gui->vote_account.vote_accounts[ found_idx ].last_vote       != FD_LOAD( ulong, data+i*112UL+72UL ) ||
    1252             :         // gui->vote_account.vote_accounts[ found_idx ].root_slot       != FD_LOAD( ulong, data+i*112UL+80UL ) ||
    1253             :         // gui->vote_account.vote_accounts[ found_idx ].epoch_credits   != FD_LOAD( ulong, data+i*112UL+88UL ) ||
    1254           0 :         gui->vote_account.vote_accounts[ found_idx ].commission      != FD_LOAD( uchar, data+i*112UL+96UL ) ||
    1255           0 :         gui->vote_account.vote_accounts[ found_idx ].delinquent      != FD_LOAD( uchar, data+i*112UL+97UL );
    1256             : 
    1257           0 :       if( FD_UNLIKELY( peer_updated ) ) {
    1258           0 :         updated[ update_cnt++ ] = found_idx;
    1259             : 
    1260           0 :         fd_memcpy( gui->vote_account.vote_accounts[ found_idx ].pubkey->uc, data+i*112UL+32UL, 32UL );
    1261           0 :         gui->vote_account.vote_accounts[ found_idx ].activated_stake = FD_LOAD( ulong, data+i*112UL+64UL );
    1262           0 :         gui->vote_account.vote_accounts[ found_idx ].last_vote = FD_LOAD( ulong, data+i*112UL+72UL );
    1263           0 :         gui->vote_account.vote_accounts[ found_idx ].root_slot = FD_LOAD( ulong, data+i*112UL+80UL );
    1264           0 :         gui->vote_account.vote_accounts[ found_idx ].epoch_credits = FD_LOAD( ulong, data+i*112UL+88UL );
    1265           0 :         gui->vote_account.vote_accounts[ found_idx ].commission = FD_LOAD( uchar, data+i*112UL+96UL );
    1266           0 :         gui->vote_account.vote_accounts[ found_idx ].delinquent = FD_LOAD( uchar, data+i*112UL+97UL );
    1267           0 :       }
    1268           0 :     }
    1269           0 :   }
    1270             : 
    1271           0 :   added_cnt = gui->vote_account.vote_account_cnt - before_peer_cnt;
    1272           0 :   for( ulong i=before_peer_cnt; i<gui->vote_account.vote_account_cnt; i++ ) added[ i-before_peer_cnt ] = i;
    1273             : 
    1274           0 :   fd_guih_printf_peers_vote_account_update( gui, updated, update_cnt, removed, removed_cnt, added, added_cnt );
    1275           0 :   fd_http_server_ws_broadcast( gui->http );
    1276           0 : }
    1277             : 
    1278             : static void
    1279             : fd_guih_handle_validator_info_update( fd_guih_t *    gui,
    1280           0 :                                      uchar const * msg ) {
    1281           0 :   if( FD_UNLIKELY( gui->validator_info.info_cnt == FD_GUIH_MAX_PEER_CNT ) ) {
    1282           0 :     FD_LOG_DEBUG(("validator info cnt exceeds 108000 %lu, ignoring additional entries", gui->validator_info.info_cnt ));
    1283           0 :     return;
    1284           0 :   }
    1285           0 :   uchar const * data = (uchar const *)fd_type_pun_const( msg );
    1286             : 
    1287           0 :   ulong added_cnt = 0UL;
    1288           0 :   ulong added[ 1 ] = {0};
    1289             : 
    1290           0 :   ulong update_cnt = 0UL;
    1291           0 :   ulong updated[ 1 ] = {0};
    1292             : 
    1293           0 :   ulong removed_cnt = 0UL;
    1294             :   /* Unlike gossip or vote account updates, validator info messages come
    1295             :      in as info is discovered, and may contain as little as 1 validator
    1296             :      per message.  Therefore, it doesn't make sense to use the remove
    1297             :      mechanism.  */
    1298             : 
    1299           0 :   ulong before_peer_cnt = gui->validator_info.info_cnt;
    1300           0 :   int found = 0;
    1301           0 :   ulong found_idx;
    1302           0 :   for( ulong j=0UL; j<gui->validator_info.info_cnt; j++ ) {
    1303           0 :     if( FD_UNLIKELY( !memcmp( gui->validator_info.info[ j ].pubkey, data, 32UL ) ) ) {
    1304           0 :       found_idx = j;
    1305           0 :       found = 1;
    1306           0 :       break;
    1307           0 :     }
    1308           0 :   }
    1309             : 
    1310           0 :   if( FD_UNLIKELY( !found ) ) {
    1311           0 :     fd_memcpy( gui->validator_info.info[ gui->validator_info.info_cnt ].pubkey->uc, data, 32UL );
    1312             : 
    1313           0 :     strncpy( gui->validator_info.info[ gui->validator_info.info_cnt ].name, (char const *)(data+32UL), 64 );
    1314           0 :     gui->validator_info.info[ gui->validator_info.info_cnt ].name[ 63 ] = '\0';
    1315             : 
    1316           0 :     strncpy( gui->validator_info.info[ gui->validator_info.info_cnt ].website, (char const *)(data+96UL), 128 );
    1317           0 :     gui->validator_info.info[ gui->validator_info.info_cnt ].website[ 127 ] = '\0';
    1318             : 
    1319           0 :     strncpy( gui->validator_info.info[ gui->validator_info.info_cnt ].details, (char const *)(data+224UL), 256 );
    1320           0 :     gui->validator_info.info[ gui->validator_info.info_cnt ].details[ 255 ] = '\0';
    1321             : 
    1322           0 :     strncpy( gui->validator_info.info[ gui->validator_info.info_cnt ].icon_uri, (char const *)(data+480UL), 128 );
    1323           0 :     gui->validator_info.info[ gui->validator_info.info_cnt ].icon_uri[ 127 ] = '\0';
    1324             : 
    1325           0 :     gui->validator_info.info_cnt++;
    1326           0 :   } else {
    1327           0 :     int peer_updated =
    1328           0 :       memcmp( gui->validator_info.info[ found_idx ].pubkey->uc, data, 32UL ) ||
    1329           0 :       strncmp( gui->validator_info.info[ found_idx ].name, (char const *)(data+32UL), 64 ) ||
    1330           0 :       strncmp( gui->validator_info.info[ found_idx ].website, (char const *)(data+96UL), 128 ) ||
    1331           0 :       strncmp( gui->validator_info.info[ found_idx ].details, (char const *)(data+224UL), 256 ) ||
    1332           0 :       strncmp( gui->validator_info.info[ found_idx ].icon_uri, (char const *)(data+480UL), 128 );
    1333             : 
    1334           0 :     if( FD_UNLIKELY( peer_updated ) ) {
    1335           0 :       updated[ update_cnt++ ] = found_idx;
    1336             : 
    1337           0 :       fd_memcpy( gui->validator_info.info[ found_idx ].pubkey->uc, data, 32UL );
    1338             : 
    1339           0 :       strncpy( gui->validator_info.info[ found_idx ].name, (char const *)(data+32UL), 64 );
    1340           0 :       gui->validator_info.info[ found_idx ].name[ 63 ] = '\0';
    1341             : 
    1342           0 :       strncpy( gui->validator_info.info[ found_idx ].website, (char const *)(data+96UL), 128 );
    1343           0 :       gui->validator_info.info[ found_idx ].website[ 127 ] = '\0';
    1344             : 
    1345           0 :       strncpy( gui->validator_info.info[ found_idx ].details, (char const *)(data+224UL), 256 );
    1346           0 :       gui->validator_info.info[ found_idx ].details[ 255 ] = '\0';
    1347             : 
    1348           0 :       strncpy( gui->validator_info.info[ found_idx ].icon_uri, (char const *)(data+480UL), 128 );
    1349           0 :       gui->validator_info.info[ found_idx ].icon_uri[ 127 ] = '\0';
    1350           0 :     }
    1351           0 :   }
    1352             : 
    1353           0 :   added_cnt = gui->validator_info.info_cnt - before_peer_cnt;
    1354           0 :   for( ulong i=before_peer_cnt; i<gui->validator_info.info_cnt; i++ ) added[ i-before_peer_cnt ] = i;
    1355             : 
    1356           0 :   fd_guih_printf_peers_validator_info_update( gui, updated, update_cnt, NULL, removed_cnt, added, added_cnt );
    1357           0 :   fd_http_server_ws_broadcast( gui->http );
    1358           0 : }
    1359             : 
    1360             : int
    1361             : fd_guih_request_slot( fd_guih_t *    gui,
    1362             :                      ulong         ws_conn_id,
    1363             :                      ulong         request_id,
    1364           0 :                      cJSON const * params ) {
    1365           0 :   const cJSON * slot_param = cJSON_GetObjectItemCaseSensitive( params, "slot" );
    1366           0 :   if( FD_UNLIKELY( !cJSON_IsNumber( slot_param ) ) ) return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1367             : 
    1368           0 :   ulong _slot = slot_param->valueulong;
    1369           0 :   fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, _slot );
    1370           0 :   if( FD_UNLIKELY( !slot ) ) {
    1371           0 :     fd_guih_printf_null_query_response( gui->http, "slot", "query", request_id );
    1372           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1373           0 :     return 0;
    1374           0 :   }
    1375             : 
    1376           0 :   fd_guih_printf_slot_request( gui, _slot, request_id );
    1377           0 :   FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1378           0 :   return 0;
    1379           0 : }
    1380             : 
    1381             : int
    1382             : fd_guih_request_slot_transactions( fd_guih_t *    gui,
    1383             :                                   ulong         ws_conn_id,
    1384             :                                   ulong         request_id,
    1385           0 :                                   cJSON const * params ) {
    1386           0 :   const cJSON * slot_param = cJSON_GetObjectItemCaseSensitive( params, "slot" );
    1387           0 :   if( FD_UNLIKELY( !cJSON_IsNumber( slot_param ) ) ) return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1388             : 
    1389           0 :   ulong _slot = slot_param->valueulong;
    1390           0 :   fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, _slot );
    1391           0 :   if( FD_UNLIKELY( !slot ) ) {
    1392           0 :     fd_guih_printf_null_query_response( gui->http, "slot", "query_transactions", request_id );
    1393           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1394           0 :     return 0;
    1395           0 :   }
    1396             : 
    1397           0 :   fd_guih_printf_slot_transactions_request( gui, _slot, request_id );
    1398           0 :   FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1399           0 :   return 0;
    1400           0 : }
    1401             : 
    1402             : int
    1403             : fd_guih_request_slot_detailed( fd_guih_t *    gui,
    1404             :                               ulong         ws_conn_id,
    1405             :                               ulong         request_id,
    1406           0 :                               cJSON const * params ) {
    1407           0 :   const cJSON * slot_param = cJSON_GetObjectItemCaseSensitive( params, "slot" );
    1408           0 :   if( FD_UNLIKELY( !cJSON_IsNumber( slot_param ) ) ) return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1409             : 
    1410           0 :   ulong _slot = slot_param->valueulong;
    1411           0 :   fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, _slot );
    1412           0 :   if( FD_UNLIKELY( !slot ) ) {
    1413           0 :     fd_guih_printf_null_query_response( gui->http, "slot", "query_detailed", request_id );
    1414           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1415           0 :     return 0;
    1416           0 :   }
    1417             : 
    1418           0 :   fd_guih_printf_slot_request_detailed( gui, _slot, request_id );
    1419           0 :   FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1420           0 :   return 0;
    1421           0 : }
    1422             : 
    1423             : static inline ulong
    1424           0 : fd_guih_slot_duration( fd_guih_t const * gui, fd_guih_slot_t const * cur ) {
    1425           0 :   fd_guih_slot_t const * prev = fd_guih_get_slot_const( gui, cur->slot-1UL );
    1426           0 :   if( FD_UNLIKELY( !prev ||
    1427           0 :                    prev->skipped ||
    1428           0 :                    prev->completed_time == LONG_MAX ||
    1429           0 :                    prev->slot != (cur->slot - 1UL) ||
    1430           0 :                    cur->skipped ||
    1431           0 :                    cur->completed_time == LONG_MAX ) ) return ULONG_MAX;
    1432             : 
    1433           0 :   return (ulong)(cur->completed_time - prev->completed_time);
    1434           0 : }
    1435             : 
    1436             : /* All rankings are initialized / reset to ULONG_MAX.  These sentinels
    1437             :    sort AFTER non-sentinel ranking entries.  Equal slots are sorted by
    1438             :    oldest slot AFTER.  Otherwise sort by value according to ranking
    1439             :    type. */
    1440             : #define SORT_NAME fd_guih_slot_ranking_sort
    1441           0 : #define SORT_KEY_T fd_guih_slot_ranking_t
    1442           0 : #define SORT_BEFORE(a,b) fd_int_if( (a).slot==ULONG_MAX, 0, fd_int_if( (b).slot==ULONG_MAX, 1, fd_int_if( (a).value==(b).value, (a).slot>(b).slot, fd_int_if( (a).type==FD_GUIH_SLOT_RANKING_TYPE_DESC, (a).value>(b).value, (a).value<(b).value ) ) ) )
    1443             : #include "../../util/tmpl/fd_sort.c"
    1444             : 
    1445             : static inline void
    1446             : fd_guih_try_insert_ranking( fd_guih_t               * gui,
    1447             :                            fd_guih_slot_rankings_t * rankings,
    1448           0 :                            fd_guih_slot_t const    * slot ) {
    1449             :   /* Rankings are inserted into an extra slot at the end of the ranking
    1450             :      array, then the array is sorted. */
    1451           0 : #define TRY_INSERT_SLOT( ranking_name, ranking_slot, ranking_value ) \
    1452           0 :   do { \
    1453           0 :     rankings->FD_CONCAT2(largest_, ranking_name) [ FD_GUIH_SLOT_RANKINGS_SZ ] = (fd_guih_slot_ranking_t){ .slot = (ranking_slot), .value = (ranking_value), .type = FD_GUIH_SLOT_RANKING_TYPE_DESC }; \
    1454           0 :     fd_guih_slot_ranking_sort_insert( rankings->FD_CONCAT2(largest_, ranking_name), FD_GUIH_SLOT_RANKINGS_SZ+1UL ); \
    1455           0 :     rankings->FD_CONCAT2(smallest_, ranking_name)[ FD_GUIH_SLOT_RANKINGS_SZ ] = (fd_guih_slot_ranking_t){ .slot = (ranking_slot), .value = (ranking_value), .type = FD_GUIH_SLOT_RANKING_TYPE_ASC  }; \
    1456           0 :     fd_guih_slot_ranking_sort_insert( rankings->FD_CONCAT2(smallest_, ranking_name), FD_GUIH_SLOT_RANKINGS_SZ+1UL ); \
    1457           0 :   } while (0)
    1458             : 
    1459           0 :     if( slot->skipped ) {
    1460           0 :       TRY_INSERT_SLOT( skipped, slot->slot, slot->slot );
    1461           0 :       return;
    1462           0 :     }
    1463             : 
    1464           0 :     ulong dur = fd_guih_slot_duration( gui, slot );
    1465           0 :     if( FD_LIKELY( dur!=ULONG_MAX ) ) TRY_INSERT_SLOT( duration, slot->slot, dur                         );
    1466           0 :     TRY_INSERT_SLOT( tips,           slot->slot, slot->tips                                              );
    1467           0 :     TRY_INSERT_SLOT( fees,           slot->slot, slot->priority_fee + slot->transaction_fee              );
    1468           0 :     TRY_INSERT_SLOT( rewards,        slot->slot, slot->tips + slot->priority_fee + slot->transaction_fee );
    1469           0 :     TRY_INSERT_SLOT( rewards_per_cu, slot->slot, slot->compute_units==0UL ? 0UL : (slot->tips + slot->priority_fee + slot->transaction_fee) / slot->compute_units );
    1470           0 :     TRY_INSERT_SLOT( compute_units,  slot->slot, slot->compute_units                                     );
    1471           0 : #undef TRY_INSERT_SLOT
    1472           0 : }
    1473             : 
    1474             : static void
    1475           0 : fd_guih_update_slot_rankings( fd_guih_t * gui ) {
    1476           0 :   ulong first_replay_slot = ULONG_MAX;
    1477           0 :   first_replay_slot = gui->summary.startup_progress.startup_ledger_max_slot;
    1478           0 :   if( FD_UNLIKELY( first_replay_slot==ULONG_MAX ) ) return;
    1479           0 :   if( FD_UNLIKELY( gui->summary.slot_rooted ==ULONG_MAX ) ) return;
    1480             : 
    1481           0 :   ulong epoch_idx = fd_guih_current_epoch_idx( gui );
    1482           0 :   if( FD_UNLIKELY( epoch_idx==ULONG_MAX ) ) return;
    1483             : 
    1484             :   /* No new slots since the last update */
    1485           0 :   if( FD_UNLIKELY( gui->epoch.epochs[ epoch_idx ].rankings_slot>gui->summary.slot_rooted ) ) return;
    1486             : 
    1487             :   /* Slots before first_replay_slot are unavailable. */
    1488           0 :   gui->epoch.epochs[ epoch_idx ].rankings_slot = fd_ulong_max( gui->epoch.epochs[ epoch_idx ].rankings_slot, first_replay_slot );
    1489             : 
    1490             :   /* Update the rankings. Only look through slots we haven't already. */
    1491           0 :   for( ulong s = gui->summary.slot_rooted; s>=gui->epoch.epochs[ epoch_idx ].rankings_slot; s--) {
    1492           0 :     fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, s );
    1493           0 :     if( FD_UNLIKELY( !slot ) ) break;
    1494             : 
    1495           0 :     fd_guih_try_insert_ranking( gui, gui->epoch.epochs[ epoch_idx ].rankings, slot );
    1496           0 :     if( FD_UNLIKELY( slot->mine ) ) fd_guih_try_insert_ranking( gui, gui->epoch.epochs[ epoch_idx ].my_rankings, slot );
    1497           0 :   }
    1498             : 
    1499           0 :   gui->epoch.epochs[ epoch_idx ].rankings_slot = gui->summary.slot_rooted + 1UL;
    1500           0 : }
    1501             : 
    1502             : int
    1503             : fd_guih_request_slot_rankings( fd_guih_t *    gui,
    1504             :                               ulong         ws_conn_id,
    1505             :                               ulong         request_id,
    1506           0 :                               cJSON const * params ) {
    1507           0 :   const cJSON * slot_param = cJSON_GetObjectItemCaseSensitive( params, "mine" );
    1508           0 :   if( FD_UNLIKELY( !cJSON_IsBool( slot_param ) ) ) return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1509             : 
    1510           0 :   int mine = !!(slot_param->type & cJSON_True);
    1511           0 :   fd_guih_update_slot_rankings( gui );
    1512           0 :   fd_guih_printf_slot_rankings_request( gui, request_id, mine );
    1513           0 :   FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1514           0 :   return 0;
    1515           0 : }
    1516             : 
    1517             : int
    1518             : fd_guih_request_slot_shreds( fd_guih_t *    gui,
    1519             :                             ulong         ws_conn_id,
    1520             :                             ulong         request_id,
    1521           0 :                             cJSON const * params ) {
    1522           0 :   const cJSON * slot_param = cJSON_GetObjectItemCaseSensitive( params, "slot" );
    1523           0 :   if( FD_UNLIKELY( !cJSON_IsNumber( slot_param ) ) ) return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1524             : 
    1525           0 :   ulong _slot = slot_param->valueulong;
    1526             : 
    1527           0 :   fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, _slot );
    1528           0 :   if( FD_UNLIKELY( !slot || slot->shreds.start_offset==ULONG_MAX || slot->shreds.end_offset==ULONG_MAX || gui->shreds.history_tail >= slot->shreds.end_offset + FD_GUIH_SHREDS_HISTORY_SZ ) ) {
    1529           0 :     fd_guih_printf_null_query_response( gui->http, "slot", "query_shreds", request_id );
    1530           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1531           0 :     return 0;
    1532           0 :   }
    1533             : 
    1534           0 :   fd_guih_printf_slot_query_shreds( gui, _slot, request_id );
    1535           0 :   FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1536           0 :   return 0;
    1537           0 : }
    1538             : 
    1539             : int
    1540             : fd_guih_ws_message( fd_guih_t *    gui,
    1541             :                    ulong         ws_conn_id,
    1542             :                    uchar const * data,
    1543           0 :                    ulong         data_len ) {
    1544             :   /* TODO: cJSON allocates, might fail SIGSYS due to brk(2)...
    1545             :      switch off this (or use wksp allocator) */
    1546           0 :   const char * parse_end;
    1547           0 :   cJSON * json = cJSON_ParseWithLengthOpts( (char *)data, data_len, &parse_end, 0 );
    1548           0 :   if( FD_UNLIKELY( !json ) ) {
    1549           0 :     return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1550           0 :   }
    1551             : 
    1552           0 :   const cJSON * node = cJSON_GetObjectItemCaseSensitive( json, "id" );
    1553           0 :   if( FD_UNLIKELY( !cJSON_IsNumber( node ) ) ) {
    1554           0 :     cJSON_Delete( json );
    1555           0 :     return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1556           0 :   }
    1557           0 :   ulong id = node->valueulong;
    1558             : 
    1559           0 :   const cJSON * topic = cJSON_GetObjectItemCaseSensitive( json, "topic" );
    1560           0 :   if( FD_UNLIKELY( !cJSON_IsString( topic ) || topic->valuestring==NULL ) ) {
    1561           0 :     cJSON_Delete( json );
    1562           0 :     return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1563           0 :   }
    1564             : 
    1565           0 :   const cJSON * key = cJSON_GetObjectItemCaseSensitive( json, "key" );
    1566           0 :   if( FD_UNLIKELY( !cJSON_IsString( key ) || key->valuestring==NULL ) ) {
    1567           0 :     cJSON_Delete( json );
    1568           0 :     return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1569           0 :   }
    1570             : 
    1571           0 :   if( FD_LIKELY( !strcmp( topic->valuestring, "slot" ) && !strcmp( key->valuestring, "query" ) ) ) {
    1572           0 :     const cJSON * params = cJSON_GetObjectItemCaseSensitive( json, "params" );
    1573           0 :     if( FD_UNLIKELY( !cJSON_IsObject( params ) ) ) {
    1574           0 :       cJSON_Delete( json );
    1575           0 :       return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1576           0 :     }
    1577             : 
    1578           0 :     int result = fd_guih_request_slot( gui, ws_conn_id, id, params );
    1579           0 :     cJSON_Delete( json );
    1580           0 :     return result;
    1581           0 :   } else if( FD_LIKELY( !strcmp( topic->valuestring, "slot" ) && !strcmp( key->valuestring, "query_detailed" ) ) ) {
    1582           0 :     const cJSON * params = cJSON_GetObjectItemCaseSensitive( json, "params" );
    1583           0 :     if( FD_UNLIKELY( !cJSON_IsObject( params ) ) ) {
    1584           0 :       cJSON_Delete( json );
    1585           0 :       return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1586           0 :     }
    1587             : 
    1588           0 :     int result = fd_guih_request_slot_detailed( gui, ws_conn_id, id, params );
    1589           0 :     cJSON_Delete( json );
    1590           0 :     return result;
    1591           0 :   } else if( FD_LIKELY( !strcmp( topic->valuestring, "slot" ) && !strcmp( key->valuestring, "query_transactions" ) ) ) {
    1592           0 :     const cJSON * params = cJSON_GetObjectItemCaseSensitive( json, "params" );
    1593           0 :     if( FD_UNLIKELY( !cJSON_IsObject( params ) ) ) {
    1594           0 :       cJSON_Delete( json );
    1595           0 :       return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1596           0 :     }
    1597             : 
    1598           0 :     int result = fd_guih_request_slot_transactions( gui, ws_conn_id, id, params );
    1599           0 :     cJSON_Delete( json );
    1600           0 :     return result;
    1601           0 :   } else if( FD_LIKELY( !strcmp( topic->valuestring, "slot" ) && !strcmp( key->valuestring, "query_rankings" ) ) ) {
    1602           0 :     const cJSON * params = cJSON_GetObjectItemCaseSensitive( json, "params" );
    1603           0 :     if( FD_UNLIKELY( !cJSON_IsObject( params ) ) ) {
    1604           0 :       cJSON_Delete( json );
    1605           0 :       return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1606           0 :     }
    1607             : 
    1608           0 :     int result = fd_guih_request_slot_rankings( gui, ws_conn_id, id, params );
    1609           0 :     cJSON_Delete( json );
    1610           0 :     return result;
    1611           0 :   } else if( FD_LIKELY( !strcmp( topic->valuestring, "slot" ) && !strcmp( key->valuestring, "query_shreds" ) ) ) {
    1612           0 :     const cJSON * params = cJSON_GetObjectItemCaseSensitive( json, "params" );
    1613           0 :     if( FD_UNLIKELY( !cJSON_IsObject( params ) ) ) {
    1614           0 :       cJSON_Delete( json );
    1615           0 :       return FD_HTTP_SERVER_CONNECTION_CLOSE_BAD_REQUEST;
    1616           0 :     }
    1617             : 
    1618           0 :     int result = fd_guih_request_slot_shreds( gui, ws_conn_id, id, params );
    1619           0 :     cJSON_Delete( json );
    1620           0 :     return result;
    1621           0 :   } else if( FD_LIKELY( !strcmp( topic->valuestring, "summary" ) && !strcmp( key->valuestring, "ping" ) ) ) {
    1622           0 :     fd_guih_printf_summary_ping( gui, id );
    1623           0 :     FD_TEST( !fd_http_server_ws_send( gui->http, ws_conn_id ) );
    1624             : 
    1625           0 :     cJSON_Delete( json );
    1626           0 :     return 0;
    1627           0 :   }
    1628             : 
    1629           0 :   cJSON_Delete( json );
    1630           0 :   return FD_HTTP_SERVER_CONNECTION_CLOSE_UNKNOWN_METHOD;
    1631           0 : }
    1632             : 
    1633             : static fd_guih_slot_t *
    1634             : fd_guih_clear_slot( fd_guih_t *      gui,
    1635             :                    ulong           _slot,
    1636           0 :                    ulong           _parent_slot ) {
    1637           0 :   fd_guih_slot_t * slot = gui->slots[ _slot % FD_GUIH_SLOTS_CNT ];
    1638             : 
    1639           0 :   int mine = 0;
    1640           0 :   ulong epoch_idx = 0UL;
    1641           0 :   for( ulong i=0UL; i<2UL; i++) {
    1642           0 :     if( FD_UNLIKELY( !gui->epoch.has_epoch[ i ] ) ) continue;
    1643           0 :     if( FD_LIKELY( _slot>=gui->epoch.epochs[ i ].start_slot && _slot<=gui->epoch.epochs[ i ].end_slot ) ) {
    1644           0 :       fd_pubkey_t const * slot_leader = fd_epoch_leaders_get( gui->epoch.epochs[ i ].lsched, _slot );
    1645           0 :       mine = !memcmp( slot_leader->uc, gui->summary.identity_key->uc, 32UL );
    1646           0 :       epoch_idx = i;
    1647           0 :       break;
    1648           0 :     }
    1649           0 :   }
    1650             : 
    1651           0 :   slot->slot                   = _slot;
    1652           0 :   slot->parent_slot            = _parent_slot;
    1653           0 :   slot->vote_slot              = ULONG_MAX;
    1654           0 :   slot->vote_latency           = UCHAR_MAX;
    1655           0 :   slot->reset_slot             = ULONG_MAX;
    1656           0 :   slot->max_compute_units      = UINT_MAX;
    1657           0 :   slot->completed_time         = LONG_MAX;
    1658           0 :   slot->mine                   = mine;
    1659           0 :   slot->skipped                = 0;
    1660           0 :   slot->must_republish         = 1;
    1661           0 :   slot->level                  = FD_GUIH_SLOT_LEVEL_INCOMPLETE;
    1662           0 :   slot->vote_failed            = UINT_MAX;
    1663           0 :   slot->vote_success           = UINT_MAX;
    1664           0 :   slot->nonvote_success        = UINT_MAX;
    1665           0 :   slot->nonvote_failed         = UINT_MAX;
    1666           0 :   slot->compute_units          = UINT_MAX;
    1667           0 :   slot->transaction_fee        = ULONG_MAX;
    1668           0 :   slot->priority_fee           = ULONG_MAX;
    1669           0 :   slot->tips                   = ULONG_MAX;
    1670           0 :   slot->shred_cnt              = UINT_MAX;
    1671           0 :   slot->shreds.start_offset    = ULONG_MAX;
    1672           0 :   slot->shreds.end_offset      = ULONG_MAX;
    1673             : 
    1674           0 :   if( FD_LIKELY( slot->mine ) ) {
    1675             :     /* All slots start off not skipped, until we see it get off the reset
    1676             :        chain. */
    1677           0 :     gui->epoch.epochs[ epoch_idx ].my_total_slots++;
    1678             : 
    1679           0 :     slot->leader_history_idx = gui->leader_slots_cnt++;
    1680           0 :     fd_guih_leader_slot_t * lslot = gui->leader_slots[ slot->leader_history_idx % FD_GUIH_LEADER_CNT ];
    1681             : 
    1682           0 :     lslot->slot                        = _slot;
    1683           0 :     memset( lslot->block_hash.uc, 0, sizeof(fd_hash_t) );
    1684           0 :     lslot->leader_start_time           = LONG_MAX;
    1685           0 :     lslot->leader_end_time             = LONG_MAX;
    1686           0 :     lslot->tile_timers_sample_cnt      = 0UL;
    1687           0 :     lslot->scheduler_counts_sample_cnt = 0UL;
    1688           0 :     lslot->txs.microblocks_upper_bound = UINT_MAX;
    1689           0 :     lslot->txs.begin_microblocks       = 0U;
    1690           0 :     lslot->txs.end_microblocks         = 0U;
    1691           0 :     lslot->txs.start_offset            = ULONG_MAX;
    1692           0 :     lslot->txs.end_offset              = ULONG_MAX;
    1693           0 :     lslot->max_microblocks             = ULONG_MAX;
    1694           0 :     lslot->unbecame_leader             = 0;
    1695           0 :   }
    1696             : 
    1697           0 :   if( FD_UNLIKELY( !_slot ) ) {
    1698             :     /* Slot 0 is always rooted */
    1699           0 :     slot->level   = FD_GUIH_SLOT_LEVEL_ROOTED;
    1700           0 :   }
    1701             : 
    1702           0 :   return slot;
    1703           0 : }
    1704             : 
    1705             : void
    1706             : fd_guih_handle_leader_schedule( fd_guih_t *                    gui,
    1707             :                                fd_stake_weight_msg_t const * leader_schedule,
    1708           0 :                                long                          now ) {
    1709           0 :   FD_TEST( leader_schedule->staked_vote_cnt<=MAX_COMPRESSED_STAKE_WEIGHTS );
    1710           0 :   FD_TEST( leader_schedule->slot_cnt<=MAX_SLOTS_PER_EPOCH );
    1711             : 
    1712           0 :   ulong idx = leader_schedule->epoch % 2UL;
    1713           0 :   gui->epoch.has_epoch[ idx ] = 1;
    1714             : 
    1715           0 :   gui->epoch.epochs[ idx ].epoch                      = leader_schedule->epoch;
    1716           0 :   gui->epoch.epochs[ idx ].start_slot                 = leader_schedule->start_slot;
    1717           0 :   gui->epoch.epochs[ idx ].end_slot                   = leader_schedule->start_slot + leader_schedule->slot_cnt - 1; // end_slot is inclusive.
    1718           0 :   gui->epoch.epochs[ idx ].target_slot_duration_nanos = leader_schedule->ns_per_slot;
    1719           0 :   gui->epoch.epochs[ idx ].my_total_slots             = 0UL;
    1720           0 :   gui->epoch.epochs[ idx ].my_skipped_slots           = 0UL;
    1721             : 
    1722           0 :   memset( gui->epoch.epochs[ idx ].rankings,    (int)(UINT_MAX), sizeof(gui->epoch.epochs[ idx ].rankings)    );
    1723           0 :   memset( gui->epoch.epochs[ idx ].my_rankings, (int)(UINT_MAX), sizeof(gui->epoch.epochs[ idx ].my_rankings) );
    1724             : 
    1725           0 :   gui->epoch.epochs[ idx ].rankings_slot = leader_schedule->start_slot;
    1726             : 
    1727           0 :   fd_vote_stake_weight_t const * stake_weights = fd_stake_weight_msg_stake_weights( leader_schedule );
    1728           0 :   fd_memcpy( gui->epoch.epochs[ idx ].stakes, stake_weights, leader_schedule->staked_vote_cnt*sizeof(fd_vote_stake_weight_t) );
    1729             : 
    1730           0 :   fd_epoch_leaders_delete( fd_epoch_leaders_leave( gui->epoch.epochs[ idx ].lsched ) );
    1731           0 :   gui->epoch.epochs[idx].lsched = fd_epoch_leaders_join( fd_epoch_leaders_new( gui->epoch.epochs[ idx ]._lsched,
    1732           0 :                                                                                leader_schedule->epoch,
    1733           0 :                                                                                gui->epoch.epochs[ idx ].start_slot,
    1734           0 :                                                                                leader_schedule->slot_cnt,
    1735           0 :                                                                                leader_schedule->staked_vote_cnt,
    1736           0 :                                                                                gui->epoch.epochs[ idx ].stakes,
    1737           0 :                                                                                0UL ) );
    1738             : 
    1739           0 :   if( FD_UNLIKELY( leader_schedule->start_slot==0UL ) ) {
    1740           0 :     gui->epoch.epochs[ 0 ].start_time = now;
    1741           0 :   } else {
    1742           0 :     gui->epoch.epochs[ idx ].start_time = LONG_MAX;
    1743             : 
    1744           0 :     for( ulong i=0UL; i<fd_ulong_min( leader_schedule->start_slot-1UL, FD_GUIH_SLOTS_CNT ); i++ ) {
    1745           0 :       fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, leader_schedule->start_slot-i );
    1746           0 :       if( FD_UNLIKELY( !slot ) ) break;
    1747           0 :       else if( FD_UNLIKELY( slot->skipped ) ) continue;
    1748             : 
    1749           0 :       gui->epoch.epochs[ idx ].start_time = slot->completed_time;
    1750           0 :       break;
    1751           0 :     }
    1752           0 :   }
    1753             : 
    1754           0 :   fd_guih_printf_epoch( gui, idx );
    1755           0 :   fd_http_server_ws_broadcast( gui->http );
    1756           0 : }
    1757             : 
    1758             : static void
    1759             : fd_guih_handle_slot_start( fd_guih_t * gui,
    1760             :                           ulong      _slot,
    1761             :                           ulong      parent_slot,
    1762           0 :                           long       now ) {
    1763           0 :   FD_TEST( gui->leader_slot==ULONG_MAX );
    1764           0 :   gui->leader_slot = _slot;
    1765             : 
    1766           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    1767           0 :   if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, _slot, parent_slot );
    1768             : 
    1769           0 :   fd_guih_tile_timers_snap( gui );
    1770           0 :   gui->summary.tile_timers_snap_idx_slot_start = (gui->summary.tile_timers_snap_idx+(FD_GUIH_TILE_TIMER_SNAP_CNT-1UL))%FD_GUIH_TILE_TIMER_SNAP_CNT;
    1771             : 
    1772           0 :   fd_guih_scheduler_counts_snap( gui, now );
    1773           0 :   gui->summary.scheduler_counts_snap_idx_slot_start = (gui->summary.scheduler_counts_snap_idx+(FD_GUIH_SCHEDULER_COUNT_SNAP_CNT-1UL))%FD_GUIH_SCHEDULER_COUNT_SNAP_CNT;
    1774             : 
    1775           0 :   fd_guih_txn_waterfall_t waterfall[ 1 ];
    1776           0 :   fd_guih_txn_waterfall_snap( gui, waterfall );
    1777           0 :   fd_guih_tile_stats_snap( gui, waterfall, slot->tile_stats_begin, now );
    1778           0 : }
    1779             : 
    1780             : static void
    1781             : fd_guih_handle_slot_end( fd_guih_t * gui,
    1782             :                         ulong      _slot,
    1783             :                         ulong      _cus_used,
    1784           0 :                         long       now ) {
    1785           0 :   if( FD_UNLIKELY( gui->leader_slot!=_slot ) ) {
    1786           0 :     FD_LOG_ERR(( "gui->leader_slot %lu _slot %lu", gui->leader_slot, _slot ));
    1787           0 :   }
    1788           0 :   gui->leader_slot = ULONG_MAX;
    1789             : 
    1790           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    1791           0 :   if( FD_UNLIKELY( !slot ) ) return;
    1792             : 
    1793           0 :   slot->compute_units = (uint)_cus_used;
    1794             : 
    1795           0 :   fd_guih_tile_timers_snap( gui );
    1796             : 
    1797           0 :   fd_guih_scheduler_counts_snap( gui, now );
    1798             : 
    1799           0 :   fd_guih_leader_slot_t * lslot = fd_guih_get_leader_slot( gui, _slot );
    1800           0 :   if( FD_LIKELY( lslot ) ) {
    1801           0 :     fd_rng_t rng[ 1 ];
    1802           0 :     fd_rng_new( rng, 0UL, 0UL);
    1803             : 
    1804           0 : #define DOWNSAMPLE( a, a_start, a_end, a_capacity, b, b_sz, stride ) (__extension__({  \
    1805           0 :   ulong __cnt = 0UL; \
    1806           0 :   ulong __rsz = (stride); \
    1807           0 :   ulong __a_sz = (fd_ulong_if( a_end<a_start, a_end+a_capacity, a_end )-a_start); \
    1808           0 :   if( FD_UNLIKELY( __a_sz && b_sz ) ) { \
    1809           0 :     for( ulong a_idx=0UL; a_idx<__a_sz && __cnt<b_sz; a_idx++ ) { \
    1810           0 :       if( FD_UNLIKELY( fd_rng_float_robust( rng ) > (float)(b_sz-__cnt) / (float)(__a_sz-__cnt) ) ) continue; \
    1811           0 :       fd_memcpy( (b) + __cnt * __rsz, (a) + ((a_start+a_idx)%a_capacity) * __rsz, __rsz * sizeof(*(b)) ); \
    1812           0 :       __cnt++; \
    1813           0 :     } \
    1814           0 :   } \
    1815           0 :   __cnt; }))
    1816             : 
    1817           0 :     lslot->tile_timers_sample_cnt = DOWNSAMPLE(
    1818           0 :       gui->summary.tile_timers_snap,
    1819           0 :       gui->summary.tile_timers_snap_idx_slot_start,
    1820           0 :       gui->summary.tile_timers_snap_idx,
    1821           0 :       FD_GUIH_TILE_TIMER_SNAP_CNT,
    1822           0 :       lslot->tile_timers,
    1823           0 :       FD_GUIH_TILE_TIMER_LEADER_DOWNSAMPLE_CNT,
    1824           0 :       gui->tile_cnt );
    1825             : 
    1826           0 :     lslot->scheduler_counts_sample_cnt = DOWNSAMPLE(
    1827           0 :       gui->summary.scheduler_counts_snap,
    1828           0 :       gui->summary.scheduler_counts_snap_idx_slot_start,
    1829           0 :       gui->summary.scheduler_counts_snap_idx,
    1830           0 :       FD_GUIH_SCHEDULER_COUNT_SNAP_CNT,
    1831           0 :       lslot->scheduler_counts,
    1832           0 :       FD_GUIH_SCHEDULER_COUNT_LEADER_DOWNSAMPLE_CNT,
    1833           0 :       1UL );
    1834           0 : #undef DOWNSAMPLE
    1835           0 :   }
    1836             : 
    1837             :   /* When a slot ends, snap the state of the waterfall and save it into
    1838             :      that slot, and also reset the reference counters to the end of the
    1839             :      slot. */
    1840             : 
    1841           0 :   fd_guih_txn_waterfall_snap( gui, slot->waterfall_end );
    1842           0 :   memcpy( slot->waterfall_begin, gui->summary.txn_waterfall_reference, sizeof(slot->waterfall_begin) );
    1843           0 :   memcpy( gui->summary.txn_waterfall_reference, slot->waterfall_end, sizeof(gui->summary.txn_waterfall_reference) );
    1844             : 
    1845           0 :   fd_guih_tile_stats_snap( gui, slot->waterfall_end, slot->tile_stats_end, now );
    1846           0 : }
    1847             : 
    1848             : static void
    1849             : fd_guih_handle_reset_slot_legacy( fd_guih_t *    gui,
    1850             :                                  uchar const * msg,
    1851           0 :                                  long          now ) {
    1852           0 :   ulong last_landed_vote = FD_LOAD( ulong, msg );
    1853             : 
    1854           0 :   ulong parent_cnt = FD_LOAD( ulong, msg + 8UL );
    1855           0 :   FD_TEST( parent_cnt<4096UL );
    1856             : 
    1857           0 :   ulong _slot = FD_LOAD( ulong, msg + 16UL );
    1858             : 
    1859           0 :   for( ulong i=0UL; i<parent_cnt; i++ ) {
    1860           0 :     ulong parent_slot = FD_LOAD( ulong, msg + (2UL+i)*8UL );
    1861           0 :     fd_guih_slot_t * slot = fd_guih_get_slot( gui, parent_slot );
    1862           0 :     if( FD_UNLIKELY( !slot ) ) {
    1863           0 :       ulong parent_parent_slot = ULONG_MAX;
    1864           0 :       if( FD_UNLIKELY( i!=parent_cnt-1UL) ) parent_parent_slot = FD_LOAD( ulong, msg + (3UL+i)*8UL );
    1865           0 :       fd_guih_clear_slot( gui, parent_slot, parent_parent_slot );
    1866           0 :     }
    1867           0 :   }
    1868             : 
    1869           0 :   if( FD_UNLIKELY( gui->summary.vote_distance!=_slot-last_landed_vote ) ) {
    1870           0 :     gui->summary.vote_distance = _slot-last_landed_vote;
    1871           0 :     fd_guih_printf_vote_distance( gui );
    1872           0 :     fd_http_server_ws_broadcast( gui->http );
    1873           0 :   }
    1874             : 
    1875           0 :   if( FD_LIKELY( gui->summary.vote_state!=FD_GUIH_VOTE_STATE_NON_VOTING ) ) {
    1876           0 :     if( FD_UNLIKELY( last_landed_vote==ULONG_MAX || (last_landed_vote+150UL)<_slot ) ) {
    1877           0 :       if( FD_UNLIKELY( gui->summary.vote_state!=FD_GUIH_VOTE_STATE_DELINQUENT ) ) {
    1878           0 :         gui->summary.vote_state = FD_GUIH_VOTE_STATE_DELINQUENT;
    1879           0 :         fd_guih_printf_vote_state( gui );
    1880           0 :         fd_http_server_ws_broadcast( gui->http );
    1881           0 :       }
    1882           0 :     } else {
    1883           0 :       if( FD_UNLIKELY( gui->summary.vote_state!=FD_GUIH_VOTE_STATE_VOTING ) ) {
    1884           0 :         gui->summary.vote_state = FD_GUIH_VOTE_STATE_VOTING;
    1885           0 :         fd_guih_printf_vote_state( gui );
    1886           0 :         fd_http_server_ws_broadcast( gui->http );
    1887           0 :       }
    1888           0 :     }
    1889           0 :   }
    1890             : 
    1891           0 :   ulong parent_slot_idx = 0UL;
    1892             : 
    1893           0 :   int republish_skip_rate[ 2 ] = {0};
    1894             : 
    1895           0 :   for( ulong i=0UL; i<fd_ulong_min( _slot+1, FD_GUIH_SLOTS_CNT ); i++ ) {
    1896           0 :     ulong parent_slot = _slot - i;
    1897             : 
    1898           0 :     fd_guih_slot_t * slot = fd_guih_get_slot( gui, parent_slot );
    1899           0 :     if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, parent_slot, ULONG_MAX );
    1900             : 
    1901             :     /* The chain of parents may stretch into already rooted slots if
    1902             :        they haven't been squashed yet, if we reach one of them we can
    1903             :        just exit, all the information prior to the root is already
    1904             :        correct. */
    1905             : 
    1906           0 :     if( FD_LIKELY( slot->level>=FD_GUIH_SLOT_LEVEL_ROOTED ) ) break;
    1907             : 
    1908           0 :     int should_republish = slot->must_republish;
    1909           0 :     slot->must_republish = 0;
    1910             : 
    1911           0 :     if( FD_UNLIKELY( parent_slot!=FD_LOAD( ulong, msg + (2UL+parent_slot_idx)*8UL ) ) ) {
    1912             :       /* We are between two parents in the rooted chain, which means
    1913             :          we were skipped. */
    1914           0 :       if( FD_UNLIKELY( !slot->skipped ) ) {
    1915           0 :         slot->skipped = 1;
    1916           0 :         should_republish = 1;
    1917           0 :         if( FD_LIKELY( slot->mine ) ) {
    1918           0 :           for( ulong i=0UL; i<2UL; i++ ) {
    1919           0 :             if( FD_LIKELY( parent_slot>=gui->epoch.epochs[ i ].start_slot && parent_slot<=gui->epoch.epochs[ i ].end_slot ) ) {
    1920           0 :               gui->epoch.epochs[ i ].my_skipped_slots++;
    1921           0 :               republish_skip_rate[ i ] = 1;
    1922           0 :               break;
    1923           0 :             }
    1924           0 :           }
    1925           0 :         }
    1926           0 :       }
    1927           0 :     } else {
    1928             :       /* Reached the next parent... */
    1929           0 :       if( FD_UNLIKELY( slot->skipped ) ) {
    1930           0 :         slot->skipped = 0;
    1931           0 :         should_republish = 1;
    1932           0 :         if( FD_LIKELY( slot->mine ) ) {
    1933           0 :           for( ulong i=0UL; i<2UL; i++ ) {
    1934           0 :             if( FD_LIKELY( parent_slot>=gui->epoch.epochs[ i ].start_slot && parent_slot<=gui->epoch.epochs[ i ].end_slot ) ) {
    1935           0 :               gui->epoch.epochs[ i ].my_skipped_slots--;
    1936           0 :               republish_skip_rate[ i ] = 1;
    1937           0 :               break;
    1938           0 :             }
    1939           0 :           }
    1940           0 :         }
    1941           0 :       }
    1942           0 :       parent_slot_idx++;
    1943           0 :     }
    1944             : 
    1945           0 :     if( FD_LIKELY( should_republish ) ) {
    1946           0 :       fd_guih_printf_slot( gui, parent_slot );
    1947           0 :       fd_http_server_ws_broadcast( gui->http );
    1948           0 :     }
    1949             : 
    1950             :     /* We reached the last parent in the chain, everything above this
    1951             :        must have already been rooted, so we can exit. */
    1952             : 
    1953           0 :     if( FD_UNLIKELY( parent_slot_idx>=parent_cnt ) ) break;
    1954           0 :   }
    1955             : 
    1956           0 :   ulong duration_sum = 0UL;
    1957           0 :   ulong slot_cnt = 0UL;
    1958             : 
    1959             :   /* If we've just caught up we should truncate our slot history to avoid including catch-up slots */
    1960           0 :   int just_caught_up = gui->summary.slot_caught_up!=ULONG_MAX && _slot>gui->summary.slot_caught_up && _slot<gui->summary.slot_caught_up+750UL;
    1961           0 :   ulong slot_duration_history_sz = fd_ulong_if( just_caught_up, _slot-gui->summary.slot_caught_up, 750UL );
    1962           0 :   for( ulong i=0UL; i<fd_ulong_min( _slot+1, slot_duration_history_sz ); i++ ) {
    1963           0 :     ulong parent_slot = _slot - i;
    1964             : 
    1965           0 :     fd_guih_slot_t const * slot = fd_guih_get_slot_const( gui, parent_slot );
    1966           0 :     if( FD_UNLIKELY( !slot) ) break;
    1967           0 :     if( FD_UNLIKELY( slot->slot!=parent_slot ) ) {
    1968           0 :       FD_LOG_ERR(( "_slot %lu i %lu we expect _slot-i %lu got slot->slot %lu", _slot, i, _slot-i, slot->slot ));
    1969           0 :     }
    1970             : 
    1971           0 :     ulong slot_duration = fd_guih_slot_duration( gui, slot );
    1972           0 :     if( FD_LIKELY( slot_duration!=ULONG_MAX ) ) {
    1973           0 :       duration_sum += slot_duration;
    1974           0 :       slot_cnt++;
    1975           0 :     }
    1976           0 :   }
    1977             : 
    1978           0 :   if( FD_LIKELY( slot_cnt>0 ) ) {
    1979           0 :     gui->summary.estimated_slot_duration_nanos = (ulong)(duration_sum / slot_cnt);
    1980           0 :     fd_guih_printf_estimated_slot_duration_nanos( gui );
    1981           0 :     fd_http_server_ws_broadcast( gui->http );
    1982           0 :   }
    1983             : 
    1984           0 :   if( FD_LIKELY( gui->summary.slot_completed==ULONG_MAX || _slot!=gui->summary.slot_completed ) ) {
    1985           0 :     gui->summary.slot_completed = _slot;
    1986           0 :     fd_guih_printf_completed_slot( gui );
    1987           0 :     fd_http_server_ws_broadcast( gui->http );
    1988             : 
    1989             :     /* Also update slot_turbine which could be larger than the max
    1990             :        turbine slot if we are leader */
    1991           0 :     if( FD_UNLIKELY( gui->summary.slots_max_turbine[ 0 ].slot!=ULONG_MAX && gui->summary.slot_completed!=ULONG_MAX && gui->summary.slot_completed>gui->summary.slots_max_turbine[ 0 ].slot ) ) {
    1992           0 :       fd_guih_try_insert_ephemeral_slot( gui->summary.slots_max_turbine, FD_GUIH_TURBINE_SLOT_HISTORY_SZ, gui->summary.slot_completed, now );
    1993           0 :     }
    1994             : 
    1995           0 :     int slot_turbine_hist_full = gui->summary.slots_max_turbine[ FD_GUIH_TURBINE_SLOT_HISTORY_SZ-1UL ].slot!=ULONG_MAX;
    1996           0 :     if( FD_UNLIKELY( gui->summary.slot_caught_up==ULONG_MAX && slot_turbine_hist_full && gui->summary.slots_max_turbine[ 0 ].slot < (gui->summary.slot_completed + 3UL) ) ) {
    1997           0 :       gui->summary.slot_caught_up = gui->summary.slot_completed + 4UL;
    1998             : 
    1999           0 :       fd_guih_printf_slot_caught_up( gui );
    2000           0 :       fd_http_server_ws_broadcast( gui->http );
    2001           0 :     }
    2002           0 :   }
    2003             : 
    2004           0 :   for( ulong i=0UL; i<2UL; i++ ) {
    2005           0 :     if( FD_LIKELY( republish_skip_rate[ i ] ) ) {
    2006           0 :       fd_guih_printf_skip_rate( gui, i );
    2007           0 :       fd_http_server_ws_broadcast( gui->http );
    2008           0 :     }
    2009           0 :   }
    2010           0 : }
    2011             : 
    2012             : static void
    2013             : fd_guih_handle_completed_slot( fd_guih_t *    gui,
    2014             :                               uchar const * msg,
    2015           0 :                               long          now ) {
    2016             : 
    2017             :   /* This is the slot used by frontend clients as the "startup slot". In
    2018             :      certain boot conditions, we don't receive this slot from Agave, so
    2019             :      we include a bit of a hacky assignment here to make sure it is
    2020             :      always present. */
    2021           0 :   if( FD_UNLIKELY( gui->summary.startup_progress.startup_ledger_max_slot==ULONG_MAX ) ) {
    2022           0 :     gui->summary.startup_progress.startup_ledger_max_slot = FD_LOAD( ulong, msg );
    2023           0 :   }
    2024             : 
    2025           0 :   ulong _slot                    = FD_LOAD( ulong, msg );
    2026           0 :   uint  total_txn_count          = (uint)FD_LOAD( ulong, msg + 1UL*8UL );
    2027           0 :   uint  nonvote_txn_count        = (uint)FD_LOAD( ulong, msg + 2UL*8UL );
    2028           0 :   uint  failed_txn_count         = (uint)FD_LOAD( ulong, msg + 3UL*8UL );
    2029           0 :   uint  nonvote_failed_txn_count = (uint)FD_LOAD( ulong, msg + 4UL*8UL );
    2030           0 :   uint  compute_units            = (uint)FD_LOAD( ulong, msg + 5UL*8UL );
    2031           0 :   ulong transaction_fee          = FD_LOAD( ulong, msg + 6UL*8UL );
    2032           0 :   ulong priority_fee             = FD_LOAD( ulong, msg + 7UL*8UL );
    2033           0 :   ulong tips                     = FD_LOAD( ulong, msg + 8UL*8UL );
    2034           0 :   ulong _parent_slot             = FD_LOAD( ulong, msg + 9UL*8UL );
    2035           0 :   ulong max_compute_units        = FD_LOAD( ulong, msg + 10UL*8UL );
    2036             : 
    2037           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    2038           0 :   if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, _slot, _parent_slot );
    2039             : 
    2040           0 :   slot->completed_time = now;
    2041           0 :   slot->parent_slot = _parent_slot;
    2042           0 :   slot->max_compute_units = (uint)max_compute_units;
    2043           0 :   if( FD_LIKELY( slot->level<FD_GUIH_SLOT_LEVEL_COMPLETED ) ) {
    2044             :     /* Typically a slot goes from INCOMPLETE to COMPLETED but it can
    2045             :        happen that it starts higher.  One such case is when we
    2046             :        optimistically confirm a higher slot that skips this one, but
    2047             :        then later we replay this one anyway to track the bank fork. */
    2048             : 
    2049           0 :     if( FD_LIKELY( gui->summary.slot_optimistically_confirmed!=ULONG_MAX && _slot<gui->summary.slot_optimistically_confirmed ) ) {
    2050             :       /* Cluster might have already optimistically confirmed by the time
    2051             :          we finish replaying it. */
    2052           0 :       slot->level = FD_GUIH_SLOT_LEVEL_OPTIMISTICALLY_CONFIRMED;
    2053           0 :     } else {
    2054           0 :       slot->level = FD_GUIH_SLOT_LEVEL_COMPLETED;
    2055           0 :     }
    2056           0 :   }
    2057             : 
    2058           0 :   slot->nonvote_success = nonvote_txn_count - nonvote_failed_txn_count;
    2059           0 :   slot->nonvote_failed  = nonvote_failed_txn_count;
    2060           0 :   slot->vote_failed     = failed_txn_count - nonvote_failed_txn_count;
    2061           0 :   slot->vote_success    = total_txn_count - nonvote_txn_count - slot->vote_failed;
    2062             : 
    2063           0 :   slot->transaction_fee        = transaction_fee;
    2064           0 :   slot->priority_fee           = priority_fee;
    2065           0 :   slot->tips                   = tips;
    2066             : 
    2067             :   /* In Frankendancer, CUs come from our own leader pipeline (the field
    2068             :      sent from the Agave codepath is zero'd out) */
    2069           0 :   slot->compute_units          = fd_uint_if( slot->mine, slot->compute_units, compute_units );
    2070             : 
    2071           0 :   if( FD_UNLIKELY( gui->epoch.has_epoch[ 0 ] && _slot==gui->epoch.epochs[ 0 ].end_slot ) ) {
    2072           0 :     gui->epoch.epochs[ 0 ].end_time = slot->completed_time;
    2073           0 :   } else if( FD_UNLIKELY( gui->epoch.has_epoch[ 1 ] && _slot==gui->epoch.epochs[ 1 ].end_slot ) ) {
    2074           0 :     gui->epoch.epochs[ 1 ].end_time = slot->completed_time;
    2075           0 :   }
    2076             : 
    2077             :   /* Broadcast new skip rate if one of our slots got completed. */
    2078           0 :   if( FD_LIKELY( slot->mine ) ) {
    2079           0 :     for( ulong i=0UL; i<2UL; i++ ) {
    2080           0 :       if( FD_LIKELY( _slot>=gui->epoch.epochs[ i ].start_slot && _slot<=gui->epoch.epochs[ i ].end_slot ) ) {
    2081           0 :         fd_guih_printf_skip_rate( gui, i );
    2082           0 :         fd_http_server_ws_broadcast( gui->http );
    2083           0 :         break;
    2084           0 :       }
    2085           0 :     }
    2086           0 :   }
    2087           0 : }
    2088             : 
    2089             : static void
    2090             : fd_guih_handle_rooted_slot_legacy( fd_guih_t *    gui,
    2091           0 :                                   uchar const * msg ) {
    2092           0 :   ulong _slot = FD_LOAD( ulong, msg );
    2093             : 
    2094             :   // FD_LOG_WARNING(( "Got rooted slot %lu", _slot ));
    2095             : 
    2096             :   /* Slot 0 is always rooted.  No need to iterate all the way back to
    2097             :      i==_slot */
    2098           0 :   for( ulong i=0UL; i<fd_ulong_min( _slot, FD_GUIH_SLOTS_CNT ); i++ ) {
    2099           0 :     ulong parent_slot = _slot - i;
    2100             : 
    2101           0 :     fd_guih_slot_t * slot = fd_guih_get_slot( gui, parent_slot );
    2102           0 :     if( FD_UNLIKELY( !slot ) ) break;
    2103             : 
    2104           0 :     if( FD_UNLIKELY( slot->slot!=parent_slot ) ) {
    2105           0 :       FD_LOG_ERR(( "_slot %lu i %lu we expect parent_slot %lu got slot->slot %lu", _slot, i, parent_slot, slot->slot ));
    2106           0 :     }
    2107           0 :     if( FD_UNLIKELY( slot->level>=FD_GUIH_SLOT_LEVEL_ROOTED ) ) break;
    2108             : 
    2109           0 :     slot->level = FD_GUIH_SLOT_LEVEL_ROOTED;
    2110           0 :     fd_guih_printf_slot( gui, parent_slot );
    2111           0 :     fd_http_server_ws_broadcast( gui->http );
    2112           0 :   }
    2113             : 
    2114           0 :   gui->summary.slot_rooted = _slot;
    2115           0 :   fd_guih_printf_root_slot( gui );
    2116           0 :   fd_http_server_ws_broadcast( gui->http );
    2117           0 : }
    2118             : 
    2119             : static void
    2120             : fd_guih_handle_optimistically_confirmed_slot( fd_guih_t * gui,
    2121           0 :                                              ulong      _slot ) {
    2122             :   /* Slot 0 is always rooted.  No need to iterate all the way back to
    2123             :      i==_slot */
    2124           0 :   for( ulong i=0UL; i<fd_ulong_min( _slot, FD_GUIH_SLOTS_CNT ); i++ ) {
    2125           0 :     ulong parent_slot = _slot - i;
    2126             : 
    2127           0 :     fd_guih_slot_t * slot = fd_guih_get_slot( gui, parent_slot );
    2128           0 :     if( FD_UNLIKELY( !slot) ) break;
    2129             : 
    2130           0 :     if( FD_UNLIKELY( slot->slot>parent_slot ) ) {
    2131           0 :       FD_LOG_ERR(( "_slot %lu i %lu we expect parent_slot %lu got slot->slot %lu", _slot, i, parent_slot, slot->slot ));
    2132           0 :     } else if( FD_UNLIKELY( slot->slot<parent_slot ) ) {
    2133             :       /* Slot not even replayed yet ... will come out as optimistically confirmed */
    2134           0 :       continue;
    2135           0 :     }
    2136           0 :     if( FD_UNLIKELY( slot->level>=FD_GUIH_SLOT_LEVEL_ROOTED ) ) break;
    2137             : 
    2138           0 :     if( FD_LIKELY( slot->level<FD_GUIH_SLOT_LEVEL_OPTIMISTICALLY_CONFIRMED ) ) {
    2139           0 :       slot->level = FD_GUIH_SLOT_LEVEL_OPTIMISTICALLY_CONFIRMED;
    2140           0 :       fd_guih_printf_slot( gui, parent_slot );
    2141           0 :       fd_http_server_ws_broadcast( gui->http );
    2142           0 :     }
    2143           0 :   }
    2144             : 
    2145           0 :   if( FD_UNLIKELY( gui->summary.slot_optimistically_confirmed!=ULONG_MAX && _slot<gui->summary.slot_optimistically_confirmed ) ) {
    2146             :     /* Optimistically confirmed slot went backwards ... mark some slots as no
    2147             :        longer optimistically confirmed. */
    2148           0 :     for( long i_=(long)gui->summary.slot_optimistically_confirmed; i_>=(long)_slot; i_-- ) {
    2149           0 :       ulong i = (ulong)i_;
    2150           0 :       fd_guih_slot_t * slot = fd_guih_get_slot( gui, i );
    2151           0 :       if( FD_UNLIKELY( !slot ) ) break;
    2152           0 :       if( FD_LIKELY( slot->slot==i ) ) {
    2153             :         /* It's possible for the optimistically confirmed slot to skip
    2154             :            backwards between two slots that we haven't yet replayed.  In
    2155             :            that case we don't need to change anything, since they will
    2156             :            get marked properly when they get completed. */
    2157           0 :         slot->level = FD_GUIH_SLOT_LEVEL_COMPLETED;
    2158           0 :         fd_guih_printf_slot( gui, i );
    2159           0 :         fd_http_server_ws_broadcast( gui->http );
    2160           0 :       }
    2161           0 :     }
    2162           0 :   }
    2163             : 
    2164           0 :   gui->summary.slot_optimistically_confirmed = _slot;
    2165           0 :   fd_guih_printf_optimistically_confirmed_slot( gui );
    2166           0 :   fd_http_server_ws_broadcast( gui->http );
    2167           0 : }
    2168             : 
    2169             : static void
    2170             : fd_guih_handle_balance_update( fd_guih_t *    gui,
    2171           0 :                               uchar const * msg ) {
    2172           0 :   switch( FD_LOAD( ulong, msg ) ) {
    2173           0 :     case 0UL:
    2174           0 :       gui->summary.identity_account_balance = FD_LOAD( ulong, msg + 8UL );
    2175           0 :       fd_guih_printf_identity_balance( gui );
    2176           0 :       fd_http_server_ws_broadcast( gui->http );
    2177           0 :       break;
    2178           0 :     case 1UL:
    2179           0 :       gui->summary.vote_account_balance = FD_LOAD( ulong, msg + 8UL );
    2180           0 :       fd_guih_printf_vote_balance( gui );
    2181           0 :       fd_http_server_ws_broadcast( gui->http );
    2182           0 :       break;
    2183           0 :     default:
    2184           0 :       FD_LOG_ERR(( "balance: unknown account type: %lu", FD_LOAD( ulong, msg ) ));
    2185           0 :   }
    2186           0 : }
    2187             : 
    2188             : static void
    2189             : fd_guih_handle_start_progress( fd_guih_t *    gui,
    2190           0 :                               uchar const * msg ) {
    2191           0 :   uchar type = msg[ 0 ];
    2192             : 
    2193           0 :   switch (type) {
    2194           0 :     case 0:
    2195           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_INITIALIZING;
    2196           0 :       FD_LOG_INFO(( "progress: initializing" ));
    2197           0 :       break;
    2198           0 :     case 1: {
    2199           0 :       char const * snapshot_type;
    2200           0 :       if( FD_UNLIKELY( gui->summary.startup_progress.startup_got_full_snapshot ) ) {
    2201           0 :         gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_SEARCHING_FOR_INCREMENTAL_SNAPSHOT;
    2202           0 :         snapshot_type = "incremental";
    2203           0 :       } else {
    2204           0 :         gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_SEARCHING_FOR_FULL_SNAPSHOT;
    2205           0 :         snapshot_type = "full";
    2206           0 :       }
    2207           0 :       FD_LOG_INFO(( "progress: searching for %s snapshot", snapshot_type ));
    2208           0 :       break;
    2209           0 :     }
    2210           0 :     case 2: {
    2211           0 :       uchar is_full_snapshot = msg[ 1 ];
    2212           0 :       if( FD_LIKELY( is_full_snapshot ) ) {
    2213           0 :           gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_DOWNLOADING_FULL_SNAPSHOT;
    2214           0 :           gui->summary.startup_progress.startup_full_snapshot_slot = FD_LOAD( ulong, msg + 2 );
    2215           0 :           gui->summary.startup_progress.startup_full_snapshot_peer_ip_addr = FD_LOAD( uint, msg + 10 );
    2216           0 :           gui->summary.startup_progress.startup_full_snapshot_peer_port = FD_LOAD( ushort, msg + 14 );
    2217           0 :           gui->summary.startup_progress.startup_full_snapshot_total_bytes = FD_LOAD( ulong, msg + 16 );
    2218           0 :           gui->summary.startup_progress.startup_full_snapshot_current_bytes = FD_LOAD( ulong, msg + 24 );
    2219           0 :           gui->summary.startup_progress.startup_full_snapshot_elapsed_secs = FD_LOAD( double, msg + 32 );
    2220           0 :           gui->summary.startup_progress.startup_full_snapshot_remaining_secs = FD_LOAD( double, msg + 40 );
    2221           0 :           gui->summary.startup_progress.startup_full_snapshot_throughput = FD_LOAD( double, msg + 48 );
    2222           0 :           FD_LOG_INFO(( "progress: downloading full snapshot: slot=%lu", gui->summary.startup_progress.startup_full_snapshot_slot ));
    2223           0 :       } else {
    2224           0 :           gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_DOWNLOADING_INCREMENTAL_SNAPSHOT;
    2225           0 :           gui->summary.startup_progress.startup_incremental_snapshot_slot = FD_LOAD( ulong, msg + 2 );
    2226           0 :           gui->summary.startup_progress.startup_incremental_snapshot_peer_ip_addr = FD_LOAD( uint, msg + 10 );
    2227           0 :           gui->summary.startup_progress.startup_incremental_snapshot_peer_port = FD_LOAD( ushort, msg + 14 );
    2228           0 :           gui->summary.startup_progress.startup_incremental_snapshot_total_bytes = FD_LOAD( ulong, msg + 16 );
    2229           0 :           gui->summary.startup_progress.startup_incremental_snapshot_current_bytes = FD_LOAD( ulong, msg + 24 );
    2230           0 :           gui->summary.startup_progress.startup_incremental_snapshot_elapsed_secs = FD_LOAD( double, msg + 32 );
    2231           0 :           gui->summary.startup_progress.startup_incremental_snapshot_remaining_secs = FD_LOAD( double, msg + 40 );
    2232           0 :           gui->summary.startup_progress.startup_incremental_snapshot_throughput = FD_LOAD( double, msg + 48 );
    2233           0 :           FD_LOG_INFO(( "progress: downloading incremental snapshot: slot=%lu", gui->summary.startup_progress.startup_incremental_snapshot_slot ));
    2234           0 :       }
    2235           0 :       break;
    2236           0 :     }
    2237           0 :     case 3: {
    2238           0 :       gui->summary.startup_progress.startup_got_full_snapshot = 1;
    2239           0 :       break;
    2240           0 :     }
    2241           0 :     case 4:
    2242           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_CLEANING_BLOCK_STORE;
    2243           0 :       FD_LOG_INFO(( "progress: cleaning block store" ));
    2244           0 :       break;
    2245           0 :     case 5:
    2246           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_CLEANING_ACCOUNTS;
    2247           0 :       FD_LOG_INFO(( "progress: cleaning accounts" ));
    2248           0 :       break;
    2249           0 :     case 6:
    2250           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_LOADING_LEDGER;
    2251           0 :       FD_LOG_INFO(( "progress: loading ledger" ));
    2252           0 :       break;
    2253           0 :     case 7: {
    2254           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_PROCESSING_LEDGER;
    2255           0 :       gui->summary.startup_progress.startup_ledger_slot = fd_ulong_load_8( msg + 1 );
    2256           0 :       gui->summary.startup_progress.startup_ledger_max_slot = fd_ulong_load_8( msg + 9 );
    2257           0 :       FD_LOG_INFO(( "progress: processing ledger: slot=%lu, max_slot=%lu", gui->summary.startup_progress.startup_ledger_slot, gui->summary.startup_progress.startup_ledger_max_slot ));
    2258           0 :       break;
    2259           0 :     }
    2260           0 :     case 8:
    2261           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_STARTING_SERVICES;
    2262           0 :       FD_LOG_INFO(( "progress: starting services" ));
    2263           0 :       break;
    2264           0 :     case 9:
    2265           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_HALTED;
    2266           0 :       FD_LOG_INFO(( "progress: halted" ));
    2267           0 :       break;
    2268           0 :     case 10: {
    2269           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_WAITING_FOR_SUPERMAJORITY;
    2270           0 :       gui->summary.startup_progress.startup_waiting_for_supermajority_slot = fd_ulong_load_8( msg + 1 );
    2271           0 :       gui->summary.startup_progress.startup_waiting_for_supermajority_stake_pct = fd_ulong_load_8( msg + 9 );
    2272           0 :       FD_LOG_INFO(( "progress: waiting for supermajority: slot=%lu, gossip_stake_percent=%lu", gui->summary.startup_progress.startup_waiting_for_supermajority_slot, gui->summary.startup_progress.startup_waiting_for_supermajority_stake_pct ));
    2273           0 :       break;
    2274           0 :     }
    2275           0 :     case 11:
    2276           0 :       gui->summary.startup_progress.phase = FD_GUIH_START_PROGRESS_TYPE_RUNNING;
    2277           0 :       FD_LOG_INFO(( "progress: running" ));
    2278           0 :       break;
    2279           0 :     default:
    2280           0 :       FD_LOG_ERR(( "progress: unknown type: %u", type ));
    2281           0 :   }
    2282             : 
    2283           0 :   fd_guih_printf_startup_progress( gui );
    2284           0 :   fd_http_server_ws_broadcast( gui->http );
    2285           0 : }
    2286             : 
    2287             : void
    2288             : fd_guih_handle_genesis_hash( fd_guih_t *        gui,
    2289           0 :                             fd_hash_t const * msg ) {
    2290           0 :   FD_BASE58_ENCODE_32_BYTES( msg->uc, hash_cstr );
    2291           0 :   ulong cluster = fd_genesis_cluster_identify(hash_cstr);
    2292           0 :   char const * cluster_name = fd_genesis_cluster_name(cluster);
    2293             : 
    2294           0 :   if( FD_LIKELY( strcmp( gui->summary.cluster, cluster_name ) ) ) {
    2295           0 :     gui->summary.cluster = fd_genesis_cluster_name(cluster);
    2296           0 :     fd_guih_printf_cluster( gui );
    2297           0 :     fd_http_server_ws_broadcast( gui->http );
    2298           0 :   }
    2299           0 : }
    2300             : 
    2301             : void
    2302             : fd_guih_handle_block_engine_update( fd_guih_t *                              gui,
    2303           0 :                                    fd_bundle_block_engine_update_t const * update ) {
    2304           0 :   gui->block_engine.has_block_engine = 1;
    2305             : 
    2306             :   /* copy strings and ensure null termination within bounds */
    2307           0 :   FD_TEST( fd_cstr_nlen( update->name,    sizeof(gui->block_engine.name   ) ) < sizeof(gui->block_engine.name   ) );
    2308           0 :   FD_TEST( fd_cstr_nlen( update->url,     sizeof(gui->block_engine.url    ) ) < sizeof(gui->block_engine.url    ) );
    2309           0 :   FD_TEST( fd_cstr_nlen( update->ip_cstr, sizeof(gui->block_engine.ip_cstr) ) < sizeof(gui->block_engine.ip_cstr) );
    2310           0 :   ulong name_len    = fd_cstr_nlen( update->name,    sizeof(gui->block_engine.name   ) );
    2311           0 :   ulong url_len     = fd_cstr_nlen( update->url,     sizeof(gui->block_engine.url    ) );
    2312           0 :   ulong ip_cstr_len = fd_cstr_nlen( update->ip_cstr, sizeof(gui->block_engine.ip_cstr) );
    2313           0 :   fd_memcpy( gui->block_engine.name,    update->name,    name_len+1UL );
    2314           0 :   fd_memcpy( gui->block_engine.url,     update->url,     url_len+1UL );
    2315           0 :   fd_memcpy( gui->block_engine.ip_cstr, update->ip_cstr, ip_cstr_len+1UL );
    2316             : 
    2317           0 :   fd_guih_printf_block_engine( gui );
    2318           0 :   fd_http_server_ws_broadcast( gui->http );
    2319           0 : }
    2320             : 
    2321             : void
    2322             : fd_guih_plugin_message( fd_guih_t *   gui,
    2323             :                        ulong        plugin_msg,
    2324             :                        void const * msg,
    2325           0 :                        long         now ) {
    2326             : 
    2327           0 :   switch( plugin_msg ) {
    2328           0 :     case FD_PLUGIN_MSG_SLOT_ROOTED:
    2329           0 :       fd_guih_handle_rooted_slot_legacy( gui, msg );
    2330           0 :       break;
    2331           0 :     case FD_PLUGIN_MSG_SLOT_OPTIMISTICALLY_CONFIRMED:
    2332           0 :       fd_guih_handle_optimistically_confirmed_slot( gui, FD_LOAD( ulong, msg ) );
    2333           0 :       break;
    2334           0 :     case FD_PLUGIN_MSG_SLOT_COMPLETED: {
    2335           0 :       fd_guih_handle_completed_slot( gui, msg, now );
    2336           0 :       break;
    2337           0 :     }
    2338           0 :     case FD_PLUGIN_MSG_LEADER_SCHEDULE: {
    2339           0 :       FD_STATIC_ASSERT( sizeof(fd_stake_weight_msg_t)==7*sizeof(ulong), "new fields breaks things" );
    2340           0 :       fd_guih_handle_leader_schedule( gui, (fd_stake_weight_msg_t *)msg, now );
    2341           0 :       break;
    2342           0 :     }
    2343           0 :     case FD_PLUGIN_MSG_SLOT_START: {
    2344           0 :       ulong slot = FD_LOAD( ulong, msg );
    2345           0 :       ulong parent_slot = FD_LOAD( ulong, (uchar const *)msg + 8UL );
    2346           0 :       fd_guih_handle_slot_start( gui, slot, parent_slot, now );
    2347           0 :       break;
    2348           0 :     }
    2349           0 :     case FD_PLUGIN_MSG_SLOT_END: {
    2350           0 :       ulong slot = FD_LOAD( ulong, msg );
    2351           0 :       ulong cus_used = FD_LOAD( ulong, (uchar const *)msg + 8UL );
    2352           0 :       fd_guih_handle_slot_end( gui, slot, cus_used, now );
    2353           0 :       break;
    2354           0 :     }
    2355           0 :     case FD_PLUGIN_MSG_GOSSIP_UPDATE: {
    2356           0 :       fd_guih_handle_gossip_update( gui, msg );
    2357           0 :       break;
    2358           0 :     }
    2359           0 :     case FD_PLUGIN_MSG_VOTE_ACCOUNT_UPDATE: {
    2360           0 :       fd_guih_handle_vote_account_update( gui, msg );
    2361           0 :       break;
    2362           0 :     }
    2363           0 :     case FD_PLUGIN_MSG_VALIDATOR_INFO: {
    2364           0 :       fd_guih_handle_validator_info_update( gui, msg );
    2365           0 :       break;
    2366           0 :     }
    2367           0 :     case FD_PLUGIN_MSG_SLOT_RESET: {
    2368           0 :       fd_guih_handle_reset_slot_legacy( gui, msg, now );
    2369           0 :       break;
    2370           0 :     }
    2371           0 :     case FD_PLUGIN_MSG_BALANCE: {
    2372           0 :       fd_guih_handle_balance_update( gui, msg );
    2373           0 :       break;
    2374           0 :     }
    2375           0 :     case FD_PLUGIN_MSG_START_PROGRESS: {
    2376           0 :       fd_guih_handle_start_progress( gui, msg );
    2377           0 :       break;
    2378           0 :     }
    2379           0 :     case FD_PLUGIN_MSG_GENESIS_HASH_KNOWN: {
    2380           0 :       fd_guih_handle_genesis_hash( gui, msg );
    2381           0 :       break;
    2382           0 :     }
    2383           0 :     default:
    2384           0 :       FD_LOG_ERR(( "Unhandled plugin msg: 0x%lx", plugin_msg ));
    2385           0 :       break;
    2386           0 :   }
    2387           0 : }
    2388             : 
    2389             : void
    2390             : fd_guih_became_leader( fd_guih_t * gui,
    2391             :                       ulong      _slot,
    2392             :                       long       start_time_nanos,
    2393             :                       long       end_time_nanos,
    2394             :                       ulong      max_compute_units,
    2395           0 :                       ulong      max_microblocks ) {
    2396           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    2397           0 :   if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, _slot, ULONG_MAX );
    2398           0 :   fd_guih_leader_slot_t * lslot = fd_guih_get_leader_slot( gui, _slot );
    2399           0 :   if( FD_UNLIKELY( !lslot ) ) return;
    2400             : 
    2401           0 :   slot->max_compute_units = (uint)max_compute_units;
    2402           0 :   lslot->leader_start_time = fd_long_if( lslot->leader_start_time==LONG_MAX, start_time_nanos, lslot->leader_start_time );
    2403           0 :   lslot->leader_end_time   = end_time_nanos;
    2404           0 :   lslot->max_microblocks   = max_microblocks;
    2405           0 :   if( FD_LIKELY( lslot->txs.microblocks_upper_bound==UINT_MAX ) ) lslot->txs.microblocks_upper_bound = (uint)max_microblocks;
    2406           0 : }
    2407             : 
    2408             : void
    2409             : fd_guih_unbecame_leader( fd_guih_t *                gui,
    2410             :                         ulong                     _slot,
    2411             :                         fd_done_packing_t const * done_packing,
    2412           0 :                         long                      now FD_PARAM_UNUSED ) {
    2413           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    2414           0 :   if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, _slot, ULONG_MAX );
    2415           0 :   fd_guih_leader_slot_t * lslot = fd_guih_get_leader_slot( gui, _slot );
    2416           0 :   if( FD_LIKELY( !lslot ) ) return;
    2417           0 :   lslot->txs.microblocks_upper_bound = (uint)done_packing->microblocks_in_slot;
    2418           0 :   fd_memcpy( lslot->scheduler_stats, done_packing, sizeof(fd_done_packing_t) );
    2419             : 
    2420           0 :   lslot->unbecame_leader = 1;
    2421           0 : }
    2422             : 
    2423             : void
    2424             : fd_guih_microblock_execution_begin( fd_guih_t *   gui,
    2425             :                                    long         tspub_ns,
    2426             :                                    ulong        _slot,
    2427             :                                    fd_txn_e_t * txns,
    2428             :                                    ulong        txn_cnt,
    2429             :                                    uint         microblock_idx,
    2430           0 :                                    ulong        pack_txn_idx ) {
    2431           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    2432           0 :   if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, _slot, ULONG_MAX );
    2433             : 
    2434           0 :   fd_guih_leader_slot_t * lslot = fd_guih_get_leader_slot( gui, _slot );
    2435           0 :   if( FD_UNLIKELY( !lslot ) ) return;
    2436             : 
    2437           0 :   lslot->leader_start_time = fd_long_if( lslot->leader_start_time==LONG_MAX, tspub_ns, lslot->leader_start_time );
    2438             : 
    2439           0 :   if( FD_UNLIKELY( lslot->txs.start_offset==ULONG_MAX ) ) lslot->txs.start_offset = pack_txn_idx;
    2440           0 :   else                                                    lslot->txs.start_offset = fd_ulong_min( lslot->txs.start_offset, pack_txn_idx );
    2441             : 
    2442           0 :   gui->pack_txn_idx = fd_ulong_max( gui->pack_txn_idx, pack_txn_idx+txn_cnt-1UL );
    2443             : 
    2444           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
    2445           0 :     fd_txn_p_t * txn_payload = txns[ i ].txnp;
    2446           0 :     fd_txn_t * txn = TXN( txn_payload );
    2447             : 
    2448           0 :     ulong sig_rewards = FD_PACK_FEE_PER_SIGNATURE * txn->signature_cnt;
    2449           0 :     ulong priority_rewards                    = ULONG_MAX;
    2450           0 :     ulong requested_execution_cus             = ULONG_MAX;
    2451           0 :     ulong precompile_sigs                     = ULONG_MAX;
    2452           0 :     ulong requested_loaded_accounts_data_cost = ULONG_MAX;
    2453           0 :     ulong allocated_data                      = ULONG_MAX;
    2454           0 :     uint _flags = 0U;
    2455           0 :     ulong cost_estimate = fd_pack_compute_cost( txn, txn_payload->payload, &_flags, &requested_execution_cus, &priority_rewards, &precompile_sigs, &requested_loaded_accounts_data_cost, &allocated_data );
    2456           0 :     sig_rewards += FD_PACK_FEE_PER_SIGNATURE * precompile_sigs;
    2457           0 :     sig_rewards = sig_rewards * FD_PACK_TXN_FEE_BURN_PCT / 100UL;
    2458             : 
    2459           0 :     fd_guih_txn_t * txn_entry = gui->txs[ (pack_txn_idx + i)%FD_GUIH_TXN_HISTORY_SZ ];
    2460             : 
    2461             :     /* If execution_end already ran for this txn, the arrival timestamp
    2462             :        it stamped will match.  Preserve all existing flags. Otherwise
    2463             :        the entry is stale from a ring buffer wrap-around, so clear all
    2464             :        flags. */
    2465           0 :     if( FD_LIKELY( txn_entry->timestamp_arrival_nanos!=txn_payload->scheduler_arrival_time_nanos ) ) txn_entry->flags = (uchar)0;
    2466             : 
    2467           0 :     fd_memcpy( txn_entry->signature, txn_payload->payload + txn->signature_off, FD_SHA512_HASH_SZ );
    2468           0 :     txn_entry->timestamp_arrival_nanos     = txn_payload->scheduler_arrival_time_nanos;
    2469           0 :     txn_entry->compute_units_requested     = cost_estimate & 0x1FFFFFU;
    2470           0 :     txn_entry->priority_fee                = priority_rewards;
    2471           0 :     txn_entry->transaction_fee             = sig_rewards;
    2472           0 :     txn_entry->microblock_start_ns_dt      = (float)(tspub_ns - lslot->leader_start_time);
    2473           0 :     txn_entry->source_ipv4                 = txn_payload->source_ipv4;
    2474           0 :     txn_entry->source_tpu                  = txn_payload->source_tpu;
    2475           0 :     txn_entry->microblock_idx              = microblock_idx;
    2476           0 :     txn_entry->flags                      |= (uchar)FD_GUIH_TXN_FLAGS_STARTED;
    2477           0 :     txn_entry->flags                      |= (uchar)fd_uint_if( !!(txn_payload->flags & FD_TXN_P_FLAGS_IS_SIMPLE_VOTE), FD_GUIH_TXN_FLAGS_IS_SIMPLE_VOTE, 0U );
    2478           0 :     txn_entry->flags                      |= (uchar)fd_uint_if( (txn_payload->flags & FD_TXN_P_FLAGS_BUNDLE) || (txn_payload->flags & FD_TXN_P_FLAGS_INITIALIZER_BUNDLE), FD_GUIH_TXN_FLAGS_FROM_BUNDLE, 0U );
    2479           0 :   }
    2480             : 
    2481             :   /* At the moment, bank publishes at most 1 transaction per microblock,
    2482             :      even if it received microblocks with multiple transactions
    2483             :      (i.e. a bundle). This means that we need to calculate microblock
    2484             :      count here based on the transaction count. */
    2485           0 :   lslot->txs.begin_microblocks += (uint)txn_cnt;
    2486           0 : }
    2487             : 
    2488             : void
    2489             : fd_guih_microblock_execution_end( fd_guih_t *     gui,
    2490             :                                  long           tspub_ns,
    2491             :                                  ulong          bank_idx,
    2492             :                                  ulong          _slot,
    2493             :                                  ulong          txn_cnt,
    2494             :                                  fd_txn_p_t *   txns,
    2495             :                                  ulong          pack_txn_idx,
    2496             :                                  fd_txn_ns_dt_t txn_ns_dt,
    2497           0 :                                  ulong          tips ) {
    2498           0 :   if( FD_UNLIKELY( 1UL!=txn_cnt ) ) FD_LOG_ERR(( "gui expects 1 txn per microblock from bank, found %lu", txn_cnt ));
    2499             : 
    2500           0 :   fd_guih_slot_t * slot = fd_guih_get_slot( gui, _slot );
    2501           0 :   if( FD_UNLIKELY( !slot ) ) slot = fd_guih_clear_slot( gui, _slot, ULONG_MAX );
    2502             : 
    2503           0 :   fd_guih_leader_slot_t * lslot = fd_guih_get_leader_slot( gui, _slot );
    2504           0 :   if( FD_UNLIKELY( !lslot ) ) return;
    2505             : 
    2506           0 :   lslot->leader_start_time = fd_long_if( lslot->leader_start_time==LONG_MAX, tspub_ns, lslot->leader_start_time );
    2507             : 
    2508           0 :   if( FD_UNLIKELY( lslot->txs.end_offset==ULONG_MAX ) ) lslot->txs.end_offset = pack_txn_idx + txn_cnt;
    2509           0 :   else                                                  lslot->txs.end_offset = fd_ulong_max( lslot->txs.end_offset, pack_txn_idx+txn_cnt );
    2510             : 
    2511           0 :   gui->pack_txn_idx = fd_ulong_max( gui->pack_txn_idx, pack_txn_idx+txn_cnt-1UL );
    2512             : 
    2513           0 :   for( ulong i=0UL; i<txn_cnt; i++ ) {
    2514           0 :     fd_txn_p_t * txn_p = &txns[ i ];
    2515             : 
    2516           0 :     fd_guih_txn_t * txn_entry = gui->txs[ (pack_txn_idx + i)%FD_GUIH_TXN_HISTORY_SZ ];
    2517             : 
    2518             :     /* If execution_begin already ran for this txn, the arrival
    2519             :        timestamp it stamped will match.  Preserve all existing flags.
    2520             :        Otherwise the entry is stale from a ring buffer wrap-around, so
    2521             :        clear all flags. */
    2522           0 :     if( FD_UNLIKELY( txn_entry->timestamp_arrival_nanos!=txn_p->scheduler_arrival_time_nanos ) ) txn_entry->flags = (uchar)0;
    2523             : 
    2524           0 :     txn_entry->timestamp_arrival_nanos   = txn_p->scheduler_arrival_time_nanos;
    2525           0 :     txn_entry->bank_idx                  = bank_idx                             & 0x3FU;
    2526           0 :     txn_entry->compute_units_consumed    = txn_p->execle_cu.actual_consumed_cus & 0x1FFFFFU;
    2527           0 :     txn_entry->error_code                = (txn_p->flags >> 24)                 & 0x3FU;
    2528           0 :     txn_entry->microblock_end_ns_dt      = (float)(tspub_ns - lslot->leader_start_time);
    2529           0 :     txn_entry->txn_ns_dt                 = txn_ns_dt;
    2530           0 :     txn_entry->tips                      = tips;
    2531           0 :     txn_entry->flags                    |= (uchar)FD_GUIH_TXN_FLAGS_ENDED;
    2532           0 :     txn_entry->flags                    &= (uchar)(~(uchar)FD_GUIH_TXN_FLAGS_LANDED_IN_BLOCK);
    2533           0 :     txn_entry->flags                    |= (uchar)fd_uint_if( !!(txn_p->flags & FD_TXN_P_FLAGS_EXECUTE_SUCCESS), FD_GUIH_TXN_FLAGS_LANDED_IN_BLOCK, 0U );
    2534           0 :   }
    2535             : 
    2536           0 :   lslot->txs.end_microblocks = lslot->txs.end_microblocks + (uint)txn_cnt;
    2537           0 : }

Generated by: LCOV version 1.14