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"
|
|
|
|
#include "lightningd.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "timeout.h"
|
|
|
|
#include "watch.h"
|
2016-04-24 12:12:18 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
|
|
|
#include <ccan/asort/asort.h>
|
2016-04-24 12:07:13 +02:00
|
|
|
#include <ccan/ptrint/ptrint.h>
|
|
|
|
#include <ccan/structeq/structeq.h>
|
|
|
|
|
|
|
|
static struct timeout topology_timeout;
|
|
|
|
|
|
|
|
struct tx_in_block {
|
|
|
|
struct list_node list;
|
|
|
|
struct txwatch *w;
|
|
|
|
struct block *block;
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* We can have multiple children. */
|
2016-04-24 12:07:13 +02:00
|
|
|
struct block **nexts;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
struct list_head txs;
|
2016-04-24 12:18:35 +02:00
|
|
|
|
|
|
|
/* Full copy of txs (trimmed to txs list in connect_blocks) */
|
|
|
|
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-04-24 12:07:13 +02:00
|
|
|
struct block **tips;
|
|
|
|
struct sha256_double *newtips;
|
|
|
|
struct block_map block_map;
|
|
|
|
};
|
|
|
|
|
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-04-24 12:18:35 +02:00
|
|
|
static void remove_tx(struct tx_in_block *t)
|
|
|
|
{
|
|
|
|
list_del_from(&t->block->txs, &t->list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_tx_to_block(struct block *b, struct txwatch *w)
|
|
|
|
{
|
|
|
|
/* We attach this to watch, so removed when that is */
|
|
|
|
struct tx_in_block *t = tal(w, struct tx_in_block);
|
|
|
|
|
|
|
|
t->block = b;
|
|
|
|
t->w = w;
|
|
|
|
list_add_tail(&b->txs, &t->list);
|
|
|
|
tal_add_destructor(t, remove_tx);
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
/* Fills in prev, height, mediantime. */
|
2016-04-24 12:18:35 +02:00
|
|
|
static void connect_blocks(struct lightningd_state *dstate, struct block *b)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
2016-04-24 12:18:35 +02:00
|
|
|
const struct topology *topo = dstate->topology;
|
|
|
|
size_t n, i;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* Hooked in already? */
|
|
|
|
if (b->height != -1)
|
2016-04-24 12:12:18 +02:00
|
|
|
return;
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
b->prev = block_map_get(&topo->block_map, &b->hdr.prev_hash);
|
2016-04-24 12:18:35 +02:00
|
|
|
connect_blocks(dstate, b->prev);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
b->height = b->prev->height + 1;
|
|
|
|
n = tal_count(b->prev->nexts);
|
|
|
|
tal_resize(&b->prev->nexts, n+1);
|
|
|
|
b->prev->nexts[n] = b;
|
|
|
|
|
2016-04-24 12:18:35 +02:00
|
|
|
b->mediantime = get_mediantime(topo, b);
|
|
|
|
|
|
|
|
/* 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 txwatch *w;
|
|
|
|
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)
|
|
|
|
txowatch_fire(dstate, txo, tx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We do spends first, in case that tells us to watch tx. */
|
|
|
|
normalized_txid(tx, &txid);
|
|
|
|
w = txwatch_hash_get(&dstate->txwatches, &txid);
|
|
|
|
if (!w) {
|
|
|
|
bitcoin_txid(tx, &txid);
|
|
|
|
w = txwatch_hash_get(&dstate->txwatches, &txid);
|
|
|
|
}
|
|
|
|
if (w) {
|
|
|
|
add_tx_to_block(b, w);
|
|
|
|
/* Fire if it's the first we've seen it: this might
|
|
|
|
* set up txo watches, which could fire in this block */
|
|
|
|
txwatch_fire(dstate, w, 0, &b->blkid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b->full_txs = tal_free(b->full_txs);
|
2016-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is expensive, but reorgs are usually short and txs are few.
|
|
|
|
*
|
|
|
|
* B TX
|
|
|
|
* o--------o-------o
|
|
|
|
* \
|
|
|
|
* \ TX
|
|
|
|
* ------o-------o-------o
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* This counts as depth 1, not 2, since the top fork may be extended.
|
|
|
|
*
|
|
|
|
* B
|
|
|
|
* o--------o-------o
|
|
|
|
* \
|
|
|
|
* \ TX
|
|
|
|
* ------o-------o-------o
|
|
|
|
*
|
|
|
|
* This TX counts as depth 0 by our algorithm, which treats "not in chain"
|
|
|
|
* as "next in chain".
|
|
|
|
*
|
|
|
|
* B
|
|
|
|
* o--------o-------o-------o
|
|
|
|
* \
|
|
|
|
* \ TX
|
|
|
|
* ------o-------o-------o
|
|
|
|
*
|
|
|
|
* This counts as -1.
|
|
|
|
*
|
|
|
|
* We calculate the "height" of a tx (subtraction from best tips gives us the
|
|
|
|
* the depth).
|
|
|
|
*
|
|
|
|
* 1) The height of a tx in a particular branch is the height of the block it
|
|
|
|
* appears in, or the max height + 1 (assuming it's pending).
|
|
|
|
* 2) The overall height of a tx is the maximum height on any branch.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static bool tx_in_block(const struct block *b, const struct txwatch *w)
|
|
|
|
{
|
|
|
|
struct tx_in_block *tx;
|
|
|
|
|
|
|
|
list_for_each(&b->txs, tx, list) {
|
|
|
|
if (tx->w == w)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Cache this on the tips. */
|
|
|
|
static size_t get_tx_branch_height(const struct topology *topo,
|
|
|
|
const struct block *tip,
|
|
|
|
const struct txwatch *w,
|
2016-04-24 12:12:18 +02:00
|
|
|
struct sha256_double *blkid,
|
2016-04-24 12:07:13 +02:00
|
|
|
size_t max)
|
|
|
|
{
|
|
|
|
const struct block *b;
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
for (b = tip; b; b = b->prev) {
|
|
|
|
if (tx_in_block(b, w)) {
|
|
|
|
*blkid = b->blkid;
|
2016-04-24 12:07:13 +02:00
|
|
|
return b->height;
|
2016-04-24 12:12:18 +02:00
|
|
|
}
|
2016-04-24 12:07:13 +02:00
|
|
|
/* Don't bother returning less than max */
|
|
|
|
if (b->height < max)
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tip->height + 1;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
size_t get_tx_depth(struct lightningd_state *dstate, const struct txwatch *w,
|
|
|
|
struct sha256_double *blkid)
|
2016-04-24 12:07:13 +02:00
|
|
|
{
|
|
|
|
const struct topology *topo = dstate->topology;
|
|
|
|
size_t i, max = 0, longest = 0;
|
|
|
|
|
|
|
|
/* Calculate tx height. */
|
|
|
|
for (i = 0; i < tal_count(topo->tips); i++) {
|
2016-04-24 12:12:18 +02:00
|
|
|
size_t h = get_tx_branch_height(topo, topo->tips[i], w, blkid,
|
|
|
|
max);
|
2016-04-24 12:07:13 +02:00
|
|
|
if (h > max)
|
|
|
|
max = h;
|
|
|
|
|
|
|
|
/* Grab longest tip while we're here. */
|
|
|
|
if (topo->tips[i]->height > longest)
|
|
|
|
longest = topo->tips[i]->height;
|
|
|
|
}
|
|
|
|
|
|
|
|
return longest + 1 - max;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void reevaluate_txs_from(struct lightningd_state *dstate,
|
|
|
|
struct block *common,
|
|
|
|
struct block *b)
|
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
size_t i;
|
|
|
|
struct tx_in_block *tx;
|
|
|
|
|
|
|
|
/* Careful! Callbacks could cause arbitrary txs to be deleted. */
|
|
|
|
again:
|
|
|
|
b->tx_deleted = false;
|
|
|
|
|
|
|
|
list_for_each(&b->txs, tx, list) {
|
|
|
|
size_t dist = get_tx_distance(common, tx->w);
|
|
|
|
int depth;
|
|
|
|
|
|
|
|
/* Worst case, distance is one past tips. */
|
|
|
|
depth = topo->tips[0]->height - (b->height + dist);
|
|
|
|
assert(depth >= -1);
|
|
|
|
|
|
|
|
#if 0 /* When we replace notifications */
|
|
|
|
if (tx->w->depth != depth) {
|
|
|
|
tx->w->depth = depth;
|
|
|
|
tx->w->cb(tx->w->peer, depth, tx->w->cbdata);
|
|
|
|
if (b->tx_deleted)
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < tal_count(b->nexts); i++)
|
|
|
|
reevaluate_txs_from(dstate, common, b->nexts[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct block *find_common(struct topology *topo,
|
|
|
|
struct block *a, struct block *b)
|
|
|
|
{
|
|
|
|
/* Special case for first time, when we have no previous tips */
|
|
|
|
if (!a)
|
|
|
|
return b;
|
|
|
|
|
|
|
|
/* Get to same height to start. */
|
|
|
|
while (a->height > b->height)
|
|
|
|
a = block_map_get(&topo->block_map, &a->prevblkid);
|
|
|
|
while (b->height > a->height)
|
|
|
|
b = block_map_get(&topo->block_map, &b->prevblkid);
|
|
|
|
|
|
|
|
while (a != b) {
|
|
|
|
a = block_map_get(&topo->block_map, &a->prevblkid);
|
|
|
|
b = block_map_get(&topo->block_map, &b->prevblkid);
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void topology_changed(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
struct block *common = NULL;
|
|
|
|
|
|
|
|
/* topo->tips is NULL for very first time. */
|
|
|
|
if (topo->tips) {
|
|
|
|
for (i = 0; i < tal_count(topo->tips); i++)
|
|
|
|
common = find_common(topo, common, topo->tips[i]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tal_free(topo->tips);
|
|
|
|
topo->tips = tal_arr(topo, struct block *, tal_count(topo->newtips));
|
2016-04-24 12:12:18 +02:00
|
|
|
for (i = 0; i < tal_count(topo->newtips); i++) {
|
|
|
|
topo->tips[i] = block_map_get(&topo->block_map,
|
|
|
|
&topo->newtips[i]);
|
2016-04-24 12:18:35 +02:00
|
|
|
connect_blocks(dstate, topo->tips[i]);
|
2016-04-24 12:12:18 +02:00
|
|
|
}
|
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-04-24 12:07:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct block *add_block(struct lightningd_state *dstate,
|
2016-04-24 12:12:18 +02:00
|
|
|
struct bitcoin_block *blk)
|
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));
|
|
|
|
log_debug(dstate->base_log, "Adding block %02x%02x%02x%02x...\n",
|
|
|
|
b->blkid.sha.u.u8[0],
|
|
|
|
b->blkid.sha.u.u8[1],
|
|
|
|
b->blkid.sha.u.u8[2],
|
|
|
|
b->blkid.sha.u.u8[3]);
|
|
|
|
assert(!block_map_get(&topo->block_map, &b->blkid));
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* We fill these out in topology_changed */
|
|
|
|
b->height = -1;
|
|
|
|
b->nexts = tal_arr(b, struct block *, 0);
|
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
|
|
|
|
|
|
|
list_head_init(&b->txs);
|
2016-04-24 12:18:35 +02:00
|
|
|
b->full_txs = tal_steal(b, blk->tx);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
block_map_add(&topo->block_map, b);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gather_blocks(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)
|
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
ptrdiff_t i;
|
|
|
|
|
2016-04-24 12:12:18 +02:00
|
|
|
add_block(dstate, blk);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* Recurse if we need prev. */
|
2016-04-24 12:12:18 +02:00
|
|
|
if (!block_map_get(&topo->block_map, &blk->hdr.prev_hash)) {
|
|
|
|
bitcoind_getrawblock(dstate, &blk->hdr.prev_hash,
|
|
|
|
gather_blocks, p);
|
2016-04-24 12:07:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recurse if more tips to map. */
|
|
|
|
for (i = ptr2int(p) + 1; i < tal_count(topo->newtips); i++) {
|
|
|
|
if (!block_map_get(&topo->block_map, &topo->newtips[i])) {
|
2016-04-24 12:12:18 +02:00
|
|
|
bitcoind_getrawblock(dstate, &topo->newtips[i],
|
|
|
|
gather_blocks, int2ptr(i));
|
2016-04-24 12:07:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All done. */
|
|
|
|
topology_changed(dstate);
|
|
|
|
|
|
|
|
refresh_timeout(dstate, &topology_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool tips_changed(struct sha256_double *blockids, struct block **tips)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* First time */
|
|
|
|
if (!tips)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (tal_count(blockids) != tal_count(tips))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (i = 0; i < tal_count(tips); i++)
|
|
|
|
if (!structeq(&blockids[i], &tips[i]->blkid))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void check_chaintips(struct lightningd_state *dstate,
|
|
|
|
struct sha256_double *blockids,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
|
|
|
|
/* We assume chaintip ordering: if we're wrong, it's just slow */
|
|
|
|
if (!tips_changed(blockids, topo->tips))
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Start iterating on first unknown tip. */
|
|
|
|
topo->newtips = tal_steal(topo, blockids);
|
|
|
|
for (i = 0; i < tal_count(topo->newtips); i++) {
|
|
|
|
if (block_map_get(&topo->block_map, &topo->newtips[i]))
|
|
|
|
continue;
|
2016-04-24 12:12:18 +02:00
|
|
|
bitcoind_getrawblock(dstate, &topo->newtips[i], gather_blocks,
|
|
|
|
int2ptr(i));
|
2016-04-24 12:07:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_unusual(dstate->base_log, "Chaintips changed but all blocks known?");
|
|
|
|
topology_changed(dstate);
|
|
|
|
|
|
|
|
out:
|
|
|
|
refresh_timeout(dstate, &topology_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void start_poll_chaintips(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
if (!list_empty(&dstate->bitcoin_req)) {
|
|
|
|
log_unusual(dstate->base_log,
|
|
|
|
"Delaying start poll: commands in progress");
|
|
|
|
refresh_timeout(dstate, &topology_timeout);
|
|
|
|
} else
|
|
|
|
bitcoind_get_chaintips(dstate, check_chaintips, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-24 12:16:32 +02:00
|
|
|
topo->root = add_block(dstate, blk);
|
|
|
|
topo->root->height = ptr2int(p);
|
2016-04-24 12:07:13 +02:00
|
|
|
|
|
|
|
/* Now grab chaintips immediately. */
|
|
|
|
bitcoind_get_chaintips(dstate, check_chaintips, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (blockcount < 100)
|
|
|
|
start = 0;
|
|
|
|
else
|
|
|
|
start = blockcount - 100;
|
|
|
|
|
|
|
|
/* Start topology from 100 blocks back. */
|
|
|
|
bitcoind_getblockhash(dstate, start, get_init_block, int2ptr(start));
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:16:32 +02:00
|
|
|
/* FIXME: Don't brute force this for the normal case! */
|
|
|
|
static void last_mediantime(const struct block *b,
|
|
|
|
const struct txwatch *w,
|
|
|
|
u32 *mediantime)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (tx_in_block(b, w)) {
|
|
|
|
if (b->mediantime > *mediantime)
|
|
|
|
*mediantime = b->mediantime;
|
|
|
|
/* Can't be in a later block */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < tal_count(b->nexts); i++)
|
|
|
|
last_mediantime(b->nexts[i], w, mediantime);
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 get_last_mediantime(struct lightningd_state *dstate,
|
|
|
|
const struct sha256_double *txid)
|
|
|
|
{
|
|
|
|
struct topology *topo = dstate->topology;
|
|
|
|
struct txwatch *w;
|
|
|
|
u32 mediantime = 0;
|
|
|
|
|
|
|
|
w = txwatch_hash_get(&dstate->txwatches, txid);
|
|
|
|
|
|
|
|
last_mediantime(topo->root, w, &mediantime);
|
|
|
|
return mediantime;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:07:13 +02:00
|
|
|
void setup_topology(struct lightningd_state *dstate)
|
|
|
|
{
|
|
|
|
dstate->topology = tal(dstate, struct topology);
|
|
|
|
dstate->topology->tips = NULL;
|
|
|
|
dstate->topology->newtips = NULL;
|
|
|
|
block_map_init(&dstate->topology->block_map);
|
|
|
|
|
|
|
|
init_timeout(&topology_timeout, dstate->config.poll_seconds,
|
|
|
|
start_poll_chaintips, dstate);
|
|
|
|
bitcoind_getblockcount(dstate, get_init_blockhash, NULL);
|
|
|
|
}
|