mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 21:35:11 +01:00
channeld: use amount_msat for struct htlc amount.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
3412c5d392
commit
0d30b89043
@ -589,21 +589,21 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
|
|||||||
{
|
{
|
||||||
struct channel_id channel_id;
|
struct channel_id channel_id;
|
||||||
u64 id;
|
u64 id;
|
||||||
u64 amount_msat;
|
struct amount_msat amount;
|
||||||
u32 cltv_expiry;
|
u32 cltv_expiry;
|
||||||
struct sha256 payment_hash;
|
struct sha256 payment_hash;
|
||||||
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
||||||
enum channel_add_err add_err;
|
enum channel_add_err add_err;
|
||||||
struct htlc *htlc;
|
struct htlc *htlc;
|
||||||
|
|
||||||
if (!fromwire_update_add_htlc(msg, &channel_id, &id, &amount_msat,
|
if (!fromwire_update_add_htlc(msg, &channel_id, &id, &amount.millisatoshis,
|
||||||
&payment_hash, &cltv_expiry,
|
&payment_hash, &cltv_expiry,
|
||||||
onion_routing_packet))
|
onion_routing_packet))
|
||||||
peer_failed(&peer->cs,
|
peer_failed(&peer->cs,
|
||||||
&peer->channel_id,
|
&peer->channel_id,
|
||||||
"Bad peer_add_htlc %s", tal_hex(msg, msg));
|
"Bad peer_add_htlc %s", tal_hex(msg, msg));
|
||||||
|
|
||||||
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount_msat,
|
add_err = channel_add_htlc(peer->channel, REMOTE, id, amount,
|
||||||
cltv_expiry, &payment_hash,
|
cltv_expiry, &payment_hash,
|
||||||
onion_routing_packet, &htlc);
|
onion_routing_packet, &htlc);
|
||||||
if (add_err != CHANNEL_ERR_ADD_OK)
|
if (add_err != CHANNEL_ERR_ADD_OK)
|
||||||
@ -866,12 +866,12 @@ static u8 *make_failmsg(const tal_t *ctx,
|
|||||||
goto done;
|
goto done;
|
||||||
case WIRE_AMOUNT_BELOW_MINIMUM:
|
case WIRE_AMOUNT_BELOW_MINIMUM:
|
||||||
channel_update = foreign_channel_update(ctx, peer, scid);
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
||||||
msg = towire_amount_below_minimum(ctx, htlc->msatoshi,
|
msg = towire_amount_below_minimum(ctx, htlc->amount.millisatoshis,
|
||||||
channel_update);
|
channel_update);
|
||||||
goto done;
|
goto done;
|
||||||
case WIRE_FEE_INSUFFICIENT:
|
case WIRE_FEE_INSUFFICIENT:
|
||||||
channel_update = foreign_channel_update(ctx, peer, scid);
|
channel_update = foreign_channel_update(ctx, peer, scid);
|
||||||
msg = towire_fee_insufficient(ctx, htlc->msatoshi,
|
msg = towire_fee_insufficient(ctx, htlc->amount.millisatoshis,
|
||||||
channel_update);
|
channel_update);
|
||||||
goto done;
|
goto done;
|
||||||
case WIRE_INCORRECT_CLTV_EXPIRY:
|
case WIRE_INCORRECT_CLTV_EXPIRY:
|
||||||
@ -888,7 +888,7 @@ static u8 *make_failmsg(const tal_t *ctx,
|
|||||||
goto done;
|
goto done;
|
||||||
case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
|
case WIRE_INCORRECT_OR_UNKNOWN_PAYMENT_DETAILS:
|
||||||
msg = towire_incorrect_or_unknown_payment_details(
|
msg = towire_incorrect_or_unknown_payment_details(
|
||||||
ctx, htlc->msatoshi);
|
ctx, htlc->amount.millisatoshis);
|
||||||
goto done;
|
goto done;
|
||||||
case WIRE_FINAL_EXPIRY_TOO_SOON:
|
case WIRE_FINAL_EXPIRY_TOO_SOON:
|
||||||
msg = towire_final_expiry_too_soon(ctx);
|
msg = towire_final_expiry_too_soon(ctx);
|
||||||
@ -897,7 +897,7 @@ static u8 *make_failmsg(const tal_t *ctx,
|
|||||||
msg = towire_final_incorrect_cltv_expiry(ctx, cltv_expiry);
|
msg = towire_final_incorrect_cltv_expiry(ctx, cltv_expiry);
|
||||||
goto done;
|
goto done;
|
||||||
case WIRE_FINAL_INCORRECT_HTLC_AMOUNT:
|
case WIRE_FINAL_INCORRECT_HTLC_AMOUNT:
|
||||||
msg = towire_final_incorrect_htlc_amount(ctx, htlc->msatoshi);
|
msg = towire_final_incorrect_htlc_amount(ctx, htlc->amount.millisatoshis);
|
||||||
goto done;
|
goto done;
|
||||||
case WIRE_INVALID_ONION_VERSION:
|
case WIRE_INVALID_ONION_VERSION:
|
||||||
msg = towire_invalid_onion_version(ctx, sha256);
|
msg = towire_invalid_onion_version(ctx, sha256);
|
||||||
@ -1251,7 +1251,7 @@ static u8 *got_commitsig_msg(const tal_t *ctx,
|
|||||||
struct secret s;
|
struct secret s;
|
||||||
|
|
||||||
a.id = htlc->id;
|
a.id = htlc->id;
|
||||||
a.amount_msat = htlc->msatoshi;
|
a.amount_msat = htlc->amount.millisatoshis;
|
||||||
a.payment_hash = htlc->rhash;
|
a.payment_hash = htlc->rhash;
|
||||||
a.cltv_expiry = abs_locktime_to_blocks(&htlc->expiry);
|
a.cltv_expiry = abs_locktime_to_blocks(&htlc->expiry);
|
||||||
memcpy(a.onion_routing_packet,
|
memcpy(a.onion_routing_packet,
|
||||||
@ -1899,7 +1899,7 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
|
|||||||
|
|
||||||
if (h->state == SENT_ADD_COMMIT) {
|
if (h->state == SENT_ADD_COMMIT) {
|
||||||
u8 *msg = towire_update_add_htlc(NULL, &peer->channel_id,
|
u8 *msg = towire_update_add_htlc(NULL, &peer->channel_id,
|
||||||
h->id, h->msatoshi,
|
h->id, h->amount.millisatoshis,
|
||||||
&h->rhash,
|
&h->rhash,
|
||||||
abs_locktime_to_blocks(
|
abs_locktime_to_blocks(
|
||||||
&h->expiry),
|
&h->expiry),
|
||||||
@ -2419,7 +2419,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
|
|||||||
{
|
{
|
||||||
u8 *msg;
|
u8 *msg;
|
||||||
u32 cltv_expiry;
|
u32 cltv_expiry;
|
||||||
u64 amount_msat;
|
struct amount_msat amount;
|
||||||
struct sha256 payment_hash;
|
struct sha256 payment_hash;
|
||||||
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
u8 onion_routing_packet[TOTAL_PACKET_SIZE];
|
||||||
enum channel_add_err e;
|
enum channel_add_err e;
|
||||||
@ -2431,23 +2431,25 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg)
|
|||||||
status_failed(STATUS_FAIL_MASTER_IO,
|
status_failed(STATUS_FAIL_MASTER_IO,
|
||||||
"funding not locked for offer_htlc");
|
"funding not locked for offer_htlc");
|
||||||
|
|
||||||
if (!fromwire_channel_offer_htlc(inmsg, &amount_msat,
|
if (!fromwire_channel_offer_htlc(inmsg, &amount.millisatoshis,
|
||||||
&cltv_expiry, &payment_hash,
|
&cltv_expiry, &payment_hash,
|
||||||
onion_routing_packet))
|
onion_routing_packet))
|
||||||
master_badmsg(WIRE_CHANNEL_OFFER_HTLC, inmsg);
|
master_badmsg(WIRE_CHANNEL_OFFER_HTLC, inmsg);
|
||||||
|
|
||||||
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
|
e = channel_add_htlc(peer->channel, LOCAL, peer->htlc_id,
|
||||||
amount_msat, cltv_expiry, &payment_hash,
|
amount, cltv_expiry, &payment_hash,
|
||||||
onion_routing_packet, NULL);
|
onion_routing_packet, NULL);
|
||||||
status_trace("Adding HTLC %"PRIu64" msat=%"PRIu64" cltv=%u gave %s",
|
status_trace("Adding HTLC %"PRIu64" amount=%s cltv=%u gave %s",
|
||||||
peer->htlc_id, amount_msat, cltv_expiry,
|
peer->htlc_id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat, &amount),
|
||||||
|
cltv_expiry,
|
||||||
channel_add_err_name(e));
|
channel_add_err_name(e));
|
||||||
|
|
||||||
switch (e) {
|
switch (e) {
|
||||||
case CHANNEL_ERR_ADD_OK:
|
case CHANNEL_ERR_ADD_OK:
|
||||||
/* Tell the peer. */
|
/* Tell the peer. */
|
||||||
msg = towire_update_add_htlc(NULL, &peer->channel_id,
|
msg = towire_update_add_htlc(NULL, &peer->channel_id,
|
||||||
peer->htlc_id, amount_msat,
|
peer->htlc_id, amount.millisatoshis,
|
||||||
&payment_hash, cltv_expiry,
|
&payment_hash, cltv_expiry,
|
||||||
onion_routing_packet);
|
onion_routing_packet);
|
||||||
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
sync_crypto_write(&peer->cs, PEER_FD, take(msg));
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include <bitcoin/locktime.h>
|
#include <bitcoin/locktime.h>
|
||||||
#include <ccan/short_types/short_types.h>
|
#include <ccan/short_types/short_types.h>
|
||||||
|
#include <common/amount.h>
|
||||||
#include <common/htlc.h>
|
#include <common/htlc.h>
|
||||||
#include <common/pseudorand.h>
|
#include <common/pseudorand.h>
|
||||||
#include <wire/gen_onion_wire.h>
|
#include <wire/gen_onion_wire.h>
|
||||||
@ -13,7 +14,7 @@ struct htlc {
|
|||||||
/* The unique ID for this peer and this direction (LOCAL or REMOTE) */
|
/* The unique ID for this peer and this direction (LOCAL or REMOTE) */
|
||||||
u64 id;
|
u64 id;
|
||||||
/* The amount in millisatoshi. */
|
/* The amount in millisatoshi. */
|
||||||
u64 msatoshi;
|
struct amount_msat amount;
|
||||||
/* When the HTLC can no longer be redeemed. */
|
/* When the HTLC can no longer be redeemed. */
|
||||||
struct abs_locktime expiry;
|
struct abs_locktime expiry;
|
||||||
/* The hash of the preimage which can redeem this HTLC */
|
/* The hash of the preimage which can redeem this HTLC */
|
||||||
|
@ -45,7 +45,7 @@ static bool trim(const struct htlc *htlc,
|
|||||||
/* If these overflow, it implies htlc must be less. */
|
/* If these overflow, it implies htlc must be less. */
|
||||||
if (!amount_sat_add(&htlc_min, dust_limit, htlc_fee))
|
if (!amount_sat_add(&htlc_min, dust_limit, htlc_fee))
|
||||||
return true;
|
return true;
|
||||||
return htlc->msatoshi / 1000 < htlc_min.satoshis;
|
return amount_msat_less_sat(htlc->amount, htlc_min);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
|
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
|
||||||
@ -70,7 +70,7 @@ static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
|
|||||||
|
|
||||||
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
||||||
wscript = htlc_offered_wscript(tx->output, &ripemd, keyset);
|
wscript = htlc_offered_wscript(tx->output, &ripemd, keyset);
|
||||||
tx->output[n].amount = htlc->msatoshi / 1000;
|
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount).satoshis;
|
||||||
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
|
tx->output[n].script = scriptpubkey_p2wsh(tx, wscript);
|
||||||
SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n",
|
SUPERVERBOSE("# HTLC %"PRIu64" offered amount %"PRIu64" wscript %s\n",
|
||||||
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript));
|
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript));
|
||||||
@ -86,7 +86,7 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
|
|||||||
|
|
||||||
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
||||||
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset);
|
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset);
|
||||||
tx->output[n].amount = htlc->msatoshi / 1000;
|
tx->output[n].amount = amount_msat_to_sat_round_down(htlc->amount).satoshis;
|
||||||
tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript);
|
tx->output[n].script = scriptpubkey_p2wsh(tx->output, wscript);
|
||||||
SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n",
|
SUPERVERBOSE("# HTLC %"PRIu64" received amount %"PRIu64" wscript %s\n",
|
||||||
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript));
|
htlc->id, tx->output[n].amount, tal_hex(wscript, wscript));
|
||||||
@ -147,18 +147,19 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
|||||||
|
|
||||||
#ifdef PRINT_ACTUAL_FEE
|
#ifdef PRINT_ACTUAL_FEE
|
||||||
{
|
{
|
||||||
u64 satoshis_out = 0;
|
struct amount_sat out = AMOUNT_SAT(0);
|
||||||
|
bool ok = true;
|
||||||
for (i = 0; i < tal_count(htlcs); i++) {
|
for (i = 0; i < tal_count(htlcs); i++) {
|
||||||
if (!trim(htlcs[i], feerate_per_kw, dust_limit,
|
if (!trim(htlcs[i], feerate_per_kw, dust_limit, side))
|
||||||
side))
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(htlcs[i]->amount));
|
||||||
satoshis_out += htlcs[i]->msatoshi / 1000;
|
|
||||||
}
|
}
|
||||||
if (amount_msat_greater_sat(self_pay, dust_limit))
|
if (amount_msat_greater_sat(self_pay, dust_limit))
|
||||||
satoshis_out += self_pay.millisatoshis / 1000;
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(self_pay));
|
||||||
if (amount_msat_greater_sat(other_pay, dust_limit))
|
if (amount_msat_greater_sat(other_pay, dust_limit))
|
||||||
satoshis_out += other_pay.millisatoshis / 1000;
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(other_pay));
|
||||||
|
assert(ok);
|
||||||
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
|
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
|
||||||
funding.satoshis - satoshis_out);
|
funding.satoshis - out.satoshis);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -73,12 +73,8 @@ static bool WARN_UNUSED_RESULT balance_add_htlc(struct amount_msat *msat,
|
|||||||
const struct htlc *htlc,
|
const struct htlc *htlc,
|
||||||
enum side side)
|
enum side side)
|
||||||
{
|
{
|
||||||
struct amount_msat htlc_msat;
|
|
||||||
|
|
||||||
htlc_msat.millisatoshis = htlc->msatoshi;
|
|
||||||
|
|
||||||
if (htlc_owner(htlc) == side)
|
if (htlc_owner(htlc) == side)
|
||||||
return amount_msat_sub(msat, *msat, htlc_msat);
|
return amount_msat_sub(msat, *msat, htlc->amount);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,11 +83,8 @@ static bool WARN_UNUSED_RESULT balance_remove_htlc(struct amount_msat *msat,
|
|||||||
const struct htlc *htlc,
|
const struct htlc *htlc,
|
||||||
enum side side)
|
enum side side)
|
||||||
{
|
{
|
||||||
struct amount_msat htlc_msat;
|
|
||||||
enum side paid_to;
|
enum side paid_to;
|
||||||
|
|
||||||
htlc_msat.millisatoshis = htlc->msatoshi;
|
|
||||||
|
|
||||||
/* Fulfilled HTLCs are paid to recipient, otherwise returns to owner */
|
/* Fulfilled HTLCs are paid to recipient, otherwise returns to owner */
|
||||||
if (htlc->r)
|
if (htlc->r)
|
||||||
paid_to = !htlc_owner(htlc);
|
paid_to = !htlc_owner(htlc);
|
||||||
@ -99,7 +92,7 @@ static bool WARN_UNUSED_RESULT balance_remove_htlc(struct amount_msat *msat,
|
|||||||
paid_to = htlc_owner(htlc);
|
paid_to = htlc_owner(htlc);
|
||||||
|
|
||||||
if (side == paid_to)
|
if (side == paid_to)
|
||||||
return amount_msat_add(msat, *msat, htlc_msat);
|
return amount_msat_add(msat, *msat, htlc->amount);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,9 +177,7 @@ static bool sum_offered_msatoshis(struct amount_msat *total,
|
|||||||
*total = AMOUNT_MSAT(0);
|
*total = AMOUNT_MSAT(0);
|
||||||
for (i = 0; i < tal_count(htlcs); i++) {
|
for (i = 0; i < tal_count(htlcs); i++) {
|
||||||
if (htlc_owner(htlcs[i]) == side) {
|
if (htlc_owner(htlcs[i]) == side) {
|
||||||
struct amount_msat m;
|
if (!amount_msat_add(total, *total, htlcs[i]->amount))
|
||||||
m.millisatoshis = htlcs[i]->msatoshi;
|
|
||||||
if (!amount_msat_add(total, *total, m))
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -211,15 +202,13 @@ static void add_htlcs(struct bitcoin_tx ***txs,
|
|||||||
const struct htlc *htlc = htlcmap[i];
|
const struct htlc *htlc = htlcmap[i];
|
||||||
struct bitcoin_tx *tx;
|
struct bitcoin_tx *tx;
|
||||||
u8 *wscript;
|
u8 *wscript;
|
||||||
struct amount_msat htlc_amount;
|
|
||||||
|
|
||||||
if (!htlc)
|
if (!htlc)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
htlc_amount.millisatoshis = htlc->msatoshi;
|
|
||||||
if (htlc_owner(htlc) == side) {
|
if (htlc_owner(htlc) == side) {
|
||||||
tx = htlc_timeout_tx(*txs, &txid, i,
|
tx = htlc_timeout_tx(*txs, &txid, i,
|
||||||
htlc_amount,
|
htlc->amount,
|
||||||
htlc->expiry.locktime,
|
htlc->expiry.locktime,
|
||||||
channel->config[!side].to_self_delay,
|
channel->config[!side].to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
@ -231,7 +220,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
|
|||||||
&keyset->self_revocation_key);
|
&keyset->self_revocation_key);
|
||||||
} else {
|
} else {
|
||||||
tx = htlc_success_tx(*txs, &txid, i,
|
tx = htlc_success_tx(*txs, &txid, i,
|
||||||
htlc_amount,
|
htlc->amount,
|
||||||
channel->config[!side].to_self_delay,
|
channel->config[!side].to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
keyset);
|
keyset);
|
||||||
@ -302,7 +291,9 @@ struct bitcoin_tx **channel_txs(const tal_t *ctx,
|
|||||||
|
|
||||||
static enum channel_add_err add_htlc(struct channel *channel,
|
static enum channel_add_err add_htlc(struct channel *channel,
|
||||||
enum htlc_state state,
|
enum htlc_state state,
|
||||||
u64 id, u64 msatoshi, u32 cltv_expiry,
|
u64 id,
|
||||||
|
struct amount_msat amount,
|
||||||
|
u32 cltv_expiry,
|
||||||
const struct sha256 *payment_hash,
|
const struct sha256 *payment_hash,
|
||||||
const u8 routing[TOTAL_PACKET_SIZE],
|
const u8 routing[TOTAL_PACKET_SIZE],
|
||||||
struct htlc **htlcp,
|
struct htlc **htlcp,
|
||||||
@ -320,7 +311,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
|
|||||||
htlc = tal(tmpctx, struct htlc);
|
htlc = tal(tmpctx, struct htlc);
|
||||||
|
|
||||||
htlc->id = id;
|
htlc->id = id;
|
||||||
htlc->msatoshi = msatoshi;
|
htlc->amount = amount;
|
||||||
htlc->state = state;
|
htlc->state = state;
|
||||||
htlc->shared_secret = NULL;
|
htlc->shared_secret = NULL;
|
||||||
|
|
||||||
@ -348,7 +339,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
|
|||||||
old = htlc_get(channel->htlcs, htlc->id, htlc_owner(htlc));
|
old = htlc_get(channel->htlcs, htlc->id, htlc_owner(htlc));
|
||||||
if (old) {
|
if (old) {
|
||||||
if (old->state != htlc->state
|
if (old->state != htlc->state
|
||||||
|| old->msatoshi != htlc->msatoshi
|
|| !amount_msat_eq(old->amount, htlc->amount)
|
||||||
|| old->expiry.locktime != htlc->expiry.locktime
|
|| old->expiry.locktime != htlc->expiry.locktime
|
||||||
|| !sha256_eq(&old->rhash, &htlc->rhash))
|
|| !sha256_eq(&old->rhash, &htlc->rhash))
|
||||||
return CHANNEL_ERR_DUPLICATE_ID_DIFFERENT;
|
return CHANNEL_ERR_DUPLICATE_ID_DIFFERENT;
|
||||||
@ -366,10 +357,10 @@ static enum channel_add_err add_htlc(struct channel *channel,
|
|||||||
* `htlc_minimum_msat`:
|
* `htlc_minimum_msat`:
|
||||||
* - SHOULD fail the channel.
|
* - SHOULD fail the channel.
|
||||||
*/
|
*/
|
||||||
if (htlc->msatoshi == 0) {
|
if (amount_msat_eq(htlc->amount, AMOUNT_MSAT(0))) {
|
||||||
return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
|
return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
|
||||||
}
|
}
|
||||||
if (htlc->msatoshi < channel->config[recipient].htlc_minimum.millisatoshis) {
|
if (amount_msat_less(htlc->amount, channel->config[recipient].htlc_minimum)) {
|
||||||
return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
|
return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -378,7 +369,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
|
|||||||
* - for channels with `chain_hash` identifying the Bitcoin blockchain:
|
* - for channels with `chain_hash` identifying the Bitcoin blockchain:
|
||||||
* - MUST set the four most significant bytes of `amount_msat` to 0.
|
* - MUST set the four most significant bytes of `amount_msat` to 0.
|
||||||
*/
|
*/
|
||||||
if (htlc->msatoshi > channel->chainparams->max_payment.millisatoshis) {
|
if (amount_msat_greater(htlc->amount, channel->chainparams->max_payment)) {
|
||||||
return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED;
|
return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -523,7 +514,7 @@ static enum channel_add_err add_htlc(struct channel *channel,
|
|||||||
enum channel_add_err channel_add_htlc(struct channel *channel,
|
enum channel_add_err channel_add_htlc(struct channel *channel,
|
||||||
enum side sender,
|
enum side sender,
|
||||||
u64 id,
|
u64 id,
|
||||||
u64 msatoshi,
|
struct amount_msat amount,
|
||||||
u32 cltv_expiry,
|
u32 cltv_expiry,
|
||||||
const struct sha256 *payment_hash,
|
const struct sha256 *payment_hash,
|
||||||
const u8 routing[TOTAL_PACKET_SIZE],
|
const u8 routing[TOTAL_PACKET_SIZE],
|
||||||
@ -536,7 +527,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
|
|||||||
else
|
else
|
||||||
state = RCVD_ADD_HTLC;
|
state = RCVD_ADD_HTLC;
|
||||||
|
|
||||||
return add_htlc(channel, state, id, msatoshi, cltv_expiry,
|
return add_htlc(channel, state, id, amount, cltv_expiry,
|
||||||
payment_hash, routing, htlcp, true);
|
payment_hash, routing, htlcp, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -689,15 +680,19 @@ static void htlc_incstate(struct channel *channel,
|
|||||||
if (!balance_add_htlc(&channel->view[sidechanged].owed[LOCAL],
|
if (!balance_add_htlc(&channel->view[sidechanged].owed[LOCAL],
|
||||||
htlc, LOCAL))
|
htlc, LOCAL))
|
||||||
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
||||||
"Cannot add htlc #%"PRIu64" %"PRIu64
|
"Cannot add htlc #%"PRIu64" %s"
|
||||||
" to LOCAL",
|
" to LOCAL",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
if (!balance_add_htlc(&channel->view[sidechanged].owed[REMOTE],
|
if (!balance_add_htlc(&channel->view[sidechanged].owed[REMOTE],
|
||||||
htlc, REMOTE))
|
htlc, REMOTE))
|
||||||
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
||||||
"Cannot add htlc #%"PRIu64" %"PRIu64
|
"Cannot add htlc #%"PRIu64" %s"
|
||||||
" to REMOTE",
|
" to REMOTE",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
status_trace("-> local %s remote %s",
|
status_trace("-> local %s remote %s",
|
||||||
type_to_string(tmpctx, struct amount_msat,
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
&channel->view[sidechanged].owed[LOCAL]),
|
&channel->view[sidechanged].owed[LOCAL]),
|
||||||
@ -713,15 +708,19 @@ static void htlc_incstate(struct channel *channel,
|
|||||||
if (!balance_remove_htlc(&channel->view[sidechanged].owed[LOCAL],
|
if (!balance_remove_htlc(&channel->view[sidechanged].owed[LOCAL],
|
||||||
htlc, LOCAL))
|
htlc, LOCAL))
|
||||||
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
||||||
"Cannot remove htlc #%"PRIu64" %"PRIu64
|
"Cannot remove htlc #%"PRIu64" %s"
|
||||||
" from LOCAL",
|
" from LOCAL",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
if (!balance_remove_htlc(&channel->view[sidechanged].owed[REMOTE],
|
if (!balance_remove_htlc(&channel->view[sidechanged].owed[REMOTE],
|
||||||
htlc, REMOTE))
|
htlc, REMOTE))
|
||||||
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
||||||
"Cannot remove htlc #%"PRIu64" %"PRIu64
|
"Cannot remove htlc #%"PRIu64" %s"
|
||||||
" from REMOTE",
|
" from REMOTE",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
status_trace("-> local %s remote %s",
|
status_trace("-> local %s remote %s",
|
||||||
type_to_string(tmpctx, struct amount_msat,
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
&channel->view[sidechanged].owed[LOCAL]),
|
&channel->view[sidechanged].owed[LOCAL]),
|
||||||
@ -982,16 +981,20 @@ static bool adjust_balance(struct channel *channel, struct htlc *htlc)
|
|||||||
/* Add it. */
|
/* Add it. */
|
||||||
if (!balance_add_htlc(&channel->view[side].owed[LOCAL],
|
if (!balance_add_htlc(&channel->view[side].owed[LOCAL],
|
||||||
htlc, LOCAL)) {
|
htlc, LOCAL)) {
|
||||||
status_broken("Cannot add htlc #%"PRIu64" %"PRIu64
|
status_broken("Cannot add htlc #%"PRIu64" %s"
|
||||||
" to LOCAL",
|
" to LOCAL",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!balance_add_htlc(&channel->view[side].owed[REMOTE],
|
if (!balance_add_htlc(&channel->view[side].owed[REMOTE],
|
||||||
htlc, REMOTE)) {
|
htlc, REMOTE)) {
|
||||||
status_broken("Cannot add htlc #%"PRIu64" %"PRIu64
|
status_broken("Cannot add htlc #%"PRIu64" %s"
|
||||||
" to REMOTE",
|
" to REMOTE",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1011,16 +1014,20 @@ static bool adjust_balance(struct channel *channel, struct htlc *htlc)
|
|||||||
}
|
}
|
||||||
if (!balance_remove_htlc(&channel->view[side].owed[LOCAL],
|
if (!balance_remove_htlc(&channel->view[side].owed[LOCAL],
|
||||||
htlc, LOCAL)) {
|
htlc, LOCAL)) {
|
||||||
status_broken("Cannot remove htlc #%"PRIu64" %"PRIu64
|
status_broken("Cannot remove htlc #%"PRIu64" %s"
|
||||||
" from LOCAL",
|
" from LOCAL",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!balance_remove_htlc(&channel->view[side].owed[REMOTE],
|
if (!balance_remove_htlc(&channel->view[side].owed[REMOTE],
|
||||||
htlc, REMOTE)) {
|
htlc, REMOTE)) {
|
||||||
status_broken("Cannot remove htlc #%"PRIu64" %"PRIu64
|
status_broken("Cannot remove htlc #%"PRIu64" %s"
|
||||||
" from REMOTE",
|
" from REMOTE",
|
||||||
htlc->id, htlc->msatoshi);
|
htlc->id,
|
||||||
|
type_to_string(tmpctx, struct amount_msat,
|
||||||
|
&htlc->amount));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1059,6 +1066,7 @@ bool channel_force_htlcs(struct channel *channel,
|
|||||||
for (i = 0; i < tal_count(htlcs); i++) {
|
for (i = 0; i < tal_count(htlcs); i++) {
|
||||||
enum channel_add_err e;
|
enum channel_add_err e;
|
||||||
struct htlc *htlc;
|
struct htlc *htlc;
|
||||||
|
struct amount_msat amount;
|
||||||
|
|
||||||
status_trace("Restoring HTLC %zu/%zu:"
|
status_trace("Restoring HTLC %zu/%zu:"
|
||||||
" id=%"PRIu64" msat=%"PRIu64" cltv=%u"
|
" id=%"PRIu64" msat=%"PRIu64" cltv=%u"
|
||||||
@ -1069,8 +1077,9 @@ bool channel_force_htlcs(struct channel *channel,
|
|||||||
type_to_string(tmpctx, struct sha256,
|
type_to_string(tmpctx, struct sha256,
|
||||||
&htlcs[i].payment_hash));
|
&htlcs[i].payment_hash));
|
||||||
|
|
||||||
|
amount.millisatoshis = htlcs[i].amount_msat;
|
||||||
e = add_htlc(channel, hstates[i],
|
e = add_htlc(channel, hstates[i],
|
||||||
htlcs[i].id, htlcs[i].amount_msat,
|
htlcs[i].id, amount,
|
||||||
htlcs[i].cltv_expiry,
|
htlcs[i].cltv_expiry,
|
||||||
&htlcs[i].payment_hash,
|
&htlcs[i].payment_hash,
|
||||||
htlcs[i].onion_routing_packet, &htlc, false);
|
htlcs[i].onion_routing_packet, &htlc, false);
|
||||||
|
@ -84,7 +84,7 @@ u32 actual_feerate(const struct channel *channel,
|
|||||||
* @channel: The channel
|
* @channel: The channel
|
||||||
* @offerer: the side offering the HTLC (to the other side).
|
* @offerer: the side offering the HTLC (to the other side).
|
||||||
* @id: unique HTLC id.
|
* @id: unique HTLC id.
|
||||||
* @msatoshi: amount in millisatoshi.
|
* @amount: amount in millisatoshi.
|
||||||
* @cltv_expiry: block number when HTLC can no longer be redeemed.
|
* @cltv_expiry: block number when HTLC can no longer be redeemed.
|
||||||
* @payment_hash: hash whose preimage can redeem HTLC.
|
* @payment_hash: hash whose preimage can redeem HTLC.
|
||||||
* @routing: routing information (copied)
|
* @routing: routing information (copied)
|
||||||
@ -97,7 +97,7 @@ u32 actual_feerate(const struct channel *channel,
|
|||||||
enum channel_add_err channel_add_htlc(struct channel *channel,
|
enum channel_add_err channel_add_htlc(struct channel *channel,
|
||||||
enum side sender,
|
enum side sender,
|
||||||
u64 id,
|
u64 id,
|
||||||
u64 msatoshi,
|
struct amount_msat msatoshi,
|
||||||
u32 cltv_expiry,
|
u32 cltv_expiry,
|
||||||
const struct sha256 *payment_hash,
|
const struct sha256 *payment_hash,
|
||||||
const u8 routing[TOTAL_PACKET_SIZE],
|
const u8 routing[TOTAL_PACKET_SIZE],
|
||||||
|
@ -123,23 +123,23 @@ static const struct htlc **setup_htlcs(const tal_t *ctx)
|
|||||||
switch (i) {
|
switch (i) {
|
||||||
case 0:
|
case 0:
|
||||||
htlc->state = RCVD_ADD_ACK_REVOCATION;
|
htlc->state = RCVD_ADD_ACK_REVOCATION;
|
||||||
htlc->msatoshi = 1000000;
|
htlc->amount = AMOUNT_MSAT(1000000);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
htlc->state = RCVD_ADD_ACK_REVOCATION;
|
htlc->state = RCVD_ADD_ACK_REVOCATION;
|
||||||
htlc->msatoshi = 2000000;
|
htlc->amount = AMOUNT_MSAT(2000000);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
htlc->state = SENT_ADD_ACK_REVOCATION;
|
htlc->state = SENT_ADD_ACK_REVOCATION;
|
||||||
htlc->msatoshi = 2000000;
|
htlc->amount = AMOUNT_MSAT(2000000);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
htlc->state = SENT_ADD_ACK_REVOCATION;
|
htlc->state = SENT_ADD_ACK_REVOCATION;
|
||||||
htlc->msatoshi = 3000000;
|
htlc->amount = AMOUNT_MSAT(3000000);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
htlc->state = RCVD_ADD_ACK_REVOCATION;
|
htlc->state = RCVD_ADD_ACK_REVOCATION;
|
||||||
htlc->msatoshi = 4000000;
|
htlc->amount = AMOUNT_MSAT(4000000);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,15 +217,13 @@ static void report_htlcs(const struct bitcoin_tx *tx,
|
|||||||
|
|
||||||
for (i = 0; i < tal_count(htlc_map); i++) {
|
for (i = 0; i < tal_count(htlc_map); i++) {
|
||||||
const struct htlc *htlc = htlc_map[i];
|
const struct htlc *htlc = htlc_map[i];
|
||||||
struct amount_msat htlc_amount;
|
|
||||||
|
|
||||||
if (!htlc)
|
if (!htlc)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
htlc_amount.millisatoshis = htlc->msatoshi;
|
|
||||||
if (htlc_owner(htlc) == LOCAL) {
|
if (htlc_owner(htlc) == LOCAL) {
|
||||||
htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i,
|
htlc_tx[i] = htlc_timeout_tx(htlc_tx, &txid, i,
|
||||||
htlc_amount,
|
htlc->amount,
|
||||||
htlc->expiry.locktime,
|
htlc->expiry.locktime,
|
||||||
to_self_delay,
|
to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
@ -237,7 +235,7 @@ static void report_htlcs(const struct bitcoin_tx *tx,
|
|||||||
remote_revocation_key);
|
remote_revocation_key);
|
||||||
} else {
|
} else {
|
||||||
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
|
htlc_tx[i] = htlc_success_tx(htlc_tx, &txid, i,
|
||||||
htlc_amount,
|
htlc->amount,
|
||||||
to_self_delay,
|
to_self_delay,
|
||||||
feerate_per_kw,
|
feerate_per_kw,
|
||||||
&keyset);
|
&keyset);
|
||||||
|
@ -121,31 +121,31 @@ static const struct htlc **include_htlcs(struct channel *channel, enum side side
|
|||||||
struct sha256 hash;
|
struct sha256 hash;
|
||||||
enum channel_add_err e;
|
enum channel_add_err e;
|
||||||
enum side sender;
|
enum side sender;
|
||||||
u64 msatoshi = 0;
|
struct amount_msat msatoshi = AMOUNT_MSAT(0);
|
||||||
|
|
||||||
switch (i) {
|
switch (i) {
|
||||||
case 0:
|
case 0:
|
||||||
sender = !side;
|
sender = !side;
|
||||||
msatoshi = 1000000;
|
msatoshi = AMOUNT_MSAT(1000000);
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
sender = !side;
|
sender = !side;
|
||||||
msatoshi = 2000000;
|
msatoshi = AMOUNT_MSAT(2000000);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
sender = side;
|
sender = side;
|
||||||
msatoshi = 2000000;
|
msatoshi = AMOUNT_MSAT(2000000);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
sender = side;
|
sender = side;
|
||||||
msatoshi = 3000000;
|
msatoshi = AMOUNT_MSAT(3000000);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
sender = !side;
|
sender = !side;
|
||||||
msatoshi = 4000000;
|
msatoshi = AMOUNT_MSAT(4000000);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
assert(msatoshi != 0);
|
assert(msatoshi.millisatoshis != 0);
|
||||||
|
|
||||||
memset(&preimage, i, sizeof(preimage));
|
memset(&preimage, i, sizeof(preimage));
|
||||||
sha256(&hash, &preimage, sizeof(preimage));
|
sha256(&hash, &preimage, sizeof(preimage));
|
||||||
@ -230,7 +230,7 @@ static void txs_must_be_eq(struct bitcoin_tx **a, struct bitcoin_tx **b)
|
|||||||
|
|
||||||
static void send_and_fulfill_htlc(struct channel *channel,
|
static void send_and_fulfill_htlc(struct channel *channel,
|
||||||
enum side sender,
|
enum side sender,
|
||||||
u64 msatoshi)
|
struct amount_msat msatoshi)
|
||||||
{
|
{
|
||||||
struct preimage r;
|
struct preimage r;
|
||||||
struct sha256 rhash;
|
struct sha256 rhash;
|
||||||
@ -531,8 +531,8 @@ int main(void)
|
|||||||
* here: it's as if local side paid for all the HTLCs. We can
|
* here: it's as if local side paid for all the HTLCs. We can
|
||||||
* fix this by having local side offer an HTLC, and having
|
* fix this by having local side offer an HTLC, and having
|
||||||
* remote side accept it */
|
* remote side accept it */
|
||||||
send_and_fulfill_htlc(lchannel, LOCAL, 7000000);
|
send_and_fulfill_htlc(lchannel, LOCAL, AMOUNT_MSAT(7000000));
|
||||||
send_and_fulfill_htlc(rchannel, REMOTE, 7000000);
|
send_and_fulfill_htlc(rchannel, REMOTE, AMOUNT_MSAT(7000000));
|
||||||
|
|
||||||
assert(lchannel->view[LOCAL].owed[LOCAL].millisatoshis
|
assert(lchannel->view[LOCAL].owed[LOCAL].millisatoshis
|
||||||
== rchannel->view[REMOTE].owed[REMOTE].millisatoshis);
|
== rchannel->view[REMOTE].owed[REMOTE].millisatoshis);
|
||||||
|
@ -974,9 +974,9 @@ def test_forward_different_fees_and_cltv(node_factory, bitcoind):
|
|||||||
|
|
||||||
# We add one to the blockcount for a bit of fuzz (FIXME: Shadowroute would fix this!)
|
# We add one to the blockcount for a bit of fuzz (FIXME: Shadowroute would fix this!)
|
||||||
shadow_route = 1
|
shadow_route = 1
|
||||||
l1.daemon.wait_for_log("Adding HTLC 0 msat=5010198 cltv={} gave CHANNEL_ERR_ADD_OK"
|
l1.daemon.wait_for_log("Adding HTLC 0 amount=5010198msat cltv={} gave CHANNEL_ERR_ADD_OK"
|
||||||
.format(bitcoind.rpc.getblockcount() + 20 + 9 + shadow_route))
|
.format(bitcoind.rpc.getblockcount() + 20 + 9 + shadow_route))
|
||||||
l2.daemon.wait_for_log("Adding HTLC 0 msat=4999999 cltv={} gave CHANNEL_ERR_ADD_OK"
|
l2.daemon.wait_for_log("Adding HTLC 0 amount=4999999msat cltv={} gave CHANNEL_ERR_ADD_OK"
|
||||||
.format(bitcoind.rpc.getblockcount() + 9 + shadow_route))
|
.format(bitcoind.rpc.getblockcount() + 9 + shadow_route))
|
||||||
l3.daemon.wait_for_log("test_forward_different_fees_and_cltv: Actual amount 4999999msat, HTLC expiry {}"
|
l3.daemon.wait_for_log("test_forward_different_fees_and_cltv: Actual amount 4999999msat, HTLC expiry {}"
|
||||||
.format(bitcoind.rpc.getblockcount() + 9 + shadow_route))
|
.format(bitcoind.rpc.getblockcount() + 9 + shadow_route))
|
||||||
|
Loading…
Reference in New Issue
Block a user