From b04392609a24825a6f386196b391c0d0c2f2e9cb Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 22 Jan 2016 06:41:49 +1030 Subject: [PATCH] daemon: encapsulate each side's state in a struct. Signed-off-by: Rusty Russell --- daemon/peer.c | 12 +++++++++--- daemon/peer.h | 23 +++++++++++++++++------ daemon/secrets.c | 4 ++-- daemon/watch.c | 4 ++-- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/daemon/peer.c b/daemon/peer.c index 4a3f75e55..3dd5ad528 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -64,6 +64,9 @@ static struct peer *new_peer(struct lightningd_state *state, { struct peer *peer = tal(state, struct peer); + assert(offer_anchor == CMD_OPEN_WITH_ANCHOR + || offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); + /* FIXME: Stop listening if too many peers? */ list_add(&state->peers, &peer->list); @@ -72,11 +75,14 @@ static struct peer *new_peer(struct lightningd_state *state, peer->addr.protocol = addr_protocol; peer->io_data = NULL; peer->secrets = NULL; - peer->offer_anchor = offer_anchor; - assert(offer_anchor == CMD_OPEN_WITH_ANCHOR - || offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); list_head_init(&peer->watches); + peer->us.offer_anchor = offer_anchor; + peer->us.locktime = state->config.rel_locktime; + peer->us.mindepth = state->config.anchor_confirms; + /* FIXME: Make this dynamic. */ + peer->us.commit_fee = state->config.commitment_fee; + /* FIXME: Attach IO logging for this peer. */ tal_add_destructor(peer, destroy_peer); diff --git a/daemon/peer.h b/daemon/peer.h index fed6129b8..013627912 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -7,6 +7,19 @@ #include "state_types.h" #include +struct peer_visible_state { + /* CMD_OPEN_WITH_ANCHOR or CMD_OPEN_WITHOUT_ANCHOR */ + enum state_input offer_anchor; + /* Key for commitment tx inputs, then key for commitment tx outputs */ + struct pubkey commitkey, finalkey; + /* How long to they want the other's outputs locked (seconds) */ + unsigned int locktime; + /* Minimum depth of anchor before channel usable. */ + unsigned int mindepth; + /* Commitment fee they're offering (satoshi). */ + u64 commit_fee; +}; + struct peer { /* state->peers list */ struct list_node list; @@ -32,13 +45,11 @@ struct peer { /* Things we're watching for (see watches.c) */ struct list_head watches; - /* Did we offer an anchor? */ - enum state_input offer_anchor; - - /* Keys for transactions with this peer. */ - struct pubkey their_commitkey, their_finalkey; - struct pubkey our_commitkey, our_finalkey; + /* Private keys for dealing with this peer. */ struct peer_secrets *secrets; + + /* Stuff we have in common. */ + struct peer_visible_state us, them; }; void setup_listeners(struct lightningd_state *state, unsigned int portnum); diff --git a/daemon/secrets.c b/daemon/secrets.c index 8eef34944..8b7d9a6ba 100644 --- a/daemon/secrets.c +++ b/daemon/secrets.c @@ -55,8 +55,8 @@ void peer_secrets_init(struct peer *peer) { peer->secrets = tal(peer, struct peer_secrets); - new_keypair(peer->state, &peer->secrets->commit, &peer->our_commitkey); - new_keypair(peer->state, &peer->secrets->final, &peer->our_finalkey); + new_keypair(peer->state, &peer->secrets->commit, &peer->us.commitkey); + new_keypair(peer->state, &peer->secrets->final, &peer->us.finalkey); if (RAND_bytes(peer->secrets->revocation_seed.u.u8, sizeof(peer->secrets->revocation_seed.u.u8)) != 1) fatal("Could not get random bytes for revocation seed"); diff --git a/daemon/watch.c b/daemon/watch.c index 8c231e521..9e0bd7406 100644 --- a/daemon/watch.c +++ b/daemon/watch.c @@ -144,8 +144,8 @@ void add_anchor_watch_(struct peer *peer, insert_txwatch(peer, peer->state, peer, txid, anchor_cb, cbdata); insert_txo_watch(peer, txid, out, spend_cb, cbdata); - redeemscript = bitcoin_redeem_2of2(peer, &peer->their_commitkey, - &peer->our_commitkey); + redeemscript = bitcoin_redeem_2of2(peer, &peer->them.commitkey, + &peer->us.commitkey); sha256(&h, redeemscript, tal_count(redeemscript)); ripemd160(&redeemhash, h.u.u8, sizeof(h)); tal_free(redeemscript);