Skip to content

Commit 8cc7f31

Browse files
committed
Update wire from spec 9e0a0e8
Now we send genesis block in handshake. Signed-off-by: Rusty Russell <[email protected]>
1 parent e45bc0c commit 8cc7f31

File tree

7 files changed

+49
-19
lines changed

7 files changed

+49
-19
lines changed

bitcoin/block.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
#include "bitcoin/tx.h"
44
#include <ccan/str/hex/hex.h>
55

6+
const struct sha256_double genesis_blockhash
7+
= { { .u.u8 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x19, 0xd6, 0x68,
8+
0x9c, 0x08, 0x5a, 0xe1, 0x65, 0x83, 0x1e, 0x93,
9+
0x4f, 0xf7, 0x63, 0xae, 0x46, 0xa2, 0xa6, 0xc1,
10+
0x72, 0xb3, 0xf1, 0xb6, 0x0a, 0x8c, 0xe2, 0x6f
11+
} } };
12+
613
/* Encoding is <blockhdr> <varint-num-txs> <tx>... */
714
struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
815
const char *hex, size_t hexlen)

bitcoin/block.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ struct bitcoin_block {
2222
struct bitcoin_tx **tx;
2323
};
2424

25+
const struct sha256_double genesis_blockhash;
26+
2527
struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
2628
const char *hex, size_t hexlen);
2729

lightningd/channel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ enum channel_add_err channel_add_htlc(struct channel *channel,
322322
*
323323
* A receiving node SHOULD fail the channel if a sending node ... or
324324
* adds more than its `max-htlc-value-in-flight-msat` worth of offered
325-
* HTLCs to its local commitment transaction. */
325+
* HTLCs to its local commitment transaction */
326326
if (msat_in_htlcs > max_htlc_value_in_flight_msat(channel, recipient)) {
327327
e = CHANNEL_ERR_MAX_HTLC_VALUE_EXCEEDED;
328328
goto out;

lightningd/channel_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* 1. type: 32 (`open_channel`)
1010
* 2. data:
11+
* * [32:chain-hash]
1112
* * [32:temporary-channel-id]
1213
* * [8:funding-satoshis]
1314
* * [8:push-msat]

lightningd/opening/opening.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* FIXME: Handle incoming gossip messages! */
2+
#include <bitcoin/block.h>
23
#include <bitcoin/privkey.h>
34
#include <bitcoin/script.h>
45
#include <ccan/breakpoint/breakpoint.h>
@@ -248,7 +249,7 @@ static u8 *open_channel(struct state *state,
248249
"push-msat must be < %"PRIu64,
249250
1000 * state->funding_satoshis);
250251

251-
msg = towire_open_channel(state, &channel_id,
252+
msg = towire_open_channel(state, &genesis_blockhash.sha, &channel_id,
252253
state->funding_satoshis, state->push_msat,
253254
state->localconf.dust_limit_satoshis,
254255
state->localconf.max_htlc_value_in_flight_msat,
@@ -456,6 +457,7 @@ static u8 *recv_channel(struct state *state,
456457
struct pubkey their_funding_pubkey;
457458
secp256k1_ecdsa_signature theirsig, sig;
458459
struct bitcoin_tx *tx;
460+
struct sha256_double chain_hash;
459461
u8 *msg;
460462

461463
state->remoteconf = tal(state, struct channel_config);
@@ -467,7 +469,7 @@ static u8 *recv_channel(struct state *state,
467469
* `delayed-payment-basepoint` are not valid DER-encoded compressed
468470
* secp256k1 pubkeys.
469471
*/
470-
if (!fromwire_open_channel(peer_msg, NULL, &channel_id,
472+
if (!fromwire_open_channel(peer_msg, NULL, &chain_hash.sha, &channel_id,
471473
&state->funding_satoshis, &state->push_msat,
472474
&state->remoteconf->dust_limit_satoshis,
473475
&state->remoteconf->max_htlc_value_in_flight_msat,
@@ -485,6 +487,20 @@ static u8 *recv_channel(struct state *state,
485487
"Parsing open_channel %s",
486488
tal_hex(peer_msg, peer_msg));
487489

490+
/* BOLT #2:
491+
*
492+
* The receiving MUST reject the channel if the `chain-hash` value
493+
* within the `open_channel` message is set to a hash of a chain
494+
* unknown to the receiver.
495+
*/
496+
if (!structeq(&chain_hash, &genesis_blockhash)) {
497+
peer_failed(PEER_FD, &state->cs, NULL,
498+
WIRE_OPENING_PEER_BAD_INITIAL_MESSAGE,
499+
"Unknown chain-hash %s",
500+
type_to_string(peer_msg, struct sha256_double,
501+
&chain_hash));
502+
}
503+
488504
/* BOLT #2 FIXME:
489505
*
490506
* The receiving node ... MUST fail the channel if `funding-satoshis`

wire/gen_peer_wire_csv

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ error,0,channel-id,32
88
error,32,len,2
99
error,34,data,len
1010
open_channel,32
11-
open_channel,0,temporary-channel-id,32
12-
open_channel,32,funding-satoshis,8
13-
open_channel,40,push-msat,8
14-
open_channel,48,dust-limit-satoshis,8
15-
open_channel,56,max-htlc-value-in-flight-msat,8
16-
open_channel,64,channel-reserve-satoshis,8
17-
open_channel,72,htlc-minimum-msat,4
18-
open_channel,76,feerate-per-kw,4
19-
open_channel,80,to-self-delay,2
20-
open_channel,82,max-accepted-htlcs,2
21-
open_channel,84,funding-pubkey,33
22-
open_channel,117,revocation-basepoint,33
23-
open_channel,150,payment-basepoint,33
24-
open_channel,183,delayed-payment-basepoint,33
25-
open_channel,216,first-per-commitment-point,33
11+
open_channel,0,chain-hash,32
12+
open_channel,32,temporary-channel-id,32
13+
open_channel,64,funding-satoshis,8
14+
open_channel,72,push-msat,8
15+
open_channel,80,dust-limit-satoshis,8
16+
open_channel,88,max-htlc-value-in-flight-msat,8
17+
open_channel,96,channel-reserve-satoshis,8
18+
open_channel,104,htlc-minimum-msat,4
19+
open_channel,108,feerate-per-kw,4
20+
open_channel,112,to-self-delay,2
21+
open_channel,114,max-accepted-htlcs,2
22+
open_channel,116,funding-pubkey,33
23+
open_channel,149,revocation-basepoint,33
24+
open_channel,182,payment-basepoint,33
25+
open_channel,215,delayed-payment-basepoint,33
26+
open_channel,248,first-per-commitment-point,33
2627
accept_channel,33
2728
accept_channel,0,temporary-channel-id,32
2829
accept_channel,32,dust-limit-satoshis,8

wire/test/run-peer-wire.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ struct msg_node_announcement {
162162
u8 *addresses;
163163
};
164164
struct msg_open_channel {
165+
struct sha256 chain_hash;
165166
struct channel_id temporary_channel_id;
166167
u64 funding_satoshis;
167168
u64 push_msat;
@@ -250,6 +251,7 @@ static void *towire_struct_open_channel(const tal_t *ctx,
250251
const struct msg_open_channel *s)
251252
{
252253
return towire_open_channel(ctx,
254+
&s->chain_hash,
253255
&s->temporary_channel_id,
254256
s->funding_satoshis,
255257
s->push_msat,
@@ -271,7 +273,8 @@ static struct msg_open_channel *fromwire_struct_open_channel(const tal_t *ctx, c
271273
{
272274
struct msg_open_channel *s = tal(ctx, struct msg_open_channel);
273275

274-
if (fromwire_open_channel(p, plen,
276+
if (fromwire_open_channel(p, plen,
277+
&s->chain_hash,
275278
&s->temporary_channel_id,
276279
&s->funding_satoshis,
277280
&s->push_msat,

0 commit comments

Comments
 (0)