From e2c7925e0e4ea94106913faad542bb4c5ed7c284 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 2 Mar 2017 22:51:49 +1030 Subject: [PATCH] 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 --- daemon/chaintopology.c | 82 +++++++++++++++++++++++++----------------- daemon/chaintopology.h | 18 ++++++++-- daemon/lightningd.c | 2 +- daemon/lightningd.h | 6 ---- daemon/options.c | 16 ++------- daemon/packets.c | 2 +- daemon/peer.c | 20 +++++------ 7 files changed, 80 insertions(+), 66 deletions(-) diff --git a/daemon/chaintopology.c b/daemon/chaintopology.c index 1a4877e99..8e90b9d94 100644 --- a/daemon/chaintopology.c +++ b/daemon/chaintopology.c @@ -237,15 +237,12 @@ static void broadcast_done(struct bitcoind *bitcoind, int exitstatus, const char *msg, struct outgoing_tx *otx) { - struct lightningd_state *dstate = tal_parent(bitcoind); - struct topology *topo = dstate->topology; - if (otx->failed && exitstatus != 0) { otx->failed(otx->peer, exitstatus, msg); tal_free(otx); } else { /* 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); } } @@ -262,9 +259,10 @@ void broadcast_tx(struct topology *topo, bitcoin_txid(tx, &otx->txid); otx->hextx = tal_hex(otx, rawtx); otx->failed = failed; + otx->topo = topo; tal_free(rawtx); - log_add_struct(topo->bitcoind->log, + log_add_struct(topo->log, " (tx %s)", struct sha256_double, &otx->txid); 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, - rate, *feerate); - *feerate = rate; + log_debug(topo->log, "Feerate %"PRIu64" (was %"PRIu64")", + rate, topo->feerate); + topo->feerate = rate; } /* 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); /* 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, @@ -331,10 +330,11 @@ static struct block *new_block(struct topology *topo, struct block *b = tal(topo, struct block); 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); assert(!block_map_get(&topo->block_map, &b->blkid)); b->next = next; + b->topo = topo; /* We fill these out in topology_changed */ b->height = -1; @@ -350,12 +350,23 @@ static struct block *new_block(struct topology *topo, return b; } -static void gather_blocks(struct bitcoind *bitcoind, - struct bitcoin_block *blk, - struct block *next) +static void add_block(struct bitcoind *bitcoind, + struct topology *topo, + 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; b = new_block(topo, blk, next); @@ -363,8 +374,8 @@ static void gather_blocks(struct bitcoind *bitcoind, /* Recurse if we need prev. */ prev = block_map_get(&topo->block_map, &blk->hdr.prev_hash); if (!prev) { - bitcoind_getrawblock(dstate->bitcoind, &blk->hdr.prev_hash, - gather_blocks, b); + bitcoind_getrawblock(bitcoind, &blk->hdr.prev_hash, + gather_previous_blocks, b); return; } @@ -373,14 +384,20 @@ static void gather_blocks(struct bitcoind *bitcoind, 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, const struct sha256_double *tipid, struct topology *topo) { /* 0 is the main tip. */ if (!structeq(tipid, &topo->tip->blkid)) - bitcoind_getrawblock(bitcoind, tipid, gather_blocks, - (struct block *)NULL); + bitcoind_getrawblock(bitcoind, tipid, rawblock_tip, topo); else /* Next! */ next_topology_timer(topo); @@ -389,7 +406,7 @@ static void check_chaintip(struct bitcoind *bitcoind, static void start_poll_chaintip(struct topology *topo) { if (!list_empty(&topo->bitcoind->pending)) { - log_unusual(topo->bitcoind->log, + log_unusual(topo->log, "Delaying start poll: commands in progress"); next_topology_timer(topo); } else @@ -453,19 +470,17 @@ u32 get_block_height(const struct topology *topo) return topo->tip->height; } -u64 get_feerate(struct lightningd_state *dstate) +u64 get_feerate(const struct topology *topo) { - if (dstate->config.override_fee_rate) { - log_debug(dstate->base_log, - "Forcing fee rate, ignoring estimate"); - return dstate->config.override_fee_rate; + if (topo->override_fee_rate) { + log_debug(topo->log, "Forcing fee rate, ignoring estimate"); + return topo->override_fee_rate; } - else if (dstate->topology->feerate == 0) { - log_info(dstate->base_log, - "No fee estimate: using default fee rate"); - return dstate->config.default_fee_rate; + else if (topo->feerate == 0) { + log_info(topo->log, "No fee estimate: using default fee rate"); + return topo->default_fee_rate; } - return dstate->topology->feerate; + return topo->feerate; } 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); } -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); @@ -536,6 +551,9 @@ struct topology *new_topology(const tal_t *ctx) list_head_init(&topo->outgoing_txs); txwatch_hash_init(&topo->txwatches); txowatch_hash_init(&topo->txowatches); + topo->log = log; + topo->default_fee_rate = 40000; + topo->override_fee_rate = 0; topo->dev_no_broadcast = false; return topo; diff --git a/daemon/chaintopology.h b/daemon/chaintopology.h index 45d942e12..68f04eb1e 100644 --- a/daemon/chaintopology.h +++ b/daemon/chaintopology.h @@ -26,6 +26,8 @@ struct outgoing_tx { const char *hextx; struct sha256_double txid; void (*failed)(struct peer *peer, int exitstatus, const char *err); + /* FIXME: Remove this. */ + struct topology *topo; }; struct block { @@ -54,6 +56,9 @@ struct block { /* Full copy of txs (trimmed to txs list in connect_block) */ struct bitcoin_tx **full_txs; + + /* FIXME: Remove this. */ + struct topology *topo; }; /* Hash blocks by sha */ @@ -83,6 +88,9 @@ struct topology { u64 feerate; bool startup; + /* Where to log things. */ + struct log *log; + /* How far back (in blocks) to go. */ unsigned int first_blocknum; @@ -98,6 +106,12 @@ struct topology { /* Bitcoin transctions we're broadcasting */ 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. */ struct txwatch_hash txwatches; struct txowatch_hash txowatches; @@ -132,7 +146,7 @@ u32 get_tip_mediantime(const struct topology *topo); u32 get_block_height(const struct topology *topo); /* 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). * If failed is non-NULL, call that and don't rebroadcast. */ @@ -142,7 +156,7 @@ void broadcast_tx(struct topology *topo, int exitstatus, 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, struct timers *timers, struct timerel poll_time, u32 first_peer_block); diff --git a/daemon/lightningd.c b/daemon/lightningd.c index 1fcf8f67e..a23e4913b 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - dstate->topology = new_topology(dstate); + dstate->topology = new_topology(dstate, dstate->base_log); dstate->bitcoind = new_bitcoind(dstate, dstate->base_log); /* Handle options and config; move to .lightningd */ diff --git a/daemon/lightningd.h b/daemon/lightningd.h index 66394c83a..45c3f2462 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -37,12 +37,6 @@ struct config { /* Percent of fee rate we'll use. */ 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). */ u32 min_htlc_expiry, max_htlc_expiry; diff --git a/daemon/options.c b/daemon/options.c index be59bb6c0..a141c7647 100644 --- a/daemon/options.c +++ b/daemon/options.c @@ -168,10 +168,10 @@ static void config_register_opts(struct lightningd_state *dstate) &dstate->config.commitment_fee_percent, "Percentage of fee to request for their commitment"); 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"); 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"); opt_register_arg("--min-htlc-expiry", opt_set_u32, opt_show_u32, &dstate->config.min_htlc_expiry, @@ -249,12 +249,6 @@ static const struct config testnet_config = { /* We offer to pay 5 times 2-block fee */ .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. */ .min_htlc_expiry = 6 * 6, /* 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 */ .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. */ .min_htlc_expiry = 6 * 6, /* Don't lock up channel for more than 5 days. */ diff --git a/daemon/packets.c b/daemon/packets.c index ecc977184..1c0995dff 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -304,7 +304,7 @@ Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt, { struct rel_locktime locktime; 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)) return pkt_err(peer, "Invalid delay"); diff --git a/daemon/peer.c b/daemon/peer.c index d945a9185..585b87bf4 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -130,7 +130,7 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer) * use 176 from an example run. */ 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) */ if (fee > commit->output[p2wsh_out].amount @@ -391,7 +391,7 @@ static void peer_calculate_close_fee(struct peer *peer) uint64_t maxfee; peer->closing.our_fee - = fee_by_feerate(txsize, get_feerate(peer->dstate)); + = fee_by_feerate(txsize, get_feerate(peer->dstate->topology)); /* FIXME-OLD #2: * 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 * 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) */ 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) { - 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) @@ -3219,7 +3219,7 @@ static void json_connect(struct command *cmd, connect->input->in_amount = tx->output[output].amount; /* 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) { command_fail(cmd, "Amount %"PRIu64" below fee %"PRIu64, 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. */ if (!state_is_onchain(peer->state) && !state_is_error(peer->state) && 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, peer->local.commit->cstate->fee_rate, - get_feerate(peer->dstate)); + get_feerate(peer->dstate->topology)); 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 * 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) */ 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)); - fee = get_feerate(peer->dstate) + fee = get_feerate(peer->dstate->topology) * (measure_tx_cost(steal_tx) + wsize) / 1000; if (fee > input_total || is_dust(input_total - fee)) { @@ -5087,7 +5087,7 @@ static void json_feerate(struct command *cmd, return; } 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)); }