Browse Source

daemon: encapsulate each side's state in a struct.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
b04392609a
  1. 12
      daemon/peer.c
  2. 23
      daemon/peer.h
  3. 4
      daemon/secrets.c
  4. 4
      daemon/watch.c

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

23
daemon/peer.h

@ -7,6 +7,19 @@
#include "state_types.h"
#include <ccan/list/list.h>
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);

4
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");

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

Loading…
Cancel
Save