2016-04-24 12:10:29 +02:00
|
|
|
#include "bitcoin/block.h"
|
2016-08-18 06:53:45 +02:00
|
|
|
#include "bitcoin/pullpush.h"
|
2016-04-24 12:10:29 +02:00
|
|
|
#include "bitcoin/tx.h"
|
|
|
|
#include <ccan/str/hex/hex.h>
|
2017-12-18 07:44:10 +01:00
|
|
|
#include <common/type_to_string.h>
|
2016-04-24 12:10:29 +02:00
|
|
|
|
2020-01-23 15:06:02 +01:00
|
|
|
static void sha256_varint(struct sha256_ctx *ctx, u64 val)
|
|
|
|
{
|
|
|
|
u8 vt[VARINT_MAX_LEN];
|
|
|
|
size_t vtlen;
|
|
|
|
vtlen = varint_put(vt, val);
|
|
|
|
sha256_update(ctx, vt, vtlen);
|
|
|
|
}
|
|
|
|
|
2020-01-24 22:19:04 +01:00
|
|
|
static void bitcoin_block_pull_dynafed_params(const u8 **cursor, size_t *len, struct sha256_ctx *shactx)
|
|
|
|
{
|
|
|
|
u8 type;
|
|
|
|
u64 l1, l2;
|
|
|
|
pull(cursor, len, &type, 1);
|
|
|
|
sha256_update(shactx, &type, 1);
|
|
|
|
switch ((enum dynafed_params_type)type) {
|
|
|
|
case DYNAFED_PARAMS_NULL:
|
|
|
|
break;
|
|
|
|
case DYNAFED_PARAMS_COMPACT:
|
|
|
|
/* "scriptPubKey" used for block signing */
|
|
|
|
l1 = pull_varint(cursor, len);
|
|
|
|
sha256_varint(shactx, l1);
|
|
|
|
sha256_update(shactx, *cursor, l1);
|
|
|
|
pull(cursor, len, NULL, l1);
|
|
|
|
|
|
|
|
/* signblock_witness_limit */
|
|
|
|
sha256_update(shactx, *cursor, 4);
|
|
|
|
pull(cursor, len, NULL, 4);
|
|
|
|
|
|
|
|
/* Skip elided_root */
|
|
|
|
sha256_update(shactx, *cursor, 32);
|
|
|
|
pull(cursor, len, NULL, 32);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DYNAFED_PARAMS_FULL:
|
|
|
|
/* "scriptPubKey" used for block signing */
|
|
|
|
l1 = pull_varint(cursor, len);
|
|
|
|
sha256_varint(shactx, l1);
|
|
|
|
sha256_update(shactx, *cursor, l1);
|
|
|
|
pull(cursor, len, NULL, l1);
|
|
|
|
|
|
|
|
/* signblock_witness_limit */
|
|
|
|
sha256_update(shactx, *cursor, 4);
|
|
|
|
pull(cursor, len, NULL, 4);
|
|
|
|
|
|
|
|
/* fedpeg_program */
|
|
|
|
l1 = pull_varint(cursor, len);
|
|
|
|
sha256_varint(shactx, l1);
|
|
|
|
sha256_update(shactx, *cursor, l1);
|
|
|
|
pull(cursor, len, NULL, l1);
|
|
|
|
|
|
|
|
/* fedpegscript */
|
|
|
|
l1 = pull_varint(cursor, len);
|
|
|
|
sha256_varint(shactx, l1);
|
|
|
|
sha256_update(shactx, *cursor, l1);
|
|
|
|
pull(cursor, len, NULL, l1);
|
|
|
|
|
|
|
|
/* extension space */
|
|
|
|
l2 = pull_varint(cursor, len);
|
|
|
|
sha256_varint(shactx, l2);
|
|
|
|
for (size_t i = 0; i < l2; i++) {
|
|
|
|
l1 = pull_varint(cursor, len);
|
|
|
|
sha256_varint(shactx, l1);
|
|
|
|
sha256_update(shactx, *cursor, l1);
|
|
|
|
pull(cursor, len, NULL, l1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bitcoin_block_pull_dynafed_details(const u8 **cursor, size_t *len, struct sha256_ctx *shactx)
|
|
|
|
{
|
|
|
|
bitcoin_block_pull_dynafed_params(cursor, len, shactx);
|
|
|
|
bitcoin_block_pull_dynafed_params(cursor, len, shactx);
|
|
|
|
|
|
|
|
/* Consume the signblock_witness */
|
|
|
|
u64 numwitnesses = pull_varint(cursor, len);
|
|
|
|
for (size_t i=0; i<numwitnesses; i++) {
|
|
|
|
u64 witsize = pull_varint(cursor, len);
|
|
|
|
pull(cursor, len, NULL, witsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:10:29 +02:00
|
|
|
/* Encoding is <blockhdr> <varint-num-txs> <tx>... */
|
2019-07-30 19:51:53 +02:00
|
|
|
struct bitcoin_block *
|
|
|
|
bitcoin_block_from_hex(const tal_t *ctx, const struct chainparams *chainparams,
|
|
|
|
const char *hex, size_t hexlen)
|
2016-04-24 12:10:29 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_block *b;
|
|
|
|
u8 *linear_tx;
|
|
|
|
const u8 *p;
|
2020-01-25 13:14:34 +01:00
|
|
|
size_t len, i, num, templen;
|
2020-01-23 13:38:13 +01:00
|
|
|
struct sha256_ctx shactx;
|
2020-01-24 22:19:04 +01:00
|
|
|
bool is_dynafed;
|
2020-01-25 13:14:34 +01:00
|
|
|
u32 height;
|
2016-04-24 12:10:29 +02:00
|
|
|
|
|
|
|
if (hexlen && hex[hexlen-1] == '\n')
|
|
|
|
hexlen--;
|
|
|
|
|
|
|
|
/* Set up the block for success. */
|
|
|
|
b = tal(ctx, struct bitcoin_block);
|
|
|
|
|
|
|
|
/* De-hex the array. */
|
|
|
|
len = hex_data_size(hexlen);
|
|
|
|
p = linear_tx = tal_arr(ctx, u8, len);
|
|
|
|
if (!hex_decode(hex, hexlen, linear_tx, len))
|
|
|
|
return tal_free(b);
|
|
|
|
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_init(&shactx);
|
|
|
|
|
2019-04-12 21:57:52 +02:00
|
|
|
b->hdr.version = pull_le32(&p, &len);
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_le32(&shactx, b->hdr.version);
|
|
|
|
|
2019-04-12 21:57:52 +02:00
|
|
|
pull(&p, &len, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_update(&shactx, &b->hdr.prev_hash, sizeof(b->hdr.prev_hash));
|
|
|
|
|
2019-04-12 21:57:52 +02:00
|
|
|
pull(&p, &len, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_update(&shactx, &b->hdr.merkle_hash, sizeof(b->hdr.merkle_hash));
|
|
|
|
|
2019-04-12 21:57:52 +02:00
|
|
|
b->hdr.timestamp = pull_le32(&p, &len);
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_le32(&shactx, b->hdr.timestamp);
|
2019-04-12 21:57:52 +02:00
|
|
|
|
2019-09-26 00:42:26 +02:00
|
|
|
if (is_elements(chainparams)) {
|
2020-01-24 22:19:04 +01:00
|
|
|
/* A dynafed block is signalled by setting the MSB of the version. */
|
|
|
|
is_dynafed = (b->hdr.version >> 31 == 1);
|
2020-01-25 13:14:34 +01:00
|
|
|
|
|
|
|
/* elements_header.height */
|
|
|
|
height = pull_le32(&p, &len);
|
|
|
|
sha256_le32(&shactx, height);
|
2019-04-12 21:57:52 +02:00
|
|
|
|
2020-01-24 22:19:04 +01:00
|
|
|
if (is_dynafed) {
|
|
|
|
bitcoin_block_pull_dynafed_details(&p, &len, &shactx);
|
|
|
|
} else {
|
2020-01-25 13:14:34 +01:00
|
|
|
/* elemens_header.challenge */
|
|
|
|
templen = pull_varint(&p, &len);
|
|
|
|
sha256_varint(&shactx, templen);
|
|
|
|
sha256_update(&shactx, p, templen);
|
|
|
|
pull(&p, &len, NULL, templen);
|
|
|
|
|
|
|
|
/* elements_header.solution. Not hashed since it'd be
|
|
|
|
* a circular dependency. */
|
|
|
|
templen = pull_varint(&p, &len);
|
|
|
|
pull(&p, &len, NULL, templen);
|
2020-01-24 22:19:04 +01:00
|
|
|
}
|
2019-04-12 21:57:52 +02:00
|
|
|
|
|
|
|
} else {
|
|
|
|
b->hdr.target = pull_le32(&p, &len);
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_le32(&shactx, b->hdr.target);
|
|
|
|
|
2019-04-12 21:57:52 +02:00
|
|
|
b->hdr.nonce = pull_le32(&p, &len);
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_le32(&shactx, b->hdr.nonce);
|
2019-04-12 21:57:52 +02:00
|
|
|
}
|
2020-01-23 13:38:13 +01:00
|
|
|
sha256_double_done(&shactx, &b->hdr.hash.shad);
|
2019-04-12 21:57:52 +02:00
|
|
|
|
2016-04-24 12:10:29 +02:00
|
|
|
num = pull_varint(&p, &len);
|
|
|
|
b->tx = tal_arr(b, struct bitcoin_tx *, num);
|
2019-07-30 19:51:53 +02:00
|
|
|
for (i = 0; i < num; i++) {
|
2016-04-24 12:10:29 +02:00
|
|
|
b->tx[i] = pull_bitcoin_tx(b->tx, &p, &len);
|
2019-07-30 19:51:53 +02:00
|
|
|
b->tx[i]->chainparams = chainparams;
|
|
|
|
}
|
2016-04-24 12:10:29 +02:00
|
|
|
|
|
|
|
/* We should end up not overrunning, nor have extra */
|
|
|
|
if (!p || len)
|
|
|
|
return tal_free(b);
|
|
|
|
|
|
|
|
tal_free(linear_tx);
|
|
|
|
return b;
|
|
|
|
}
|
2016-04-24 12:11:20 +02:00
|
|
|
|
2019-04-13 17:55:06 +02:00
|
|
|
void bitcoin_block_blkid(const struct bitcoin_block *b,
|
|
|
|
struct bitcoin_blkid *out)
|
|
|
|
{
|
2020-01-24 21:02:07 +01:00
|
|
|
*out = b->hdr.hash;
|
2019-04-13 17:55:06 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
/* We do the same hex-reversing crud as txids. */
|
2016-04-24 12:11:20 +02:00
|
|
|
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid *blockid)
|
2016-04-24 12:11:20 +02:00
|
|
|
{
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid fake_txid;
|
|
|
|
if (!bitcoin_txid_from_hex(hexstr, hexstr_len, &fake_txid))
|
|
|
|
return false;
|
2017-12-18 07:44:10 +01:00
|
|
|
blockid->shad = fake_txid.shad;
|
2017-12-18 07:41:52 +01:00
|
|
|
return true;
|
2016-04-24 12:11:20 +02:00
|
|
|
}
|
|
|
|
|
2017-12-18 07:44:10 +01:00
|
|
|
bool bitcoin_blkid_to_hex(const struct bitcoin_blkid *blockid,
|
2016-04-24 12:11:20 +02:00
|
|
|
char *hexstr, size_t hexstr_len)
|
|
|
|
{
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid fake_txid;
|
2017-12-18 07:44:10 +01:00
|
|
|
fake_txid.shad = blockid->shad;
|
2017-12-18 07:41:52 +01:00
|
|
|
return bitcoin_txid_to_hex(&fake_txid, hexstr, hexstr_len);
|
2016-04-24 12:11:20 +02:00
|
|
|
}
|
2017-12-22 02:25:01 +01:00
|
|
|
|
|
|
|
static char *fmt_bitcoin_blkid(const tal_t *ctx,
|
|
|
|
const struct bitcoin_blkid *blkid)
|
|
|
|
{
|
|
|
|
char *hexstr = tal_arr(ctx, char, hex_str_size(sizeof(*blkid)));
|
|
|
|
|
|
|
|
bitcoin_blkid_to_hex(blkid, hexstr, hex_str_size(sizeof(*blkid)));
|
|
|
|
return hexstr;
|
|
|
|
}
|
|
|
|
REGISTER_TYPE_TO_STRING(bitcoin_blkid, fmt_bitcoin_blkid);
|