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

4
src/bitcoin.h

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

Loading…
Cancel
Save