Skip to content

Commit 4d6fdc7

Browse files
committed
replay: make should become leader checks faster
1 parent 029b63c commit 4d6fdc7

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

src/discof/replay/fd_replay_tile.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,13 @@ struct fd_replay_tile {
339339
int recv_poh;
340340
int recv_block_id;
341341
ulong next_leader_slot;
342+
long next_leader_tickcount;
342343
ulong highwater_leader_slot;
343344
ulong reset_slot;
344345
fd_hash_t reset_block_id;
345346
long reset_timestamp_nanos;
346347
double slot_duration_nanos;
348+
double slot_duration_ticks;
347349
ulong max_active_descendant;
348350
fd_bank_t * leader_bank; /* ==NULL if not currently the leader */
349351

@@ -1084,18 +1086,15 @@ init_after_snapshot( fd_replay_tile_t * ctx ) {
10841086
if( FD_UNLIKELY( ctx->capture_ctx ) ) fd_solcap_writer_flush( ctx->capture_ctx->capture );
10851087
}
10861088

1087-
static int
1089+
static inline int
10881090
maybe_become_leader( fd_replay_tile_t * ctx,
10891091
fd_stem_context_t * stem ) {
10901092
FD_TEST( ctx->is_booted );
1091-
if( FD_UNLIKELY( ctx->replay_out->idx==ULONG_MAX ) ) return 0;
1092-
if( FD_UNLIKELY( ctx->is_leader || ctx->next_leader_slot==ULONG_MAX ) ) return 0;
1093-
if( FD_UNLIKELY( !ctx->has_identity_vote_rooted ) ) return 0;
1093+
if( FD_LIKELY( ctx->next_leader_slot==ULONG_MAX || ctx->is_leader || !ctx->has_identity_vote_rooted || ctx->replay_out->idx==ULONG_MAX ) ) return 0;
10941094

10951095
FD_TEST( ctx->next_leader_slot>ctx->reset_slot );
1096-
long now = fd_log_wallclock();
1097-
long next_leader_timestamp = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_nanos) + ctx->reset_timestamp_nanos;
1098-
if( FD_UNLIKELY( now<next_leader_timestamp ) ) return 0;
1096+
long now = fd_tickcount();
1097+
if( FD_LIKELY( now<ctx->next_leader_tickcount ) ) return 0;
10991098

11001099
/* TODO:
11011100
if( FD_UNLIKELY( ctx->halted_switching_key ) ) return 0; */
@@ -1106,7 +1105,7 @@ maybe_become_leader( fd_replay_tile_t * ctx,
11061105
is a reasonable default here, although any value between 0 and 1.6
11071106
seconds could be considered reasonable. This is arbitrary and
11081107
chosen due to intuition. */
1109-
if( FD_UNLIKELY( now<next_leader_timestamp+(long)(3.0*ctx->slot_duration_nanos) ) ) {
1108+
if( FD_UNLIKELY( now<ctx->next_leader_tickcount+(long)(3.0*ctx->slot_duration_ticks) ) ) {
11101109
/* If the max_active_descendant is >= next_leader_slot, we waited
11111110
too long and a leader after us started publishing to try and skip
11121111
us. Just start our leader slot immediately, we might win ... */
@@ -1176,6 +1175,7 @@ maybe_become_leader( fd_replay_tile_t * ctx,
11761175
ctx->replay_out->chunk = fd_dcache_compact_next( ctx->replay_out->chunk, sizeof(fd_became_leader_t), ctx->replay_out->chunk0, ctx->replay_out->wmark );
11771176

11781177
ctx->next_leader_slot = ULONG_MAX;
1178+
ctx->next_leader_tickcount = LONG_MAX;
11791179

11801180
return 1;
11811181
}
@@ -1295,6 +1295,11 @@ boot_genesis( fd_replay_tile_t * ctx,
12951295
ctx->reset_slot = 0UL;
12961296
ctx->reset_timestamp_nanos = fd_log_wallclock();
12971297
ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
1298+
if( FD_LIKELY( ctx->next_leader_slot ) ) {
1299+
ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1300+
} else {
1301+
ctx->next_leader_tickcount = LONG_MAX;
1302+
}
12981303

12991304
ctx->is_booted = 1;
13001305
maybe_become_leader( ctx, stem );
@@ -1366,6 +1371,11 @@ on_snapshot_message( fd_replay_tile_t * ctx,
13661371
ctx->reset_slot = snapshot_slot;
13671372
ctx->reset_timestamp_nanos = fd_log_wallclock();
13681373
ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, 1UL, ctx->identity_pubkey );
1374+
if( FD_LIKELY( ctx->next_leader_slot ) ) {
1375+
ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1376+
} else {
1377+
ctx->next_leader_tickcount = LONG_MAX;
1378+
}
13691379

13701380
fd_sched_block_add_done( ctx->sched, bank->idx, ULONG_MAX );
13711381
FD_TEST( bank->idx==0UL );
@@ -1877,6 +1887,11 @@ process_tower_update( fd_replay_tile_t * ctx,
18771887
ctx->reset_timestamp_nanos = fd_log_wallclock();
18781888
ulong min_leader_slot = fd_ulong_max( msg->reset_slot+1UL, fd_ulong_if( ctx->highwater_leader_slot==ULONG_MAX, 0UL, ctx->highwater_leader_slot+1UL ) );
18791889
ctx->next_leader_slot = fd_multi_epoch_leaders_get_next_slot( ctx->mleaders, min_leader_slot, ctx->identity_pubkey );
1890+
if( FD_LIKELY( ctx->next_leader_slot ) ) {
1891+
ctx->next_leader_tickcount = (long)((double)(ctx->next_leader_slot-ctx->reset_slot-1UL)*ctx->slot_duration_ticks) + fd_tickcount();
1892+
} else {
1893+
ctx->next_leader_tickcount = LONG_MAX;
1894+
}
18801895

18811896
fd_block_id_ele_t * block_id_ele = fd_block_id_map_ele_query( ctx->block_id_map, &msg->reset_block_id, NULL, ctx->block_id_arr );
18821897
if( FD_UNLIKELY( !block_id_ele ) ) {
@@ -2214,8 +2229,10 @@ unprivileged_init( fd_topo_t * topo,
22142229
ctx->reset_block_id = (fd_hash_t){ .ul[0] = FD_RUNTIME_INITIAL_BLOCK_ID };
22152230
ctx->reset_timestamp_nanos = 0UL;
22162231
ctx->next_leader_slot = ULONG_MAX;
2232+
ctx->next_leader_tickcount = LONG_MAX;
22172233
ctx->highwater_leader_slot = ULONG_MAX;
22182234
ctx->slot_duration_nanos = 400L*1000L*1000L; /* TODO: Not fixed ... not always 400ms ... */
2235+
ctx->slot_duration_ticks = (double)ctx->slot_duration_nanos*fd_tempo_tick_per_ns( NULL );
22192236
ctx->max_active_descendant = 0UL; /* TODO: Update this properly ... */
22202237
ctx->leader_bank = NULL;
22212238

0 commit comments

Comments
 (0)