Line data Source code
1 : #include "../../disco/topo/fd_topo.h"
2 : #include "../../disco/metrics/fd_metrics.h"
3 : #include "../../ballet/lthash/fd_lthash.h"
4 : #include "../../flamenco/runtime/fd_hashes.h"
5 :
6 : #include "utils/fd_ssctrl.h"
7 :
8 : #include "generated/fd_snapls_tile_seccomp.h"
9 :
10 : #define NAME "snapls"
11 :
12 0 : #define IN_KIND_SNAPIN (0)
13 0 : #define IN_KIND_SNAPLA (1)
14 : #define MAX_IN_LINKS (1 + FD_SNAPSHOT_MAX_SNAPLA_TILES)
15 :
16 : struct fd_snapls_tile {
17 : int state;
18 : int full;
19 :
20 : fd_lthash_value_t running_lthash;
21 :
22 : fd_blake3_t b3[1];
23 : ulong acc_data_sz;
24 : int hash_account;
25 : ulong num_hash_tiles;
26 :
27 : uchar in_kind[ MAX_IN_LINKS ];
28 : ulong adder_in_offset;
29 :
30 : ulong pending_ctrl_sig;
31 : ulong num_acks;
32 : uchar acks[ 1 + FD_SNAPSHOT_MAX_SNAPLA_TILES ];
33 :
34 : struct {
35 : fd_lthash_value_t expected_lthash;
36 : fd_lthash_value_t calculated_lthash;
37 : } hash_accum;
38 :
39 : struct {
40 : uchar pubkey[ FD_HASH_FOOTPRINT ];
41 : uchar owner[ FD_HASH_FOOTPRINT ];
42 : ulong data_len;
43 : int executable;
44 : } account_hdr;
45 :
46 : struct {
47 : struct {
48 : ulong accounts_hashed;
49 : } full;
50 :
51 : struct {
52 : ulong accounts_hashed;
53 : } incremental;
54 : } metrics;
55 :
56 : struct {
57 : fd_wksp_t * wksp;
58 : ulong chunk0;
59 : ulong wmark;
60 : ulong mtu;
61 : ulong pos;
62 : } in;
63 :
64 : struct {
65 : fd_wksp_t * wksp;
66 : ulong chunk0;
67 : ulong wmark;
68 : ulong mtu;
69 : } adder_in[ FD_SNAPSHOT_MAX_SNAPLA_TILES ];
70 : };
71 :
72 : typedef struct fd_snapls_tile fd_snapls_tile_t;
73 :
74 : static inline int
75 0 : should_shutdown( fd_snapls_tile_t * ctx ) {
76 0 : return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
77 0 : }
78 :
79 : static ulong
80 0 : scratch_align( void ) {
81 0 : return alignof(fd_snapls_tile_t);
82 0 : }
83 :
84 : static ulong
85 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
86 0 : (void)tile;
87 0 : ulong l = FD_LAYOUT_INIT;
88 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_snapls_tile_t), sizeof(fd_snapls_tile_t) );
89 0 : return FD_LAYOUT_FINI( l, alignof(fd_snapls_tile_t) );
90 0 : }
91 :
92 : static void
93 0 : metrics_write( fd_snapls_tile_t * ctx ) {
94 0 : FD_MGAUGE_SET( SNAPLS, FULL_ACCOUNTS_HASHED, ctx->metrics.full.accounts_hashed );
95 0 : FD_MGAUGE_SET( SNAPLS, INCREMENTAL_ACCOUNTS_HASHED, ctx->metrics.incremental.accounts_hashed );
96 0 : FD_MGAUGE_SET( SNAPLS, STATE, (ulong)(ctx->state) );
97 0 : }
98 :
99 : static void
100 : transition_malformed( fd_snapls_tile_t * ctx,
101 0 : fd_stem_context_t * stem ) {
102 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
103 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
104 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
105 0 : }
106 :
107 : static void
108 : handle_data_frag( fd_snapls_tile_t * ctx,
109 : ulong sig,
110 : ulong chunk,
111 0 : ulong sz ) {
112 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
113 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
114 :
115 0 : switch( sig ) {
116 0 : case FD_SNAPSHOT_HASH_MSG_SUB: {
117 0 : fd_snapshot_full_account_t const * prev_acc = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
118 :
119 0 : fd_lthash_value_t prev_lthash[1];
120 0 : fd_hashes_account_lthash_simple( prev_acc->hdr.pubkey,
121 0 : prev_acc->hdr.owner,
122 0 : prev_acc->hdr.lamports,
123 0 : prev_acc->hdr.executable,
124 0 : prev_acc->data,
125 0 : prev_acc->hdr.data_len,
126 0 : prev_lthash );
127 0 : fd_lthash_add( &ctx->running_lthash, prev_lthash );
128 :
129 0 : if( FD_LIKELY( ctx->full ) ) ctx->metrics.full.accounts_hashed++;
130 0 : else ctx->metrics.incremental.accounts_hashed++;
131 0 : break;
132 0 : }
133 0 : case FD_SNAPSHOT_HASH_MSG_SUB_HDR: {
134 0 : fd_snapshot_account_hdr_t const * acc = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
135 :
136 0 : if( acc->lamports!=0UL ) {
137 0 : ctx->hash_account = 1;
138 0 : fd_blake3_init( ctx->b3 );
139 0 : fd_blake3_append( ctx->b3, &acc->lamports, sizeof(ulong) );
140 0 : ctx->account_hdr.data_len = acc->data_len;
141 0 : ctx->account_hdr.executable = acc->executable;
142 0 : memcpy( ctx->account_hdr.owner, acc->owner, FD_HASH_FOOTPRINT );
143 0 : memcpy( ctx->account_hdr.pubkey, acc->pubkey, FD_HASH_FOOTPRINT );
144 0 : }
145 0 : break;
146 0 : }
147 0 : case FD_SNAPSHOT_HASH_MSG_SUB_DATA: {
148 0 : if( FD_LIKELY( !ctx->hash_account ) ) break;
149 :
150 0 : uchar const * acc_data = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
151 0 : fd_blake3_append( ctx->b3, acc_data, sz );
152 0 : ctx->acc_data_sz += sz;
153 0 : break;
154 0 : }
155 0 : default:
156 0 : FD_LOG_ERR(( "unexpected sig %lu in handle_data_frag", sig ));
157 0 : return;
158 0 : }
159 :
160 0 : if( FD_LIKELY( ctx->hash_account && ctx->acc_data_sz==ctx->account_hdr.data_len ) ) {
161 0 : fd_lthash_value_t account_lthash[1];
162 0 : fd_lthash_zero( account_lthash );
163 :
164 0 : uchar executable_flag = ctx->account_hdr.executable & 0x1;
165 0 : fd_blake3_append( ctx->b3, &executable_flag, sizeof(uchar) );
166 0 : fd_blake3_append( ctx->b3, ctx->account_hdr.owner, FD_HASH_FOOTPRINT );
167 0 : fd_blake3_append( ctx->b3, ctx->account_hdr.pubkey, FD_HASH_FOOTPRINT );
168 0 : fd_blake3_fini_2048( ctx->b3, account_lthash->bytes );
169 0 : fd_lthash_add( &ctx->running_lthash, account_lthash );
170 :
171 0 : ctx->acc_data_sz = 0UL;
172 0 : ctx->hash_account = 0;
173 :
174 0 : if( FD_LIKELY( ctx->full ) ) ctx->metrics.full.accounts_hashed++;
175 0 : else ctx->metrics.incremental.accounts_hashed++;
176 0 : }
177 0 : }
178 :
179 : static int
180 : recv_acks( fd_snapls_tile_t * ctx,
181 : ulong in_idx,
182 0 : ulong sig ) {
183 0 : if( FD_UNLIKELY( !ctx->pending_ctrl_sig ) ) {
184 0 : ctx->pending_ctrl_sig = sig;
185 0 : ctx->num_acks = 0UL;
186 0 : fd_memset( ctx->acks, 0, sizeof(ctx->acks) );
187 0 : } else FD_TEST( ctx->pending_ctrl_sig==sig );
188 :
189 0 : FD_TEST( ctx->acks[ in_idx ]==0 );
190 0 : ctx->acks[ in_idx ] = 1;
191 0 : ctx->num_acks++;
192 :
193 0 : FD_TEST( ctx->num_acks<=1UL+ctx->num_hash_tiles );
194 0 : if( FD_LIKELY( ctx->num_acks<1UL+ctx->num_hash_tiles ) ) return 0;
195 :
196 0 : ctx->pending_ctrl_sig = 0UL;
197 0 : ctx->num_acks = 0UL;
198 0 : fd_memset( ctx->acks, 0, sizeof(ctx->acks) );
199 0 : return 1;
200 0 : }
201 :
202 : static void
203 : handle_control_frag( fd_snapls_tile_t * ctx,
204 : fd_stem_context_t * stem,
205 : ulong sig,
206 0 : ulong in_idx ) {
207 0 : switch( sig ) {
208 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
209 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:
210 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE || ctx->state==FD_SNAPSHOT_STATE_ERROR );
211 0 : if( !recv_acks( ctx, in_idx, sig ) ) return;
212 0 : if( ctx->state==FD_SNAPSHOT_STATE_IDLE ) {
213 0 : ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
214 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
215 0 : fd_lthash_zero( &ctx->running_lthash );
216 0 : }
217 0 : break;
218 :
219 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL:
220 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_MSG_CTRL_SHUTDOWN );
221 0 : if( !recv_acks( ctx, in_idx, sig ) ) return;
222 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
223 0 : break;
224 :
225 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
226 0 : case FD_SNAPSHOT_MSG_CTRL_DONE:
227 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ||
228 0 : ctx->state==FD_SNAPSHOT_STATE_ERROR );
229 0 : if( !recv_acks( ctx, in_idx, sig ) ) return;
230 0 : if( FD_LIKELY( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ) ) {
231 0 : fd_lthash_sub( &ctx->hash_accum.calculated_lthash, &ctx->running_lthash );
232 0 : if( FD_UNLIKELY( memcmp( &ctx->hash_accum.expected_lthash, &ctx->hash_accum.calculated_lthash, sizeof(fd_lthash_value_t) ) ) ) {
233 0 : FD_LOG_WARNING(( "calculated accounts lthash %s does not match accounts lthash %s in snapshot manifest",
234 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.calculated_lthash ),
235 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.expected_lthash ) ));
236 0 : transition_malformed( ctx, stem );
237 0 : break;
238 0 : } else {
239 0 : FD_LOG_NOTICE(( "calculated accounts lthash %s matches accounts lthash %s in snapshot manifest",
240 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.calculated_lthash ),
241 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.expected_lthash ) ));
242 0 : }
243 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
244 0 : }
245 0 : break;
246 :
247 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
248 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
249 0 : if( !recv_acks( ctx, in_idx, sig ) ) return;
250 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
251 0 : break;
252 :
253 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR:
254 0 : transition_malformed( ctx, stem );
255 0 : return;
256 :
257 0 : default:
258 0 : FD_LOG_ERR(( "unexpected control sig %lu", sig ));
259 0 : return;
260 0 : }
261 :
262 : /* Forward the control message down the pipeline */
263 0 : fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
264 0 : }
265 :
266 : static void
267 : handle_hash_frag( fd_snapls_tile_t * ctx,
268 : ulong in_idx,
269 : ulong sig,
270 : ulong chunk,
271 0 : ulong sz ) {
272 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
273 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
274 0 : switch( sig ) {
275 0 : case FD_SNAPSHOT_HASH_MSG_RESULT_ADD: {
276 0 : FD_TEST( sz==sizeof(fd_lthash_value_t) );
277 0 : fd_lthash_value_t const * result = fd_chunk_to_laddr_const( ctx->adder_in[ in_idx-ctx->adder_in_offset ].wksp, chunk );
278 0 : fd_lthash_add( &ctx->hash_accum.calculated_lthash, result );
279 0 : break;
280 0 : }
281 0 : case FD_SNAPSHOT_HASH_MSG_EXPECTED: {
282 0 : FD_TEST( sz==sizeof(fd_lthash_value_t) );
283 0 : FD_TEST( ctx->in_kind[ in_idx ]==IN_KIND_SNAPIN );
284 0 : fd_lthash_value_t const * result = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
285 0 : ctx->hash_accum.expected_lthash = *result;
286 0 : break;
287 0 : }
288 0 : default:
289 0 : FD_LOG_ERR(( "unexpected hash sig %lu", sig ));
290 0 : break;
291 0 : }
292 0 : }
293 :
294 : static inline int
295 : returnable_frag( fd_snapls_tile_t * ctx,
296 : ulong in_idx FD_PARAM_UNUSED,
297 : ulong seq FD_PARAM_UNUSED,
298 : ulong sig,
299 : ulong chunk,
300 : ulong sz,
301 : ulong ctl FD_PARAM_UNUSED,
302 : ulong tsorig FD_PARAM_UNUSED,
303 : ulong tspub FD_PARAM_UNUSED,
304 0 : fd_stem_context_t * stem ) {
305 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
306 :
307 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_HASH_MSG_SUB ||
308 0 : sig==FD_SNAPSHOT_HASH_MSG_SUB_HDR ||
309 0 : sig==FD_SNAPSHOT_HASH_MSG_SUB_DATA ) ) handle_data_frag( ctx, sig, chunk, sz );
310 0 : else if( FD_LIKELY( sig==FD_SNAPSHOT_HASH_MSG_RESULT_ADD ||
311 0 : sig==FD_SNAPSHOT_HASH_MSG_EXPECTED ) ) handle_hash_frag( ctx, in_idx, sig, chunk, sz );
312 0 : else handle_control_frag( ctx, stem, sig, in_idx );
313 :
314 0 : return 0;
315 0 : }
316 :
317 : static ulong
318 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
319 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
320 : ulong out_fds_cnt,
321 0 : int * out_fds ) {
322 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
323 :
324 0 : ulong out_cnt = 0;
325 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
326 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
327 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
328 0 : }
329 :
330 0 : return out_cnt;
331 0 : }
332 :
333 : static ulong
334 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
335 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
336 : ulong out_cnt,
337 0 : struct sock_filter * out ) {
338 0 : populate_sock_filter_policy_fd_snapls_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
339 0 : return sock_filter_policy_fd_snapls_tile_instr_cnt;
340 0 : }
341 :
342 : static void
343 : unprivileged_init( fd_topo_t * topo,
344 0 : fd_topo_tile_t * tile ) {
345 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
346 :
347 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
348 0 : fd_snapls_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapls_tile_t), sizeof(fd_snapls_tile_t) );
349 :
350 0 : ulong expected_in_cnt = 1UL + fd_topo_tile_name_cnt( topo, "snapla" );
351 0 : if( FD_UNLIKELY( tile->in_cnt!=expected_in_cnt ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected %lu", tile->in_cnt, expected_in_cnt ));
352 0 : if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
353 :
354 0 : ulong adder_idx = 0UL;
355 0 : for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
356 0 : fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ i ] ];
357 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
358 0 : if( FD_LIKELY( 0==strcmp( in_link->name, "snapin_ls" ) ) ) {
359 0 : ctx->in.wksp = in_wksp->wksp;;
360 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
361 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
362 0 : ctx->in.mtu = in_link->mtu;
363 0 : ctx->in.pos = 0UL;
364 0 : ctx->in_kind[ i ] = IN_KIND_SNAPIN;
365 0 : } else if( FD_LIKELY( 0==strcmp( in_link->name, "snapla_ls" ) ) ) {
366 0 : ctx->adder_in[ adder_idx ].wksp = in_wksp->wksp;
367 0 : ctx->adder_in[ adder_idx ].chunk0 = fd_dcache_compact_chunk0( ctx->adder_in[ adder_idx ].wksp, in_link->dcache );
368 0 : ctx->adder_in[ adder_idx ].wmark = fd_dcache_compact_wmark ( ctx->adder_in[ adder_idx ].wksp, in_link->dcache, in_link->mtu );
369 0 : ctx->adder_in[ adder_idx ].mtu = in_link->mtu;
370 0 : ctx->in_kind[ i ] = IN_KIND_SNAPLA;
371 0 : if( FD_LIKELY( adder_idx==0UL ) ) ctx->adder_in_offset = i;
372 0 : adder_idx++;
373 0 : } else {
374 0 : FD_LOG_ERR(( "tile `" NAME "` has unexpected in link name `%s`", in_link->name ));
375 0 : }
376 0 : }
377 :
378 0 : fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ 0UL ] ];
379 0 : FD_TEST( 0==strcmp( out_link->name, "snapls_ct" ) );
380 :
381 0 : ctx->metrics.full.accounts_hashed = 0UL;
382 0 : ctx->metrics.incremental.accounts_hashed = 0UL;
383 :
384 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
385 0 : ctx->full = 1;
386 0 : ctx->hash_account = 0;
387 :
388 0 : ctx->num_hash_tiles = fd_topo_tile_name_cnt( topo, "snapla" );
389 :
390 0 : ctx->pending_ctrl_sig = 0UL;
391 0 : ctx->num_acks = 0UL;
392 0 : fd_memset( ctx->acks, 0, sizeof(ctx->acks) );
393 :
394 0 : fd_lthash_zero( &ctx->hash_accum.calculated_lthash );
395 0 : fd_lthash_zero( &ctx->running_lthash );
396 0 : }
397 :
398 0 : #define STEM_BURST 2UL /* one control message and one malformed message */
399 0 : #define STEM_LAZY 1000L
400 :
401 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapls_tile_t
402 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapls_tile_t)
403 :
404 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
405 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
406 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
407 :
408 : #include "../../disco/stem/fd_stem.c"
409 :
410 : fd_topo_run_tile_t fd_tile_snapls = {
411 : .name = NAME,
412 : .populate_allowed_fds = populate_allowed_fds,
413 : .populate_allowed_seccomp = populate_allowed_seccomp,
414 : .scratch_align = scratch_align,
415 : .scratch_footprint = scratch_footprint,
416 : .unprivileged_init = unprivileged_init,
417 : .run = stem_run,
418 : };
419 :
420 : #undef NAME
421 :
|