Skip to content

Commit 0e6c0db

Browse files
rustyrussellcdecker
authored andcommitted
bitcoin: expose feerate_floor.
Onchaind will want it. Signed-off-by: Rusty Russell <[email protected]>
1 parent 4a5cff8 commit 0e6c0db

File tree

6 files changed

+53
-48
lines changed

6 files changed

+53
-48
lines changed

bitcoin/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ BITCOIN_HEADERS := bitcoin/address.h \
2020
bitcoin/base58.h \
2121
bitcoin/block.h \
2222
bitcoin/chainparams.h \
23+
bitcoin/feerate.h \
2324
bitcoin/locktime.h \
2425
bitcoin/preimage.h \
2526
bitcoin/privkey.h \

bitcoin/feerate.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef LIGHTNING_BITCOIN_FEERATE_H
2+
#define LIGHTNING_BITCOIN_FEERATE_H
3+
#include "config.h"
4+
#include <ccan/build_assert/build_assert.h>
5+
#include <ccan/short_types/short_types.h>
6+
7+
/* bitcoind considers 250 satoshi per kw to be the minimum acceptable fee:
8+
* less than this won't even relay.
9+
*/
10+
#define BITCOIND_MINRELAYTXFEE_PER_KW 250
11+
/*
12+
* But bitcoind uses vbytes (ie. (weight + 3) / 4) for this
13+
* calculation, rather than weight, meaning we can disagree since we do
14+
* it sanely (as specified in BOLT #3).
15+
*/
16+
#define FEERATE_BITCOIND_SEES(feerate, weight) \
17+
(((feerate) * (weight)) / 1000 * 1000 / ((weight) + 3))
18+
/* ie. fee = (feerate * weight) // 1000
19+
* bitcoind needs (worst-case): fee * 1000 / (weight + 3) >= 250
20+
*
21+
* (feerate * weight) // 1000 * 1000 // (weight + 3) >= 250
22+
*
23+
* The feerate needs to be higher for lower weight, and our minimum tx weight
24+
* is 464 (version (4) + count_tx_in (1) + tx_in (32 + 4 + 1 + 4) +
25+
* count_tx_out (1) + amount (8) + P2WSH (1 + 1 + 32) + witness 1 + 1 + <sig>
26+
* + 1 + <key>). Assume it's 400 to give a significant safety margin (it
27+
* only makes 1 difference in the result anyway).
28+
*/
29+
#define MINIMUM_TX_WEIGHT 400
30+
31+
/*
32+
* This formula is satisfied by a feerate of 253 (hand-search).
33+
*/
34+
#define FEERATE_FLOOR 253
35+
36+
static inline u32 feerate_floor(void)
37+
{
38+
/* Assert that bitcoind will see this as above minRelayTxFee */
39+
BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, MINIMUM_TX_WEIGHT)
40+
>= BITCOIND_MINRELAYTXFEE_PER_KW);
41+
/* And a lesser value won't do */
42+
BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR-1, MINIMUM_TX_WEIGHT)
43+
< BITCOIND_MINRELAYTXFEE_PER_KW);
44+
/* And I'm right about it being OK for larger txs, too */
45+
BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, (MINIMUM_TX_WEIGHT*2))
46+
>= BITCOIND_MINRELAYTXFEE_PER_KW);
47+
48+
return FEERATE_FLOOR;
49+
}
50+
#endif /* LIGHTNING_BITCOIN_FEERATE_H */

lightningd/chaintopology.c

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "bitcoin/block.h"
2+
#include "bitcoin/feerate.h"
23
#include "bitcoin/script.h"
34
#include "bitcoin/tx.h"
45
#include "bitcoind.h"
@@ -229,48 +230,6 @@ static const char *feerate_name(enum feerate feerate)
229230
/* Mutual recursion via timer. */
230231
static void next_updatefee_timer(struct chain_topology *topo);
231232

232-
/* bitcoind considers 250 satoshi per kw to be the minimum acceptable fee:
233-
* less than this won't even relay.
234-
*/
235-
#define BITCOIND_MINRELAYTXFEE_PER_KW 250
236-
/*
237-
* But bitcoind uses vbytes (ie. (weight + 3) / 4) for this
238-
* calculation, rather than weight, meaning we can disagree since we do
239-
* it sanely (as specified in BOLT #3).
240-
*/
241-
#define FEERATE_BITCOIND_SEES(feerate, weight) \
242-
(((feerate) * (weight)) / 1000 * 1000 / ((weight) + 3))
243-
/* ie. fee = (feerate * weight) // 1000
244-
* bitcoind needs (worst-case): fee * 1000 / (weight + 3) >= 4000
245-
*
246-
* (feerate * weight) // 1000 * 1000 // (weight + 3) >= 4000
247-
*
248-
* The feerate needs to be higher for lower weight, and our minimum tx weight
249-
* is 464 (version (4) + count_tx_in (1) + tx_in (32 + 4 + 1 + 4) +
250-
* count_tx_out (1) + amount (8) + P2WSH (1 + 1 + 32) + witness 1 + 1 + <sig>
251-
* + 1 + <key>). Assume it's 400 to give a significant safety margin (it
252-
* only makes 1 difference in the result anyway).
253-
*/
254-
#define MINIMUM_TX_WEIGHT 400
255-
/*
256-
* This formula is satisfied by a feerate of 4030 (hand-search).
257-
*/
258-
#define FEERATE_FLOOR 253
259-
u32 feerate_floor(void)
260-
{
261-
/* Assert that bitcoind will see this as above minRelayTxFee */
262-
BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, MINIMUM_TX_WEIGHT)
263-
>= BITCOIND_MINRELAYTXFEE_PER_KW);
264-
/* And a lesser value won't do */
265-
BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR-1, MINIMUM_TX_WEIGHT)
266-
< BITCOIND_MINRELAYTXFEE_PER_KW);
267-
/* And I'm right about it being OK for larger txs, too */
268-
BUILD_ASSERT(FEERATE_BITCOIND_SEES(FEERATE_FLOOR, (MINIMUM_TX_WEIGHT*2))
269-
>= BITCOIND_MINRELAYTXFEE_PER_KW);
270-
271-
return FEERATE_FLOOR;
272-
}
273-
274233
/* We sanitize feerates if necessary to put them in descending order. */
275234
static void update_feerates(struct bitcoind *bitcoind,
276235
const u32 *satoshi_per_kw,

lightningd/chaintopology.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,6 @@ u32 get_block_height(const struct chain_topology *topo);
138138
/* Get fee rate in satoshi per kiloweight. */
139139
u32 get_feerate(const struct chain_topology *topo, enum feerate feerate);
140140

141-
/* Get the minimum possible fee to allow relaying by bitcoind */
142-
u32 feerate_floor(void);
143-
144141
/* Broadcast a single tx, and rebroadcast as reqd (copies tx).
145142
* If failed is non-NULL, call that and don't rebroadcast. */
146143
void broadcast_tx(struct chain_topology *topo,

lightningd/peer_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "peer_control.h"
33
#include "subd.h"
44
#include <arpa/inet.h>
5+
#include <bitcoin/feerate.h>
56
#include <bitcoin/script.h>
67
#include <bitcoin/tx.h>
78
#include <ccan/array_size/array_size.h>

wallet/test/run-wallet.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ bool extract_channel_id(const u8 *in_pkt UNNEEDED, struct channel_id *channel_id
7373
/* Generated stub for features_supported */
7474
bool features_supported(const u8 *gfeatures UNNEEDED, const u8 *lfeatures UNNEEDED)
7575
{ fprintf(stderr, "features_supported called!\n"); abort(); }
76-
/* Generated stub for feerate_floor */
77-
u32 feerate_floor(void)
78-
{ fprintf(stderr, "feerate_floor called!\n"); abort(); }
7976
/* Generated stub for fromwire_gossipctl_peer_disconnect_reply */
8077
bool fromwire_gossipctl_peer_disconnect_reply(const void *p UNNEEDED)
8178
{ fprintf(stderr, "fromwire_gossipctl_peer_disconnect_reply called!\n"); abort(); }

0 commit comments

Comments
 (0)