2016-04-24 12:12:18 +02:00
|
|
|
#include "bitcoin/block.h"
|
|
|
|
#include "bitcoin/tx.h"
|
2016-04-24 12:07:13 +02:00
|
|
|
#include "bitcoind.h"
|
|
|
|
#include "chaintopology.h"
|
2016-11-09 07:44:21 +01:00
|
|
|
#include "jsonrpc.h"
|
2016-04-24 12:07:13 +02:00
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
2016-05-04 08:33:10 +02:00
|
|
|
#include "peer.h"
|
2016-04-24 12:07:13 +02:00
|
|
|
#include "timeout.h"
|
2016-05-04 08:33:10 +02:00
|
|
|
#include "utils.h"
|
2016-04-24 12:07:13 +02:00
|
|
|
#include "watch.h"
|
2016-04-24 12:12:18 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
|
|
|
#include <ccan/asort/asort.h>
|
2016-08-09 05:41:22 +02:00
|
|
|
#include <ccan/io/io.h>
|
2016-04-24 12:07:13 +02:00
|
|
|
#include <ccan/structeq/structeq.h>
|
2016-11-07 13:29:02 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2016-08-18 06:53:46 +02:00
|
|
|
#include <inttypes.h>
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
struct block {
|
|
|
|
int height;
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
/* Actual header. */
|
|
|
|
struct bitcoin_block_hdr hdr;
|
|
|
|
|
|
|
|
/* Previous block (if any). */
|
|
|
|
struct block *prev;
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
/* Next block (if any). */
|
|
|
|
struct block *next;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* Key for hash table */
|
|
|
|
struct sha256_double blkid;
|
2016-04-24 12:12:18 +02:00
|
|
|
|
|
|
|
/* 0 if not enough predecessors. */
|
2016-04-24 12:07:13 +02:00
|
|
|
u32 mediantime;
|
|
|
|
|
|
|
|
/* Transactions in this block we care about */
|
2016-05-04 08:40:37 +02:00
|
|
|
struct sha256_double *txids;
|
2016-04-24 12:18:35 +02:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
/* Full copy of txs (trimmed to txs list in connect_block) */
|
2016-04-24 12:18:35 +02:00
|
|
|
struct bitcoin_tx **full_txs;
|
2016-04-24 12:07:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Hash blocks by sha */
|
|
|
|
static const struct sha256_double *keyof_block_map(const struct block *b)
|
|
|
|
{
|
|
|
|
return &b->blkid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t hash_sha(const struct sha256_double *key)
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
|
|
|
|
memcpy(&ret, key, sizeof(ret));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool block_eq(const struct block *b, const struct sha256_double *key)
|
|
|
|
{
|
|
|
|
return structeq(&b->blkid, key);
|
|
|
|
}
|
|
|
|
HTABLE_DEFINE_TYPE(struct block, keyof_block_map, hash_sha, block_eq, block_map);
|
|
|
|
|
|
|
|
struct topology {
|
2016-04-24 12:16:32 +02:00
|
|
|
struct block *root;
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *tip;
|
2016-04-24 12:07:13 +02:00
|
|
|
struct block_map block_map;
|
2016-08-18 06:53:46 +02:00
|
|
|
u64 feerate;
|
2016-08-18 06:53:46 +02:00
|
|
|
bool startup;
|
2016-04-24 12:07:13 +02:00
|
|
|
};
|
|
|
|
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
static void start_poll_chaintip(struct lightningd_state *dstate);
|
2016-05-09 22:56:09 +02:00
|
|
|
|
|
|
|
static void next_topology_timer(struct lightningd_state *dstate)
|
|
|
|
{
|
2016-08-18 06:53:46 +02:00
|
|
|
if (dstate->topology->startup) {
|
|
|
|
dstate->topology->startup = false;
|
|
|
|
io_break(dstate);
|
|
|
|
}
|
2016-05-09 22:59:12 +02:00
|
|
|
new_reltimer(dstate, dstate, dstate->config.poll_time,
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
start_poll_chaintip, dstate);
|
2016-05-09 22:56:09 +02:00
|
|
|
}
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
static int cmp_times(const u32 *a, const u32 *b, void *unused)
|
|
|
|
{
|
|
|
|
if (*a > *b)
|
|
|
|
return -1;
|
|
|
|
else if (*b > * a)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
/* Mediantime is median of this and previous 10 blocks. */
|
2016-04-24 12:12:18 +02:00
|
|
|
static u32 get_mediantime(const struct topology *topo, const struct block *b)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
u32 times[11];
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(times); i++) {
|
|
|
|
if (!b)
|
|
|
|
return 0;
|
|
|
|
times[i] = le32_to_cpu(b->hdr.timestamp);
|
|
|
|
b = b->prev;
|
|
|
|
}
|
|
|
|
asort(times, ARRAY_SIZE(times), cmp_times, NULL);
|
|
|
|
return times[ARRAY_SIZE(times) / 2];
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
/* FIXME: Remove tx from block when peer done. */
|
|
|
|
static void add_tx_to_block(struct block *b, const struct sha256_double *txid)
|
2016-04-24 12:18:35 +02:00
|
|
|
{
|
2016-05-04 08:40:37 +02:00
|
|
|
size_t n = tal_count(b->txids);
|
2016-04-24 12:18:35 +02:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
tal_resize(&b->txids, n+1);
|
|
|
|
b->txids[n] = *txid;
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:41:16 +02:00
|
|
|
static bool we_broadcast(struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *txid)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
list_for_each(&dstate->peers, peer, list) {
|
|
|
|
struct outgoing_tx *otx;
|
|
|
|
|
|
|
|
list_for_each(&peer->outgoing_txs, otx, list) {
|
|
|
|
if (structeq(&otx->txid, txid))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
/* Fills in prev, height, mediantime. */
|
2016-05-04 08:36:19 +02:00
|
|
|
static void connect_block(struct lightningd_state *dstate,
|
|
|
|
struct block *prev,
|
|
|
|
struct block *b)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
size_t i;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
assert(b->height == -1);
|
|
|
|
assert(b->mediantime == 0);
|
|
|
|
assert(b->prev == NULL);
|
|
|
|
assert(prev->next == b);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
b->prev = prev;
|
2016-04-24 12:12:18 +02:00
|
|
|
b->height = b->prev->height + 1;
|
2016-04-24 12:18:35 +02:00
|
|
|
b->mediantime = get_mediantime(topo, b);
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
block_map_add(&topo->block_map, b);
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
/* Now we see if any of those txs are interesting. */
|
|
|
|
for (i = 0; i < tal_count(b->full_txs); i++) {
|
|
|
|
struct bitcoin_tx *tx = b->full_txs[i];
|
|
|
|
struct sha256_double txid;
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
/* Tell them if it spends a txo we care about. */
|
|
|
|
for (j = 0; j < tx->input_count; j++) {
|
|
|
|
struct txwatch_output out;
|
|
|
|
struct txowatch *txo;
|
|
|
|
out.txid = tx->input[j].txid;
|
|
|
|
out.index = tx->input[j].index;
|
|
|
|
|
|
|
|
txo = txowatch_hash_get(&dstate->txowatches, &out);
|
|
|
|
if (txo)
|
2016-05-03 03:30:20 +02:00
|
|
|
txowatch_fire(dstate, txo, tx, j);
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
/* We did spends first, in case that tells us to watch tx. */
|
2016-04-24 12:31:52 +02:00
|
|
|
bitcoin_txid(tx, &txid);
|
2016-05-04 08:41:16 +02:00
|
|
|
if (watching_txid(dstate, &txid) || we_broadcast(dstate, &txid))
|
2016-05-04 08:40:37 +02:00
|
|
|
add_tx_to_block(b, &txid);
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
|
|
|
b->full_txs = tal_free(b->full_txs);
|
2016-11-09 07:44:22 +01:00
|
|
|
|
|
|
|
/* Tell peers about new block. */
|
|
|
|
peers_new_block(dstate, b->height);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
static bool tx_in_block(const struct block *b,
|
|
|
|
const struct sha256_double *txid)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:40:37 +02:00
|
|
|
size_t i, n = tal_count(b->txids);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (structeq(&b->txids[i], txid))
|
2016-04-24 12:07:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
/* FIXME: Use hash table. */
|
|
|
|
static struct block *block_for_tx(struct lightningd_state *dstate,
|
2016-05-04 08:36:19 +02:00
|
|
|
const struct sha256_double *txid)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:40:37 +02:00
|
|
|
struct topology *topo = dstate->topology;
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *b;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
for (b = topo->tip; b; b = b->prev) {
|
|
|
|
if (tx_in_block(b, txid))
|
|
|
|
return b;
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
2016-05-04 08:36:19 +02:00
|
|
|
return NULL;
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
size_t get_tx_depth(struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *txid)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
2016-05-04 08:36:19 +02:00
|
|
|
const struct block *b;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
b = block_for_tx(dstate, txid);
|
2016-05-04 08:36:19 +02:00
|
|
|
if (!b)
|
|
|
|
return 0;
|
|
|
|
return topo->tip->height - b->height + 1;
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
struct txs_to_broadcast {
|
|
|
|
/* We just sent txs[cursor] */
|
|
|
|
size_t cursor;
|
|
|
|
/* These are hex encoded already, for bitcoind_sendrawtx */
|
|
|
|
const char **txs;
|
2016-11-09 07:44:21 +01:00
|
|
|
|
|
|
|
/* Command to complete when we're done, iff dev-broadcast triggered */
|
|
|
|
struct command *cmd;
|
2016-11-09 07:44:10 +01:00
|
|
|
};
|
|
|
|
|
2016-11-07 13:34:02 +01:00
|
|
|
/* We just sent the last entry in txs[]. Shrink and send the next last. */
|
2016-11-07 13:29:02 +01:00
|
|
|
static void broadcast_remainder(struct lightningd_state *dstate,
|
2016-11-09 07:44:10 +01:00
|
|
|
int exitstatus, const char *msg,
|
|
|
|
struct txs_to_broadcast *txs)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
|
|
|
/* These are expected. */
|
|
|
|
if (strstr(msg, "txn-mempool-conflict")
|
|
|
|
|| strstr(msg, "transaction already in block chain"))
|
|
|
|
log_debug(dstate->base_log,
|
|
|
|
"Expected error broadcasting tx %s: %s",
|
2016-11-09 07:44:10 +01:00
|
|
|
txs->txs[txs->cursor], msg);
|
2016-11-07 13:30:02 +01:00
|
|
|
else if (exitstatus)
|
|
|
|
log_unusual(dstate->base_log, "Broadcasting tx %s: %i %s",
|
2016-11-09 07:44:10 +01:00
|
|
|
txs->txs[txs->cursor], exitstatus, msg);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
txs->cursor++;
|
|
|
|
if (txs->cursor == tal_count(txs->txs)) {
|
2016-11-09 07:44:21 +01:00
|
|
|
if (txs->cmd)
|
|
|
|
command_success(txs->cmd, null_response(txs->cmd));
|
2016-05-04 08:33:10 +02:00
|
|
|
tal_free(txs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:34:02 +01:00
|
|
|
/* Broadcast next one. */
|
2016-11-09 07:44:10 +01:00
|
|
|
bitcoind_sendrawtx(NULL, dstate, txs->txs[txs->cursor],
|
|
|
|
broadcast_remainder, txs);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: This is dumb. We can group txs and avoid bothering bitcoind
|
|
|
|
* if any one tx is in the main chain. */
|
2016-11-09 07:44:21 +01:00
|
|
|
static void rebroadcast_txs(struct lightningd_state *dstate,
|
|
|
|
struct command *cmd)
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
|
|
|
/* Copy txs now (peers may go away, and they own txs). */
|
|
|
|
size_t num_txs = 0;
|
2016-11-09 07:44:21 +01:00
|
|
|
struct txs_to_broadcast *txs;
|
2016-05-04 08:33:10 +02:00
|
|
|
struct peer *peer;
|
|
|
|
|
2016-11-09 07:44:21 +01:00
|
|
|
if (dstate->dev_no_broadcast)
|
|
|
|
return;
|
|
|
|
|
|
|
|
txs = tal(dstate, struct txs_to_broadcast);
|
|
|
|
txs->cmd = cmd;
|
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
/* Put any txs we want to broadcast in ->txs. */
|
|
|
|
txs->txs = tal_arr(txs, const char *, 0);
|
2016-05-04 08:33:10 +02:00
|
|
|
list_for_each(&dstate->peers, peer, list) {
|
|
|
|
struct outgoing_tx *otx;
|
|
|
|
|
|
|
|
list_for_each(&peer->outgoing_txs, otx, list) {
|
2016-05-04 08:40:37 +02:00
|
|
|
if (block_for_tx(dstate, &otx->txid))
|
2016-05-04 08:33:10 +02:00
|
|
|
continue;
|
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
tal_resize(&txs->txs, num_txs+1);
|
|
|
|
txs->txs[num_txs] = tal_strdup(txs, otx->hextx);
|
2016-05-04 08:33:10 +02:00
|
|
|
num_txs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
/* Let this do the dirty work. */
|
|
|
|
txs->cursor = (size_t)-1;
|
|
|
|
broadcast_remainder(dstate, 0, "", txs);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_outgoing_tx(struct outgoing_tx *otx)
|
|
|
|
{
|
2016-11-07 13:29:02 +01:00
|
|
|
list_del_from(&otx->peer->outgoing_txs, &otx->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void broadcast_done(struct lightningd_state *dstate,
|
2016-11-07 13:30:02 +01:00
|
|
|
int exitstatus, const char *msg,
|
|
|
|
struct outgoing_tx *otx)
|
2016-11-07 13:29:02 +01:00
|
|
|
{
|
2016-11-07 13:31:02 +01:00
|
|
|
if (otx->failed && exitstatus != 0) {
|
|
|
|
otx->failed(otx->peer, exitstatus, msg);
|
|
|
|
tal_free(otx);
|
|
|
|
} else {
|
|
|
|
/* For continual rebroadcasting */
|
|
|
|
list_add_tail(&otx->peer->outgoing_txs, &otx->list);
|
|
|
|
tal_add_destructor(otx, destroy_outgoing_tx);
|
|
|
|
}
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2016-11-07 13:31:02 +01:00
|
|
|
void broadcast_tx(struct peer *peer, const struct bitcoin_tx *tx,
|
|
|
|
void (*failed)(struct peer *peer,
|
|
|
|
int exitstatus, const char *err))
|
2016-05-04 08:33:10 +02:00
|
|
|
{
|
|
|
|
struct outgoing_tx *otx = tal(peer, struct outgoing_tx);
|
2016-11-07 13:29:02 +01:00
|
|
|
const u8 *rawtx = linearize_tx(otx, tx);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2016-11-07 13:29:02 +01:00
|
|
|
otx->peer = peer;
|
|
|
|
bitcoin_txid(tx, &otx->txid);
|
|
|
|
otx->hextx = tal_hexstr(otx, rawtx, tal_count(rawtx));
|
2016-11-07 13:31:02 +01:00
|
|
|
otx->failed = failed;
|
2016-11-07 13:29:02 +01:00
|
|
|
tal_free(rawtx);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
log_add_struct(peer->log, " (tx %s)", struct sha256_double, &otx->txid);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2016-11-09 07:44:21 +01:00
|
|
|
if (peer->dstate->dev_no_broadcast)
|
|
|
|
broadcast_done(peer->dstate, 0, "dev_no_broadcast", otx);
|
|
|
|
else
|
|
|
|
bitcoind_sendrawtx(peer, peer->dstate, otx->hextx,
|
|
|
|
broadcast_done, otx);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
static void free_blocks(struct lightningd_state *dstate, struct block *b)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *next;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
while (b) {
|
2016-05-04 08:40:37 +02:00
|
|
|
size_t i, n = tal_count(b->txids);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
/* Notify that txs are kicked out. */
|
2016-05-04 08:40:37 +02:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
txwatch_fire(dstate, &b->txids[i], 0);
|
2016-05-04 08:36:19 +02:00
|
|
|
|
|
|
|
next = b->next;
|
|
|
|
tal_free(b);
|
|
|
|
b = next;
|
2016-04-24 12:12:18 +02:00
|
|
|
}
|
2016-05-04 08:36:19 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
static void update_fee(struct lightningd_state *dstate, u64 rate, u64 *feerate)
|
|
|
|
{
|
|
|
|
log_debug(dstate->base_log, "Feerate %"PRIu64" -> %"PRIu64,
|
|
|
|
rate, *feerate);
|
|
|
|
*feerate = rate;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
/* B is the new chain (linked by ->next); update topology */
|
|
|
|
static void topology_changed(struct lightningd_state *dstate,
|
|
|
|
struct block *prev,
|
|
|
|
struct block *b)
|
|
|
|
{
|
|
|
|
/* Eliminate any old chain. */
|
|
|
|
if (prev->next)
|
|
|
|
free_blocks(dstate, prev->next);
|
|
|
|
|
|
|
|
prev->next = b;
|
|
|
|
do {
|
|
|
|
connect_block(dstate, prev, b);
|
|
|
|
dstate->topology->tip = prev = b;
|
|
|
|
b = b->next;
|
|
|
|
} while (b);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
/* Tell watch code to re-evaluate all txs. */
|
|
|
|
watch_topology_changed(dstate);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
|
|
|
/* Maybe need to rebroadcast. */
|
2016-11-09 07:44:21 +01:00
|
|
|
rebroadcast_txs(dstate, NULL);
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
/* Once per new block head, update fee estimate. */
|
|
|
|
bitcoind_estimate_fee(dstate, update_fee, &dstate->topology->feerate);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
static struct block *new_block(struct lightningd_state *dstate,
|
|
|
|
struct bitcoin_block *blk,
|
|
|
|
struct block *next)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
struct block *b = tal(topo, struct block);
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
sha256_double(&b->blkid, &blk->hdr, sizeof(blk->hdr));
|
2016-06-28 23:19:21 +02:00
|
|
|
log_debug_struct(dstate->base_log, "Adding block %s",
|
|
|
|
struct sha256_double, &b->blkid);
|
2016-04-24 12:12:18 +02:00
|
|
|
assert(!block_map_get(&topo->block_map, &b->blkid));
|
2016-05-04 08:36:19 +02:00
|
|
|
b->next = next;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* We fill these out in topology_changed */
|
|
|
|
b->height = -1;
|
2016-04-24 12:12:18 +02:00
|
|
|
b->mediantime = 0;
|
|
|
|
b->prev = NULL;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
b->hdr = blk->hdr;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
b->txids = tal_arr(b, struct sha256_double, 0);
|
2016-04-24 12:18:35 +02:00
|
|
|
b->full_txs = tal_steal(b, blk->tx);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gather_blocks(struct lightningd_state *dstate,
|
2016-04-24 12:12:18 +02:00
|
|
|
struct bitcoin_block *blk,
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *next)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *b, *prev;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
b = new_block(dstate, blk, next);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* Recurse if we need prev. */
|
2016-05-04 08:36:19 +02:00
|
|
|
prev = block_map_get(&topo->block_map, &blk->hdr.prev_hash);
|
|
|
|
if (!prev) {
|
2016-04-24 12:12:18 +02:00
|
|
|
bitcoind_getrawblock(dstate, &blk->hdr.prev_hash,
|
2016-05-04 08:36:19 +02:00
|
|
|
gather_blocks, b);
|
2016-04-24 12:07:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done. */
|
2016-05-04 08:36:19 +02:00
|
|
|
topology_changed(dstate, prev, b);
|
2016-05-09 22:56:09 +02:00
|
|
|
next_topology_timer(dstate);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
static void check_chaintip(struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *tipid,
|
|
|
|
void *arg)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
/* 0 is the main tip. */
|
2016-08-09 05:41:22 +02:00
|
|
|
if (!structeq(tipid, &topo->tip->blkid))
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
bitcoind_getrawblock(dstate, tipid, gather_blocks,
|
2016-05-04 08:36:19 +02:00
|
|
|
(struct block *)NULL);
|
|
|
|
else
|
2016-05-09 22:56:09 +02:00
|
|
|
/* Next! */
|
|
|
|
next_topology_timer(dstate);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
static void start_poll_chaintip(struct lightningd_state *dstate)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
if (!list_empty(&dstate->bitcoin_req)) {
|
|
|
|
log_unusual(dstate->base_log,
|
|
|
|
"Delaying start poll: commands in progress");
|
2016-05-09 22:56:09 +02:00
|
|
|
next_topology_timer(dstate);
|
2016-04-24 12:07:13 +02:00
|
|
|
} else
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
bitcoind_get_chaintip(dstate, check_chaintip, NULL);
|
2016-08-09 05:41:22 +02:00
|
|
|
}
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
static void init_topo(struct lightningd_state *dstate,
|
2016-04-24 12:12:18 +02:00
|
|
|
struct bitcoin_block *blk,
|
2016-04-24 12:07:13 +02:00
|
|
|
ptrint_t *p)
|
|
|
|
{
|
2016-04-24 12:16:32 +02:00
|
|
|
struct topology *topo = dstate->topology;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
topo->root = new_block(dstate, blk, NULL);
|
2016-04-24 12:16:32 +02:00
|
|
|
topo->root->height = ptr2int(p);
|
2016-05-04 08:36:19 +02:00
|
|
|
block_map_add(&topo->block_map, topo->root);
|
2016-08-09 05:41:22 +02:00
|
|
|
topo->tip = topo->root;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
chaintopology: only report active chaintip.
getchaintips returns tips even if we don't have the body for them, so
we need to look for the active tip, not just the first (most-work) one.
Here's what happens in the log:
+2849.963414597 lightningd(26779):BROKEN: bitcoin-cli getblock 0000000000000000018626ff7160bdf38a602e6650bd04ec258759ea578b106d false exited 91: 'error code: -32603
error message:
Can't read block from disk
'
And here's an example problematic getchaintips output:
[
{
"height": 419635,
"hash": "0000000000000000000fd32d87fce19efb7ccd07aa4ddaf1b94b9a219deec0f9",
"branchlen": 1,
"status": "headers-only"
},
{
"height": 419634,
"hash": "000000000000000002988d6512719697147cf252b2f64d247cf229266615d2bb",
"branchlen": 0,
"status": "active"
},
{
"height": 416372,
"hash": "0000000000000000004d0a54341c992ae174a91c8dd3981a9f2b3d3f6221ba59",
"branchlen": 1,
"status": "valid-headers"
},
{
"height": 416231,
"hash": "0000000000000000044d0d2c25f33cb48931540366149cde3fb0154f55b58c76",
"branchlen": 1,
"status": "headers-only"
}
]
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2016-07-07 06:06:39 +02:00
|
|
|
/* Now grab chaintip immediately. */
|
|
|
|
bitcoind_get_chaintip(dstate, check_chaintip, NULL);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_init_block(struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *blkid,
|
|
|
|
ptrint_t *blknum)
|
|
|
|
{
|
2016-04-24 12:12:18 +02:00
|
|
|
bitcoind_getrawblock(dstate, blkid, init_topo, blknum);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void get_init_blockhash(struct lightningd_state *dstate, u32 blockcount,
|
|
|
|
void *unused)
|
|
|
|
{
|
|
|
|
u32 start;
|
2016-08-18 06:55:14 +02:00
|
|
|
struct peer *peer;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-11-10 14:23:48 +01:00
|
|
|
/* Start back before any reasonable forks. */
|
|
|
|
if (blockcount < dstate->config.forever_confirms)
|
2016-04-24 12:07:13 +02:00
|
|
|
start = 0;
|
|
|
|
else
|
2016-11-10 14:23:48 +01:00
|
|
|
start = blockcount - dstate->config.forever_confirms;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-08-18 06:55:14 +02:00
|
|
|
/* If loaded from database, go back to earliest possible peer anchor. */
|
|
|
|
list_for_each(&dstate->peers, peer, list) {
|
|
|
|
if (peer->anchor.min_depth && peer->anchor.min_depth < start)
|
|
|
|
start = peer->anchor.min_depth;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:07:13 +02:00
|
|
|
/* Start topology from 100 blocks back. */
|
|
|
|
bitcoind_getblockhash(dstate, start, get_init_block, int2ptr(start));
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
u32 get_tx_mediantime(struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *txid)
|
2016-04-24 12:16:32 +02:00
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *b;
|
2016-04-24 12:16:32 +02:00
|
|
|
|
2016-05-04 08:40:37 +02:00
|
|
|
b = block_for_tx(dstate, txid);
|
2016-05-04 08:36:19 +02:00
|
|
|
if (b)
|
|
|
|
return b->mediantime;
|
2016-04-24 12:16:32 +02:00
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
fatal("Tx %s not found for get_tx_mediantime",
|
|
|
|
tal_hexstr(dstate, txid, sizeof(*txid)));
|
2016-04-24 12:16:32 +02:00
|
|
|
}
|
2016-04-24 12:22:35 +02:00
|
|
|
|
|
|
|
u32 get_tip_mediantime(struct lightningd_state *dstate)
|
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
return dstate->topology->tip->mediantime;
|
2016-04-24 12:22:35 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 23:19:21 +02:00
|
|
|
u32 get_block_height(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
return dstate->topology->tip->height;
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
u64 get_feerate(struct lightningd_state *dstate)
|
|
|
|
{
|
2016-11-06 07:46:58 +01:00
|
|
|
if (dstate->config.override_fee_rate) {
|
|
|
|
log_debug(dstate->base_log,
|
|
|
|
"Forcing fee rate, ignoring estimate");
|
|
|
|
return dstate->config.override_fee_rate;
|
|
|
|
}
|
|
|
|
else if (dstate->topology->feerate == 0) {
|
2016-08-18 06:53:46 +02:00
|
|
|
log_info(dstate->base_log,
|
|
|
|
"No fee estimate: using default fee rate");
|
|
|
|
return dstate->config.default_fee_rate;
|
|
|
|
}
|
|
|
|
return dstate->topology->feerate;
|
|
|
|
}
|
|
|
|
|
2016-09-07 23:34:36 +02:00
|
|
|
struct txlocator *locate_tx(const void *ctx, struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *txid)
|
|
|
|
{
|
|
|
|
struct block *block = block_for_tx(dstate, txid);
|
|
|
|
if (block == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct txlocator *loc = talz(ctx, struct txlocator);
|
|
|
|
loc->blkheight = block->height;
|
|
|
|
|
|
|
|
size_t i, n = tal_count(block->txids);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (structeq(&block->txids[i], txid)){
|
|
|
|
loc->index = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
2016-11-09 07:44:21 +01:00
|
|
|
static void json_dev_broadcast(struct command *cmd,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
jsmntok_t *enabletok;
|
|
|
|
bool enable;
|
|
|
|
|
|
|
|
if (!json_get_params(buffer, params,
|
|
|
|
"enable", &enabletok,
|
|
|
|
NULL)) {
|
|
|
|
command_fail(cmd, "Need enable");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_tok_bool(buffer, enabletok, &enable)) {
|
|
|
|
command_fail(cmd, "enable must be true or false");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug(cmd->dstate->base_log, "dev-broadcast: broadcast %s",
|
|
|
|
enable ? "enabled" : "disabled");
|
|
|
|
cmd->dstate->dev_no_broadcast = !enable;
|
|
|
|
|
|
|
|
/* If enabling, flush and wait. */
|
|
|
|
if (enable)
|
|
|
|
rebroadcast_txs(cmd->dstate, cmd);
|
|
|
|
else
|
2016-11-11 00:02:04 +01:00
|
|
|
command_success(cmd, null_response(cmd));
|
2016-11-09 07:44:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const struct json_command dev_broadcast_command = {
|
|
|
|
"dev-broadcast",
|
|
|
|
json_dev_broadcast,
|
|
|
|
"Pretend we broadcast txs, but don't send to bitcoind",
|
|
|
|
"Returns an empty result on success (waits for flush if enabled)"
|
|
|
|
};
|
|
|
|
|
2016-04-24 12:07:13 +02:00
|
|
|
void setup_topology(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
dstate->topology = tal(dstate, struct topology);
|
|
|
|
block_map_init(&dstate->topology->block_map);
|
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
dstate->topology->startup = true;
|
2016-08-18 06:53:46 +02:00
|
|
|
dstate->topology->feerate = 0;
|
2016-04-24 12:07:13 +02:00
|
|
|
bitcoind_getblockcount(dstate, get_init_blockhash, NULL);
|
2016-08-09 05:41:22 +02:00
|
|
|
|
2016-08-18 06:53:46 +02:00
|
|
|
/* Once it gets topology, it calls io_break() and we return. */
|
2016-08-09 05:41:22 +02:00
|
|
|
io_loop(NULL, NULL);
|
2016-08-18 06:53:46 +02:00
|
|
|
assert(!dstate->topology->startup);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|