From 6f181e0dc1a6c442f6235b3367d0a648df60c326 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 6 Jun 2017 09:19:10 +0930 Subject: [PATCH] BOLT update for 8-byte satoshi values, and other updates. Signed-off-by: Rusty Russell --- bitcoin/script.c | 4 +-- daemon/routing.c | 2 +- lightningd/channel.c | 15 +++++++-- lightningd/channel/channel.c | 9 +++--- lightningd/channel/channel_wire.csv | 4 +-- lightningd/channel_config.h | 6 ++-- lightningd/handshake/handshake.c | 9 +++--- lightningd/hsm/hsm.c | 3 +- lightningd/htlc_end.h | 2 +- lightningd/new_connection.c | 6 ++-- lightningd/opening/opening.c | 4 +-- lightningd/peer_control.c | 2 ++ lightningd/sphinx.h | 4 +-- wire/gen_onion_wire_csv | 15 ++++----- wire/gen_peer_wire_csv | 48 ++++++++++++++--------------- wire/test/run-peer-wire.c | 16 +++++----- 16 files changed, 83 insertions(+), 66 deletions(-) diff --git a/bitcoin/script.c b/bitcoin/script.c index b7a043b01..0d08507cb 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -702,7 +702,7 @@ u8 **bitcoin_to_local_spend_revocation(const tal_t *ctx, * key. The output is a P2WSH, with a witness script: * * # To you with revocation key - * OP_DUP OP_HASH160 OP_EQUAL + * OP_DUP OP_HASH160 OP_EQUAL * OP_IF * OP_CHECKSIG * OP_ELSE @@ -767,7 +767,7 @@ u8 *bitcoin_wscript_htlc_offer(const tal_t *ctx, * payment preimage. The output is a P2WSH, with a witness script: * * # To you with revocation key - * OP_DUP OP_HASH160 OP_EQUAL + * OP_DUP OP_HASH160 OP_EQUAL * OP_IF * OP_CHECKSIG * OP_ELSE diff --git a/daemon/routing.c b/daemon/routing.c index 1d839e0be..0df44fc0d 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -762,7 +762,7 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update, size_ u32 timestamp; u16 flags; u16 expiry; - u32 htlc_minimum_msat; + u64 htlc_minimum_msat; u32 fee_base_msat; u32 fee_proportional_millionths; const tal_t *tmpctx = tal_tmpctx(rstate); diff --git a/lightningd/channel.c b/lightningd/channel.c index ea7922a34..dceb55cec 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -351,9 +351,9 @@ enum channel_add_err channel_add_htlc(struct channel *channel, * 2. data: * * [`32`:`channel_id`] * * [`8`:`id`] - * * [`4`:`amount_msat`] - * * [`4`:`cltv_expiry`] + * * [`8`:`amount_msat`] * * [`32`:`payment_hash`] + * * [`4`:`cltv_expiry`] * * [`1366`:`onion_routing_packet`] */ htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE, 0); @@ -391,6 +391,17 @@ enum channel_add_err channel_add_htlc(struct channel *channel, goto out; } + /* BOLT #2: + * + * For channels with `chain_hash` identifying the Bitcoin blockchain, + * the sending node MUST set the 4 most significant bytes of + * `amount_msat` to zero. + */ + if (htlc->msatoshi & 0xFFFFFFFF00000000ULL) { + e = CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED; + goto out; + } + /* Figure out what receiver will already be committed to. */ gather_htlcs(tmpctx, channel, recipient, &committed, &removing, &adding); htlc_arr_append(&adding, htlc); diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 2657a91eb..3d9d2d8af 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -316,14 +316,14 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg) { struct channel_id channel_id; u64 id; - u32 amount_msat; + u64 amount_msat; u32 cltv_expiry; struct sha256 payment_hash; u8 onion_routing_packet[TOTAL_PACKET_SIZE]; enum channel_add_err add_err; if (!fromwire_update_add_htlc(msg, NULL, &channel_id, &id, &amount_msat, - &cltv_expiry, &payment_hash, + &payment_hash, &cltv_expiry, onion_routing_packet)) peer_failed(io_conn_fd(peer->peer_conn), &peer->pcs.cs, @@ -1128,7 +1128,8 @@ static void handle_funding_announce_depth(struct peer *peer, const u8 *msg) static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) { u8 *msg; - u32 amount_msat, cltv_expiry; + u32 cltv_expiry; + u64 amount_msat; struct sha256 payment_hash; u8 onion_routing_packet[TOTAL_PACKET_SIZE]; enum channel_add_err e; @@ -1156,7 +1157,7 @@ static void handle_offer_htlc(struct peer *peer, const u8 *inmsg) /* Tell the peer. */ msg = towire_update_add_htlc(peer, &peer->channel_id, peer->htlc_id, amount_msat, - cltv_expiry, &payment_hash, + &payment_hash, cltv_expiry, onion_routing_packet); msg_enqueue(&peer->peer_out, take(msg)); peer->funding_locked[LOCAL] = true; diff --git a/lightningd/channel/channel_wire.csv b/lightningd/channel/channel_wire.csv index f8aec6508..b90847adf 100644 --- a/lightningd/channel/channel_wire.csv +++ b/lightningd/channel/channel_wire.csv @@ -53,7 +53,7 @@ channel_funding_announce_depth,3 # Tell channel to offer this htlc channel_offer_htlc,4 -channel_offer_htlc,,amount_msat,4 +channel_offer_htlc,,amount_msat,8 channel_offer_htlc,,cltv_expiry,4 channel_offer_htlc,,payment_hash,32 channel_offer_htlc,,onion_routing_packet,1366*u8 @@ -81,7 +81,7 @@ channel_fail_htlc,,error_pkt,len*u8 # Peer and I are irrevocably committed to this HTLC. channel_accepted_htlc,7 channel_accepted_htlc,,id,8 -channel_accepted_htlc,,amount_msat,4 +channel_accepted_htlc,,amount_msat,8 channel_accepted_htlc,,cltv_expiry,4 channel_accepted_htlc,,payment_hash,32 channel_accepted_htlc,,next_onion,1366*u8 diff --git a/lightningd/channel_config.h b/lightningd/channel_config.h index fb10b77ec..5b2ad9d6b 100644 --- a/lightningd/channel_config.h +++ b/lightningd/channel_config.h @@ -15,7 +15,7 @@ * * [`8`:`dust_limit_satoshis`] * * [`8`:`max_htlc_value_in_flight_msat`] * * [`8`:`channel_reserve_satoshis`] - * * [`4`:`htlc_minimum_msat`] + * * [`8`:`htlc_minimum_msat`] * * [`4`:`feerate_per_kw`] * * [`2`:`to_self_delay`] * * [`2`:`max_accepted_htlcs`] @@ -26,8 +26,8 @@ * * [`8`:`dust_limit_satoshis`] * * [`8`:`max_htlc_value_in_flight_msat`] * * [`8`:`channel_reserve_satoshis`] + * * [`8`:`htlc_minimum_msat`] * * [`4`:`minimum_depth`] - * * [`4`:`htlc_minimum_msat`] * * [`2`:`to_self_delay`] * * [`2`:`max_accepted_htlcs`] */ @@ -35,7 +35,7 @@ struct channel_config { u64 dust_limit_satoshis; u64 max_htlc_value_in_flight_msat; u64 channel_reserve_satoshis; - u32 htlc_minimum_msat; + u64 htlc_minimum_msat; u16 to_self_delay; u16 max_accepted_htlcs; }; diff --git a/lightningd/handshake/handshake.c b/lightningd/handshake/handshake.c index 0108d369f..10e2315d4 100644 --- a/lightningd/handshake/handshake.c +++ b/lightningd/handshake/handshake.c @@ -964,10 +964,11 @@ static void exchange_init(int fd, struct crypto_state *cs, /* BOLT #1: * * The sending node SHOULD use the minimum lengths required to - * represent the feature fields. The sending node MUST set feature - * bits corresponding to features it requires the peer to support, and - * SHOULD set feature bits corresponding to features it optionally - * supports. + * represent the feature fields. + * + * The sender MUST set feature bits as defined in [BOLT + * #9](09-features.md), and MUST set to zero any feature bits that are + * not defined. */ u8 *msg = towire_init(NULL, NULL, NULL); diff --git a/lightningd/hsm/hsm.c b/lightningd/hsm/hsm.c index b521c753d..1af139347 100644 --- a/lightningd/hsm/hsm.c +++ b/lightningd/hsm/hsm.c @@ -170,7 +170,8 @@ static struct io_plan *handle_channel_update_sig(struct io_conn *conn, struct sha256_double hash; secp256k1_ecdsa_signature sig; struct short_channel_id scid; - u32 timestamp, htlc_minimum_msat, fee_base_msat, fee_proportional_mill; + u32 timestamp, fee_base_msat, fee_proportional_mill; + u64 htlc_minimum_msat; u16 flags, cltv_expiry_delta; u8 *cu; diff --git a/lightningd/htlc_end.h b/lightningd/htlc_end.h index 047cd9197..e4b6cc60c 100644 --- a/lightningd/htlc_end.h +++ b/lightningd/htlc_end.h @@ -15,7 +15,7 @@ struct htlc_end { enum htlc_end_type which_end; struct peer *peer; u64 htlc_id; - u32 msatoshis; + u64 msatoshis; struct htlc_end *other_end; /* If this is driven by a command. */ diff --git a/lightningd/new_connection.c b/lightningd/new_connection.c index 65d6401f5..34d5307df 100644 --- a/lightningd/new_connection.c +++ b/lightningd/new_connection.c @@ -123,9 +123,9 @@ static bool handshake_succeeded(struct subd *handshaked, /* BOLT #1: * - * The receiving node MUST fail the channels if it receives a - * `globalfeatures` or `localfeatures` with an even bit set which it - * does not understand. + * For unknown feature bits which are non-zero, the receiver + * MUST ignore the bit if the bit number is odd, and MUST fail + * the connection if the bit number is even. */ if (has_even_bit(globalfeatures)) { connection_failed(c, handshaked->log, diff --git a/lightningd/opening/opening.c b/lightningd/opening/opening.c index c0c1d763f..bf5528d22 100644 --- a/lightningd/opening/opening.c +++ b/lightningd/opening/opening.c @@ -107,7 +107,7 @@ static void check_config_bounds(struct state *state, if (remoteconf->htlc_minimum_msat * (u64)1000 > capacity_msat) peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_PEER_BAD_CONFIG, - "Invalid htlc_minimum_msat %u" + "Invalid htlc_minimum_msat %"PRIu64 " for funding_satoshis %"PRIu64 " capacity_msat %"PRIu64, remoteconf->htlc_minimum_msat, @@ -283,8 +283,8 @@ static u8 *funder_channel(struct state *state, ->max_htlc_value_in_flight_msat, &state->remoteconf ->channel_reserve_satoshis, - &minimum_depth, &state->remoteconf->htlc_minimum_msat, + &minimum_depth, &state->remoteconf->to_self_delay, &state->remoteconf->max_accepted_htlcs, &their_funding_pubkey, diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index c483391a5..ac3a6d3d7 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -836,6 +836,8 @@ static u8 *make_failmsg(const tal_t *ctx, const struct htlc_end *hend, return towire_invalid_onion_key(ctx, onion_sha); case WIRE_TEMPORARY_CHANNEL_FAILURE: return towire_temporary_channel_failure(ctx, channel_update); + case WIRE_CHANNEL_DISABLED: + return towire_channel_disabled(ctx); case WIRE_PERMANENT_CHANNEL_FAILURE: return towire_permanent_channel_failure(ctx); case WIRE_REQUIRED_CHANNEL_FEATURE_MISSING: diff --git a/lightningd/sphinx.h b/lightningd/sphinx.h index d4bb333fc..f3ea734d6 100644 --- a/lightningd/sphinx.h +++ b/lightningd/sphinx.h @@ -59,9 +59,9 @@ enum route_next_case { * 1. type: `per_hop` (for `realm` 0) * 2. data: * * [`8`:`channel_id`] - * * [`4`:`amt_to_forward`] + * * [`8`:`amt_to_forward`] * * [`4`:`outgoing_cltv_value`] - * * [`16`:`padding`] + * * [`12`:`padding`] */ struct hop_data { u8 realm; diff --git a/wire/gen_onion_wire_csv b/wire/gen_onion_wire_csv index 5c6cf4e26..fab3c535f 100644 --- a/wire/gen_onion_wire_csv +++ b/wire/gen_onion_wire_csv @@ -9,20 +9,20 @@ invalid_onion_hmac,BADONION|PERM|5 invalid_onion_hmac,0,sha256_of_onion,32 invalid_onion_key,BADONION|PERM|6 invalid_onion_key,0,sha256_of_onion,32 -temporary_channel_failure,7 +temporary_channel_failure,UPDATE|7 temporary_channel_failure,0,len,2 temporary_channel_failure,2,channel_update,len permanent_channel_failure,PERM|8 required_channel_feature_missing,PERM|9 unknown_next_peer,PERM|10 amount_below_minimum,UPDATE|11 -amount_below_minimum,0,htlc_msat,4 -amount_below_minimum,4,len,2 -amount_below_minimum,6,channel_update,len +amount_below_minimum,0,htlc_msat,8 +amount_below_minimum,8,len,2 +amount_below_minimum,10,channel_update,len fee_insufficient,UPDATE|12 -fee_insufficient,0,htlc_msat,4 -fee_insufficient,4,len,2 -fee_insufficient,6,channel_update,len +fee_insufficient,0,htlc_msat,8 +fee_insufficient,8,len,2 +fee_insufficient,10,channel_update,len incorrect_cltv_expiry,UPDATE|13 incorrect_cltv_expiry,0,cltv_expiry,4 incorrect_cltv_expiry,4,len,2 @@ -30,6 +30,7 @@ incorrect_cltv_expiry,6,channel_update,len expiry_too_soon,UPDATE|14 expiry_too_soon,0,len,2 expiry_too_soon,2,channel_update,len +channel_disabled,UPDATE|20 unknown_payment_hash,PERM|15 incorrect_payment_amount,PERM|16 final_expiry_too_soon,17 diff --git a/wire/gen_peer_wire_csv b/wire/gen_peer_wire_csv index c6839990e..0ab68db1a 100644 --- a/wire/gen_peer_wire_csv +++ b/wire/gen_peer_wire_csv @@ -22,29 +22,29 @@ open_channel,72,push_msat,8 open_channel,80,dust_limit_satoshis,8 open_channel,88,max_htlc_value_in_flight_msat,8 open_channel,96,channel_reserve_satoshis,8 -open_channel,104,htlc_minimum_msat,4 -open_channel,108,feerate_per_kw,4 -open_channel,112,to_self_delay,2 -open_channel,114,max_accepted_htlcs,2 -open_channel,116,funding_pubkey,33 -open_channel,149,revocation_basepoint,33 -open_channel,182,payment_basepoint,33 -open_channel,215,delayed_payment_basepoint,33 -open_channel,248,first_per_commitment_point,33 +open_channel,104,htlc_minimum_msat,8 +open_channel,112,feerate_per_kw,4 +open_channel,116,to_self_delay,2 +open_channel,118,max_accepted_htlcs,2 +open_channel,120,funding_pubkey,33 +open_channel,153,revocation_basepoint,33 +open_channel,186,payment_basepoint,33 +open_channel,219,delayed_payment_basepoint,33 +open_channel,252,first_per_commitment_point,33 accept_channel,33 accept_channel,0,temporary_channel_id,32 accept_channel,32,dust_limit_satoshis,8 accept_channel,40,max_htlc_value_in_flight_msat,8 accept_channel,48,channel_reserve_satoshis,8 -accept_channel,56,minimum_depth,4 -accept_channel,60,htlc_minimum_msat,4 -accept_channel,64,to_self_delay,2 -accept_channel,66,max_accepted_htlcs,2 -accept_channel,68,funding_pubkey,33 -accept_channel,101,revocation_basepoint,33 -accept_channel,134,payment_basepoint,33 -accept_channel,167,delayed_payment_basepoint,33 -accept_channel,200,first_per_commitment_point,33 +accept_channel,56,htlc_minimum_msat,8 +accept_channel,64,minimum_depth,4 +accept_channel,68,to_self_delay,2 +accept_channel,70,max_accepted_htlcs,2 +accept_channel,72,funding_pubkey,33 +accept_channel,105,revocation_basepoint,33 +accept_channel,138,payment_basepoint,33 +accept_channel,171,delayed_payment_basepoint,33 +accept_channel,204,first_per_commitment_point,33 funding_created,34 funding_created,0,temporary_channel_id,32 funding_created,32,funding_txid,32 @@ -67,10 +67,10 @@ closing_signed,40,signature,64 update_add_htlc,128 update_add_htlc,0,channel_id,32 update_add_htlc,32,id,8 -update_add_htlc,40,amount_msat,4 -update_add_htlc,44,cltv_expiry,4 +update_add_htlc,40,amount_msat,8 update_add_htlc,48,payment_hash,32 -update_add_htlc,80,onion_routing_packet,1366 +update_add_htlc,80,cltv_expiry,4 +update_add_htlc,84,onion_routing_packet,1366 update_fulfill_htlc,130 update_fulfill_htlc,0,channel_id,32 update_fulfill_htlc,32,id,8 @@ -130,6 +130,6 @@ channel_update,64,short_channel_id,8 channel_update,72,timestamp,4 channel_update,76,flags,2 channel_update,78,cltv_expiry_delta,2 -channel_update,80,htlc_minimum_msat,4 -channel_update,84,fee_base_msat,4 -channel_update,88,fee_proportional_millionths,4 +channel_update,80,htlc_minimum_msat,8 +channel_update,88,fee_base_msat,4 +channel_update,92,fee_proportional_millionths,4 diff --git a/wire/test/run-peer-wire.c b/wire/test/run-peer-wire.c index 55fe29110..63c731bed 100644 --- a/wire/test/run-peer-wire.c +++ b/wire/test/run-peer-wire.c @@ -101,8 +101,8 @@ struct msg_accept_channel { u64 dust_limit_satoshis; u64 max_htlc_value_in_flight_msat; u64 channel_reserve_satoshis; + u64 htlc_minimum_msat; u32 minimum_depth; - u32 htlc_minimum_msat; u16 to_self_delay; u16 max_accepted_htlcs; struct pubkey funding_pubkey; @@ -134,7 +134,7 @@ struct msg_channel_update { u32 timestamp; u16 flags; u16 expiry; - u32 htlc_minimum_msat; + u64 htlc_minimum_msat; u32 fee_base_msat; u32 fee_proportional_millionths; struct short_channel_id short_channel_id; @@ -171,7 +171,7 @@ struct msg_open_channel { u64 dust_limit_satoshis; u64 max_htlc_value_in_flight_msat; u64 channel_reserve_satoshis; - u32 htlc_minimum_msat; + u64 htlc_minimum_msat; u32 feerate_per_kw; u16 to_self_delay; u16 max_accepted_htlcs; @@ -205,7 +205,7 @@ struct msg_init { struct msg_update_add_htlc { struct channel_id channel_id; u64 id; - u32 amount_msat; + u64 amount_msat; u32 expiry; struct sha256 payment_hash; u8 onion_routing_packet[TOTAL_PACKET_SIZE]; @@ -304,8 +304,8 @@ static void *towire_struct_accept_channel(const tal_t *ctx, s->dust_limit_satoshis, s->max_htlc_value_in_flight_msat, s->channel_reserve_satoshis, - s->minimum_depth, s->htlc_minimum_msat, + s->minimum_depth, s->to_self_delay, s->max_accepted_htlcs, &s->funding_pubkey, @@ -324,8 +324,8 @@ static struct msg_accept_channel *fromwire_struct_accept_channel(const tal_t *ct &s->dust_limit_satoshis, &s->max_htlc_value_in_flight_msat, &s->channel_reserve_satoshis, - &s->minimum_depth, &s->htlc_minimum_msat, + &s->minimum_depth, &s->to_self_delay, &s->max_accepted_htlcs, &s->funding_pubkey, @@ -631,8 +631,8 @@ static void *towire_struct_update_add_htlc(const tal_t *ctx, &s->channel_id, s->id, s->amount_msat, - s->expiry, &s->payment_hash, + s->expiry, s->onion_routing_packet); } @@ -644,8 +644,8 @@ static struct msg_update_add_htlc *fromwire_struct_update_add_htlc(const tal_t * &s->channel_id, &s->id, &s->amount_msat, - &s->expiry, &s->payment_hash, + &s->expiry, s->onion_routing_packet)) return s; return tal_free(s);