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