Skip to content

Commit

Permalink
peer: split and expose new_peer function.
Browse files Browse the repository at this point in the history
More of a pure allocator, for when we load peers from db.  Also moves
shachain_init out of secrets and into new_peer where it logically
belongs.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Aug 18, 2016
1 parent ab2fac3 commit 0bb183e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
83 changes: 46 additions & 37 deletions daemon/peer.c
Expand Up @@ -1941,26 +1941,19 @@ static bool peer_reconnected(struct peer *peer,
return true;
}

static struct peer *new_peer(struct lightningd_state *dstate,
struct io_conn *conn,
int addr_type, int addr_protocol,
enum state_input offer_anchor,
bool we_connected)
struct peer *new_peer(struct lightningd_state *dstate,
enum state state,
enum state_input offer_anchor)
{
struct peer *peer = tal(dstate, struct peer);

assert(offer_anchor == CMD_OPEN_WITH_ANCHOR
|| offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);

/* FIXME: Stop listening if too many peers? */
list_add(&dstate->peers, &peer->list);

peer->state = STATE_INIT;
peer->state = state;
peer->connected = false;
peer->id = NULL;
peer->dstate = dstate;
peer->addr.type = addr_type;
peer->addr.protocol = addr_protocol;
peer->io_data = NULL;
peer->secrets = NULL;
list_head_init(&peer->watches);
Expand All @@ -1985,47 +1978,66 @@ static struct peer *new_peer(struct lightningd_state *dstate,
peer->onchain.wscripts = NULL;
peer->commit_timer = NULL;
peer->nc = NULL;
peer->log = NULL;
peer->their_prev_revocation_hash = NULL;
/* Make it different from other node (to catch bugs!), but a
* round number for simple eyeballing. */
peer->htlc_id_counter = pseudorand(1ULL << 32) * 1000;

/* If we free peer, conn should be closed, but can't be freed
* immediately so don't make peer a parent. */
peer->conn = conn;
peer->conn = NULL;
peer->fake_close = false;
peer->output_enabled = true;
io_set_finish(conn, peer_disconnect, peer);

peer->local.offer_anchor = offer_anchor;
if (!blocks_to_rel_locktime(dstate->config.locktime_blocks,
&peer->local.locktime))
fatal("Could not convert locktime_blocks");
peer->local.mindepth = dstate->config.anchor_confirms;
peer->local.commit = peer->remote.commit = NULL;
peer->local.staging_cstate = peer->remote.staging_cstate = NULL;

htlc_map_init(&peer->htlcs);
shachain_init(&peer->their_preimages);

list_add(&dstate->peers, &peer->list);
tal_add_destructor(peer, destroy_peer);
return peer;
}

static bool peer_first_connected(struct peer *peer,
struct io_conn *conn,
int addr_type, int addr_protocol,
struct io_data *iod,
const struct pubkey *id,
bool we_connected)
{
peer->io_data = tal_steal(peer, iod);
peer->id = tal_dup(peer, struct pubkey, id);
/* FIXME: Make this dynamic! */
peer->local.commit_fee_rate
= get_feerate(peer->dstate)
* peer->dstate->config.commitment_fee_percent / 100;
peer->local.commit = peer->remote.commit = NULL;
peer->local.staging_cstate = peer->remote.staging_cstate = NULL;

peer->anchor.min_depth = get_block_height(peer->dstate);
htlc_map_init(&peer->htlcs);
/* Make it different from other node (to catch bugs!), but a
* round number for simple eyeballing. */
peer->htlc_id_counter = pseudorand(1ULL << 32) * 1000;

/* If we free peer, conn should be closed, but can't be freed
* immediately so don't make peer a parent. */
peer->conn = conn;
io_set_finish(conn, peer_disconnect, peer);

/* FIXME: Attach IO logging for this peer. */
tal_add_destructor(peer, destroy_peer);

peer->addr.type = addr_type;
peer->addr.protocol = addr_protocol;
peer->addr.addrlen = sizeof(peer->addr.saddr);
if (getpeername(io_conn_fd(conn), &peer->addr.saddr.s,
&peer->addr.addrlen) != 0) {
log_unusual(dstate->base_log,
log_unusual(peer->dstate->base_log,
"Could not get address for peer: %s",
strerror(errno));
return tal_free(peer);
}
peer->anchor.min_depth = get_block_height(peer->dstate);

peer->log = new_log(peer, dstate->log_record, "%s%s:%s:",
log_prefix(dstate->base_log),
peer->log = new_log(peer, peer->dstate->log_record, "%s%s:%s:",
log_prefix(peer->dstate->base_log),
we_connected ? "out" : "in",
netaddr_name(peer, &peer->addr));

Expand Down Expand Up @@ -2063,7 +2075,6 @@ struct htlc *peer_new_htlc(struct peer *peer,
{
struct htlc *h = tal(peer, struct htlc);
h->peer = peer;
assert(state == SENT_ADD_HTLC || state == RCVD_ADD_HTLC);
h->state = state;
h->id = id;
h->msatoshis = msatoshis;
Expand Down Expand Up @@ -2191,9 +2202,9 @@ static struct io_plan *crypto_on_out(struct io_conn *conn,
struct json_connecting *connect)
{
/* Initiator currently funds channel */
struct peer *peer = new_peer(dstate, conn, SOCK_STREAM, IPPROTO_TCP,
CMD_OPEN_WITH_ANCHOR, true);
if (!peer) {
struct peer *peer = new_peer(dstate, STATE_INIT, CMD_OPEN_WITH_ANCHOR);
if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
iod, id, true)) {
command_fail(connect->cmd, "Failed to make peer for %s:%s",
connect->name, connect->port);
return io_close(conn);
Expand Down Expand Up @@ -2243,13 +2254,11 @@ static struct io_plan *crypto_on_in(struct io_conn *conn,
}

/* Initiator currently funds channel */
peer = new_peer(dstate, conn, SOCK_STREAM, IPPROTO_TCP,
CMD_OPEN_WITHOUT_ANCHOR, false);
if (!peer)
peer = new_peer(dstate, STATE_INIT, CMD_OPEN_WITHOUT_ANCHOR);
if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
iod, id, false))
return io_close(conn);

peer->io_data = tal_steal(peer, iod);
peer->id = tal_dup(peer, struct pubkey, id);
return peer_crypto_on(conn, peer);
}

Expand Down
4 changes: 4 additions & 0 deletions daemon/peer.h
Expand Up @@ -232,6 +232,10 @@ void setup_listeners(struct lightningd_state *dstate, unsigned int portnum);

struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id);

struct peer *new_peer(struct lightningd_state *dstate,
enum state state,
enum state_input offer_anchor);

/* Populates very first peer->{local,remote}.commit->{tx,cstate} */
bool setup_first_commit(struct peer *peer);

Expand Down
1 change: 0 additions & 1 deletion daemon/secrets.c
Expand Up @@ -157,7 +157,6 @@ void peer_secrets_init(struct peer *peer)
new_keypair(peer->dstate, &peer->secrets->commit, &peer->local.commitkey);
new_keypair(peer->dstate, &peer->secrets->final, &peer->local.finalkey);
randombytes_buf(peer->secrets->revocation_seed.u.u8, sizeof(peer->secrets->revocation_seed.u.u8));
shachain_init(&peer->their_preimages);
}

void peer_get_revocation_preimage(const struct peer *peer, u64 index,
Expand Down

0 comments on commit 0bb183e

Please sign in to comment.