Skip to content

Commit ba12e31

Browse files
committed
lightningd: fix minimum depth.
Only the side *accepting* the connection gives a `minumum_depth`, but both sides are supposed to wait that long: BOLT #2: ### The `funding_locked` message ... #### Requirements The sender MUST wait until the funding transaction has reached `minimum-depth` before sending this message. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent d27a5d3 commit ba12e31

File tree

6 files changed

+31
-23
lines changed

6 files changed

+31
-23
lines changed

lightningd/channel_config.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ void towire_channel_config(u8 **pptr, const struct channel_config *config)
66
towire_u64(pptr, config->dust_limit_satoshis);
77
towire_u64(pptr, config->max_htlc_value_in_flight_msat);
88
towire_u64(pptr, config->channel_reserve_satoshis);
9-
towire_u32(pptr, config->minimum_depth);
109
towire_u32(pptr, config->htlc_minimum_msat);
1110
towire_u16(pptr, config->to_self_delay);
1211
towire_u16(pptr, config->max_accepted_htlcs);
@@ -18,7 +17,6 @@ void fromwire_channel_config(const u8 **ptr, size_t *max,
1817
config->dust_limit_satoshis = fromwire_u64(ptr, max);
1918
config->max_htlc_value_in_flight_msat = fromwire_u64(ptr, max);
2019
config->channel_reserve_satoshis = fromwire_u64(ptr, max);
21-
config->minimum_depth = fromwire_u32(ptr, max);
2220
config->htlc_minimum_msat = fromwire_u32(ptr, max);
2321
config->to_self_delay = fromwire_u16(ptr, max);
2422
config->max_accepted_htlcs = fromwire_u16(ptr, max);

lightningd/channel_config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ struct channel_config {
3535
u64 dust_limit_satoshis;
3636
u64 max_htlc_value_in_flight_msat;
3737
u64 channel_reserve_satoshis;
38-
u32 minimum_depth;
3938
u32 htlc_minimum_msat;
4039
u16 to_self_delay;
4140
u16 max_accepted_htlcs;

lightningd/opening/opening.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ static u8 *open_channel(struct state *state,
174174
struct basepoints theirs;
175175
struct pubkey their_funding_pubkey;
176176
secp256k1_ecdsa_signature sig;
177+
u32 minimum_depth;
177178
const u8 **wscripts;
178179

179180
set_reserve(&state->localconf.channel_reserve_satoshis,
@@ -236,7 +237,7 @@ static u8 *open_channel(struct state *state,
236237
->max_htlc_value_in_flight_msat,
237238
&state->remoteconf
238239
->channel_reserve_satoshis,
239-
&state->remoteconf->minimum_depth,
240+
&minimum_depth,
240241
&state->remoteconf->htlc_minimum_msat,
241242
&state->remoteconf->to_self_delay,
242243
&state->remoteconf->max_accepted_htlcs,
@@ -266,10 +267,10 @@ static u8 *open_channel(struct state *state,
266267
* Other fields have the same requirements as their counterparts in
267268
* `open_channel`.
268269
*/
269-
if (state->remoteconf->minimum_depth > max_minimum_depth)
270+
if (minimum_depth > max_minimum_depth)
270271
peer_failed(PEER_FD, &state->cs, NULL, WIRE_OPENING_BAD_PARAM,
271272
"minimum_depth %u larger than %u",
272-
state->remoteconf->minimum_depth, max_minimum_depth);
273+
minimum_depth, max_minimum_depth);
273274
check_config_bounds(state, state->remoteconf);
274275

275276
/* Now, ask master create a transaction to pay those two addresses. */
@@ -397,14 +398,16 @@ static u8 *open_channel(struct state *state,
397398
&theirs.revocation,
398399
&theirs.payment,
399400
&theirs.delayed_payment,
400-
&state->next_per_commit[REMOTE]);
401+
&state->next_per_commit[REMOTE],
402+
minimum_depth);
401403
}
402404

403405
/* This is handed the message the peer sent which caused gossip to stop:
404406
* it should be an open_channel */
405407
static u8 *recv_channel(struct state *state,
406408
const struct pubkey *our_funding_pubkey,
407409
const struct basepoints *ours,
410+
u32 minimum_depth,
408411
u32 min_feerate, u32 max_feerate, const u8 *peer_msg)
409412
{
410413
struct channel_id id_in, channel_id;
@@ -501,7 +504,7 @@ static u8 *recv_channel(struct state *state,
501504
state->localconf
502505
.max_htlc_value_in_flight_msat,
503506
state->localconf.channel_reserve_satoshis,
504-
state->localconf.minimum_depth,
507+
minimum_depth,
505508
state->localconf.htlc_minimum_msat,
506509
state->localconf.to_self_delay,
507510
state->localconf.max_accepted_htlcs,
@@ -637,7 +640,7 @@ int main(int argc, char *argv[])
637640
struct privkey seed;
638641
struct basepoints our_points;
639642
struct pubkey our_funding_pubkey;
640-
u32 max_minimum_depth;
643+
u32 minimum_depth, max_minimum_depth;
641644
u32 min_feerate, max_feerate;
642645

643646
if (argc == 2 && streq(argv[1], "--version")) {
@@ -685,10 +688,11 @@ int main(int argc, char *argv[])
685688
&state->feerate_per_kw, &max_minimum_depth))
686689
msg = open_channel(state, &our_funding_pubkey, &our_points,
687690
max_minimum_depth);
688-
else if (fromwire_opening_accept(state, msg, NULL, &min_feerate,
689-
&max_feerate, &peer_msg))
691+
else if (fromwire_opening_accept(state, msg, NULL, &minimum_depth,
692+
&min_feerate, &max_feerate, &peer_msg))
690693
msg = recv_channel(state, &our_funding_pubkey, &our_points,
691-
min_feerate, max_feerate, peer_msg);
694+
minimum_depth, min_feerate, max_feerate,
695+
peer_msg);
692696

693697
/* Write message and hand back the fd. */
694698
wire_sync_write(REQ_FD, msg);

lightningd/opening/opening_wire.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ opening_open_funding_reply,244,revocation_basepoint,33
4949
opening_open_funding_reply,277,payment_basepoint,33
5050
opening_open_funding_reply,310,delayed_payment_basepoint,33
5151
opening_open_funding_reply,343,their_per_commit_point,33
52+
opening_open_funding_reply,376,minimum_depth,4
5253

5354
# This means they offer the open (contains their offer packet)
5455
opening_accept,3
56+
opening_accept,0,minimum_depth,4
5557
opening_accept,0,min_feerate,4
5658
opening_accept,4,max_feerate,4
5759
opening_accept,8,len,2

lightningd/peer_control.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,9 @@ static enum watch_result funding_depth_cb(struct peer *peer,
553553
loc = tal_free(loc);
554554

555555
log_debug(peer->log, "Funding tx %s depth %u of %u",
556-
txidstr, depth, peer->our_config.minimum_depth);
556+
txidstr, depth, peer->minimum_depth);
557557

558-
if (depth < peer->our_config.minimum_depth)
558+
if (depth < peer->minimum_depth)
559559
return KEEP_WATCHING;
560560

561561
/* In theory, it could have been buried before we got back
@@ -981,7 +981,8 @@ static bool opening_release_tx(struct subd *opening, const u8 *resp,
981981
&theirbase.revocation,
982982
&theirbase.payment,
983983
&theirbase.delayed_payment,
984-
&their_per_commit_point)) {
984+
&their_per_commit_point,
985+
&fc->peer->minimum_depth)) {
985986
log_broken(fc->peer->log, "bad OPENING_OPEN_FUNDING_REPLY %s",
986987
tal_hex(resp, resp));
987988
tal_free(fc->peer);
@@ -1134,13 +1135,6 @@ static void channel_config(struct lightningd *ld,
11341135
ours->dust_limit_satoshis = 546;
11351136
ours->max_htlc_value_in_flight_msat = UINT64_MAX;
11361137

1137-
/* BOLT #2:
1138-
*
1139-
* The sender SHOULD set `minimum-depth` to an amount where
1140-
* the sender considers reorganizations to be low risk.
1141-
*/
1142-
ours->minimum_depth = ld->dstate.config.anchor_confirms;
1143-
11441138
/* Don't care */
11451139
ours->htlc_minimum_msat = 0;
11461140

@@ -1197,6 +1191,13 @@ void peer_accept_open(struct peer *peer,
11971191
/* We handed off peer fd */
11981192
peer->fd = -1;
11991193

1194+
/* BOLT #2:
1195+
*
1196+
* The sender SHOULD set `minimum-depth` to an amount where
1197+
* the sender considers reorganizations to be low risk.
1198+
*/
1199+
peer->minimum_depth = ld->dstate.config.anchor_confirms;
1200+
12001201
channel_config(ld, &peer->our_config,
12011202
&max_to_self_delay, &max_minimum_depth,
12021203
&min_effective_htlc_capacity_msat);
@@ -1209,7 +1210,8 @@ void peer_accept_open(struct peer *peer,
12091210
cs, peer->seed);
12101211

12111212
subd_send_msg(peer->owner, take(msg));
1212-
msg = towire_opening_accept(peer, 7500, 150000, from_peer);
1213+
msg = towire_opening_accept(peer, peer->minimum_depth,
1214+
7500, 150000, from_peer);
12131215

12141216
/* Careful here! Their message could push us overlength! */
12151217
if (tal_len(msg) >= 65536) {

lightningd/peer_control.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ struct peer {
4646
/* Our channel config. */
4747
struct channel_config our_config;
4848

49+
/* Minimum funding depth (specified by us if they fund). */
50+
u32 minimum_depth;
51+
4952
/* Funding txid and amounts (once known) */
5053
struct sha256_double *funding_txid;
5154
u16 funding_outnum;

0 commit comments

Comments
 (0)