mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-21 22:31:48 +01:00
peer_failed: automatically hand PEER_FD, GOSSIP_FD; add gossip_index
We make it a macro, since everyone uses PEER_FD and GOSSIP_FD constants (they're actually always the same, but this is slightly safer), and add a gossip_index arg: this is groundwork for when we want to hand the peer back to master for gossipd. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
e17b0ebcb4
commit
201d498e39
5 changed files with 150 additions and 116 deletions
|
@ -452,11 +452,13 @@ static void handle_peer_funding_locked(struct peer *peer, const u8 *msg)
|
|||
peer->old_remote_per_commit = peer->remote_per_commit;
|
||||
if (!fromwire_funding_locked(msg, NULL, &chanid,
|
||||
&peer->remote_per_commit))
|
||||
peer_failed(PEER_FD, &peer->cs, &peer->channel_id,
|
||||
peer_failed(&peer->cs, peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad funding_locked %s", tal_hex(msg, msg));
|
||||
|
||||
if (!structeq(&chanid, &peer->channel_id))
|
||||
peer_failed(PEER_FD, &peer->cs, &peer->channel_id,
|
||||
peer_failed(&peer->cs, peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Wrong channel id in %s (expected %s)",
|
||||
tal_hex(trc, msg),
|
||||
type_to_string(msg, struct channel_id,
|
||||
|
@ -485,7 +487,8 @@ static void check_short_ids_match(struct peer *peer)
|
|||
|
||||
if (!short_channel_id_eq(&peer->short_channel_ids[LOCAL],
|
||||
&peer->short_channel_ids[REMOTE]))
|
||||
peer_failed(PEER_FD, &peer->cs, &peer->channel_id,
|
||||
peer_failed(&peer->cs, peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"We disagree on short_channel_ids:"
|
||||
" I have %s, you say %s",
|
||||
type_to_string(peer, struct short_channel_id,
|
||||
|
@ -519,13 +522,15 @@ static void handle_peer_announcement_signatures(struct peer *peer, const u8 *msg
|
|||
&peer->short_channel_ids[REMOTE],
|
||||
&peer->announcement_node_sigs[REMOTE],
|
||||
&peer->announcement_bitcoin_sigs[REMOTE]))
|
||||
peer_failed(PEER_FD, &peer->cs, &peer->channel_id,
|
||||
peer_failed(&peer->cs, peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad announcement_signatures %s",
|
||||
tal_hex(msg, msg));
|
||||
|
||||
/* Make sure we agree on the channel ids */
|
||||
if (!structeq(&chanid, &peer->channel_id)) {
|
||||
peer_failed(PEER_FD, &peer->cs, &peer->channel_id,
|
||||
peer_failed(&peer->cs, peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Wrong channel_id: expected %s, got %s",
|
||||
type_to_string(trc, struct channel_id,
|
||||
&peer->channel_id),
|
||||
|
@ -584,8 +589,8 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
|
|||
if (!fromwire_update_add_htlc(msg, NULL, &channel_id, &id, &amount_msat,
|
||||
&payment_hash, &cltv_expiry,
|
||||
onion_routing_packet))
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad peer_add_htlc %s", tal_hex(msg, msg));
|
||||
|
||||
|
@ -593,8 +598,8 @@ static void handle_peer_add_htlc(struct peer *peer, const u8 *msg)
|
|||
cltv_expiry, &payment_hash,
|
||||
onion_routing_packet, &htlc);
|
||||
if (add_err != CHANNEL_ERR_ADD_OK)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad peer_add_htlc: %s",
|
||||
channel_add_err_name(add_err));
|
||||
|
@ -611,8 +616,8 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
|
|||
u32 feerate;
|
||||
|
||||
if (!fromwire_update_fee(msg, NULL, &channel_id, &feerate)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fee %s", tal_hex(msg, msg));
|
||||
}
|
||||
|
@ -623,8 +628,8 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
|
|||
* responsible for paying the bitcoin fee.
|
||||
*/
|
||||
if (peer->channel->funder != REMOTE)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"update_fee from non-funder?");
|
||||
|
||||
|
@ -637,8 +642,8 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
|
|||
* low for timely processing, or unreasonably large.
|
||||
*/
|
||||
if (feerate < peer->feerate_min || feerate > peer->feerate_max)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"update_fee %u outside range %u-%u",
|
||||
feerate, peer->feerate_min, peer->feerate_max);
|
||||
|
@ -651,8 +656,8 @@ static void handle_peer_feechange(struct peer *peer, const u8 *msg)
|
|||
* committed.
|
||||
*/
|
||||
if (!channel_update_feerate(peer->channel, feerate))
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"update_fee %u unaffordable",
|
||||
feerate);
|
||||
|
@ -1146,8 +1151,8 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
|
|||
* A node MUST NOT send a `commitment_signed` message which
|
||||
* does not include any updates.
|
||||
*/
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"commit_sig with no changes");
|
||||
}
|
||||
|
@ -1160,8 +1165,8 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
|
|||
|
||||
if (!fromwire_commitment_signed(tmpctx, msg, NULL,
|
||||
&channel_id, &commit_sig, &htlc_sigs))
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad commit_sig %s", tal_hex(msg, msg));
|
||||
|
||||
|
@ -1192,8 +1197,8 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
|
|||
if (!check_tx_sig(txs[0], 0, NULL, wscripts[0],
|
||||
&peer->channel->funding_pubkey[REMOTE], &commit_sig)) {
|
||||
dump_htlcs(peer->channel, "receiving commit_sig");
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad commit_sig signature %"PRIu64" %s for tx %s wscript %s key %s",
|
||||
peer->next_index[LOCAL],
|
||||
|
@ -1213,8 +1218,8 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
|
|||
* once all pending updates are applied.
|
||||
*/
|
||||
if (tal_count(htlc_sigs) != tal_count(txs) - 1)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Expected %zu htlc sigs, not %zu",
|
||||
tal_count(txs) - 1, tal_count(htlc_sigs));
|
||||
|
@ -1228,8 +1233,8 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
|
|||
for (i = 0; i < tal_count(htlc_sigs); i++) {
|
||||
if (!check_tx_sig(txs[1+i], 0, NULL, wscripts[1+i],
|
||||
&remote_htlckey, &htlc_sigs[i]))
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad commit_sig signature %s for htlc %s wscript %s key %s",
|
||||
type_to_string(msg, secp256k1_ecdsa_signature, &htlc_sigs[i]),
|
||||
|
@ -1291,15 +1296,15 @@ static void handle_peer_revoke_and_ack(struct peer *peer, const u8 *msg)
|
|||
|
||||
if (!fromwire_revoke_and_ack(msg, NULL, &channel_id, &old_commit_secret,
|
||||
&next_per_commit)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad revoke_and_ack %s", tal_hex(msg, msg));
|
||||
}
|
||||
|
||||
if (peer->revocations_received != peer->next_index[REMOTE] - 2) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Unexpected revoke_and_ack");
|
||||
}
|
||||
|
@ -1312,15 +1317,15 @@ static void handle_peer_revoke_and_ack(struct peer *peer, const u8 *msg)
|
|||
*/
|
||||
memcpy(&privkey, &old_commit_secret, sizeof(privkey));
|
||||
if (!pubkey_from_privkey(&privkey, &per_commit_point)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad privkey %s",
|
||||
type_to_string(msg, struct privkey, &privkey));
|
||||
}
|
||||
if (!pubkey_eq(&per_commit_point, &peer->old_remote_per_commit)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Wrong privkey %s for %"PRIu64" %s",
|
||||
type_to_string(msg, struct privkey, &privkey),
|
||||
|
@ -1366,8 +1371,8 @@ static void handle_peer_fulfill_htlc(struct peer *peer, const u8 *msg)
|
|||
|
||||
if (!fromwire_update_fulfill_htlc(msg, NULL, &channel_id,
|
||||
&id, &preimage)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fulfill_htlc %s", tal_hex(msg, msg));
|
||||
}
|
||||
|
@ -1386,8 +1391,8 @@ static void handle_peer_fulfill_htlc(struct peer *peer, const u8 *msg)
|
|||
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
||||
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
||||
case CHANNEL_ERR_BAD_PREIMAGE:
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fulfill_htlc: failed to fulfill %"
|
||||
PRIu64 " error %s", id, channel_remove_err_name(e));
|
||||
|
@ -1405,8 +1410,8 @@ static void handle_peer_fail_htlc(struct peer *peer, const u8 *msg)
|
|||
|
||||
if (!fromwire_update_fail_htlc(msg, msg, NULL,
|
||||
&channel_id, &id, &reason)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fulfill_htlc %s", tal_hex(msg, msg));
|
||||
}
|
||||
|
@ -1424,8 +1429,8 @@ static void handle_peer_fail_htlc(struct peer *peer, const u8 *msg)
|
|||
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
||||
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
||||
case CHANNEL_ERR_BAD_PREIMAGE:
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fail_htlc: failed to remove %"
|
||||
PRIu64 " error %s", id,
|
||||
|
@ -1447,8 +1452,8 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
|
|||
if (!fromwire_update_fail_malformed_htlc(msg, NULL, &channel_id, &id,
|
||||
&sha256_of_onion,
|
||||
&failure_code)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fail_malformed_htlc %s",
|
||||
tal_hex(msg, msg));
|
||||
|
@ -1460,8 +1465,8 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
|
|||
* `failure_code` is not set for `update_fail_malformed_htlc`.
|
||||
*/
|
||||
if (!(failure_code & BADONION)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fail_malformed_htlc failure code %u",
|
||||
failure_code);
|
||||
|
@ -1498,8 +1503,8 @@ static void handle_peer_fail_malformed_htlc(struct peer *peer, const u8 *msg)
|
|||
case CHANNEL_ERR_HTLC_UNCOMMITTED:
|
||||
case CHANNEL_ERR_HTLC_NOT_IRREVOCABLE:
|
||||
case CHANNEL_ERR_BAD_PREIMAGE:
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad update_fail_malformed_htlc: failed to remove %"
|
||||
PRIu64 " error %s", id, channel_remove_err_name(e));
|
||||
|
@ -1513,14 +1518,14 @@ static void handle_pong(struct peer *peer, const u8 *pong)
|
|||
|
||||
status_trace("Got pong!");
|
||||
if (!fromwire_pong(pong, pong, NULL, &ignored))
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad pong %s", tal_hex(pong, pong));
|
||||
|
||||
if (!peer->num_pings_outstanding)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Unexpected pong");
|
||||
|
||||
|
@ -1538,8 +1543,8 @@ static void handle_peer_shutdown(struct peer *peer, const u8 *shutdown)
|
|||
wire_sync_write(GOSSIP_FD, take(msg));
|
||||
|
||||
if (!fromwire_shutdown(peer, shutdown, NULL, &channel_id, &scriptpubkey))
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Bad shutdown %s", tal_hex(peer, shutdown));
|
||||
|
||||
|
@ -1559,8 +1564,8 @@ static void peer_in(struct peer *peer, const u8 *msg)
|
|||
/* Must get funding_locked before almost anything. */
|
||||
if (!peer->funding_locked[REMOTE]) {
|
||||
if (type != WIRE_FUNDING_LOCKED && type != WIRE_PONG) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"%s (%u) before funding locked",
|
||||
wire_type_name(type), type);
|
||||
|
@ -1620,8 +1625,8 @@ static void peer_in(struct peer *peer, const u8 *msg)
|
|||
abort();
|
||||
}
|
||||
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Peer sent unknown message %u (%s)",
|
||||
type, wire_type_name(type));
|
||||
|
@ -1662,8 +1667,8 @@ static void send_fail_or_fulfill(struct peer *peer, const struct htlc *h)
|
|||
msg = towire_update_fulfill_htlc(peer, &peer->channel_id, h->id,
|
||||
h->r);
|
||||
} else
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"HTLC %"PRIu64" state %s not failed/fulfilled",
|
||||
h->id, htlc_state_name(h->state));
|
||||
|
@ -1695,8 +1700,8 @@ static void resend_commitment(struct peer *peer, const struct changed_htlc *last
|
|||
/* I think this can happen if we actually received revoke_and_ack
|
||||
* then they asked for a retransmit */
|
||||
if (!h)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"Can't find HTLC %"PRIu64" to resend",
|
||||
last[i].id);
|
||||
|
@ -1791,8 +1796,8 @@ static void peer_reconnect(struct peer *peer)
|
|||
if (!fromwire_channel_reestablish(msg, NULL, &channel_id,
|
||||
&next_local_commitment_number,
|
||||
&next_remote_revocation_number)) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"bad reestablish msg: %s %s",
|
||||
wire_type_name(fromwire_peektype(msg)),
|
||||
|
@ -1840,8 +1845,8 @@ static void peer_reconnect(struct peer *peer)
|
|||
if (next_remote_revocation_number == peer->next_index[LOCAL] - 2) {
|
||||
/* Don't try to retransmit revocation index -1! */
|
||||
if (peer->next_index[LOCAL] < 2) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"bad reestablish revocation_number: %"
|
||||
PRIu64,
|
||||
|
@ -1849,8 +1854,8 @@ static void peer_reconnect(struct peer *peer)
|
|||
}
|
||||
retransmit_revoke_and_ack = true;
|
||||
} else if (next_remote_revocation_number != peer->next_index[LOCAL] - 1) {
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"bad reestablish revocation_number: %"PRIu64
|
||||
" vs %"PRIu64,
|
||||
|
@ -1874,8 +1879,8 @@ static void peer_reconnect(struct peer *peer)
|
|||
if (next_local_commitment_number == peer->next_index[REMOTE] - 1) {
|
||||
/* We completed opening, we don't re-transmit that one! */
|
||||
if (next_local_commitment_number == 0)
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"bad reestablish commitment_number: %"
|
||||
PRIu64,
|
||||
|
@ -1890,8 +1895,8 @@ static void peer_reconnect(struct peer *peer)
|
|||
* the receiving node has sent, it SHOULD fail the channel.
|
||||
*/
|
||||
} else if (next_local_commitment_number != peer->next_index[REMOTE])
|
||||
peer_failed(PEER_FD,
|
||||
&peer->cs,
|
||||
peer_failed(&peer->cs,
|
||||
peer->gossip_index,
|
||||
&peer->channel_id,
|
||||
"bad reestablish commitment_number: %"PRIu64
|
||||
" vs %"PRIu64,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* FIXME: We don't relay from gossipd at all here. */
|
||||
#include <bitcoin/script.h>
|
||||
#include <ccan/structeq/structeq.h>
|
||||
#include <closingd/gen_closing_wire.h>
|
||||
|
@ -27,6 +28,7 @@
|
|||
|
||||
static struct bitcoin_tx *close_tx(const tal_t *ctx,
|
||||
struct crypto_state *cs,
|
||||
u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
u8 *scriptpubkey[NUM_SIDES],
|
||||
const struct bitcoin_txid *funding_txid,
|
||||
|
@ -40,7 +42,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
|
|||
struct bitcoin_tx *tx;
|
||||
|
||||
if (satoshi_out[funder] < fee)
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"Funder cannot afford fee %"PRIu64
|
||||
" (%"PRIu64" and %"PRIu64")",
|
||||
fee, satoshi_out[LOCAL],
|
||||
|
@ -59,7 +61,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
|
|||
satoshi_out[REMOTE] - (funder == REMOTE ? fee : 0),
|
||||
dust_limit);
|
||||
if (!tx)
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"Both outputs below dust limit:"
|
||||
" funding = %"PRIu64
|
||||
" fee = %"PRIu64
|
||||
|
@ -91,6 +93,7 @@ static u8 *closing_read_peer_msg(const tal_t *ctx,
|
|||
}
|
||||
|
||||
static void do_reconnect(struct crypto_state *cs,
|
||||
u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
const u64 next_index[NUM_SIDES],
|
||||
u64 revocations_received)
|
||||
|
@ -124,7 +127,7 @@ static void do_reconnect(struct crypto_state *cs,
|
|||
if (!fromwire_channel_reestablish(msg, NULL, &their_channel_id,
|
||||
&next_local_commitment_number,
|
||||
&next_remote_revocation_number)) {
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"bad reestablish msg: %s %s",
|
||||
wire_type_name(fromwire_peektype(msg)),
|
||||
tal_hex(tmpctx, msg));
|
||||
|
@ -149,6 +152,7 @@ static void do_reconnect(struct crypto_state *cs,
|
|||
}
|
||||
|
||||
static void send_offer(struct crypto_state *cs,
|
||||
u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
const struct pubkey funding_pubkey[NUM_SIDES],
|
||||
const u8 *funding_wscript,
|
||||
|
@ -173,7 +177,7 @@ static void send_offer(struct crypto_state *cs,
|
|||
* the close transaction as specified in [BOLT
|
||||
* #3](03-transactions.md#closing-transaction).
|
||||
*/
|
||||
tx = close_tx(tmpctx, cs, channel_id,
|
||||
tx = close_tx(tmpctx, cs, gossip_index, channel_id,
|
||||
scriptpubkey,
|
||||
funding_txid,
|
||||
funding_txout,
|
||||
|
@ -222,6 +226,7 @@ static void tell_master_their_offer(u64 their_offer,
|
|||
|
||||
/* Returns fee they offered. */
|
||||
static uint64_t receive_offer(struct crypto_state *cs,
|
||||
u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
const struct pubkey funding_pubkey[NUM_SIDES],
|
||||
const u8 *funding_wscript,
|
||||
|
@ -265,7 +270,7 @@ static uint64_t receive_offer(struct crypto_state *cs,
|
|||
|
||||
if (!fromwire_closing_signed(msg, NULL, &their_channel_id,
|
||||
&received_fee, &their_sig))
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"Expected closing_signed: %s",
|
||||
tal_hex(trc, msg));
|
||||
|
||||
|
@ -276,7 +281,7 @@ static uint64_t receive_offer(struct crypto_state *cs,
|
|||
* #3](03-transactions.md#closing-transaction), and MUST fail
|
||||
* the connection if it is not.
|
||||
*/
|
||||
tx = close_tx(tmpctx, cs, channel_id,
|
||||
tx = close_tx(tmpctx, cs, gossip_index, channel_id,
|
||||
scriptpubkey,
|
||||
funding_txid,
|
||||
funding_txout,
|
||||
|
@ -302,7 +307,7 @@ static uint64_t receive_offer(struct crypto_state *cs,
|
|||
* then remove any output below its own `dust_limit_satoshis`,
|
||||
* and MAY also eliminate its own output.
|
||||
*/
|
||||
trimmed = close_tx(tmpctx, cs, channel_id,
|
||||
trimmed = close_tx(tmpctx, cs, gossip_index, channel_id,
|
||||
scriptpubkey,
|
||||
funding_txid,
|
||||
funding_txout,
|
||||
|
@ -312,7 +317,7 @@ static uint64_t receive_offer(struct crypto_state *cs,
|
|||
if (!trimmed
|
||||
|| !check_tx_sig(trimmed, 0, NULL, funding_wscript,
|
||||
&funding_pubkey[REMOTE], &their_sig)) {
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"Bad closing_signed signature for"
|
||||
" %s (and trimmed version %s)",
|
||||
type_to_string(tmpctx,
|
||||
|
@ -372,13 +377,14 @@ static void init_feerange(struct feerange *feerange,
|
|||
}
|
||||
|
||||
static void adjust_feerange(struct crypto_state *cs,
|
||||
u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
struct feerange *feerange,
|
||||
u64 offer, enum side side)
|
||||
{
|
||||
if (offer < feerange->min || offer > feerange->max) {
|
||||
if (!feerange->allow_mistakes || side != REMOTE)
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"%s offer %"PRIu64
|
||||
" not between %"PRIu64" and %"PRIu64,
|
||||
side == LOCAL ? "local" : "remote",
|
||||
|
@ -405,6 +411,7 @@ static void adjust_feerange(struct crypto_state *cs,
|
|||
|
||||
/* Figure out what we should offer now. */
|
||||
static u64 adjust_offer(struct crypto_state *cs,
|
||||
u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
const struct feerange *feerange,
|
||||
u64 remote_offer,
|
||||
|
@ -416,7 +423,7 @@ static u64 adjust_offer(struct crypto_state *cs,
|
|||
|
||||
/* Max is below our minimum acceptable? */
|
||||
if (feerange->max < min_fee_to_accept)
|
||||
peer_failed(PEER_FD, cs, channel_id,
|
||||
peer_failed(cs, gossip_index, channel_id,
|
||||
"Feerange %"PRIu64"-%"PRIu64
|
||||
" below minimum acceptable %"PRIu64,
|
||||
feerange->min, feerange->max,
|
||||
|
@ -490,7 +497,8 @@ int main(int argc, char *argv[])
|
|||
&funding_pubkey[REMOTE]);
|
||||
|
||||
if (reconnected)
|
||||
do_reconnect(&cs, &channel_id, next_index, revocations_received);
|
||||
do_reconnect(&cs, gossip_index, &channel_id,
|
||||
next_index, revocations_received);
|
||||
|
||||
/* BOLT #2:
|
||||
*
|
||||
|
@ -502,14 +510,16 @@ int main(int argc, char *argv[])
|
|||
whose_turn = funder;
|
||||
for (size_t i = 0; i < 2; i++, whose_turn = !whose_turn) {
|
||||
if (whose_turn == LOCAL) {
|
||||
send_offer(&cs, &channel_id, funding_pubkey,
|
||||
send_offer(&cs, gossip_index,
|
||||
&channel_id, funding_pubkey,
|
||||
funding_wscript,
|
||||
scriptpubkey, &funding_txid, funding_txout,
|
||||
funding_satoshi, satoshi_out, funder,
|
||||
our_dust_limit, &secrets, offer[LOCAL]);
|
||||
} else {
|
||||
offer[REMOTE]
|
||||
= receive_offer(&cs, &channel_id, funding_pubkey,
|
||||
= receive_offer(&cs, gossip_index,
|
||||
&channel_id, funding_pubkey,
|
||||
funding_wscript,
|
||||
scriptpubkey, &funding_txid,
|
||||
funding_txout, funding_satoshi,
|
||||
|
@ -523,30 +533,33 @@ int main(int argc, char *argv[])
|
|||
init_feerange(&feerange, commitment_fee, offer, deprecated_api);
|
||||
|
||||
/* Now apply the one constraint from above (other is inside loop). */
|
||||
adjust_feerange(&cs, &channel_id, &feerange,
|
||||
adjust_feerange(&cs, gossip_index, &channel_id, &feerange,
|
||||
offer[!whose_turn], !whose_turn);
|
||||
|
||||
/* Now any extra rounds required. */
|
||||
while (offer[LOCAL] != offer[REMOTE]) {
|
||||
/* If they differ, adjust feerate. */
|
||||
adjust_feerange(&cs, &channel_id, &feerange,
|
||||
adjust_feerange(&cs, gossip_index, &channel_id, &feerange,
|
||||
offer[whose_turn], whose_turn);
|
||||
|
||||
/* Now its the other side's turn. */
|
||||
whose_turn = !whose_turn;
|
||||
|
||||
if (whose_turn == LOCAL) {
|
||||
offer[LOCAL] = adjust_offer(&cs, &channel_id,
|
||||
offer[LOCAL] = adjust_offer(&cs, gossip_index,
|
||||
&channel_id,
|
||||
&feerange, offer[REMOTE],
|
||||
min_fee_to_accept);
|
||||
send_offer(&cs, &channel_id, funding_pubkey,
|
||||
send_offer(&cs, gossip_index, &channel_id,
|
||||
funding_pubkey,
|
||||
funding_wscript,
|
||||
scriptpubkey, &funding_txid, funding_txout,
|
||||
funding_satoshi, satoshi_out, funder,
|
||||
our_dust_limit, &secrets, offer[LOCAL]);
|
||||
} else {
|
||||
offer[REMOTE]
|
||||
= receive_offer(&cs, &channel_id, funding_pubkey,
|
||||
= receive_offer(&cs, gossip_index, &channel_id,
|
||||
funding_pubkey,
|
||||
funding_wscript,
|
||||
scriptpubkey, &funding_txid,
|
||||
funding_txout, funding_satoshi,
|
||||
|
|
|
@ -4,15 +4,15 @@
|
|||
#include <common/peer_failed.h>
|
||||
#include <common/status.h>
|
||||
#include <common/wire_error.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <unistd.h>
|
||||
#include <wire/gen_peer_wire.h>
|
||||
|
||||
/* We only support one channel per peer anyway */
|
||||
void peer_failed(int peer_fd, struct crypto_state *cs,
|
||||
const struct channel_id *channel_id,
|
||||
const char *fmt, ...)
|
||||
void peer_failed_(int peer_fd, int gossip_fd,
|
||||
struct crypto_state *cs, u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
const char *desc;
|
||||
|
|
|
@ -7,14 +7,19 @@
|
|||
struct channel_id;
|
||||
|
||||
/**
|
||||
* peer_failed - Try sending error to peer, but exit with status.
|
||||
* @peer_fd: file descriptor for peer.
|
||||
* peer_failed - Exit with error for peer.
|
||||
* @cs: the peer's current crypto state.
|
||||
* @gossip_index: the peer's current gossip_index.
|
||||
* @channel_id: channel with error, or NULL for all.
|
||||
* @fmt: format as per status_failed(STATUS_FAIL_PEER_BAD,
|
||||
* @fmt...: format as per status_failed(STATUS_FAIL_PEER_BAD)
|
||||
*/
|
||||
void peer_failed(int peer_fd, struct crypto_state *cs,
|
||||
const struct channel_id *channel_id,
|
||||
const char *fmt, ...)
|
||||
PRINTF_FMT(4,5) NORETURN;
|
||||
#define peer_failed(cs, gossip_index, channel_id, ...) \
|
||||
peer_failed_(PEER_FD, GOSSIP_FD, (cs), (gossip_index), (channel_id), \
|
||||
__VA_ARGS__)
|
||||
|
||||
void peer_failed_(int peer_fd, int gossip_fd,
|
||||
struct crypto_state *cs, u64 gossip_index,
|
||||
const struct channel_id *channel_id,
|
||||
const char *fmt, ...)
|
||||
PRINTF_FMT(6,7) NORETURN;
|
||||
#endif /* LIGHTNING_COMMON_PEER_FAILED_H */
|
||||
|
|
|
@ -180,7 +180,8 @@ static void check_config_bounds(struct state *state,
|
|||
* than 483.
|
||||
*/
|
||||
if (remoteconf->max_accepted_htlcs > 483)
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"max_accepted_htlcs %u too large",
|
||||
remoteconf->max_accepted_htlcs);
|
||||
}
|
||||
|
@ -316,7 +317,8 @@ static u8 *funder_channel(struct state *state,
|
|||
&theirs.delayed_payment,
|
||||
&theirs.htlc,
|
||||
&state->next_per_commit[REMOTE]))
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"Parsing accept_channel %s", tal_hex(msg, msg));
|
||||
|
||||
/* BOLT #2:
|
||||
|
@ -324,7 +326,8 @@ static u8 *funder_channel(struct state *state,
|
|||
* The `temporary_channel_id` MUST be the same as the
|
||||
* `temporary_channel_id` in the `open_channel` message. */
|
||||
if (!structeq(&id_in, &state->channel_id))
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"accept_channel ids don't match: sent %s got %s",
|
||||
type_to_string(msg, struct channel_id, &id_in),
|
||||
type_to_string(msg, struct channel_id,
|
||||
|
@ -374,7 +377,8 @@ static u8 *funder_channel(struct state *state,
|
|||
&their_funding_pubkey,
|
||||
LOCAL);
|
||||
if (!state->channel)
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"could not create channel with given config");
|
||||
|
||||
/* BOLT #2:
|
||||
|
@ -414,7 +418,8 @@ static u8 *funder_channel(struct state *state,
|
|||
msg = opening_read_peer_msg(state);
|
||||
|
||||
if (!fromwire_funding_signed(msg, NULL, &id_in, &sig))
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"Parsing funding_signed: %s", tal_hex(msg, msg));
|
||||
|
||||
/* BOLT #2:
|
||||
|
@ -429,7 +434,7 @@ static u8 *funder_channel(struct state *state,
|
|||
&state->funding_txid, state->funding_txout);
|
||||
|
||||
if (!structeq(&id_in, &state->channel_id))
|
||||
peer_failed(PEER_FD, &state->cs, &id_in,
|
||||
peer_failed(&state->cs, state->gossip_index, &id_in,
|
||||
"funding_signed ids don't match: expected %s got %s",
|
||||
type_to_string(msg, struct channel_id,
|
||||
&state->channel_id),
|
||||
|
@ -443,7 +448,8 @@ static u8 *funder_channel(struct state *state,
|
|||
&state->next_per_commit[LOCAL], LOCAL);
|
||||
|
||||
if (!check_tx_sig(tx, 0, NULL, wscript, &their_funding_pubkey, &sig)) {
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"Bad signature %s on tx %s using key %s",
|
||||
type_to_string(trc, secp256k1_ecdsa_signature,
|
||||
&sig),
|
||||
|
@ -517,7 +523,7 @@ static u8 *fundee_channel(struct state *state,
|
|||
&theirs.htlc,
|
||||
&state->next_per_commit[REMOTE],
|
||||
&channel_flags))
|
||||
peer_failed(PEER_FD, &state->cs, NULL,
|
||||
peer_failed(&state->cs, state->gossip_index, NULL,
|
||||
"Bad open_channel %s",
|
||||
tal_hex(peer_msg, peer_msg));
|
||||
|
||||
|
@ -540,7 +546,8 @@ static u8 *fundee_channel(struct state *state,
|
|||
* The receiving node ... MUST fail the channel if `funding-satoshis`
|
||||
* is greater than or equal to 2^24 */
|
||||
if (state->funding_satoshis > MAX_FUNDING_SATOSHI)
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"funding_satoshis %"PRIu64" too large",
|
||||
state->funding_satoshis);
|
||||
|
||||
|
@ -550,7 +557,8 @@ static u8 *fundee_channel(struct state *state,
|
|||
* greater than `funding_satoshis` * 1000.
|
||||
*/
|
||||
if (state->push_msat > state->funding_satoshis * 1000)
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"push_msat %"PRIu64
|
||||
" too large for funding_satoshis %"PRIu64,
|
||||
state->push_msat, state->funding_satoshis);
|
||||
|
@ -599,7 +607,8 @@ static u8 *fundee_channel(struct state *state,
|
|||
&state->funding_txid,
|
||||
&state->funding_txout,
|
||||
&theirsig))
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"Parsing funding_created");
|
||||
|
||||
/* BOLT #2:
|
||||
|
@ -607,7 +616,7 @@ static u8 *fundee_channel(struct state *state,
|
|||
* The sender MUST set `temporary_channel_id` the same as the
|
||||
* `temporary_channel_id` in the `open_channel` message. */
|
||||
if (!structeq(&id_in, &state->channel_id))
|
||||
peer_failed(PEER_FD, &state->cs, &id_in,
|
||||
peer_failed(&state->cs, state->gossip_index, &id_in,
|
||||
"funding_created ids don't match: sent %s got %s",
|
||||
type_to_string(msg, struct channel_id,
|
||||
&state->channel_id),
|
||||
|
@ -626,7 +635,8 @@ static u8 *fundee_channel(struct state *state,
|
|||
&their_funding_pubkey,
|
||||
REMOTE);
|
||||
if (!state->channel)
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"could not create channel with given config");
|
||||
|
||||
/* BOLT #2:
|
||||
|
@ -638,7 +648,8 @@ static u8 *fundee_channel(struct state *state,
|
|||
|
||||
if (!check_tx_sig(their_commit, 0, NULL, wscript, &their_funding_pubkey,
|
||||
&theirsig)) {
|
||||
peer_failed(PEER_FD, &state->cs, &state->channel_id,
|
||||
peer_failed(&state->cs, state->gossip_index,
|
||||
&state->channel_id,
|
||||
"Bad signature %s on tx %s using key %s",
|
||||
type_to_string(trc, secp256k1_ecdsa_signature,
|
||||
&theirsig),
|
||||
|
|
Loading…
Add table
Reference in a new issue