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

@@ -3,6 +3,7 @@ WALLET_TEST_OBJS := $(WALLET_TEST_SRC:.c=.o)
WALLET_TEST_PROGRAMS := $(WALLET_TEST_OBJS:.o=)
WALLET_TEST_COMMON_OBJS := \
common/amount.o \
common/base32.o \
common/derive_basepoints.o \
common/htlc_state.o \

View File

@@ -30,15 +30,6 @@ static void db_log_(struct log *log UNUSED, enum log_level level UNUSED, const c
bool deprecated_apis = true;
/* AUTOGENERATED MOCKS START */
/* Generated stub for amount_msat_sub_sat */
bool amount_msat_sub_sat(struct amount_msat *val UNNEEDED,
struct amount_msat a UNNEEDED,
struct amount_sat b UNNEEDED)
{ fprintf(stderr, "amount_msat_sub_sat called!\n"); abort(); }
/* Generated stub for amount_sat_to_msat */
bool amount_sat_to_msat(struct amount_msat *msat UNNEEDED,
struct amount_sat sat UNNEEDED)
{ fprintf(stderr, "amount_sat_to_msat called!\n"); abort(); }
/* Generated stub for bitcoind_gettxout */
void bitcoind_gettxout(struct bitcoind *bitcoind UNNEEDED,
const struct bitcoin_txid *txid UNNEEDED, const u32 outnum UNNEEDED,
@@ -493,7 +484,7 @@ u8 *towire_channel_got_commitsig_reply(const tal_t *ctx UNNEEDED)
u8 *towire_channel_got_revoke_reply(const tal_t *ctx UNNEEDED)
{ fprintf(stderr, "towire_channel_got_revoke_reply called!\n"); abort(); }
/* Generated stub for towire_channel_offer_htlc */
u8 *towire_channel_offer_htlc(const tal_t *ctx UNNEEDED, u64 amount_msat UNNEEDED, u32 cltv_expiry UNNEEDED, const struct sha256 *payment_hash UNNEEDED, const u8 onion_routing_packet[1366])
u8 *towire_channel_offer_htlc(const tal_t *ctx UNNEEDED, struct amount_msat amount_msat UNNEEDED, u32 cltv_expiry UNNEEDED, const struct sha256 *payment_hash UNNEEDED, const u8 onion_routing_packet[1366])
{ fprintf(stderr, "towire_channel_offer_htlc called!\n"); abort(); }
/* Generated stub for towire_channel_sending_commitsig_reply */
u8 *towire_channel_sending_commitsig_reply(const tal_t *ctx UNNEEDED)
@@ -516,7 +507,7 @@ u8 *towire_errorfmt(const tal_t *ctx UNNEEDED,
u8 *towire_gossip_get_channel_peer(const tal_t *ctx UNNEEDED, const struct short_channel_id *channel_id UNNEEDED)
{ fprintf(stderr, "towire_gossip_get_channel_peer called!\n"); abort(); }
/* Generated stub for towire_hsm_sign_commitment_tx */
u8 *towire_hsm_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct pubkey *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, u64 funding_amount UNNEEDED)
u8 *towire_hsm_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct pubkey *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, struct amount_sat funding_amount UNNEEDED)
{ fprintf(stderr, "towire_hsm_sign_commitment_tx called!\n"); abort(); }
/* Generated stub for towire_onchain_dev_memleak */
u8 *towire_onchain_dev_memleak(const tal_t *ctx UNNEEDED)
@@ -821,7 +812,7 @@ static bool channelseq(struct channel *c1, struct channel *c2)
CHECK_MSG(pubkey_eq(&p1->id, &p2->id), "NodeIDs do not match");
CHECK((c1->scid == NULL && c2->scid == NULL)
|| short_channel_id_eq(c1->scid, c2->scid));
CHECK(c1->our_msatoshi == c2->our_msatoshi);
CHECK(amount_msat_eq(c1->our_msat, c2->our_msat));
CHECK((c1->remote_shutdown_scriptpubkey == NULL && c2->remote_shutdown_scriptpubkey == NULL) || memeq(
c1->remote_shutdown_scriptpubkey,
tal_count(c1->remote_shutdown_scriptpubkey),
@@ -1040,12 +1031,12 @@ static bool test_htlc_crud(struct lightningd *ld, const tal_t *ctx)
memset(&payment_key, 'B', sizeof(payment_key));
in.key.id = 42;
in.key.channel = chan;
in.msatoshi = 42;
in.msat = AMOUNT_MSAT(42);
out.in = &in;
out.key.id = 1337;
out.key.channel = chan;
out.msatoshi = 41;
out.msat = AMOUNT_MSAT(41);
/* Store the htlc_in */
CHECK_MSG(transaction_wrap(w->db, wallet_htlc_save_in(w, chan, &in)),

View File

@@ -680,13 +680,13 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s
sqlite3_column_int64(stmt, 11),
&funding_txid,
sqlite3_column_int(stmt, 13),
sqlite3_column_int64(stmt, 14),
sqlite3_column_int64(stmt, 16),
sqlite3_column_amount_sat(stmt, 14),
sqlite3_column_amount_msat(stmt, 16),
sqlite3_column_int(stmt, 15) != 0,
scid,
sqlite3_column_int64(stmt, 17),
sqlite3_column_int64(stmt, 38), /* msatoshi_to_us_min */
sqlite3_column_int64(stmt, 39), /* msatoshi_to_us_max */
sqlite3_column_amount_msat(stmt, 17),
sqlite3_column_amount_msat(stmt, 38), /* msatoshi_to_us_min */
sqlite3_column_amount_msat(stmt, 39), /* msatoshi_to_us_max */
sqlite3_column_tx(tmpctx, stmt, 32),
&last_sig,
wallet_htlc_sigs_load(tmpctx, w,
@@ -761,7 +761,7 @@ void wallet_channel_stats_incr_x(struct wallet *w,
char const *dir,
char const *typ,
u64 cdbid,
u64 msatoshi)
struct amount_msat msat)
{
char const *payments_stat = tal_fmt(tmpctx, "%s_payments_%s",
dir, typ);
@@ -773,24 +773,28 @@ void wallet_channel_stats_incr_x(struct wallet *w,
" , %s = COALESCE(%s, 0) + %"PRIu64""
" WHERE id = %"PRIu64";",
payments_stat, payments_stat,
msatoshi_stat, msatoshi_stat, msatoshi,
msatoshi_stat, msatoshi_stat, msat.millisatoshis,
cdbid);
sqlite3_stmt *stmt = db_prepare(w->db, qry);
db_exec_prepared(w->db, stmt);
}
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 id, u64 m)
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "in", "offered", id, m);
}
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 id, u64 m)
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "in", "fulfilled", id, m);
}
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 id, u64 m)
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "out", "offered", id, m);
}
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 id, u64 m)
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 id,
struct amount_msat m)
{
wallet_channel_stats_incr_x(w, "out", "fulfilled", id, m);
}
@@ -817,12 +821,12 @@ void wallet_channel_stats_load(struct wallet *w,
stats->in_payments_offered = sqlite3_column_int64(stmt, 0);
stats->in_payments_fulfilled = sqlite3_column_int64(stmt, 1);
stats->in_msatoshi_offered = sqlite3_column_int64(stmt, 2);
stats->in_msatoshi_fulfilled = sqlite3_column_int64(stmt, 3);
stats->in_msatoshi_offered = sqlite3_column_amount_msat(stmt, 2);
stats->in_msatoshi_fulfilled = sqlite3_column_amount_msat(stmt, 3);
stats->out_payments_offered = sqlite3_column_int64(stmt, 4);
stats->out_payments_fulfilled = sqlite3_column_int64(stmt, 5);
stats->out_msatoshi_offered = sqlite3_column_int64(stmt, 6);
stats->out_msatoshi_fulfilled = sqlite3_column_int64(stmt, 7);
stats->out_msatoshi_offered = sqlite3_column_amount_msat(stmt, 6);
stats->out_msatoshi_fulfilled = sqlite3_column_amount_msat(stmt, 7);
db_stmt_done(stmt);
}
@@ -960,10 +964,10 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
sqlite3_bind_sha256_double(stmt, 10, &chan->funding_txid.shad);
sqlite3_bind_int(stmt, 11, chan->funding_outnum);
sqlite3_bind_int64(stmt, 12, chan->funding_satoshi);
sqlite3_bind_amount_sat(stmt, 12, chan->funding);
sqlite3_bind_int(stmt, 13, chan->remote_funding_locked);
sqlite3_bind_int64(stmt, 14, chan->push_msat);
sqlite3_bind_int64(stmt, 15, chan->our_msatoshi);
sqlite3_bind_amount_msat(stmt, 14, chan->push);
sqlite3_bind_amount_msat(stmt, 15, chan->our_msat);
if (chan->remote_shutdown_scriptpubkey)
sqlite3_bind_blob(stmt, 16, chan->remote_shutdown_scriptpubkey,
@@ -979,8 +983,8 @@ void wallet_channel_save(struct wallet *w, struct channel *chan)
sqlite3_bind_int(stmt, 21, chan->last_was_revoke);
sqlite3_bind_int(stmt, 22, chan->min_possible_feerate);
sqlite3_bind_int(stmt, 23, chan->max_possible_feerate);
sqlite3_bind_int64(stmt, 24, chan->msatoshi_to_us_min);
sqlite3_bind_int64(stmt, 25, chan->msatoshi_to_us_max);
sqlite3_bind_amount_msat(stmt, 24, chan->msat_to_us_min);
sqlite3_bind_amount_msat(stmt, 25, chan->msat_to_us_max);
sqlite3_bind_int64(stmt, 26, chan->dbid);
db_exec_prepared(w->db, stmt);
@@ -1181,7 +1185,7 @@ void wallet_htlc_save_in(struct wallet *wallet,
sqlite3_bind_int64(stmt, 1, chan->dbid);
sqlite3_bind_int64(stmt, 2, in->key.id);
sqlite3_bind_int(stmt, 3, DIRECTION_INCOMING);
sqlite3_bind_int64(stmt, 4, in->msatoshi);
sqlite3_bind_amount_msat(stmt, 4, in->msat);
sqlite3_bind_int(stmt, 5, in->cltv_expiry);
sqlite3_bind_sha256(stmt, 6, &in->payment_hash);
@@ -1236,7 +1240,7 @@ void wallet_htlc_save_out(struct wallet *wallet,
sqlite3_bind_int64(stmt, 4, out->in->dbid);
else
sqlite3_bind_null(stmt, 4);
sqlite3_bind_int64(stmt, 5, out->msatoshi);
sqlite3_bind_amount_msat(stmt, 5, out->msat);
sqlite3_bind_int(stmt, 6, out->cltv_expiry);
sqlite3_bind_sha256(stmt, 7, &out->payment_hash);
@@ -1302,7 +1306,7 @@ static bool wallet_stmt2htlc_in(struct channel *channel,
in->dbid = sqlite3_column_int64(stmt, 0);
in->key.id = sqlite3_column_int64(stmt, 1);
in->key.channel = channel;
in->msatoshi = sqlite3_column_int64(stmt, 2);
in->msat = sqlite3_column_amount_msat(stmt, 2);
in->cltv_expiry = sqlite3_column_int(stmt, 3);
in->hstate = sqlite3_column_int(stmt, 4);
@@ -1345,7 +1349,7 @@ static bool wallet_stmt2htlc_out(struct channel *channel,
out->dbid = sqlite3_column_int64(stmt, 0);
out->key.id = sqlite3_column_int64(stmt, 1);
out->key.channel = channel;
out->msatoshi = sqlite3_column_int64(stmt, 2);
out->msat = sqlite3_column_amount_msat(stmt, 2);
out->cltv_expiry = sqlite3_column_int(stmt, 3);
out->hstate = sqlite3_column_int(stmt, 4);
sqlite3_column_sha256(stmt, 5, &out->payment_hash);
@@ -1405,12 +1409,12 @@ static void fixup_hin(struct wallet *wallet, struct htlc_in *hin)
hin->failcode = WIRE_TEMPORARY_NODE_FAILURE;
log_broken(wallet->log, "HTLC #%"PRIu64" (%s) "
" for amount %"PRIu64
" for amount %s"
" from %s"
" is missing a resolution:"
" subsituting temporary node failure",
hin->key.id, htlc_state_name(hin->hstate),
hin->msatoshi,
type_to_string(tmpctx, struct amount_msat, &hin->msat),
type_to_string(tmpctx, struct pubkey,
&hin->key.channel->peer->id));
#endif
@@ -2240,7 +2244,7 @@ struct outpoint *wallet_outpoint_for_scid(struct wallet *w, tal_t *ctx,
op->spendheight = sqlite3_column_int(stmt, 1);
op->scriptpubkey = tal_arr(op, u8, sqlite3_column_bytes(stmt, 2));
memcpy(op->scriptpubkey, sqlite3_column_blob(stmt, 2), sqlite3_column_bytes(stmt, 2));
op->satoshis = sqlite3_column_int64(stmt, 3);
op->sat = sqlite3_column_amount_sat(stmt, 3);
db_stmt_done(stmt);
return op;
@@ -2450,8 +2454,8 @@ void wallet_forwarded_payment_add(struct wallet *w, const struct htlc_in *in,
sqlite3_bind_int64(stmt, 2, out->dbid);
sqlite3_bind_int64(stmt, 3, in->key.channel->scid->u64);
sqlite3_bind_int64(stmt, 4, out->key.channel->scid->u64);
sqlite3_bind_int64(stmt, 5, in->msatoshi);
sqlite3_bind_int64(stmt, 6, out->msatoshi);
sqlite3_bind_amount_msat(stmt, 5, in->msat);
sqlite3_bind_amount_msat(stmt, 6, out->msat);
sqlite3_bind_int(stmt, 7, wallet_forward_status_in_db(state));
db_exec_prepared(w->db, stmt);
}

View File

@@ -234,7 +234,7 @@ struct outpoint {
u32 blockheight;
u32 txindex;
u32 outnum;
u64 satoshis;
struct amount_sat sat;
u8 *scriptpubkey;
u32 spendheight;
};
@@ -242,9 +242,9 @@ struct outpoint {
/* Statistics for a channel */
struct channel_stats {
u64 in_payments_offered, in_payments_fulfilled;
u64 in_msatoshi_offered, in_msatoshi_fulfilled;
struct amount_msat in_msatoshi_offered, in_msatoshi_fulfilled;
u64 out_payments_offered, out_payments_fulfilled;
u64 out_msatoshi_offered, out_msatoshi_fulfilled;
struct amount_msat out_msatoshi_offered, out_msatoshi_fulfilled;
};
struct channeltx {
@@ -435,10 +435,10 @@ bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w);
* @cdbid: channel database id
* @msatoshi: amount in msatoshi being transferred
*/
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 cdbid, u64 msatoshi);
void wallet_channel_stats_incr_in_offered(struct wallet *w, u64 cdbid, struct amount_msat msatoshi);
void wallet_channel_stats_incr_in_fulfilled(struct wallet *w, u64 cdbid, struct amount_msat msatoshi);
void wallet_channel_stats_incr_out_offered(struct wallet *w, u64 cdbid, struct amount_msat msatoshi);
void wallet_channel_stats_incr_out_fulfilled(struct wallet *w, u64 cdbid, struct amount_msat msatoshi);
/**
* wallet_channel_stats_load - Load channel statistics

View File

@@ -156,8 +156,8 @@ static struct command_result *json_withdraw(struct command *cmd,
txfilter_add_derkey(cmd->ld->owned_txfilter, ext.pub_key);
u8 *msg = towire_hsm_sign_withdrawal(cmd,
withdraw->wtx.amount.satoshis,
withdraw->wtx.change.satoshis,
withdraw->wtx.amount,
withdraw->wtx.change,
withdraw->wtx.change_key_index,
withdraw->destination,
withdraw->wtx.utxos);
@@ -463,10 +463,9 @@ static struct command_result *json_listfunds(struct command *cmd,
c->scid);
json_add_amount_sat(response,
amount_msat_to_sat_round_down((struct amount_msat){c->our_msatoshi}),
amount_msat_to_sat_round_down(c->our_msat),
"channel_sat", "our_amount_msat");
json_add_amount_sat(response,
(struct amount_sat){c->funding_satoshi},
json_add_amount_sat(response, c->funding,
"channel_total_sat", "amount_msat");
json_add_txid(response, "funding_txid",
&c->funding_txid);