mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
lightningd: save num_commits_sent/received and num_revocations_received.
We need this for reestablishing a channel. (Note: this patch changes quite a bit in this series, but reshuffling was tedious). Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -775,6 +775,14 @@ int peer_sending_commitsig(struct peer *peer, const u8 *msg)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (commitnum != peer->num_commits_sent) {
|
||||
log_broken(peer->log,
|
||||
"channel_sending_commitsig: expected commitnum %"
|
||||
PRIu64" got %"PRIu64,
|
||||
peer->num_commits_sent, commitnum);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < tal_count(changed_htlcs); i++) {
|
||||
if (!changed_htlc(peer, changed_htlcs + i)) {
|
||||
log_broken(peer->log,
|
||||
@@ -783,6 +791,8 @@ int peer_sending_commitsig(struct peer *peer, const u8 *msg)
|
||||
}
|
||||
}
|
||||
|
||||
peer->num_commits_sent++;
|
||||
|
||||
/* Tell it we've got it, and to go ahead with commitment_signed. */
|
||||
subd_send_msg(peer->owner,
|
||||
take(towire_channel_sending_commitsig_reply(msg)));
|
||||
@@ -853,6 +863,23 @@ static bool peer_sending_revocation(struct peer *peer,
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Also used for opening's initial commitsig */
|
||||
bool peer_save_commitsig(struct peer *peer, u64 commitnum)
|
||||
{
|
||||
if (commitnum != peer->num_commits_received) {
|
||||
log_broken(peer->log,
|
||||
"channel_got_commitsig: expected commitnum %"PRIu64
|
||||
" got %"PRIu64,
|
||||
peer->num_commits_received, commitnum);
|
||||
return false;
|
||||
}
|
||||
|
||||
peer->num_commits_received++;
|
||||
|
||||
/* FIXME: Save to database, with sig and HTLCs. */
|
||||
return true;
|
||||
}
|
||||
|
||||
/* This also implies we're sending revocation */
|
||||
int peer_got_commitsig(struct peer *peer, const u8 *msg)
|
||||
{
|
||||
@@ -915,6 +942,9 @@ int peer_got_commitsig(struct peer *peer, const u8 *msg)
|
||||
if (!peer_sending_revocation(peer, added, fulfilled, failed, changed))
|
||||
return -1;
|
||||
|
||||
peer->channel_info->commit_sig = commit_sig;
|
||||
peer_save_commitsig(peer, commitnum);
|
||||
|
||||
/* Tell it we've committed, and to go ahead with revoke. */
|
||||
msg = towire_channel_got_commitsig_reply(msg);
|
||||
subd_send_msg(peer->owner, take(msg));
|
||||
@@ -932,18 +962,9 @@ static void update_per_commit_point(struct peer *peer,
|
||||
per_commitment_point);
|
||||
}
|
||||
|
||||
/* FIXME: add to ccan/shachain */
|
||||
static shachain_index_t shachain_next_index(const struct shachain *chain)
|
||||
{
|
||||
if (chain->num_valid == 0)
|
||||
return (shachain_index_t)(UINT64_MAX >> (64 - SHACHAIN_BITS));
|
||||
else
|
||||
return chain->min_index - 1;
|
||||
}
|
||||
|
||||
int peer_got_revoke(struct peer *peer, const u8 *msg)
|
||||
{
|
||||
u64 revokenum, shachainidx;
|
||||
u64 revokenum;
|
||||
struct sha256 per_commitment_secret;
|
||||
struct pubkey next_per_commitment_point;
|
||||
struct changed_htlc *changed;
|
||||
@@ -986,14 +1007,10 @@ int peer_got_revoke(struct peer *peer, const u8 *msg)
|
||||
return -1;
|
||||
}
|
||||
|
||||
shachainidx = shachain_index(revokenum);
|
||||
/* Channeld must feed us these in order. */
|
||||
if (shachainidx != shachain_next_index(&peer->their_shachain)) {
|
||||
log_broken(peer->log, "got_revoke: bad revoke number %"PRIu64
|
||||
" != %"PRIu64,
|
||||
revokenum,
|
||||
(u64)281474976710655
|
||||
- shachain_next_index(&peer->their_shachain));
|
||||
if (revokenum != peer->num_revocations_received) {
|
||||
log_broken(peer->log, "got_revoke: expected %"PRIu64
|
||||
" got %"PRIu64,
|
||||
peer->num_revocations_received, revokenum);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -1003,7 +1020,8 @@ int peer_got_revoke(struct peer *peer, const u8 *msg)
|
||||
* generated by the protocol in [BOLT #3]
|
||||
*/
|
||||
if (!shachain_add_hash(&peer->their_shachain,
|
||||
shachainidx, &per_commitment_secret)) {
|
||||
shachain_index(revokenum),
|
||||
&per_commitment_secret)) {
|
||||
peer_fail(peer, "Bad per_commitment_secret %s for %"PRIu64,
|
||||
type_to_string(msg, struct sha256,
|
||||
&per_commitment_secret),
|
||||
@@ -1013,6 +1031,7 @@ int peer_got_revoke(struct peer *peer, const u8 *msg)
|
||||
|
||||
/* FIXME: Check per_commitment_secret -> per_commit_point */
|
||||
update_per_commit_point(peer, &next_per_commitment_point);
|
||||
peer->num_revocations_received++;
|
||||
|
||||
/* FIXME: Commit shachain and next_per_commit_point to db */
|
||||
|
||||
|
||||
Reference in New Issue
Block a user