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 "../../ballet/lthash/fd_lthash_adder.h"
5 :
6 : #include "generated/fd_snapla_tile_seccomp.h"
7 :
8 : #include "utils/fd_ssctrl.h"
9 : #include "utils/fd_ssparse.h"
10 : #include "utils/fd_ssmanifest_parser.h"
11 :
12 : #define NAME "snapla"
13 :
14 0 : #define FD_SNAPLA_OUT_CTRL 0UL
15 :
16 : struct fd_snapla_tile {
17 : int state;
18 : int full;
19 :
20 : ulong seed;
21 : int hash_account;
22 : ulong num_hash_tiles;
23 : ulong hash_tile_idx;
24 : ulong accounts_seen;
25 :
26 : fd_lthash_adder_t adder[1];
27 : uchar data[ FD_RUNTIME_ACC_SZ_MAX ];
28 : ulong acc_data_sz;
29 :
30 : fd_ssparse_t * ssparse;
31 : fd_ssmanifest_parser_t * manifest_parser;
32 : fd_lthash_value_t running_lthash;
33 :
34 : struct {
35 : uchar pubkey[ FD_HASH_FOOTPRINT ];
36 : uchar owner[ FD_HASH_FOOTPRINT ];
37 : ulong data_len;
38 : ulong lamports;
39 : int executable;
40 : } account_hdr;
41 :
42 : struct {
43 : struct {
44 : ulong accounts_hashed;
45 : } full;
46 :
47 : struct {
48 : ulong accounts_hashed;
49 : } incremental;
50 : } metrics;
51 :
52 : struct {
53 : fd_wksp_t * wksp;
54 : ulong chunk0;
55 : ulong wmark;
56 : ulong mtu;
57 : ulong pos;
58 : } in;
59 :
60 : struct {
61 : fd_wksp_t * wksp;
62 : ulong chunk0;
63 : ulong wmark;
64 : ulong chunk;
65 : ulong mtu;
66 : } out;
67 :
68 : fd_snapshot_manifest_t manifest[1];
69 : };
70 :
71 : typedef struct fd_snapla_tile fd_snapla_tile_t;
72 :
73 : static inline int
74 0 : should_shutdown( fd_snapla_tile_t * ctx ) {
75 0 : return ctx->state==FD_SNAPSHOT_STATE_SHUTDOWN;
76 0 : }
77 :
78 : static ulong
79 0 : scratch_align( void ) {
80 0 : return fd_ulong_max( alignof(fd_snapla_tile_t),
81 0 : fd_ulong_max( fd_ssparse_align(), fd_ssmanifest_parser_align() ) );
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_snapla_tile_t), sizeof(fd_snapla_tile_t) );
89 0 : l = FD_LAYOUT_APPEND( l, fd_ssparse_align(), fd_ssparse_footprint( 1UL<<24UL ) );
90 0 : l = FD_LAYOUT_APPEND( l, fd_ssmanifest_parser_align(), fd_ssmanifest_parser_footprint() );
91 0 : return FD_LAYOUT_FINI( l, alignof(fd_snapla_tile_t) );
92 0 : }
93 :
94 : static void
95 0 : metrics_write( fd_snapla_tile_t * ctx ) {
96 0 : FD_MGAUGE_SET( SNAPLA, FULL_ACCOUNTS_HASHED, ctx->metrics.full.accounts_hashed );
97 0 : FD_MGAUGE_SET( SNAPLA, INCREMENTAL_ACCOUNTS_HASHED, ctx->metrics.incremental.accounts_hashed );
98 0 : FD_MGAUGE_SET( SNAPLA, STATE, (ulong)(ctx->state) );
99 0 : }
100 :
101 : static void
102 : transition_malformed( fd_snapla_tile_t * ctx,
103 0 : fd_stem_context_t * stem ) {
104 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
105 0 : fd_stem_publish( stem, FD_SNAPLA_OUT_CTRL, FD_SNAPSHOT_MSG_CTRL_ERROR, 0UL, 0UL, 0UL, 0UL, 0UL );
106 0 : }
107 :
108 : static int
109 0 : should_hash_account( fd_snapla_tile_t * ctx ) {
110 0 : return ctx->accounts_seen%ctx->num_hash_tiles==ctx->hash_tile_idx;
111 0 : }
112 :
113 : static void
114 : streamlined_hash( fd_snapla_tile_t * ctx,
115 0 : uchar const * frame ) {
116 0 : ulong data_len = fd_ulong_load_8_fast( frame+0x08UL );
117 0 : uchar pubkey[32]; memcpy( pubkey, frame+0x10UL, 32UL );
118 0 : ulong lamports = fd_ulong_load_8_fast( frame+0x30UL );
119 0 : ulong rent_epoch = fd_ulong_load_8_fast( frame+0x38UL ); (void)rent_epoch;
120 0 : uchar owner[32]; memcpy( owner, frame+0x40UL, 32UL );
121 0 : _Bool executable = !!frame[ 0x60UL ];
122 :
123 0 : if( FD_UNLIKELY( data_len > FD_RUNTIME_ACC_SZ_MAX ) ) FD_LOG_ERR(( "Found unusually large account (data_sz=%lu), aborting", data_len ));
124 0 : if( FD_UNLIKELY( lamports==0UL ) ) return;
125 :
126 0 : uchar executable_flag = executable & 0x1;
127 :
128 0 : fd_lthash_adder_push_solana_account( ctx->adder,
129 0 : &ctx->running_lthash,
130 0 : pubkey,
131 0 : frame+0x88UL,
132 0 : data_len,
133 0 : lamports,
134 0 : executable_flag,
135 0 : owner );
136 :
137 0 : if( FD_LIKELY( ctx->full ) ) ctx->metrics.full.accounts_hashed++;
138 0 : else ctx->metrics.incremental.accounts_hashed++;
139 0 : }
140 :
141 : static int
142 : handle_data_frag( fd_snapla_tile_t * ctx,
143 : ulong chunk,
144 : ulong sz,
145 0 : fd_stem_context_t * stem ) {
146 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_FINISHING ) ) {
147 0 : FD_LOG_WARNING(( "received data fragment while in finishing state" ));
148 0 : transition_malformed( ctx, stem );
149 0 : return 0;
150 0 : } else if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) {
151 : /* Ignore all data frags after observing an error in the stream until
152 : we receive fail & init control messages to restart processing. */
153 0 : return 0;
154 0 : } else if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_PROCESSING ) ) {
155 0 : FD_LOG_ERR(( "invalid state for data frag %d", ctx->state ));
156 0 : }
157 :
158 0 : FD_TEST( chunk>=ctx->in.chunk0 && chunk<=ctx->in.wmark && sz<=ctx->in.mtu );
159 :
160 0 : for(;;) {
161 0 : if( FD_UNLIKELY( sz-ctx->in.pos==0UL ) ) break;
162 0 : uchar const * data = (uchar const *)fd_chunk_to_laddr_const( ctx->in.wksp, chunk ) + ctx->in.pos;
163 :
164 0 : fd_ssparse_advance_result_t result[1];
165 0 : int res = fd_ssparse_advance( ctx->ssparse, data, sz-ctx->in.pos, result );
166 0 : switch( res ) {
167 0 : case FD_SSPARSE_ADVANCE_ERROR:
168 0 : transition_malformed( ctx, stem );
169 0 : return 0;
170 0 : case FD_SSPARSE_ADVANCE_AGAIN:
171 0 : break;
172 0 : case FD_SSPARSE_ADVANCE_STATUS_CACHE:
173 : /* ignore */
174 0 : break;
175 0 : case FD_SSPARSE_ADVANCE_MANIFEST: {
176 0 : int res = fd_ssmanifest_parser_consume( ctx->manifest_parser,
177 0 : result->manifest.data,
178 0 : result->manifest.data_sz,
179 0 : result->manifest.acc_vec_map,
180 0 : result->manifest.acc_vec_pool );
181 0 : if( FD_UNLIKELY( res==FD_SSMANIFEST_PARSER_ADVANCE_ERROR ) ) {
182 0 : transition_malformed( ctx, stem );
183 0 : return 0;
184 0 : }
185 0 : break;
186 0 : }
187 0 : case FD_SSPARSE_ADVANCE_ACCOUNT_HEADER:
188 0 : if( FD_LIKELY( should_hash_account( ctx ) && result->account_header.lamports!=0UL ) ) {
189 0 : FD_TEST( ctx->acc_data_sz==0UL );
190 0 : ctx->hash_account = 1;
191 0 : fd_memcpy( ctx->account_hdr.pubkey, result->account_header.pubkey, FD_HASH_FOOTPRINT );
192 0 : fd_memcpy( ctx->account_hdr.owner, result->account_header.owner, FD_HASH_FOOTPRINT );
193 0 : ctx->account_hdr.data_len = result->account_header.data_len;
194 0 : ctx->account_hdr.executable = result->account_header.executable;
195 0 : ctx->account_hdr.lamports = result->account_header.lamports;
196 0 : }
197 0 : ctx->accounts_seen++;
198 0 : break;
199 0 : case FD_SSPARSE_ADVANCE_ACCOUNT_DATA:
200 0 : if( FD_LIKELY( ctx->hash_account ) ) {
201 0 : fd_memcpy( ctx->data + ctx->acc_data_sz, result->account_data.data, result->account_data.data_sz );
202 0 : ctx->acc_data_sz += result->account_data.data_sz;
203 0 : }
204 0 : break;
205 0 : case FD_SSPARSE_ADVANCE_ACCOUNT_BATCH: {
206 0 : for( ulong i=0UL; i<result->account_batch.batch_cnt; i++ ) {
207 0 : if( FD_LIKELY( should_hash_account( ctx ) ) ) streamlined_hash( ctx, result->account_batch.batch[ i ] );
208 0 : ctx->accounts_seen++;
209 0 : }
210 0 : break;
211 0 : }
212 0 : case FD_SSPARSE_ADVANCE_DONE:
213 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
214 0 : break;
215 0 : default:
216 0 : FD_LOG_ERR(( "unexpected fd_ssparse_advance result %d", res ));
217 0 : break;
218 0 : }
219 :
220 0 : ctx->in.pos += result->bytes_consumed;
221 0 : if( FD_LIKELY( ctx->hash_account && ctx->acc_data_sz==ctx->account_hdr.data_len ) ) {
222 0 : fd_lthash_adder_push_solana_account( ctx->adder,
223 0 : &ctx->running_lthash,
224 0 : ctx->account_hdr.pubkey,
225 0 : ctx->data,
226 0 : ctx->account_hdr.data_len,
227 0 : ctx->account_hdr.lamports,
228 0 : (uchar)ctx->account_hdr.executable,
229 0 : ctx->account_hdr.owner );
230 0 : ctx->acc_data_sz = 0UL;
231 0 : ctx->hash_account = 0;
232 :
233 0 : if( FD_LIKELY( ctx->full ) ) ctx->metrics.full.accounts_hashed++;
234 0 : else ctx->metrics.incremental.accounts_hashed++;
235 0 : }
236 0 : }
237 :
238 0 : int reprocess_frag = ctx->in.pos<sz;
239 0 : if( FD_LIKELY( !reprocess_frag ) ) ctx->in.pos = 0UL;
240 0 : return reprocess_frag;
241 0 : }
242 :
243 : static void
244 : handle_control_frag( fd_snapla_tile_t * ctx,
245 : fd_stem_context_t * stem,
246 0 : ulong sig ) {
247 0 : switch( sig ) {
248 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
249 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR:
250 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
251 0 : ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
252 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
253 0 : fd_lthash_zero( &ctx->running_lthash );
254 0 : fd_ssparse_reset( ctx->ssparse );
255 0 : fd_ssmanifest_parser_init( ctx->manifest_parser, ctx->manifest );
256 0 : fd_lthash_adder_new( ctx->adder );
257 0 : break;
258 :
259 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL:
260 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ||
261 0 : ctx->state==FD_SNAPSHOT_STATE_FINISHING ||
262 0 : ctx->state==FD_SNAPSHOT_STATE_ERROR );
263 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
264 0 : fd_lthash_zero( &ctx->running_lthash );
265 0 : fd_ssparse_reset( ctx->ssparse );
266 0 : fd_ssmanifest_parser_init( ctx->manifest_parser, ctx->manifest );
267 0 : fd_lthash_adder_new( ctx->adder );
268 0 : break;
269 :
270 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
271 0 : case FD_SNAPSHOT_MSG_CTRL_DONE:{
272 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING ||
273 0 : ctx->state==FD_SNAPSHOT_STATE_FINISHING ||
274 0 : ctx->state==FD_SNAPSHOT_STATE_ERROR );
275 0 : if( FD_UNLIKELY( ctx->state!=FD_SNAPSHOT_STATE_FINISHING ) ) {
276 0 : transition_malformed( ctx, stem );
277 0 : return;
278 0 : }
279 0 : fd_lthash_adder_flush( ctx->adder, &ctx->running_lthash );
280 0 : uchar * lthash_out = fd_chunk_to_laddr( ctx->out.wksp, ctx->out.chunk );
281 0 : fd_memcpy( lthash_out, &ctx->running_lthash, sizeof(fd_lthash_value_t) );
282 0 : fd_stem_publish( stem, 0UL, FD_SNAPSHOT_HASH_MSG_RESULT_ADD, ctx->out.chunk, FD_LTHASH_LEN_BYTES, 0UL, 0UL, 0UL );
283 0 : ctx->out.chunk = fd_dcache_compact_next( ctx->out.chunk, FD_LTHASH_LEN_BYTES, ctx->out.chunk0, ctx->out.wmark );
284 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
285 0 : break;
286 0 : }
287 :
288 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN:
289 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
290 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
291 0 : break;
292 :
293 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR:
294 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
295 0 : break;
296 :
297 0 : default:
298 0 : FD_LOG_ERR(( "unexpected control sig %lu", sig ));
299 0 : return;
300 0 : }
301 :
302 : /* Forward the control message down the pipeline */
303 0 : fd_stem_publish( stem, FD_SNAPLA_OUT_CTRL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
304 0 : }
305 :
306 : static inline int
307 : returnable_frag( fd_snapla_tile_t * ctx,
308 : ulong in_idx FD_PARAM_UNUSED,
309 : ulong seq FD_PARAM_UNUSED,
310 : ulong sig,
311 : ulong chunk,
312 : ulong sz,
313 : ulong ctl FD_PARAM_UNUSED,
314 : ulong tsorig FD_PARAM_UNUSED,
315 : ulong tspub FD_PARAM_UNUSED,
316 0 : fd_stem_context_t * stem ) {
317 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
318 :
319 0 : if( FD_UNLIKELY( sig==FD_SNAPSHOT_MSG_DATA ) ) return handle_data_frag( ctx, chunk, sz, stem );
320 0 : else handle_control_frag( ctx, stem, sig );
321 :
322 0 : return 0;
323 0 : }
324 :
325 : static ulong
326 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
327 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
328 : ulong out_fds_cnt,
329 0 : int * out_fds ) {
330 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
331 :
332 0 : ulong out_cnt = 0;
333 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
334 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
335 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
336 0 : }
337 :
338 0 : return out_cnt;
339 0 : }
340 :
341 : static ulong
342 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
343 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
344 : ulong out_cnt,
345 0 : struct sock_filter * out ) {
346 0 : populate_sock_filter_policy_fd_snapla_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
347 0 : return sock_filter_policy_fd_snapla_tile_instr_cnt;
348 0 : }
349 :
350 : static void
351 : privileged_init( fd_topo_t * topo,
352 0 : fd_topo_tile_t * tile ) {
353 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
354 :
355 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
356 0 : fd_snapla_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapla_tile_t), sizeof(fd_snapla_tile_t) );
357 :
358 0 : FD_TEST( fd_rng_secure( &ctx->seed, 8UL ) );
359 0 : }
360 :
361 : static void
362 : unprivileged_init( fd_topo_t * topo,
363 0 : fd_topo_tile_t * tile ) {
364 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
365 :
366 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
367 0 : fd_snapla_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapla_tile_t), sizeof(fd_snapla_tile_t) );
368 0 : void * _ssparse = FD_SCRATCH_ALLOC_APPEND( l, fd_ssparse_align(), fd_ssparse_footprint( 1UL<<24UL ));
369 0 : void * _manifest_parser = FD_SCRATCH_ALLOC_APPEND( l, fd_ssmanifest_parser_align(), fd_ssmanifest_parser_footprint() );
370 :
371 0 : if( FD_UNLIKELY( tile->in_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu ins, expected 1", tile->in_cnt ));
372 0 : if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
373 :
374 0 : fd_topo_link_t const * in_link = &topo->links[ tile->in_link_id[ 0UL ] ];
375 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
376 0 : ctx->in.wksp = in_wksp->wksp;;
377 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
378 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
379 0 : ctx->in.mtu = in_link->mtu;
380 0 : ctx->in.pos = 0UL;
381 :
382 0 : fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ 0UL ] ];
383 0 : ctx->out.wksp = topo->workspaces[ topo->objs[ out_link->dcache_obj_id ].wksp_id ].wksp;
384 0 : ctx->out.chunk0 = fd_dcache_compact_chunk0( fd_wksp_containing( out_link->dcache ), out_link->dcache );
385 0 : ctx->out.wmark = fd_dcache_compact_wmark ( ctx->out.wksp, out_link->dcache, out_link->mtu );
386 0 : ctx->out.chunk = ctx->out.chunk0;
387 0 : ctx->out.mtu = out_link->mtu;
388 0 : FD_TEST( 0==strcmp( out_link->name, "snapla_ls" ) );
389 :
390 0 : ctx->ssparse = fd_ssparse_new( _ssparse, 1UL<<24UL, 0UL );
391 0 : FD_TEST( ctx->ssparse );
392 :
393 0 : ctx->manifest_parser = fd_ssmanifest_parser_join( fd_ssmanifest_parser_new( _manifest_parser ) );
394 0 : FD_TEST( ctx->manifest_parser );
395 :
396 0 : fd_ssparse_batch_enable( ctx->ssparse, 1 );
397 0 : fd_lthash_adder_new( ctx->adder );
398 0 : fd_ssmanifest_parser_init( ctx->manifest_parser, ctx->manifest );
399 :
400 0 : ctx->metrics.full.accounts_hashed = 0UL;
401 0 : ctx->metrics.incremental.accounts_hashed = 0UL;
402 :
403 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
404 0 : ctx->full = 1;
405 0 : ctx->acc_data_sz = 0UL;
406 0 : ctx->hash_account = 0;
407 0 : ctx->num_hash_tiles = fd_topo_tile_name_cnt( topo, "snapla" );
408 0 : ctx->hash_tile_idx = tile->kind_id;
409 0 : ctx->accounts_seen = 0UL;
410 0 : fd_lthash_zero( &ctx->running_lthash );
411 0 : }
412 :
413 0 : #define STEM_BURST 2UL /* one control message and one malformed message or one hash result message */
414 0 : #define STEM_LAZY 1000L
415 :
416 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapla_tile_t
417 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapla_tile_t)
418 :
419 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
420 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
421 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
422 :
423 : #include "../../disco/stem/fd_stem.c"
424 :
425 : fd_topo_run_tile_t fd_tile_snapla = {
426 : .name = NAME,
427 : .populate_allowed_fds = populate_allowed_fds,
428 : .populate_allowed_seccomp = populate_allowed_seccomp,
429 : .scratch_align = scratch_align,
430 : .scratch_footprint = scratch_footprint,
431 : .privileged_init = privileged_init,
432 : .unprivileged_init = unprivileged_init,
433 : .run = stem_run,
434 : };
435 :
436 : #undef NAME
437 :
|