From 8fcca4afbd2fe48291a0937c00c77d609f1017e1 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Wed, 15 Jan 2025 20:20:38 +0000 Subject: [PATCH] Abstract userpass concatenation to bitcoind_json_rpc_call helper --- src/datum_blocktemplates.c | 10 ++-------- src/datum_jsonrpc.c | 7 +++++++ src/datum_jsonrpc.h | 3 +++ src/datum_stratum.c | 16 +++++++--------- src/datum_submitblock.c | 13 ++----------- 5 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/datum_blocktemplates.c b/src/datum_blocktemplates.c index bac75a7..7e8ba36 100644 --- a/src/datum_blocktemplates.c +++ b/src/datum_blocktemplates.c @@ -334,7 +334,6 @@ T_DATUM_TEMPLATE_DATA *datum_gbt_parser(json_t *gbt) { void *datum_gateway_fallback_notifier(void *args) { CURL *tcurl = NULL; - char userpass[512]; char req[512]; char p1[72]; p1[0] = 0; @@ -348,11 +347,9 @@ void *datum_gateway_fallback_notifier(void *args) { } DLOG_DEBUG("Fallback notifier thread ready."); - sprintf(userpass, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword); - while(1) { sprintf(req, "{\"jsonrpc\":\"1.0\",\"id\":\"%"PRIu64"\",\"method\":\"getbestblockhash\",\"params\":[]}", current_time_millis()); - gbbh = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, userpass, req); + gbbh = bitcoind_json_rpc_call(tcurl, &datum_config, req); if (gbbh) { res_val = json_object_get(gbbh, "result"); if (!res_val) { @@ -386,7 +383,6 @@ void *datum_gateway_template_thread(void *args) { uint64_t i = 0; char gbt_req[1024]; int j; - char userpass[512]; T_DATUM_TEMPLATE_DATA *t; bool was_notified = false; int wnc = 0; @@ -414,14 +410,12 @@ void *datum_gateway_template_thread(void *args) { char p1[72]; p1[0] = 0; - sprintf(userpass, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword); - while(1) { i++; // fetch latest template sprintf(gbt_req, "{\"method\":\"getblocktemplate\",\"params\":[{\"rules\":[\"segwit\"]}],\"id\":%"PRIu64"}",(uint64_t)((uint64_t)time(NULL)<<(uint64_t)8)|(uint64_t)(i&255)); - gbt = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, userpass, gbt_req); + gbt = bitcoind_json_rpc_call(tcurl, &datum_config, gbt_req); if (!gbt) { DLOG_ERROR("Could not fetch new template from %s!", datum_config.bitcoind_rpcurl); diff --git a/src/datum_jsonrpc.c b/src/datum_jsonrpc.c index 9bda530..23b0447 100644 --- a/src/datum_jsonrpc.c +++ b/src/datum_jsonrpc.c @@ -41,6 +41,7 @@ #include #include +#include "datum_conf.h" #include "datum_jsonrpc.h" #include "datum_utils.h" @@ -232,3 +233,9 @@ err_out: json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, const char *rpc_req) { return json_rpc_call_full(curl, url, userpass, rpc_req, NULL); } + +json_t *bitcoind_json_rpc_call(CURL * const curl, global_config_t * const cfg, const char * const rpc_req) { + char userpass[sizeof(cfg->bitcoind_rpcuser) + sizeof(cfg->bitcoind_rpcpassword)]; + sprintf(userpass, "%s:%s", cfg->bitcoind_rpcuser, cfg->bitcoind_rpcpassword); + return json_rpc_call(curl, cfg->bitcoind_rpcurl, userpass, rpc_req); +} diff --git a/src/datum_jsonrpc.h b/src/datum_jsonrpc.h index c564e07..3d84576 100644 --- a/src/datum_jsonrpc.h +++ b/src/datum_jsonrpc.h @@ -39,6 +39,8 @@ #include #include +#include "datum_conf.h" + #ifndef JSON_INTEGER_IS_LONG_LONG # error "Jansson 2.0 with long long support required!" #endif @@ -63,5 +65,6 @@ struct upload_buffer { json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass, const char *rpc_req); char *basic_http_call(CURL *curl, const char *url); +json_t *bitcoind_json_rpc_call(CURL *curl, global_config_t *cfg, const char *rpc_req); #endif diff --git a/src/datum_stratum.c b/src/datum_stratum.c index badbec5..ab157b8 100644 --- a/src/datum_stratum.c +++ b/src/datum_stratum.c @@ -2087,7 +2087,7 @@ int assembleBlockAndSubmit(uint8_t *block_header, uint8_t *coinbase_txn, size_t size_t i; json_t *r; CURL *tcurl; - char userpass[512]; + char submitblockpath[512]; int ret = 0; bool free_submitblock_req = false; char *s = NULL; @@ -2156,14 +2156,14 @@ int assembleBlockAndSubmit(uint8_t *block_header, uint8_t *coinbase_txn, size_t if (datum_config.mining_save_submitblocks_dir[0] != 0) { // save the block submission to a file named by the block's hash FILE *f; - snprintf(userpass, 511, "%s/datum_submitblock_%s.json", datum_config.mining_save_submitblocks_dir, block_hash_hex); - userpass[511] = 0; - f = fopen(userpass, "w"); + snprintf(submitblockpath, 511, "%s/datum_submitblock_%s.json", datum_config.mining_save_submitblocks_dir, block_hash_hex); + submitblockpath[511] = 0; + f = fopen(submitblockpath, "w"); if (!f) { - DLOG_ERROR("Could not open %s for writing submitblock record to disk: %s!", userpass, strerror(errno)); + DLOG_ERROR("Could not open %s for writing submitblock record to disk: %s!", submitblockpath, strerror(errno)); } else { if (!fwrite(submitblock_req, ptr-submitblock_req, 1, f)) { - DLOG_ERROR("Could not write to %s when writing submitblock record to disk: %s!", userpass, strerror(errno)); + DLOG_ERROR("Could not write to %s when writing submitblock record to disk: %s!", submitblockpath, strerror(errno)); } fclose(f); } @@ -2178,10 +2178,8 @@ int assembleBlockAndSubmit(uint8_t *block_header, uint8_t *coinbase_txn, size_t return 0; } - sprintf(userpass, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword); - // make the call! - r = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, userpass, submitblock_req); + r = bitcoind_json_rpc_call(tcurl, &datum_config, submitblock_req); curl_easy_cleanup(tcurl); if (!r) { // oddly, this means success here. diff --git a/src/datum_submitblock.c b/src/datum_submitblock.c index adc8acc..bd4a18a 100644 --- a/src/datum_submitblock.c +++ b/src/datum_submitblock.c @@ -52,14 +52,9 @@ char submitblock_hash[256] = { 0 }; void preciousblock(CURL *curl, char *blockhash) { json_t *json; char rpc_data[384]; - char userpass[1024]; - - // TODO: Move these types of things to the conf file - snprintf(userpass, 1023, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword); - userpass[1023] = 0; sprintf(rpc_data,"{\"method\":\"preciousblock\",\"params\":[\"%s\"],\"id\":1}",blockhash); - json = json_rpc_call(curl, datum_config.bitcoind_rpcurl, userpass, rpc_data); + json = bitcoind_json_rpc_call(curl, &datum_config, rpc_data); if (!json) return; json_decref(json); @@ -67,15 +62,11 @@ void preciousblock(CURL *curl, char *blockhash) { } void datum_submitblock_doit(CURL *tcurl, char *url, const char *submitblock_req, const char *block_hash_hex) { - char userpass[1024]; json_t *r; char *s = NULL; // TODO: Move these types of things to the conf file if (!url) { - snprintf(userpass, 1023, "%s:%s", datum_config.bitcoind_rpcuser, datum_config.bitcoind_rpcpassword); - userpass[1023] = 0; - - r = json_rpc_call(tcurl, datum_config.bitcoind_rpcurl, userpass, submitblock_req); + r = bitcoind_json_rpc_call(tcurl, &datum_config, submitblock_req); } else { r = json_rpc_call(tcurl, url, NULL, submitblock_req); }