From 15405f95e1f7e9867e08d51875eeac0310874a6f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 20 Jun 2017 15:48:03 +0930 Subject: [PATCH] lightningd: simply store commit index, rather than count sent/received. This is what channeld wants to know, so just do that. Signed-off-by: Rusty Russell --- lightningd/channel/channel.c | 26 +++++++---- lightningd/channel/channel_wire.csv | 4 +- lightningd/peer_control.c | 25 ++++++----- lightningd/peer_control.h | 3 +- lightningd/peer_htlcs.c | 68 ++++++++++++++--------------- lightningd/peer_htlcs.h | 3 -- 6 files changed, 68 insertions(+), 61 deletions(-) diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 21f8b4bf6..55ec0070e 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -1541,7 +1541,6 @@ static void init_channel(struct peer *peer) struct basepoints points[NUM_SIDES]; u64 funding_satoshi; u16 funding_txout; - u64 commits_sent, commits_received; u64 local_msatoshi; struct pubkey funding_pubkey[NUM_SIDES]; struct sha256_double funding_txid; @@ -1580,8 +1579,8 @@ static void init_channel(struct peer *peer) &peer->cltv_delta, &peer->last_was_revoke, &peer->last_sent_commit, - &commits_sent, - &commits_received, + &peer->commit_index[LOCAL], + &peer->commit_index[REMOTE], &peer->revocations_received, &peer->htlc_id, &htlcs, @@ -1598,13 +1597,22 @@ static void init_channel(struct peer *peer) status_failed(WIRE_CHANNEL_BAD_COMMAND, "Init: %s", tal_hex(msg, msg)); - /* First commit is used for opening. */ - assert(commits_sent > 0); - assert(commits_received > 0); + status_trace("init %s: remote_per_commit = %s, old_remote_per_commit = %s" + " commit_idx_local = %"PRIu64 + " commit_idx_remote = %"PRIu64 + " revocations_received = %"PRIu64, + am_funder ? "LOCAL" : "REMOTE", + type_to_string(trc, struct pubkey, + &peer->remote_per_commit), + type_to_string(trc, struct pubkey, + &peer->old_remote_per_commit), + peer->commit_index[LOCAL], peer->commit_index[REMOTE], + peer->revocations_received); - /* If we've sent 1, we're on index 1. */ - peer->commit_index[LOCAL] = commits_sent; - peer->commit_index[REMOTE] = commits_received; + /* First commit is used for opening: if we've sent 0, we're on + * index 1. */ + assert(peer->commit_index[LOCAL] > 0); + assert(peer->commit_index[REMOTE] > 0); /* channel_id is set from funding txout */ derive_channel_id(&peer->channel_id, &funding_txid, funding_txout); diff --git a/lightningd/channel/channel_wire.csv b/lightningd/channel/channel_wire.csv index 94a7b104e..11003b0ec 100644 --- a/lightningd/channel/channel_wire.csv +++ b/lightningd/channel/channel_wire.csv @@ -44,8 +44,8 @@ channel_init,,cltv_delta,u16 channel_init,,last_was_revoke,bool channel_init,,num_last_sent_commit,u16 channel_init,,last_sent_commit,num_last_sent_commit*struct changed_htlc -channel_init,,commits_sent,u64 -channel_init,,commits_received,u64 +channel_init,,commit_index_local,u64 +channel_init,,commit_index_remote,u64 channel_init,,revocations_received,u64 channel_init,,next_htlc_id,u64 channel_init,,num_htlcs,u16 diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index cc954fff1..0206e25fa 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -364,7 +364,8 @@ void add_peer(struct lightningd *ld, u64 unique_id, peer->channel_info = NULL; peer->last_was_revoke = false; peer->last_sent_commit = NULL; - peer->num_commits_sent = peer->num_commits_received + peer->commit_index[LOCAL] + = peer->commit_index[REMOTE] = peer->num_revocations_received = 0; peer->next_htlc_id = 0; shachain_init(&peer->their_shachain); @@ -1058,8 +1059,8 @@ static bool peer_start_channeld_hsmfd(struct subd *hsm, const u8 *resp, cfg->deadline_blocks, peer->last_was_revoke, peer->last_sent_commit, - peer->num_commits_sent, - peer->num_commits_received, + peer->commit_index[LOCAL], + peer->commit_index[REMOTE], peer->num_revocations_received, peer->next_htlc_id, htlcs, htlc_states, @@ -1110,6 +1111,14 @@ static void peer_start_channeld(struct peer *peer, enum peer_state oldstate, -1, 1, peer_start_channeld_hsmfd, peer); } +static bool peer_commit_initial(struct peer *peer) +{ + peer->commit_index[LOCAL] = peer->commit_index[REMOTE] = 1; + + /* FIXME: Db channel_info, etc. */ + return true; +} + static bool opening_funder_finished(struct subd *opening, const u8 *resp, const int *fds, struct funding_channel *fc) @@ -1181,10 +1190,8 @@ static bool opening_funder_finished(struct subd *opening, const u8 *resp, return false; } - /* We should have sent and received the first commitsig */ - if (!peer_save_commitsig_received(fc->peer, 0) - || !peer_save_commitsig_sent(fc->peer, 0)) { - peer_internal_error(fc->peer, "Saving commitsig failed"); + if (!peer_commit_initial(fc->peer)) { + peer_internal_error(fc->peer, "Initial peer to db failed"); return false; } @@ -1245,9 +1252,7 @@ static bool opening_fundee_finished(struct subd *opening, /* old_remote_per_commit not valid yet, copy valid one. */ channel_info->old_remote_per_commit = channel_info->remote_per_commit; - /* We should have sent and received the first commitsig */ - if (!peer_save_commitsig_received(peer, 0) - || !peer_save_commitsig_sent(peer, 0)) + if (!peer_commit_initial(peer)) return false; log_debug(peer->log, "Watching funding tx %s", diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index e8237164e..dae858310 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -67,7 +67,8 @@ struct peer { u32 minimum_depth; /* Tracking commitment transaction numbers. */ - u64 num_commits_sent, num_commits_received, num_revocations_received; + u64 commit_index[NUM_SIDES]; + u64 num_revocations_received; u64 next_htlc_id; /* Funding txid and amounts (once known) */ diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index a7c0fd2f5..ec89e722d 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -816,6 +816,38 @@ static bool changed_htlc(struct peer *peer, return update_in_htlc(peer, changed->id, changed->newstate); } +static bool peer_save_commitsig_received(struct peer *peer, u64 commitnum) +{ + if (commitnum != peer->commit_index[LOCAL]) { + peer_internal_error(peer, + "channel_got_commitsig: expected commitnum %"PRIu64 + " got %"PRIu64, + peer->commit_index[LOCAL], commitnum); + return false; + } + + peer->commit_index[LOCAL]++; + + /* FIXME: Save to database, with sig and HTLCs. */ + return true; +} + +static bool peer_save_commitsig_sent(struct peer *peer, u64 commitnum) +{ + if (commitnum != peer->commit_index[REMOTE]) { + peer_internal_error(peer, + "channel_sent_commitsig: expected commitnum %"PRIu64 + " got %"PRIu64, + peer->commit_index[REMOTE], commitnum); + return false; + } + + peer->commit_index[REMOTE]++; + + /* FIXME: Save to database, with sig and HTLCs. */ + return true; +} + int peer_sending_commitsig(struct peer *peer, const u8 *msg) { u64 commitnum; @@ -865,8 +897,6 @@ int peer_sending_commitsig(struct peer *peer, const u8 *msg) if (!peer_save_commitsig_sent(peer, commitnum)) return -1; - /* FIXME: save commit_sig and htlc_sigs */ - /* Last was commit. */ peer->last_was_revoke = false; tal_free(peer->last_sent_commit); @@ -943,40 +973,6 @@ static bool peer_sending_revocation(struct peer *peer, return true; } -/* Also used for opening's initial commitsig */ -bool peer_save_commitsig_received(struct peer *peer, u64 commitnum) -{ - if (commitnum != peer->num_commits_received) { - peer_internal_error(peer, - "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; -} - -/* Also used for opening's initial commitsig */ -bool peer_save_commitsig_sent(struct peer *peer, u64 commitnum) -{ - if (commitnum != peer->num_commits_sent) { - peer_internal_error(peer, - "channel_sent_commitsig: expected commitnum %"PRIu64 - " got %"PRIu64, - peer->num_commits_sent, commitnum); - return false; - } - - peer->num_commits_sent++; - - /* 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) { diff --git a/lightningd/peer_htlcs.h b/lightningd/peer_htlcs.h index 75f107f87..3fbd54ce6 100644 --- a/lightningd/peer_htlcs.h +++ b/lightningd/peer_htlcs.h @@ -24,9 +24,6 @@ void peer_htlcs(const tal_t *ctx, struct failed_htlc **failed_htlcs, enum side **failed_sides); -bool peer_save_commitsig_received(struct peer *peer, u64 commitnum); -bool peer_save_commitsig_sent(struct peer *peer, u64 commitnum); - int peer_sending_commitsig(struct peer *peer, const u8 *msg); int peer_got_commitsig(struct peer *peer, const u8 *msg); int peer_got_revoke(struct peer *peer, const u8 *msg);