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
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void start_poll_chaintip(struct topology *topo);
|
2016-05-09 22:56:09 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void next_topology_timer(struct topology *topo)
|
2016-05-09 22:56:09 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
if (topo->startup) {
|
|
|
|
topo->startup = false;
|
|
|
|
io_break(topo);
|
2016-08-18 06:53:46 +02:00
|
|
|
}
|
2017-03-02 13:21:49 +01:00
|
|
|
new_reltimer(topo->timers, topo, topo->poll_time,
|
|
|
|
start_poll_chaintip, topo);
|
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. */
|
2016-12-11 23:22:08 +01:00
|
|
|
static void add_tx_to_block(struct block *b, const struct sha256_double *txid, const u32 txnum)
|
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);
|
2016-12-11 23:22:08 +01:00
|
|
|
tal_resize(&b->txnums, n+1);
|
2016-05-04 08:40:37 +02:00
|
|
|
b->txids[n] = *txid;
|
2016-12-11 23:22:08 +01:00
|
|
|
b->txnums[n] = txnum;
|
2016-04-24 12:18:35 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static bool we_broadcast(const struct topology *topo,
|
2016-05-04 08:41:16 +02:00
|
|
|
const struct sha256_double *txid)
|
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
const struct outgoing_tx *otx;
|
2016-05-04 08:41:16 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
list_for_each(&topo->outgoing_txs, otx, list) {
|
|
|
|
if (structeq(&otx->txid, txid))
|
|
|
|
return true;
|
2016-05-04 08:41:16 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
/* Fills in prev, height, mediantime. */
|
2017-03-02 13:21:49 +01:00
|
|
|
static void connect_block(struct topology *topo,
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *prev,
|
|
|
|
struct block *b)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
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. */
|
2017-01-25 00:35:43 +01:00
|
|
|
for (j = 0; j < tal_count(tx->input); j++) {
|
2016-04-24 12:18:35 +02:00
|
|
|
struct txwatch_output out;
|
|
|
|
struct txowatch *txo;
|
|
|
|
out.txid = tx->input[j].txid;
|
|
|
|
out.index = tx->input[j].index;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
txo = txowatch_hash_get(&topo->txowatches, &out);
|
2016-04-24 12:18:35 +02:00
|
|
|
if (txo)
|
2017-03-02 13:21:49 +01:00
|
|
|
txowatch_fire(topo, 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);
|
2017-03-02 13:21:49 +01:00
|
|
|
if (watching_txid(topo, &txid) || we_broadcast(topo, &txid))
|
2016-12-11 23:22:08 +01:00
|
|
|
add_tx_to_block(b, &txid, i);
|
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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
notify_new_block(topo, 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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
static struct block *block_for_tx(const struct topology *topo,
|
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: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
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
size_t get_tx_depth(const struct topology *topo,
|
2016-05-04 08:40:37 +02:00
|
|
|
const struct sha256_double *txid)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-05-04 08:36:19 +02:00
|
|
|
const struct block *b;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
b = block_for_tx(topo, 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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
static void broadcast_remainder(struct bitcoind *bitcoind,
|
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"))
|
2017-03-02 13:21:49 +01:00
|
|
|
log_debug(bitcoind->log,
|
2016-05-04 08:33:10 +02:00
|
|
|
"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)
|
2017-03-02 13:21:49 +01:00
|
|
|
log_unusual(bitcoind->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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_sendrawtx(NULL, bitcoind, txs->txs[txs->cursor],
|
2016-11-09 07:44:10 +01:00
|
|
|
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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
static void rebroadcast_txs(struct topology *topo, 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;
|
2017-03-02 13:21:49 +01:00
|
|
|
struct outgoing_tx *otx;
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
if (topo->dev_no_broadcast)
|
2016-11-09 07:44:21 +01:00
|
|
|
return;
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
txs = tal(topo, struct txs_to_broadcast);
|
2016-11-09 07:44:21 +01:00
|
|
|
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);
|
2017-03-02 13:21:49 +01:00
|
|
|
list_for_each(&topo->outgoing_txs, otx, list) {
|
2017-03-02 13:21:49 +01:00
|
|
|
if (block_for_tx(topo, &otx->txid))
|
2017-03-02 13:21:49 +01:00
|
|
|
continue;
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
tal_resize(&txs->txs, num_txs+1);
|
|
|
|
txs->txs[num_txs] = tal_strdup(txs, otx->hextx);
|
|
|
|
num_txs++;
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2016-11-09 07:44:10 +01:00
|
|
|
/* Let this do the dirty work. */
|
|
|
|
txs->cursor = (size_t)-1;
|
2017-03-02 13:21:49 +01:00
|
|
|
broadcast_remainder(topo->bitcoind, 0, "", txs);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_outgoing_tx(struct outgoing_tx *otx)
|
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
list_del(&otx->list);
|
2016-11-07 13:29:02 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void broadcast_done(struct bitcoind *bitcoind,
|
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
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
struct lightningd_state *dstate = tal_parent(bitcoind);
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
|
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 */
|
2017-03-02 13:21:49 +01:00
|
|
|
list_add_tail(&topo->outgoing_txs, &otx->list);
|
2016-11-07 13:31:02 +01:00
|
|
|
tal_add_destructor(otx, destroy_outgoing_tx);
|
|
|
|
}
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
void broadcast_tx(struct topology *topo,
|
|
|
|
struct peer *peer, const struct bitcoin_tx *tx,
|
2016-11-07 13:31:02 +01:00
|
|
|
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);
|
2017-01-10 05:49:25 +01:00
|
|
|
otx->hextx = tal_hex(otx, 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
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
log_add_struct(topo->bitcoind->log,
|
|
|
|
" (tx %s)", struct sha256_double, &otx->txid);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
if (topo->dev_no_broadcast)
|
2017-03-02 13:21:49 +01:00
|
|
|
broadcast_done(topo->bitcoind, 0, "dev_no_broadcast", otx);
|
2016-11-09 07:44:21 +01:00
|
|
|
else
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_sendrawtx(peer, topo->bitcoind, otx->hextx,
|
2016-11-09 07:44:21 +01:00
|
|
|
broadcast_done, otx);
|
2016-05-04 08:33:10 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void free_blocks(struct topology *topo, 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++)
|
2017-03-02 13:21:49 +01:00
|
|
|
txwatch_fire(topo, &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
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void update_fee(struct bitcoind *bitcoind, u64 rate, u64 *feerate)
|
2016-08-18 06:53:46 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
log_debug(bitcoind->log, "Feerate %"PRIu64" -> %"PRIu64,
|
2016-08-18 06:53:46 +02:00
|
|
|
rate, *feerate);
|
|
|
|
*feerate = rate;
|
|
|
|
}
|
|
|
|
|
2016-05-04 08:36:19 +02:00
|
|
|
/* B is the new chain (linked by ->next); update topology */
|
2017-03-02 13:21:49 +01:00
|
|
|
static void topology_changed(struct topology *topo,
|
2016-05-04 08:36:19 +02:00
|
|
|
struct block *prev,
|
|
|
|
struct block *b)
|
|
|
|
{
|
|
|
|
/* Eliminate any old chain. */
|
|
|
|
if (prev->next)
|
2017-03-02 13:21:49 +01:00
|
|
|
free_blocks(topo, prev->next);
|
2016-05-04 08:36:19 +02:00
|
|
|
|
|
|
|
prev->next = b;
|
|
|
|
do {
|
2017-03-02 13:21:49 +01:00
|
|
|
connect_block(topo, prev, b);
|
|
|
|
topo->tip = prev = b;
|
2016-05-04 08:36:19 +02:00
|
|
|
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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
watch_topology_changed(topo);
|
2016-05-04 08:33:10 +02:00
|
|
|
|
|
|
|
/* Maybe need to rebroadcast. */
|
2017-03-02 13:21:49 +01:00
|
|
|
rebroadcast_txs(topo, NULL);
|
2016-08-18 06:53:46 +02:00
|
|
|
|
|
|
|
/* Once per new block head, update fee estimate. */
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_estimate_fee(topo->bitcoind, update_fee, &topo->feerate);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static struct block *new_block(struct topology *topo,
|
2016-05-04 08:36:19 +02:00
|
|
|
struct bitcoin_block *blk,
|
|
|
|
struct block *next)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
struct block *b = tal(topo, struct block);
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
sha256_double(&b->blkid, &blk->hdr, sizeof(blk->hdr));
|
2017-03-02 13:21:49 +01:00
|
|
|
log_debug_struct(topo->bitcoind->log, "Adding block %s",
|
2016-06-28 23:19:21 +02:00
|
|
|
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-12-11 23:22:08 +01:00
|
|
|
b->txnums = tal_arr(b, u32, 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;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void gather_blocks(struct bitcoind *bitcoind,
|
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
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
struct lightningd_state *dstate = tal_parent(bitcoind);
|
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
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
b = new_block(topo, 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) {
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_getrawblock(dstate->bitcoind, &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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
topology_changed(topo, prev, b);
|
|
|
|
next_topology_timer(topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void check_chaintip(struct bitcoind *bitcoind,
|
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
|
|
|
const struct sha256_double *tipid,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
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))
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_getrawblock(bitcoind, tipid, gather_blocks,
|
2016-05-04 08:36:19 +02:00
|
|
|
(struct block *)NULL);
|
|
|
|
else
|
2016-05-09 22:56:09 +02:00
|
|
|
/* Next! */
|
2017-03-02 13:21:49 +01:00
|
|
|
next_topology_timer(topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void start_poll_chaintip(struct topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
if (!list_empty(&topo->bitcoind->pending)) {
|
|
|
|
log_unusual(topo->bitcoind->log,
|
2016-04-24 12:07:13 +02:00
|
|
|
"Delaying start poll: commands in progress");
|
2017-03-02 13:21:49 +01:00
|
|
|
next_topology_timer(topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
} else
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_get_chaintip(topo->bitcoind, check_chaintip, topo);
|
2016-08-09 05:41:22 +02:00
|
|
|
}
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void init_topo(struct bitcoind *bitcoind,
|
2016-04-24 12:12:18 +02:00
|
|
|
struct bitcoin_block *blk,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
topo->root = new_block(topo, blk, NULL);
|
|
|
|
topo->root->height = topo->first_blocknum;
|
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. */
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_get_chaintip(bitcoind, check_chaintip, topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void get_init_block(struct bitcoind *bitcoind,
|
2016-04-24 12:07:13 +02:00
|
|
|
const struct sha256_double *blkid,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_getrawblock(bitcoind, blkid, init_topo, topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
static void get_init_blockhash(struct bitcoind *bitcoind, u32 blockcount,
|
2017-03-02 13:21:49 +01:00
|
|
|
struct topology *topo)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-11-10 14:23:48 +01:00
|
|
|
/* Start back before any reasonable forks. */
|
2017-03-02 13:21:49 +01:00
|
|
|
if (blockcount < 100)
|
|
|
|
topo->first_blocknum = 0;
|
|
|
|
else if (!topo->first_blocknum || blockcount - 100 < topo->first_blocknum)
|
|
|
|
topo->first_blocknum = blockcount - 100;
|
2016-08-18 06:55:14 +02:00
|
|
|
|
2016-04-24 12:07:13 +02:00
|
|
|
/* Start topology from 100 blocks back. */
|
2017-03-02 13:21:49 +01:00
|
|
|
bitcoind_getblockhash(bitcoind, topo->first_blocknum,
|
|
|
|
get_init_block, topo);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
u32 get_tx_mediantime(const struct topology *topo,
|
2016-05-04 08:36:19 +02:00
|
|
|
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
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
b = block_for_tx(topo, 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",
|
2017-03-02 13:21:49 +01:00
|
|
|
tal_hexstr(topo, txid, sizeof(*txid)));
|
2016-04-24 12:16:32 +02:00
|
|
|
}
|
2016-04-24 12:22:35 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
u32 get_tip_mediantime(const struct topology *topo)
|
2016-04-24 12:22:35 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
return topo->tip->mediantime;
|
2016-04-24 12:22:35 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
u32 get_block_height(const struct topology *topo)
|
2016-06-28 23:19:21 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
return topo->tip->height;
|
2016-06-28 23:19:21 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct txlocator *locate_tx(const void *ctx, const struct topology *topo,
|
2016-09-07 23:34:36 +02:00
|
|
|
const struct sha256_double *txid)
|
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
struct block *block = block_for_tx(topo, txid);
|
2016-09-07 23:34:36 +02:00
|
|
|
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)){
|
2016-12-11 23:22:08 +01:00
|
|
|
loc->index = block->txnums[i];
|
|
|
|
return loc;
|
2016-09-07 23:34:36 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 23:22:08 +01:00
|
|
|
return tal_free(loc);
|
2016-09-07 23:34:36 +02:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
void json_dev_broadcast(struct command *cmd,
|
|
|
|
struct topology *topo,
|
|
|
|
const char *buffer, const jsmntok_t *params)
|
2016-11-09 07:44:21 +01:00
|
|
|
{
|
|
|
|
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");
|
2017-03-02 13:21:49 +01:00
|
|
|
cmd->dstate->topology->dev_no_broadcast = !enable;
|
2016-11-09 07:44:21 +01:00
|
|
|
|
|
|
|
/* If enabling, flush and wait. */
|
|
|
|
if (enable)
|
2017-03-02 13:21:49 +01:00
|
|
|
rebroadcast_txs(cmd->dstate->topology, cmd);
|
2016-11-09 07:44:21 +01:00
|
|
|
else
|
2016-11-11 00:02:04 +01:00
|
|
|
command_success(cmd, null_response(cmd));
|
2016-11-09 07:44:21 +01:00
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
/* On shutdown, peers get deleted last. That frees from our list, so
|
|
|
|
* do it now instead. */
|
|
|
|
static void destroy_outgoing_txs(struct topology *topo)
|
|
|
|
{
|
|
|
|
struct outgoing_tx *otx;
|
|
|
|
|
|
|
|
while ((otx = list_pop(&topo->outgoing_txs, struct outgoing_tx, list)))
|
|
|
|
tal_free(otx);
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
struct topology *new_topology(const tal_t *ctx)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2017-03-02 13:21:49 +01:00
|
|
|
struct topology *topo = tal(ctx, struct topology);
|
|
|
|
|
|
|
|
block_map_init(&topo->block_map);
|
|
|
|
list_head_init(&topo->outgoing_txs);
|
|
|
|
txwatch_hash_init(&topo->txwatches);
|
|
|
|
txowatch_hash_init(&topo->txowatches);
|
2017-03-02 13:21:49 +01:00
|
|
|
topo->dev_no_broadcast = false;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
return topo;
|
|
|
|
}
|
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
void setup_topology(struct topology *topo, struct bitcoind *bitcoind,
|
|
|
|
struct timers *timers,
|
|
|
|
struct timerel poll_time, u32 first_peer_block)
|
2017-03-02 13:21:49 +01:00
|
|
|
{
|
|
|
|
topo->startup = true;
|
|
|
|
topo->feerate = 0;
|
2017-03-02 13:21:49 +01:00
|
|
|
topo->timers = timers;
|
|
|
|
topo->bitcoind = bitcoind;
|
|
|
|
topo->poll_time = poll_time;
|
|
|
|
topo->first_blocknum = first_peer_block;
|
|
|
|
|
|
|
|
bitcoind_getblockcount(bitcoind, get_init_blockhash, topo);
|
2016-08-09 05:41:22 +02:00
|
|
|
|
2017-03-02 13:21:49 +01:00
|
|
|
tal_add_destructor(topo, destroy_outgoing_txs);
|
2017-03-02 13:21:49 +01: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);
|
2017-03-02 13:21:49 +01:00
|
|
|
assert(!topo->startup);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|