diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index b29433312..613191459 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -706,6 +706,57 @@ u32 try_get_feerate(const struct chain_topology *topo, enum feerate feerate) return topo->feerate[feerate]; } +u32 feerate_min(struct lightningd *ld, bool *unknown) +{ + u32 min; + + if (unknown) + *unknown = false; + + /* We can't allow less than feerate_floor, since that won't relay */ + if (ld->config.ignore_fee_limits) + min = 1; + else { + u32 feerate = try_get_feerate(ld->topology, FEERATE_SLOW); + if (!feerate && unknown) + *unknown = true; + + /* Set this to half of slow rate (if unknown, will be floor) */ + min = feerate / 2; + } + + if (min < feerate_floor()) + return feerate_floor(); + return min; +} + +/* BOLT #2: + * + * Given the variance in fees, and the fact that the transaction may be + * spent in the future, it's a good idea for the fee payer to keep a good + * margin (say 5x the expected fee requirement) + */ +u32 feerate_max(struct lightningd *ld, bool *unknown) +{ + u32 feerate; + + if (unknown) + *unknown = false; + + if (ld->config.ignore_fee_limits) + return UINT_MAX; + + /* If we don't know feerate, don't limit other side. */ + feerate = try_get_feerate(ld->topology, FEERATE_URGENT); + if (!feerate) { + if (unknown) + *unknown = true; + return UINT_MAX; + } + + return feerate * ld->config.max_fee_multiplier; +} + /* On shutdown, channels get deleted last. That frees from our list, so * do it now instead. */ static void destroy_chain_topology(struct chain_topology *topo) diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 2c8650a72..758c5fa3d 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -131,6 +131,11 @@ u32 get_block_height(const struct chain_topology *topo); /* Get fee rate in satoshi per kiloweight, or 0 if unavailable! */ u32 try_get_feerate(const struct chain_topology *topo, enum feerate feerate); +/* Get range of feerates to insist other side abide by for normal channels. + * If we have to guess, sets *unknown to true, otherwise false. */ +u32 feerate_min(struct lightningd *ld, bool *unknown); +u32 feerate_max(struct lightningd *ld, bool *unknown); + /* Broadcast a single tx, and rebroadcast as reqd (copies tx). * If failed is non-NULL, call that and don't rebroadcast. */ void broadcast_tx(struct chain_topology *topo, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index db962035c..7c7c1902e 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -189,57 +189,6 @@ u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx) return scriptpubkey_p2wpkh(ctx, &shutdownkey); } -u32 feerate_min(struct lightningd *ld, bool *unknown) -{ - u32 min; - - if (unknown) - *unknown = false; - - /* We can't allow less than feerate_floor, since that won't relay */ - if (ld->config.ignore_fee_limits) - min = 1; - else { - u32 feerate = try_get_feerate(ld->topology, FEERATE_SLOW); - if (!feerate && unknown) - *unknown = true; - - /* Set this to half of slow rate (if unknown, will be floor) */ - min = feerate / 2; - } - - if (min < feerate_floor()) - return feerate_floor(); - return min; -} - -/* BOLT #2: - * - * Given the variance in fees, and the fact that the transaction may be - * spent in the future, it's a good idea for the fee payer to keep a good - * margin (say 5x the expected fee requirement) - */ -u32 feerate_max(struct lightningd *ld, bool *unknown) -{ - u32 feerate; - - if (unknown) - *unknown = false; - - if (ld->config.ignore_fee_limits) - return UINT_MAX; - - /* If we don't know feerate, don't limit other side. */ - feerate = try_get_feerate(ld->topology, FEERATE_URGENT); - if (!feerate) { - if (unknown) - *unknown = true; - return UINT_MAX; - } - - return feerate * ld->config.max_fee_multiplier; -} - static void sign_last_tx(struct channel *channel) { struct lightningd *ld = channel->peer->ld; diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index b1c78527a..860f7bb33 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -88,10 +88,5 @@ void activate_peers(struct lightningd *ld); void drop_to_chain(struct lightningd *ld, struct channel *channel, bool cooperative); -/* Get range of feerates to insist other side abide by for normal channels. - * If we have to guess, sets *unknown to true, otherwise false. */ -u32 feerate_min(struct lightningd *ld, bool *unknown); -u32 feerate_max(struct lightningd *ld, bool *unknown); - void channel_watch_funding(struct lightningd *ld, struct channel *channel); #endif /* LIGHTNING_LIGHTNINGD_PEER_CONTROL_H */