peer: save txid -> commit_num mapping.

This is in preparation for placing it in a database.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-08-18 14:23:45 +09:30
parent 9b2fd3a969
commit 7c2165f5b4
3 changed files with 33 additions and 0 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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,