From 7c2165f5b41e9f564626cb9574737495aa296417 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 18 Aug 2016 14:23:45 +0930 Subject: [PATCH] peer: save txid -> commit_num mapping. This is in preparation for placing it in a database. Signed-off-by: Rusty Russell --- daemon/peer.c | 12 ++++++++++++ daemon/peer.h | 19 +++++++++++++++++++ state.c | 2 ++ 3 files changed, 33 insertions(+) diff --git a/daemon/peer.c b/daemon/peer.c index 02d97fa52..5ebede06c 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -56,6 +56,15 @@ static bool command_htlc_fail(struct peer *peer, struct htlc *htlc); static bool command_htlc_fulfill(struct peer *peer, struct htlc *htlc); static void try_commit(struct peer *peer); +void peer_add_their_commit(struct peer *peer, + const struct sha256_double *txid, u64 commit_num) +{ + struct their_commit *tc = tal(peer, struct their_commit); + tc->txid = *txid; + tc->commit_num = commit_num; + list_add_tail(&peer->their_commits, &tc->list); +} + /* Create a bitcoin close tx, using last signature they sent. */ static const struct bitcoin_tx *bitcoin_close(struct peer *peer) { @@ -1733,6 +1742,8 @@ static void do_commit(struct peer *peer, struct command *jsoncmd) /* Switch to the new commitment. */ peer->remote.commit = ci; + peer_add_their_commit(peer, &ci->txid, ci->commit_num); + queue_pkt_commit(peer); if (peer->state == STATE_CLEARING) { set_peer_state(peer, STATE_CLEARING_COMMITTING, __func__); @@ -1792,6 +1803,7 @@ static struct peer *new_peer(struct lightningd_state *dstate, peer->commit_jsoncmd = NULL; list_head_init(&peer->outgoing_txs); list_head_init(&peer->pay_commands); + list_head_init(&peer->their_commits); peer->anchor.ok_depth = -1; peer->cur_commit.watch = NULL; peer->closing.their_sig = NULL; diff --git a/daemon/peer.h b/daemon/peer.h index 2d6a72165..d2d2eeb37 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -55,6 +55,18 @@ struct anchor_input { struct wallet *w; }; +/* Information we remember for their commitment txs which we signed. + * + * Given the commit_num, we can use shachain to derive the revocation preimage + * (if we've received it yet: we might have not, for the last). + */ +struct their_commit { + struct list_node list; + + struct sha256_double txid; + u64 commit_num; +}; + struct commit_info { /* Previous one, if any. */ struct commit_info *prev; @@ -137,6 +149,9 @@ struct peer { /* Queue of output packets. */ Pkt **outpkt; + /* Their commitments we have signed (which could appear on chain). */ + struct list_head their_commits; + /* Anchor tx output */ struct { struct sha256_double txid; @@ -226,6 +241,10 @@ struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id) /* Populates very first peer->{local,remote}.commit->{tx,cstate} */ bool setup_first_commit(struct peer *peer); +/* Whenever we send a signature, remember the txid -> commit_num mapping */ +void peer_add_their_commit(struct peer *peer, + const struct sha256_double *txid, u64 commit_num); + /* Allocate a new commit_info struct. */ struct commit_info *new_commit_info(const tal_t *ctx); diff --git a/state.c b/state.c index 131407508..17d46826c 100644 --- a/state.c +++ b/state.c @@ -154,6 +154,8 @@ enum state state(struct peer *peer, peer->remote.commit->sig->stype = SIGHASH_ALL; peer_sign_theircommit(peer, peer->remote.commit->tx, &peer->remote.commit->sig->sig); + peer_add_their_commit(peer, &peer->remote.commit->txid, + peer->remote.commit->commit_num); queue_pkt_open_commit_sig(peer); peer_watch_anchor(peer,