diff --git a/channeld/channeld.c b/channeld/channeld.c index 3e2fd26ad..cd53a6395 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -232,13 +232,11 @@ static const u8 *hsm_req(const tal_t *ctx, const u8 *req TAKES) * capacity minus the cumulative reserve. * FIXME: does this need fuzz? */ -static u64 advertised_htlc_max(const struct channel *channel) +static struct amount_msat advertised_htlc_max(const struct channel *channel) { - struct amount_sat cumulative_reserve, funding, lower_bound; + struct amount_sat cumulative_reserve, lower_bound; struct amount_msat lower_bound_msat; - funding.satoshis = channel->funding_msat / 1000; - /* This shouldn't fail */ if (!amount_sat_add(&cumulative_reserve, channel->config[LOCAL].channel_reserve, @@ -252,11 +250,11 @@ static u64 advertised_htlc_max(const struct channel *channel) } /* This shouldn't fail either */ - if (!amount_sat_sub(&lower_bound, funding, cumulative_reserve)) { + if (!amount_sat_sub(&lower_bound, channel->funding, cumulative_reserve)) { status_failed(STATUS_FAIL_INTERNAL_ERROR, "funding %s - cumulative_reserve %s?", type_to_string(tmpctx, struct amount_sat, - &funding), + &channel->funding), type_to_string(tmpctx, struct amount_sat, &cumulative_reserve)); } @@ -272,7 +270,7 @@ static u64 advertised_htlc_max(const struct channel *channel) channel->chainparams->max_payment)) lower_bound_msat = channel->chainparams->max_payment; - return lower_bound_msat.millisatoshis; + return lower_bound_msat; } /* Create and send channel_update to gossipd (and maybe peer) */ @@ -296,7 +294,7 @@ static void send_channel_update(struct peer *peer, int disable_flag) peer->channel->config[REMOTE].htlc_minimum.millisatoshis, peer->fee_base, peer->fee_per_satoshi, - advertised_htlc_max(peer->channel)); + advertised_htlc_max(peer->channel).millisatoshis); wire_sync_write(GOSSIP_FD, take(msg)); } @@ -318,7 +316,7 @@ static void make_channel_local_active(struct peer *peer) msg = towire_gossipd_local_add_channel(NULL, &peer->short_channel_ids[LOCAL], &peer->node_ids[REMOTE], - peer->channel->funding_msat / 1000); + peer->channel->funding.satoshis); wire_sync_write(GOSSIP_FD, take(msg)); /* Tell gossipd and the other side what parameters we expect should @@ -2703,9 +2701,9 @@ static void init_shared_secrets(struct channel *channel, static void init_channel(struct peer *peer) { struct basepoints points[NUM_SIDES]; - u64 funding_satoshi; + struct amount_sat funding; u16 funding_txout; - u64 local_msatoshi; + struct amount_msat local_msat; struct pubkey funding_pubkey[NUM_SIDES]; struct channel_config conf[NUM_SIDES]; struct bitcoin_txid funding_txid; @@ -2730,7 +2728,7 @@ static void init_channel(struct peer *peer) if (!fromwire_channel_init(peer, msg, &peer->chain_hash, &funding_txid, &funding_txout, - &funding_satoshi, + &funding.satoshis, &conf[LOCAL], &conf[REMOTE], feerate_per_kw, &peer->feerate_min, &peer->feerate_max, @@ -2743,7 +2741,7 @@ static void init_channel(struct peer *peer) &funder, &peer->fee_base, &peer->fee_per_satoshi, - &local_msatoshi, + &local_msat.millisatoshis, &points[LOCAL], &funding_pubkey[LOCAL], &peer->node_ids[LOCAL], @@ -2805,8 +2803,8 @@ static void init_channel(struct peer *peer) peer->channel = new_full_channel(peer, &peer->chain_hash, &funding_txid, funding_txout, - funding_satoshi, - local_msatoshi, + funding, + local_msat, feerate_per_kw, &conf[LOCAL], &conf[REMOTE], &points[LOCAL], &points[REMOTE], diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index 9c38291bf..a01f89f9e 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -104,7 +104,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, u64 obscured_commitment_number, enum side side) { - u64 base_fee_msat; + struct amount_sat base_fee; struct bitcoin_tx *tx; size_t i, n, untrimmed; u32 *cltvs; @@ -125,18 +125,23 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * 2. Calculate the base [commitment transaction * fee](#fee-calculation). */ - base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed); + base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed); - SUPERVERBOSE("# base commitment transaction fee = %"PRIu64"\n", - base_fee_msat / 1000); + SUPERVERBOSE("# base commitment transaction fee = %s\n", + type_to_string(tmpctx, struct amount_sat, &base_fee)); /* BOLT #3: * * 3. Subtract this base fee from the funder (either `to_local` or * `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)). */ - try_subtract_fee(funder, side, base_fee_msat, - &self_pay_msat, &other_pay_msat); + struct amount_msat self_pay, other_pay; + self_pay.millisatoshis = self_pay_msat; + other_pay.millisatoshis = other_pay_msat; + + try_subtract_fee(funder, side, base_fee, &self_pay, &other_pay); + self_pay_msat = self_pay.millisatoshis; + other_pay_msat = other_pay.millisatoshis; #ifdef PRINT_ACTUAL_FEE { diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 4aa29001a..fa56c9bb4 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -26,8 +26,8 @@ struct channel *new_full_channel(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, - u64 funding_satoshis, - u64 local_msatoshi, + struct amount_sat funding, + struct amount_msat local_msat, const u32 feerate_per_kw[NUM_SIDES], const struct channel_config *local, const struct channel_config *remote, @@ -41,8 +41,8 @@ struct channel *new_full_channel(const tal_t *ctx, chain_hash, funding_txid, funding_txout, - funding_satoshis, - local_msatoshi, + funding, + local_msat, feerate_per_kw[LOCAL], local, remote, local_basepoints, @@ -68,19 +68,30 @@ static void htlc_arr_append(const struct htlc ***arr, const struct htlc *htlc) tal_arr_expand(arr, htlc); } -/* What does adding the HTLC do to the balance for this side */ -static s64 balance_adding_htlc(const struct htlc *htlc, enum side side) +/* What does adding the HTLC do to the balance for this side (subtracts) */ +static bool WARN_UNUSED_RESULT balance_add_htlc(struct amount_msat *msat, + const struct htlc *htlc, + enum side side) { + struct amount_msat htlc_msat; + + htlc_msat.millisatoshis = htlc->msatoshi; + if (htlc_owner(htlc) == side) - return -(s64)htlc->msatoshi; - return 0; + return amount_msat_sub(msat, *msat, htlc_msat); + return true; } -/* What does removing the HTLC do to the balance for this side */ -static s64 balance_removing_htlc(const struct htlc *htlc, enum side side) +/* What does removing the HTLC do to the balance for this side (adds) */ +static bool WARN_UNUSED_RESULT balance_remove_htlc(struct amount_msat *msat, + const struct htlc *htlc, + enum side side) { + struct amount_msat htlc_msat; enum side paid_to; + htlc_msat.millisatoshis = htlc->msatoshi; + /* Fulfilled HTLCs are paid to recipient, otherwise returns to owner */ if (htlc->r) paid_to = !htlc_owner(htlc); @@ -88,8 +99,8 @@ static s64 balance_removing_htlc(const struct htlc *htlc, enum side side) paid_to = htlc_owner(htlc); if (side == paid_to) - return htlc->msatoshi; - return 0; + return amount_msat_add(msat, *msat, htlc_msat); + return true; } static void dump_htlc(const struct htlc *htlc, const char *prefix) @@ -164,16 +175,22 @@ static void gather_htlcs(const tal_t *ctx, } } -static u64 total_offered_msatoshis(const struct htlc **htlcs, enum side side) +static bool sum_offered_msatoshis(struct amount_msat *total, + const struct htlc **htlcs, + enum side side) { size_t i; - u64 total = 0; + *total = AMOUNT_MSAT(0); for (i = 0; i < tal_count(htlcs); i++) { - if (htlc_owner(htlcs[i]) == side) - total += htlcs[i]->msatoshi; + if (htlc_owner(htlcs[i]) == side) { + struct amount_msat m; + m.millisatoshis = htlcs[i]->msatoshi; + if (!amount_msat_add(total, *total, m)) + return false; + } } - return total; + return true; } static void add_htlcs(struct bitcoin_tx ***txs, @@ -257,14 +274,14 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx, txs = tal_arr(ctx, struct bitcoin_tx *, 1); txs[0] = commit_tx(ctx, &channel->funding_txid, channel->funding_txout, - channel->funding_msat / 1000, + channel->funding.satoshis, channel->funder, channel->config[!side].to_self_delay, &keyset, channel->view[side].feerate_per_kw, channel->config[side].dust_limit.satoshis, - channel->view[side].owed_msat[side], - channel->view[side].owed_msat[!side], + channel->view[side].owed[side].millisatoshis, + channel->view[side].owed[!side].millisatoshis, committed, htlcmap, commitment_number ^ channel->commitment_number_obscurer, @@ -290,10 +307,12 @@ static enum channel_add_err add_htlc(struct channel *channel, bool enforce_aggregate_limits) { struct htlc *htlc, *old; - s64 msat_in_htlcs, fee_msat; + struct amount_msat msat_in_htlcs, committed_msat, adding_msat, removing_msat; + struct amount_sat fee; enum side sender = htlc_state_owner(state), recipient = !sender; const struct htlc **committed, **adding, **removing; const struct channel_view *view; + bool ok; size_t i; htlc = tal(tmpctx, struct htlc); @@ -376,9 +395,21 @@ static enum channel_add_err add_htlc(struct channel *channel, return CHANNEL_ERR_TOO_MANY_HTLCS; } - msat_in_htlcs = total_offered_msatoshis(committed, htlc_owner(htlc)) - - total_offered_msatoshis(removing, htlc_owner(htlc)) - + total_offered_msatoshis(adding, htlc_owner(htlc)); + /* These cannot overflow with HTLC amount limitations, but + * maybe adding could later if they try to add a maximal HTLC. */ + if (!sum_offered_msatoshis(&committed_msat, + committed, htlc_owner(htlc)) + || !sum_offered_msatoshis(&removing_msat, + removing, htlc_owner(htlc)) + || !sum_offered_msatoshis(&adding_msat, + adding, htlc_owner(htlc))) { + return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; + } + + if (!amount_msat_add(&msat_in_htlcs, committed_msat, adding_msat) + || !amount_msat_sub(&msat_in_htlcs, msat_in_htlcs, removing_msat)) { + return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; + } /* BOLT #2: * @@ -391,7 +422,8 @@ static enum channel_add_err add_htlc(struct channel *channel, /* We don't enforce this for channel_force_htlcs: some might already * be fulfilled/failed */ if (enforce_aggregate_limits - && msat_in_htlcs > channel->config[recipient].max_htlc_value_in_flight.millisatoshis) { + && amount_msat_greater(msat_in_htlcs, + channel->config[recipient].max_htlc_value_in_flight)) { return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; } @@ -416,37 +448,57 @@ static enum channel_add_err add_htlc(struct channel *channel, - commit_tx_num_untrimmed(removing, feerate, dust, recipient); - fee_msat = commit_tx_base_fee_msat(feerate, untrimmed); + fee = commit_tx_base_fee(feerate, untrimmed); } else - fee_msat = 0; + fee = AMOUNT_SAT(0); - assert(fee_msat >= 0); + assert((s64)fee.satoshis >= 0); if (enforce_aggregate_limits) { /* Figure out what balance sender would have after applying all * pending changes. */ - s64 balance_msat = view->owed_msat[sender]; - - assert(balance_msat >= 0); - for (i = 0; i < tal_count(removing); i++) - balance_msat += balance_removing_htlc(removing[i], sender); - assert(balance_msat >= 0); - for (i = 0; i < tal_count(adding); i++) - balance_msat += balance_adding_htlc(adding[i], sender); - + struct amount_msat balance = view->owed[sender]; /* This is a little subtle: * * The change is being applied to the receiver but it will * come back to the sender after revoke_and_ack. So the check * here is that the balance to the sender doesn't go below the * sender's reserve. */ - if (balance_msat - fee_msat < (s64)channel->config[!sender].channel_reserve.satoshis * 1000) { - status_trace("balance = %"PRIu64 - ", fee is %"PRIu64 - ", reserve is %s", - balance_msat, fee_msat, + const struct amount_sat reserve + = channel->config[!sender].channel_reserve; + + assert(amount_msat_greater_eq(balance, AMOUNT_MSAT(0))); + ok = true; + for (i = 0; i < tal_count(removing); i++) + ok &= balance_remove_htlc(&balance, removing[i], sender); + assert(amount_msat_greater_eq(balance, AMOUNT_MSAT(0))); + for (i = 0; i < tal_count(adding); i++) + ok &= balance_add_htlc(&balance, adding[i], sender); + + /* Overflow shouldn't happen, but if it does, complain */ + if (!ok) { + status_broken("Failed to add %zu remove %zu htlcs", + tal_count(adding), tal_count(removing)); + return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; + } + + if (!amount_msat_sub_sat(&balance, balance, fee)) { + status_trace("Cannot afford fee %s with balance %s", type_to_string(tmpctx, struct amount_sat, - &channel->config[!sender].channel_reserve)); + &fee), + type_to_string(tmpctx, struct amount_msat, + &balance)); + return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; + } + if (!amount_msat_greater_eq_sat(balance, reserve)) { + status_trace("Cannot afford fee %s: would make balance %s" + " below reserve %s", + type_to_string(tmpctx, struct amount_sat, + &fee), + type_to_string(tmpctx, struct amount_msat, + &balance), + type_to_string(tmpctx, struct amount_sat, + &reserve)); return CHANNEL_ERR_CHANNEL_CAPACITY_EXCEEDED; } } @@ -626,23 +678,53 @@ static void htlc_incstate(struct channel *channel, /* If we've added or removed, adjust balances. */ if (!(preflags & committed_f) && (postflags & committed_f)) { - status_trace("htlc added %s: local %+"PRIi64" remote %+"PRIi64, + status_trace("htlc added %s: local %s remote %s", side_to_str(sidechanged), - balance_adding_htlc(htlc, LOCAL), - balance_adding_htlc(htlc, REMOTE)); - channel->view[sidechanged].owed_msat[LOCAL] - += balance_adding_htlc(htlc, LOCAL); - channel->view[sidechanged].owed_msat[REMOTE] - += balance_adding_htlc(htlc, REMOTE); + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[LOCAL]), + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[REMOTE])); + if (!balance_add_htlc(&channel->view[sidechanged].owed[LOCAL], + htlc, LOCAL)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Cannot add htlc #%"PRIu64" %"PRIu64 + " to LOCAL", + htlc->id, htlc->msatoshi); + if (!balance_add_htlc(&channel->view[sidechanged].owed[REMOTE], + htlc, REMOTE)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Cannot add htlc #%"PRIu64" %"PRIu64 + " to REMOTE", + htlc->id, htlc->msatoshi); + status_trace("-> local %s remote %s", + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[LOCAL]), + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[REMOTE])); } else if ((preflags & committed_f) && !(postflags & committed_f)) { - status_trace("htlc removed %s: local %+"PRIi64" remote %+"PRIi64, + status_trace("htlc added %s: local %s remote %s", side_to_str(sidechanged), - balance_removing_htlc(htlc, LOCAL), - balance_removing_htlc(htlc, REMOTE)); - channel->view[sidechanged].owed_msat[LOCAL] - += balance_removing_htlc(htlc, LOCAL); - channel->view[sidechanged].owed_msat[REMOTE] - += balance_removing_htlc(htlc, REMOTE); + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[LOCAL]), + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[REMOTE])); + if (!balance_remove_htlc(&channel->view[sidechanged].owed[LOCAL], + htlc, LOCAL)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Cannot remove htlc #%"PRIu64" %"PRIu64 + " from LOCAL", + htlc->id, htlc->msatoshi); + if (!balance_remove_htlc(&channel->view[sidechanged].owed[REMOTE], + htlc, REMOTE)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Cannot remove htlc #%"PRIu64" %"PRIu64 + " from REMOTE", + htlc->id, htlc->msatoshi); + status_trace("-> local %s remote %s", + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[LOCAL]), + type_to_string(tmpctx, struct amount_msat, + &channel->view[sidechanged].owed[REMOTE])); } } @@ -687,7 +769,8 @@ static int change_htlcs(struct channel *channel, u32 approx_max_feerate(const struct channel *channel) { size_t num; - u64 weight, avail_msat; + u64 weight; + struct amount_sat avail; const struct htlc **committed, **adding, **removing; gather_htlcs(tmpctx, channel, !channel->funder, @@ -699,16 +782,18 @@ u32 approx_max_feerate(const struct channel *channel) weight = 724 + 172 * num; /* We should never go below reserve. */ - avail_msat = channel->view[!channel->funder].owed_msat[channel->funder] - - channel->config[!channel->funder].channel_reserve.satoshis * 1000; + if (!amount_sat_sub(&avail, + amount_msat_to_sat_round_down(channel->view[!channel->funder].owed[channel->funder]), + channel->config[!channel->funder].channel_reserve)) + avail = AMOUNT_SAT(0); - /* We have to pay fee from onchain funds, so it's in satoshi. */ - return avail_msat / 1000 / weight * 1000; + return avail.satoshis / weight * 1000; } bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw) { - u64 fee_msat, dust = channel->config[!channel->funder].dust_limit.satoshis; + struct amount_sat needed, fee; + u64 dust = channel->config[!channel->funder].dust_limit.satoshis; size_t untrimmed; const struct htlc **committed, **adding, **removing; gather_htlcs(tmpctx, channel, !channel->funder, @@ -721,7 +806,7 @@ bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw - commit_tx_num_untrimmed(removing, feerate_per_kw, dust, !channel->funder); - fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed); + fee = commit_tx_base_fee(feerate_per_kw, untrimmed); /* BOLT #2: * @@ -732,8 +817,17 @@ bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw /* Note: sender == funder */ /* How much does it think it has? Must be >= reserve + fee */ - return channel->view[!channel->funder].owed_msat[channel->funder] - >= channel->config[!channel->funder].channel_reserve.satoshis * 1000 + fee_msat; + if (!amount_sat_add(&needed, fee, + channel->config[!channel->funder].channel_reserve)) + status_failed(STATUS_FAIL_INTERNAL_ERROR, + "Cannot add fee %s and reserve %s", + type_to_string(tmpctx, struct amount_sat, + &fee), + type_to_string(tmpctx, struct amount_sat, + &channel->config[!channel->funder].channel_reserve)); + + return amount_msat_greater_eq_sat(channel->view[!channel->funder].owed[channel->funder], + needed); } bool channel_update_feerate(struct channel *channel, u32 feerate_per_kw) @@ -884,10 +978,20 @@ static bool adjust_balance(struct channel *channel, struct htlc *htlc) continue; /* Add it. */ - channel->view[side].owed_msat[LOCAL] - += balance_adding_htlc(htlc, LOCAL); - channel->view[side].owed_msat[REMOTE] - += balance_adding_htlc(htlc, REMOTE); + if (!balance_add_htlc(&channel->view[side].owed[LOCAL], + htlc, LOCAL)) { + status_broken("Cannot add htlc #%"PRIu64" %"PRIu64 + " to LOCAL", + htlc->id, htlc->msatoshi); + return false; + } + if (!balance_add_htlc(&channel->view[side].owed[REMOTE], + htlc, REMOTE)) { + status_broken("Cannot add htlc #%"PRIu64" %"PRIu64 + " to REMOTE", + htlc->id, htlc->msatoshi); + return false; + } /* If it is no longer committed, remove it (depending * on fail || fulfill). */ @@ -895,18 +999,28 @@ static bool adjust_balance(struct channel *channel, struct htlc *htlc) continue; if (!htlc->fail && !htlc->failcode && !htlc->r) { - status_trace("%s HTLC %"PRIu64 - " %s neither fail nor fulfill?", - htlc_state_owner(htlc->state) == LOCAL - ? "out" : "in", - htlc->id, - htlc_state_name(htlc->state)); + status_broken("%s HTLC %"PRIu64 + " %s neither fail nor fulfill?", + htlc_state_owner(htlc->state) == LOCAL + ? "out" : "in", + htlc->id, + htlc_state_name(htlc->state)); + return false; + } + if (!balance_remove_htlc(&channel->view[side].owed[LOCAL], + htlc, LOCAL)) { + status_broken("Cannot remove htlc #%"PRIu64" %"PRIu64 + " from LOCAL", + htlc->id, htlc->msatoshi); + return false; + } + if (!balance_remove_htlc(&channel->view[side].owed[REMOTE], + htlc, REMOTE)) { + status_broken("Cannot remove htlc #%"PRIu64" %"PRIu64 + " from REMOTE", + htlc->id, htlc->msatoshi); return false; } - channel->view[side].owed_msat[LOCAL] - += balance_removing_htlc(htlc, LOCAL); - channel->view[side].owed_msat[REMOTE] - += balance_removing_htlc(htlc, REMOTE); } return true; } diff --git a/channeld/full_channel.h b/channeld/full_channel.h index 103de0a8f..925aa5304 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -12,8 +12,8 @@ * @ctx: tal context to allocate return value from. * @funding_txid: The commitment transaction id. * @funding_txout: The commitment transaction output number. - * @funding_satoshis: The commitment transaction amount. - * @local_msatoshi: The amount for the local side (remainder goes to remote) + * @funding: The commitment transaction amount. + * @local_msat: The amount for the local side (remainder goes to remote) * @feerate_per_kw: feerate per kiloweight (satoshis) for the commitment * transaction and HTLCS for each side. * @local: local channel configuration @@ -30,8 +30,8 @@ struct channel *new_full_channel(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, - u64 funding_satoshis, - u64 local_msatoshi, + struct amount_sat funding, + struct amount_msat local_msat, const u32 feerate_per_kw[NUM_SIDES], const struct channel_config *local, const struct channel_config *remote, diff --git a/channeld/test/Makefile b/channeld/test/Makefile index 2a5598cba..623aa6403 100644 --- a/channeld/test/Makefile +++ b/channeld/test/Makefile @@ -8,6 +8,7 @@ ALL_TEST_PROGRAMS += $(CHANNELD_TEST_PROGRAMS) ALL_OBJS += $(CHANNELD_TEST_OBJS) CHANNELD_TEST_COMMON_OBJS := \ + common/amount.o \ common/daemon_conn.o \ common/htlc_state.o \ common/htlc_tx.o \ diff --git a/channeld/test/run-commit_tx.c b/channeld/test/run-commit_tx.c index 443551c61..aad480491 100644 --- a/channeld/test/run-commit_tx.c +++ b/channeld/test/run-commit_tx.c @@ -917,9 +917,12 @@ int main(void) /* Now make sure we cover case where funder can't afford the fee; * its output cannot go negative! */ for (;;) { - u64 base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, 0); + struct amount_msat to_local; + struct amount_sat base_fee + = commit_tx_base_fee(feerate_per_kw, 0); - if (base_fee_msat <= to_local_msat) { + to_local.millisatoshis = to_local_msat; + if (amount_msat_greater_eq_sat(to_local, base_fee)) { feerate_per_kw++; continue; } diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index fde0e25ad..3e2aa9791 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -14,6 +14,10 @@ #include /* AUTOGENERATED MOCKS START */ +/* Generated stub for status_failed */ +void status_failed(enum status_failreason code UNNEEDED, + const char *fmt UNNEEDED, ...) +{ fprintf(stderr, "status_failed called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ void status_fmt(enum log_level level UNUSED, const char *fmt, ...) @@ -324,7 +328,7 @@ int main(void) struct bitcoin_txid funding_txid; /* We test from both sides. */ struct channel *lchannel, *rchannel; - u64 funding_amount_satoshi; + struct amount_sat funding_amount; u32 *feerate_per_kw; unsigned int funding_output_index; struct keyset keyset; @@ -334,7 +338,7 @@ int main(void) struct pubkey *unknown; struct bitcoin_tx *raw_tx, **txs, **txs2; struct channel_config *local_config, *remote_config; - u64 to_local_msat, to_remote_msat; + struct amount_msat to_local_msat, to_remote_msat; const struct htlc **htlc_map, **htlcs; const u8 *funding_wscript, **wscripts; size_t i; @@ -376,7 +380,7 @@ int main(void) */ funding_txid = txid_from_hex("8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be"); funding_output_index = 0; - funding_amount_satoshi = 10000000; + funding_amount = AMOUNT_SAT(10000000); remote_config->to_self_delay = 144; local_config->dust_limit = AMOUNT_SAT(546); @@ -444,13 +448,13 @@ int main(void) * local_feerate_per_kw: 15000 */ - to_local_msat = 7000000000; - to_remote_msat = 3000000000; + to_local_msat = AMOUNT_MSAT(7000000000); + to_remote_msat = AMOUNT_MSAT(3000000000); feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 15000; lchannel = new_full_channel(tmpctx, &chainparams->genesis_blockhash, &funding_txid, funding_output_index, - funding_amount_satoshi, to_local_msat, + funding_amount, to_local_msat, feerate_per_kw, local_config, remote_config, @@ -461,7 +465,7 @@ int main(void) rchannel = new_full_channel(tmpctx, &chainparams->genesis_blockhash, &funding_txid, funding_output_index, - funding_amount_satoshi, to_remote_msat, + funding_amount, to_remote_msat, feerate_per_kw, remote_config, local_config, @@ -491,13 +495,13 @@ int main(void) keyset.other_htlc_key = keyset.other_payment_key; raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount_satoshi, + funding_amount.satoshis, LOCAL, remote_config->to_self_delay, &keyset, feerate_per_kw[LOCAL], local_config->dust_limit.satoshis, - to_local_msat, - to_remote_msat, + to_local_msat.millisatoshis, + to_remote_msat.millisatoshis, NULL, &htlc_map, 0x2bb038521914 ^ 42, LOCAL); txs = channel_txs(tmpctx, &htlc_map, &wscripts, @@ -519,8 +523,8 @@ int main(void) * to_remote_msat: 3000000000 * local_feerate_per_kw: 0 */ - to_local_msat = 6988000000; - to_remote_msat = 3000000000; + to_local_msat = AMOUNT_MSAT(6988000000); + to_remote_msat = AMOUNT_MSAT(3000000000); feerate_per_kw[LOCAL] = feerate_per_kw[REMOTE] = 0; /* Now, BOLT doesn't adjust owed amounts the same way we do @@ -530,10 +534,10 @@ int main(void) send_and_fulfill_htlc(lchannel, LOCAL, 7000000); send_and_fulfill_htlc(rchannel, REMOTE, 7000000); - assert(lchannel->view[LOCAL].owed_msat[LOCAL] - == rchannel->view[REMOTE].owed_msat[REMOTE]); - assert(lchannel->view[REMOTE].owed_msat[REMOTE] - == rchannel->view[LOCAL].owed_msat[LOCAL]); + assert(lchannel->view[LOCAL].owed[LOCAL].millisatoshis + == rchannel->view[REMOTE].owed[REMOTE].millisatoshis); + assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis + == rchannel->view[LOCAL].owed[LOCAL].millisatoshis); txs = channel_txs(tmpctx, &htlc_map, &wscripts, lchannel, &local_per_commitment_point, 42, LOCAL); @@ -548,10 +552,10 @@ int main(void) htlcs = include_htlcs(lchannel, LOCAL); include_htlcs(rchannel, REMOTE); - assert(lchannel->view[LOCAL].owed_msat[LOCAL] - == rchannel->view[REMOTE].owed_msat[REMOTE]); - assert(lchannel->view[REMOTE].owed_msat[REMOTE] - == rchannel->view[LOCAL].owed_msat[LOCAL]); + assert(lchannel->view[LOCAL].owed[LOCAL].millisatoshis + == rchannel->view[REMOTE].owed[REMOTE].millisatoshis); + assert(lchannel->view[REMOTE].owed[REMOTE].millisatoshis + == rchannel->view[LOCAL].owed[LOCAL].millisatoshis); txs = channel_txs(tmpctx, &htlc_map, &wscripts, lchannel, &local_per_commitment_point, 42, LOCAL); @@ -609,13 +613,13 @@ int main(void) rchannel->view[REMOTE].feerate_per_kw = feerate_per_kw[REMOTE]; raw_tx = commit_tx(tmpctx, &funding_txid, funding_output_index, - funding_amount_satoshi, + funding_amount.satoshis, LOCAL, remote_config->to_self_delay, &keyset, feerate_per_kw[LOCAL], local_config->dust_limit.satoshis, - to_local_msat, - to_remote_msat, + to_local_msat.millisatoshis, + to_remote_msat.millisatoshis, htlcs, &htlc_map, 0x2bb038521914 ^ 42, LOCAL); diff --git a/common/initial_channel.c b/common/initial_channel.c index 0fe160938..06823f535 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -12,8 +12,8 @@ struct channel *new_initial_channel(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, - u64 funding_satoshis, - u64 local_msatoshi, + struct amount_sat funding, + struct amount_msat local_msatoshi, u32 feerate_per_kw, const struct channel_config *local, const struct channel_config *remote, @@ -24,14 +24,13 @@ struct channel *new_initial_channel(const tal_t *ctx, enum side funder) { struct channel *channel = tal(ctx, struct channel); + struct amount_msat remote_msatoshi; channel->funding_txid = *funding_txid; channel->funding_txout = funding_txout; - if (funding_satoshis > UINT64_MAX / 1000) - return tal_free(channel); - - channel->funding_msat = funding_satoshis * 1000; - if (local_msatoshi > channel->funding_msat) + channel->funding = funding; + if (!amount_sat_sub_msat(&remote_msatoshi, + channel->funding, local_msatoshi)) return tal_free(channel); channel->funder = funder; @@ -47,12 +46,12 @@ struct channel *new_initial_channel(const tal_t *ctx, = channel->view[REMOTE].feerate_per_kw = feerate_per_kw; - channel->view[LOCAL].owed_msat[LOCAL] - = channel->view[REMOTE].owed_msat[LOCAL] + channel->view[LOCAL].owed[LOCAL] + = channel->view[REMOTE].owed[LOCAL] = local_msatoshi; - channel->view[REMOTE].owed_msat[REMOTE] - = channel->view[LOCAL].owed_msat[REMOTE] - = channel->funding_msat - local_msatoshi; + channel->view[REMOTE].owed[REMOTE] + = channel->view[LOCAL].owed[REMOTE] + = remote_msatoshi; channel->basepoints[LOCAL] = *local_basepoints; channel->basepoints[REMOTE] = *remote_basepoints; @@ -91,16 +90,16 @@ struct bitcoin_tx *initial_channel_tx(const tal_t *ctx, return initial_commit_tx(ctx, &channel->funding_txid, channel->funding_txout, - channel->funding_msat / 1000, + channel->funding, channel->funder, /* They specify our to_self_delay and v.v. */ channel->config[!side].to_self_delay, &keyset, channel->view[side].feerate_per_kw, - channel->config[side].dust_limit.satoshis, - channel->view[side].owed_msat[side], - channel->view[side].owed_msat[!side], - channel->config[!side].channel_reserve.satoshis * 1000, + channel->config[side].dust_limit, + channel->view[side].owed[side], + channel->view[side].owed[!side], + channel->config[!side].channel_reserve, 0 ^ channel->commitment_number_obscurer, side); } @@ -108,21 +107,24 @@ 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=%"PRIu32"," - " owed_local=%"PRIu64"," - " owed_remote=%"PRIu64" }", + " owed_local=%s," + " owed_remote=%s }", view->feerate_per_kw, - view->owed_msat[LOCAL], - view->owed_msat[REMOTE]); + type_to_string(tmpctx, struct amount_msat, + &view->owed[LOCAL]), + type_to_string(tmpctx, struct amount_msat, + &view->owed[REMOTE])); } /* FIXME: This should reference HTLCs somehow. */ static char *fmt_channel(const tal_t *ctx, const struct channel *channel) { - return tal_fmt(ctx, "{ funding_msat=%"PRIu64"," + return tal_fmt(ctx, "{ funding=%s," " funder=%s," " local=%s," " remote=%s }", - channel->funding_msat, + type_to_string(tmpctx, struct amount_sat, + &channel->funding), side_to_str(channel->funder), fmt_channel_view(ctx, &channel->view[LOCAL]), fmt_channel_view(ctx, &channel->view[REMOTE])); diff --git a/common/initial_channel.h b/common/initial_channel.h index c45b6f599..0966d45b4 100644 --- a/common/initial_channel.h +++ b/common/initial_channel.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -23,7 +24,7 @@ struct channel_view { u32 feerate_per_kw; /* How much is owed to each side (includes pending changes) */ - u64 owed_msat[NUM_SIDES]; + struct amount_msat owed[NUM_SIDES]; }; struct channel { @@ -34,8 +35,8 @@ struct channel { /* Keys used to spend funding tx. */ struct pubkey funding_pubkey[NUM_SIDES]; - /* Millisatoshis in from commitment tx */ - u64 funding_msat; + /* satoshis in from commitment tx */ + struct amount_sat funding; /* Who is paying fees. */ enum side funder; @@ -86,8 +87,8 @@ struct channel *new_initial_channel(const tal_t *ctx, const struct bitcoin_blkid *chain_hash, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, - u64 funding_satoshis, - u64 local_msatoshi, + struct amount_sat funding, + struct amount_msat local_msatoshi, u32 feerate_per_kw, const struct channel_config *local, const struct channel_config *remote, diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index 8d246a666..372f30603 100644 --- a/common/initial_commit_tx.c +++ b/common/initial_commit_tx.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -31,22 +32,22 @@ u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint, } bool try_subtract_fee(enum side funder, enum side side, - u64 base_fee_msat, u64 *self_msat, u64 *other_msat) + struct amount_sat base_fee, + struct amount_msat *self, + struct amount_msat *other) { - u64 *funder_msat; + struct amount_msat *funder_amount; if (funder == side) - funder_msat = self_msat; + funder_amount = self; else - funder_msat = other_msat; + funder_amount = other; - if (*funder_msat >= base_fee_msat) { - *funder_msat -= base_fee_msat; + if (amount_msat_sub_sat(funder_amount, *funder_amount, base_fee)) return true; - } else { - *funder_msat = 0; - return false; - } + + *funder_amount = AMOUNT_MSAT(0); + return false; } u8 *to_self_wscript(const tal_t *ctx, @@ -61,23 +62,26 @@ u8 *to_self_wscript(const tal_t *ctx, struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, - u64 funding_satoshis, + struct amount_sat funding, enum side funder, u16 to_self_delay, const struct keyset *keyset, u32 feerate_per_kw, - u64 dust_limit_satoshis, - u64 self_pay_msat, - u64 other_pay_msat, - u64 self_reserve_msat, + struct amount_sat dust_limit, + struct amount_msat self_pay, + struct amount_msat other_pay, + struct amount_sat self_reserve, u64 obscured_commitment_number, enum side side) { - u64 base_fee_msat; + struct amount_sat base_fee; struct bitcoin_tx *tx; size_t n, untrimmed; + struct amount_msat total_pay; - assert(self_pay_msat + other_pay_msat <= funding_satoshis * 1000); + if (!amount_msat_add(&total_pay, self_pay, other_pay)) + abort(); + assert(!amount_msat_greater_sat(total_pay, funding)); /* BOLT #3: * @@ -91,15 +95,14 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * 2. Calculate the base [commitment transaction * fee](#fee-calculation). */ - base_fee_msat = commit_tx_base_fee_msat(feerate_per_kw, untrimmed); + base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed); /* BOLT #3: * * 3. Subtract this base fee from the funder (either `to_local` or * `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)). */ - if (!try_subtract_fee(funder, side, base_fee_msat, - &self_pay_msat, &other_pay_msat)) { + if (!try_subtract_fee(funder, side, base_fee, &self_pay, &other_pay)) { /* BOLT #2: * * The receiving node MUST fail the channel if: @@ -120,15 +123,18 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * commitment transaction are less than or equal to * `channel_reserve_satoshis`. */ - if (self_pay_msat <= self_reserve_msat - && other_pay_msat <= self_reserve_msat) { - status_unusual("Neither self amount %"PRIu64 - " nor other amount %"PRIu64 - " exceed reserve %"PRIu64 + if (!amount_msat_greater_sat(self_pay, self_reserve) + && !amount_msat_greater_sat(other_pay, self_reserve)) { + status_unusual("Neither self amount %s" + " nor other amount %s" + " exceed reserve %s" " on initial commitment transaction", - self_pay_msat / 1000, - other_pay_msat / 1000, - self_reserve_msat / 1000); + type_to_string(tmpctx, struct amount_msat, + &self_pay), + type_to_string(tmpctx, struct amount_msat, + &other_pay), + type_to_string(tmpctx, struct amount_sat, + &self_reserve)); return NULL; } @@ -158,9 +164,9 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * `dust_limit_satoshis`, add a [`to_local` * output](#to_local-output). */ - if (self_pay_msat / 1000 >= dust_limit_satoshis) { + if (amount_msat_greater_eq_sat(self_pay, dust_limit)) { u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset); - tx->output[n].amount = self_pay_msat / 1000; + tx->output[n].amount = amount_msat_to_sat_round_down(self_pay).satoshis; tx->output[n].script = scriptpubkey_p2wsh(tx, wscript); n++; } @@ -171,7 +177,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * `dust_limit_satoshis`, add a [`to_remote` * output](#to_remote-output). */ - if (other_pay_msat / 1000 >= dust_limit_satoshis) { + if (amount_msat_greater_eq_sat(other_pay, dust_limit)) { /* BOLT #3: * * #### `to_remote` Output @@ -179,7 +185,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * This output sends funds to the other peer and thus is a simple * P2WPKH to `remotepubkey`. */ - tx->output[n].amount = other_pay_msat / 1000; + tx->output[n].amount = amount_msat_to_sat_round_down(other_pay).satoshis; tx->output[n].script = scriptpubkey_p2wpkh(tx, &keyset->other_payment_key); n++; @@ -228,7 +234,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); /* Input amount needed for signature code. */ - tx->input[0].amount = tal_dup(tx->input, u64, &funding_satoshis); + tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis); return tx; } diff --git a/common/initial_commit_tx.h b/common/initial_commit_tx.h index bb31afabf..fd6830f61 100644 --- a/common/initial_commit_tx.h +++ b/common/initial_commit_tx.h @@ -3,6 +3,7 @@ #define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H #include "config.h" #include +#include #include struct keyset; @@ -18,8 +19,8 @@ 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_sat(u32 feerate_per_kw, - size_t num_untrimmed_htlcs) +static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw, + size_t num_untrimmed_htlcs) { u64 weight; @@ -44,26 +45,20 @@ static inline u64 commit_tx_base_fee_sat(u32 feerate_per_kw, * 3. Multiply `feerate_per_kw` by `weight`, divide by 1000 (rounding * down). */ - return (feerate_per_kw * weight / 1000); -} - -static inline u64 commit_tx_base_fee_msat(u32 feerate_per_kw, - size_t num_untrimmed_htlcs) -{ - return commit_tx_base_fee_sat(feerate_per_kw, num_untrimmed_htlcs) - * 1000; + return amount_tx_fee(feerate_per_kw, weight); } /** * initial_commit_tx: create (unsigned) commitment tx to spend the funding tx output * @ctx: context to allocate transaction and @htlc_map from. - * @funding_txid, @funding_out, @funding_satoshis: funding outpoint. + * @funding_txid, @funding_out, @funding: funding outpoint. * @funder: is the LOCAL or REMOTE paying the fee? * @keyset: keys derived for this commit tx. * @feerate_per_kw: feerate to use - * @dust_limit_satoshis: dust limit below which to trim outputs. - * @self_pay_msat: amount to pay directly to self - * @other_pay_msat: amount to pay directly to the other side + * @dust_limit: dust limit below which to trim outputs. + * @self_pay: amount to pay directly to self + * @other_pay: amount to pay directly to the other side + * @self_reserve: reserve the other side insisted we have * @obscured_commitment_number: number to encode in commitment transaction * @side: side to generate commitment transaction for. * @@ -74,21 +69,23 @@ static inline u64 commit_tx_base_fee_msat(u32 feerate_per_kw, struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, const struct bitcoin_txid *funding_txid, unsigned int funding_txout, - u64 funding_satoshis, + struct amount_sat funding, enum side funder, u16 to_self_delay, const struct keyset *keyset, u32 feerate_per_kw, - u64 dust_limit_satoshis, - u64 self_pay_msat, - u64 other_pay_msat, - u64 self_reserve_msat, + struct amount_sat dust_limit, + struct amount_msat self_pay, + struct amount_msat other_pay, + struct amount_sat self_reserve, u64 obscured_commitment_number, enum side side); /* try_subtract_fee - take away this fee from the funder (and return true), or all if insufficient (and return false). */ bool try_subtract_fee(enum side funder, enum side side, - u64 base_fee_msat, u64 *self_msat, u64 *other_msat); + struct amount_sat base_fee, + struct amount_msat *self, + struct amount_msat *other); /* Generate the witness script for the to-self output: * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */ diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 6316ccc62..a0a0e07c2 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -184,11 +184,11 @@ void peer_start_closingd(struct channel *channel, * fee of the final commitment transaction, as calculated in * [BOLT #3](03-transactions.md#fee-calculation). */ - feelimit = commit_tx_base_fee_sat(channel->channel_info.feerate_per_kw[LOCAL], - 0); + feelimit = commit_tx_base_fee(channel->channel_info.feerate_per_kw[LOCAL], + 0).satoshis; /* Pick some value above slow feerate (or min possible if unknown) */ - minfee = commit_tx_base_fee_sat(feerate_min(ld, NULL), 0); + minfee = commit_tx_base_fee(feerate_min(ld, NULL), 0).satoshis; /* If we can't determine feerate, start at half unilateral feerate. */ feerate = mutual_close_feerate(ld->topology); @@ -197,7 +197,7 @@ void peer_start_closingd(struct channel *channel, if (feerate < feerate_floor()) feerate = feerate_floor(); } - startfee = commit_tx_base_fee_sat(feerate, 0); + startfee = commit_tx_base_fee(feerate, 0).satoshis; if (startfee > feelimit) startfee = feelimit; diff --git a/openingd/openingd.c b/openingd/openingd.c index d367f644a..9a5897587 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -444,6 +444,7 @@ static u8 *funder_channel(struct state *state, u32 minimum_depth; const u8 *wscript; struct bitcoin_tx *funding; + struct amount_msat local_msat; /*~ For symmetry, we calculate our own reserve even though lightningd * could do it for the we-are-funding case. */ @@ -474,7 +475,7 @@ static u8 *funder_channel(struct state *state, * - MUST set `push_msat` to equal or less than 1000 * * `funding_satoshis`. */ - if (amount_msat_greater_sat(state->push_msat, state->funding)) + if (!amount_sat_sub_msat(&local_msat, state->funding, state->push_msat)) status_failed(STATUS_FAIL_MASTER_IO, "push-msat must be < %s", type_to_string(tmpctx, struct amount_sat, @@ -639,9 +640,8 @@ static u8 *funder_channel(struct state *state, &state->chainparams->genesis_blockhash, &state->funding_txid, state->funding_txout, - state->funding.satoshis, - state->funding.satoshis * 1000 - - state->push_msat.millisatoshis, + state->funding, + local_msat, state->feerate_per_kw, &state->localconf, &state->remoteconf, @@ -686,7 +686,7 @@ static u8 *funder_channel(struct state *state, msg = towire_hsm_sign_remote_commitment_tx(NULL, tx, &state->channel->funding_pubkey[REMOTE], - state->channel->funding_msat / 1000); + state->channel->funding.satoshis); wire_sync_write(HSM_FD, take(msg)); msg = wire_sync_read(tmpctx, HSM_FD); @@ -1044,8 +1044,8 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) &chain_hash, &state->funding_txid, state->funding_txout, - state->funding.satoshis, - state->push_msat.millisatoshis, + state->funding, + state->push_msat, state->feerate_per_kw, &state->localconf, &state->remoteconf, @@ -1139,7 +1139,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) msg = towire_hsm_sign_remote_commitment_tx(NULL, remote_commit, &state->channel->funding_pubkey[REMOTE], - state->channel->funding_msat / 1000); + state->channel->funding.satoshis); wire_sync_write(HSM_FD, take(msg)); msg = wire_sync_read(tmpctx, HSM_FD);