Skip to content

Commit

Permalink
feerate: use u32 everywhere.
Browse files Browse the repository at this point in the history
The wire protocol uses this, in the assumption that we'll never see feerates
in excess of 4294967 satoshi per kiloweight.

So let's use that consistently internally as well.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Nov 23, 2017
1 parent 88af0f5 commit f1e4cad
Show file tree
Hide file tree
Showing 20 changed files with 60 additions and 80 deletions.
6 changes: 3 additions & 3 deletions channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#endif

static bool trim(const struct htlc *htlc,
u64 feerate_per_kw, u64 dust_limit_satoshis,
u32 feerate_per_kw, u64 dust_limit_satoshis,
enum side side)
{
u64 htlc_fee;
Expand Down Expand Up @@ -42,7 +42,7 @@ static bool trim(const struct htlc *htlc,
}

size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
u64 feerate_per_kw, u64 dust_limit_satoshis,
u32 feerate_per_kw, u64 dust_limit_satoshis,
enum side side)
{
size_t i, n;
Expand Down Expand Up @@ -92,7 +92,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
enum side funder,
u16 to_self_delay,
const struct keyset *keyset,
u64 feerate_per_kw,
u32 feerate_per_kw,
u64 dust_limit_satoshis,
u64 self_pay_msat,
u64 other_pay_msat,
Expand Down
4 changes: 2 additions & 2 deletions channeld/commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct sha256_double;
* received HTLCs.
*/
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
u64 feerate_per_kw, u64 dust_limit_satoshis,
u32 feerate_per_kw, u64 dust_limit_satoshis,
enum side side);

/**
Expand Down Expand Up @@ -49,7 +49,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
enum side funder,
u16 to_self_delay,
const struct keyset *keyset,
u64 feerate_per_kw,
u32 feerate_per_kw,
u64 dust_limit_satoshis,
u64 self_pay_msat,
u64 other_pay_msat,
Expand Down
2 changes: 1 addition & 1 deletion channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
* `feerate_per_kw` while maintaining its channel reserve.
*/
if (channel->funder == htlc_owner(htlc)) {
u64 feerate = view->feerate_per_kw;
u32 feerate = view->feerate_per_kw;
u64 dust = dust_limit_satoshis(channel, recipient);
size_t untrimmed;

Expand Down
10 changes: 5 additions & 5 deletions channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
* This is the fee rate we actually care about, if we're going to check
* whether it's actually too low.
*/
uint32_t actual_feerate(const struct channel *channel,
const struct signature *theirsig);
u32 actual_feerate(const struct channel *channel,
const struct signature *theirsig);

enum channel_add_err {
/* All OK! */
Expand Down Expand Up @@ -175,22 +175,22 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
*
* This is not exact! To check if their offer is valid, use can_afford_feerate.
*/
u64 approx_max_feerate(const struct channel *channel);
u32 approx_max_feerate(const struct channel *channel);

/**
* can_afford_feerate: could the initiator pay for the fee at fee_rate?
* @channel: The channel state
* @feerate_per_kw: the new fee rate proposed
*/
bool can_afford_feerate(const struct channel *channel, u64 feerate_per_kw);
bool can_afford_feerate(const struct channel *channel, u32 feerate_per_kw);

/**
* adjust_fee: Change fee rate.
* @channel: The channel state
* @feerate_per_kw: fee in satoshi per 1000 bytes.
* @side: which side to adjust.
*/
void adjust_fee(struct channel *channel, u64 feerate_per_kw, enum side side);
void adjust_fee(struct channel *channel, u32 feerate_per_kw, enum side side);

/**
* channel_sending_commit: commit all remote outstanding changes.
Expand Down
5 changes: 3 additions & 2 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static struct bitcoin_tx *tx_from_hex(const tal_t *ctx, const char *hex)
* ...
* local_feerate_per_kw: 9651936
*/
static u64 feerates[] = {
static u32 feerates[] = {
647, 648,
2069, 2070,
2194, 2195,
Expand Down Expand Up @@ -304,7 +304,8 @@ int main(void)
struct sha256_double funding_txid;
/* We test from both sides. */
struct channel *lchannel, *rchannel;
u64 funding_amount_satoshi, feerate_per_kw;
u64 funding_amount_satoshi;
u32 feerate_per_kw;
unsigned int funding_output_index;
struct keyset keyset;
struct pubkey local_funding_pubkey, remote_funding_pubkey;
Expand Down
4 changes: 2 additions & 2 deletions common/htlc_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
unsigned int commit_output_number,
u64 htlc_msatoshi,
u16 to_self_delay,
u64 feerate_per_kw,
u32 feerate_per_kw,
const struct keyset *keyset)
{
/* BOLT #3:
Expand Down Expand Up @@ -122,7 +122,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
u64 htlc_msatoshi,
u32 cltv_expiry,
u16 to_self_delay,
u64 feerate_per_kw,
u32 feerate_per_kw,
const struct keyset *keyset)
{
/* BOLT #3:
Expand Down
12 changes: 6 additions & 6 deletions common/htlc_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct preimage;
struct pubkey;
struct sha256_double;

static inline u64 htlc_timeout_fee(u64 feerate_per_kw)
static inline u64 htlc_timeout_fee(u32 feerate_per_kw)
{
/* BOLT #3:
*
Expand All @@ -17,10 +17,10 @@ static inline u64 htlc_timeout_fee(u64 feerate_per_kw)
* 1. Multiply `feerate_per_kw` by 663 and divide by 1000 (rounding
* down).
*/
return feerate_per_kw * 663 / 1000;
return feerate_per_kw * 663ULL / 1000;
}

static inline u64 htlc_success_fee(u64 feerate_per_kw)
static inline u64 htlc_success_fee(u32 feerate_per_kw)
{
/* BOLT #3:
*
Expand All @@ -29,7 +29,7 @@ static inline u64 htlc_success_fee(u64 feerate_per_kw)
* 1. Multiply `feerate_per_kw` by 703 and divide by 1000 (rounding
* down).
*/
return feerate_per_kw * 703 / 1000;
return feerate_per_kw * 703ULL / 1000;
}

/* Create HTLC-success tx to spend a received HTLC commitment tx
Expand All @@ -39,7 +39,7 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
unsigned int commit_output_number,
u64 htlc_msatoshi,
u16 to_self_delay,
u64 feerate_per_kw,
u32 feerate_per_kw,
const struct keyset *keyset);

/* Fill in the witness for HTLC-success tx produced above. */
Expand All @@ -60,7 +60,7 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
u64 htlc_msatoshi,
u32 cltv_expiry,
u16 to_self_delay,
u64 feerate_per_kw,
u32 feerate_per_kw,
const struct keyset *keyset);

/* Fill in the witness for HTLC-timeout tx produced above. */
Expand Down
2 changes: 1 addition & 1 deletion common/initial_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,

static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view)
{
return tal_fmt(ctx, "{ feerate_per_kw=%"PRIu64","
return tal_fmt(ctx, "{ feerate_per_kw=%"PRIu32","
" owed_local=%"PRIu64","
" owed_remote=%"PRIu64" }",
view->feerate_per_kw,
Expand Down
2 changes: 1 addition & 1 deletion common/initial_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct fulfilled_htlc;
/* View from each side */
struct channel_view {
/* Current feerate in satoshis per 1000 weight. */
u64 feerate_per_kw;
u32 feerate_per_kw;

/* How much is owed to each side (includes pending changes) */
u64 owed_msat[NUM_SIDES];
Expand Down
2 changes: 1 addition & 1 deletion common/initial_commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
enum side funder,
u16 to_self_delay,
const struct keyset *keyset,
u64 feerate_per_kw,
u32 feerate_per_kw,
u64 dust_limit_satoshis,
u64 self_pay_msat,
u64 other_pay_msat,
Expand Down
4 changes: 2 additions & 2 deletions common/initial_commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
const struct pubkey *accepter_payment_basepoint);

/* Helper to calculate the base fee if we have this many htlc outputs */
static inline u64 commit_tx_base_fee(u64 feerate_per_kw,
static inline u64 commit_tx_base_fee(u32 feerate_per_kw,
size_t num_untrimmed_htlcs)
{
u64 weight;
Expand Down Expand Up @@ -73,7 +73,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
enum side funder,
u16 to_self_delay,
const struct keyset *keyset,
u64 feerate_per_kw,
u32 feerate_per_kw,
u64 dust_limit_satoshis,
u64 self_pay_msat,
u64 other_pay_msat,
Expand Down
8 changes: 4 additions & 4 deletions lightningd/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,10 @@ struct estimatefee {
const u32 *blocks;
const char **estmode;

void (*cb)(struct bitcoind *bitcoind, const u64 satoshi_per_kw[],
void (*cb)(struct bitcoind *bitcoind, const u32 satoshi_per_kw[],
void *);
void *arg;
u64 *satoshi_per_kw;
u32 *satoshi_per_kw;
};

static void do_one_estimatefee(struct bitcoind *bitcoind,
Expand Down Expand Up @@ -312,7 +312,7 @@ void bitcoind_estimate_fees_(struct bitcoind *bitcoind,
const u32 blocks[], const char *estmode[],
size_t num_estimates,
void (*cb)(struct bitcoind *bitcoind,
const u64 satoshi_per_kw[], void *),
const u32 satoshi_per_kw[], void *),
void *arg)
{
struct estimatefee *efee = tal(bitcoind, struct estimatefee);
Expand All @@ -323,7 +323,7 @@ void bitcoind_estimate_fees_(struct bitcoind *bitcoind,
0);
efee->cb = cb;
efee->arg = arg;
efee->satoshi_per_kw = tal_arr(efee, u64, num_estimates);
efee->satoshi_per_kw = tal_arr(efee, u32, num_estimates);

do_one_estimatefee(bitcoind, efee);
}
Expand Down
4 changes: 2 additions & 2 deletions lightningd/bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ void bitcoind_estimate_fees_(struct bitcoind *bitcoind,
const u32 blocks[], const char *estmode[],
size_t num_estimates,
void (*cb)(struct bitcoind *bitcoind,
const u64 satoshi_per_kw[], void *),
const u32 satoshi_per_kw[], void *),
void *arg);

#define bitcoind_estimate_fees(bitcoind_, blocks, estmode, num, cb, arg) \
bitcoind_estimate_fees_((bitcoind_), (blocks), (estmode), (num), \
typesafe_cb_preargs(void, void *, \
(cb), (arg), \
struct bitcoind *, \
const u64 *), \
const u32 *), \
(arg))

void bitcoind_sendrawtx_(struct bitcoind *bitcoind,
Expand Down
13 changes: 6 additions & 7 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ static const char *feerate_name(enum feerate feerate)

/* We sanitize feerates if necessary to put them in descending order. */
static void update_feerates(struct bitcoind *bitcoind,
const u64 *satoshi_per_kw,
const u32 *satoshi_per_kw,
struct chain_topology *topo)
{
for (size_t i = 0; i < NUM_FEERATES; i++) {
log_debug(topo->log, "%s feerate %"PRIu64" (was %"PRIu64")",
log_debug(topo->log, "%s feerate %u (was %u)",
feerate_name(i),
satoshi_per_kw[i], topo->feerate[i]);
topo->feerate[i] = satoshi_per_kw[i];
Expand All @@ -314,8 +314,7 @@ static void update_feerates(struct bitcoind *bitcoind,
for (size_t j = 0; j < i; j++) {
if (topo->feerate[j] < topo->feerate[i]) {
log_unusual(topo->log,
"Feerate %s (%"PRIu64") above"
" %s (%"PRIu64")",
"Feerate %s (%u) above %s (%u)",
feerate_name(i), topo->feerate[i],
feerate_name(j), topo->feerate[j]);
topo->feerate[j] = topo->feerate[i];
Expand Down Expand Up @@ -486,10 +485,10 @@ u32 get_block_height(const struct chain_topology *topo)
}

/* We may only have estimate for 2 blocks, for example. Extrapolate. */
static u64 guess_feerate(const struct chain_topology *topo, enum feerate feerate)
static u32 guess_feerate(const struct chain_topology *topo, enum feerate feerate)
{
size_t i = 0;
u64 rate = 0;
u32 rate = 0;

/* We assume each one is half the previous. */
for (i = 0; i < feerate; i++) {
Expand All @@ -513,7 +512,7 @@ static u64 guess_feerate(const struct chain_topology *topo, enum feerate feerate
return rate;
}

u64 get_feerate(const struct chain_topology *topo, enum feerate feerate)
u32 get_feerate(const struct chain_topology *topo, enum feerate feerate)
{
if (topo->override_fee_rate) {
log_debug(topo->log, "Forcing fee rate, ignoring estimate");
Expand Down
8 changes: 4 additions & 4 deletions lightningd/chaintopology.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ struct chain_topology {
struct block *root;
struct block *tip;
struct block_map block_map;
u64 feerate[NUM_FEERATES];
u32 feerate[NUM_FEERATES];
bool startup;

/* Where to log things. */
Expand All @@ -111,10 +111,10 @@ struct chain_topology {
struct list_head outgoing_txs;

/* Force a partiular fee rate regardless of estimatefee (satoshis/kb) */
u64 *override_fee_rate;
u32 *override_fee_rate;

/* What fee we use if estimatefee fails (satoshis/kb) */
u64 default_fee_rate;
u32 default_fee_rate;

/* Transactions/txos we are watching. */
struct txwatch_hash txwatches;
Expand Down Expand Up @@ -146,7 +146,7 @@ size_t get_tx_depth(const struct chain_topology *topo,
u32 get_block_height(const struct chain_topology *topo);

/* Get fee rate in satoshi per kiloweight. */
u64 get_feerate(const struct chain_topology *topo, enum feerate feerate);
u32 get_feerate(const struct chain_topology *topo, enum feerate feerate);

/* Broadcast a single tx, and rebroadcast as reqd (copies tx).
* If failed is non-NULL, call that and don't rebroadcast. */
Expand Down
Loading

0 comments on commit f1e4cad

Please sign in to comment.