daemon/chaintopology.c: remove last remaining lightningd_state references.

We put a topology pointer into struct outgoing_tx and struct block for now.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-03-02 22:51:49 +10:30
parent 38cc6c2f21
commit e2c7925e0e
7 changed files with 80 additions and 66 deletions

View file

@ -237,15 +237,12 @@ static void broadcast_done(struct bitcoind *bitcoind,
int exitstatus, const char *msg, int exitstatus, const char *msg,
struct outgoing_tx *otx) struct outgoing_tx *otx)
{ {
struct lightningd_state *dstate = tal_parent(bitcoind);
struct topology *topo = dstate->topology;
if (otx->failed && exitstatus != 0) { if (otx->failed && exitstatus != 0) {
otx->failed(otx->peer, exitstatus, msg); otx->failed(otx->peer, exitstatus, msg);
tal_free(otx); tal_free(otx);
} else { } else {
/* For continual rebroadcasting */ /* For continual rebroadcasting */
list_add_tail(&topo->outgoing_txs, &otx->list); list_add_tail(&otx->topo->outgoing_txs, &otx->list);
tal_add_destructor(otx, destroy_outgoing_tx); tal_add_destructor(otx, destroy_outgoing_tx);
} }
} }
@ -262,9 +259,10 @@ void broadcast_tx(struct topology *topo,
bitcoin_txid(tx, &otx->txid); bitcoin_txid(tx, &otx->txid);
otx->hextx = tal_hex(otx, rawtx); otx->hextx = tal_hex(otx, rawtx);
otx->failed = failed; otx->failed = failed;
otx->topo = topo;
tal_free(rawtx); tal_free(rawtx);
log_add_struct(topo->bitcoind->log, log_add_struct(topo->log,
" (tx %s)", struct sha256_double, &otx->txid); " (tx %s)", struct sha256_double, &otx->txid);
if (topo->dev_no_broadcast) if (topo->dev_no_broadcast)
@ -291,11 +289,12 @@ static void free_blocks(struct topology *topo, struct block *b)
} }
} }
static void update_fee(struct bitcoind *bitcoind, u64 rate, u64 *feerate) static void update_fee(struct bitcoind *bitcoind, u64 rate,
struct topology *topo)
{ {
log_debug(bitcoind->log, "Feerate %"PRIu64" -> %"PRIu64, log_debug(topo->log, "Feerate %"PRIu64" (was %"PRIu64")",
rate, *feerate); rate, topo->feerate);
*feerate = rate; topo->feerate = rate;
} }
/* B is the new chain (linked by ->next); update topology */ /* B is the new chain (linked by ->next); update topology */
@ -321,7 +320,7 @@ static void topology_changed(struct topology *topo,
rebroadcast_txs(topo, NULL); rebroadcast_txs(topo, NULL);
/* Once per new block head, update fee estimate. */ /* Once per new block head, update fee estimate. */
bitcoind_estimate_fee(topo->bitcoind, update_fee, &topo->feerate); bitcoind_estimate_fee(topo->bitcoind, update_fee, topo);
} }
static struct block *new_block(struct topology *topo, static struct block *new_block(struct topology *topo,
@ -331,10 +330,11 @@ static struct block *new_block(struct topology *topo,
struct block *b = tal(topo, struct block); struct block *b = tal(topo, struct block);
sha256_double(&b->blkid, &blk->hdr, sizeof(blk->hdr)); sha256_double(&b->blkid, &blk->hdr, sizeof(blk->hdr));
log_debug_struct(topo->bitcoind->log, "Adding block %s", log_debug_struct(topo->log, "Adding block %s",
struct sha256_double, &b->blkid); struct sha256_double, &b->blkid);
assert(!block_map_get(&topo->block_map, &b->blkid)); assert(!block_map_get(&topo->block_map, &b->blkid));
b->next = next; b->next = next;
b->topo = topo;
/* We fill these out in topology_changed */ /* We fill these out in topology_changed */
b->height = -1; b->height = -1;
@ -350,12 +350,23 @@ static struct block *new_block(struct topology *topo,
return b; return b;
} }
static void gather_blocks(struct bitcoind *bitcoind, static void add_block(struct bitcoind *bitcoind,
struct bitcoin_block *blk, struct topology *topo,
struct block *next) struct bitcoin_block *blk,
struct block *next);
static void gather_previous_blocks(struct bitcoind *bitcoind,
struct bitcoin_block *blk,
struct block *next)
{
add_block(bitcoind, next->topo, blk, next);
}
static void add_block(struct bitcoind *bitcoind,
struct topology *topo,
struct bitcoin_block *blk,
struct block *next)
{ {
struct lightningd_state *dstate = tal_parent(bitcoind);
struct topology *topo = dstate->topology;
struct block *b, *prev; struct block *b, *prev;
b = new_block(topo, blk, next); b = new_block(topo, blk, next);
@ -363,8 +374,8 @@ static void gather_blocks(struct bitcoind *bitcoind,
/* Recurse if we need prev. */ /* Recurse if we need prev. */
prev = block_map_get(&topo->block_map, &blk->hdr.prev_hash); prev = block_map_get(&topo->block_map, &blk->hdr.prev_hash);
if (!prev) { if (!prev) {
bitcoind_getrawblock(dstate->bitcoind, &blk->hdr.prev_hash, bitcoind_getrawblock(bitcoind, &blk->hdr.prev_hash,
gather_blocks, b); gather_previous_blocks, b);
return; return;
} }
@ -373,14 +384,20 @@ static void gather_blocks(struct bitcoind *bitcoind,
next_topology_timer(topo); next_topology_timer(topo);
} }
static void rawblock_tip(struct bitcoind *bitcoind,
struct bitcoin_block *blk,
struct topology *topo)
{
add_block(bitcoind, topo, blk, NULL);
}
static void check_chaintip(struct bitcoind *bitcoind, static void check_chaintip(struct bitcoind *bitcoind,
const struct sha256_double *tipid, const struct sha256_double *tipid,
struct topology *topo) struct topology *topo)
{ {
/* 0 is the main tip. */ /* 0 is the main tip. */
if (!structeq(tipid, &topo->tip->blkid)) if (!structeq(tipid, &topo->tip->blkid))
bitcoind_getrawblock(bitcoind, tipid, gather_blocks, bitcoind_getrawblock(bitcoind, tipid, rawblock_tip, topo);
(struct block *)NULL);
else else
/* Next! */ /* Next! */
next_topology_timer(topo); next_topology_timer(topo);
@ -389,7 +406,7 @@ static void check_chaintip(struct bitcoind *bitcoind,
static void start_poll_chaintip(struct topology *topo) static void start_poll_chaintip(struct topology *topo)
{ {
if (!list_empty(&topo->bitcoind->pending)) { if (!list_empty(&topo->bitcoind->pending)) {
log_unusual(topo->bitcoind->log, log_unusual(topo->log,
"Delaying start poll: commands in progress"); "Delaying start poll: commands in progress");
next_topology_timer(topo); next_topology_timer(topo);
} else } else
@ -453,19 +470,17 @@ u32 get_block_height(const struct topology *topo)
return topo->tip->height; return topo->tip->height;
} }
u64 get_feerate(struct lightningd_state *dstate) u64 get_feerate(const struct topology *topo)
{ {
if (dstate->config.override_fee_rate) { if (topo->override_fee_rate) {
log_debug(dstate->base_log, log_debug(topo->log, "Forcing fee rate, ignoring estimate");
"Forcing fee rate, ignoring estimate"); return topo->override_fee_rate;
return dstate->config.override_fee_rate;
} }
else if (dstate->topology->feerate == 0) { else if (topo->feerate == 0) {
log_info(dstate->base_log, log_info(topo->log, "No fee estimate: using default fee rate");
"No fee estimate: using default fee rate"); return topo->default_fee_rate;
return dstate->config.default_fee_rate;
} }
return dstate->topology->feerate; return topo->feerate;
} }
struct txlocator *locate_tx(const void *ctx, const struct topology *topo, struct txlocator *locate_tx(const void *ctx, const struct topology *topo,
@ -528,7 +543,7 @@ static void destroy_outgoing_txs(struct topology *topo)
tal_free(otx); tal_free(otx);
} }
struct topology *new_topology(const tal_t *ctx) struct topology *new_topology(const tal_t *ctx, struct log *log)
{ {
struct topology *topo = tal(ctx, struct topology); struct topology *topo = tal(ctx, struct topology);
@ -536,6 +551,9 @@ struct topology *new_topology(const tal_t *ctx)
list_head_init(&topo->outgoing_txs); list_head_init(&topo->outgoing_txs);
txwatch_hash_init(&topo->txwatches); txwatch_hash_init(&topo->txwatches);
txowatch_hash_init(&topo->txowatches); txowatch_hash_init(&topo->txowatches);
topo->log = log;
topo->default_fee_rate = 40000;
topo->override_fee_rate = 0;
topo->dev_no_broadcast = false; topo->dev_no_broadcast = false;
return topo; return topo;

View file

@ -26,6 +26,8 @@ struct outgoing_tx {
const char *hextx; const char *hextx;
struct sha256_double txid; struct sha256_double txid;
void (*failed)(struct peer *peer, int exitstatus, const char *err); void (*failed)(struct peer *peer, int exitstatus, const char *err);
/* FIXME: Remove this. */
struct topology *topo;
}; };
struct block { struct block {
@ -54,6 +56,9 @@ struct block {
/* Full copy of txs (trimmed to txs list in connect_block) */ /* Full copy of txs (trimmed to txs list in connect_block) */
struct bitcoin_tx **full_txs; struct bitcoin_tx **full_txs;
/* FIXME: Remove this. */
struct topology *topo;
}; };
/* Hash blocks by sha */ /* Hash blocks by sha */
@ -83,6 +88,9 @@ struct topology {
u64 feerate; u64 feerate;
bool startup; bool startup;
/* Where to log things. */
struct log *log;
/* How far back (in blocks) to go. */ /* How far back (in blocks) to go. */
unsigned int first_blocknum; unsigned int first_blocknum;
@ -98,6 +106,12 @@ struct topology {
/* Bitcoin transctions we're broadcasting */ /* Bitcoin transctions we're broadcasting */
struct list_head outgoing_txs; struct list_head outgoing_txs;
/* Force a partiular fee rate regardless of estimatefee (satoshis/kb) */
u64 override_fee_rate;
/* What fee we use if estimatefee fails (satoshis/kb) */
u64 default_fee_rate;
/* Transactions/txos we are watching. */ /* Transactions/txos we are watching. */
struct txwatch_hash txwatches; struct txwatch_hash txwatches;
struct txowatch_hash txowatches; struct txowatch_hash txowatches;
@ -132,7 +146,7 @@ u32 get_tip_mediantime(const struct topology *topo);
u32 get_block_height(const struct topology *topo); u32 get_block_height(const struct topology *topo);
/* Get fee rate. */ /* Get fee rate. */
u64 get_feerate(struct lightningd_state *dstate); u64 get_feerate(const struct topology *topo);
/* Broadcast a single tx, and rebroadcast as reqd (copies tx). /* Broadcast a single tx, and rebroadcast as reqd (copies tx).
* If failed is non-NULL, call that and don't rebroadcast. */ * If failed is non-NULL, call that and don't rebroadcast. */
@ -142,7 +156,7 @@ void broadcast_tx(struct topology *topo,
int exitstatus, int exitstatus,
const char *err)); const char *err));
struct topology *new_topology(const tal_t *ctx); struct topology *new_topology(const tal_t *ctx, struct log *log);
void setup_topology(struct topology *topology, struct bitcoind *bitcoind, void setup_topology(struct topology *topology, struct bitcoind *bitcoind,
struct timers *timers, struct timers *timers,
struct timerel poll_time, u32 first_peer_block); struct timerel poll_time, u32 first_peer_block);

View file

@ -80,7 +80,7 @@ int main(int argc, char *argv[])
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN); | SECP256K1_CONTEXT_SIGN);
dstate->topology = new_topology(dstate); dstate->topology = new_topology(dstate, dstate->base_log);
dstate->bitcoind = new_bitcoind(dstate, dstate->base_log); dstate->bitcoind = new_bitcoind(dstate, dstate->base_log);
/* Handle options and config; move to .lightningd */ /* Handle options and config; move to .lightningd */

View file

@ -37,12 +37,6 @@ struct config {
/* Percent of fee rate we'll use. */ /* Percent of fee rate we'll use. */
u32 commitment_fee_percent; u32 commitment_fee_percent;
/* Force a partiular fee rate regardless of estimatefee (satoshis/kb) */
u64 override_fee_rate;
/* What fee we use if estimatefee fails (satoshis/kb) */
u64 default_fee_rate;
/* Minimum/maximum time for an expiring HTLC (blocks). */ /* Minimum/maximum time for an expiring HTLC (blocks). */
u32 min_htlc_expiry, max_htlc_expiry; u32 min_htlc_expiry, max_htlc_expiry;

View file

@ -168,10 +168,10 @@ static void config_register_opts(struct lightningd_state *dstate)
&dstate->config.commitment_fee_percent, &dstate->config.commitment_fee_percent,
"Percentage of fee to request for their commitment"); "Percentage of fee to request for their commitment");
opt_register_arg("--override-fee-rate", opt_set_u64, opt_show_u64, opt_register_arg("--override-fee-rate", opt_set_u64, opt_show_u64,
&dstate->config.override_fee_rate, &dstate->topology->override_fee_rate,
"Force a specific rate in satoshis per kb regardless of estimated fees"); "Force a specific rate in satoshis per kb regardless of estimated fees");
opt_register_arg("--default-fee-rate", opt_set_u64, opt_show_u64, opt_register_arg("--default-fee-rate", opt_set_u64, opt_show_u64,
&dstate->config.default_fee_rate, &dstate->topology->default_fee_rate,
"Satoshis per kb if can't estimate fees"); "Satoshis per kb if can't estimate fees");
opt_register_arg("--min-htlc-expiry", opt_set_u32, opt_show_u32, opt_register_arg("--min-htlc-expiry", opt_set_u32, opt_show_u32,
&dstate->config.min_htlc_expiry, &dstate->config.min_htlc_expiry,
@ -249,12 +249,6 @@ static const struct config testnet_config = {
/* We offer to pay 5 times 2-block fee */ /* We offer to pay 5 times 2-block fee */
.commitment_fee_percent = 500, .commitment_fee_percent = 500,
/* Use this rate, if specified, regardless of what estimatefee says. */
.override_fee_rate = 0,
/* Use this rate by default if estimatefee doesn't estimate. */
.default_fee_rate = 40000,
/* Don't bother me unless I have 6 hours to collect. */ /* Don't bother me unless I have 6 hours to collect. */
.min_htlc_expiry = 6 * 6, .min_htlc_expiry = 6 * 6,
/* Don't lock up channel for more than 5 days. */ /* Don't lock up channel for more than 5 days. */
@ -316,12 +310,6 @@ static const struct config mainnet_config = {
/* We offer to pay 5 times 2-block fee */ /* We offer to pay 5 times 2-block fee */
.commitment_fee_percent = 500, .commitment_fee_percent = 500,
/* Use this rate, if specified, regardless of what estimatefee says. */
.override_fee_rate = 0,
/* Use this rate by default if estimatefee doesn't estimate. */
.default_fee_rate = 40000,
/* Don't bother me unless I have 6 hours to collect. */ /* Don't bother me unless I have 6 hours to collect. */
.min_htlc_expiry = 6 * 6, .min_htlc_expiry = 6 * 6,
/* Don't lock up channel for more than 5 days. */ /* Don't lock up channel for more than 5 days. */

View file

@ -304,7 +304,7 @@ Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt,
{ {
struct rel_locktime locktime; struct rel_locktime locktime;
const OpenChannel *o = pkt->open; const OpenChannel *o = pkt->open;
u64 feerate = get_feerate(peer->dstate); u64 feerate = get_feerate(peer->dstate->topology);
if (!proto_to_rel_locktime(o->delay, &locktime)) if (!proto_to_rel_locktime(o->delay, &locktime))
return pkt_err(peer, "Invalid delay"); return pkt_err(peer, "Invalid delay");

View file

@ -130,7 +130,7 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
* use 176 from an example run. */ * use 176 from an example run. */
assert(measure_tx_cost(tx) == 83 * 4); assert(measure_tx_cost(tx) == 83 * 4);
fee = fee_by_feerate(83 + 176 / 4, get_feerate(peer->dstate)); fee = fee_by_feerate(83 + 176 / 4, get_feerate(peer->dstate->topology));
/* FIXME: Fail gracefully in these cases (not worth collecting) */ /* FIXME: Fail gracefully in these cases (not worth collecting) */
if (fee > commit->output[p2wsh_out].amount if (fee > commit->output[p2wsh_out].amount
@ -391,7 +391,7 @@ static void peer_calculate_close_fee(struct peer *peer)
uint64_t maxfee; uint64_t maxfee;
peer->closing.our_fee peer->closing.our_fee
= fee_by_feerate(txsize, get_feerate(peer->dstate)); = fee_by_feerate(txsize, get_feerate(peer->dstate->topology));
/* FIXME-OLD #2: /* FIXME-OLD #2:
* The sender MUST set `close_fee` lower than or equal to the * The sender MUST set `close_fee` lower than or equal to the
@ -1991,7 +1991,7 @@ static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer,
/* Witness length can vary, due to DER encoding of sigs, but we /* Witness length can vary, due to DER encoding of sigs, but we
* use 539 from an example run. */ * use 539 from an example run. */
fee = fee_by_feerate(83 + 539 / 4, get_feerate(peer->dstate)); fee = fee_by_feerate(83 + 539 / 4, get_feerate(peer->dstate->topology));
/* FIXME: Fail gracefully in these cases (not worth collecting) */ /* FIXME: Fail gracefully in these cases (not worth collecting) */
if (fee > satoshis || is_dust(satoshis - fee)) if (fee > satoshis || is_dust(satoshis - fee))
@ -2459,7 +2459,7 @@ static void retransmit_pkts(struct peer *peer, s64 ack)
static u64 desired_commit_feerate(struct lightningd_state *dstate) static u64 desired_commit_feerate(struct lightningd_state *dstate)
{ {
return get_feerate(dstate) * dstate->config.commitment_fee_percent / 100; return get_feerate(dstate->topology) * dstate->config.commitment_fee_percent / 100;
} }
static bool want_feechange(const struct peer *peer) static bool want_feechange(const struct peer *peer)
@ -3219,7 +3219,7 @@ static void json_connect(struct command *cmd,
connect->input->in_amount = tx->output[output].amount; connect->input->in_amount = tx->output[output].amount;
/* FIXME: This is normal case, not exact. */ /* FIXME: This is normal case, not exact. */
fee = fee_by_feerate(94 + 1+73 + 1+33 + 1, get_feerate(cmd->dstate)); fee = fee_by_feerate(94 + 1+73 + 1+33 + 1, get_feerate(cmd->dstate->topology));
if (fee >= connect->input->in_amount) { if (fee >= connect->input->in_amount) {
command_fail(cmd, "Amount %"PRIu64" below fee %"PRIu64, command_fail(cmd, "Amount %"PRIu64" below fee %"PRIu64,
connect->input->in_amount, fee); connect->input->in_amount, fee);
@ -3393,10 +3393,10 @@ static enum watch_result anchor_depthchange(struct peer *peer,
/* FIXME: BOLT should say what to do if it can't! We drop conn. */ /* FIXME: BOLT should say what to do if it can't! We drop conn. */
if (!state_is_onchain(peer->state) && !state_is_error(peer->state) if (!state_is_onchain(peer->state) && !state_is_error(peer->state)
&& peer->dstate->config.commitment_fee_min_percent != 0 && peer->dstate->config.commitment_fee_min_percent != 0
&& peer->local.commit->cstate->fee_rate < get_feerate(peer->dstate)) { && peer->local.commit->cstate->fee_rate < get_feerate(peer->dstate->topology)) {
log_broken(peer->log, "fee rate %"PRIu64" lower than %"PRIu64, log_broken(peer->log, "fee rate %"PRIu64" lower than %"PRIu64,
peer->local.commit->cstate->fee_rate, peer->local.commit->cstate->fee_rate,
get_feerate(peer->dstate)); get_feerate(peer->dstate->topology));
peer_fail(peer, __func__); peer_fail(peer, __func__);
} }
@ -3551,7 +3551,7 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
/* Witness length can vary, due to DER encoding of sigs, but we /* Witness length can vary, due to DER encoding of sigs, but we
* use 539 from an example run. */ * use 539 from an example run. */
fee = fee_by_feerate(83 + 539 / 4, get_feerate(peer->dstate)); fee = fee_by_feerate(83 + 539 / 4, get_feerate(peer->dstate->topology));
/* FIXME: Fail gracefully in these cases (not worth collecting) */ /* FIXME: Fail gracefully in these cases (not worth collecting) */
if (fee > satoshis || is_dust(satoshis - fee)) if (fee > satoshis || is_dust(satoshis - fee))
@ -4113,7 +4113,7 @@ static void resolve_their_steal(struct peer *peer,
} }
assert(n == tal_count(steal_tx->input)); assert(n == tal_count(steal_tx->input));
fee = get_feerate(peer->dstate) fee = get_feerate(peer->dstate->topology)
* (measure_tx_cost(steal_tx) + wsize) / 1000; * (measure_tx_cost(steal_tx) + wsize) / 1000;
if (fee > input_total || is_dust(input_total - fee)) { if (fee > input_total || is_dust(input_total - fee)) {
@ -5087,7 +5087,7 @@ static void json_feerate(struct command *cmd,
return; return;
} }
log_debug(cmd->jcon->log, "Fee rate changed to %"PRIu64, feerate); log_debug(cmd->jcon->log, "Fee rate changed to %"PRIu64, feerate);
cmd->dstate->config.default_fee_rate = feerate; cmd->dstate->topology->default_fee_rate = feerate;
command_success(cmd, null_response(cmd)); command_success(cmd, null_response(cmd));
} }