Line data Source code
1 : #include "fd_resolv_tile.h"
2 : #include "../../disco/fd_txn_m.h"
3 : #include "../../disco/topo/fd_topo.h"
4 : #include "../replay/fd_replay_tile.h"
5 : #include "../../discof/fd_startup.h"
6 : #include "../../disco/metrics/fd_metrics.h"
7 : #include "../../flamenco/accdb/fd_accdb.h"
8 : #include "../../flamenco/accdb/fd_accdb_shmem.h"
9 : #include "../../flamenco/runtime/fd_alut.h"
10 : #include "../../flamenco/runtime/fd_runtime_const.h"
11 : #include "../../flamenco/runtime/fd_system_ids_pp.h"
12 : #include "../../flamenco/runtime/fd_bank.h"
13 : #include "../../tango/fseq/fd_fseq.h"
14 : #include "../../util/pod/fd_pod_format.h"
15 :
16 : #include <time.h>
17 :
18 : #include "generated/fd_resolv_tile_seccomp.h"
19 :
20 : #if FD_HAS_AVX
21 : #include "../../util/simd/fd_avx.h"
22 : #endif
23 :
24 0 : #define IN_KIND_DEDUP (0)
25 0 : #define IN_KIND_REPLAY (1)
26 :
27 : struct blockhash {
28 : uchar b[ 32 ];
29 : };
30 :
31 : typedef struct blockhash blockhash_t;
32 :
33 : struct blockhash_map {
34 : blockhash_t key;
35 : ulong slot;
36 : };
37 :
38 : typedef struct blockhash_map blockhash_map_t;
39 :
40 : static const blockhash_t null_blockhash = { 0 };
41 :
42 : /* The blockhash ring holds recent blockhashes, so we can identify when
43 : a transaction arrives, what slot it will expire (and can no longer be
44 : packed) in. This is useful so we don't send transactions to pack
45 : that are no longer packable.
46 :
47 : Unfortunately, poorly written transaction senders frequently send
48 : transactions from millions of slots ago, so we need a large ring to
49 : be able to determine and evict these. The highest practically useful
50 : value here is around 22, which works out to 19 days of blockhash
51 : history. Beyond this, the validator is likely to be restarted, and
52 : lose the history anyway. */
53 :
54 0 : #define BLOCKHASH_LG_RING_CNT 22UL
55 0 : #define BLOCKHASH_RING_LEN (1UL<<BLOCKHASH_LG_RING_CNT)
56 :
57 : #define MAP_NAME map
58 0 : #define MAP_T blockhash_map_t
59 0 : #define MAP_KEY_T blockhash_t
60 0 : #define MAP_LG_SLOT_CNT (BLOCKHASH_LG_RING_CNT+1UL)
61 0 : #define MAP_KEY_NULL null_blockhash
62 : #if FD_HAS_AVX
63 0 : # define MAP_KEY_INVAL(k) _mm256_testz_si256( wb_ldu( (k).b ), wb_ldu( (k).b ) )
64 : #else
65 : # define MAP_KEY_INVAL(k) MAP_KEY_EQUAL(k, null_blockhash)
66 : #endif
67 0 : #define MAP_KEY_EQUAL(k0,k1) (!memcmp((k0).b,(k1).b, 32UL))
68 : #define MAP_MEMOIZE 0
69 : #define MAP_KEY_EQUAL_IS_SLOW 1
70 0 : #define MAP_KEY_HASH(key) fd_uint_load_4( (key).b )
71 : #define MAP_QUERY_OPT 1
72 :
73 : #include "../../util/tmpl/fd_map.c"
74 :
75 : typedef struct {
76 : union {
77 : ulong pool_next; /* Used when it's released */
78 : ulong lru_next; /* Used when it's acquired */
79 : }; /* .. so it's okay to store them in the same memory */
80 : ulong lru_prev;
81 :
82 : ulong map_next;
83 : ulong map_prev;
84 :
85 : blockhash_t * blockhash;
86 : uchar _[ FD_TPU_PARSED_MTU ] __attribute__((aligned(alignof(fd_txn_m_t))));
87 : } fd_stashed_txn_m_t;
88 :
89 : #define POOL_NAME pool
90 0 : #define POOL_T fd_stashed_txn_m_t
91 0 : #define POOL_NEXT pool_next
92 : #define POOL_IDX_T ulong
93 :
94 : #include "../../util/tmpl/fd_pool.c"
95 :
96 : /* We'll push at the head, which means the tail is the oldest. */
97 : #define DLIST_NAME lru_list
98 : #define DLIST_ELE_T fd_stashed_txn_m_t
99 0 : #define DLIST_PREV lru_prev
100 0 : #define DLIST_NEXT lru_next
101 :
102 : #include "../../util/tmpl/fd_dlist.c"
103 :
104 : #define MAP_NAME map_chain
105 0 : #define MAP_ELE_T fd_stashed_txn_m_t
106 : #define MAP_KEY_T blockhash_t *
107 0 : #define MAP_KEY blockhash
108 0 : #define MAP_IDX_T ulong
109 0 : #define MAP_NEXT map_next
110 0 : #define MAP_PREV map_prev
111 0 : #define MAP_KEY_HASH(k,s) ((s) ^ fd_ulong_load_8( (*(k))->b ))
112 0 : #define MAP_KEY_EQ(k0,k1) (!memcmp((*(k0))->b, (*(k1))->b, 32UL))
113 : #define MAP_OPTIMIZE_RANDOM_ACCESS_REMOVAL 1
114 : #define MAP_MULTI 1
115 :
116 : #include "../../util/tmpl/fd_map_chain.c"
117 :
118 : typedef struct {
119 : int kind;
120 :
121 : fd_wksp_t * mem;
122 : ulong chunk0;
123 : ulong wmark;
124 : ulong mtu;
125 : } fd_resolv_in_ctx_t;
126 :
127 : typedef struct {
128 : fd_wksp_t * mem;
129 : ulong chunk0;
130 : ulong wmark;
131 : ulong chunk;
132 : } fd_resolv_out_ctx_t;
133 :
134 : typedef struct {
135 : ulong round_robin_idx;
136 : ulong round_robin_cnt;
137 :
138 : int bundle_failed;
139 : ulong bundle_id;
140 :
141 : blockhash_map_t * blockhash_map;
142 :
143 : ulong flushing_slot;
144 : ulong flush_pool_idx;
145 :
146 : /* In the full client, the resolv tile is passed only a rooted bank
147 : index from replay whenever the root is advanced.
148 :
149 : This is enough to query the accounts database for that bank and
150 : retrieve the address lookup tables. Because of lifetime concerns
151 : around bank ownership, the replay tile is solely responsible for
152 : freeing the bank when it is no longer needed. To facilitate this,
153 : the resolv tile sends a message to replay when it is done with a
154 : rooted bank (after exchanging it for a new rooted bank). */
155 : fd_banks_t * banks;
156 : fd_bank_t * bank;
157 : fd_accdb_t * accdb;
158 :
159 : fd_stashed_txn_m_t * pool;
160 : map_chain_t * map_chain;
161 : lru_list_t lru_list[1];
162 :
163 : ulong completed_slot;
164 : ulong blockhash_ring_idx;
165 : blockhash_t blockhash_ring[ BLOCKHASH_RING_LEN ];
166 :
167 : fd_replay_root_advanced_t _rooted_slot_msg;
168 : fd_replay_slot_completed_t _completed_slot_msg;
169 :
170 : struct {
171 : ulong lut[ FD_METRICS_COUNTER_RESOLV_LUT_RESOLVED_CNT ];
172 : ulong blockhash_expired;
173 : ulong bundle_peer_failure;
174 : ulong stash[ FD_METRICS_COUNTER_RESOLV_STASH_OPERATION_CNT ];
175 : } metrics;
176 :
177 : fd_resolv_in_ctx_t in[ 64UL ];
178 :
179 : fd_resolv_out_ctx_t out_pack[ 1UL ];
180 : fd_resolv_out_ctx_t out_replay[ 1UL ];
181 :
182 : /* Scratch buffers for fd_accdb_read_one_nocache. RO accdb joiners
183 : must use the nocache API (see fd_accdb.h), which writes the account
184 : data into caller-provided buffers rather than returning a pointer
185 : into the cache. Reused across alut reads; peek_alut consumes the
186 : bytes synchronously inside fd_alut_interp_next. */
187 : uchar alut_owner[ 32UL ];
188 : uchar alut_data[ FD_RUNTIME_ACC_SZ_MAX ];
189 : } fd_resolv_ctx_t;
190 :
191 : FD_FN_CONST static inline ulong
192 0 : scratch_align( void ) {
193 0 : return fd_ulong_max( fd_ulong_max( alignof( fd_resolv_ctx_t ), pool_align() ), fd_ulong_max( map_chain_align(), map_align() ) );
194 0 : }
195 :
196 : FD_FN_PURE static inline ulong
197 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
198 0 : ulong l = FD_LAYOUT_INIT;
199 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t ) );
200 0 : l = FD_LAYOUT_APPEND( l, pool_align(), pool_footprint ( 1UL<<16UL ) );
201 0 : l = FD_LAYOUT_APPEND( l, map_chain_align(), map_chain_footprint( 8192UL ) );
202 0 : l = FD_LAYOUT_APPEND( l, map_align(), map_footprint() );
203 0 : l = FD_LAYOUT_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->resolv.max_live_slots ) );
204 0 : return FD_LAYOUT_FINI( l, scratch_align() );
205 0 : }
206 :
207 : static inline void
208 0 : metrics_write( fd_resolv_ctx_t * ctx ) {
209 0 : FD_MCNT_SET( RESOLV, BLOCKHASH_EXPIRED, ctx->metrics.blockhash_expired );
210 0 : FD_MCNT_ENUM_COPY( RESOLV, LUT_RESOLVED, ctx->metrics.lut );
211 0 : FD_MCNT_ENUM_COPY( RESOLV, STASH_OPERATION, ctx->metrics.stash );
212 0 : FD_MCNT_SET( RESOLV, TXN_BUNDLE_PEER_FAILED, ctx->metrics.bundle_peer_failure );
213 :
214 0 : FD_ACCDB_METRICS_WRITE_RO( RESOLV, fd_accdb_metrics( ctx->accdb ) );
215 0 : }
216 :
217 : static int
218 : before_frag( fd_resolv_ctx_t * ctx,
219 : ulong in_idx,
220 : ulong seq,
221 0 : ulong sig ) {
222 0 : if( FD_UNLIKELY( ctx->in[in_idx].kind==IN_KIND_REPLAY ) ) return 0;
223 :
224 : /* Bundle transactions (sig==1) must arrive at pack in order. Route
225 : all bundle traffic to resolv:0. */
226 0 : if( FD_UNLIKELY( sig ) ) return ctx->round_robin_idx!=0UL;
227 :
228 0 : return (seq % ctx->round_robin_cnt) != ctx->round_robin_idx;
229 0 : }
230 :
231 : static inline void
232 : during_frag( fd_resolv_ctx_t * ctx,
233 : ulong in_idx,
234 : ulong seq FD_PARAM_UNUSED,
235 : ulong sig FD_PARAM_UNUSED,
236 : ulong chunk,
237 : ulong sz,
238 0 : ulong ctl FD_PARAM_UNUSED ) {
239 :
240 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
241 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 ));
242 :
243 0 : switch( ctx->in[in_idx].kind ) {
244 0 : case IN_KIND_DEDUP: {
245 0 : uchar * src = (uchar *)fd_chunk_to_laddr( ctx->in[in_idx].mem, chunk );
246 0 : uchar * dst = (uchar *)fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
247 0 : fd_memcpy( dst, src, sz );
248 0 : break;
249 0 : }
250 0 : case IN_KIND_REPLAY: {
251 0 : if( FD_UNLIKELY( sig==REPLAY_SIG_ROOT_ADVANCED ) ) {
252 0 : ctx->_rooted_slot_msg = *(fd_replay_root_advanced_t *)fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
253 0 : } else if( FD_UNLIKELY( sig==REPLAY_SIG_SLOT_COMPLETED ) ) {
254 0 : ctx->_completed_slot_msg = *(fd_replay_slot_completed_t *)fd_chunk_to_laddr_const( ctx->in[in_idx].mem, chunk );
255 0 : }
256 0 : break;
257 0 : }
258 0 : default:
259 0 : FD_LOG_ERR(( "unknown in kind %d", ctx->in[in_idx].kind ));
260 0 : }
261 0 : }
262 :
263 : /* peek_alut reads a single address lookup table from database cache. */
264 :
265 : static int
266 : peek_alut( fd_resolv_ctx_t * ctx,
267 : fd_txn_m_t * txnm,
268 : fd_alut_interp_t * interp,
269 0 : ulong alut_idx ) {
270 0 : fd_txn_t const * txn = fd_txn_m_txn_t_const ( txnm );
271 0 : uchar const * txn_payload = fd_txn_m_payload_const( txnm );
272 0 : fd_txn_acct_addr_lut_t const * addr_lut = &fd_txn_get_address_tables_const( txn )[ alut_idx ];
273 0 : fd_pubkey_t addr_lut_acc = FD_LOAD( fd_pubkey_t, txn_payload+addr_lut->addr_off );
274 :
275 : /* https://github.com/anza-xyz/agave/blob/368ea563c423b0a85cc317891187e15c9a321521/accounts-db/src/accounts.rs#L90-L94
276 :
277 : The resolv tile maps accdb read-only and so must use the nocache
278 : read API; fd_accdb_read_one would mutate writer-only shmem. */
279 0 : ulong lamports;
280 0 : int executable;
281 0 : ulong data_len;
282 0 : fd_accdb_read_one_nocache( ctx->accdb, ctx->bank->accdb_fork_id, addr_lut_acc.uc,
283 0 : &lamports, &executable, ctx->alut_owner, ctx->alut_data, &data_len );
284 0 : if( FD_UNLIKELY( !lamports ) ) return FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND;
285 :
286 0 : return fd_alut_interp_next( interp, &addr_lut_acc, ctx->alut_owner, ctx->alut_data, data_len );
287 0 : }
288 :
289 : /* peek_aluts reads address lookup tables from database cache.
290 : Gracefully recovers from data races and missing accounts. */
291 :
292 : static int
293 : peek_aluts( fd_resolv_ctx_t * ctx,
294 0 : fd_txn_m_t * txnm ) {
295 : /* Unpack context */
296 0 : fd_txn_t const * txn = fd_txn_m_txn_t_const ( txnm );
297 0 : uchar const * txn_payload = fd_txn_m_payload_const( txnm );
298 0 : ulong const alut_cnt = txn->addr_table_lookup_cnt;
299 0 : ulong const slot = ctx->bank->f.slot;
300 0 : fd_sysvar_cache_t const * sysvar_cache = &ctx->bank->f.sysvar_cache;
301 0 : fd_slot_hashes_t slot_hashes_view[1];
302 0 : if( FD_UNLIKELY( !fd_sysvar_cache_slot_hashes_view( sysvar_cache, slot_hashes_view ) ) ) {
303 0 : FD_LOG_ERR(( "slot hashes sysvar cache is invalid" ));
304 0 : }
305 :
306 : /* Write indirect addrs into here */
307 0 : fd_acct_addr_t * indir_addrs = fd_txn_m_alut( txnm );
308 :
309 0 : int err = FD_RUNTIME_EXECUTE_SUCCESS;
310 0 : fd_alut_interp_t interp[1];
311 0 : fd_alut_interp_new( interp, indir_addrs, txn, txn_payload, slot_hashes_view, slot );
312 0 : for( ulong i=0UL; i<alut_cnt; i++ ) {
313 0 : err = peek_alut( ctx, txnm, interp, i );
314 0 : if( FD_UNLIKELY( err ) ) break;
315 0 : }
316 :
317 0 : ulong ctr_idx;
318 0 : switch( err ) {
319 0 : case FD_RUNTIME_EXECUTE_SUCCESS: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_SUCCESS_IDX; break;
320 0 : case FD_RUNTIME_TXN_ERR_ADDRESS_LOOKUP_TABLE_NOT_FOUND: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_ACCOUNT_NOT_FOUND_IDX; break;
321 0 : case FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_OWNER: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_INVALID_ACCOUNT_OWNER_IDX; break;
322 0 : case FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_DATA: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_INVALID_ACCOUNT_DATA_IDX; break;
323 0 : case FD_RUNTIME_TXN_ERR_INVALID_ADDRESS_LOOKUP_TABLE_INDEX: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_INVALID_LOOKUP_INDEX_IDX; break;
324 0 : default: ctr_idx = FD_METRICS_ENUM_LUT_RESOLVE_RESULT_V_ACCOUNT_UNINITIALIZED_IDX; break;
325 0 : }
326 0 : ctx->metrics.lut[ ctr_idx ]++;
327 0 : return err;
328 0 : }
329 :
330 : static int
331 : publish_txn( fd_resolv_ctx_t * ctx,
332 : fd_stem_context_t * stem,
333 0 : fd_stashed_txn_m_t const * stashed ) {
334 0 : fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
335 0 : fd_memcpy( txnm, stashed->_, fd_txn_m_realized_footprint( (fd_txn_m_t *)stashed->_, 1, 0 ) );
336 :
337 0 : fd_txn_t const * txnt = fd_txn_m_txn_t( txnm );
338 :
339 0 : txnm->reference_slot = ctx->flushing_slot;
340 :
341 0 : if( FD_UNLIKELY( txnt->addr_table_adtl_cnt ) ) {
342 0 : if( FD_UNLIKELY( !ctx->bank ) ) {
343 0 : FD_MCNT_INC( RESOLV, TXN_NO_BANK, 1 );
344 0 : return 0;
345 0 : }
346 0 : int err = peek_aluts( ctx, txnm );
347 0 : if( FD_UNLIKELY( err ) ) return 0;
348 0 : }
349 :
350 0 : ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 1 );
351 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
352 0 : fd_stem_publish( stem, 0UL, txnm->reference_slot, ctx->out_pack->chunk, realized_sz, 0UL, 0UL, tspub );
353 0 : ctx->out_pack->chunk = fd_dcache_compact_next( ctx->out_pack->chunk, realized_sz, ctx->out_pack->chunk0, ctx->out_pack->wmark );
354 :
355 0 : return 1;
356 0 : }
357 :
358 : static inline void
359 : after_credit( fd_resolv_ctx_t * ctx,
360 : fd_stem_context_t * stem,
361 : int * opt_poll_in,
362 0 : int * charge_busy ) {
363 0 : if( FD_LIKELY( ctx->flush_pool_idx==ULONG_MAX ) ) return;
364 :
365 0 : *charge_busy = 1;
366 0 : *opt_poll_in = 0;
367 :
368 0 : ulong next = map_chain_idx_next_const( ctx->flush_pool_idx, ULONG_MAX, ctx->pool );
369 0 : map_chain_idx_remove_fast( ctx->map_chain, ctx->flush_pool_idx, ctx->pool );
370 0 : if( FD_LIKELY( publish_txn( ctx, stem, pool_ele( ctx->pool, ctx->flush_pool_idx ) ) ) ) {
371 0 : ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_PUBLISHED_IDX ]++;
372 0 : } else {
373 0 : ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_REMOVED_IDX ]++;
374 0 : }
375 0 : lru_list_idx_remove( ctx->lru_list, ctx->flush_pool_idx, ctx->pool );
376 0 : pool_idx_release( ctx->pool, ctx->flush_pool_idx );
377 0 : ctx->flush_pool_idx = next;
378 0 : }
379 :
380 : /* Returns 0 if not a durable nonce transaction and 1 if it may be a
381 : durable nonce transaction */
382 :
383 : FD_FN_PURE static inline int
384 : fd_resolv_is_durable_nonce( fd_txn_t const * txn,
385 0 : uchar const * payload ) {
386 0 : if( FD_UNLIKELY( txn->instr_cnt==0 ) ) return 0;
387 :
388 0 : fd_txn_instr_t const * ix0 = &txn->instr[ 0 ];
389 0 : fd_acct_addr_t const * prog0 = fd_txn_get_acct_addrs( txn, payload ) + ix0->program_id;
390 : /* First instruction must be SystemProgram nonceAdvance instruction */
391 0 : fd_acct_addr_t const system_program[1] = { { { SYS_PROG_ID } } };
392 0 : if( FD_LIKELY( memcmp( prog0, system_program, sizeof(fd_acct_addr_t) ) ) ) return 0;
393 :
394 : /* instruction with three accounts and a four byte instruction data, a
395 : little-endian uint value 4 */
396 0 : if( FD_UNLIKELY( (ix0->data_sz!=4) | (ix0->acct_cnt!=3) ) ) return 0;
397 :
398 0 : return fd_uint_load_4( payload + ix0->data_off )==4U;
399 0 : }
400 :
401 : static inline void
402 : after_frag( fd_resolv_ctx_t * ctx,
403 : ulong in_idx,
404 : ulong seq,
405 : ulong sig,
406 : ulong sz,
407 : ulong tsorig,
408 : ulong _tspub,
409 0 : fd_stem_context_t * stem ) {
410 0 : (void)seq;
411 0 : (void)sz;
412 0 : (void)_tspub;
413 :
414 0 : if( FD_UNLIKELY( ctx->in[in_idx].kind==IN_KIND_REPLAY ) ) {
415 0 : switch( sig ) {
416 0 : case REPLAY_SIG_SLOT_COMPLETED: {
417 0 : fd_replay_slot_completed_t const * msg = &ctx->_completed_slot_msg;
418 :
419 : /* Equivocating slot with same blockhash, ignore. See fd_txncache.h on how this is possible.
420 : TODO make sure matches how agave handles it */
421 0 : if( FD_UNLIKELY( map_query( ctx->blockhash_map, *(blockhash_t *)msg->block_hash.uc, NULL ) ) ) {
422 0 : FD_LOG_WARNING(( "slot with same blockhash, ignoring: %lu", msg->slot ));
423 0 : return;
424 0 : }
425 :
426 : /* blockhash_ring is initialized to all zeros. blockhash=0 is an illegal map query */
427 0 : if( FD_UNLIKELY( memcmp( &ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ], (uchar[ 32UL ]){ 0UL }, sizeof(blockhash_t) ) ) ) {
428 0 : blockhash_map_t * entry = map_query( ctx->blockhash_map, ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ], NULL );
429 0 : if( FD_LIKELY( entry ) ) map_remove( ctx->blockhash_map, entry );
430 0 : }
431 :
432 0 : memcpy( ctx->blockhash_ring[ ctx->blockhash_ring_idx%BLOCKHASH_RING_LEN ].b, msg->block_hash.uc, 32UL );
433 0 : ctx->blockhash_ring_idx++;
434 :
435 0 : blockhash_map_t * blockhash = map_insert( ctx->blockhash_map, *(blockhash_t *)msg->block_hash.uc );
436 0 : blockhash->slot = msg->slot;
437 :
438 0 : blockhash_t * hash = (blockhash_t *)msg->block_hash.uc;
439 0 : ctx->flush_pool_idx = map_chain_idx_query_const( ctx->map_chain, &hash, ULONG_MAX, ctx->pool );
440 0 : ctx->flushing_slot = msg->slot;
441 :
442 0 : ctx->completed_slot = msg->slot;
443 0 : break;
444 0 : }
445 0 : case REPLAY_SIG_ROOT_ADVANCED: {
446 0 : fd_replay_root_advanced_t const * msg = &ctx->_rooted_slot_msg;
447 :
448 : /* Replace current bank with new bank */
449 0 : fd_bank_t * prev_bank = ctx->bank;
450 :
451 0 : ctx->bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
452 0 : FD_TEST( ctx->bank );
453 :
454 : /* Send slot completed message back to replay, so it can
455 : decrement the reference count of the previous bank. */
456 0 : if( FD_LIKELY( prev_bank ) ) {
457 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
458 0 : fd_resolv_slot_exchanged_t * slot_exchanged =
459 0 : fd_type_pun( fd_chunk_to_laddr( ctx->out_replay->mem, ctx->out_replay->chunk ) );
460 0 : slot_exchanged->bank_idx = prev_bank->idx;
461 0 : fd_stem_publish( stem, 1UL, 0UL, ctx->out_replay->chunk, sizeof(fd_resolv_slot_exchanged_t), 0UL, tsorig, tspub );
462 0 : ctx->out_replay->chunk = fd_dcache_compact_next( ctx->out_replay->chunk, sizeof(fd_resolv_slot_exchanged_t), ctx->out_replay->chunk0, ctx->out_replay->wmark );
463 0 : }
464 :
465 0 : break;
466 0 : }
467 0 : default: break;
468 0 : }
469 0 : return;
470 0 : }
471 :
472 0 : fd_txn_m_t * txnm = (fd_txn_m_t *)fd_chunk_to_laddr( ctx->out_pack->mem, ctx->out_pack->chunk );
473 0 : FD_TEST( txnm->payload_sz<=FD_TPU_MTU );
474 0 : FD_TEST( txnm->txn_t_sz<=FD_TXN_MAX_SZ );
475 0 : fd_txn_t const * txnt = fd_txn_m_txn_t( txnm );
476 :
477 : /* If we find the recent blockhash, life is simple. We drop
478 : transactions that couldn't possibly execute any more, and forward
479 : to pack ones that could.
480 :
481 : If we can't find the recent blockhash ... it means one of four
482 : things,
483 :
484 : (1) The blockhash is really old (more than 19 days) or just
485 : non-existent.
486 : (2) The blockhash is not that old, but was created before this
487 : validator was started.
488 : (3) It's really new (we haven't seen the bank yet).
489 : (4) It's a durable nonce transaction, or part of a bundle (just let
490 : it pass).
491 :
492 : For durable nonce transactions, there isn't much we can do except
493 : pass them along and see if they execute.
494 :
495 : For the other three cases ... we don't want to flood pack with what
496 : might be junk transactions, so we accumulate them into a local
497 : buffer. If we later see the blockhash come to exist, we forward any
498 : buffered transactions to back. */
499 :
500 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id && (txnm->block_engine.bundle_id!=ctx->bundle_id) ) ) {
501 0 : ctx->bundle_failed = 0;
502 0 : ctx->bundle_id = txnm->block_engine.bundle_id;
503 0 : }
504 :
505 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id && ctx->bundle_failed ) ) {
506 0 : ctx->metrics.bundle_peer_failure++;
507 0 : return;
508 0 : }
509 :
510 0 : txnm->reference_slot = ctx->completed_slot;
511 :
512 0 : blockhash_t const * recent_blockhash = (blockhash_t const *)( fd_txn_m_payload( txnm )+txnt->recent_blockhash_off );
513 0 : blockhash_map_t const * blockhash = NULL;
514 0 : if( FD_LIKELY( !map_key_inval( *recent_blockhash ) ) ) {
515 0 : blockhash = map_query_const( ctx->blockhash_map, *recent_blockhash, NULL );
516 0 : }
517 0 : if( FD_LIKELY( blockhash ) ) {
518 0 : txnm->reference_slot = blockhash->slot;
519 0 : if( FD_UNLIKELY( txnm->reference_slot+151UL<ctx->completed_slot ) ) {
520 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
521 0 : ctx->metrics.blockhash_expired++;
522 0 : return;
523 0 : }
524 0 : }
525 :
526 0 : int is_bundle_member = !!txnm->block_engine.bundle_id;
527 0 : int is_durable_nonce = fd_resolv_is_durable_nonce( txnt, fd_txn_m_payload( txnm ) );
528 :
529 0 : if( FD_UNLIKELY( !is_bundle_member && !is_durable_nonce && !blockhash ) ) {
530 0 : ulong pool_idx;
531 0 : if( FD_UNLIKELY( !pool_free( ctx->pool ) ) ) {
532 0 : pool_idx = lru_list_idx_pop_tail( ctx->lru_list, ctx->pool );
533 0 : map_chain_idx_remove_fast( ctx->map_chain, pool_idx, ctx->pool );
534 0 : ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_OVERRUN_IDX ]++;
535 0 : } else {
536 0 : pool_idx = pool_idx_acquire( ctx->pool );
537 0 : }
538 :
539 0 : fd_stashed_txn_m_t * stash_txn = pool_ele( ctx->pool, pool_idx );
540 : /* There's a compiler bug in GCC version 12 (at least 12.1, 12.3 and
541 : 12.4) that cause it to think stash_txn is a null pointer. It
542 : then complains that the memcpy is bad and refuses to compile the
543 : memcpy below. It is possible for pool_ele to return NULL, but
544 : that can't happen because if pool_free is 0, then all the pool
545 : elements must be in the LRU list, so idx_pop_tail won't return
546 : IDX_NULL; and if pool_free returns non-zero, then
547 : pool_idx_acquire won't return POOL_IDX_NULL. */
548 0 : FD_COMPILER_FORGET( stash_txn );
549 0 : fd_memcpy( stash_txn->_, txnm, fd_txn_m_realized_footprint( txnm, 1, 0 ) );
550 0 : stash_txn->blockhash = (blockhash_t *)(fd_txn_m_payload( (fd_txn_m_t *)(stash_txn->_) ) + txnt->recent_blockhash_off);
551 0 : ctx->metrics.stash[ FD_METRICS_ENUM_RESOLVE_STASH_OPERATION_V_INSERTED_IDX ]++;
552 :
553 0 : map_chain_ele_insert( ctx->map_chain, stash_txn, ctx->pool );
554 0 : lru_list_idx_push_head( ctx->lru_list, pool_idx, ctx->pool );
555 :
556 0 : return;
557 0 : }
558 :
559 0 : if( FD_UNLIKELY( txnt->addr_table_adtl_cnt ) ) {
560 0 : if( FD_UNLIKELY( !ctx->bank ) ) {
561 0 : FD_MCNT_INC( RESOLV, TXN_NO_BANK, 1 );
562 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
563 0 : return;
564 0 : }
565 :
566 0 : int result = peek_aluts( ctx, txnm );
567 0 : if( FD_UNLIKELY( result ) ) {
568 0 : if( FD_UNLIKELY( txnm->block_engine.bundle_id ) ) ctx->bundle_failed = 1;
569 0 : return;
570 0 : }
571 0 : }
572 :
573 0 : ulong realized_sz = fd_txn_m_realized_footprint( txnm, 1, 1 );
574 0 : ulong tspub = fd_frag_meta_ts_comp( fd_tickcount() );
575 0 : fd_stem_publish( stem, 0UL, txnm->reference_slot, ctx->out_pack->chunk, realized_sz, 0UL, tsorig, tspub );
576 0 : ctx->out_pack->chunk = fd_dcache_compact_next( ctx->out_pack->chunk, realized_sz, ctx->out_pack->chunk0, ctx->out_pack->wmark );
577 0 : }
578 :
579 : static void
580 : unprivileged_init( fd_topo_t const * topo,
581 0 : fd_topo_tile_t const * tile ) {
582 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
583 :
584 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
585 0 : fd_resolv_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_resolv_ctx_t ), sizeof( fd_resolv_ctx_t ) );
586 :
587 0 : ctx->round_robin_cnt = fd_topo_tile_name_cnt( topo, tile->name );
588 0 : ctx->round_robin_idx = tile->kind_id;
589 :
590 0 : ctx->bundle_failed = 0;
591 0 : ctx->bundle_id = 0UL;
592 :
593 0 : ctx->completed_slot = 0UL;
594 0 : ctx->blockhash_ring_idx = 0UL;
595 :
596 0 : ctx->flush_pool_idx = ULONG_MAX;
597 :
598 0 : ctx->pool = pool_join( pool_new( FD_SCRATCH_ALLOC_APPEND( l, pool_align(), pool_footprint( 1UL<<16UL ) ), 1UL<<16UL ) );
599 0 : FD_TEST( ctx->pool );
600 :
601 0 : ctx->map_chain = map_chain_join( map_chain_new( FD_SCRATCH_ALLOC_APPEND( l, map_chain_align(), map_chain_footprint( 8192ULL ) ), 8192UL , 0UL ) );
602 0 : FD_TEST( ctx->map_chain );
603 :
604 0 : FD_TEST( ctx->lru_list==lru_list_join( lru_list_new( ctx->lru_list ) ) );
605 :
606 0 : memset( ctx->blockhash_ring, 0, sizeof( ctx->blockhash_ring ) );
607 0 : memset( &ctx->metrics, 0, sizeof( ctx->metrics ) );
608 :
609 0 : ctx->blockhash_map = map_join( map_new( FD_SCRATCH_ALLOC_APPEND( l, map_align(), map_footprint() ) ) );
610 0 : FD_TEST( ctx->blockhash_map );
611 :
612 0 : FD_TEST( tile->in_cnt<=sizeof( ctx->in )/sizeof( ctx->in[ 0 ] ) );
613 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
614 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
615 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
616 :
617 0 : if( FD_LIKELY( !strcmp( link->name, "replay_out" ) ) ) ctx->in[ i ].kind = IN_KIND_REPLAY;
618 0 : else if( FD_LIKELY( !strcmp( link->name, "dedup_resolv" ) ) ) ctx->in[ i ].kind = IN_KIND_DEDUP;
619 0 : else FD_LOG_ERR(( "unknown in link name '%s'", link->name ));
620 :
621 0 : ctx->in[i].mem = link_wksp->wksp;
622 0 : ctx->in[i].chunk0 = fd_dcache_compact_chunk0( ctx->in[i].mem, link->dcache );
623 0 : ctx->in[i].wmark = fd_dcache_compact_wmark ( ctx->in[i].mem, link->dcache, link->mtu );
624 0 : ctx->in[i].mtu = link->mtu;
625 0 : }
626 :
627 0 : ctx->out_pack->mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
628 0 : ctx->out_pack->chunk0 = fd_dcache_compact_chunk0( ctx->out_pack->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
629 0 : ctx->out_pack->wmark = fd_dcache_compact_wmark ( ctx->out_pack->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
630 0 : ctx->out_pack->chunk = ctx->out_pack->chunk0;
631 :
632 0 : ctx->out_replay->mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 1 ] ].dcache_obj_id ].wksp_id ].wksp;
633 0 : ctx->out_replay->chunk0 = fd_dcache_compact_chunk0( ctx->out_replay->mem, topo->links[ tile->out_link_id[ 1 ] ].dcache );
634 0 : ctx->out_replay->wmark = fd_dcache_compact_wmark ( ctx->out_replay->mem, topo->links[ tile->out_link_id[ 1 ] ].dcache, topo->links[ tile->out_link_id[ 1 ] ].mtu );
635 0 : ctx->out_replay->chunk = ctx->out_replay->chunk0;
636 :
637 0 : ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
638 0 : FD_TEST( banks_obj_id!=ULONG_MAX );
639 0 : ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
640 0 : FD_TEST( ctx->banks );
641 0 : ctx->bank = NULL;
642 :
643 : /* Read-only join to accdb. The accdb workspace is mapped PROT_READ
644 : in this tile (see topology); the only writable external mapping
645 : is our private epoch fseq. FD_ACCDB_FD_RO is the O_RDONLY dup
646 : of the accdb data file. */
647 0 : void * _accdb_join = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->resolv.max_live_slots ) );
648 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->resolv.accdb_obj_id );
649 0 : fd_accdb_shmem_t * accdb_shmem_ro = fd_accdb_shmem_join( _accdb_shmem );
650 0 : FD_TEST( accdb_shmem_ro );
651 0 : ulong * epoch_fseq = fd_fseq_join( fd_topo_obj_laddr( topo, tile->resolv.accdb_epoch_fseq_obj_id ) );
652 0 : FD_TEST( epoch_fseq );
653 0 : ctx->accdb = fd_accdb_join_readonly( _accdb_join, accdb_shmem_ro, epoch_fseq, FD_ACCDB_FD_RO );
654 0 : FD_TEST( ctx->accdb );
655 :
656 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
657 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
658 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
659 :
660 0 : fd_sleep_until_replay_started( topo );
661 0 : }
662 :
663 : static ulong
664 : populate_allowed_seccomp( fd_topo_t const * topo,
665 : fd_topo_tile_t const * tile,
666 : ulong out_cnt,
667 0 : struct sock_filter * out ) {
668 0 : (void)topo;
669 0 : (void)tile;
670 :
671 0 : populate_sock_filter_policy_fd_resolv_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)FD_ACCDB_FD_RO );
672 0 : return sock_filter_policy_fd_resolv_tile_instr_cnt;
673 0 : }
674 :
675 : static ulong
676 : populate_allowed_fds( fd_topo_t const * topo,
677 : fd_topo_tile_t const * tile,
678 : ulong out_fds_cnt,
679 0 : int * out_fds ) {
680 0 : (void)topo;
681 0 : (void)tile;
682 :
683 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
684 :
685 0 : ulong out_cnt = 0UL;
686 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
687 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
688 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
689 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RO; /* accounts db readonly fd */
690 0 : return out_cnt;
691 0 : }
692 :
693 0 : #define STEM_BURST (1UL)
694 :
695 : /* The default STEM_LAZY is derived from cr_max, which is the minimum
696 : depth among all reliably-consumed output links. The resolv_replay
697 : link (depth 4096) dominates this, even though it only carries ~2-3
698 : msgs/s. This makes housekeeping fire ~16x more often than necessary.
699 : We override with roughly what the default would be without accounting
700 : for it. */
701 0 : #define STEM_LAZY (128000L) /* 128 us */
702 :
703 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_resolv_ctx_t
704 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_resolv_ctx_t)
705 :
706 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
707 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
708 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
709 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
710 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
711 :
712 : #include "../../disco/stem/fd_stem.c"
713 :
714 : fd_topo_run_tile_t fd_tile_resolv = {
715 : .name = "resolv",
716 : .populate_allowed_seccomp = populate_allowed_seccomp,
717 : .populate_allowed_fds = populate_allowed_fds,
718 : .scratch_align = scratch_align,
719 : .scratch_footprint = scratch_footprint,
720 : .unprivileged_init = unprivileged_init,
721 : .run = stem_run,
722 : };
|