Line data Source code
1 : #include "../../rpc_client/fd_rpc_client.h"
2 : #include "../../rpc_client/fd_rpc_client_private.h"
3 : #include "../../../../disco/topo/fd_topo.h"
4 : #include "../../../../ballet/json/cJSON_alloc.h"
5 : #include "../../../../util/net/fd_ip4.h"
6 :
7 : #define FD_BENCHO_STATE_INIT 0UL
8 0 : #define FD_BENCHO_STATE_WAIT 1UL
9 0 : #define FD_BENCHO_STATE_READY 2UL
10 0 : #define FD_BENCHO_STATE_SENT 3UL
11 :
12 0 : #define FD_BENCHO_RPC_INITIALIZE_TIMEOUT (30L * 1000L * 1000L * 1000L)
13 0 : #define FD_BENCHO_RPC_RESPONSE_TIMEOUT (5L * 1000L * 1000L * 1000L)
14 :
15 : typedef struct {
16 : long rpc_ready_deadline;
17 :
18 : long blockhash_request;
19 : ulong blockhash_state;
20 : long blockhash_deadline;
21 :
22 : fd_rpc_client_t rpc[ 1 ];
23 :
24 : fd_wksp_t * mem;
25 : ulong out_chunk0;
26 : ulong out_wmark;
27 : ulong out_chunk;
28 : } fd_bencho_ctx_t;
29 :
30 : FD_FN_CONST static inline ulong
31 0 : scratch_align( void ) {
32 0 : ulong a = alignof( fd_bencho_ctx_t );
33 0 : a = fd_ulong_max( a, fd_alloc_align() );
34 0 : return a;
35 0 : }
36 :
37 : FD_FN_PURE static inline ulong
38 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
39 0 : (void)tile;
40 0 : ulong l = FD_LAYOUT_INIT;
41 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_bencho_ctx_t ), sizeof( fd_bencho_ctx_t ) );
42 0 : l = FD_LAYOUT_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
43 0 : return FD_LAYOUT_FINI( l, scratch_align() );
44 0 : }
45 :
46 : FD_FN_PURE static inline ulong
47 0 : loose_footprint( fd_topo_tile_t const * tile FD_PARAM_UNUSED ) {
48 0 : return 256UL * (1UL<<20UL); /* 256MiB of heap space for the cJSON allocator */
49 0 : }
50 :
51 : static int
52 : service_block_hash( fd_bencho_ctx_t * ctx,
53 0 : fd_stem_context_t * stem ) {
54 0 : int did_work = 0;
55 :
56 0 : if( FD_UNLIKELY( ctx->blockhash_state==FD_BENCHO_STATE_WAIT ) ) {
57 0 : if( FD_LIKELY( fd_log_wallclock()>=ctx->blockhash_deadline ) )
58 0 : ctx->blockhash_state = FD_BENCHO_STATE_READY;
59 0 : }
60 :
61 0 : if( FD_UNLIKELY( ctx->blockhash_state==FD_BENCHO_STATE_READY ) ) {
62 0 : ctx->blockhash_request = fd_rpc_client_request_latest_block_hash( ctx->rpc );
63 0 : if( FD_UNLIKELY( ctx->blockhash_request<0L ) ) FD_LOG_ERR(( "failed to send RPC request" ));
64 :
65 0 : ctx->blockhash_state = FD_BENCHO_STATE_SENT;
66 0 : ctx->blockhash_deadline = fd_log_wallclock() + FD_BENCHO_RPC_RESPONSE_TIMEOUT;
67 :
68 0 : did_work = 1;
69 0 : }
70 :
71 0 : if( FD_UNLIKELY( ctx->blockhash_state==FD_BENCHO_STATE_SENT ) ) {
72 0 : fd_rpc_client_response_t * response = fd_rpc_client_status( ctx->rpc, ctx->blockhash_request, 0 );
73 0 : if( FD_UNLIKELY( response->status==FD_RPC_CLIENT_PENDING ) ) {
74 0 : if( FD_UNLIKELY( fd_log_wallclock()>=ctx->blockhash_deadline ) ) {
75 0 : FD_LOG_WARNING(( "timed out waiting for RPC server to respond" ));
76 0 : fd_rpc_client_close( ctx->rpc, ctx->blockhash_request );
77 0 : ctx->blockhash_state = FD_BENCHO_STATE_WAIT;
78 0 : ctx->blockhash_deadline = fd_log_wallclock() + 100L * 1000L * 1000L; /* 100 millis to retry */
79 0 : }
80 0 : return did_work;
81 0 : }
82 :
83 0 : if( FD_UNLIKELY( fd_log_wallclock()<ctx->rpc_ready_deadline &&
84 0 : ( response->status==FD_RPC_CLIENT_ERR_NETWORK ||
85 0 : response->status==FD_RPC_CLIENT_ERR_MALFORMED ) ) ) {
86 : /* RPC server not yet responding, give it some more time... */
87 0 : ctx->blockhash_state = FD_BENCHO_STATE_WAIT;
88 0 : ctx->blockhash_deadline = fd_log_wallclock() + 100L * 1000L * 1000L; /* 100 millis to retry */
89 0 : fd_rpc_client_close( ctx->rpc, ctx->blockhash_request );
90 0 : return did_work;
91 0 : }
92 :
93 0 : if( FD_UNLIKELY( response->status!=FD_RPC_CLIENT_SUCCESS ) )
94 0 : FD_LOG_ERR(( "RPC server returned error %ld-%s", response->status, fd_rpc_client_strerror( response->status ) ));
95 :
96 0 : ctx->blockhash_state = FD_BENCHO_STATE_WAIT;
97 0 : ctx->blockhash_deadline = fd_log_wallclock() + 400L * 1000L * 1000L; /* 400 millis til we fetch new blockhash */
98 0 : fd_memcpy( fd_chunk_to_laddr( ctx->mem, ctx->out_chunk ), response->result.latest_block_hash.block_hash, 32 );
99 0 : fd_stem_publish( stem, 0UL, 0UL, ctx->out_chunk, 32UL, 0UL, 0UL, 0UL );
100 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, 32, ctx->out_chunk0, ctx->out_wmark );
101 :
102 0 : fd_rpc_client_close( ctx->rpc, ctx->blockhash_request );
103 :
104 0 : did_work = 1;
105 0 : }
106 :
107 0 : return did_work;
108 0 : }
109 :
110 : static inline void
111 : after_credit( fd_bencho_ctx_t * ctx,
112 : fd_stem_context_t * stem,
113 : int * opt_poll_in,
114 0 : int * charge_busy ) {
115 0 : (void)opt_poll_in;
116 :
117 0 : int did_work_rpc = fd_rpc_client_service( ctx->rpc, 0 );
118 0 : int did_work_service_block_hash = service_block_hash( ctx, stem );
119 :
120 0 : *charge_busy = did_work_rpc | did_work_service_block_hash;
121 0 : }
122 :
123 : extern FD_TL fd_alloc_t * g_cjson_alloc_ctx;
124 :
125 : static void
126 : unprivileged_init( fd_topo_t * topo,
127 0 : fd_topo_tile_t * tile ) {
128 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
129 :
130 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
131 0 : fd_bencho_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_bencho_ctx_t ), sizeof( fd_bencho_ctx_t ) );
132 0 : void * _alloc = FD_SCRATCH_ALLOC_APPEND( l, fd_alloc_align(), fd_alloc_footprint() );
133 :
134 0 : fd_alloc_t * alloc = fd_alloc_join( fd_alloc_new( _alloc, 1UL ), 1UL );
135 0 : FD_TEST( alloc );
136 0 : cJSON_alloc_install( alloc );
137 :
138 0 : ctx->mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
139 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
140 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
141 0 : ctx->out_chunk = ctx->out_chunk0;
142 :
143 0 : ctx->rpc_ready_deadline = fd_log_wallclock() + FD_BENCHO_RPC_INITIALIZE_TIMEOUT;
144 0 : ctx->blockhash_state = FD_BENCHO_STATE_READY;
145 :
146 0 : FD_LOG_NOTICE(( "connecting to RPC server " FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS( tile->bencho.rpc_ip_addr ), tile->bencho.rpc_port ));
147 0 : FD_TEST( fd_rpc_client_join( fd_rpc_client_new( ctx->rpc, tile->bencho.rpc_ip_addr, tile->bencho.rpc_port ) ) );
148 :
149 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
150 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
151 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
152 0 : }
153 :
154 0 : #define STEM_BURST (1UL)
155 :
156 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_bencho_ctx_t
157 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bencho_ctx_t)
158 :
159 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
160 :
161 : #include "../../../../disco/stem/fd_stem.c"
162 :
163 : fd_topo_run_tile_t fd_tile_bencho = {
164 : .name = "bencho",
165 : .scratch_align = scratch_align,
166 : .scratch_footprint = scratch_footprint,
167 : .loose_footprint = loose_footprint,
168 : .unprivileged_init = unprivileged_init,
169 : .run = stem_run,
170 : };
|