tidy rpc callbacks

This commit is contained in:
Jethro Grassie 2019-05-11 01:46:36 -04:00
parent 20154a8133
commit 6dffa4788d
No known key found for this signature in database
GPG key ID: DE8ED755616565BB

View file

@ -202,11 +202,13 @@ typedef struct payment_t
char address[ADDRESS_MAX]; char address[ADDRESS_MAX];
} payment_t; } payment_t;
typedef struct rpc_callback_t typedef struct rpc_callback_t rpc_callback_t;
typedef void (*rpc_callback_fun)(const char*, rpc_callback_t*);
struct rpc_callback_t
{ {
void (*cb)(const char*, struct rpc_callback_t*); rpc_callback_fun f;
void *data; void *data;
} rpc_callback_t; };
static int database_init(); static int database_init();
static void database_close(); static void database_close();
@ -297,6 +299,25 @@ static FILE *fd_log;
} \ } \
} }
static inline rpc_callback_t *
rpc_callback_new(rpc_callback_fun f, void *data)
{
rpc_callback_t *c = calloc(1, sizeof(rpc_callback_t));
c->f = f;
c->data = data;
return c;
}
static inline void
rpc_callback_free(rpc_callback_t *callback)
{
if (!callback)
return;
if (callback->data)
free(callback->data);
free(callback);
}
static int static int
compare_uint64(const MDB_val *a, const MDB_val *b) compare_uint64(const MDB_val *a, const MDB_val *b)
{ {
@ -852,10 +873,9 @@ send_payments()
start = stecpy(start, "]}}", end); start = stecpy(start, "]}}", end);
} }
log_trace(body); log_trace(body);
rpc_callback_t *callback = calloc(1, sizeof(rpc_callback_t)); rpc_callback_t *cb = rpc_callback_new(
callback->data = payments; rpc_on_wallet_transferred, payments);
callback->cb = rpc_on_wallet_transferred; rpc_wallet_request(base, body, cb);
rpc_wallet_request(base, body, callback);
} }
else else
free(payments); free(payments);
@ -917,9 +937,9 @@ startup_pauout(uint64_t height)
char body[RPC_BODY_MAX]; char body[RPC_BODY_MAX];
rpc_get_request_body(body, "get_block_header_by_height", "sd", "height", block->height); rpc_get_request_body(body, "get_block_header_by_height", "sd", "height", block->height);
rpc_callback_t *c = calloc(1, sizeof(rpc_callback_t)); rpc_callback_t *cb = rpc_callback_new(
c->cb = rpc_on_block_header_by_height; rpc_on_block_header_by_height, NULL);
rpc_request(base, body, c); rpc_request(base, body, cb);
} }
mdb_cursor_close(cursor); mdb_cursor_close(cursor);
@ -1222,26 +1242,26 @@ rpc_on_response(struct evhttp_request *req, void *arg)
if (!req) if (!req)
{ {
log_error("Request failure. Aborting."); log_error("Request failure. Aborting.");
goto cleanup; rpc_callback_free(callback);
return;
} }
int rc = evhttp_request_get_response_code(req); int rc = evhttp_request_get_response_code(req);
if (rc < 200 || rc >= 300) if (rc < 200 || rc >= 300)
{ {
log_error("HTTP status code %d for %s. Aborting.", rc, evhttp_request_get_uri(req)); log_error("HTTP status code %d for %s. Aborting.",
goto cleanup; rc, evhttp_request_get_uri(req));
rpc_callback_free(callback);
return;
} }
input = evhttp_request_get_input_buffer(req); input = evhttp_request_get_input_buffer(req);
size_t len = evbuffer_get_length(input); size_t len = evbuffer_get_length(input);
char *data = (char*) alloca(len+1); char body[len+1];
evbuffer_remove(input, data, len); evbuffer_remove(input, body, len);
data[len] = '\0'; body[len] = '\0';
callback->cb(data, callback); callback->f(body, callback);
cleanup: rpc_callback_free(callback);
if (callback->data)
free(callback->data);
free(callback);
} }
static void static void
@ -1475,16 +1495,15 @@ rpc_on_last_block_header(const char* data, rpc_callback_t *callback)
char body[RPC_BODY_MAX]; char body[RPC_BODY_MAX];
uint64_t reserve = 17; uint64_t reserve = 17;
rpc_get_request_body(body, "get_block_template", "sssd", "wallet_address", config.pool_wallet, "reserve_size", reserve); rpc_get_request_body(body, "get_block_template", "sssd", "wallet_address", config.pool_wallet, "reserve_size", reserve);
rpc_callback_t *c1 = calloc(1, sizeof(rpc_callback_t)); rpc_callback_t *cb1 = rpc_callback_new(rpc_on_block_template, NULL);
c1->cb = rpc_on_block_template; rpc_request(base, body, cb1);
rpc_request(base, body, c1);
uint64_t end = block->height - 60; uint64_t end = block->height - 60;
uint64_t start = end - BLOCK_HEADERS_RANGE + 1; uint64_t start = end - BLOCK_HEADERS_RANGE + 1;
rpc_get_request_body(body, "get_block_headers_range", "sdsd", "start_height", start, "end_height", end); rpc_get_request_body(body, "get_block_headers_range", "sdsd", "start_height", start, "end_height", end);
rpc_callback_t *c2 = calloc(1, sizeof(rpc_callback_t)); rpc_callback_t *cb2 = rpc_callback_new(
c2->cb = rpc_on_block_headers_range; rpc_on_block_headers_range, NULL);
rpc_request(base, body, c2); rpc_request(base, body, cb2);
} }
json_object_put(root); json_object_put(root);
@ -1646,9 +1665,8 @@ fetch_last_block_header()
log_info("Fetching last block header"); log_info("Fetching last block header");
char body[RPC_BODY_MAX]; char body[RPC_BODY_MAX];
rpc_get_request_body(body, "get_last_block_header", NULL); rpc_get_request_body(body, "get_last_block_header", NULL);
rpc_callback_t *callback = calloc(1, sizeof(rpc_callback_t)); rpc_callback_t *cb = rpc_callback_new(rpc_on_last_block_header, NULL);
callback->cb = rpc_on_last_block_header; rpc_request(base, body, cb);
rpc_request(base, body, callback);
} }
static void static void
@ -2068,11 +2086,10 @@ client_on_submit(json_object *message, client_t *client)
"{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"submit_block\", \"params\":[\"%s\"]}", "{\"jsonrpc\":\"2.0\",\"id\":\"0\",\"method\":\"submit_block\", \"params\":[\"%s\"]}",
block_hex); block_hex);
rpc_callback_t *callback = calloc(1, sizeof(rpc_callback_t)); rpc_callback_t *cb = rpc_callback_new(rpc_on_block_submitted, NULL);
callback->cb = rpc_on_block_submitted; cb->data = calloc(1, sizeof(block_t));
callback->data = calloc(1, sizeof(block_t));
block_t* b = (block_t*)callback->data; block_t* b = (block_t*)cb->data;
b->height = bt->height; b->height = bt->height;
bin_to_hex(submitted_hash, 32, b->hash, 64); bin_to_hex(submitted_hash, 32, b->hash, 64);
memcpy(b->prev_hash, bt->prev_hash, 64); memcpy(b->prev_hash, bt->prev_hash, 64);
@ -2080,7 +2097,7 @@ client_on_submit(json_object *message, client_t *client)
b->status = BLOCK_LOCKED; b->status = BLOCK_LOCKED;
b->timestamp = now; b->timestamp = now;
rpc_request(base, body, callback); rpc_request(base, body, cb);
free(block_hex); free(block_hex);
} }
else if (BN_cmp(hd, jd) < 0) else if (BN_cmp(hd, jd) < 0)