diff --git a/bitcoin/Makefile b/bitcoin/Makefile index b6500b3ee..a6f876f8f 100644 --- a/bitcoin/Makefile +++ b/bitcoin/Makefile @@ -20,6 +20,7 @@ BITCOIN_HEADERS := bitcoin/address.h \ bitcoin/base58.h \ bitcoin/block.h \ bitcoin/chainparams.h \ + bitcoin/feerate.h \ bitcoin/locktime.h \ bitcoin/preimage.h \ bitcoin/privkey.h \ diff --git a/bitcoin/feerate.h b/bitcoin/feerate.h new file mode 100644 index 000000000..a0379a432 --- /dev/null +++ b/bitcoin/feerate.h @@ -0,0 +1,50 @@ +#ifndef LIGHTNING_BITCOIN_FEERATE_H +#define LIGHTNING_BITCOIN_FEERATE_H +#include "config.h" +#include +#include + +/* bitcoind considers 250 satoshi per kw to be the minimum acceptable fee: + * less than this won't even relay. + */ +#define BITCOIND_MINRELAYTXFEE_PER_KW 250 +/* + * But bitcoind uses vbytes (ie. (weight + 3) / 4) for this + * calculation, rather than weight, meaning we can disagree since we do + * it sanely (as specified in BOLT #3). + */ +#define FEERATE_BITCOIND_SEES(feerate, weight) \ + (((feerate) * (weight)) / 1000 * 1000 / ((weight) + 3)) +/* ie. fee = (feerate * weight) // 1000 + * bitcoind needs (worst-case): fee * 1000 / (weight + 3) >= 250 + * + * (feerate * weight) // 1000 * 1000 // (weight + 3) >= 250 + * + * The feerate needs to be higher for lower weight, and our minimum tx weight + * is 464 (version (4) + count_tx_in (1) + tx_in (32 + 4 + 1 + 4) + + * count_tx_out (1) + amount (8) + P2WSH (1 + 1 + 32) + witness 1 + 1 + + * + 1 + ). Assume it's 400 to give a significant safety margin (it + * only makes 1 difference in the result anyway). + */ +#define MINIMUM_TX_WEIGHT 400 + +/* + * This formula is satisfied by a feerate of 253 (hand-search). + */ +#define FEERATE_FLOOR 253 + +static inline u32 feerate_floor(void) +{ + /* Assert that bitcoind will see this as above minRelayTxFee */ + BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, MINIMUM_TX_WEIGHT) + >= BITCOIND_MINRELAYTXFEE_PER_KW); + /* And a lesser value won't do */ + BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR-1, MINIMUM_TX_WEIGHT) + < BITCOIND_MINRELAYTXFEE_PER_KW); + /* And I'm right about it being OK for larger txs, too */ + BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, (MINIMUM_TX_WEIGHT*2)) + >= BITCOIND_MINRELAYTXFEE_PER_KW); + + return FEERATE_FLOOR; +} +#endif /* LIGHTNING_BITCOIN_FEERATE_H */ diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index c0e6d390d..6e16c6a65 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -1,4 +1,5 @@ #include "bitcoin/block.h" +#include "bitcoin/feerate.h" #include "bitcoin/script.h" #include "bitcoin/tx.h" #include "bitcoind.h" @@ -229,48 +230,6 @@ static const char *feerate_name(enum feerate feerate) /* Mutual recursion via timer. */ static void next_updatefee_timer(struct chain_topology *topo); -/* bitcoind considers 250 satoshi per kw to be the minimum acceptable fee: - * less than this won't even relay. - */ -#define BITCOIND_MINRELAYTXFEE_PER_KW 250 -/* - * But bitcoind uses vbytes (ie. (weight + 3) / 4) for this - * calculation, rather than weight, meaning we can disagree since we do - * it sanely (as specified in BOLT #3). - */ -#define FEERATE_BITCOIND_SEES(feerate, weight) \ - (((feerate) * (weight)) / 1000 * 1000 / ((weight) + 3)) -/* ie. fee = (feerate * weight) // 1000 - * bitcoind needs (worst-case): fee * 1000 / (weight + 3) >= 4000 - * - * (feerate * weight) // 1000 * 1000 // (weight + 3) >= 4000 - * - * The feerate needs to be higher for lower weight, and our minimum tx weight - * is 464 (version (4) + count_tx_in (1) + tx_in (32 + 4 + 1 + 4) + - * count_tx_out (1) + amount (8) + P2WSH (1 + 1 + 32) + witness 1 + 1 + - * + 1 + ). Assume it's 400 to give a significant safety margin (it - * only makes 1 difference in the result anyway). - */ -#define MINIMUM_TX_WEIGHT 400 -/* - * This formula is satisfied by a feerate of 4030 (hand-search). - */ -#define FEERATE_FLOOR 253 -u32 feerate_floor(void) -{ - /* Assert that bitcoind will see this as above minRelayTxFee */ - BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, MINIMUM_TX_WEIGHT) - >= BITCOIND_MINRELAYTXFEE_PER_KW); - /* And a lesser value won't do */ - BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR-1, MINIMUM_TX_WEIGHT) - < BITCOIND_MINRELAYTXFEE_PER_KW); - /* And I'm right about it being OK for larger txs, too */ - BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, (MINIMUM_TX_WEIGHT*2)) - >= BITCOIND_MINRELAYTXFEE_PER_KW); - - return FEERATE_FLOOR; -} - /* We sanitize feerates if necessary to put them in descending order. */ static void update_feerates(struct bitcoind *bitcoind, const u32 *satoshi_per_kw, diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index b6021984a..fe54fd8a7 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -138,9 +138,6 @@ u32 get_block_height(const struct chain_topology *topo); /* Get fee rate in satoshi per kiloweight. */ u32 get_feerate(const struct chain_topology *topo, enum feerate feerate); -/* Get the minimum possible fee to allow relaying by bitcoind */ -u32 feerate_floor(void); - /* 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 53b45be09..818ff12a7 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -2,6 +2,7 @@ #include "peer_control.h" #include "subd.h" #include +#include #include #include #include diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9205f4f8e..784fb4b18 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -73,9 +73,6 @@ bool extract_channel_id(const u8 *in_pkt UNNEEDED, struct channel_id *channel_id /* Generated stub for features_supported */ bool features_supported(const u8 *gfeatures UNNEEDED, const u8 *lfeatures UNNEEDED) { fprintf(stderr, "features_supported called!\n"); abort(); } -/* Generated stub for feerate_floor */ -u32 feerate_floor(void) -{ fprintf(stderr, "feerate_floor called!\n"); abort(); } /* Generated stub for fromwire_gossipctl_peer_disconnect_reply */ bool fromwire_gossipctl_peer_disconnect_reply(const void *p UNNEEDED) { fprintf(stderr, "fromwire_gossipctl_peer_disconnect_reply called!\n"); abort(); }