Line data Source code
1 : /* The txsend tile relays new transactions to the current leader.
2 :
3 : To recap, every ~1.6 seconds, a new validator is elected by the
4 : protocol to receive all transactions, and pack them into blocks.
5 : To submit a transaction to Solana, one has to track the "leader
6 : schedule" and get the timing right to submit a transaction early
7 : enough.
8 :
9 : The txsend tile supports the TPU-UDP (connectionless) and TPU-QUIC
10 : transports. This tile has the following jobs:
11 : - decide who to connect to (continually monitor gossip, leader schedule)
12 : - manage a pool of QUIC connections
13 : - actually send transactions
14 :
15 : An important quirk is that txsend tolerates temporary gossip outages
16 : by indefinitely caching old endpoint info (until the gossip feed is
17 : revived). */
18 :
19 : #include "fd_txsend_tile.h"
20 :
21 : #include "../../disco/topo/fd_topo.h"
22 : #include "../../disco/fd_txn_m.h"
23 : #include "../../disco/metrics/fd_metrics.h"
24 : #include "../../disco/events/generated/fd_event_gen.h"
25 : #include "../../disco/events/fd_event_report.h"
26 : #include "../../choreo/tower/fd_tower_serdes.h"
27 : #include "../../flamenco/runtime/fd_system_ids.h"
28 : #include "../../disco/keyguard/fd_keyguard.h"
29 : #include "../../disco/keyguard/fd_keyload.h"
30 : #include "../fd_startup.h"
31 : #include "../tower/fd_tower_tile.h"
32 : #include "../../util/net/fd_net_headers.h"
33 : #include "../../waltz/quic/fd_quic.h"
34 :
35 : #include <time.h>
36 : #include "generated/fd_txsend_tile_seccomp.h"
37 :
38 0 : #define IN_KIND_SIGN (0UL)
39 0 : #define IN_KIND_GOSSIP (1UL)
40 0 : #define IN_KIND_EPOCH (2UL)
41 0 : #define IN_KIND_TOWER (3UL)
42 0 : #define IN_KIND_NET (4UL)
43 :
44 : fd_quic_limits_t quic_limits = {
45 : .conn_cnt = 128UL,
46 : .handshake_cnt = 128UL,
47 : .conn_id_cnt = FD_QUIC_MIN_CONN_ID_CNT,
48 : .inflight_frame_cnt = 16UL * 128UL,
49 : .min_inflight_frame_cnt_conn = 4UL,
50 : .stream_id_cnt = 64UL,
51 : .tx_buf_sz = FD_TXN_MTU,
52 : .stream_pool_cnt = 128UL,
53 : };
54 :
55 : #define MAP_NAME peer_map
56 0 : #define MAP_KEY pubkey
57 : #define MAP_ELE_T peer_entry_t
58 : #define MAP_KEY_T fd_pubkey_t
59 0 : #define MAP_NEXT map.next
60 : #define MAP_KEY_EQ(k0,k1) fd_pubkey_eq( k0, k1 )
61 : #define MAP_KEY_HASH(key,seed) fd_progcache_rec_key_hash1( (key)->uc, (seed) )
62 : #define MAP_IMPL_STYLE 2
63 : #include "../../util/tmpl/fd_map_chain.c"
64 :
65 : FD_FN_CONST static inline ulong
66 0 : scratch_align( void ) {
67 0 : return fd_ulong_max( 128UL, fd_quic_align() );
68 0 : }
69 :
70 : FD_FN_PURE static inline ulong
71 0 : scratch_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
72 0 : ulong l = FD_LAYOUT_INIT;
73 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_txsend_tile_t), sizeof(fd_txsend_tile_t) );
74 0 : l = FD_LAYOUT_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) );
75 0 : l = FD_LAYOUT_APPEND( l, peer_map_align(), peer_map_footprint( 2UL*FD_CONTACT_INFO_TABLE_SIZE ) );
76 0 : return FD_LAYOUT_FINI( l, scratch_align() );
77 0 : }
78 :
79 : static void
80 0 : during_housekeeping( fd_txsend_tile_t * ctx ) {
81 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_UNHALT_PENDING ) ) {
82 0 : FD_LOG_DEBUG(( "keyswitch: unhalting" ));
83 0 : ctx->halt_net_frags = 0;
84 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
85 0 : }
86 :
87 0 : if( FD_UNLIKELY( fd_keyswitch_state_query( ctx->keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
88 0 : FD_LOG_DEBUG(( "keyswitch: switching identity" ));
89 0 : ulong seq_must_complete = ctx->keyswitch->param;
90 0 : if( FD_UNLIKELY( fd_seq_lt( ctx->tower_in_expect_seq, seq_must_complete ) ) ) {
91 : /* See fd_keyswitch.h, we need to flush any in-flight shreds from
92 : the leader pipeline before switching key. */
93 0 : FD_LOG_WARNING(( "Flushing in-flight unpublished votes from tower, must reach seq %lu, currently at %lu ...", seq_must_complete, ctx->tower_in_expect_seq ));
94 0 : return;
95 0 : }
96 :
97 : /* Halt net frags to avoid potential quic callback in after_frag */
98 0 : ctx->halt_net_frags = 1;
99 :
100 0 : fd_quic_set_identity_public_key( ctx->quic, ctx->keyswitch->bytes );
101 :
102 0 : memcpy( ctx->identity_key, ctx->keyswitch->bytes, 32UL );
103 0 : fd_keyswitch_state( ctx->keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
104 0 : }
105 :
106 0 : if( FD_UNLIKELY( ctx->av_keyswitch && fd_keyswitch_state_query( ctx->av_keyswitch )==FD_KEYSWITCH_STATE_SWITCH_PENDING ) ) {
107 0 : ulong seq_must_complete = fd_keyswitch_param_query( ctx->av_keyswitch );
108 0 : if( FD_UNLIKELY( fd_seq_lt( ctx->tower_in_expect_seq, seq_must_complete ) ) ) {
109 0 : FD_LOG_WARNING(( "Flushing in-flight votes from tower, must reach seq %lu, currently at %lu ...", seq_must_complete, ctx->tower_in_expect_seq ));
110 0 : return;
111 0 : }
112 0 : fd_keyswitch_state( ctx->av_keyswitch, FD_KEYSWITCH_STATE_COMPLETED );
113 0 : }
114 0 : }
115 :
116 : static void
117 0 : metrics_write( fd_txsend_tile_t * ctx ) {
118 0 : FD_MCNT_SET( TXSEND, PKT_RX_BYTES, ctx->quic->metrics.net_rx_byte_cnt );
119 0 : FD_MCNT_ENUM_COPY( TXSEND, FRAME_RX, ctx->quic->metrics.frame_rx_cnt );
120 0 : FD_MCNT_SET( TXSEND, PKT_RX, ctx->quic->metrics.net_rx_pkt_cnt );
121 0 : FD_MCNT_SET( TXSEND, STREAM_RX_BYTES, ctx->quic->metrics.stream_rx_byte_cnt );
122 0 : FD_MCNT_SET( TXSEND, STREAM_RX, ctx->quic->metrics.stream_rx_event_cnt );
123 :
124 0 : FD_MCNT_SET( TXSEND, PKT_TX, ctx->quic->metrics.net_tx_pkt_cnt );
125 0 : FD_MCNT_SET( TXSEND, PKT_TX_BYTES, ctx->quic->metrics.net_tx_byte_cnt );
126 0 : FD_MCNT_SET( TXSEND, PKT_TX_RETRY, ctx->quic->metrics.retry_tx_cnt );
127 0 : FD_MCNT_ENUM_COPY( TXSEND, ACK_TX, ctx->quic->metrics.ack_tx );
128 :
129 0 : FD_MGAUGE_ENUM_COPY( TXSEND, CONN_STATE, ctx->quic->metrics.conn_state_cnt );
130 0 : FD_MGAUGE_SET( TXSEND, CONN_IN_USE, ctx->quic->metrics.conn_alloc_cnt );
131 0 : FD_MCNT_SET( TXSEND, CONN_CREATED, ctx->quic->metrics.conn_created_cnt );
132 0 : FD_MCNT_SET( TXSEND, CONN_CLOSED, ctx->quic->metrics.conn_closed_cnt );
133 0 : FD_MCNT_SET( TXSEND, CONN_ABORTED, ctx->quic->metrics.conn_aborted_cnt );
134 0 : FD_MCNT_SET( TXSEND, CONN_TIMED_OUT, ctx->quic->metrics.conn_timeout_cnt );
135 0 : FD_MCNT_SET( TXSEND, CONN_RETRIED, ctx->quic->metrics.conn_retry_cnt );
136 0 : FD_MCNT_SET( TXSEND, CONN_ERROR_NO_SLOTS, ctx->quic->metrics.conn_err_no_slots_cnt );
137 0 : FD_MCNT_SET( TXSEND, CONN_ERROR_RETRY_FAILED, ctx->quic->metrics.conn_err_retry_fail_cnt );
138 :
139 0 : FD_MCNT_ENUM_COPY( TXSEND, PKT_CRYPTO_FAILED, ctx->quic->metrics.pkt_decrypt_fail_cnt );
140 0 : FD_MCNT_ENUM_COPY( TXSEND, PKT_NO_KEY, ctx->quic->metrics.pkt_no_key_cnt );
141 0 : FD_MCNT_ENUM_COPY( TXSEND, PKT_NO_CONN, ctx->quic->metrics.pkt_no_conn_cnt );
142 0 : FD_MCNT_SET( TXSEND, PKT_SRC_INVALID, ctx->quic->metrics.pkt_wrong_src_cnt );
143 0 : FD_MCNT_ENUM_COPY( TXSEND, FRAME_META_ACQUIRED, ctx->quic->metrics.frame_tx_alloc_cnt );
144 0 : FD_MCNT_SET( TXSEND, PKT_NET_HEADER_INVALID, ctx->quic->metrics.pkt_net_hdr_err_cnt );
145 0 : FD_MCNT_SET( TXSEND, PKT_HEADER_INVALID, ctx->quic->metrics.pkt_quic_hdr_err_cnt );
146 0 : FD_MCNT_SET( TXSEND, PKT_UNDERSIZE, ctx->quic->metrics.pkt_undersz_cnt );
147 0 : FD_MCNT_SET( TXSEND, PKT_OVERSIZE, ctx->quic->metrics.pkt_oversz_cnt );
148 0 : FD_MCNT_SET( TXSEND, PKT_RX_VERSION_NEGOTIATION, ctx->quic->metrics.pkt_verneg_cnt );
149 0 : FD_MCNT_ENUM_COPY( TXSEND, PKT_TX_RETRANSMITTED, ctx->quic->metrics.pkt_retransmissions_cnt );
150 :
151 0 : FD_MCNT_SET( TXSEND, HANDSHAKE_CREATED, ctx->quic->metrics.hs_created_cnt );
152 0 : FD_MCNT_SET( TXSEND, HANDSHAKE_ERROR_ALLOC_FAIL, ctx->quic->metrics.hs_err_alloc_fail_cnt );
153 0 : FD_MCNT_SET( TXSEND, HANDSHAKE_EVICTED, ctx->quic->metrics.hs_evicted_cnt );
154 :
155 0 : FD_MCNT_SET( TXSEND, FRAME_PARSE_FAILED, ctx->quic->metrics.frame_rx_err_cnt );
156 :
157 0 : FD_MHIST_COPY( TXSEND, SERVICE_DURATION_SECONDS, ctx->quic->metrics.service_duration );
158 0 : FD_MHIST_COPY( TXSEND, RX_DURATION_SECONDS, ctx->quic->metrics.receive_duration );
159 0 : }
160 :
161 : static void
162 : quic_tls_cv_sign( void * signer_ctx,
163 : uchar signature[ static 64 ],
164 0 : uchar const payload[ static 130 ] ) {
165 0 : fd_txsend_tile_t * ctx = signer_ctx;
166 :
167 0 : fd_keyguard_client_sign( ctx->keyguard_client, signature, payload, 130UL, FD_KEYGUARD_SIGN_TYPE_ED25519 );
168 0 : }
169 :
170 : static void
171 : send_to_net( fd_txsend_tile_t * ctx,
172 : fd_ip4_hdr_t const * ip4_hdr,
173 : fd_udp_hdr_t const * udp_hdr,
174 : uchar const * payload,
175 : ulong payload_sz,
176 0 : long now ) {
177 0 : uint const ip_dst = FD_LOAD( uint, ip4_hdr->daddr_c );
178 0 : ulong const ip_sz = FD_IP4_GET_LEN( *ip4_hdr );
179 :
180 0 : fd_txsend_out_t * net_out_link = ctx->net_out;
181 0 : uchar * packet_l2 = fd_chunk_to_laddr( net_out_link->mem, net_out_link->chunk );
182 0 : uchar * packet_l3 = packet_l2 + sizeof(fd_eth_hdr_t);
183 0 : uchar * packet_l4 = packet_l3 + ip_sz;
184 0 : uchar * packet_l5 = packet_l4 + sizeof(fd_udp_hdr_t);
185 :
186 0 : fd_memcpy( packet_l2, ctx->packet_hdr->eth, sizeof(fd_eth_hdr_t) );
187 0 : fd_memcpy( packet_l3, ip4_hdr, ip_sz );
188 0 : fd_memcpy( packet_l4, udp_hdr, sizeof(fd_udp_hdr_t) );
189 0 : fd_memcpy( packet_l5, payload, payload_sz );
190 :
191 0 : ulong sig = fd_disco_netmux_sig( ip_dst, 0U, ip_dst, DST_PROTO_OUTGOING, FD_NETMUX_SIG_MIN_HDR_SZ );
192 0 : ulong sz_l2 = sizeof(fd_eth_hdr_t) + ip_sz + sizeof(fd_udp_hdr_t) + payload_sz;
193 :
194 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( now );
195 0 : fd_stem_publish( ctx->stem, net_out_link->idx, sig, net_out_link->chunk, sz_l2, 0UL, 0, tspub );
196 0 : net_out_link->chunk = fd_dcache_compact_next( net_out_link->chunk, sz_l2, net_out_link->chunk0, net_out_link->wmark );
197 0 : }
198 :
199 : static int
200 : quic_tx_aio_send( void * _ctx,
201 : fd_aio_pkt_info_t const * batch,
202 : ulong batch_cnt,
203 : ulong * opt_batch_idx,
204 0 : int flush FD_PARAM_UNUSED ) {
205 0 : fd_txsend_tile_t * ctx = _ctx;
206 :
207 0 : long now = fd_log_wallclock();
208 :
209 0 : for( ulong i=0; i<batch_cnt; i++ ) {
210 0 : if( FD_UNLIKELY( batch[ i ].buf_sz<FD_NETMUX_SIG_MIN_HDR_SZ ) ) continue;
211 0 : uchar * buf = batch[ i ].buf;
212 0 : fd_ip4_hdr_t * ip4_hdr = fd_type_pun( buf );
213 0 : ulong const ip4_len = FD_IP4_GET_LEN( *ip4_hdr );
214 0 : fd_udp_hdr_t * udp_hdr = fd_type_pun( buf + ip4_len );
215 0 : uchar * payload = buf + ip4_len + sizeof(fd_udp_hdr_t);
216 0 : FD_TEST( batch[ i ].buf_sz >= ip4_len + sizeof(fd_udp_hdr_t) );
217 0 : ulong payload_sz = batch[ i ].buf_sz - ip4_len - sizeof(fd_udp_hdr_t);
218 0 : send_to_net( ctx, ip4_hdr, udp_hdr, payload, payload_sz, now );
219 0 : }
220 :
221 0 : if( FD_LIKELY( opt_batch_idx ) ) {
222 0 : *opt_batch_idx = batch_cnt;
223 0 : }
224 :
225 0 : return FD_AIO_SUCCESS;
226 0 : }
227 :
228 : /* quic_conn_deregister removes references to a quic_conn object from
229 : txsend state. */
230 :
231 : static void
232 : quic_conn_deregister( fd_txsend_tile_t * tile,
233 0 : fd_quic_conn_t * conn ) {
234 0 : for( ulong i=0UL; i<tile->conns_len; i++ ) {
235 0 : if( FD_LIKELY( tile->conns[ i ].conn!=conn ) ) continue;
236 0 : peer_entry_t * peer = peer_map_ele_query( tile->peer_map, &tile->conns[ i ].pubkey, NULL, tile->peers );
237 0 : if( FD_LIKELY( peer ) ) {
238 0 : for( ulong j=0UL; j<2UL; j++ ) {
239 0 : if( peer->quic_conns[ j ].quic_conn==conn ) {
240 0 : peer->quic_conns[ j ].quic_conn = NULL;
241 0 : }
242 0 : }
243 0 : }
244 0 : if( FD_UNLIKELY( i!=tile->conns_len-1UL ) ) tile->conns[ i ] = tile->conns[ tile->conns_len-1UL ];
245 0 : tile->conns_len--;
246 0 : return;
247 0 : }
248 0 : }
249 :
250 : /* quic_conn_final is invoked by fd_quic just before a conn object is
251 : deallocated. Here, we must remove all references to this conn. */
252 :
253 : static void
254 : quic_conn_final( fd_quic_conn_t * conn,
255 0 : void * ctx ) {
256 0 : fd_txsend_tile_t * tile = ctx;
257 0 : quic_conn_deregister( tile, conn );
258 0 : }
259 :
260 : /* quic_conn_close instructs fd_quic to deallocate the given conn and
261 : say goodbye (CONNECTION_CLOSE) to the peer. */
262 :
263 : static void
264 : quic_conn_close( fd_txsend_tile_t * tile,
265 : fd_quic_conn_t * conn,
266 0 : uint reason ) {
267 0 : if( FD_UNLIKELY( !conn ) ) return;
268 : /* Defer a conn close operation */
269 0 : fd_quic_conn_close( conn, reason );
270 0 : quic_conn_deregister( tile, conn );
271 : /* Send out a packet and invoke quic_conn_final */
272 0 : fd_quic_service( tile->quic, fd_log_wallclock() );
273 0 : }
274 :
275 : /* This QUIC servicing is very precarious. Recall a few facts,
276 :
277 : 1) QUIC needs to be serviced periodically to make progress
278 : 2) QUIC servicing may produce outgoing packets that need to be sent
279 : to the network
280 : 3) Elsewhere, the the tile publishes frags to the verify tile to
281 : send our own votes into our leader pipeline
282 :
283 : You could service QUIC in before_credit, as the QUIC tile does, but
284 : this has a problem. If you publish frags in before_credit, you might
285 : overrun the downstream consumer. For net tile, this is OK because it
286 : expects that (as does verify). But the credit counting mechanism
287 : doesn't expect this behavior and will underflow. (That's also not
288 : ideal, in case some plugin wanted to listen reliably on quic->verify
289 : they could not, if it got underflowed). Here though, we want to
290 : avoid dropping outgoing votes to verify, since they might be needed
291 : for liveness of a small cluster.
292 :
293 : We thus take the trade of servicing QUIC in after_credit, which means
294 : it could theoretically get backpressured by verify, however this
295 : isn't realistic in practice, as verify polls round robin and there's
296 : only one vote per slot. */
297 :
298 : static inline void
299 : after_credit( fd_txsend_tile_t * ctx,
300 : fd_stem_context_t * stem,
301 : int * opt_poll_in,
302 0 : int * charge_busy ) {
303 0 : ctx->stem = stem;
304 :
305 0 : if( FD_UNLIKELY( !fd_startup_gate_idle( ctx->startup_gate ) ) ) return;
306 :
307 0 : *charge_busy = fd_quic_service( ctx->quic, fd_log_wallclock() );
308 0 : *opt_poll_in = !*charge_busy; /* refetch credits to prevent above documented situation */
309 :
310 0 : if( FD_UNLIKELY( ctx->leader_schedules<2UL ) ) return;
311 0 : if( FD_UNLIKELY( ctx->voted_slot==ULONG_MAX ) ) return;
312 :
313 0 : fd_pubkey_t const * leaders[ 7UL ];
314 :
315 0 : for( ulong i=0UL; i<7UL; i++ ) {
316 : /* It's possible for leaders[i] to be NULL if target slot is two
317 : epochs ahead of the replay root. This is not possible on mainnet
318 : but can occur on local clusters during warmup epochs. */
319 0 : ulong target_slot = ctx->voted_slot+1UL + i*FD_EPOCH_SLOTS_PER_ROTATION;
320 0 : leaders[ i ] = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, target_slot );
321 0 : }
322 :
323 : /* Disconnect any QUIC connection to a leader that does not have a
324 : rotation coming up in the next 7 slots. */
325 0 : ulong conn_cnt = ctx->conns_len;
326 0 : for( ulong i=0UL; i<conn_cnt; ) {
327 0 : int keep_conn = 0;
328 0 : for( ulong j=0UL; j<7UL; j++ ) {
329 0 : if( leaders[j] && fd_pubkey_eq( &ctx->conns[ i ].pubkey, leaders[ j ] ) ) {
330 0 : keep_conn = 1;
331 0 : break;
332 0 : }
333 0 : }
334 :
335 0 : if( FD_UNLIKELY( !keep_conn ) ) quic_conn_close( ctx, ctx->conns[ i ].conn, 0 );
336 0 : if( ctx->conns_len==conn_cnt ) i++;
337 0 : conn_cnt = ctx->conns_len;
338 0 : }
339 :
340 : /* Connect to any leader that does not have a connection yet. */
341 0 : for( ulong i=0UL; i<7UL; i++ ) {
342 0 : fd_pubkey_t const * leader = leaders[ i ];
343 0 : if( FD_UNLIKELY( !leader ) ) continue;
344 0 : peer_entry_t * peer = peer_map_ele_query( ctx->peer_map, leader, NULL, ctx->peers );
345 0 : if( FD_UNLIKELY( !peer ) ) continue; /* no contact info */
346 :
347 0 : for( ulong j=0UL; j<2UL; j++ ) {
348 0 : if( FD_UNLIKELY( ctx->conns_len==128UL ) ) break; /* connection limit reached */
349 0 : txsend_conn_t * conn = &peer->quic_conns[ j ];
350 0 : if( FD_LIKELY( conn->quic_conn ) ) continue; /* already connected */
351 0 : if( FD_UNLIKELY( !conn->quic_ip_addr || !conn->quic_port ) ) continue;
352 :
353 : /* Don't try to reconnect more than once every two seconds ...
354 : Basically Agave limits us to 8 connections per minute, so if we
355 : keep trying to reconnect rapidly it's much less effective than
356 : waiting a little bit to ensure we stay under the threshold.
357 :
358 : We should probably make this a bit more sophisticated, with a
359 : simple model that considers past connection attempts, and
360 : future leader slots (e.g. we might still want to burn an
361 : attempt if a leader slot is imminent, even if we recently tried
362 : to connect). For now the dumb logic seems to work well enough. */
363 0 : long now = fd_log_wallclock();
364 0 : if( FD_UNLIKELY( conn->quic_last_connected+2e9L>now ) ) continue;
365 :
366 0 : fd_quic_conn_t * quic_conn =
367 0 : fd_quic_connect( ctx->quic,
368 0 : conn->quic_ip_addr,
369 0 : conn->quic_port,
370 0 : ctx->src_ip_addr,
371 0 : ctx->src_port,
372 0 : now );
373 0 : if( FD_UNLIKELY( !quic_conn ) ) {
374 : /* Should never happen, but handle it gracefully */
375 0 : return;
376 0 : }
377 0 : ctx->conns[ ctx->conns_len ].conn = quic_conn;
378 0 : ctx->conns[ ctx->conns_len ].pubkey = *leader;
379 0 : conn->quic_conn = quic_conn;
380 0 : conn->quic_last_connected = now;
381 0 : ctx->conns_len++;
382 0 : }
383 0 : }
384 0 : }
385 :
386 : static void
387 : send_vote_to_leader( fd_txsend_tile_t * ctx,
388 : fd_pubkey_t const * leader_pubkey,
389 : uchar const * vote_payload,
390 0 : ulong vote_payload_sz ) {
391 0 : peer_entry_t const * peer = peer_map_ele_query_const( ctx->peer_map, leader_pubkey, NULL, ctx->peers );
392 0 : if( FD_UNLIKELY( !peer ) ) return; /* no known contact info */
393 :
394 0 : for( ulong i=0UL; i<2UL; i++ ) {
395 0 : if( FD_UNLIKELY( !peer->udp_ip_addrs[ i ] | !peer->udp_ports[ i ] ) ) continue;
396 :
397 0 : fd_ip4_hdr_t * ip4_hdr = ctx->packet_hdr->ip4;
398 0 : fd_udp_hdr_t * udp_hdr = ctx->packet_hdr->udp;
399 :
400 0 : ip4_hdr->daddr = peer->udp_ip_addrs[ i ];
401 0 : ip4_hdr->net_tot_len = fd_ushort_bswap( (ushort)(vote_payload_sz+sizeof(fd_ip4_hdr_t)+sizeof(fd_udp_hdr_t)) );
402 0 : ip4_hdr->net_id = fd_ushort_bswap( ctx->net_id++ );
403 0 : ip4_hdr->check = 0;
404 0 : ip4_hdr->check = fd_ip4_hdr_check_fast( ip4_hdr );
405 :
406 0 : udp_hdr->net_dport = fd_ushort_bswap( peer->udp_ports[ i ] );
407 0 : udp_hdr->net_len = fd_ushort_bswap( (ushort)( vote_payload_sz+sizeof(fd_udp_hdr_t) ) );
408 0 : send_to_net( ctx, ip4_hdr, udp_hdr, vote_payload, vote_payload_sz, fd_log_wallclock() );
409 0 : }
410 :
411 0 : for( ulong i=0UL; i<2UL; i++ ) {
412 0 : fd_quic_conn_t * conn = peer->quic_conns[ i ].quic_conn;
413 0 : if( FD_UNLIKELY( !conn ) ) continue;
414 :
415 0 : fd_quic_stream_t * stream = fd_quic_conn_new_stream( conn );
416 0 : if( FD_UNLIKELY( !stream ) ) continue;
417 :
418 0 : fd_quic_stream_send( stream, vote_payload, vote_payload_sz, 1 );
419 0 : }
420 0 : }
421 :
422 : /* gossip -> txsend peer synchronization
423 :
424 : The gossip update stream can be replayed to perfectly replicate the
425 : ContactInfo table. The stream is laid out so there are no duplicate
426 : pubkeys.
427 :
428 : However, the txsend tile wants to retain pubkeys past deletion.
429 : Gossip evicts ContactInfos without updates quickly, but txsend should
430 : continue sending to staked leaders even if there is a temporary
431 : gossip outage.
432 :
433 : Therefore, the txsend tile only tombstones entries when the gossip
434 : stream instructs to remove them, instead of deleting. The tombstone
435 : handling is then resolved downstream during ContactInfo updates. */
436 :
437 : static inline void
438 : handle_contact_info_remove( fd_txsend_tile_t * ctx,
439 0 : fd_gossip_update_message_t const * msg ) {
440 0 : FD_TEST( msg->contact_info_remove->idx < FD_CONTACT_INFO_TABLE_SIZE );
441 0 : peer_entry_t * entry = &ctx->peers[ msg->contact_info_remove->idx ];
442 0 : entry->tombstoned = 1;
443 0 : }
444 :
445 : static void
446 : handle_contact_info_update( fd_txsend_tile_t * ctx,
447 0 : fd_gossip_update_message_t const * msg ) {
448 0 : FD_TEST( msg->contact_info->idx < FD_CONTACT_INFO_TABLE_SIZE );
449 :
450 : /* Key updated by the gossip event */
451 0 : fd_pubkey_t key = FD_LOAD( fd_pubkey_t, msg->origin );
452 :
453 : /* Storage entry updated by the gossip event */
454 0 : peer_entry_t * entry = &ctx->peers[ msg->contact_info->idx ];
455 :
456 : /* At this point, entry contains an arbitrary old tombstoned entry or
457 : a previous version of this key. */
458 :
459 0 : if( FD_UNLIKELY( !fd_pubkey_eq( &entry->pubkey, &key ) ) ) {
460 : /* Overwriting an unrelated (tombstoned) entry, free it */
461 0 : quic_conn_close( ctx, entry->quic_conns[ 0 ].quic_conn, 0 );
462 0 : quic_conn_close( ctx, entry->quic_conns[ 1 ].quic_conn, 0 );
463 0 : peer_map_ele_remove( ctx->peer_map, &entry->pubkey, NULL, ctx->peers );
464 0 : memset( entry, 0, sizeof(peer_entry_t) );
465 0 : }
466 :
467 : /* At this point, entry contains a stale version of the same key or is
468 : empty. */
469 :
470 0 : peer_entry_t * stale = peer_map_ele_query( ctx->peer_map, &key, NULL, ctx->peers );
471 0 : if( FD_UNLIKELY( stale && stale!=entry ) ) {
472 : /* The key exists at another entry location, drop that and migrate
473 : it to this slot. */
474 0 : for( ulong i=0UL; i<2UL; i++ ) {
475 0 : entry->quic_conns [ i ] = stale->quic_conns [ i ];
476 0 : entry->udp_ip_addrs[ i ] = stale->udp_ip_addrs[ i ];
477 0 : entry->udp_ports [ i ] = stale->udp_ports [ i ];
478 0 : }
479 0 : peer_map_ele_remove( ctx->peer_map, &stale->pubkey, NULL, ctx->peers );
480 0 : memset( stale, 0, sizeof(peer_entry_t) );
481 0 : fd_memcpy( entry->pubkey.uc, msg->origin, 32UL );
482 0 : FD_TEST( peer_map_ele_insert( ctx->peer_map, entry, ctx->peers ) );
483 0 : } else if( !stale ) {
484 0 : fd_memcpy( entry->pubkey.uc, msg->origin, 32UL );
485 0 : FD_TEST( peer_map_ele_insert( ctx->peer_map, entry, ctx->peers ) );
486 0 : }
487 :
488 0 : entry->tombstoned = 0;
489 :
490 0 : static ulong const quic_socket_idx[ 2UL ] = {
491 0 : FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_VOTE_QUIC,
492 0 : FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_QUIC,
493 0 : };
494 :
495 0 : static ulong const udp_socket_idx[ 2UL ] = {
496 0 : FD_GOSSIP_CONTACT_INFO_SOCKET_TPU_VOTE,
497 0 : FD_GOSSIP_CONTACT_INFO_SOCKET_TPU,
498 0 : };
499 :
500 : /* At this point, entry is not in a map and *entry might still contain
501 : stale endpoint info (from a previous update). Only overwrite it if
502 : update actually contains endpoint info. */
503 :
504 0 : for( ulong i=0UL; i<2UL; i++ ) {
505 0 : if( FD_LIKELY( !msg->contact_info->value->sockets[ quic_socket_idx[ i ] ].is_ipv6 && msg->contact_info->value->sockets[ quic_socket_idx[ i ] ].ip4 ) ) {
506 0 : entry->quic_conns[ i ].quic_ip_addr = msg->contact_info->value->sockets[ quic_socket_idx[ i ] ].ip4;
507 0 : }
508 0 : ushort port = fd_ushort_bswap( msg->contact_info->value->sockets[ quic_socket_idx[ i ] ].port );
509 0 : if( FD_LIKELY( port ) ) {
510 0 : entry->quic_conns[ i ].quic_port = port;
511 0 : }
512 0 : }
513 :
514 0 : for( ulong i=0UL; i<2UL; i++ ) {
515 0 : if( FD_LIKELY( !msg->contact_info->value->sockets[ udp_socket_idx[ i ] ].is_ipv6 && msg->contact_info->value->sockets[ udp_socket_idx[ i ] ].ip4 ) ) {
516 0 : entry->udp_ip_addrs[ i ] = msg->contact_info->value->sockets[ udp_socket_idx[ i ] ].ip4;
517 0 : }
518 0 : if( FD_LIKELY( fd_ushort_bswap( msg->contact_info->value->sockets[ udp_socket_idx[ i ] ].port ) ) ) {
519 0 : entry->udp_ports [ i ] = fd_ushort_bswap( msg->contact_info->value->sockets[ udp_socket_idx[ i ] ].port );
520 0 : }
521 0 : }
522 0 : }
523 :
524 : static void
525 : report_signed_vote( uchar const * payload,
526 : fd_txn_t const * txn,
527 : uchar const * signatures,
528 0 : ulong vote_txn_sz ) {
529 0 : if( FD_LIKELY( !fd_event_tl ) ) return;
530 :
531 0 : if( FD_UNLIKELY( txn->instr_cnt!=1UL ) ) return;
532 0 : if( FD_UNLIKELY( txn->acct_addr_cnt<3UL || txn->acct_addr_cnt>4UL ) ) return;
533 0 : fd_txn_instr_t const * instr = &txn->instr[ 0 ];
534 0 : fd_acct_addr_t const * addrs = fd_txn_get_acct_addrs( txn, payload );
535 0 : if( FD_UNLIKELY( 0!=memcmp( addrs[ instr->program_id ].b, fd_solana_vote_program_id.uc, sizeof(fd_pubkey_t) ) ) ) return;
536 0 : if( FD_UNLIKELY( instr->acct_cnt!=2UL ) ) return;
537 0 : uchar const * instr_addrs = fd_txn_get_instr_accts( instr, payload );
538 0 : uchar const * instr_data = payload + instr->data_off;
539 0 : ulong instr_data_sz = instr->data_sz;
540 0 : if( FD_UNLIKELY( instr_data_sz<sizeof(uint) ) ) return;
541 0 : if( FD_UNLIKELY( FD_LOAD( uint, instr_data )!=FD_VOTE_IX_KIND_TOWER_SYNC ) ) return;
542 0 : instr_data += 4; instr_data_sz -= 4;
543 :
544 0 : fd_compact_tower_sync_serde_t sync[1];
545 0 : if( FD_UNLIKELY( 0!=fd_compact_tower_sync_de( sync, instr_data, instr_data_sz ) ) ) return;
546 0 : if( FD_UNLIKELY( sync->lockouts_cnt==0 || sync->lockouts_cnt>31 ) ) return;
547 :
548 0 : uchar const * rbh = fd_txn_get_recent_blockhash( txn, payload );
549 :
550 0 : fd_event_signed_vote_t ev = {0};
551 0 : FD_TEST( vote_txn_sz<=sizeof(ev.signed_txn) );
552 0 : fd_memcpy( ev.signed_txn, payload, vote_txn_sz );
553 0 : ev.signed_txn_len = vote_txn_sz;
554 0 : fd_memcpy( ev.vote_account, addrs[ instr_addrs[ 0 ] ].b, sizeof(fd_pubkey_t) );
555 0 : fd_memcpy( ev.vote_authority, addrs[ instr_addrs[ 1 ] ].b, sizeof(fd_pubkey_t) );
556 0 : fd_memcpy( ev.fee_payer, addrs[ 0 ].b, sizeof(fd_pubkey_t) );
557 0 : fd_memcpy( ev.signature, signatures, sizeof(fd_ed25519_sig_t) );
558 0 : fd_memcpy( ev.vote_bank_hash, sync->hash.uc, sizeof(fd_hash_t) );
559 0 : fd_memcpy( ev.vote_block_id, sync->block_id.uc, sizeof(fd_hash_t) );
560 0 : fd_memcpy( ev.txn_blockhash, rbh, sizeof(fd_hash_t) );
561 :
562 0 : ulong root_slot = fd_ulong_if( sync->root==ULONG_MAX, 0UL, sync->root );
563 0 : ulong slot = root_slot;
564 0 : for( ulong i=0UL; i<sync->lockouts_cnt; i++ ) {
565 0 : slot += sync->lockouts[ i ].offset;
566 0 : ev.tower[ i ].slot = slot;
567 0 : ev.tower[ i ].confirmation_count = (uchar)sync->lockouts[ i ].confirmation_count;
568 0 : }
569 0 : ev.tower_cnt = sync->lockouts_cnt;
570 0 : ev.vote_slot = slot; /* top of tower */
571 :
572 0 : fd_event_report_signed_vote( &ev );
573 0 : }
574 :
575 : static void
576 : handle_vote_msg( fd_txsend_tile_t * ctx,
577 : fd_stem_context_t * stem,
578 : fd_tower_slot_done_t const * slot_done,
579 0 : ulong tsorig_comp ) {
580 0 : if( FD_UNLIKELY( slot_done->vote_slot==ULONG_MAX ) ) return;
581 0 : if( FD_UNLIKELY( !slot_done->has_vote_txn ) ) return;
582 :
583 0 : ctx->voted_slot = slot_done->vote_slot;
584 :
585 0 : fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->txsend_out->mem, ctx->txsend_out->chunk );
586 0 : FD_TEST( slot_done->vote_txn_sz<=FD_TXN_MTU );
587 0 : txnm->payload_sz = (ushort)slot_done->vote_txn_sz;
588 0 : txnm->source_ipv4 = ctx->src_ip_addr;
589 0 : txnm->source_tpu = FD_TXN_M_TPU_SOURCE_TXSEND;
590 0 : txnm->block_engine.bundle_id = 0UL;
591 0 : fd_memcpy( fd_txn_m_payload( txnm ), slot_done->vote_txn, slot_done->vote_txn_sz );
592 :
593 0 : txnm->txn_t_sz = (ushort)fd_txn_parse( slot_done->vote_txn, slot_done->vote_txn_sz, fd_txn_m_txn_t( txnm ), NULL );
594 0 : FD_TEST( txnm->txn_t_sz );
595 :
596 0 : uchar * payload = fd_txn_m_payload( txnm );
597 0 : fd_txn_t const * txn = fd_txn_m_txn_t_const( txnm );
598 :
599 0 : uchar * signatures = payload + txn->signature_off;
600 0 : uchar const * message = payload + txn->message_off;
601 0 : ulong message_sz = fd_txn_msg_sz( txn, slot_done->vote_txn_sz );
602 0 : fd_keyguard_client_vote_txn_sign( ctx->keyguard_client, signatures, slot_done->authority_idx, message, message_sz );
603 :
604 0 : FD_BASE58_ENCODE_64_BYTES( signatures, vote_sig_b58 );
605 0 : FD_LOG_INFO(( "vote txn for slot %lu created: %s", slot_done->vote_slot, vote_sig_b58 ));
606 :
607 0 : report_signed_vote( payload, txn, signatures, slot_done->vote_txn_sz );
608 :
609 0 : for( ulong i=0UL; i<3UL; i++ ) {
610 0 : ulong target_slot = slot_done->vote_slot+1UL + i*FD_EPOCH_SLOTS_PER_ROTATION;
611 0 : fd_pubkey_t const * leader = fd_multi_epoch_leaders_get_leader_for_slot( ctx->mleaders, target_slot );
612 0 : if( FD_UNLIKELY( !leader ) ) {
613 0 : FD_LOG_WARNING(( "no leader found for slot %lu", target_slot ));
614 0 : continue;
615 0 : }
616 0 : send_vote_to_leader( ctx, leader, payload, slot_done->vote_txn_sz );
617 0 : }
618 :
619 0 : ulong msg_sz = fd_txn_m_realized_footprint( txnm, 0, 0 );
620 0 : ulong tspub_comp = fd_frag_meta_ts_comp( fd_tickcount() );
621 0 : fd_stem_publish( stem, ctx->txsend_out->idx, 1UL, ctx->txsend_out->chunk, msg_sz, 0UL, tsorig_comp, tspub_comp );
622 0 : ctx->txsend_out->chunk = fd_dcache_compact_next( ctx->txsend_out->chunk, msg_sz, ctx->txsend_out->chunk0, ctx->txsend_out->wmark );
623 0 : }
624 :
625 :
626 : static inline int
627 : before_frag( fd_txsend_tile_t * ctx,
628 : ulong in_idx,
629 : ulong seq,
630 0 : ulong sig ) {
631 0 : fd_startup_gate_busy( ctx->startup_gate );
632 :
633 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_TOWER ) ) ctx->tower_in_expect_seq = seq+1UL;
634 0 : if( FD_UNLIKELY( ctx->halt_net_frags && ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) return -1;
635 :
636 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
637 0 : return sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO && sig!=FD_GOSSIP_UPDATE_TAG_CONTACT_INFO_REMOVE;
638 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_TOWER ) ) {
639 0 : return sig!=FD_TOWER_SIG_SLOT_DONE;
640 0 : }
641 :
642 0 : return 0;
643 0 : }
644 :
645 : static void
646 : during_frag( fd_txsend_tile_t * ctx,
647 : ulong in_idx,
648 : ulong seq,
649 : ulong sig,
650 : ulong chunk,
651 : ulong sz,
652 0 : ulong ctl ) {
653 0 : (void)seq; (void)sig;
654 :
655 0 : ctx->chunk = chunk;
656 :
657 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) ) {
658 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark ) )
659 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu,%lu]", chunk, sz, ctx->in[in_idx].chunk0, ctx->in[in_idx].wmark, ctx->in[ in_idx ].mtu ));
660 :
661 0 : fd_epoch_info_msg_t const * msg = fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, chunk );
662 0 : FD_TEST( msg->staked_vote_cnt<=MAX_STAKE_WEIGHTS ); /* implicit sz verification since sz field on frag_meta too small */
663 0 : FD_TEST( msg->staked_id_cnt<=MAX_STAKE_WEIGHTS );
664 0 : } else {
665 0 : if( FD_UNLIKELY( chunk<ctx->in[ in_idx ].chunk0 || chunk>ctx->in[ in_idx ].wmark || sz>ctx->in[ in_idx ].mtu ) )
666 0 : FD_LOG_ERR(( "chunk %lu %lu corrupt, not in range [%lu,%lu,%lu]", chunk, sz, ctx->in[in_idx].chunk0, ctx->in[in_idx].wmark, ctx->in[ in_idx ].mtu ));
667 0 : }
668 :
669 0 : if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
670 0 : void const * src = fd_net_rx_translate_frag( &ctx->net_in_bounds[ in_idx ], chunk, ctl, sz );
671 0 : fd_memcpy( ctx->quic_buf, src, sz );
672 0 : }
673 0 : }
674 :
675 : static void
676 : after_frag( fd_txsend_tile_t * ctx,
677 : ulong in_idx,
678 : ulong seq,
679 : ulong sig,
680 : ulong sz,
681 : ulong tsorig,
682 : ulong tspub,
683 0 : fd_stem_context_t * stem ) {
684 0 : (void)seq; (void)sig; (void)tspub;
685 :
686 0 : if( FD_LIKELY( ctx->in_kind[ in_idx ]==IN_KIND_NET ) ) {
687 0 : uchar * ip_packet = ctx->quic_buf+sizeof(fd_eth_hdr_t);
688 0 : ulong ip_packet_sz = sz-sizeof(fd_eth_hdr_t);
689 0 : fd_quic_process_packet( ctx->quic, ip_packet, ip_packet_sz, fd_log_wallclock() );
690 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_GOSSIP ) ) {
691 0 : if( FD_LIKELY( sig==FD_GOSSIP_UPDATE_TAG_CONTACT_INFO ) ) handle_contact_info_update( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, ctx->chunk ) );
692 0 : else handle_contact_info_remove( ctx, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, ctx->chunk ) );
693 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_TOWER ) ) {
694 0 : handle_vote_msg( ctx, stem, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, ctx->chunk ), tsorig );
695 0 : } else if( FD_UNLIKELY( ctx->in_kind[ in_idx ]==IN_KIND_EPOCH ) ) {
696 0 : fd_multi_epoch_leaders_epoch_msg_init( ctx->mleaders, fd_chunk_to_laddr_const( ctx->in[ in_idx ].mem, ctx->chunk ) );
697 0 : fd_multi_epoch_leaders_stake_msg_fini( ctx->mleaders );
698 0 : ctx->leader_schedules++;
699 0 : } else {
700 0 : FD_LOG_ERR(( "unknown in_kind %d on link %lu", ctx->in_kind[ in_idx ], in_idx ));
701 0 : }
702 0 : }
703 :
704 : static void
705 : privileged_init( fd_topo_t const * topo,
706 0 : fd_topo_tile_t const * tile ) {
707 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
708 :
709 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
710 0 : fd_txsend_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_txsend_tile_t), sizeof(fd_txsend_tile_t) );
711 :
712 0 : if( FD_UNLIKELY( !strcmp( tile->txsend.identity_key_path, "" ) ) )
713 0 : FD_LOG_ERR(( "identity_key_path not set" ));
714 :
715 0 : ctx->identity_key[ 0 ] = *(fd_pubkey_t const *)fd_type_pun_const( fd_keyload_load( tile->txsend.identity_key_path, /* pubkey only: */ 1 ) );
716 :
717 0 : FD_TEST( fd_rng_secure( &ctx->seed, sizeof(ctx->seed) ) );
718 0 : }
719 :
720 : static inline fd_txsend_out_t
721 : out1( fd_topo_t const * topo,
722 : fd_topo_tile_t const * tile,
723 0 : char const * name ) {
724 0 : ulong idx = ULONG_MAX;
725 :
726 0 : for( ulong i=0UL; i<tile->out_cnt; i++ ) {
727 0 : fd_topo_link_t const * link = &topo->links[ tile->out_link_id[ i ] ];
728 0 : if( !strcmp( link->name, name ) ) {
729 0 : if( FD_UNLIKELY( idx!=ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had multiple output links named %s but expected one", tile->name, tile->kind_id, name ));
730 0 : idx = i;
731 0 : }
732 0 : }
733 :
734 0 : if( FD_UNLIKELY( idx==ULONG_MAX ) ) FD_LOG_ERR(( "tile %s:%lu had no output link named %s", tile->name, tile->kind_id, name ));
735 :
736 0 : void * mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ idx ] ].dcache_obj_id ].wksp_id ].wksp;
737 0 : ulong chunk0 = fd_dcache_compact_chunk0( mem, topo->links[ tile->out_link_id[ idx ] ].dcache );
738 0 : ulong wmark = fd_dcache_compact_wmark ( mem, topo->links[ tile->out_link_id[ idx ] ].dcache, topo->links[ tile->out_link_id[ idx ] ].mtu );
739 :
740 0 : return (fd_txsend_out_t){ .idx = idx, .mem = mem, .chunk0 = chunk0, .wmark = wmark, .chunk = chunk0 };
741 0 : }
742 :
743 : static void
744 : unprivileged_init( fd_topo_t const * topo,
745 0 : fd_topo_tile_t const * tile ) {
746 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
747 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
748 0 : fd_txsend_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_txsend_tile_t), sizeof(fd_txsend_tile_t) );
749 0 : void * _quic = FD_SCRATCH_ALLOC_APPEND( l, fd_quic_align(), fd_quic_footprint( &quic_limits ) );
750 0 : void * _peer_map = FD_SCRATCH_ALLOC_APPEND( l, peer_map_align(), peer_map_footprint( 2UL*FD_CONTACT_INFO_TABLE_SIZE ) );
751 :
752 0 : ctx->quic = fd_quic_join( fd_quic_new( _quic, &quic_limits ) );
753 0 : FD_TEST( ctx->quic );
754 :
755 0 : ctx->leader_schedules = 0UL;
756 :
757 0 : ctx->mleaders = fd_multi_epoch_leaders_join( fd_multi_epoch_leaders_new( ctx->mleaders_mem ) );
758 0 : FD_TEST( ctx->mleaders );
759 :
760 0 : ctx->peer_map = peer_map_join( peer_map_new( _peer_map, 2UL*FD_CONTACT_INFO_TABLE_SIZE, ctx->seed ) );
761 0 : FD_TEST( ctx->peer_map );
762 :
763 0 : fd_aio_t * quic_tx_aio = fd_aio_join( fd_aio_new( ctx->quic_tx_aio, ctx, quic_tx_aio_send ) );
764 0 : FD_TEST( quic_tx_aio );
765 0 : fd_quic_set_aio_net_tx( ctx->quic, quic_tx_aio );
766 :
767 0 : ctx->quic->config.role = FD_QUIC_ROLE_CLIENT;
768 0 : ctx->quic->config.idle_timeout = 30e9L;
769 0 : ctx->quic->config.ack_delay = 25e6L;
770 0 : ctx->quic->config.keep_alive = 1;
771 0 : ctx->quic->config.sign = quic_tls_cv_sign;
772 0 : ctx->quic->config.sign_ctx = ctx;
773 0 : fd_memcpy( ctx->quic->config.identity_public_key, ctx->identity_key, sizeof(ctx->identity_key) );
774 :
775 0 : ctx->quic->cb.conn_final = quic_conn_final;
776 0 : ctx->quic->cb.quic_ctx = ctx;
777 :
778 0 : FD_TEST( fd_quic_init( ctx->quic ));
779 :
780 0 : for( ulong i=0UL; i<FD_CONTACT_INFO_TABLE_SIZE; i++ ) {
781 0 : ctx->peers[ i ] = (peer_entry_t){0};
782 0 : }
783 :
784 0 : ctx->conns_len = 0UL;
785 0 : ctx->voted_slot = ULONG_MAX;
786 0 : ctx->net_id = 0;
787 :
788 0 : ctx->src_ip_addr = tile->txsend.ip_addr;
789 0 : ctx->src_port = tile->txsend.txsend_src_port;
790 0 : fd_ip4_udp_hdr_init( ctx->packet_hdr, FD_TXN_MTU, ctx->src_ip_addr, ctx->src_port );
791 :
792 0 : fd_startup_gate_init( ctx->startup_gate, topo, tile->in_cnt );
793 :
794 0 : FD_TEST( tile->in_cnt<sizeof(ctx->in_kind)/sizeof(ctx->in_kind[ 0 ]) );
795 0 : for( ulong i=0UL; i<tile->in_cnt; i++ ) {
796 0 : fd_topo_link_t const * link = &topo->links[ tile->in_link_id[ i ] ];
797 0 : fd_topo_wksp_t const * link_wksp = &topo->workspaces[ topo->objs[ link->dcache_obj_id ].wksp_id ];
798 :
799 0 : ctx->in[ i ].mem = link_wksp->wksp;
800 0 : ctx->in[ i ].chunk0 = fd_dcache_compact_chunk0( ctx->in[ i ].mem, link->dcache );
801 0 : ctx->in[ i ].wmark = fd_dcache_compact_wmark ( ctx->in[ i ].mem, link->dcache, link->mtu );
802 0 : ctx->in[ i ].mtu = link->mtu;
803 :
804 0 : if( !strcmp( link->name, "net_txsend" ) ) {
805 0 : fd_net_rx_bounds_init( &ctx->net_in_bounds[ i ], link->dcache );
806 0 : ctx->in_kind[ i ] = IN_KIND_NET;
807 0 : } else if( !strcmp( link->name, "gossip_out" ) ) ctx->in_kind[ i ] = IN_KIND_GOSSIP;
808 0 : else if( !strcmp( link->name, "replay_epoch" ) ) ctx->in_kind[ i ] = IN_KIND_EPOCH;
809 0 : else if( !strcmp( link->name, "tower_out" ) ) ctx->in_kind[ i ] = IN_KIND_TOWER;
810 0 : else if( !strcmp( link->name, "sign_txsend" ) ) ctx->in_kind[ i ] = IN_KIND_SIGN;
811 0 : else FD_LOG_ERR(( "unexpected input link name %s", link->name ));
812 0 : }
813 :
814 0 : *ctx->txsend_out = out1( topo, tile, "txsend_out" );
815 0 : *ctx->net_out = out1( topo, tile, "txsend_net" );
816 :
817 0 : ulong sign_in_idx = fd_topo_find_tile_in_link ( topo, tile, "sign_txsend", tile->kind_id );
818 0 : ulong sign_out_idx = fd_topo_find_tile_out_link( topo, tile, "txsend_sign", tile->kind_id );
819 0 : FD_TEST( sign_in_idx!=ULONG_MAX );
820 0 : fd_topo_link_t const * sign_in = &topo->links[ tile->in_link_id[ sign_in_idx ] ];
821 0 : fd_topo_link_t const * sign_out = &topo->links[ tile->out_link_id[ sign_out_idx ] ];
822 0 : if( FD_UNLIKELY( !fd_keyguard_client_join( fd_keyguard_client_new( ctx->keyguard_client,
823 0 : sign_out->mcache,
824 0 : sign_out->dcache,
825 0 : sign_in->mcache,
826 0 : sign_in->dcache,
827 0 : sign_out->mtu ) ) ) ) {
828 0 : FD_LOG_ERR(( "failed to construct keyguard" ));
829 0 : }
830 :
831 0 : ctx->keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->id_keyswitch_obj_id ) );
832 0 : FD_TEST( ctx->keyswitch );
833 :
834 0 : ctx->av_keyswitch = NULL;
835 0 : if( FD_UNLIKELY( tile->av_keyswitch_obj_id!=ULONG_MAX ) ) {
836 0 : ctx->av_keyswitch = fd_keyswitch_join( fd_topo_obj_laddr( topo, tile->av_keyswitch_obj_id ) );
837 0 : FD_TEST( ctx->av_keyswitch );
838 0 : }
839 :
840 0 : ctx->tower_in_expect_seq = 0UL;
841 0 : ctx->halt_net_frags = 0;
842 :
843 0 : fd_histf_join( fd_histf_new( ctx->quic->metrics.service_duration, FD_MHIST_SECONDS_MIN( TXSEND, SERVICE_DURATION_SECONDS ),
844 0 : FD_MHIST_SECONDS_MAX( TXSEND, SERVICE_DURATION_SECONDS ) ) );
845 0 : fd_histf_join( fd_histf_new( ctx->quic->metrics.receive_duration, FD_MHIST_SECONDS_MIN( TXSEND, RX_DURATION_SECONDS ),
846 0 : FD_MHIST_SECONDS_MAX( TXSEND, RX_DURATION_SECONDS ) ) );
847 :
848 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
849 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
850 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
851 0 : }
852 :
853 : static ulong
854 : populate_allowed_seccomp( fd_topo_t const * topo FD_PARAM_UNUSED,
855 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
856 : ulong out_cnt,
857 0 : struct sock_filter * out ) {
858 :
859 0 : populate_sock_filter_policy_fd_txsend_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
860 0 : return sock_filter_policy_fd_txsend_tile_instr_cnt;
861 0 : }
862 :
863 : static ulong
864 : populate_allowed_fds( fd_topo_t const * topo FD_PARAM_UNUSED,
865 : fd_topo_tile_t const * tile FD_PARAM_UNUSED,
866 : ulong out_fds_cnt,
867 0 : int * out_fds ) {
868 0 : if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
869 :
870 0 : ulong out_cnt = 0;
871 0 : out_fds[ out_cnt++ ] = 2UL; /* stderr */
872 0 : if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
873 0 : out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
874 0 : return out_cnt;
875 0 : }
876 :
877 0 : #define STEM_BURST 1UL
878 0 : #define STEM_LAZY (128L*3000L)
879 :
880 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_txsend_tile_t
881 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_txsend_tile_t)
882 :
883 0 : #define STEM_CALLBACK_DURING_HOUSEKEEPING during_housekeeping
884 0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
885 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
886 0 : #define STEM_CALLBACK_BEFORE_FRAG before_frag
887 0 : #define STEM_CALLBACK_DURING_FRAG during_frag
888 0 : #define STEM_CALLBACK_AFTER_FRAG after_frag
889 :
890 : #include "../../disco/stem/fd_stem.c"
891 :
892 : static ulong
893 0 : max_event_sz( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
894 0 : return sizeof(fd_event_signed_vote_t);
895 0 : }
896 :
897 : fd_topo_run_tile_t fd_tile_txsend = {
898 : .name = "txsend",
899 : .max_event_sz = max_event_sz,
900 : .populate_allowed_seccomp = populate_allowed_seccomp,
901 : .populate_allowed_fds = populate_allowed_fds,
902 : .scratch_align = scratch_align,
903 : .scratch_footprint = scratch_footprint,
904 : .privileged_init = privileged_init,
905 : .unprivileged_init = unprivileged_init,
906 : .run = stem_run,
907 : };
|