Line data Source code
1 : /* fd_bundle_client.c steps gRPC related tasks. */
2 :
3 : #define _GNU_SOURCE /* SOL_TCP */
4 : #include "fd_bundle_auth.h"
5 : #include "fd_bundle_tile_private.h"
6 : #include "fd_bundle_tile.h"
7 : #include "../waker/fd_waker.h"
8 : #include "../fd_txn_m.h"
9 : #include "../../waltz/h2/fd_h2_conn.h"
10 : #include "../../waltz/http/fd_url.h" /* fd_url_unescape */
11 : #include "../../ballet/base58/fd_base58.h"
12 : #include "../../ballet/ed25519/fd_x25519.h"
13 : #include "../../ballet/pb/fd_pb_tokenize.h"
14 : #include "../../util/net/fd_ip4.h"
15 :
16 : #include <fcntl.h>
17 : #include <errno.h>
18 : #include <unistd.h> /* close */
19 : #include <poll.h> /* poll */
20 : #include <sys/epoll.h>
21 : #include <sys/socket.h> /* socket */
22 : #include <netinet/in.h>
23 : #include <netinet/ip.h>
24 : #include <netinet/tcp.h>
25 :
26 0 : #define FD_BUNDLE_CLIENT_REQUEST_TIMEOUT ((long)8e9) /* 8 seconds */
27 0 : #define FD_BUNDLE_CONN_TIMEOUT_NS ((long)3e9) /* TCP connect plus TLS and HTTP/2 setup */
28 :
29 :
30 : __attribute__((weak)) long
31 0 : fd_bundle_now( fd_bundle_tile_t const * ctx ) {
32 0 : return fd_clock_tile_now( ctx->clock );
33 0 : }
34 :
35 : void
36 0 : fd_bundle_client_reset( fd_bundle_tile_t * ctx ) {
37 0 : if( FD_UNLIKELY( ctx->tcp_sock >= 0 ) ) {
38 0 : if( FD_UNLIKELY( 0!=close( ctx->tcp_sock ) ) ) {
39 0 : FD_LOG_ERR(( "close(tcp_sock=%i) failed (%i-%s)", ctx->tcp_sock, errno, fd_io_strerror( errno ) ));
40 0 : }
41 0 : ctx->tcp_sock = -1;
42 0 : ctx->tcp_sock_connected = 0;
43 0 : ctx->sock_in_epoll = 0;
44 0 : ctx->epoll_out_armed = 0;
45 0 : }
46 0 : fd_tlsrec_conn_fini( ctx->tls_conn );
47 0 : fd_memzero_explicit( ctx->tls->kex_private_key, 32UL );
48 0 : ctx->defer_reset = 0;
49 :
50 0 : ctx->builder_info_avail = 0;
51 0 : ctx->builder_info_wait = 0;
52 0 : ctx->packet_subscription_live = 0;
53 0 : ctx->packet_subscription_wait = 0;
54 0 : ctx->bundle_subscription_live = 0;
55 0 : ctx->bundle_subscription_wait = 0;
56 :
57 0 : fd_memset( ctx->rtt, 0, sizeof(fd_rtt_estimate_t) );
58 :
59 0 : fd_bundle_tile_backoff( ctx, fd_bundle_now( ctx ) );
60 :
61 0 : fd_bundle_auther_reset( &ctx->auther );
62 0 : fd_grpc_client_reset( ctx->grpc_client );
63 0 : }
64 :
65 : static int
66 : fd_bundle_client_do_connect( fd_bundle_tile_t const * ctx,
67 0 : uint ip4_addr ) {
68 0 : struct sockaddr_in addr = {
69 0 : .sin_family = AF_INET,
70 0 : .sin_addr.s_addr = ip4_addr,
71 0 : .sin_port = fd_ushort_bswap( ctx->server_tcp_port )
72 0 : };
73 0 : int err = connect( ctx->tcp_sock, fd_type_pun_const( &addr ), sizeof(struct sockaddr_in) );
74 : /* FD_LIKELY is used here as EINPROGRESS is expected even to local tcp ports */
75 0 : if( FD_LIKELY( err==-1 ) ) {
76 0 : return errno;
77 0 : }
78 0 : return 0;
79 0 : }
80 :
81 : static int
82 0 : fd_bundle_client_get_connect_result( fd_bundle_tile_t const * ctx ) {
83 0 : int so_err = 0;
84 0 : socklen_t so_err_sz = sizeof(so_err);
85 0 : if( FD_UNLIKELY( getsockopt( ctx->tcp_sock, SOL_SOCKET, SO_ERROR, &so_err, &so_err_sz )==-1 ) ) {
86 0 : return errno;
87 0 : }
88 0 : return so_err;
89 0 : }
90 :
91 : static void
92 0 : fd_bundle_client_create_conn( fd_bundle_tile_t * ctx ) {
93 0 : fd_bundle_client_reset( ctx );
94 :
95 : /* FIXME IPv6 support */
96 0 : fd_addrinfo_t hints = {0};
97 0 : hints.ai_family = AF_INET;
98 0 : fd_addrinfo_t * res = NULL;
99 0 : uchar scratch[ 4096 ];
100 0 : void * pscratch = scratch;
101 0 : int err = fd_getaddrinfo( ctx->server_fqdn, &hints, &res, &pscratch, sizeof(scratch) );
102 0 : if( FD_UNLIKELY( err ) ) {
103 0 : FD_LOG_WARNING(( "fd_getaddrinfo `%s` failed (%d-%s)", ctx->server_fqdn, err, fd_gai_strerror( err ) ));
104 0 : fd_bundle_client_reset( ctx );
105 0 : ctx->metrics.transport_fail_cnt++;
106 0 : return;
107 0 : }
108 0 : uint const ip4_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr;
109 0 : ctx->server_ip4_addr = ip4_addr;
110 :
111 0 : int tcp_sock = socket( AF_INET, SOCK_STREAM|SOCK_CLOEXEC, 0 );
112 0 : if( FD_UNLIKELY( tcp_sock<0 ) ) {
113 0 : FD_LOG_ERR(( "socket(AF_INET,SOCK_STREAM|SOCK_CLOEXEC,0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
114 0 : }
115 0 : ctx->tcp_sock = tcp_sock;
116 :
117 0 : struct epoll_event ev = { .events = EPOLLIN|EPOLLOUT, .data.fd = tcp_sock };
118 0 : if( FD_UNLIKELY( -1==epoll_ctl( FD_WAKER_INNER_FD( ctx->waker_client_idx ), EPOLL_CTL_ADD, tcp_sock, &ev ) ) ) {
119 0 : FD_LOG_ERR(( "epoll_ctl(ADD,tcp_sock) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
120 0 : }
121 0 : ctx->sock_in_epoll = 1;
122 0 : ctx->epoll_out_armed = 1;
123 :
124 0 : if( FD_UNLIKELY( 0!=setsockopt( tcp_sock, SOL_SOCKET, SO_RCVBUF, &ctx->so_rcvbuf, sizeof(int) ) ) ) {
125 0 : FD_LOG_ERR(( "setsockopt(SOL_SOCKET,SO_RCVBUF,%i) failed (%i-%s)", ctx->so_rcvbuf, errno, fd_io_strerror( errno ) ));
126 0 : }
127 :
128 0 : int tcp_nodelay = 1;
129 0 : if( FD_UNLIKELY( 0!=setsockopt( tcp_sock, SOL_TCP, TCP_NODELAY, &tcp_nodelay, sizeof(int) ) ) ) {
130 0 : FD_LOG_ERR(( "setsockopt failed (%d-%s)", errno, fd_io_strerror( errno ) ));
131 0 : }
132 :
133 0 : if( FD_UNLIKELY( fcntl( tcp_sock, F_SETFL, O_NONBLOCK )==-1 ) ) {
134 0 : FD_LOG_ERR(( "fcntl(tcp_sock,F_SETFL,O_NONBLOCK) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
135 0 : }
136 :
137 0 : char const * scheme = ctx->is_ssl ? "https" : "http";
138 :
139 0 : FD_LOG_INFO(( "Connecting to %s://" FD_IP4_ADDR_FMT ":%hu (%.*s)",
140 0 : scheme,
141 0 : FD_IP4_ADDR_FMT_ARGS( ip4_addr ), ctx->server_tcp_port,
142 0 : (int)ctx->server_sni_len, ctx->server_sni ));
143 :
144 0 : int connect_err = fd_bundle_client_do_connect( ctx, ip4_addr );
145 : /* FD_LIKELY as EINPROGRESS is expected */
146 0 : if( FD_LIKELY( connect_err ) ) {
147 0 : if( FD_UNLIKELY( connect_err!=EINPROGRESS ) ) {
148 0 : FD_LOG_WARNING(( "connect(tcp_sock," FD_IP4_ADDR_FMT ":%u) failed (%i-%s)",
149 0 : FD_IP4_ADDR_FMT_ARGS( ip4_addr ), ctx->server_tcp_port,
150 0 : connect_err, fd_io_strerror( connect_err ) ));
151 0 : fd_bundle_client_reset( ctx );
152 0 : ctx->metrics.transport_fail_cnt++;
153 0 : return;
154 0 : }
155 0 : }
156 :
157 0 : if( ctx->is_ssl ) {
158 0 : fd_tls_t * tls = ctx->tls;
159 0 : ulong sni_len = strlen( ctx->server_sni );
160 0 : fd_memcpy( tls->server_name, ctx->server_sni, sni_len );
161 0 : tls->server_name[ sni_len ] = '\0';
162 0 : tls->server_name_len = (ushort)sni_len;
163 0 : if( FD_UNLIKELY( !fd_rng_secure( tls->kex_private_key, 32UL ) ) ) FD_LOG_CRIT(( "fd_rng_secure failed" ));
164 0 : fd_x25519_public( tls->kex_public_key, tls->kex_private_key );
165 0 : fd_tlsrec_conn_init( ctx->tls_conn, tls, 0 );
166 0 : }
167 :
168 0 : fd_grpc_client_reset( ctx->grpc_client );
169 0 : fd_keepalive_init( ctx->keepalive, ctx->rng, ctx->keepalive_interval, ctx->keepalive_interval, fd_bundle_now( ctx ) );
170 0 : ctx->conn_deadline = fd_bundle_now( ctx ) + FD_BUNDLE_CONN_TIMEOUT_NS;
171 0 : }
172 :
173 : static int
174 : fd_bundle_client_drive_io( fd_bundle_tile_t * ctx,
175 : long now,
176 0 : int * charge_busy ) {
177 0 : if( ctx->is_ssl ) {
178 0 : return fd_grpc_client_rxtx_tls( ctx->grpc_client, ctx->tls_conn, ctx->tcp_sock, now, charge_busy );
179 0 : }
180 :
181 0 : return fd_grpc_client_rxtx_socket( ctx->grpc_client, ctx->tcp_sock, now, charge_busy );
182 0 : }
183 :
184 : static void
185 0 : fd_bundle_client_request_builder_info( fd_bundle_tile_t * ctx ) {
186 0 : if( FD_UNLIKELY( fd_grpc_client_request_is_blocked( ctx->grpc_client ) ) ) return;
187 :
188 0 : static char const path[] = "/block_engine.BlockEngineValidator/GetBlockBuilderFeeInfo";
189 0 : fd_grpc_h2_stream_t * request = fd_grpc_client_request_start(
190 0 : ctx->grpc_client,
191 0 : path, sizeof(path)-1,
192 0 : FD_BUNDLE_CLIENT_REQ_Bundle_GetBlockBuilderFeeInfo,
193 0 : (uchar const *)"", 0UL, /* empty request message */
194 0 : ctx->auther.access_token, ctx->auther.access_token_sz,
195 0 : 0 /* is_streaming */
196 0 : );
197 0 : if( FD_UNLIKELY( !request ) ) return;
198 0 : fd_grpc_client_deadline_set(
199 0 : request,
200 0 : FD_GRPC_DEADLINE_RX_END,
201 0 : fd_log_wallclock() + FD_BUNDLE_CLIENT_REQUEST_TIMEOUT );
202 :
203 0 : ctx->builder_info_wait = 1;
204 0 : }
205 :
206 : static void
207 0 : fd_bundle_client_subscribe_packets( fd_bundle_tile_t * ctx ) {
208 0 : if( FD_UNLIKELY( fd_grpc_client_request_is_blocked( ctx->grpc_client ) ) ) return;
209 :
210 0 : static char const path[] = "/block_engine.BlockEngineValidator/SubscribePackets";
211 0 : fd_grpc_h2_stream_t * request = fd_grpc_client_request_start(
212 0 : ctx->grpc_client,
213 0 : path, sizeof(path)-1,
214 0 : FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets,
215 0 : (uchar const *)"", 0UL, /* empty request message */
216 0 : ctx->auther.access_token, ctx->auther.access_token_sz,
217 0 : 0 /* is_streaming */
218 0 : );
219 0 : if( FD_UNLIKELY( !request ) ) return;
220 0 : fd_grpc_client_deadline_set(
221 0 : request,
222 0 : FD_GRPC_DEADLINE_HEADER,
223 0 : fd_log_wallclock() + FD_BUNDLE_CLIENT_REQUEST_TIMEOUT );
224 :
225 0 : ctx->packet_subscription_wait = 1;
226 0 : }
227 :
228 : static void
229 0 : fd_bundle_client_subscribe_bundles( fd_bundle_tile_t * ctx ) {
230 0 : if( FD_UNLIKELY( fd_grpc_client_request_is_blocked( ctx->grpc_client ) ) ) return;
231 :
232 0 : static char const path[] = "/block_engine.BlockEngineValidator/SubscribeBundles";
233 0 : fd_grpc_h2_stream_t * request = fd_grpc_client_request_start(
234 0 : ctx->grpc_client,
235 0 : path, sizeof(path)-1,
236 0 : FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles,
237 0 : (uchar const *)"", 0UL, /* empty request message */
238 0 : ctx->auther.access_token, ctx->auther.access_token_sz,
239 0 : 0 /* is_streaming */
240 0 : );
241 0 : if( FD_UNLIKELY( !request ) ) return;
242 0 : fd_grpc_client_deadline_set(
243 0 : request,
244 0 : FD_GRPC_DEADLINE_HEADER,
245 0 : fd_log_wallclock() + FD_BUNDLE_CLIENT_REQUEST_TIMEOUT );
246 :
247 0 : ctx->bundle_subscription_wait = 1;
248 0 : }
249 :
250 : void
251 0 : fd_bundle_client_send_ping( fd_bundle_tile_t * ctx ) {
252 0 : if( FD_UNLIKELY( !ctx->grpc_client ) ) return; /* no client */
253 0 : fd_h2_conn_t * conn = fd_grpc_client_h2_conn( ctx->grpc_client );
254 0 : if( FD_UNLIKELY( !conn ) ) return; /* no conn */
255 0 : if( FD_UNLIKELY( conn->flags ) ) return; /* conn busy */
256 0 : fd_h2_rbuf_t * rbuf_tx = fd_grpc_client_rbuf_tx( ctx->grpc_client );
257 :
258 0 : if( FD_LIKELY( fd_h2_tx_ping( conn, rbuf_tx ) ) ) {
259 0 : long now = fd_bundle_now( ctx );
260 0 : fd_keepalive_tx( ctx->keepalive, ctx->rng, now );
261 0 : FD_LOG_DEBUG(( "Keepalive TX (deadline=+%gs)", (double)( ctx->keepalive->ts_deadline-now )/1e9 ));
262 0 : }
263 0 : }
264 :
265 : static void
266 : fd_bundle_client_epoll_out( fd_bundle_tile_t * ctx,
267 0 : int want ) {
268 0 : if( FD_UNLIKELY( !ctx->sock_in_epoll ) ) return;
269 0 : if( FD_LIKELY( want==(int)ctx->epoll_out_armed ) ) return;
270 0 : struct epoll_event ev = { .events = EPOLLIN | (want ? EPOLLOUT : 0U), .data.fd = ctx->tcp_sock };
271 0 : if( FD_UNLIKELY( -1==epoll_ctl( FD_WAKER_INNER_FD( ctx->waker_client_idx ), EPOLL_CTL_MOD, ctx->tcp_sock, &ev ) ) )
272 0 : FD_LOG_ERR(( "epoll_ctl(MOD,tcp_sock) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
273 0 : ctx->epoll_out_armed = !!want;
274 0 : }
275 :
276 : long
277 : fd_bundle_client_next_deadline( fd_bundle_tile_t const * ctx,
278 0 : long now ) {
279 : /* Connecting: completion/failure arrives as an EPOLLOUT/EPOLLERR
280 : event, or the setup times out. */
281 0 : if( FD_UNLIKELY( ctx->tcp_sock>=0 && !ctx->tcp_sock_connected ) ) return ctx->conn_deadline;
282 :
283 : /* Disconnected: next action is the reconnect attempt. */
284 0 : if( FD_UNLIKELY( ctx->tcp_sock<0 ) ) return fd_long_max( ctx->backoff_until, now );
285 :
286 : /* Decrypted TLS bytes buffered in the gRPC client do not make the fd
287 : readable: drain now or they wait for the next unrelated event.
288 : Except while TX is blocked: then they are held because the HTTP/2
289 : rings are full behind the parked send, and only the EPOLLOUT armed
290 : for that send can make progress. */
291 0 : if( FD_UNLIKELY( ctx->is_ssl &&
292 0 : fd_grpc_client_tls_rx_pending( ctx->grpc_client ) &&
293 0 : !fd_grpc_client_tls_tx_pending( ctx->grpc_client ) ) ) return now;
294 :
295 0 : int request_ready = ctx->backoff_until<=now &&
296 0 : !fd_grpc_client_request_is_blocked( ctx->grpc_client ) &&
297 0 : !ctx->auther.needs_poll &&
298 0 : ctx->auther.state==FD_BUNDLE_AUTH_STATE_DONE_WAIT;
299 0 : long deadline = fd_grpc_client_next_deadline( ctx->grpc_client );
300 0 : if( FD_LIKELY( ctx->keepalive->interval ) ) {
301 0 : if( ctx->keepalive->inflight )
302 0 : deadline = fd_long_min( deadline, ctx->keepalive->ts_deadline );
303 0 : else if( request_ready && !fd_grpc_client_h2_conn( ctx->grpc_client )->flags )
304 0 : deadline = fd_long_min( deadline, ctx->keepalive->ts_next_tx );
305 0 : }
306 0 : if( FD_LIKELY( request_ready && ctx->builder_info_avail && !ctx->builder_info_wait ) )
307 0 : deadline = fd_long_min( deadline, ctx->builder_info_valid_until );
308 0 : if( FD_UNLIKELY( ctx->backoff_until>now ) )
309 0 : deadline = fd_long_min( deadline, ctx->backoff_until );
310 0 : if( FD_UNLIKELY( !fd_grpc_client_is_connected( ctx->grpc_client ) ) )
311 0 : deadline = fd_long_min( deadline, ctx->conn_deadline );
312 0 : return deadline;
313 0 : }
314 :
315 : int
316 : fd_bundle_client_step_reconnect( fd_bundle_tile_t * ctx,
317 0 : long now ) {
318 : /* Drive auth */
319 0 : if( FD_UNLIKELY( ctx->auther.needs_poll ) ) {
320 0 : fd_bundle_auther_poll( &ctx->auther, ctx->grpc_client, ctx->keyguard_client );
321 0 : return 1;
322 0 : }
323 0 : if( FD_UNLIKELY( ctx->auther.state!=FD_BUNDLE_AUTH_STATE_DONE_WAIT ) ) return 0;
324 :
325 : /* Request block builder info */
326 0 : int const builder_info_expired = ( ctx->builder_info_valid_until - now )<0;
327 0 : if( FD_UNLIKELY( ( ( !ctx->builder_info_avail ) |
328 0 : ( builder_info_expired ) ) &
329 0 : ( !ctx->builder_info_wait ) ) ) {
330 0 : fd_bundle_client_request_builder_info( ctx );
331 0 : return 1;
332 0 : }
333 :
334 : /* Subscribe to packets */
335 0 : if( FD_UNLIKELY( !ctx->packet_subscription_live && !ctx->packet_subscription_wait ) ) {
336 0 : fd_bundle_client_subscribe_packets( ctx );
337 0 : return 1;
338 0 : }
339 :
340 : /* Subscribe to bundles */
341 0 : if( FD_UNLIKELY( !ctx->bundle_subscription_live && !ctx->bundle_subscription_wait ) ) {
342 0 : fd_bundle_client_subscribe_bundles( ctx );
343 0 : return 1;
344 0 : }
345 :
346 : /* Send a PING */
347 0 : if( FD_UNLIKELY( fd_keepalive_should_tx( ctx->keepalive, now ) ) ) {
348 0 : fd_bundle_client_send_ping( ctx );
349 0 : return 1;
350 0 : }
351 :
352 0 : return 0;
353 0 : }
354 :
355 : static void
356 : fd_bundle_client_step1( fd_bundle_tile_t * ctx,
357 0 : int * charge_busy ) {
358 0 : if( FD_UNLIKELY( ctx->tcp_sock>=0 &&
359 0 : !( ctx->tcp_sock_connected && fd_grpc_client_is_connected( ctx->grpc_client ) ) &&
360 0 : fd_bundle_now( ctx )>=ctx->conn_deadline ) ) {
361 0 : FD_LOG_INFO(( "Bundle connect to " FD_IP4_ADDR_FMT ":%hu timed out (%s)", FD_IP4_ADDR_FMT_ARGS( ctx->server_ip4_addr ), ctx->server_tcp_port,
362 0 : ctx->tcp_sock_connected ? "TLS/HTTP/2 setup" : "TCP connect" ));
363 0 : fd_bundle_client_reset( ctx );
364 0 : ctx->metrics.transport_fail_cnt++;
365 0 : *charge_busy = 1;
366 0 : return;
367 0 : }
368 :
369 : /* Wait for TCP socket to connect */
370 0 : if( FD_UNLIKELY( !ctx->tcp_sock_connected ) ) {
371 0 : if( FD_UNLIKELY( ctx->tcp_sock < 0 ) ) goto reconnect;
372 :
373 0 : struct pollfd pfds[1] = {
374 0 : { .fd = ctx->tcp_sock, .events = POLLOUT }
375 0 : };
376 0 : int poll_res = fd_syscall_poll( pfds, 1, 0 );
377 0 : if( FD_UNLIKELY( poll_res<0 ) ) {
378 0 : FD_LOG_ERR(( "fd_syscall_poll(tcp_sock) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
379 0 : }
380 0 : if( poll_res==0 ) return;
381 :
382 0 : int connect_result = 0;
383 0 : if( pfds[0].revents & (POLLERR|POLLHUP) ) {
384 0 : connect_result = fd_bundle_client_get_connect_result( ctx );
385 0 : connect_failed:
386 0 : FD_LOG_INFO(( "Bundle gRPC connect attempt failed (%i-%s)", connect_result, fd_io_strerror( connect_result ) ));
387 0 : fd_bundle_client_reset( ctx );
388 0 : ctx->metrics.transport_fail_cnt++;
389 0 : *charge_busy = 1;
390 0 : return;
391 0 : }
392 0 : if( FD_UNLIKELY( !(pfds[0].revents & POLLOUT) ) ) return;
393 0 : connect_result = fd_bundle_client_get_connect_result( ctx );
394 0 : if( FD_UNLIKELY( connect_result!=0 ) ) {
395 0 : goto connect_failed;
396 0 : }
397 0 : FD_LOG_DEBUG(( "Bundle TCP socket connected" ));
398 0 : ctx->tcp_sock_connected = 1;
399 0 : fd_bundle_client_epoll_out( ctx, 0 );
400 0 : *charge_busy = 1;
401 0 : }
402 :
403 : /* gRPC conn died? */
404 0 : if( FD_UNLIKELY( !ctx->grpc_client ) ) {
405 0 : long sleep_start;
406 0 : reconnect:
407 0 : sleep_start = fd_bundle_now( ctx );
408 0 : if( FD_UNLIKELY( fd_bundle_tile_should_stall( ctx, sleep_start ) ) ) {
409 0 : long wait_dur = ctx->backoff_until - sleep_start;
410 0 : fd_log_sleep( fd_long_min( wait_dur, 1e6 ) );
411 0 : return;
412 0 : }
413 0 : fd_bundle_client_create_conn( ctx );
414 0 : *charge_busy = 1;
415 0 : return;
416 0 : }
417 :
418 : /* Did a HTTP/2 PING time out */
419 0 : long check_ts = ctx->cached_ts = fd_bundle_now( ctx );
420 0 : if( FD_UNLIKELY( fd_keepalive_is_timeout( ctx->keepalive, check_ts ) ) ) {
421 0 : FD_LOG_WARNING(( "Bundle gRPC timed out (HTTP/2 PING went unanswered for %.2f seconds)",
422 0 : (double)( check_ts - ctx->keepalive->ts_last_tx )/1e9 ));
423 0 : ctx->keepalive->inflight = 0;
424 0 : ctx->defer_reset = 1;
425 0 : *charge_busy = 1;
426 0 : return;
427 0 : }
428 :
429 : /* Drive I/O, SSL handshake, and any inflight requests */
430 0 : if( FD_UNLIKELY( -1==fd_bundle_client_drive_io( ctx, check_ts, charge_busy ) || ctx->defer_reset /* new error? */ ) ) {
431 0 : fd_bundle_client_reset( ctx );
432 0 : ctx->metrics.transport_fail_cnt++;
433 0 : *charge_busy = 1;
434 0 : return;
435 0 : }
436 :
437 : /* Are we ready to issue a new request? */
438 0 : if( FD_UNLIKELY( fd_grpc_client_request_is_blocked( ctx->grpc_client ) ) ) return;
439 0 : long io_ts = fd_bundle_now( ctx );
440 0 : if( FD_UNLIKELY( fd_bundle_tile_should_stall( ctx, io_ts ) ) ) return;
441 :
442 0 : *charge_busy |= fd_bundle_client_step_reconnect( ctx, io_ts );
443 0 : }
444 :
445 : static void
446 0 : fd_bundle_client_log_status( fd_bundle_tile_t * ctx ) {
447 0 : int status = fd_bundle_client_status( ctx );
448 :
449 0 : int const connected_now = ( status==FD_BUNDLE_STATE_CONNECTED );
450 0 : int const connected_before = ( ctx->bundle_status_logged==FD_BUNDLE_STATE_CONNECTED );
451 :
452 0 : if( FD_UNLIKELY( connected_now!=connected_before ) ) {
453 0 : long ts = fd_log_wallclock();
454 0 : if( FD_LIKELY( ts-(ctx->last_bundle_status_log_nanos) >= (long)1e6 ) ) {
455 0 : if( connected_now ) FD_LOG_INFO(( "Connected to bundle server" ));
456 0 : else FD_LOG_INFO(( "Disconnected from bundle server" ));
457 0 : ctx->last_bundle_status_log_nanos = ts;
458 0 : ctx->bundle_status_logged = (uchar)status;
459 0 : }
460 0 : }
461 0 : }
462 :
463 : void
464 : fd_bundle_client_step( fd_bundle_tile_t * ctx,
465 0 : int * charge_busy ) {
466 : /* Edge-trigger logging with rate limiting */
467 0 : fd_bundle_client_step1( ctx, charge_busy );
468 0 : fd_bundle_client_log_status( ctx );
469 :
470 0 : if( FD_UNLIKELY( ctx->tcp_sock_connected && fd_grpc_client_tx_pending( ctx->grpc_client ) ) ) {
471 0 : int flush_err = ctx->is_ssl ? fd_grpc_client_tls_flush ( ctx->grpc_client, ctx->tcp_sock )
472 0 : : fd_grpc_client_tx_flush_socket( ctx->grpc_client, ctx->tcp_sock );
473 0 : if( FD_UNLIKELY( -1==flush_err ) ) {
474 0 : fd_bundle_client_reset( ctx );
475 0 : ctx->metrics.transport_fail_cnt++;
476 0 : *charge_busy = 1;
477 0 : return;
478 0 : }
479 0 : }
480 :
481 : /* Arm EPOLLOUT only on write demand: unsent HTTP/2 bytes, or a TLS
482 : handshake blocked on write (a permanently armed idle-writable
483 : socket would keep the epoll set ready forever). */
484 0 : if( FD_LIKELY( ctx->tcp_sock_connected && ctx->grpc_client ) ) {
485 0 : int want = fd_grpc_client_tx_pending( ctx->grpc_client );
486 0 : want |= ctx->is_ssl && fd_grpc_client_tls_tx_pending( ctx->grpc_client );
487 0 : fd_bundle_client_epoll_out( ctx, want );
488 0 : }
489 0 : }
490 :
491 : void
492 : fd_bundle_tile_backoff( fd_bundle_tile_t * ctx,
493 0 : long now ) {
494 0 : uint iter = ctx->backoff_iter;
495 0 : if( now < ctx->backoff_reset ) iter = 0U;
496 0 : iter++;
497 :
498 : /* FIXME proper backoff */
499 0 : long wait_ns = (long)2e9;
500 0 : wait_ns = (long)( fd_rng_ulong( ctx->rng ) & ( (1UL<<fd_ulong_find_msb_w_default( (ulong)wait_ns, 0 ))-1UL ) );
501 :
502 0 : ctx->backoff_until = now + wait_ns;
503 0 : ctx->backoff_reset = now + 2*wait_ns;
504 :
505 0 : ctx->backoff_iter = iter;
506 0 : }
507 :
508 : static void
509 0 : fd_bundle_client_grpc_conn_established( void * app_ctx ) {
510 0 : (void)app_ctx;
511 0 : FD_LOG_INFO(( "Bundle gRPC connection established" ));
512 0 : }
513 :
514 : static void
515 : fd_bundle_client_grpc_conn_dead( void * app_ctx,
516 : uint h2_err,
517 0 : int closed_by ) {
518 0 : fd_bundle_tile_t * ctx = app_ctx;
519 0 : FD_LOG_INFO(( "Bundle gRPC connection closed %s (%u-%s)",
520 0 : closed_by ? "by peer" : "due to error",
521 0 : h2_err, fd_h2_strerror( h2_err ) ));
522 0 : ctx->defer_reset = 1;
523 0 : }
524 :
525 : /* Publishes a transaction to the verify tiles. Assumes the caller
526 : checked that a credit is available. bundle_txn_cnt is zero for a
527 : standalone packet. */
528 :
529 : static void
530 : fd_bundle_tile_publish_txn( fd_bundle_tile_t * ctx,
531 : void const * txn,
532 : ulong txn_sz, /* <=FD_TXN_MTU */
533 : uint source_ipv4,
534 0 : ulong bundle_txn_cnt ) {
535 0 : int is_bundle = bundle_txn_cnt>0UL;
536 0 : fd_txn_m_t * txnm = fd_chunk_to_laddr( ctx->verify_out.mem, ctx->verify_out.chunk );
537 0 : *txnm = (fd_txn_m_t) {
538 0 : .reference_block_height = 0UL,
539 0 : .payload_sz = (ushort)txn_sz,
540 0 : .txn_t_sz = 0U,
541 0 : .source_ipv4 = source_ipv4,
542 0 : .source_tpu = FD_TXN_M_TPU_SOURCE_BUNDLE,
543 0 : .first_seen_nanos = fd_bundle_now( ctx ),
544 0 : .block_engine = {
545 0 : .bundle_id = is_bundle ? ctx->bundle_seq : 0UL,
546 0 : .bundle_txn_cnt = is_bundle ? bundle_txn_cnt : 1UL,
547 0 : .commission = is_bundle ? (uchar)ctx->builder_commission : (uchar)0,
548 0 : },
549 0 : };
550 0 : if( is_bundle ) fd_memcpy( txnm->block_engine.commission_pubkey, ctx->builder_pubkey, 32UL );
551 0 : else fd_memset( txnm->block_engine.commission_pubkey, 0, 32UL );
552 0 : fd_memcpy( fd_txn_m_payload( txnm ), txn, txn_sz );
553 :
554 0 : ulong sz = fd_txn_m_realized_footprint( txnm, 0, 0 );
555 0 : ulong tspub = (ulong)fd_frag_meta_ts_comp( fd_bundle_now( ctx ) );
556 0 : fd_stem_publish( ctx->stem, ctx->verify_out.idx, (ulong)is_bundle, ctx->verify_out.chunk, sz, 0UL, 0UL, tspub );
557 0 : ctx->verify_out.chunk = fd_dcache_compact_next( ctx->verify_out.chunk, sz, ctx->verify_out.chunk0, ctx->verify_out.wmark );
558 0 : ctx->metrics.txn_received_cnt++;
559 0 : }
560 :
561 : static inline ulong
562 0 : fd_bundle_tile_cr_avail( fd_bundle_tile_t const * ctx ) {
563 0 : return ctx->stem->cr_avail[ ctx->verify_out.idx ];
564 0 : }
565 :
566 : /* fd_bundle_packet_t holds the fields of a packet.Packet message used
567 : by the bundle client. */
568 :
569 : struct fd_bundle_packet {
570 : uchar const * data;
571 : ulong data_sz;
572 : int has_meta;
573 : char addr[ 256 ]; /* packet.Meta.addr, NUL terminated */
574 : };
575 :
576 : typedef struct fd_bundle_packet fd_bundle_packet_t;
577 :
578 : /* fd_bundle_client_decode_packet decodes a packet.Packet message.
579 : Returns packet on success and NULL on decode failure. */
580 :
581 : static fd_bundle_packet_t *
582 : fd_bundle_client_decode_packet( fd_pb_inbuf_t in,
583 0 : fd_bundle_packet_t * packet ) {
584 0 : packet->data = NULL;
585 0 : packet->data_sz = 0UL;
586 0 : packet->has_meta = 0;
587 0 : packet->addr[0] = '\0';
588 0 : while( fd_pb_inbuf_sz( &in ) ) {
589 0 : fd_pb_tlv_t tlv[1];
590 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( &in, tlv ) ) ) return NULL;
591 0 : switch( tlv->field_id ) {
592 0 : case 1: /* data */
593 0 : packet->data = fd_pb_tlv_bytes( &in, tlv );
594 0 : if( FD_UNLIKELY( !packet->data ) ) return NULL;
595 0 : packet->data_sz = tlv->len;
596 0 : break;
597 0 : case 2: { /* meta */
598 0 : fd_pb_inbuf_t meta[1];
599 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, meta ) ) ) return NULL;
600 0 : packet->has_meta = 1;
601 0 : while( fd_pb_inbuf_sz( meta ) ) {
602 0 : fd_pb_tlv_t meta_tlv[1];
603 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( meta, meta_tlv ) ) ) return NULL;
604 0 : if( meta_tlv->field_id==2U ) { /* addr */
605 0 : if( FD_UNLIKELY( !fd_pb_tlv_cstr( meta, meta_tlv, packet->addr, sizeof(packet->addr) ) ) ) return NULL;
606 0 : }
607 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( meta, meta_tlv ) ) ) return NULL;
608 0 : }
609 0 : break;
610 0 : }
611 0 : default:
612 0 : break;
613 0 : }
614 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( &in, tlv ) ) ) return NULL;
615 0 : }
616 0 : return packet;
617 0 : }
618 :
619 : /* fd_bundle_client_decode_header_ts decodes the timestamp of a
620 : shared.Header message. Returns the timestamp in nanoseconds since
621 : the Unix epoch on success (0 if absent). Returns -1 on decode
622 : failure. */
623 :
624 : static long
625 0 : fd_bundle_client_decode_header_ts( fd_pb_inbuf_t in ) {
626 0 : ulong seconds = 0UL;
627 0 : ulong nanos = 0UL;
628 0 : while( fd_pb_inbuf_sz( &in ) ) {
629 0 : fd_pb_tlv_t tlv[1];
630 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( &in, tlv ) ) ) return -1L;
631 0 : if( tlv->field_id==1U ) { /* ts */
632 0 : fd_pb_inbuf_t ts[1];
633 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, ts ) ) ) return -1L;
634 0 : while( fd_pb_inbuf_sz( ts ) ) {
635 0 : fd_pb_tlv_t ts_tlv[1];
636 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( ts, ts_tlv ) ) ) return -1L;
637 0 : switch( ts_tlv->field_id ) {
638 0 : case 1: /* seconds */
639 0 : if( FD_UNLIKELY( ts_tlv->wire_type!=FD_PB_WIRE_TYPE_VARINT ) ) return -1L;
640 0 : seconds = ts_tlv->varint;
641 0 : break;
642 0 : case 2: /* nanos */
643 0 : if( FD_UNLIKELY( ts_tlv->wire_type!=FD_PB_WIRE_TYPE_VARINT ) ) return -1L;
644 0 : nanos = (ulong)(uint)ts_tlv->varint;
645 0 : break;
646 0 : default:
647 0 : break;
648 0 : }
649 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( ts, ts_tlv ) ) ) return -1L;
650 0 : }
651 0 : }
652 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( &in, tlv ) ) ) return -1L;
653 0 : }
654 0 : return (long)( seconds*(ulong)1e9 + nanos );
655 0 : }
656 :
657 : static void
658 : fd_bundle_client_sample_rx_delay( fd_bundle_tile_t * ctx,
659 0 : long ts ) {
660 0 : fd_histf_sample( ctx->metrics.msg_rx_delay, fd_ulong_sat_sub( (ulong)ctx->cached_ts, (ulong)ts ) );
661 0 : }
662 :
663 : static uint
664 : fd_bundle_packet_ip4( fd_bundle_packet_t const * packet,
665 0 : uint dflt ) {
666 0 : uint ip4;
667 0 : if( packet->has_meta && fd_cstr_to_ip4_addr( packet->addr, &ip4 ) ) return ip4;
668 0 : return dflt;
669 0 : }
670 :
671 : /* Handles a bundle.Bundle message. Returns 1 on success and 0 on
672 : decode failure. */
673 :
674 : static int
675 : fd_bundle_client_handle_bundle( fd_bundle_tile_t * ctx,
676 0 : fd_pb_inbuf_t in ) {
677 0 : fd_bundle_packet_t packets[ FD_BUNDLE_CLIENT_MAX_TXN_PER_BUNDLE ];
678 0 : ulong cnt = 0UL;
679 0 : long ts = 0L;
680 :
681 0 : while( fd_pb_inbuf_sz( &in ) ) {
682 0 : fd_pb_tlv_t tlv[1];
683 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( &in, tlv ) ) ) return 0;
684 0 : switch( tlv->field_id ) {
685 0 : case 2: { /* header */
686 0 : fd_pb_inbuf_t header[1];
687 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, header ) ) ) return 0;
688 0 : ts = fd_bundle_client_decode_header_ts( *header );
689 0 : if( FD_UNLIKELY( ts<0L ) ) return 0;
690 0 : break;
691 0 : }
692 0 : case 3: { /* packets */
693 0 : fd_pb_inbuf_t sub[1];
694 0 : fd_bundle_packet_t packet[1];
695 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, sub ) ) ) return 0;
696 0 : if( FD_UNLIKELY( !fd_bundle_client_decode_packet( *sub, packet ) ) ) return 0;
697 0 : if( FD_UNLIKELY( packet->data_sz==0UL || packet->data_sz>FD_TXN_MTU ) ) {
698 0 : FD_LOG_WARNING(( "Bundle server delivered a %lu byte packet, dropping bundle", packet->data_sz ));
699 0 : return 1;
700 0 : }
701 0 : if( FD_UNLIKELY( cnt>=FD_BUNDLE_CLIENT_MAX_TXN_PER_BUNDLE ) ) return 1;
702 0 : packets[ cnt++ ] = *packet;
703 0 : break;
704 0 : }
705 0 : default:
706 0 : break;
707 0 : }
708 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( &in, tlv ) ) ) return 0;
709 0 : }
710 :
711 0 : if( FD_UNLIKELY( fd_bundle_tile_cr_avail( ctx )<cnt ) ) {
712 0 : ctx->metrics.backpressure_drop_cnt += cnt;
713 0 : return 1;
714 0 : }
715 :
716 0 : ctx->bundle_seq++;
717 0 : ctx->metrics.bundle_received_cnt++;
718 0 : for( ulong i=0UL; i<cnt; i++ ) {
719 0 : uint ip4 = fd_bundle_packet_ip4( &packets[ i ], ctx->server_ip4_addr );
720 0 : fd_bundle_tile_publish_txn( ctx, packets[ i ].data, packets[ i ].data_sz, ip4, cnt );
721 0 : }
722 0 : fd_bundle_client_sample_rx_delay( ctx, ts );
723 0 : return 1;
724 0 : }
725 :
726 : /* Handle a SubscribeBundlesResponse from a SubscribeBundles gRPC call. */
727 :
728 : static void
729 : fd_bundle_client_handle_bundle_batch(
730 : fd_bundle_tile_t * ctx,
731 : fd_pb_inbuf_t in
732 0 : ) {
733 0 : if( FD_UNLIKELY( !ctx->builder_info_avail ) ) {
734 0 : ctx->metrics.missing_builder_info_fail_cnt++; /* unreachable */
735 0 : return;
736 0 : }
737 :
738 0 : while( fd_pb_inbuf_sz( &in ) ) {
739 0 : fd_pb_tlv_t tlv[1];
740 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( &in, tlv ) ) ) goto fail;
741 0 : if( tlv->field_id==1U ) { /* bundles (BundleUuid) */
742 0 : fd_pb_inbuf_t uuid[1];
743 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, uuid ) ) ) goto fail;
744 0 : fd_pb_inbuf_t bundle[1] = {{0}};
745 0 : while( fd_pb_inbuf_sz( uuid ) ) {
746 0 : fd_pb_tlv_t uuid_tlv[1];
747 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( uuid, uuid_tlv ) ) ) goto fail;
748 0 : if( uuid_tlv->field_id==1U ) { /* bundle */
749 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( uuid, uuid_tlv, bundle ) ) ) goto fail;
750 0 : }
751 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( uuid, uuid_tlv ) ) ) goto fail;
752 0 : }
753 0 : if( FD_UNLIKELY( !fd_bundle_client_handle_bundle( ctx, *bundle ) ) ) goto fail;
754 0 : }
755 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( &in, tlv ) ) ) goto fail;
756 0 : }
757 0 : return;
758 :
759 0 : fail:
760 0 : ctx->metrics.decode_fail_cnt++;
761 0 : FD_LOG_WARNING(( "Protobuf decode of (block_engine.SubscribeBundlesResponse) failed" ));
762 0 : }
763 :
764 : /* Called for each 'Packet' (a regular transaction) of a
765 : SubscribePacketsResponse. */
766 :
767 : static void
768 : fd_bundle_client_handle_packet( fd_bundle_tile_t * ctx,
769 0 : fd_bundle_packet_t const * packet ) {
770 0 : if( FD_UNLIKELY( packet->data_sz==0UL || packet->data_sz>FD_TXN_MTU ) ) {
771 0 : FD_LOG_WARNING(( "Bundle server delivered a %lu byte packet, ignoring", packet->data_sz ));
772 0 : return;
773 0 : }
774 0 : if( FD_UNLIKELY( !fd_bundle_tile_cr_avail( ctx ) ) ) {
775 0 : ctx->metrics.backpressure_drop_cnt++;
776 0 : return;
777 0 : }
778 0 : fd_bundle_tile_publish_txn( ctx, packet->data, packet->data_sz, fd_bundle_packet_ip4( packet, 0U ), 0UL );
779 0 : ctx->metrics.packet_received_cnt++;
780 0 : }
781 :
782 : /* Handle a SubscribePacketsResponse from a SubscribePackets gRPC call. */
783 :
784 : static void
785 : fd_bundle_client_handle_packet_batch(
786 : fd_bundle_tile_t * ctx,
787 : fd_pb_inbuf_t in
788 0 : ) {
789 0 : long ts = 0L;
790 0 : while( fd_pb_inbuf_sz( &in ) ) {
791 0 : fd_pb_tlv_t tlv[1];
792 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( &in, tlv ) ) ) goto fail;
793 0 : switch( tlv->field_id ) {
794 0 : case 1: { /* header */
795 0 : fd_pb_inbuf_t header[1];
796 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, header ) ) ) goto fail;
797 0 : ts = fd_bundle_client_decode_header_ts( *header );
798 0 : if( FD_UNLIKELY( ts<0L ) ) goto fail;
799 0 : break;
800 0 : }
801 0 : case 2: { /* batch (PacketBatch) */
802 0 : fd_pb_inbuf_t batch[1];
803 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( &in, tlv, batch ) ) ) goto fail;
804 0 : while( fd_pb_inbuf_sz( batch ) ) {
805 0 : fd_pb_tlv_t batch_tlv[1];
806 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( batch, batch_tlv ) ) ) goto fail;
807 0 : if( batch_tlv->field_id==1U ) { /* packets */
808 0 : fd_pb_inbuf_t sub[1];
809 0 : fd_bundle_packet_t packet[1];
810 0 : if( FD_UNLIKELY( !fd_pb_tlv_submsg( batch, batch_tlv, sub ) ) ) goto fail;
811 0 : if( FD_UNLIKELY( !fd_bundle_client_decode_packet( *sub, packet ) ) ) goto fail;
812 0 : fd_bundle_client_handle_packet( ctx, packet );
813 0 : }
814 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( batch, batch_tlv ) ) ) goto fail;
815 0 : }
816 0 : break;
817 0 : }
818 0 : default:
819 0 : break;
820 0 : }
821 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( &in, tlv ) ) ) goto fail;
822 0 : }
823 :
824 0 : fd_bundle_client_sample_rx_delay( ctx, ts );
825 0 : return;
826 :
827 0 : fail:
828 0 : ctx->metrics.decode_fail_cnt++;
829 0 : FD_LOG_WARNING(( "Protobuf decode of (block_engine.SubscribePacketsResponse) failed" ));
830 0 : }
831 :
832 : /* Handle a BlockBuilderFeeInfoResponse from a GetBlockBuilderFeeInfo
833 : gRPC call. */
834 :
835 : static void
836 : fd_bundle_client_handle_builder_fee_info(
837 : fd_bundle_tile_t * ctx,
838 : fd_pb_inbuf_t in
839 0 : ) {
840 0 : char pubkey[ FD_BASE58_ENCODED_32_SZ ] = {0};
841 0 : ulong commission = 0UL;
842 0 : while( fd_pb_inbuf_sz( &in ) ) {
843 0 : fd_pb_tlv_t tlv[1];
844 0 : if( FD_UNLIKELY( !fd_pb_tlv_read( &in, tlv ) ) ) goto decode_fail;
845 0 : switch( tlv->field_id ) {
846 0 : case 1: /* pubkey */
847 0 : if( FD_UNLIKELY( !fd_pb_tlv_cstr( &in, tlv, pubkey, sizeof(pubkey) ) ) ) goto decode_fail;
848 0 : break;
849 0 : case 2: /* commission */
850 0 : if( FD_UNLIKELY( tlv->wire_type!=FD_PB_WIRE_TYPE_VARINT ) ) goto decode_fail;
851 0 : commission = tlv->varint;
852 0 : break;
853 0 : default:
854 0 : break;
855 0 : }
856 0 : if( FD_UNLIKELY( !fd_pb_tlv_skip( &in, tlv ) ) ) goto decode_fail;
857 0 : }
858 0 : if( FD_UNLIKELY( commission > 100 ) ) {
859 0 : ctx->metrics.decode_fail_cnt++;
860 0 : FD_LOG_WARNING(( "BlockBuilderFeeInfoResponse commission out of range (0-100): %lu", commission ));
861 0 : return;
862 0 : }
863 :
864 0 : fd_pubkey_t decoded_builder_pubkey;
865 0 : if( FD_UNLIKELY( !fd_base58_decode_32( pubkey, decoded_builder_pubkey.uc ) ||
866 0 : fd_pubkey_check_zero( &decoded_builder_pubkey ) ) ) {
867 0 : FD_LOG_HEXDUMP_WARNING(( "Invalid pubkey in BlockBuilderFeeInfoResponse", pubkey, strnlen( pubkey, sizeof(pubkey) ) ));
868 0 : return;
869 0 : }
870 :
871 0 : ctx->builder_commission = (uchar)commission; /* Apply update atomically */
872 0 : fd_memcpy( ctx->builder_pubkey, decoded_builder_pubkey.uc, sizeof(ctx->builder_pubkey) );
873 :
874 0 : long validity_duration_ns = (long)( 60e9 * 5. ); /* 5 minutes */
875 0 : ctx->builder_info_avail = 1;
876 0 : ctx->builder_info_valid_until = fd_bundle_now( ctx ) + validity_duration_ns;
877 0 : return;
878 :
879 0 : decode_fail:
880 0 : ctx->metrics.decode_fail_cnt++;
881 0 : FD_LOG_WARNING(( "Protobuf decode of (block_engine.BlockBuilderFeeInfoResponse) failed" ));
882 0 : }
883 :
884 : static void
885 : fd_bundle_client_grpc_tx_complete(
886 : void * app_ctx,
887 : ulong request_ctx
888 0 : ) {
889 0 : (void)app_ctx; (void)request_ctx;
890 0 : }
891 :
892 : void
893 : fd_bundle_client_grpc_rx_start(
894 : void * app_ctx,
895 : ulong request_ctx
896 0 : ) {
897 0 : fd_bundle_tile_t * ctx = app_ctx;
898 0 : switch( request_ctx ) {
899 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets:
900 0 : ctx->packet_subscription_live = 1;
901 0 : ctx->packet_subscription_wait = 0;
902 0 : break;
903 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles:
904 0 : ctx->bundle_subscription_live = 1;
905 0 : ctx->bundle_subscription_wait = 0;
906 0 : break;
907 0 : }
908 0 : }
909 :
910 : void
911 : fd_bundle_client_grpc_rx_msg(
912 : void * app_ctx,
913 : void const * protobuf,
914 : ulong protobuf_sz,
915 : ulong request_ctx
916 0 : ) {
917 0 : fd_bundle_tile_t * ctx = app_ctx;
918 0 : ctx->metrics.proto_received_bytes += protobuf_sz;
919 0 : fd_pb_inbuf_t in[1]; fd_pb_inbuf_init( in, protobuf, protobuf_sz );
920 0 : switch( request_ctx ) {
921 0 : case FD_BUNDLE_CLIENT_REQ_Auth_GenerateAuthChallenge:
922 0 : if( FD_UNLIKELY( !fd_bundle_auther_handle_challenge_resp( &ctx->auther, protobuf, protobuf_sz ) ) ) {
923 0 : ctx->metrics.decode_fail_cnt++;
924 0 : fd_bundle_tile_backoff( ctx, fd_bundle_now( ctx ) );
925 0 : }
926 0 : break;
927 0 : case FD_BUNDLE_CLIENT_REQ_Auth_GenerateAuthTokens:
928 0 : if( FD_UNLIKELY( !fd_bundle_auther_handle_tokens_resp( &ctx->auther, protobuf, protobuf_sz ) ) ) {
929 0 : ctx->metrics.decode_fail_cnt++;
930 0 : fd_bundle_tile_backoff( ctx, fd_bundle_now( ctx ) );
931 0 : }
932 0 : break;
933 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles:
934 0 : fd_bundle_client_handle_bundle_batch( ctx, *in );
935 0 : break;
936 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets:
937 0 : fd_bundle_client_handle_packet_batch( ctx, *in );
938 0 : break;
939 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_GetBlockBuilderFeeInfo:
940 0 : fd_bundle_client_handle_builder_fee_info( ctx, *in );
941 0 : break;
942 0 : default:
943 0 : FD_LOG_ERR(( "Received unexpected gRPC message (request_ctx=%lu)", request_ctx ));
944 0 : }
945 0 : }
946 :
947 : static void
948 : fd_bundle_client_request_failed( fd_bundle_tile_t * ctx,
949 0 : ulong request_ctx ) {
950 0 : fd_bundle_tile_backoff( ctx, fd_bundle_now( ctx ) );
951 0 : switch( request_ctx ) {
952 0 : case FD_BUNDLE_CLIENT_REQ_Auth_GenerateAuthChallenge:
953 0 : case FD_BUNDLE_CLIENT_REQ_Auth_GenerateAuthTokens:
954 0 : fd_bundle_auther_handle_request_fail( &ctx->auther );
955 0 : break;
956 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_GetBlockBuilderFeeInfo:
957 0 : ctx->builder_info_wait = 0;
958 0 : break;
959 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets:
960 0 : ctx->packet_subscription_live = 0;
961 0 : ctx->packet_subscription_wait = 0;
962 0 : break;
963 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles:
964 0 : ctx->bundle_subscription_live = 0;
965 0 : ctx->bundle_subscription_wait = 0;
966 0 : break;
967 0 : }
968 0 : }
969 :
970 : void
971 : fd_bundle_client_grpc_rx_end(
972 : void * app_ctx,
973 : ulong request_ctx,
974 : fd_grpc_resp_hdrs_t * resp
975 0 : ) {
976 0 : fd_bundle_tile_t * ctx = app_ctx;
977 0 : if( FD_UNLIKELY( resp->h2_status!=200 ) ) {
978 0 : FD_LOG_WARNING(( "gRPC request failed (HTTP status %u)", resp->h2_status ));
979 0 : fd_bundle_client_request_failed( ctx, request_ctx );
980 0 : fd_bundle_auther_reset( &ctx->auther );
981 0 : ctx->defer_reset = 1;
982 0 : return;
983 0 : }
984 :
985 0 : resp->grpc_msg_len = (uint)fd_url_unescape( resp->grpc_msg, resp->grpc_msg_len );
986 0 : if( !resp->grpc_msg_len ) {
987 0 : fd_memcpy( resp->grpc_msg, "unknown error", 13 );
988 0 : resp->grpc_msg_len = 13;
989 0 : }
990 :
991 0 : switch( request_ctx ) {
992 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets:
993 0 : ctx->packet_subscription_live = 0;
994 0 : ctx->packet_subscription_wait = 0;
995 0 : fd_bundle_tile_backoff( ctx, fd_bundle_now( ctx ) );
996 0 : ctx->defer_reset = 1;
997 0 : FD_LOG_INFO(( "SubscribePackets stream failed (gRPC status %u-%s). Reconnecting ...",
998 0 : resp->grpc_status, fd_grpc_status_cstr( resp->grpc_status ) ));
999 0 : return;
1000 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles:
1001 0 : ctx->bundle_subscription_live = 0;
1002 0 : ctx->bundle_subscription_wait = 0;
1003 0 : fd_bundle_tile_backoff( ctx, fd_bundle_now( ctx ) );
1004 0 : ctx->defer_reset = 1;
1005 0 : FD_LOG_INFO(( "SubscribeBundles stream failed (gRPC status %u-%s). Reconnecting ...",
1006 0 : resp->grpc_status, fd_grpc_status_cstr( resp->grpc_status ) ));
1007 0 : return;
1008 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_GetBlockBuilderFeeInfo:
1009 0 : ctx->builder_info_wait = 0;
1010 0 : break;
1011 0 : default:
1012 0 : break;
1013 0 : }
1014 :
1015 0 : if( FD_UNLIKELY( resp->grpc_status!=FD_GRPC_STATUS_OK ) ) {
1016 0 : FD_LOG_INFO(( "gRPC request failed (gRPC status %u-%s): %.*s",
1017 0 : resp->grpc_status, fd_grpc_status_cstr( resp->grpc_status ),
1018 0 : (int)resp->grpc_msg_len, resp->grpc_msg ));
1019 0 : fd_bundle_client_request_failed( ctx, request_ctx );
1020 0 : if( resp->grpc_status==FD_GRPC_STATUS_UNAUTHENTICATED ||
1021 0 : resp->grpc_status==FD_GRPC_STATUS_PERMISSION_DENIED ) {
1022 0 : fd_bundle_auther_reset( &ctx->auther );
1023 0 : }
1024 0 : return;
1025 0 : }
1026 0 : }
1027 :
1028 : void
1029 : fd_bundle_client_grpc_rx_timeout(
1030 : void * app_ctx,
1031 : ulong request_ctx, /* FD_BUNDLE_CLIENT_REQ_{...} */
1032 : int deadline_kind /* FD_GRPC_DEADLINE_{HEADER|RX_END} */
1033 0 : ) {
1034 0 : (void)deadline_kind;
1035 0 : FD_LOG_WARNING(( "Request timed out: %s", fd_bundle_request_ctx_cstr( request_ctx ) ));
1036 0 : fd_bundle_tile_t * ctx = app_ctx;
1037 0 : ctx->defer_reset = 1;
1038 0 : }
1039 :
1040 : static void
1041 0 : fd_bundle_client_grpc_ping_ack( void * app_ctx ) {
1042 0 : fd_bundle_tile_t * ctx = app_ctx;
1043 0 : long rtt_sample = fd_keepalive_rx( ctx->keepalive, fd_bundle_now( ctx ) );
1044 0 : if( FD_LIKELY( rtt_sample ) ) {
1045 0 : fd_rtt_sample( ctx->rtt, (float)rtt_sample, 0 );
1046 0 : FD_LOG_DEBUG(( "Keepalive ACK" ));
1047 0 : }
1048 0 : ctx->metrics.ping_ack_cnt++;
1049 0 : }
1050 :
1051 : fd_grpc_client_callbacks_t fd_bundle_client_grpc_callbacks = {
1052 : .conn_established = fd_bundle_client_grpc_conn_established,
1053 : .conn_dead = fd_bundle_client_grpc_conn_dead,
1054 : .tx_complete = fd_bundle_client_grpc_tx_complete,
1055 : .rx_start = fd_bundle_client_grpc_rx_start,
1056 : .rx_msg = fd_bundle_client_grpc_rx_msg,
1057 : .rx_end = fd_bundle_client_grpc_rx_end,
1058 : .rx_timeout = fd_bundle_client_grpc_rx_timeout,
1059 : .ping_ack = fd_bundle_client_grpc_ping_ack,
1060 : };
1061 :
1062 : int
1063 0 : fd_bundle_client_status( fd_bundle_tile_t const * ctx ) {
1064 0 : if( FD_UNLIKELY( ( !ctx->tcp_sock_connected ) |
1065 0 : ( !ctx->grpc_client ) ) ) {
1066 0 : return FD_BUNDLE_STATE_DISCONNECTED;
1067 0 : }
1068 :
1069 0 : fd_h2_conn_t * conn = fd_grpc_client_h2_conn( ctx->grpc_client );
1070 0 : if( FD_UNLIKELY( !conn ) ) {
1071 0 : return FD_BUNDLE_STATE_DISCONNECTED; /* no conn */
1072 0 : }
1073 0 : if( FD_UNLIKELY( conn->flags &
1074 0 : ( FD_H2_CONN_FLAGS_DEAD |
1075 0 : FD_H2_CONN_FLAGS_SEND_GOAWAY ) ) ) {
1076 0 : return FD_BUNDLE_STATE_DISCONNECTED;
1077 0 : }
1078 :
1079 0 : if( FD_UNLIKELY( conn->flags &
1080 0 : ( FD_H2_CONN_FLAGS_CLIENT_INITIAL |
1081 0 : FD_H2_CONN_FLAGS_WAIT_SETTINGS_ACK_0 |
1082 0 : FD_H2_CONN_FLAGS_WAIT_SETTINGS_0 |
1083 0 : FD_H2_CONN_FLAGS_SERVER_INITIAL ) ) ) {
1084 0 : return FD_BUNDLE_STATE_CONNECTING; /* connection is not ready */
1085 0 : }
1086 :
1087 0 : if( FD_UNLIKELY( ctx->auther.state != FD_BUNDLE_AUTH_STATE_DONE_WAIT ) ) {
1088 0 : return FD_BUNDLE_STATE_CONNECTING; /* not authenticated */
1089 0 : }
1090 :
1091 0 : if( FD_UNLIKELY( ( !ctx->builder_info_avail ) |
1092 0 : ( !ctx->packet_subscription_live ) |
1093 0 : ( !ctx->bundle_subscription_live ) ) ) {
1094 0 : return FD_BUNDLE_STATE_CONNECTING; /* not fully connected */
1095 0 : }
1096 :
1097 0 : if( FD_UNLIKELY( fd_keepalive_is_timeout( ctx->keepalive, fd_bundle_now( ctx ) ) ) ) {
1098 0 : return FD_BUNDLE_STATE_DISCONNECTED; /* possible timeout */
1099 0 : }
1100 :
1101 0 : if( FD_UNLIKELY( !fd_grpc_client_is_connected( ctx->grpc_client ) ) ) {
1102 0 : return FD_BUNDLE_STATE_CONNECTING;
1103 0 : }
1104 :
1105 : /* As far as we know, the bundle connection is alive and well. */
1106 0 : return FD_BUNDLE_STATE_CONNECTED;
1107 0 : }
1108 :
1109 : #undef DISCONNECTED
1110 : #undef CONNECTING
1111 : #undef CONNECTED
1112 :
1113 : FD_FN_CONST char const *
1114 0 : fd_bundle_request_ctx_cstr( ulong request_ctx ) {
1115 0 : switch( request_ctx ) {
1116 0 : case FD_BUNDLE_CLIENT_REQ_Auth_GenerateAuthChallenge:
1117 0 : return "GenerateAuthChallenge";
1118 0 : case FD_BUNDLE_CLIENT_REQ_Auth_GenerateAuthTokens:
1119 0 : return "GenerateAuthTokens";
1120 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribePackets:
1121 0 : return "SubscribePackets";
1122 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_SubscribeBundles:
1123 0 : return "SubscribeBundles";
1124 0 : case FD_BUNDLE_CLIENT_REQ_Bundle_GetBlockBuilderFeeInfo:
1125 0 : return "GetBlockBuilderFeeInfo";
1126 0 : default:
1127 0 : return "unknown";
1128 0 : }
1129 0 : }
|