Browse Source

Use a preallocated hash char for get blockhash to avoid alloc/free

master
Con Kolivas 11 years ago
parent
commit
cf04ec012a
  1. 22
      src/bitcoin.c
  2. 4
      src/bitcoin.h

22
src/bitcoin.c

@ -260,13 +260,14 @@ out:
return ret; return ret;
} }
/* Request getblockhash from bitcoind for height, returning a freshly allocated /* Request getblockhash from bitcoind for height, writing the value into *hash
* string or NULL if it fails. */ * which should be at least 65 bytes long since the hash is 64 chars. */
char *get_blockhash(connsock_t *cs, int height) bool get_blockhash(connsock_t *cs, int height, char *hash)
{ {
char rpc_req[128], *ret = NULL;
json_t *val, *res_val; json_t *val, *res_val;
const char *res_ret; const char *res_ret;
char rpc_req[128];
bool ret = false;
sprintf(rpc_req, "{\"method\": \"getblockhash\", \"params\": [%d]}\n", height); sprintf(rpc_req, "{\"method\": \"getblockhash\", \"params\": [%d]}\n", height);
val = json_rpc_call(cs, rpc_req); val = json_rpc_call(cs, rpc_req);
@ -284,7 +285,8 @@ char *get_blockhash(connsock_t *cs, int height)
LOGWARNING("Got null string in result to getblockhash"); LOGWARNING("Got null string in result to getblockhash");
goto out; goto out;
} }
ret = strdup(res_ret); strncpy(hash, res_ret, 65);
ret = true;
out: out:
json_decref(val); json_decref(val);
return ret; return ret;
@ -292,13 +294,12 @@ out:
static const char *bestblockhash_req = "{\"method\": \"getbestblockhash\"}\n"; static const char *bestblockhash_req = "{\"method\": \"getbestblockhash\"}\n";
/* Request getbestblockhash from bitcoind, returning a freshly allocated /* Request getbestblockhash from bitcoind. bitcoind 0.9+ only */
* string or NULL if it fails. bitcoind 0.9+ only */ bool get_bestblockhash(connsock_t *cs, char *hash)
char *get_bestblockhash(connsock_t *cs)
{ {
json_t *val, *res_val; json_t *val, *res_val;
const char *res_ret; const char *res_ret;
char *ret = NULL; bool ret = false;
val = json_rpc_call(cs, bestblockhash_req); val = json_rpc_call(cs, bestblockhash_req);
if (!val) { if (!val) {
@ -315,7 +316,8 @@ char *get_bestblockhash(connsock_t *cs)
LOGWARNING("Got null string in result to getbestblockhash"); LOGWARNING("Got null string in result to getbestblockhash");
goto out; goto out;
} }
ret = strdup(res_ret); strncpy(hash, res_ret, 65);
ret = true;
out: out:
json_decref(val); json_decref(val);
return ret; return ret;

4
src/bitcoin.h

@ -13,7 +13,7 @@
bool validate_address(connsock_t *cs, const char *address); bool validate_address(connsock_t *cs, const char *address);
bool gen_gbtbase(connsock_t *cs, gbtbase_t *gbt); bool gen_gbtbase(connsock_t *cs, gbtbase_t *gbt);
int get_blockcount(connsock_t *cs); int get_blockcount(connsock_t *cs);
char *get_blockhash(connsock_t *cs, int height); bool get_blockhash(connsock_t *cs, int height, char *hash);
char *get_bestblockhash(connsock_t *cs); bool get_bestblockhash(connsock_t *cs, char *hash);
#endif /* BITCOIN_H */ #endif /* BITCOIN_H */

Loading…
Cancel
Save