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