mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-26 02:24:18 +01:00
bitcoind: use correct endianness for block hashes.
Like txids, we need to reverse them. We didn't, but then we only used them to pass to/from bitcoind. We're about to get them from the block header, so we need to fix that. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -36,3 +36,15 @@ struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
|
||||
tal_free(linear_tx);
|
||||
return b;
|
||||
}
|
||||
|
||||
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
||||
struct sha256_double *blockid)
|
||||
{
|
||||
return bitcoin_txid_from_hex(hexstr, hexstr_len, blockid);
|
||||
}
|
||||
|
||||
bool bitcoin_blkid_to_hex(const struct sha256_double *blockid,
|
||||
char *hexstr, size_t hexstr_len)
|
||||
{
|
||||
return bitcoin_txid_to_hex(blockid, hexstr, hexstr_len);
|
||||
}
|
||||
|
||||
@@ -25,4 +25,11 @@ struct bitcoin_block {
|
||||
struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
|
||||
const char *hex, size_t hexlen);
|
||||
|
||||
/* Parse hex string to get blockid (reversed, a-la bitcoind). */
|
||||
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
||||
struct sha256_double *blockid);
|
||||
|
||||
/* Get hex string of blockid (reversed, a-la bitcoind). */
|
||||
bool bitcoin_blkid_to_hex(const struct sha256_double *blockid,
|
||||
char *hexstr, size_t hexstr_len);
|
||||
#endif /* LIGHTNING_BITCOIN_BLOCK_H */
|
||||
|
||||
@@ -259,9 +259,9 @@ static void process_transactions(struct bitcoin_cli *bcli)
|
||||
if (!blktok)
|
||||
fatal("listtransactions: no blockhash field!");
|
||||
|
||||
if (!hex_decode(bcli->output + blktok->start,
|
||||
blktok->end - blktok->start,
|
||||
&blkhash, sizeof(blkhash))) {
|
||||
if (!bitcoin_blkid_from_hex(bcli->output + blktok->start,
|
||||
blktok->end - blktok->start,
|
||||
&blkhash)) {
|
||||
fatal("listtransactions: bad blockhash '%.*s'",
|
||||
(int)(blktok->end - blktok->start),
|
||||
bcli->output + blktok->start);
|
||||
@@ -496,7 +496,7 @@ void bitcoind_get_mediantime(struct lightningd_state *dstate,
|
||||
{
|
||||
char hex[hex_str_size(sizeof(*blockid))];
|
||||
|
||||
hex_encode(blockid, sizeof(*blockid), hex, sizeof(hex));
|
||||
bitcoin_blkid_to_hex(blockid, hex, sizeof(hex));
|
||||
start_bitcoin_cli(dstate, process_getblock_for_mediantime,
|
||||
NULL, mediantime,
|
||||
"getblock", hex, NULL);
|
||||
@@ -532,9 +532,9 @@ static void process_chaintips(struct bitcoin_cli *bcli)
|
||||
for (i = 0, t = tokens + 1; t < end; t = json_next(t), i++) {
|
||||
const jsmntok_t *hash = json_get_member(bcli->output, t, "hash");
|
||||
tal_resize(&tips, i+1);
|
||||
if (!hex_decode(bcli->output + hash->start,
|
||||
hash->end - hash->start,
|
||||
&tips[i], sizeof(tips[i])))
|
||||
if (!bitcoin_blkid_from_hex(bcli->output + hash->start,
|
||||
hash->end - hash->start,
|
||||
&tips[i]))
|
||||
fatal("%s: gave bad hash for %zu'th tip (%.*s)?",
|
||||
bcli_args(bcli), i,
|
||||
(int)bcli->output_bytes, bcli->output);
|
||||
@@ -631,9 +631,9 @@ static void process_getblock(struct bitcoin_cli *bcli)
|
||||
prevblk_tok = json_get_member(bcli->output, tokens, "previousblockhash");
|
||||
if (prevblk_tok) {
|
||||
prevblk = tal(bcli, struct sha256_double);
|
||||
if (!hex_decode(bcli->output + prevblk_tok->start,
|
||||
prevblk_tok->end - prevblk_tok->start,
|
||||
prevblk, sizeof(*prevblk))) {
|
||||
if (!bitcoin_blkid_from_hex(bcli->output + prevblk_tok->start,
|
||||
prevblk_tok->end - prevblk_tok->start,
|
||||
prevblk)) {
|
||||
fatal("%s: bad previousblockhash '%.*s'",
|
||||
bcli_args(bcli),
|
||||
(int)(prevblk_tok->end - prevblk_tok->start),
|
||||
@@ -648,9 +648,9 @@ static void process_getblock(struct bitcoin_cli *bcli)
|
||||
bcli_args(bcli),
|
||||
(int)bcli->output_bytes, bcli->output);
|
||||
|
||||
if (!hex_decode(bcli->output + blkid_tok->start,
|
||||
blkid_tok->end - blkid_tok->start,
|
||||
&blkid, sizeof(blkid))) {
|
||||
if (!bitcoin_blkid_from_hex(bcli->output + blkid_tok->start,
|
||||
blkid_tok->end - blkid_tok->start,
|
||||
&blkid)) {
|
||||
fatal("%s: bad hash '%.*s'",
|
||||
bcli_args(bcli),
|
||||
(int)(blkid_tok->end - blkid_tok->start),
|
||||
@@ -720,7 +720,7 @@ void bitcoind_getblock_(struct lightningd_state *dstate,
|
||||
{
|
||||
char hex[hex_str_size(sizeof(*blockid))];
|
||||
|
||||
hex_encode(blockid, sizeof(*blockid), hex, sizeof(hex));
|
||||
bitcoin_blkid_to_hex(blockid, hex, sizeof(hex));
|
||||
start_bitcoin_cli(dstate, process_getblock, cb, arg,
|
||||
"getblock", hex, NULL);
|
||||
}
|
||||
@@ -754,7 +754,7 @@ void bitcoind_getrawblock_(struct lightningd_state *dstate,
|
||||
{
|
||||
char hex[hex_str_size(sizeof(*blockid))];
|
||||
|
||||
hex_encode(blockid, sizeof(*blockid), hex, sizeof(hex));
|
||||
bitcoin_blkid_to_hex(blockid, hex, sizeof(hex));
|
||||
start_bitcoin_cli(dstate, process_rawblock, cb, arg,
|
||||
"getblock", hex, "false", NULL);
|
||||
}
|
||||
@@ -800,8 +800,8 @@ static void process_getblockhash(struct bitcoin_cli *bcli)
|
||||
fatal("%s: failed", bcli_args(bcli));
|
||||
|
||||
if (bcli->output_bytes == 0
|
||||
|| !hex_decode(bcli->output, bcli->output_bytes-1,
|
||||
&blkid, sizeof(blkid))) {
|
||||
|| !bitcoin_blkid_from_hex(bcli->output, bcli->output_bytes-1,
|
||||
&blkid)) {
|
||||
fatal("%s: bad blockid '%.*s'",
|
||||
bcli_args(bcli), (int)bcli->output_bytes, bcli->output);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user