Browse Source

channeld: fix up BOLT references.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 7 years ago
committed by Christian Decker
parent
commit
b40b6240ce
  1. 217
      channeld/channel.c
  2. 37
      channeld/commit_tx.c
  3. 85
      channeld/full_channel.c
  4. 31
      channeld/test/run-full_channel.c

217
channeld/channel.c

@ -96,8 +96,10 @@ struct peer {
/* BOLT #2: /* BOLT #2:
* *
* A sending node MUST set `id` to 0 for the first HTLC it offers, and * A sending node:
* increase the value by 1 for each successive offer. *...
* - for the first HTLC it offers:
* - MUST set `id` to 0.
*/ */
u64 htlc_id; u64 htlc_id;
@ -472,9 +474,9 @@ static void channel_announcement_negotiate(struct peer *peer)
} }
/* BOLT #7: /* BOLT #7:
* *
* If sent, `announcement_signatures` messages MUST NOT be sent until * - MUST NOT send `announcement_signatures` messages until
* `funding_locked` has been sent and the funding transaction has * `funding_locked` has been sent AND the funding transaction has
* at least 6 confirmations. * at least six confirmations.
*/ */
if (peer->announce_depth_reached && !peer->have_sigs[LOCAL]) { if (peer->announce_depth_reached && !peer->have_sigs[LOCAL]) {
send_announcement_signatures(peer); send_announcement_signatures(peer);
@ -494,8 +496,10 @@ static void handle_peer_funding_locked(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* On reconnection, a node MUST ignore a redundant `funding_locked` if * A node:
* it receives one. *...
* - upon reconnection:
* - MUST ignore any redundant `funding_locked` it receives.
*/ */
if (peer->funding_locked[REMOTE]) if (peer->funding_locked[REMOTE])
return; return;
@ -632,8 +636,10 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST fail the channel if the sender is not * A receiving node:
* responsible for paying the bitcoin fee. *...
* - if the sender is not responsible for paying the Bitcoin fee:
* - MUST fail the channel.
*/ */
if (peer->channel->funder != REMOTE) if (peer->channel->funder != REMOTE)
peer_failed(&peer->cs, peer_failed(&peer->cs,
@ -645,8 +651,10 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if the `update_fee` is too * A receiving node:
* low for timely processing, or unreasonably large. * - if the `update_fee` is too low for timely processing, OR is
* unreasonably large:
* - SHOULD fail the channel.
*/ */
if (feerate < peer->feerate_min || feerate > peer->feerate_max) if (feerate < peer->feerate_min || feerate > peer->feerate_max)
peer_failed(&peer->cs, peer_failed(&peer->cs,
@ -656,10 +664,10 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if the sender cannot * - if the sender cannot afford the new fee rate on the receiving
* afford the new fee rate on the receiving node's current commitment * node's current commitment transaction:
* transaction, but it MAY delay this check until the `update_fee` is * - SHOULD fail the channel,
* committed. * - but MAY delay this check until the `update_fee` is committed.
*/ */
if (!channel_update_feerate(peer->channel, feerate)) if (!channel_update_feerate(peer->channel, feerate))
peer_failed(&peer->cs, peer_failed(&peer->cs,
@ -715,8 +723,11 @@ static bool shutdown_complete(const struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* A node MUST NOT send a `shutdown` if there are updates pending on * A sending node:
* the receiving node's commitment transaction. *...
* - if there are updates pending on the receiving node's commitment
* transaction:
* - MUST NOT send a `shutdown`.
*/ */
/* So we only call this after reestablish or immediately after sending commit */ /* So we only call this after reestablish or immediately after sending commit */
static void maybe_send_shutdown(struct peer *peer) static void maybe_send_shutdown(struct peer *peer)
@ -845,9 +856,11 @@ static struct commit_sigs *calc_commitsigs(const tal_t *ctx,
/* BOLT #2: /* BOLT #2:
* *
* A node MUST include one `htlc_signature` for every HTLC transaction * A sending node:
* corresponding to BIP69 lexicographic ordering of the commitment *...
* transaction. * - MUST include one `htlc_signature` for every HTLC transaction
* corresponding to BIP69 lexicographic ordering of the commitment
* transaction.
*/ */
commit_sigs->htlc_sigs = tal_arr(commit_sigs, secp256k1_ecdsa_signature, commit_sigs->htlc_sigs = tal_arr(commit_sigs, secp256k1_ecdsa_signature,
tal_count(txs) - 1); tal_count(txs) - 1);
@ -941,8 +954,9 @@ static void send_commit(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* A node MUST NOT send a `commitment_signed` message which does not * A sending node:
* include any updates. * - MUST NOT send a `commitment_signed` message that does not include
* any updates.
*/ */
changed_htlcs = tal_arr(tmpctx, const struct htlc *, 0); changed_htlcs = tal_arr(tmpctx, const struct htlc *, 0);
if (!channel_sending_commit(peer->channel, &changed_htlcs)) { if (!channel_sending_commit(peer->channel, &changed_htlcs)) {
@ -1137,8 +1151,9 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
if (!channel_rcvd_commit(peer->channel, &changed_htlcs)) { if (!channel_rcvd_commit(peer->channel, &changed_htlcs)) {
/* BOLT #2: /* BOLT #2:
* *
* A node MUST NOT send a `commitment_signed` message which * A sending node:
* does not include any updates. * - MUST NOT send a `commitment_signed` message that does not
* include any updates.
*/ */
peer_failed(&peer->cs, peer_failed(&peer->cs,
&peer->channel_id, &peer->channel_id,
@ -1177,9 +1192,10 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
type_to_string(tmpctx, struct pubkey, &point)); type_to_string(tmpctx, struct pubkey, &point));
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST fail the channel if `signature` is not valid * A receiving node:
* for its local commitment transaction once all pending updates are * - once all pending updates are applied:
* applied. * - if `signature` is not valid for its local commitment transaction:
* - MUST fail the channel.
*/ */
if (!check_tx_sig(txs[0], 0, NULL, wscripts[0], if (!check_tx_sig(txs[0], 0, NULL, wscripts[0],
&peer->channel->funding_pubkey[REMOTE], &commit_sig)) { &peer->channel->funding_pubkey[REMOTE], &commit_sig)) {
@ -1199,9 +1215,11 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST fail the channel if `num_htlcs` is not equal * A receiving node:
* to the number of HTLC outputs in the local commitment transaction *...
* once all pending updates are applied. * - if `num_htlcs` is not equal to the number of HTLC outputs in the
* local commitment transaction:
* - MUST fail the channel.
*/ */
if (tal_count(htlc_sigs) != tal_count(txs) - 1) if (tal_count(htlc_sigs) != tal_count(txs) - 1)
peer_failed(&peer->cs, peer_failed(&peer->cs,
@ -1211,9 +1229,9 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST fail * - if any `htlc_signature` is not valid for the corresponding HTLC
* the channel if any `htlc_signature` is not valid for the * transaction:
* corresponding HTLC transaction. * - MUST fail the channel.
*/ */
for (i = 0; i < tal_count(htlc_sigs); i++) { for (i = 0; i < tal_count(htlc_sigs); i++) {
if (!check_tx_sig(txs[1+i], 0, NULL, wscripts[1+i], if (!check_tx_sig(txs[1+i], 0, NULL, wscripts[1+i],
@ -1289,9 +1307,10 @@ static void handle_peer_revoke_and_ack(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST check that `per_commitment_secret` generates * A receiving node:
* the previous `per_commitment_point`, and MUST fail if it does * - if `per_commitment_secret` does not generate the previous
* not. * `per_commitment_point`:
* - MUST fail the channel.
*/ */
memcpy(&privkey, &old_commit_secret, sizeof(privkey)); memcpy(&privkey, &old_commit_secret, sizeof(privkey));
if (!pubkey_from_privkey(&privkey, &per_commit_point)) { if (!pubkey_from_privkey(&privkey, &per_commit_point)) {
@ -1430,8 +1449,9 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST fail the channel if the `BADONION` bit in * - if the `BADONION` bit in `failure_code` is not set for
* `failure_code` is not set for `update_fail_malformed_htlc`. * `update_fail_malformed_htlc`:
* - MUST fail the channel.
*/ */
if (!(failure_code & BADONION)) { if (!(failure_code & BADONION)) {
peer_failed(&peer->cs, peer_failed(&peer->cs,
@ -1446,19 +1466,20 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
/* FIXME: Do this! */ /* FIXME: Do this! */
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MAY check the `sha256_of_onion` * - if the `sha256_of_onion` in `update_fail_malformed_htlc`
* in `update_fail_malformed_htlc` and MAY retry or choose an * doesn't match the onion it sent:
* alternate error response if it does not match the onion it * - MAY retry or choose an alternate error response.
* sent.
*/ */
/* BOLT #2: /* BOLT #2:
* *
* Otherwise, a receiving node which has an outgoing HTLC * - otherwise, a receiving node which has an outgoing HTLC
* canceled by `update_fail_malformed_htlc` MUST return an * canceled by `update_fail_malformed_htlc`:
* error in the `update_fail_htlc` sent to the link which *
* originally sent the HTLC using the `failure_code` given and * - MUST return an error in the `update_fail_htlc` sent to
* setting the data to `sha256_of_onion`. * the link which originally sent the HTLC, using the
* `failure_code` given and setting the data to
* `sha256_of_onion`.
*/ */
fail = tal_arr(htlc, u8, 0); fail = tal_arr(htlc, u8, 0);
towire_u16(&fail, failure_code); towire_u16(&fail, failure_code);
@ -1653,10 +1674,11 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
/* BOLT #2: /* BOLT #2:
* *
* If `next_local_commitment_number` is equal to the commitment number * - if `next_local_commitment_number` is equal to the commitment
* of the last `commitment_signed` message the receiving node has * number of the last `commitment_signed` message the receiving node
* sent, it MUST reuse the same commitment number for its next * has sent:
* `commitment_signed` * - MUST reuse the same commitment number for its next
* `commitment_signed`.
*/ */
/* In our case, we consider ourselves already committed to this, so /* In our case, we consider ourselves already committed to this, so
* retransmission is simplest. */ * retransmission is simplest. */
@ -1748,16 +1770,20 @@ static void peer_reconnect(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* On reconnection, a node MUST transmit `channel_reestablish` for * - upon reconnection:
* each channel, and MUST wait for to receive the other node's * - if a channel is in an error state:
* `channel_reestablish` message before sending any other messages for * - SHOULD retransmit the error packet and ignore any other packets for
* that channel. * that channel.
* - otherwise:
* - MUST transmit `channel_reestablish` for each channel.
* - MUST wait to receive the other node's `channel_reestablish`
* message before sending any other messages for that channel.
* *
* The sending node MUST set `next_local_commitment_number` to the * The sending node:
* commitment number of the next `commitment_signed` it expects to * - MUST set `next_local_commitment_number` to the commitment number
* receive, and MUST set `next_remote_revocation_number` to the * of the next `commitment_signed` it expects to receive.
* commitment number of the next `revoke_and_ack` message it expects * - MUST set `next_remote_revocation_number` to the commitment number
* to receive. * of the next `revoke_and_ack` message it expects to receive.
*/ */
msg = towire_channel_reestablish(NULL, &peer->channel_id, msg = towire_channel_reestablish(NULL, &peer->channel_id,
peer->next_index[LOCAL], peer->next_index[LOCAL],
@ -1787,9 +1813,11 @@ static void peer_reconnect(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* If `next_local_commitment_number` is 1 in both the * - if `next_local_commitment_number` is 1 in both the
* `channel_reestablish` it sent and received, then the node MUST * `channel_reestablish` it sent and received:
* retransmit `funding_locked`, otherwise it MUST NOT. * - MUST retransmit `funding_locked`.
* - otherwise:
* - MUST NOT retransmit `funding_locked`.
*/ */
if (peer->funding_locked[LOCAL] if (peer->funding_locked[LOCAL]
&& peer->next_index[LOCAL] == 1 && peer->next_index[LOCAL] == 1
@ -1811,13 +1839,16 @@ static void peer_reconnect(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* If `next_remote_revocation_number` is equal to the commitment * - if `next_local_commitment_number` is equal to the commitment
* number of the last `revoke_and_ack` the receiving node has sent and the receiving node has not already received a `closing_signed`, it * number of the last `commitment_signed` message the receiving node
* MUST re-send the `revoke_and_ack`, otherwise if * has sent:
* `next_remote_revocation_number` is not equal to one greater than * - MUST reuse the same commitment number for its next
* the commitment number of the last `revoke_and_ack` the receiving * `commitment_signed`.
* node has sent (or equal to zero if none have been sent), it SHOULD * - otherwise:
* fail the channel. * - if `next_local_commitment_number` is not 1 greater than the
* commitment number of the last `commitment_signed` message the
* receiving node has sent:
* - SHOULD fail the channel.
*/ */
if (next_remote_revocation_number == peer->next_index[LOCAL] - 2) { if (next_remote_revocation_number == peer->next_index[LOCAL] - 2) {
/* Don't try to retransmit revocation index -1! */ /* Don't try to retransmit revocation index -1! */
@ -1846,10 +1877,11 @@ static void peer_reconnect(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* If `next_local_commitment_number` is equal to the commitment number * - if `next_local_commitment_number` is equal to the commitment
* of the last `commitment_signed` message the receiving node has * number of the last `commitment_signed` message the receiving node
* sent, it MUST reuse the same commitment number for its next * has sent:
* `commitment_signed` * - MUST reuse the same commitment number for its next
* `commitment_signed`.
*/ */
if (next_local_commitment_number == peer->next_index[REMOTE] - 1) { if (next_local_commitment_number == peer->next_index[REMOTE] - 1) {
/* We completed opening, we don't re-transmit that one! */ /* We completed opening, we don't re-transmit that one! */
@ -1864,9 +1896,11 @@ static void peer_reconnect(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* ... otherwise if `next_local_commitment_number` is not one greater * - otherwise:
* than the commitment number of the last `commitment_signed` message * - if `next_local_commitment_number` is not 1 greater than the
* the receiving node has sent, it SHOULD fail the channel. * commitment number of the last `commitment_signed` message the
* receiving node has sent:
* - SHOULD fail the channel.
*/ */
} else if (next_local_commitment_number != peer->next_index[REMOTE]) } else if (next_local_commitment_number != peer->next_index[REMOTE])
peer_failed(&peer->cs, peer_failed(&peer->cs,
@ -1882,11 +1916,11 @@ static void peer_reconnect(struct peer *peer)
/* BOLT #2: /* BOLT #2:
* *
* On reconnection if the node has sent a previous `closing_signed` it * - upon reconnection:
* MUST send another `closing_signed`, otherwise if the node has sent * - if it has sent a previous `shutdown`:
* a previous `shutdown` it MUST retransmit it. * - MUST retransmit `shutdown`.
*/ */
/* If we had sent `closing_signed`, we'd be in closingd. */ /* (If we had sent `closing_signed`, we'd be in closingd). */
maybe_send_shutdown(peer); maybe_send_shutdown(peer);
/* Corner case: we didn't send shutdown before because update_add_htlc /* Corner case: we didn't send shutdown before because update_add_htlc
@ -2051,18 +2085,19 @@ static void handle_feerates(struct peer *peer, const u8 *inmsg)
/* BOLT #2: /* BOLT #2:
* *
* The node which is responsible for paying the bitcoin fee SHOULD * The node _responsible_ for paying the Bitcoin fee:
* send `update_fee` to ensure the current fee rate is sufficient for * - SHOULD send `update_fee` to ensure the current fee rate is
* timely processing of the commitment transaction by a significant * sufficient (by a significant margin) for timely processing of the
* margin. */ * commitment transaction.
*/
if (peer->channel->funder == LOCAL) { if (peer->channel->funder == LOCAL) {
peer->desired_feerate = feerate; peer->desired_feerate = feerate;
start_commit_timer(peer); start_commit_timer(peer);
} else { } else {
/* BOLT #2: /* BOLT #2:
* *
* The node which is not responsible for paying the bitcoin * The node _not responsible_ for paying the Bitcoin fee:
* fee MUST NOT send `update_fee`. * - MUST NOT send `update_fee`.
*/ */
/* FIXME: We could drop to chain if fees are too low, but /* FIXME: We could drop to chain if fees are too low, but
* that's fraught too. */ * that's fraught too. */
@ -2285,9 +2320,11 @@ static void handle_ping_cmd(struct peer *peer, const u8 *inmsg)
/* BOLT #1: /* BOLT #1:
* *
* if `num_pong_bytes` is less than 65532 it MUST respond by sending a * - if `num_pong_bytes` is less than 65532:
* `pong` message with `byteslen` equal to `num_pong_bytes`, otherwise * - MUST respond by sending a `pong` message, with `byteslen` equal
* it MUST ignore the `ping`. * to `num_pong_bytes`.
* - otherwise (`num_pong_bytes` is **not** less than 65532):
* - MUST ignore the `ping`.
*/ */
if (num_pong_bytes >= 65532) if (num_pong_bytes >= 65532)
wire_sync_write(MASTER_FD, wire_sync_write(MASTER_FD,

37
channeld/commit_tx.c

@ -19,21 +19,24 @@ static bool trim(const struct htlc *htlc,
/* BOLT #3: /* BOLT #3:
* *
* For every offered HTLC, if the HTLC amount minus the HTLC-timeout * - for every offered HTLC:
* fee would be less than `dust_limit_satoshis` set by the transaction * - if the HTLC amount minus the HTLC-timeout fee would be less than
* owner, the commitment transaction MUST NOT contain that output, * `dust_limit_satoshis` set by the transaction owner:
* otherwise it MUST be generated as specified in [Offered HTLC * - MUST NOT contain that output.
* Outputs](#offered-htlc-outputs). * - otherwise:
* - MUST be generated as specified in
* [Offered HTLC Outputs](#offered-htlc-outputs).
*/ */
if (htlc_owner(htlc) == side) if (htlc_owner(htlc) == side)
htlc_fee = htlc_timeout_fee(feerate_per_kw); htlc_fee = htlc_timeout_fee(feerate_per_kw);
/* BOLT #3: /* BOLT #3:
* *
* For every received HTLC, if the HTLC amount minus the HTLC-success * - for every received HTLC:
* fee would be less than `dust_limit_satoshis` set by the transaction * - if the HTLC amount minus the HTLC-success fee would be less than
* owner, the commitment transaction MUST NOT contain that output, * `dust_limit_satoshis` set by the transaction owner:
* otherwise it MUST be generated as specified in [Received HTLC * - MUST NOT contain that output.
* Outputs](#received-htlc-outputs). * - otherwise:
* - MUST be generated as specified in
*/ */
else else
htlc_fee = htlc_success_fee(feerate_per_kw); htlc_fee = htlc_success_fee(feerate_per_kw);
@ -129,7 +132,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
/* BOLT #3: /* BOLT #3:
* *
* 3. Subtract this base fee from the funder (either `to_local` or * 3. Subtract this base fee from the funder (either `to_local` or
* `to_remote`), with a floor of zero (see [Fee Payment](#fee-payment)). * `to_remote`), with a floor of 0 (see [Fee Payment](#fee-payment)).
*/ */
try_subtract_fee(funder, side, base_fee_msat, try_subtract_fee(funder, side, base_fee_msat,
&self_pay_msat, &other_pay_msat); &self_pay_msat, &other_pay_msat);
@ -197,7 +200,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* *
* 5. If the `to_local` amount is greater or equal to * 5. If the `to_local` amount is greater or equal to
* `dust_limit_satoshis`, add a [`to_local` * `dust_limit_satoshis`, add a [`to_local`
* Output](#to-local-output). * output](#to-local-output).
*/ */
if (self_pay_msat / 1000 >= dust_limit_satoshis) { if (self_pay_msat / 1000 >= dust_limit_satoshis) {
u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset); u8 *wscript = to_self_wscript(tmpctx, to_self_delay,keyset);
@ -215,15 +218,15 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
* *
* 6. If the `to_remote` amount is greater or equal to * 6. If the `to_remote` amount is greater or equal to
* `dust_limit_satoshis`, add a [`to_remote` * `dust_limit_satoshis`, add a [`to_remote`
* Output](#to-remote-output). * output](#to-remote-output).
*/ */
if (other_pay_msat / 1000 >= dust_limit_satoshis) { if (other_pay_msat / 1000 >= dust_limit_satoshis) {
/* BOLT #3: /* BOLT #3:
* *
* #### `to_remote` Output * #### `to_remote` Output
* *
* This output sends funds to the other peer, thus is a simple * This output sends funds to the other peer and thus is a simple
* P2WPKH to `remotekey`. * P2WPKH to `remotepubkey`.
*/ */
tx->output[n].amount = other_pay_msat / 1000; tx->output[n].amount = other_pay_msat / 1000;
tx->output[n].script = scriptpubkey_p2wpkh(tx, tx->output[n].script = scriptpubkey_p2wpkh(tx,
@ -261,7 +264,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
/* BOLT #3: /* BOLT #3:
* *
* * locktime: upper 8 bits are 0x20, lower 24 bits are the lower * * locktime: upper 8 bits are 0x20, lower 24 bits are the lower
* 24 bits of the obscured commitment transaction number. * 24 bits of the obscured commitment transaction number
*/ */
tx->lock_time tx->lock_time
= (0x20000000 | (obscured_commitment_number & 0xFFFFFF)); = (0x20000000 | (obscured_commitment_number & 0xFFFFFF));
@ -278,7 +281,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx,
/* BOLT #3: /* BOLT #3:
* *
* * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are * * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are
* upper 24 bits of the obscured commitment transaction number. * upper 24 bits of the obscured commitment transaction number
*/ */
tx->input[0].sequence_number tx->input[0].sequence_number
= (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF)); = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));

85
channeld/full_channel.c

@ -313,8 +313,11 @@ static enum channel_add_err add_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if a sending node... sets * A receiving node:
* `cltv_expiry` to greater or equal to 500000000. *...
* - if sending node sets `cltv_expiry` to greater or equal to
* 500000000:
* - SHOULD fail the channel.
*/ */
if (!blocks_to_abs_locktime(cltv_expiry, &htlc->expiry)) { if (!blocks_to_abs_locktime(cltv_expiry, &htlc->expiry)) {
return CHANNEL_ERR_INVALID_EXPIRY; return CHANNEL_ERR_INVALID_EXPIRY;
@ -342,9 +345,10 @@ static enum channel_add_err add_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if it receives an * A receiving node:
* `amount_msat` equal to zero, below its own `htlc_minimum_msat`, * - receiving an `amount_msat` equal to 0, OR less than its own
* or... * `htlc_minimum_msat`:
* - SHOULD fail the channel.
*/ */
if (htlc->msatoshi == 0) { if (htlc->msatoshi == 0) {
return CHANNEL_ERR_HTLC_BELOW_MINIMUM; return CHANNEL_ERR_HTLC_BELOW_MINIMUM;
@ -355,9 +359,8 @@ static enum channel_add_err add_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* For channels with `chain_hash` identifying the Bitcoin blockchain, * - for channels with `chain_hash` identifying the Bitcoin blockchain:
* the sending node MUST set the 4 most significant bytes of * - MUST set the four most significant bytes of `amount_msat` to 0.
* `amount_msat` to zero.
*/ */
if (htlc->msatoshi & 0xFFFFFFFF00000000ULL) { if (htlc->msatoshi & 0xFFFFFFFF00000000ULL) {
return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED;
@ -369,9 +372,10 @@ static enum channel_add_err add_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if a sending node * - if a sending node adds more than its `max_accepted_htlcs` HTLCs to
* adds more than its `max_accepted_htlcs` HTLCs to its local * its local commitment transaction...
* commitment transaction */ * - SHOULD fail the channel.
*/
if (enforce_aggregate_limits if (enforce_aggregate_limits
&& tal_count(committed) - tal_count(removing) + tal_count(adding) && tal_count(committed) - tal_count(removing) + tal_count(adding)
> max_accepted_htlcs(channel, recipient)) { > max_accepted_htlcs(channel, recipient)) {
@ -384,9 +388,11 @@ static enum channel_add_err add_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if a sending node ... or * - if a sending node... adds more than its
* adds more than its `max_htlc_value_in_flight_msat` worth of offered * `max_htlc_value_in_flight_msat` worth of offered HTLCs to its
* HTLCs to its local commitment transaction */ * local commitment transaction:
* - SHOULD fail the channel.
*/
if (enforce_aggregate_limits if (enforce_aggregate_limits
&& msat_in_htlcs > max_htlc_value_in_flight_msat(channel, recipient)) { && msat_in_htlcs > max_htlc_value_in_flight_msat(channel, recipient)) {
return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; return CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED;
@ -394,8 +400,12 @@ static enum channel_add_err add_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* or which the sending node cannot afford at the current * A receiving node:
* `feerate_per_kw` while maintaining its channel reserve. *...
* - receiving an `amount_msat` that the sending node cannot afford at
* the current `feerate_per_kw` (while maintaining its channel
* reserve):
* - SHOULD fail the channel.
*/ */
if (channel->funder == htlc_owner(htlc)) { if (channel->funder == htlc_owner(htlc)) {
u32 feerate = view->feerate_per_kw; u32 feerate = view->feerate_per_kw;
@ -501,9 +511,9 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
sha256(&hash, preimage, sizeof(*preimage)); sha256(&hash, preimage, sizeof(*preimage));
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST check that the `payment_preimage` value in * - if the `payment_preimage` value in `update_fulfill_htlc`
* `update_fulfill_htlc` SHA256 hashes to the corresponding HTLC * doesn't SHA256 hash to the corresponding HTLC `payment_hash`:
* `payment_hash`, and MUST fail the channel if it does not. * - MUST fail the channel.
*/ */
if (!structeq(&hash, &htlc->rhash)) if (!structeq(&hash, &htlc->rhash))
return CHANNEL_ERR_BAD_PREIMAGE; return CHANNEL_ERR_BAD_PREIMAGE;
@ -512,9 +522,9 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST check that `id` corresponds to an HTLC in its * - if the `id` does not correspond to an HTLC in its current
* current commitment transaction, and MUST fail the channel if it * commitment transaction:
* does not. * - MUST fail the channel.
*/ */
if (!htlc_has(htlc, HTLC_FLAG(!htlc_owner(htlc), HTLC_F_COMMITTED))) { if (!htlc_has(htlc, HTLC_FLAG(!htlc_owner(htlc), HTLC_F_COMMITTED))) {
status_trace("channel_fulfill_htlc: %"PRIu64" in state %s", status_trace("channel_fulfill_htlc: %"PRIu64" in state %s",
@ -526,9 +536,12 @@ enum channel_remove_err channel_fulfill_htlc(struct channel *channel,
* based on: */ * based on: */
/* BOLT #2: /* BOLT #2:
* *
* A node MUST NOT send an `update_fulfill_htlc`, `update_fail_htlc` * A node:
* or `update_fail_malformed_htlc` until the corresponding HTLC is *...
* irrevocably committed in both sides' commitment transactions. * - until the corresponding HTLC is irrevocably committed in both
* sides' commitment transactions:
* - MUST NOT send an `update_fulfill_htlc`, `update_fail_htlc`, or
* `update_fail_malformed_htlc`.
*/ */
if (htlc->state == SENT_ADD_ACK_REVOCATION) if (htlc->state == SENT_ADD_ACK_REVOCATION)
htlc->state = RCVD_REMOVE_HTLC; htlc->state = RCVD_REMOVE_HTLC;
@ -559,9 +572,10 @@ enum channel_remove_err channel_fail_htlc(struct channel *channel,
/* BOLT #2: /* BOLT #2:
* *
* A receiving node MUST check that `id` corresponds to an HTLC in its * A receiving node:
* current commitment transaction, and MUST fail the channel if it * - if the `id` does not correspond to an HTLC in its current
* does not. * commitment transaction:
* - MUST fail the channel.
*/ */
if (!htlc_has(htlc, HTLC_FLAG(!htlc_owner(htlc), HTLC_F_COMMITTED))) { if (!htlc_has(htlc, HTLC_FLAG(!htlc_owner(htlc), HTLC_F_COMMITTED))) {
status_trace("channel_fail_htlc: %"PRIu64" in state %s", status_trace("channel_fail_htlc: %"PRIu64" in state %s",
@ -674,9 +688,11 @@ static int change_htlcs(struct channel *channel,
/* FIXME: The sender's requirements are *implied* by this, not stated! */ /* FIXME: The sender's requirements are *implied* by this, not stated! */
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if the sender cannot * A receiving node:
* afford the new fee rate on the receiving node's current commitment *...
* transaction * - if the sender cannot afford the new fee rate on the receiving node's
* current commitment transaction:
* - SHOULD fail the channel,
*/ */
u32 approx_max_feerate(const struct channel *channel) u32 approx_max_feerate(const struct channel *channel)
{ {
@ -715,9 +731,10 @@ bool can_funder_afford_feerate(const struct channel *channel, u32 feerate_per_kw
/* BOLT #2: /* BOLT #2:
* *
* A receiving node SHOULD fail the channel if the sender cannot afford * - if the sender cannot afford the new fee rate on the receiving
* the new fee rate on the receiving node's current commitment * node's current commitment transaction:
* transaction */ * - SHOULD fail the channel
*/
/* Note: sender == funder */ /* Note: sender == funder */
/* How much does it think it has? Must be >= reserve + fee */ /* How much does it think it has? Must be >= reserve + fee */

31
channeld/test/run-full_channel.c

@ -350,24 +350,23 @@ int main(void)
* # Appendix C: Commitment and HTLC Transaction Test Vectors * # Appendix C: Commitment and HTLC Transaction Test Vectors
* *
* In the following: * In the following:
* - we consider *local* transactions, which implies that all payments * - *local* transactions are considered, which implies that all
* to *local* are delayed * payments to *local* are delayed.
* - we assume that *local* is the funder * - It's assumed that *local* is the funder.
* - private keys are displayed as 32 bytes plus a trailing 1 * - Private keys are displayed as 32 bytes plus a trailing 1
* (bitcoin's convention for "compressed" private keys, i.e. keys * (Bitcoin's convention for "compressed" private keys, i.e. keys for
* for which the public key is compressed) * which the public key is compressed).
* - Transaction signatures are all deterministic, using RFC6979 (using
* HMAC-SHA256).
* *
* - transaction signatures are all deterministic, using * To start, common basic parameters for each test vector are defined:
* RFC6979 (using HMAC-SHA256)
*
* We start by defining common basic parameters for each test vector:
* the HTLCs are not used for the first "simple commitment tx with no * the HTLCs are not used for the first "simple commitment tx with no
* HTLCs" test. * HTLCs" test.
* *
* funding_tx_id: 8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be * funding_tx_id: 8984484a580b825b9972d7adb15050b3ab624ccd731946b3eeddb92f4e7ef6be
* funding_output_index: 0 * funding_output_index: 0
* funding_amount_satoshi: 10000000 * funding_amount_satoshi: 10000000
*... * commitment_number: 42
* local_delay: 144 * local_delay: 144
* local_dust_limit_satoshi: 546 * local_dust_limit_satoshi: 546
*/ */
@ -469,10 +468,10 @@ int main(void)
*/ */
local_per_commitment_point = pubkey_from_hex("025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486"); local_per_commitment_point = pubkey_from_hex("025f7117a78150fe2ef97db7cfc83bd57b2e2c0d0dd25eaf467a4a1c2a45ce1486");
/* BOLT #3: /* BOLT #3:
* localkey: 030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e7 * localpubkey: 030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e7
* remotekey: 0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b * remotepubkey: 0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b
* local_delayedkey: 03fd5960528dc152014952efdb702a88f71e3c1653b2314431701ec77e57fde83c * local_delayedpubkey: 03fd5960528dc152014952efdb702a88f71e3c1653b2314431701ec77e57fde83c
* local_revocation_key: 0212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b19 * local_revocation_pubkey: 0212a140cd0c6539d07cd08dfe09984dec3251ea808b892efeac3ede9402bf2b19
*/ */
keyset.self_payment_key = pubkey_from_hex("030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e7"); keyset.self_payment_key = pubkey_from_hex("030d417a46946384f88d5f3337267c5e579765875dc4daca813e21734b140639e7");
keyset.other_payment_key = pubkey_from_hex("0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b"); keyset.other_payment_key = pubkey_from_hex("0394854aa6eab5b2a8122cc726e9dded053a2184d88256816826d6231c068d4a5b");
@ -507,7 +506,7 @@ int main(void)
/* BOLT #3: /* BOLT #3:
* *
* name: commitment tx with all 5 HTLCs untrimmed (minimum feerate) * name: commitment tx with all five HTLCs untrimmed (minimum feerate)
* to_local_msat: 6988000000 * to_local_msat: 6988000000
* to_remote_msat: 3000000000 * to_remote_msat: 3000000000
* local_feerate_per_kw: 0 * local_feerate_per_kw: 0

Loading…
Cancel
Save