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 : if( ctx->state==FD_SNAPSHOT_STATE_ERROR && sig!=FD_SNAPSHOT_MSG_CTRL_FAIL ) {
208 : /* Control messages move along the snapshot load pipeline. Since
209 : error conditions can be triggered by any tile in the pipeline,
210 : it is possible to be in error state and still receive otherwise
211 : valid messages. Only a fail message can revert this. */
212 0 : return;
213 0 : };
214 :
215 0 : int forward_msg = 1;
216 :
217 0 : switch( sig ) {
218 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_FULL:
219 0 : case FD_SNAPSHOT_MSG_CTRL_INIT_INCR: {
220 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
221 0 : if( !recv_acks( ctx, in_idx, sig ) ) { forward_msg = 0; break; }
222 0 : ctx->state = FD_SNAPSHOT_STATE_PROCESSING;
223 0 : ctx->full = sig==FD_SNAPSHOT_MSG_CTRL_INIT_FULL;
224 0 : fd_lthash_zero( &ctx->running_lthash );
225 0 : break;
226 0 : }
227 :
228 0 : case FD_SNAPSHOT_MSG_CTRL_FINI: {
229 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
230 0 : if( !recv_acks( ctx, in_idx, sig ) ) { forward_msg = 0; break; }
231 0 : ctx->state = FD_SNAPSHOT_STATE_FINISHING;
232 0 : fd_lthash_sub( &ctx->hash_accum.calculated_lthash, &ctx->running_lthash );
233 0 : if( FD_UNLIKELY( memcmp( &ctx->hash_accum.expected_lthash, &ctx->hash_accum.calculated_lthash, sizeof(fd_lthash_value_t) ) ) ) {
234 : /* SnapshotError::MismatchedHash
235 : https://github.com/anza-xyz/agave/blob/v3.1.8/runtime/src/snapshot_bank_utils.rs#L479 */
236 0 : FD_LOG_WARNING(( "calculated accounts lthash %s does not match accounts lthash %s in snapshot manifest",
237 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.calculated_lthash ),
238 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.expected_lthash ) ));
239 0 : transition_malformed( ctx, stem );
240 0 : forward_msg = 0;
241 0 : break;
242 0 : } else {
243 0 : FD_LOG_NOTICE(( "calculated accounts lthash %s matches accounts lthash %s in snapshot manifest",
244 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.calculated_lthash ),
245 0 : FD_LTHASH_ENC_32_ALLOCA( &ctx->hash_accum.expected_lthash ) ));
246 0 : }
247 0 : break;
248 0 : }
249 :
250 0 : case FD_SNAPSHOT_MSG_CTRL_NEXT:
251 0 : case FD_SNAPSHOT_MSG_CTRL_DONE: {
252 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_FINISHING );
253 0 : if( !recv_acks( ctx, in_idx, sig ) ) { forward_msg = 0; break; }
254 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
255 0 : break;
256 0 : }
257 :
258 0 : case FD_SNAPSHOT_MSG_CTRL_ERROR: {
259 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
260 0 : ctx->state = FD_SNAPSHOT_STATE_ERROR;
261 0 : break;
262 0 : }
263 :
264 0 : case FD_SNAPSHOT_MSG_CTRL_FAIL: {
265 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
266 0 : if( !recv_acks( ctx, in_idx, sig ) ) { forward_msg = 0; break; }
267 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
268 0 : break;
269 0 : }
270 :
271 0 : case FD_SNAPSHOT_MSG_CTRL_SHUTDOWN: {
272 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_IDLE );
273 0 : if( !recv_acks( ctx, in_idx, sig ) ) { forward_msg = 0; break; }
274 0 : ctx->state = FD_SNAPSHOT_STATE_SHUTDOWN;
275 0 : break;
276 0 : }
277 :
278 0 : default: {
279 0 : FD_LOG_ERR(( "unexpected control sig %lu", sig ));
280 0 : break;
281 0 : }
282 0 : }
283 :
284 : /* Forward the control message down the pipeline */
285 0 : if( FD_LIKELY( forward_msg ) ) {
286 0 : fd_stem_publish( stem, 0UL, sig, 0UL, 0UL, 0UL, 0UL, 0UL );
287 0 : }
288 0 : }
289 :
290 : static void
291 : handle_hash_frag( fd_snapls_tile_t * ctx,
292 : ulong in_idx,
293 : ulong sig,
294 : ulong chunk,
295 0 : ulong sz ) {
296 0 : if( FD_UNLIKELY( ctx->state==FD_SNAPSHOT_STATE_ERROR ) ) return;
297 0 : FD_TEST( ctx->state==FD_SNAPSHOT_STATE_PROCESSING );
298 0 : switch( sig ) {
299 0 : case FD_SNAPSHOT_HASH_MSG_RESULT_ADD: {
300 0 : FD_TEST( sz==sizeof(fd_lthash_value_t) );
301 0 : fd_lthash_value_t const * result = fd_chunk_to_laddr_const( ctx->adder_in[ in_idx-ctx->adder_in_offset ].wksp, chunk );
302 0 : fd_lthash_add( &ctx->hash_accum.calculated_lthash, result );
303 0 : break;
304 0 : }
305 0 : case FD_SNAPSHOT_HASH_MSG_EXPECTED: {
306 0 : FD_TEST( sz==sizeof(fd_lthash_value_t) );
307 0 : FD_TEST( ctx->in_kind[ in_idx ]==IN_KIND_SNAPIN );
308 0 : fd_lthash_value_t const * result = fd_chunk_to_laddr_const( ctx->in.wksp, chunk );
309 0 : ctx->hash_accum.expected_lthash = *result;
310 0 : break;
311 0 : }
312 0 : default:
313 0 : FD_LOG_ERR(( "unexpected hash sig %lu", sig ));
314 0 : break;
315 0 : }
316 0 : }
317 :
318 : static inline int
319 : returnable_frag( fd_snapls_tile_t * ctx,
320 : ulong in_idx FD_PARAM_UNUSED,
321 : ulong seq FD_PARAM_UNUSED,
322 : ulong sig,
323 : ulong chunk,
324 : ulong sz,
325 : ulong ctl FD_PARAM_UNUSED,
326 : ulong tsorig FD_PARAM_UNUSED,
327 : ulong tspub FD_PARAM_UNUSED,
328 0 : fd_stem_context_t * stem ) {
329 0 : FD_TEST( ctx->state!=FD_SNAPSHOT_STATE_SHUTDOWN );
330 :
331 0 : if( FD_LIKELY( sig==FD_SNAPSHOT_HASH_MSG_SUB ||
332 0 : sig==FD_SNAPSHOT_HASH_MSG_SUB_HDR ||
333 0 : sig==FD_SNAPSHOT_HASH_MSG_SUB_DATA ) ) handle_data_frag( ctx, sig, chunk, sz );
334 0 : else if( FD_LIKELY( sig==FD_SNAPSHOT_HASH_MSG_RESULT_ADD ||
335 0 : sig==FD_SNAPSHOT_HASH_MSG_EXPECTED ) ) handle_hash_frag( ctx, in_idx, sig, chunk, sz );
336 0 : else handle_control_frag( ctx, stem, sig, in_idx );
337 :
338 0 : return 0;
339 0 : }
340 :
341 : static ulong
342 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
343 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
344 : ulong out_fds_cnt,
345 0 : int * out_fds ) {
346 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
347 :
348 0 : ulong out_cnt = 0;
349 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
350 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
351 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
352 0 : }
353 :
354 0 : return out_cnt;
355 0 : }
356 :
357 : static ulong
358 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
359 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
360 : ulong out_cnt,
361 0 : struct sock_filter * out ) {
362 0 : populate_sock_filter_policy_fd_snapls_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
363 0 : return sock_filter_policy_fd_snapls_tile_instr_cnt;
364 0 : }
365 :
366 : static void
367 : unprivileged_init( fd_topo_t * topo,
368 0 : fd_topo_tile_t * tile ) {
369 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
370 :
371 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
372 0 : fd_snapls_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_snapls_tile_t), sizeof(fd_snapls_tile_t) );
373 :
374 0 : ulong expected_in_cnt = 1UL + fd_topo_tile_name_cnt( topo, "snapla" );
375 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 ));
376 0 : if( FD_UNLIKELY( tile->out_cnt!=1UL ) ) FD_LOG_ERR(( "tile `" NAME "` has %lu outs, expected 1", tile->out_cnt ));
377 :
378 0 : ulong adder_idx = 0UL;
379 0 : for( ulong i=0UL; i<(tile->in_cnt); i++ ) {
380 0 : fd_topo_link_t * in_link = &topo->links[ tile->in_link_id[ i ] ];
381 0 : fd_topo_wksp_t const * in_wksp = &topo->workspaces[ topo->objs[ in_link->dcache_obj_id ].wksp_id ];
382 0 : if( FD_LIKELY( 0==strcmp( in_link->name, "snapin_ls" ) ) ) {
383 0 : ctx->in.wksp = in_wksp->wksp;;
384 0 : ctx->in.chunk0 = fd_dcache_compact_chunk0( ctx->in.wksp, in_link->dcache );
385 0 : ctx->in.wmark = fd_dcache_compact_wmark( ctx->in.wksp, in_link->dcache, in_link->mtu );
386 0 : ctx->in.mtu = in_link->mtu;
387 0 : ctx->in.pos = 0UL;
388 0 : ctx->in_kind[ i ] = IN_KIND_SNAPIN;
389 0 : } else if( FD_LIKELY( 0==strcmp( in_link->name, "snapla_ls" ) ) ) {
390 0 : ctx->adder_in[ adder_idx ].wksp = in_wksp->wksp;
391 0 : ctx->adder_in[ adder_idx ].chunk0 = fd_dcache_compact_chunk0( ctx->adder_in[ adder_idx ].wksp, in_link->dcache );
392 0 : ctx->adder_in[ adder_idx ].wmark = fd_dcache_compact_wmark ( ctx->adder_in[ adder_idx ].wksp, in_link->dcache, in_link->mtu );
393 0 : ctx->adder_in[ adder_idx ].mtu = in_link->mtu;
394 0 : ctx->in_kind[ i ] = IN_KIND_SNAPLA;
395 0 : if( FD_LIKELY( adder_idx==0UL ) ) ctx->adder_in_offset = i;
396 0 : adder_idx++;
397 0 : } else {
398 0 : FD_LOG_ERR(( "tile `" NAME "` has unexpected in link name `%s`", in_link->name ));
399 0 : }
400 0 : }
401 :
402 0 : fd_topo_link_t * out_link = &topo->links[ tile->out_link_id[ 0UL ] ];
403 0 : FD_TEST( 0==strcmp( out_link->name, "snapls_ct" ) );
404 :
405 0 : ctx->metrics.full.accounts_hashed = 0UL;
406 0 : ctx->metrics.incremental.accounts_hashed = 0UL;
407 :
408 0 : ctx->state = FD_SNAPSHOT_STATE_IDLE;
409 0 : ctx->full = 1;
410 0 : ctx->hash_account = 0;
411 :
412 0 : ctx->num_hash_tiles = fd_topo_tile_name_cnt( topo, "snapla" );
413 :
414 0 : ctx->pending_ctrl_sig = 0UL;
415 0 : ctx->num_acks = 0UL;
416 0 : fd_memset( ctx->acks, 0, sizeof(ctx->acks) );
417 :
418 0 : fd_lthash_zero( &ctx->hash_accum.calculated_lthash );
419 0 : fd_lthash_zero( &ctx->running_lthash );
420 0 : }
421 :
422 0 : #define STEM_BURST 2UL /* one control message and one malformed message */
423 0 : #define STEM_LAZY 1000L
424 :
425 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_snapls_tile_t
426 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_snapls_tile_t)
427 :
428 : #define STEM_CALLBACK_SHOULD_SHUTDOWN should_shutdown
429 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
430 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
431 :
432 : #include "../../disco/stem/fd_stem.c"
433 :
434 : fd_topo_run_tile_t fd_tile_snapls = {
435 : .name = NAME,
436 : .populate_allowed_fds = populate_allowed_fds,
437 : .populate_allowed_seccomp = populate_allowed_seccomp,
438 : .scratch_align = scratch_align,
439 : .scratch_footprint = scratch_footprint,
440 : .unprivileged_init = unprivileged_init,
441 : .run = stem_run,
442 : };
443 :
444 : #undef NAME
|