Line data Source code
1 : #include "../execle/fd_execle_err.h"
2 : #include "../../util/pod/fd_pod_format.h"
3 : #include "../../disco/metrics/fd_metrics.h"
4 :
5 : #include "../../choreo/tower/fd_tower_serdes.h"
6 : #include "../../discof/fd_startup.h"
7 : #include "../../discof/replay/fd_execrp.h"
8 : #include "../../flamenco/runtime/fd_bank.h"
9 : #include "../../flamenco/runtime/fd_txncache.h"
10 : #include "../../flamenco/runtime/fd_runtime.h"
11 : #include "../../flamenco/runtime/fd_executor.h"
12 : #include "../../flamenco/runtime/fd_cost_tracker.h"
13 : #include "../../flamenco/runtime/tests/fd_dump_pb.h"
14 : #include "../../flamenco/progcache/fd_progcache_user.h"
15 : #include "../../flamenco/log_collector/fd_log_collector_base.h"
16 : #include "../../disco/metrics/fd_metrics.h"
17 : #include "../../disco/events/generated/fd_event_gen.h"
18 : #include "../../flamenco/events/fd_event_runtime.h"
19 :
20 : #include <time.h>
21 : #include "generated/fd_execrp_tile_seccomp.h"
22 :
23 : /* The exec tile is responsible for executing single transactions. The
24 : tile receives a parsed transaction (fd_txn_p_t) and an identifier to
25 : which bank to execute against (index into the bank pool). With this,
26 : the exec tile is able to identify the correct bank and accounts
27 : database fork to execute the transaction against. The exec tile then
28 : commits the results of the transaction to the accounts db and makes
29 : any necessary updates to the bank. */
30 :
31 : typedef struct link_ctx {
32 : ulong idx;
33 : fd_wksp_t * mem;
34 : ulong chunk;
35 : ulong chunk0;
36 : ulong wmark;
37 : } link_ctx_t;
38 :
39 : struct fd_execrp_tile {
40 : ulong tile_idx;
41 :
42 : fd_startup_gate_t startup_gate[1];
43 :
44 : /* link-related data structures. */
45 : link_ctx_t replay_in[ 1 ];
46 : link_ctx_t execrp_replay_out[ 1 ]; /* TODO: Remove with solcap v2 */
47 :
48 : fd_sha512_t sha_mem[ FD_TXN_SIG_MAX ];
49 : fd_sha512_t * sha_lj[ FD_TXN_SIG_MAX ];
50 :
51 : /* Capture context for debugging runtime execution. */
52 : fd_capture_ctx_t * capture_ctx;
53 : fd_capture_link_buf_t cap_execrp_out[1];
54 :
55 : /* Protobuf dumping context for debugging runtime execution and
56 : collecting seed corpora. */
57 : fd_dump_proto_ctx_t * dump_proto_ctx;
58 : fd_txn_dump_ctx_t * txn_dump_ctx;
59 :
60 : fd_banks_t * banks;
61 : fd_bank_t * bank;
62 : fd_accdb_t * accdb;
63 : fd_txncache_t * txncache;
64 : fd_progcache_t progcache[1];
65 :
66 : ulong txn_idx;
67 : ulong slot;
68 : ulong dispatch_time_comp;
69 :
70 : fd_log_collector_t log_collector;
71 :
72 : fd_txn_in_t txn_in;
73 : fd_txn_out_t txn_out;
74 :
75 : fd_runtime_t runtime[1];
76 :
77 : struct {
78 : ulong sigverify_cnt;
79 : ulong poh_hash_cnt;
80 :
81 : /* Ticks spent loading txn accounts */
82 : ulong txn_load_cum_ticks;
83 :
84 : /* Ticks spent validating txn invariants (e.g. status cache, fee payer) */
85 : ulong txn_check_cum_ticks;
86 :
87 : /* Ticks spent executing a txn (includes any VM time) */
88 : ulong txn_exec_cum_ticks;
89 :
90 : /* Ticks spent committing a txn (database writes) */
91 : ulong txn_commit_cum_ticks;
92 :
93 : ulong txn_result[ FD_METRICS_ENUM_TRANSACTION_RESULT_CNT ];
94 : ulong txn_version[ FD_METRICS_ENUM_TXN_VERSION_CNT ];
95 : } metrics;
96 :
97 : };
98 :
99 : typedef struct fd_execrp_tile fd_execrp_tile_t;
100 :
101 : FD_FN_CONST static inline ulong
102 0 : scratch_align( void ) {
103 0 : return 128UL;
104 0 : }
105 :
106 : FD_FN_PURE static inline ulong
107 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
108 0 : ulong l = FD_LAYOUT_INIT;
109 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_execrp_tile_t), sizeof(fd_execrp_tile_t) );
110 0 : l = FD_LAYOUT_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->execrp.max_live_slots ) );
111 0 : l = FD_LAYOUT_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->execrp.max_live_slots ) );
112 0 : l = FD_LAYOUT_APPEND( l, FD_PROGCACHE_SCRATCH_ALIGN, FD_PROGCACHE_SCRATCH_FOOTPRINT );
113 :
114 0 : if( FD_UNLIKELY( strlen( tile->execrp.solcap_capture ) ) ) {
115 0 : l = FD_LAYOUT_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
116 0 : }
117 :
118 0 : if( FD_UNLIKELY( strlen( tile->execrp.dump_proto_dir ) ) ) {
119 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
120 0 : l = FD_LAYOUT_APPEND( l, fd_txn_dump_context_align(), fd_txn_dump_context_footprint() );
121 0 : if( FD_UNLIKELY( tile->execrp.dump_instr_to_pb || tile->execrp.dump_syscall_to_pb || tile->execrp.dump_txn_to_pb ) ) {
122 0 : l = FD_LAYOUT_APPEND( l, FD_SPAD_ALIGN, FD_SPAD_FOOTPRINT( 1UL<<28UL ) );
123 0 : }
124 0 : }
125 :
126 0 : return FD_LAYOUT_FINI( l, scratch_align() );
127 0 : }
128 :
129 : static void
130 0 : metrics_write( fd_execrp_tile_t * ctx ) {
131 0 : fd_accdb_flush_metrics( ctx->accdb );
132 :
133 0 : FD_MCNT_SET ( EXECRP, SIGNATURE_VERIFIED, ctx->metrics.sigverify_cnt );
134 0 : FD_MCNT_SET ( EXECRP, POH_HASHED, ctx->metrics.poh_hash_cnt );
135 0 : FD_MCNT_ENUM_COPY( EXECRP, TXN_RESULT, ctx->metrics.txn_result );
136 0 : FD_MCNT_ENUM_COPY( EXECRP, TXN_VERSION, ctx->metrics.txn_version );
137 :
138 0 : FD_PROGCACHE_METRICS_WRITE( EXECRP, ctx->progcache->metrics );
139 :
140 0 : FD_MCNT_SET( EXECRP, TXN_REGIME_DURATION_NANOS_SETUP, ctx->metrics.txn_load_cum_ticks+ctx->metrics.txn_check_cum_ticks );
141 0 : FD_MCNT_SET( EXECRP, TXN_REGIME_DURATION_NANOS_EXEC, ctx->metrics.txn_exec_cum_ticks );
142 0 : FD_MCNT_SET( EXECRP, TXN_REGIME_DURATION_NANOS_COMMIT, ctx->metrics.txn_commit_cum_ticks );
143 :
144 0 : fd_runtime_t const * runtime = ctx->runtime;
145 0 : ulong cpi_ticks = runtime->metrics.cpi_setup_cum_ticks +
146 0 : runtime->metrics.cpi_commit_cum_ticks;
147 0 : ulong exec_ticks = fd_ulong_sat_sub( runtime->metrics.vm_exec_cum_ticks, cpi_ticks );
148 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_SETUP, runtime->metrics.vm_setup_cum_ticks );
149 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_COMMIT, runtime->metrics.vm_commit_cum_ticks );
150 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_SETUP_CPI, runtime->metrics.cpi_setup_cum_ticks );
151 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_COMMIT_CPI, runtime->metrics.cpi_commit_cum_ticks );
152 0 : FD_MCNT_SET( EXECRP, VM_REGIME_DURATION_NANOS_INTERPRETER, exec_ticks );
153 :
154 0 : FD_MCNT_SET( EXECRP, CU_EXECUTED, runtime->metrics.cu_cum );
155 0 : FD_MCNT_SET( EXECRP, INSTRUCTION_EXECUTED, runtime->metrics.instr_cum );
156 0 : FD_MCNT_SET( EXECRP, CPI_EXECUTED, runtime->metrics.cpi_cum );
157 :
158 0 : FD_ACCDB_METRICS_WRITE( EXECRP, fd_accdb_metrics( ctx->accdb ) );
159 0 : }
160 :
161 : static void
162 : publish_txn_finalized_msg( fd_execrp_tile_t * ctx,
163 : fd_stem_context_t * stem,
164 0 : long now ) {
165 0 : uint compute_units_consumed = 0U;
166 0 : if( FD_LIKELY( ctx->txn_out.err.is_committable ) ) {
167 0 : fd_usage_cost_details_t const * cost = &ctx->txn_out.details.txn_cost.transaction;
168 0 : compute_units_consumed = cost->signature_cost + cost->write_lock_cost + cost->data_bytes_cost + cost->programs_execution_cost + cost->loaded_accounts_data_size_cost;
169 0 : }
170 :
171 0 : fd_execrp_task_done_msg_t * msg = fd_chunk_to_laddr( ctx->execrp_replay_out->mem, ctx->execrp_replay_out->chunk );
172 0 : msg->bank_idx = ctx->bank->idx;
173 0 : msg->txn_exec->txn_idx = ctx->txn_idx;
174 0 : msg->txn_exec->is_committable = ctx->txn_out.err.is_committable;
175 0 : msg->txn_exec->is_fees_only = ctx->txn_out.err.is_fees_only;
176 0 : msg->txn_exec->is_noop = ctx->txn_out.err.is_noop;
177 0 : msg->txn_exec->txn_err = ctx->txn_out.err.txn_err;
178 0 : msg->txn_exec->is_simple_vote = ctx->txn_out.details.is_simple_vote;
179 :
180 0 : msg->txn_exec->tick_load_start = ctx->txn_out.details.load_start_ticks;
181 0 : msg->txn_exec->tick_check_start = ctx->txn_out.details.check_start_ticks;
182 0 : msg->txn_exec->tick_exec_start = ctx->txn_out.details.exec_start_ticks;
183 0 : msg->txn_exec->tick_commit_start = ctx->txn_out.details.commit_start_ticks;
184 0 : msg->txn_exec->tick_commit_end = fd_long_if( ctx->txn_out.details.commit_start_ticks==LONG_MAX, LONG_MAX, now );
185 :
186 0 : msg->txn_exec->compute_units_consumed = compute_units_consumed;
187 0 : msg->txn_exec->transaction_fee = ctx->txn_out.details.execution_fee;
188 0 : msg->txn_exec->priority_fee = ctx->txn_out.details.priority_fee;
189 0 : msg->txn_exec->tips = ctx->txn_out.details.tips;
190 0 : msg->txn_exec->slot = ctx->slot;
191 0 : msg->txn_exec->bank_seq = ctx->bank->bank_seq;
192 0 : msg->txn_exec->start_shred_idx = ctx->txn_in.txn->start_shred_idx;
193 0 : msg->txn_exec->end_shred_idx = ctx->txn_in.txn->end_shred_idx;
194 :
195 0 : int is_vote = 0;
196 0 : if( FD_LIKELY( ctx->txn_out.details.is_simple_vote ) ) {
197 0 : fd_txn_t const * txn = TXN( ctx->txn_in.txn );
198 0 : uchar const * payload = ctx->txn_in.txn->payload;
199 0 : fd_compact_tower_sync_serde_t tower_sync[ 1 ];
200 0 : if( FD_LIKELY( fd_txn_parse_simple_vote( txn, payload, tower_sync ) ) ) {
201 0 : fd_acct_addr_t const * accs = fd_txn_get_acct_addrs( txn, payload );
202 0 : *msg->txn_exec->vote.identity = *(fd_pubkey_t const *)fd_type_pun_const( &accs[ 0 ] );
203 0 : *msg->txn_exec->vote.vote_acct = *(fd_pubkey_t const *)fd_type_pun_const( &accs[ txn->signature_cnt==1 ? 1 : 2 ] );
204 :
205 0 : ulong cur = fd_ulong_if( tower_sync->root==ULONG_MAX, 0UL, tower_sync->root );
206 0 : ulong cnt = 0UL;
207 0 : int overflow = 0;
208 0 : for( ulong i=0UL; i<tower_sync->lockouts_cnt; i++ ) {
209 0 : if( FD_UNLIKELY( __builtin_uaddl_overflow( cur, tower_sync->lockouts[ i ].offset, &cur ) ) ) { overflow = 1; break; }
210 0 : msg->txn_exec->vote.vote_slots[ cnt++ ] = cur;
211 0 : }
212 0 : if( FD_LIKELY( !overflow ) ) {
213 0 : msg->txn_exec->vote.vote_slot_cnt = (uchar)cnt;
214 0 : msg->txn_exec->vote.slot = cnt ? msg->txn_exec->vote.vote_slots[ cnt-1UL ] : cur;
215 0 : is_vote = 1;
216 0 : }
217 0 : }
218 0 : }
219 :
220 0 : if( FD_UNLIKELY( !is_vote ) ) {
221 0 : msg->txn_exec->vote.slot = ULONG_MAX;
222 0 : msg->txn_exec->vote.vote_slot_cnt = (uchar)0;
223 0 : *msg->txn_exec->vote.identity = (fd_pubkey_t){ 0 };
224 0 : *msg->txn_exec->vote.vote_acct = (fd_pubkey_t){ 0 };
225 0 : }
226 :
227 0 : if( FD_UNLIKELY( !msg->txn_exec->is_committable ) ) {
228 0 : uchar * signature = (uchar *)ctx->txn_in.txn->payload + TXN( ctx->txn_in.txn )->signature_off;
229 0 : FD_BASE58_ENCODE_64_BYTES( signature, signature_b58 );
230 0 : FD_LOG_WARNING(( "block marked dead (slot=%lu) because of invalid transaction (signature=%s) (txn_err=%d)", ctx->slot, signature_b58, ctx->txn_out.err.txn_err ));
231 0 : }
232 :
233 0 : fd_stem_publish( stem, ctx->execrp_replay_out->idx, (FD_EXECRP_TT_TXN_EXEC<<32)|ctx->tile_idx, ctx->execrp_replay_out->chunk, sizeof(*msg), 0UL, ctx->dispatch_time_comp, fd_frag_meta_ts_comp( fd_tickcount() ) );
234 :
235 0 : ctx->execrp_replay_out->chunk = fd_dcache_compact_next( ctx->execrp_replay_out->chunk, sizeof(*msg), ctx->execrp_replay_out->chunk0, ctx->execrp_replay_out->wmark );
236 0 : }
237 :
238 : static inline void
239 : after_credit( fd_execrp_tile_t * ctx,
240 : fd_stem_context_t * stem,
241 : int * opt_poll_in,
242 0 : int * charge_busy ) {
243 0 : (void)stem; (void)opt_poll_in; (void)charge_busy;
244 0 : fd_startup_gate_idle( ctx->startup_gate );
245 0 : }
246 :
247 : static inline int
248 : returnable_frag( fd_execrp_tile_t * ctx,
249 : ulong in_idx,
250 : ulong seq FD_PARAM_UNUSED,
251 : ulong sig,
252 : ulong chunk,
253 : ulong sz,
254 : ulong ctl FD_PARAM_UNUSED,
255 : ulong tsorig FD_PARAM_UNUSED,
256 : ulong tspub,
257 0 : fd_stem_context_t * stem ) {
258 0 : fd_startup_gate_busy( ctx->startup_gate );
259 :
260 0 : if( (sig&0xFFFFFFFFUL)!=ctx->tile_idx ) return 0;
261 :
262 0 : FD_MGAUGE_SET( EXECRP, PROCESSING, 1UL );
263 :
264 0 : if( FD_LIKELY( in_idx==ctx->replay_in->idx ) ) {
265 0 : if( FD_UNLIKELY( chunk < ctx->replay_in->chunk0 || chunk > ctx->replay_in->wmark ) ) {
266 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu]", chunk, sz, ctx->replay_in->chunk0, ctx->replay_in->wmark ));
267 0 : }
268 0 : switch( sig>>32 ) {
269 0 : case FD_EXECRP_TT_TXN_EXEC: {
270 : /* Execute. */
271 0 : fd_execrp_txn_exec_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
272 0 : ctx->bank = fd_banks_bank_query( ctx->banks, msg->bank_idx );
273 0 : FD_TEST( ctx->bank );
274 0 : ctx->txn_in.txn = msg->txn;
275 0 : memcpy( ctx->txn_in.fec_merkle_root, msg->fec_merkle_root, 32UL );
276 0 : ctx->txn_in.index_in_slot = msg->index_in_slot;
277 :
278 : /* Set the capture txn index from the message so account updates
279 : during commit are recorded with the correct transaction index. */
280 0 : if( FD_UNLIKELY( ctx->capture_ctx ) ) {
281 0 : ctx->capture_ctx->current_txn_idx = msg->capture_txn_idx;
282 0 : }
283 :
284 0 : fd_runtime_prepare_and_execute_txn( ctx->runtime, ctx->bank, &ctx->txn_in, &ctx->txn_out );
285 :
286 0 : ctx->metrics.txn_result[ fd_execle_err_from_runtime_err( ctx->txn_out.err.txn_err ) ]++;
287 0 : ctx->metrics.txn_version[ fd_execle_version_from_txn( TXN( ctx->txn_in.txn ) ) ]++;
288 :
289 0 : if( FD_LIKELY( ctx->txn_out.err.is_committable ) ) {
290 0 : fd_runtime_commit_txn( ctx->runtime, ctx->bank, &ctx->txn_in, &ctx->txn_out );
291 0 : } else {
292 0 : fd_runtime_cancel_txn( ctx->runtime, ctx->bank, &ctx->txn_in, &ctx->txn_out );
293 0 : }
294 :
295 0 : long const txn_end_ticks = fd_tickcount();
296 :
297 : /* Notify replay. */
298 0 : ctx->txn_idx = msg->txn_idx;
299 0 : ctx->dispatch_time_comp = tspub;
300 0 : ctx->slot = ctx->bank->f.slot;
301 0 : publish_txn_finalized_msg( ctx, stem, txn_end_ticks );
302 :
303 : /* Update metrics */
304 0 : ulong load_start_ticks_dt = fd_ulong_if( ctx->txn_out.details.check_start_ticks==LONG_MAX || ctx->txn_out.details.load_start_ticks==LONG_MAX, 0UL, (ulong)( ctx->txn_out.details.check_start_ticks - ctx->txn_out.details.load_start_ticks ) );
305 0 : ulong check_start_ticks_dt = fd_ulong_if( ctx->txn_out.details.exec_start_ticks==LONG_MAX || ctx->txn_out.details.check_start_ticks==LONG_MAX, 0UL, (ulong)( ctx->txn_out.details.exec_start_ticks - ctx->txn_out.details.check_start_ticks ) );
306 0 : ulong exec_start_ticks_dt = fd_ulong_if( ctx->txn_out.details.commit_start_ticks==LONG_MAX || ctx->txn_out.details.exec_start_ticks==LONG_MAX, 0UL, (ulong)( ctx->txn_out.details.commit_start_ticks - ctx->txn_out.details.exec_start_ticks ) );
307 0 : ulong commit_ticks_dt = fd_ulong_if( txn_end_ticks==LONG_MAX || ctx->txn_out.details.commit_start_ticks==LONG_MAX, 0UL, (ulong)( txn_end_ticks - ctx->txn_out.details.commit_start_ticks ) );
308 :
309 0 : ctx->metrics.txn_load_cum_ticks += load_start_ticks_dt;
310 0 : ctx->metrics.txn_check_cum_ticks += check_start_ticks_dt;
311 0 : ctx->metrics.txn_exec_cum_ticks += exec_start_ticks_dt;
312 0 : ctx->metrics.txn_commit_cum_ticks += commit_ticks_dt;
313 :
314 0 : break;
315 0 : }
316 0 : case FD_EXECRP_TT_TXN_SIGVERIFY: {
317 0 : fd_execrp_txn_sigverify_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
318 0 : int res = fd_executor_txn_verify( msg->txn, ctx->sha_lj );
319 0 : fd_execrp_task_done_msg_t * out_msg = fd_chunk_to_laddr( ctx->execrp_replay_out->mem, ctx->execrp_replay_out->chunk );
320 0 : out_msg->bank_idx = msg->bank_idx;
321 0 : out_msg->txn_sigverify->txn_idx = msg->txn_idx;
322 0 : out_msg->txn_sigverify->err = (res!=FD_RUNTIME_EXECUTE_SUCCESS);
323 0 : fd_stem_publish( stem, ctx->execrp_replay_out->idx, (FD_EXECRP_TT_TXN_SIGVERIFY<<32)|ctx->tile_idx, ctx->execrp_replay_out->chunk, sizeof(*out_msg), 0UL, 0UL, 0UL );
324 0 : ctx->execrp_replay_out->chunk = fd_dcache_compact_next( ctx->execrp_replay_out->chunk, sizeof(*out_msg), ctx->execrp_replay_out->chunk0, ctx->execrp_replay_out->wmark );
325 0 : ctx->metrics.sigverify_cnt += TXN( msg->txn )->signature_cnt;
326 0 : break;
327 0 : }
328 0 : case FD_EXECRP_TT_POH_HASH: {
329 0 : fd_execrp_poh_hash_msg_t * msg = fd_chunk_to_laddr( ctx->replay_in->mem, chunk );
330 0 : fd_execrp_task_done_msg_t * out_msg = fd_chunk_to_laddr( ctx->execrp_replay_out->mem, ctx->execrp_replay_out->chunk );
331 0 : FD_CHECK_CRIT( msg->cnt && msg->cnt<=FD_EXECRP_POH_PARA, "invalid poh batch" );
332 0 : out_msg->bank_idx = msg->bank_idx;
333 0 : out_msg->poh_hash->cnt = msg->cnt;
334 0 : ctx->metrics.poh_hash_cnt += msg->hashcnt*msg->cnt;
335 0 : fd_sha256_hash_32_repeated_batch( msg->hash, out_msg->poh_hash->hash, msg->hashcnt, msg->cnt );
336 0 : fd_stem_publish( stem, ctx->execrp_replay_out->idx, (FD_EXECRP_TT_POH_HASH<<32)|ctx->tile_idx, ctx->execrp_replay_out->chunk, sizeof(*out_msg), 0UL, 0UL, 0UL );
337 0 : ctx->execrp_replay_out->chunk = fd_dcache_compact_next( ctx->execrp_replay_out->chunk, sizeof(*out_msg), ctx->execrp_replay_out->chunk0, ctx->execrp_replay_out->wmark );
338 0 : break;
339 0 : }
340 0 : default: FD_LOG_CRIT(( "unexpected signature %lu", sig ));
341 0 : }
342 0 : } else FD_LOG_CRIT(( "invalid in_idx %lu", in_idx ));
343 :
344 0 : FD_MGAUGE_SET( EXECRP, PROCESSING, 0UL );
345 :
346 0 : return 0;
347 0 : }
348 :
349 : extern FD_TL int fd_wksp_oom_silent;
350 :
351 : static void
352 : unprivileged_init( fd_topo_t const * topo,
353 0 : fd_topo_tile_t const * tile ) {
354 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
355 :
356 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
357 0 : fd_execrp_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_execrp_tile_t), sizeof(fd_execrp_tile_t) );
358 0 : void * _txncache = FD_SCRATCH_ALLOC_APPEND( l, fd_txncache_align(), fd_txncache_footprint( tile->execrp.max_live_slots ) );
359 0 : void * _accdb = FD_SCRATCH_ALLOC_APPEND( l, fd_accdb_align(), fd_accdb_footprint( tile->execrp.max_live_slots ) );
360 0 : uchar * pc_scratch = FD_SCRATCH_ALLOC_APPEND( l, FD_PROGCACHE_SCRATCH_ALIGN, FD_PROGCACHE_SCRATCH_FOOTPRINT );
361 :
362 0 : void * _capture_ctx = NULL;
363 0 : if( FD_UNLIKELY( strlen( tile->execrp.solcap_capture ) ) ) {
364 0 : _capture_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_capture_ctx_align(), fd_capture_ctx_footprint() );
365 0 : }
366 :
367 0 : void * _dump_proto_ctx = NULL;
368 0 : void * _txn_dump_ctx = NULL;
369 0 : void * _dumping = NULL;
370 0 : if( FD_UNLIKELY( strlen( tile->execrp.dump_proto_dir ) ) ) {
371 0 : _dump_proto_ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_dump_proto_ctx_t), sizeof(fd_dump_proto_ctx_t) );
372 0 : _txn_dump_ctx = FD_SCRATCH_ALLOC_APPEND( l, fd_txn_dump_context_align(), fd_txn_dump_context_footprint() );
373 0 : if( FD_UNLIKELY( tile->execrp.dump_instr_to_pb || tile->execrp.dump_syscall_to_pb || tile->execrp.dump_txn_to_pb ) ) {
374 0 : _dumping = FD_SCRATCH_ALLOC_APPEND( l, FD_SPAD_ALIGN, FD_SPAD_FOOTPRINT( 1UL<<28UL ) );
375 0 : }
376 0 : }
377 :
378 0 : for( ulong i=0UL; i<FD_TXN_SIG_MAX; i++ ) {
379 0 : fd_sha512_t * sha = fd_sha512_join( fd_sha512_new( ctx->sha_mem+i ) );
380 0 : FD_TEST( sha );
381 0 : ctx->sha_lj[ i ] = sha;
382 0 : }
383 :
384 0 : ctx->txn_in.bundle.is_bundle = 0;
385 0 : ctx->tile_idx = tile->kind_id;
386 :
387 0 : ulong banks_obj_id = fd_pod_queryf_ulong( topo->props, ULONG_MAX, "banks" );
388 0 : FD_TEST( banks_obj_id!=ULONG_MAX );
389 :
390 0 : ctx->banks = fd_banks_join( fd_topo_obj_laddr( topo, banks_obj_id ) );
391 0 : FD_TEST( ctx->banks );
392 :
393 0 : FD_TEST( fd_progcache_join( ctx->progcache, fd_topo_obj_laddr( topo, tile->execrp.progcache_obj_id ), pc_scratch, FD_PROGCACHE_SCRATCH_FOOTPRINT ) );
394 :
395 0 : void * _txncache_shmem = fd_topo_obj_laddr( topo, tile->execrp.txncache_obj_id );
396 0 : fd_txncache_shmem_t * txncache_shmem = fd_txncache_shmem_join( _txncache_shmem );
397 0 : FD_TEST( txncache_shmem );
398 0 : ctx->txncache = fd_txncache_join( fd_txncache_new( _txncache, txncache_shmem ) );
399 0 : FD_TEST( ctx->txncache );
400 :
401 0 : void * _accdb_shmem = fd_topo_obj_laddr( topo, tile->execrp.accdb_obj_id );
402 0 : fd_accdb_shmem_t * accdb_shmem = fd_accdb_shmem_join( _accdb_shmem );
403 0 : FD_TEST( accdb_shmem );
404 0 : ctx->accdb = fd_accdb_join( fd_accdb_new( _accdb, accdb_shmem, FD_ACCDB_FD_RW, 0UL, NULL ) );
405 0 : FD_TEST( ctx->accdb );
406 :
407 : /* First find and setup the in-link from replay to exec. */
408 0 : ctx->replay_in->idx = fd_topo_find_tile_in_link( topo, tile, "replay_execrp", 0UL );
409 0 : FD_TEST( ctx->replay_in->idx!=ULONG_MAX );
410 0 : fd_topo_link_t const * replay_in_link = &topo->links[ tile->in_link_id[ ctx->replay_in->idx ] ];
411 0 : ctx->replay_in->mem = topo->workspaces[ topo->objs[ replay_in_link->dcache_obj_id ].wksp_id ].wksp;
412 0 : ctx->replay_in->chunk0 = fd_dcache_compact_chunk0( ctx->replay_in->mem, replay_in_link->dcache );
413 0 : ctx->replay_in->wmark = fd_dcache_compact_wmark( ctx->replay_in->mem, replay_in_link->dcache, replay_in_link->mtu );
414 0 : ctx->replay_in->chunk = ctx->replay_in->chunk0;
415 :
416 0 : ctx->execrp_replay_out->idx = fd_topo_find_tile_out_link( topo, tile, "execrp_replay", ctx->tile_idx );
417 0 : if( FD_LIKELY( ctx->execrp_replay_out->idx!=ULONG_MAX ) ) {
418 0 : fd_topo_link_t const * execrp_replay_link = &topo->links[ tile->out_link_id[ ctx->execrp_replay_out->idx ] ];
419 0 : ctx->execrp_replay_out->mem = topo->workspaces[ topo->objs[ execrp_replay_link->dcache_obj_id ].wksp_id ].wksp;
420 0 : ctx->execrp_replay_out->chunk0 = fd_dcache_compact_chunk0( ctx->execrp_replay_out->mem, execrp_replay_link->dcache );
421 0 : ctx->execrp_replay_out->wmark = fd_dcache_compact_wmark( ctx->execrp_replay_out->mem, execrp_replay_link->dcache, execrp_replay_link->mtu );
422 0 : ctx->execrp_replay_out->chunk = ctx->execrp_replay_out->chunk0;
423 0 : }
424 :
425 0 : ctx->capture_ctx = NULL;
426 0 : if( FD_UNLIKELY( strlen( tile->execrp.solcap_capture ) ) ) {
427 0 : ctx->capture_ctx = fd_capture_ctx_join( fd_capture_ctx_new( _capture_ctx ) );
428 0 : ctx->capture_ctx->solcap_start_slot = tile->execrp.capture_start_slot;
429 :
430 0 : ulong tile_idx = tile->kind_id;
431 0 : ulong idx = fd_topo_find_tile_out_link( topo, tile, "cap_execrp", tile_idx );
432 0 : FD_TEST( idx!=ULONG_MAX );
433 :
434 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ idx ] ];
435 0 : fd_capture_link_buf_t * cap_execrp_out = ctx->cap_execrp_out;
436 0 : cap_execrp_out->base.vt = &fd_capture_link_buf_vt;
437 0 : cap_execrp_out->idx = idx;
438 0 : cap_execrp_out->mem = topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ].wksp;
439 0 : cap_execrp_out->chunk0 = fd_dcache_compact_chunk0( cap_execrp_out->mem, link->dcache );
440 0 : cap_execrp_out->wmark = fd_dcache_compact_wmark( cap_execrp_out->mem, link->dcache, link->mtu );
441 0 : cap_execrp_out->chunk = cap_execrp_out->chunk0;
442 0 : cap_execrp_out->mcache = link->mcache;
443 0 : cap_execrp_out->depth = fd_mcache_depth( link->mcache );
444 0 : cap_execrp_out->seq = 0UL;
445 :
446 0 : ulong consumer_tile_idx = fd_topo_find_tile(topo, "solcap", 0UL);
447 0 : fd_topo_tile_t const * consumer_tile = &topo->tiles[ consumer_tile_idx ];
448 0 : cap_execrp_out->fseq = NULL;
449 0 : for( ulong j = 0UL; j < consumer_tile->in_cnt; j++ ) {
450 0 : if( FD_UNLIKELY( consumer_tile->in_link_id[ j ] == link->id ) ) {
451 0 : cap_execrp_out->fseq = fd_fseq_join( fd_topo_obj_laddr( topo, consumer_tile->in_link_fseq_obj_id[ j ] ) );
452 0 : FD_TEST( cap_execrp_out->fseq );
453 0 : break;
454 0 : }
455 0 : }
456 :
457 0 : ctx->capture_ctx->capture_solcap = 1;
458 0 : ctx->capture_ctx->capctx_type.buf = cap_execrp_out;
459 0 : ctx->capture_ctx->capture_link = &cap_execrp_out->base;
460 0 : }
461 :
462 0 : ctx->dump_proto_ctx = NULL;
463 0 : if( FD_UNLIKELY( strlen( tile->execrp.dump_proto_dir ) ) ) {
464 0 : ctx->dump_proto_ctx = _dump_proto_ctx;
465 :
466 : /* General dumping config */
467 0 : ctx->dump_proto_ctx->dump_proto_output_dir = tile->execrp.dump_proto_dir;
468 0 : ctx->dump_proto_ctx->dump_proto_start_slot = tile->execrp.capture_start_slot;
469 :
470 : /* Syscall dumping config */
471 0 : ctx->dump_proto_ctx->dump_syscall_to_pb = !!tile->execrp.dump_syscall_to_pb;
472 0 : ctx->dump_proto_ctx->dump_syscall_name_filter = tile->execrp.dump_syscall_name_filter;
473 :
474 : /* Instruction dumping config */
475 0 : ctx->dump_proto_ctx->dump_instr_to_pb = !!tile->execrp.dump_instr_to_pb;
476 0 : ctx->dump_proto_ctx->has_dump_instr_program_id_filter = !!strlen(tile->execrp.dump_instr_program_id_filter);
477 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx->has_dump_instr_program_id_filter &&
478 0 : !fd_base58_decode_32( tile->execrp.dump_instr_program_id_filter, ctx->dump_proto_ctx->dump_instr_program_id_filter ) ) ) {
479 0 : FD_LOG_ERR(( "failed to parse [capture.dump_instr_program_id_filter] %s", tile->execrp.dump_instr_program_id_filter ));
480 0 : }
481 :
482 : /* Transaction dumping config */
483 0 : ctx->dump_proto_ctx->dump_txn_to_pb = !!tile->execrp.dump_txn_to_pb;
484 0 : ctx->dump_proto_ctx->dump_txn_as_fixture = !!tile->execrp.dump_txn_as_fixture;
485 :
486 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx->dump_txn_as_fixture && !ctx->dump_proto_ctx->dump_txn_to_pb ) ) {
487 0 : FD_LOG_ERR(( "[capture.dump_txn_as_fixture] requires [capture.dump_txn_to_pb] to be enabled" ));
488 0 : }
489 0 : }
490 :
491 : /* Transaction dump context (for fixture dumping) */
492 0 : ctx->txn_dump_ctx = NULL;
493 0 : if( FD_UNLIKELY( ctx->dump_proto_ctx && ctx->dump_proto_ctx->dump_txn_to_pb ) ) {
494 0 : ctx->txn_dump_ctx = fd_txn_dump_context_join( fd_txn_dump_context_new( _txn_dump_ctx ) );
495 0 : }
496 :
497 0 : ctx->runtime->accdb = ctx->accdb;
498 0 : ctx->runtime->progcache = ctx->progcache;
499 0 : ctx->runtime->status_cache = ctx->txncache;
500 0 : memset( &ctx->runtime->log, 0, sizeof(ctx->runtime->log) );
501 0 : ctx->runtime->log.log_collector = &ctx->log_collector;
502 0 : ctx->runtime->log.dumping_mem = _dumping;
503 0 : ctx->runtime->log.tracing_mem = NULL;
504 0 : ctx->runtime->log.capture_ctx = ctx->capture_ctx;
505 0 : ctx->runtime->log.dump_proto_ctx = ctx->dump_proto_ctx;
506 0 : ctx->runtime->log.txn_dump_ctx = ctx->txn_dump_ctx;
507 0 : ctx->runtime->fuzz.enabled = 0;
508 0 : ctx->runtime->fuzz.reclaim_accounts = 0;
509 0 : ctx->runtime->accounts.executable_cnt = 0UL;
510 0 : ctx->runtime->accounts.account_cnt = 0UL;
511 :
512 0 : memset( &ctx->metrics, 0, sizeof(ctx->metrics) );
513 0 : memset( &ctx->runtime->metrics, 0, sizeof(ctx->runtime->metrics) );
514 :
515 0 : fd_wksp_oom_silent = 1;
516 :
517 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
518 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
519 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
520 :
521 0 : fd_startup_gate_init( ctx->startup_gate, topo, tile->in_cnt );
522 0 : }
523 :
524 : static ulong
525 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
526 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
527 : ulong out_cnt,
528 0 : struct sock_filter * out ) {
529 0 : populate_sock_filter_policy_fd_execrp_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(), (uint)FD_ACCDB_FD_RW );
530 0 : return sock_filter_policy_fd_execrp_tile_instr_cnt;
531 0 : }
532 :
533 : static ulong
534 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
535 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
536 : ulong out_fds_cnt,
537 0 : int * out_fds ) {
538 :
539 0 : if( FD_UNLIKELY( out_fds_cnt<3UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
540 :
541 0 : ulong out_cnt = 0UL;
542 0 : out_fds[ out_cnt++ ] = 2; /* stderr */
543 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) ) {
544 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
545 0 : }
546 0 : out_fds[ out_cnt++ ] = FD_ACCDB_FD_RW; /* accounts db */
547 :
548 0 : return out_cnt;
549 0 : }
550 :
551 0 : #define STEM_BURST (1UL)
552 :
553 : /* Right now, depth of the replay_exec link and depth of the execrp_replay
554 : links is 16K. At 1M TPS, that's ~16ms to fill. But we also want to
555 : be conservative here, so we use 1ms. */
556 0 : #define STEM_LAZY (1000000UL)
557 :
558 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_execrp_tile_t
559 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_execrp_tile_t)
560 :
561 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
562 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
563 0 : #define STEM_CALLBACK_RETURNABLE_FRAG returnable_frag
564 :
565 : #include "../../disco/stem/fd_stem.c"
566 :
567 : static ulong
568 0 : max_event_sz( fd_topo_tile_t const * tile ) {
569 : /* execrp emits accdb_partition_added, plus runtime_txn when diffs are on. */
570 0 : ulong sz = sizeof(fd_event_accdb_partition_added_t);
571 0 : if( tile->execrp.report_runtime_diffs && sizeof(fd_event_runtime_txn_t)>sz ) sz = sizeof(fd_event_runtime_txn_t);
572 0 : return sz;
573 0 : }
574 :
575 : fd_topo_run_tile_t fd_tile_execrp = {
576 : .name = "execrp",
577 : .max_event_sz = max_event_sz,
578 : .populate_allowed_seccomp = populate_allowed_seccomp,
579 : .populate_allowed_fds = populate_allowed_fds,
580 : .scratch_align = scratch_align,
581 : .scratch_footprint = scratch_footprint,
582 : .unprivileged_init = unprivileged_init,
583 : .run = stem_run,
584 : };
|