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