Skip to content

Commit fee45c1

Browse files
committed
tower: basic tower fork printer tool
1 parent a9a06f8 commit fee45c1

File tree

6 files changed

+195
-1
lines changed

6 files changed

+195
-1
lines changed

src/app/firedancer-dev/Local.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ $(call add-objs,commands/sim,fd_firedancer_dev)
1515
$(call add-objs,commands/backtest,fd_firedancer_dev)
1616
$(call add-objs,commands/snapshot_load,fd_firedancer_dev)
1717
$(call add-objs,commands/repair,fd_firedancer_dev)
18+
$(call add-objs,commands/tower,fd_firedancer_dev)
1819
$(call add-objs,commands/ipecho_server,fd_firedancer_dev)
1920
$(call add-objs,commands/gossip_dump,fd_firedancer_dev)
2021

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/* The tower command prints the tower forks tree structure and leaves.
2+
This is a standalone application that can be run to inspect the tower
3+
tile's fork structure. */
4+
5+
#include "../../shared/fd_config.h" /* config_t */
6+
#include "../../shared_dev/commands/dev.h"
7+
#include "../../../discof/tower/fd_tower_tile.c"
8+
#include "../../../choreo/tower/fd_tower_forks.h"
9+
10+
#include <stdio.h>
11+
#include <unistd.h>
12+
13+
fd_topo_run_tile_t
14+
fdctl_tile_run( fd_topo_tile_t const * tile );
15+
16+
/* ctx_t is defined in fd_tower_tile.c, we just need to access it */
17+
18+
static void
19+
tower_ctx_wksp( args_t * args,
20+
config_t * config,
21+
ctx_t ** tower_ctx,
22+
fd_topo_wksp_t ** tower_wksp ) {
23+
(void)args;
24+
25+
fd_topo_t * topo = &config->topo;
26+
27+
ulong tile_id = fd_topo_find_tile( topo, "tower", 0UL );
28+
if( FD_UNLIKELY( tile_id==ULONG_MAX ) ) FD_LOG_ERR(( "tower tile not found" ));
29+
30+
fd_topo_tile_t * tile = &topo->tiles[ tile_id ];
31+
32+
/* Get the workspace that contains the tile's scratch memory */
33+
ulong scratch_wksp_id = topo->objs[ tile->tile_obj_id ].wksp_id;
34+
if( FD_UNLIKELY( scratch_wksp_id>=topo->wksp_cnt ) ) FD_LOG_ERR(( "invalid workspace id %lu for tile scratch", scratch_wksp_id ));
35+
36+
fd_topo_wksp_t * _tower_wksp = &topo->workspaces[ scratch_wksp_id ];
37+
fd_topo_join_workspace( topo, _tower_wksp, FD_SHMEM_JOIN_MODE_READ_ONLY );
38+
39+
/* Access the tower tile scratch memory where tower_tile_ctx is stored */
40+
void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
41+
if( FD_UNLIKELY( !scratch ) ) FD_LOG_ERR(( "Failed to access tower tile scratch memory" ));
42+
43+
FD_SCRATCH_ALLOC_INIT( l, scratch );
44+
ctx_t * _tower_ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(ctx_t), sizeof(ctx_t) );
45+
46+
*tower_ctx = _tower_ctx;
47+
*tower_wksp = _tower_wksp;
48+
}
49+
50+
static void
51+
print_all_forks( fd_wksp_t * wksp, ctx_t * tower_ctx, fd_forks_t * forks ) {
52+
printf( "\n[Tower Forks]\n" );
53+
printf( "=============\n" );
54+
printf( "%-15s | %-15s | %-10s | %-10s\n", "Slot", "Parent Slot", "Voted", "Confirmed" );
55+
printf( "%-15s-+-%-15s-+-%-10s-+-%-10s\n", "---------------", "---------------", "----------", "----------" );
56+
57+
/* Iterate through all map slots */
58+
ulong tower_forks_gaddr = fd_wksp_gaddr_fast( tower_ctx->wksp, forks->tower_forks );
59+
fd_tower_forks_t * map = (fd_tower_forks_t *)fd_wksp_laddr_fast( wksp, tower_forks_gaddr );
60+
ulong slot_count = 0;
61+
62+
for( ulong slot_idx = 0UL; slot_idx < fd_tower_forks_slot_cnt( map ); slot_idx++ ) {
63+
fd_tower_forks_t * fork = &map[ slot_idx ];
64+
/* Check if key is valid (not MAP_KEY_NULL which is ULONG_MAX) */
65+
if( !fd_tower_forks_key_inval( fork->slot ) ) {
66+
printf( "%-15lu | ", fork->slot );
67+
if( fork->parent_slot == ULONG_MAX ) {
68+
printf( "%-15s | ", "NULL" );
69+
} else {
70+
printf( "%-15lu | ", fork->parent_slot );
71+
}
72+
printf( "%-10s | ", fork->voted ? "Yes" : "No" );
73+
printf( "%-10s\n", fork->confirmed ? "Yes" : "No" );
74+
slot_count++;
75+
}
76+
}
77+
78+
printf( "\n[Tower Leaves]\n" );
79+
printf( "==============\n" );
80+
81+
ulong tower_leaves_dlist_gaddr = fd_wksp_gaddr_fast( tower_ctx->wksp, forks->tower_leaves_dlist );
82+
fd_tower_leaves_dlist_t * leaves_dlist = (fd_tower_leaves_dlist_t *)fd_wksp_laddr_fast( wksp, tower_leaves_dlist_gaddr );
83+
ulong tower_leaves_pool_gaddr = fd_wksp_gaddr_fast( tower_ctx->wksp, forks->tower_leaves_pool );
84+
fd_tower_leaf_t * leaves_pool = (fd_tower_leaf_t *)fd_wksp_laddr_fast( wksp, tower_leaves_pool_gaddr );
85+
86+
ulong leaf_count = 0;
87+
for( fd_tower_leaves_dlist_iter_t iter = fd_tower_leaves_dlist_iter_fwd_init( leaves_dlist, leaves_pool );
88+
!fd_tower_leaves_dlist_iter_done( iter, leaves_dlist, leaves_pool );
89+
iter = fd_tower_leaves_dlist_iter_fwd_next( iter, leaves_dlist, leaves_pool ) ) {
90+
fd_tower_leaf_t * leaf = fd_tower_leaves_dlist_iter_ele( iter, leaves_dlist, leaves_pool );
91+
if( FD_LIKELY( leaf ) ) {
92+
fd_tower_forks_t * fork = fd_tower_forks_query( map, leaf->slot, NULL );
93+
printf( "Leaf slot: %lu", leaf->slot );
94+
if( fork->voted ) printf( " [voted]" );
95+
if( fork->confirmed ) printf( " [confirmed]" );
96+
printf( "\n" );
97+
leaf_count++;
98+
}
99+
}
100+
printf( "\nTotal leaves: %lu\n", leaf_count );
101+
printf( "Total slots: %lu\n", slot_count );
102+
printf( "\n" );
103+
}
104+
105+
static void
106+
tower_cmd_fn_forks( args_t * args,
107+
config_t * config ) {
108+
ctx_t * tower_ctx;
109+
fd_topo_wksp_t * tower_wksp;
110+
tower_ctx_wksp( args, config, &tower_ctx, &tower_wksp );
111+
112+
ulong forks_laddr = fd_wksp_gaddr_fast( tower_ctx->wksp, tower_ctx->forks );
113+
fd_forks_t * forks = (fd_forks_t *)fd_wksp_laddr( tower_wksp->wksp, forks_laddr );
114+
115+
for( ;; ) {
116+
print_all_forks( tower_wksp->wksp, tower_ctx, forks );
117+
sleep( 1 );
118+
}
119+
}
120+
121+
static const char * HELP =
122+
"\n\n"
123+
"usage: tower [-h] {forks}\n"
124+
"\n"
125+
"positional arguments:\n"
126+
" {forks}\n"
127+
" forks prints the tower forks tree structure and leaves\n"
128+
"\n"
129+
"optional arguments:\n"
130+
" -h, --help show this help message and exit\n";
131+
132+
static const char * FORKS_HELP =
133+
"\n\n"
134+
"usage: tower forks [-h]\n"
135+
"\n"
136+
"optional arguments:\n"
137+
" -h, --help show this help message and exit\n";
138+
139+
void
140+
tower_cmd_help( char const * arg ) {
141+
if ( FD_LIKELY( !arg ) ) FD_LOG_NOTICE(( "%s", HELP ));
142+
else if ( FD_LIKELY( !strcmp( arg, "forks" ) ) ) FD_LOG_NOTICE(( "%s", FORKS_HELP ));
143+
else FD_LOG_NOTICE(( "%s", HELP ));
144+
}
145+
146+
void
147+
tower_cmd_args( int * pargc,
148+
char *** pargv,
149+
args_t * args ) {
150+
151+
/* help */
152+
args->tower.help = fd_env_strip_cmdline_contains( pargc, pargv, "--help" );
153+
args->tower.help = args->tower.help || fd_env_strip_cmdline_contains( pargc, pargv, "-h" );
154+
155+
/* positional arg */
156+
args->tower.pos_arg = (*pargv)[0];
157+
if( FD_UNLIKELY( !args->tower.pos_arg ) ) {
158+
args->tower.help = 1;
159+
return;
160+
}
161+
162+
(*pargc)--;
163+
}
164+
165+
static void
166+
tower_cmd_fn( args_t * args,
167+
config_t * config ) {
168+
169+
if( args->tower.help ) {
170+
tower_cmd_help( args->tower.pos_arg );
171+
return;
172+
}
173+
174+
if ( !strcmp( args->tower.pos_arg, "forks" ) ) tower_cmd_fn_forks( args, config );
175+
else tower_cmd_help( NULL );
176+
}
177+
178+
action_t fd_action_tower = {
179+
.name = "tower",
180+
.args = tower_cmd_args,
181+
.fn = tower_cmd_fn,
182+
.perm = dev_cmd_perm,
183+
};

src/app/firedancer-dev/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ extern action_t fd_action_sim;
203203
extern action_t fd_action_backtest;
204204
extern action_t fd_action_snapshot_load;
205205
extern action_t fd_action_repair;
206+
extern action_t fd_action_tower;
206207
extern action_t fd_action_shred_version;
207208
extern action_t fd_action_ipecho_server;
208209
extern action_t fd_action_send_test;
@@ -238,6 +239,7 @@ action_t * ACTIONS[] = {
238239
&fd_action_backtest,
239240
&fd_action_snapshot_load,
240241
&fd_action_repair,
242+
&fd_action_tower,
241243
&fd_action_shred_version,
242244
&fd_action_ipecho_server,
243245
&fd_action_send_test,

src/app/shared/fd_action.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ union fdctl_args {
147147
ulong max_contact;
148148
int compact_mode;
149149
} gossip;
150+
151+
struct {
152+
char const * pos_arg;
153+
int help;
154+
} tower;
150155
};
151156

152157
typedef union fdctl_args args_t;

src/discof/replay/fd_replay_tile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2173,7 +2173,7 @@ maybe_verify_shred_version( fd_replay_tile_t * ctx ) {
21732173
xor = fd_ushort_if( xor<USHORT_MAX, (ushort)(xor + 1), USHORT_MAX );
21742174

21752175
if( FD_UNLIKELY( expected_shred_version!=xor ) ) {
2176-
FD_LOG_ERR(( "shred version mismatch: expected %u but got %u from genesis hash %s and hard forks", expected_shred_version, xor, FD_BASE58_ENC_32_ALLOCA( &ctx->genesis_hash ) ));
2176+
FD_LOG_WARNING(( "shred version mismatch: expected %u but got %u from genesis hash %s and hard forks", expected_shred_version, xor, FD_BASE58_ENC_32_ALLOCA( &ctx->genesis_hash ) ));
21772177
}
21782178
}
21792179
}

src/discof/tower/fd_tower_tile.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ typedef struct {
7777
} in_ctx_t;
7878

7979
typedef struct {
80+
fd_wksp_t * wksp; /* workspace */
81+
8082
ulong seed; /* map seed */
8183
int checkpt_fd;
8284
int restore_fd;
@@ -788,6 +790,7 @@ unprivileged_init( fd_topo_t * topo,
788790
void * notif = FD_SCRATCH_ALLOC_APPEND( l, notif_align(), notif_footprint( slot_max ) );
789791
FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
790792

793+
ctx->wksp = topo->workspaces[ topo->objs[ tile->tile_obj_id ].wksp_id ].wksp;
791794
ctx->ghost = fd_ghost_join ( fd_ghost_new ( ghost, 2*slot_max, FD_VOTER_MAX, 42UL ) ); /* FIXME seed */
792795
ctx->hfork = fd_hfork_join ( fd_hfork_new ( hfork, slot_max, FD_VOTER_MAX, ctx->seed, tile->tower.hard_fork_fatal ) );
793796
ctx->notar = fd_notar_join ( fd_notar_new ( notar, tile->tower.max_vote_lookahead ) );

0 commit comments

Comments
 (0)