daemons: use amount_msat/amount_sat in all internal wire transfers.

As a side-effect of using amount_msat in gossipd/routing.c, we explicitly
handle overflows and don't need to pre-prune ridiculous-fee channels.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2019-02-21 14:15:55 +10:30
parent 0d30b89043
commit 3ac0e814d0
57 changed files with 1012 additions and 800 deletions

View File

@@ -9,14 +9,17 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *their_script,
const struct bitcoin_txid *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them,
uint64_t dust_limit)
struct amount_sat funding,
struct amount_sat to_us,
struct amount_sat to_them,
struct amount_sat dust_limit)
{
struct bitcoin_tx *tx;
size_t num_outputs = 0;
struct amount_sat total_out;
assert(to_us + to_them <= anchor_satoshis);
assert(amount_sat_add(&total_out, to_us, to_them));
assert(amount_sat_less_eq(total_out, funding));
/* BOLT #3:
*
@@ -34,19 +37,19 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
/* Our input spends the anchor tx output. */
tx->input[0].txid = *anchor_txid;
tx->input[0].index = anchor_index;
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
tx->input[0].amount = tal_dup(tx->input, u64, &funding.satoshis);
if (to_us >= dust_limit) {
if (amount_sat_greater_eq(to_us, dust_limit)) {
/* One output is to us. */
tx->output[num_outputs].amount = to_us;
tx->output[num_outputs].amount = to_us.satoshis;
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
our_script, tal_count(our_script), 0);
num_outputs++;
}
if (to_them >= dust_limit) {
if (amount_sat_greater_eq(to_them, dust_limit)) {
/* Other output is to them. */
tx->output[num_outputs].amount = to_them;
tx->output[num_outputs].amount = to_them.satoshis;
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
their_script, tal_count(their_script),
0);

View File

@@ -3,6 +3,7 @@
#include "config.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <common/amount.h>
struct pubkey;
@@ -13,7 +14,8 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *their_script,
const struct bitcoin_txid *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them,
uint64_t dust_limit);
struct amount_sat funding,
struct amount_sat to_us,
struct amount_sat to_them,
struct amount_sat dust_limit);
#endif /* LIGHTNING_COMMON_CLOSE_TX_H */

View File

@@ -10,7 +10,7 @@
void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
{
towire_u64(pptr, added->id);
towire_u64(pptr, added->amount_msat);
towire_amount_msat(pptr, added->amount);
towire_sha256(pptr, &added->payment_hash);
towire_u32(pptr, added->cltv_expiry);
towire(pptr, added->onion_routing_packet,
@@ -77,7 +77,7 @@ void fromwire_added_htlc(const u8 **cursor, size_t *max,
struct added_htlc *added)
{
added->id = fromwire_u64(cursor, max);
added->amount_msat = fromwire_u64(cursor, max);
added->amount = fromwire_amount_msat(cursor, max);
fromwire_sha256(cursor, max, &added->payment_hash);
added->cltv_expiry = fromwire_u32(cursor, max);
fromwire(cursor, max, added->onion_routing_packet,

View File

@@ -3,6 +3,7 @@
#include "config.h"
#include <bitcoin/preimage.h>
#include <ccan/short_types/short_types.h>
#include <common/amount.h>
#include <common/htlc.h>
#include <common/sphinx.h>
#include <wire/gen_onion_wire.h>
@@ -13,7 +14,7 @@ struct shachain;
/* These are how we communicate about HTLC state to the master daemon */
struct added_htlc {
u64 id;
u64 amount_msat;
struct amount_msat amount;
struct sha256 payment_hash;
u32 cltv_expiry;
u8 onion_routing_packet[TOTAL_PACKET_SIZE];

View File

@@ -307,7 +307,7 @@ static void serialize_hop_data(tal_t *ctx, u8 *dst, const struct hop_data *data)
u8 *buf = tal_arr(ctx, u8, 0);
towire_u8(&buf, data->realm);
towire_short_channel_id(&buf, &data->channel_id);
towire_u64(&buf, data->amt_forward);
towire_amount_msat(&buf, data->amt_forward);
towire_u32(&buf, data->outgoing_cltv);
towire_pad(&buf, 12);
towire(&buf, data->hmac, SECURITY_PARAMETER);
@@ -321,7 +321,7 @@ static void deserialize_hop_data(struct hop_data *data, const u8 *src)
size_t max = HOP_DATA_SIZE;
data->realm = fromwire_u8(&cursor, &max);
fromwire_short_channel_id(&cursor, &max, &data->channel_id);
data->amt_forward = fromwire_u64(&cursor, &max);
data->amt_forward = fromwire_amount_msat(&cursor, &max);
data->outgoing_cltv = fromwire_u32(&cursor, &max);
fromwire_pad(&cursor, &max, 12);
fromwire(&cursor, &max, &data->hmac, SECURITY_PARAMETER);

View File

@@ -67,7 +67,7 @@ enum route_next_case {
struct hop_data {
u8 realm;
struct short_channel_id channel_id;
u64 amt_forward;
struct amount_msat amt_forward;
u32 outgoing_cltv;
/* Padding omitted, will be zeroed */
u8 hmac[SECURITY_PARAMETER];

View File

@@ -15,6 +15,9 @@
/* Generated stub for fromwire */
const u8 *fromwire(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, void *copy UNNEEDED, size_t n UNNEEDED)
{ fprintf(stderr, "fromwire called!\n"); abort(); }
/* Generated stub for fromwire_amount_msat */
struct amount_msat fromwire_amount_msat(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_amount_msat called!\n"); abort(); }
/* Generated stub for fromwire_pad */
void fromwire_pad(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "fromwire_pad called!\n"); abort(); }
@@ -28,15 +31,15 @@ u16 fromwire_u16(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
/* Generated stub for fromwire_u32 */
u32 fromwire_u32(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u32 called!\n"); abort(); }
/* Generated stub for fromwire_u64 */
u64 fromwire_u64(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u64 called!\n"); abort(); }
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
/* Generated stub for towire_amount_msat */
void towire_amount_msat(u8 **pptr UNNEEDED, const struct amount_msat msat UNNEEDED)
{ fprintf(stderr, "towire_amount_msat called!\n"); abort(); }
/* Generated stub for towire_pad */
void towire_pad(u8 **pptr UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "towire_pad called!\n"); abort(); }
@@ -50,9 +53,6 @@ void towire_u16(u8 **pptr UNNEEDED, u16 v UNNEEDED)
/* Generated stub for towire_u32 */
void towire_u32(u8 **pptr UNNEEDED, u32 v UNNEEDED)
{ fprintf(stderr, "towire_u32 called!\n"); abort(); }
/* Generated stub for towire_u64 */
void towire_u64(u8 **pptr UNNEEDED, u64 v UNNEEDED)
{ fprintf(stderr, "towire_u64 called!\n"); abort(); }
/* Generated stub for towire_u8 */
void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
{ fprintf(stderr, "towire_u8 called!\n"); abort(); }