Browse Source

daemon/lightningd: store more state in peer.

We need to keep more information about the peer for handing off to the
channel daemon.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
c0b4bb1387
  1. 4
      lightningd/opening/opening.c
  2. 2
      lightningd/opening/opening_control_wire.csv
  3. 78
      lightningd/peer_control.c
  4. 12
      lightningd/peer_control.h

4
lightningd/opening/opening.c

@ -651,7 +651,9 @@ static u8 *recv_channel(struct state *state,
&theirs.revocation, &theirs.revocation,
&theirs.payment, &theirs.payment,
&theirs.delayed_payment, &theirs.delayed_payment,
&state->next_per_commit[REMOTE]); &state->next_per_commit[REMOTE],
state->funding_satoshis,
state->push_msat);
} }
#ifndef TESTING #ifndef TESTING

2
lightningd/opening/opening_control_wire.csv

@ -61,6 +61,8 @@ opening_accept_finish_resp,277,revocation_basepoint,33
opening_accept_finish_resp,310,payment_basepoint,33 opening_accept_finish_resp,310,payment_basepoint,33
opening_accept_finish_resp,343,delayed_payment_basepoint,33 opening_accept_finish_resp,343,delayed_payment_basepoint,33
opening_accept_finish_resp,377,their_per_commit_point,33 opening_accept_finish_resp,377,their_per_commit_point,33
opening_accept_finish_resp,410,funding_satoshis,8
opening_accept_finish_resp,418,push_msat,8
# You're OK to exit. # You're OK to exit.
opening_exit_req,99 opening_exit_req,99

Can't render this file because it has a wrong number of fields in line 3.

78
lightningd/peer_control.c

@ -14,6 +14,7 @@
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
#include <lightningd/build_utxos.h> #include <lightningd/build_utxos.h>
#include <lightningd/channel.h>
#include <lightningd/funding_tx.h> #include <lightningd/funding_tx.h>
#include <lightningd/gossip/gen_gossip_control_wire.h> #include <lightningd/gossip/gen_gossip_control_wire.h>
#include <lightningd/gossip/gen_gossip_status_wire.h> #include <lightningd/gossip/gen_gossip_status_wire.h>
@ -64,6 +65,9 @@ static struct peer *new_peer(struct lightningd *ld,
peer->id = NULL; peer->id = NULL;
peer->fd = io_conn_fd(conn); peer->fd = io_conn_fd(conn);
peer->connect_cmd = cmd; peer->connect_cmd = cmd;
peer->funding_txid = NULL;
peer->seed = NULL;
/* Max 128k per peer. */ /* Max 128k per peer. */
peer->log_book = new_log_book(peer, 128*1024, peer->log_book = new_log_book(peer, 128*1024,
get_log_level(ld->dstate.log_book)); get_log_level(ld->dstate.log_book));
@ -530,8 +534,8 @@ static enum watch_result funding_depth_cb(struct peer *peer,
const char *txidstr = type_to_string(peer, struct sha256_double, txid); const char *txidstr = type_to_string(peer, struct sha256_double, txid);
log_debug(peer->log, "Funding tx %s depth %u of %u", log_debug(peer->log, "Funding tx %s depth %u of %u",
txidstr, depth, peer->ld->dstate.config.anchor_confirms); txidstr, depth, peer->our_config.minimum_depth);
if (depth >= peer->ld->dstate.config.anchor_confirms) { if (depth >= peer->our_config.minimum_depth) {
peer_set_condition(peer, "Funding tx reached depth %u", depth); peer_set_condition(peer, "Funding tx reached depth %u", depth);
/* FIXME! Start channel proper... */ /* FIXME! Start channel proper... */
return DELETE_WATCH; return DELETE_WATCH;
@ -609,8 +613,6 @@ static void opening_gen_funding(struct subdaemon *opening, const u8 *resp,
struct funding_channel *fc) struct funding_channel *fc)
{ {
u8 *msg; u8 *msg;
struct sha256_double txid;
u16 outnum;
struct pubkey changekey; struct pubkey changekey;
peer_set_condition(fc->peer, "Created funding transaction for channel"); peer_set_condition(fc->peer, "Created funding transaction for channel");
@ -628,14 +630,17 @@ static void opening_gen_funding(struct subdaemon *opening, const u8 *resp,
&changekey, fc->change_keyindex)) &changekey, fc->change_keyindex))
fatal("Error deriving change key %u", fc->change_keyindex); fatal("Error deriving change key %u", fc->change_keyindex);
fc->funding_tx = funding_tx(fc, &outnum, fc->utxomap, fc->satoshi, fc->funding_tx = funding_tx(fc, &fc->peer->funding_outnum,
fc->utxomap, fc->satoshi,
&fc->local_fundingkey, &fc->local_fundingkey,
&fc->remote_fundingkey, &fc->remote_fundingkey,
fc->change, &changekey, fc->change, &changekey,
fc->peer->ld->bip32_base); fc->peer->ld->bip32_base);
bitcoin_txid(fc->funding_tx, &txid); fc->peer->funding_txid = tal(fc->peer, struct sha256_double);
bitcoin_txid(fc->funding_tx, fc->peer->funding_txid);
msg = towire_opening_open_funding(fc, &txid, outnum); msg = towire_opening_open_funding(fc, fc->peer->funding_txid,
fc->peer->funding_outnum);
subdaemon_req(fc->peer->owner, take(msg), -1, &fc->peer->fd, subdaemon_req(fc->peer->owner, take(msg), -1, &fc->peer->fd,
opening_release_tx, fc); opening_release_tx, fc);
} }
@ -644,25 +649,25 @@ static void opening_accept_finish_response(struct subdaemon *opening,
const u8 *resp, const u8 *resp,
struct peer *peer) struct peer *peer)
{ {
u16 funding_txout;
struct channel_config their_config; struct channel_config their_config;
secp256k1_ecdsa_signature first_commit_sig; secp256k1_ecdsa_signature first_commit_sig;
struct crypto_state crypto_state; struct crypto_state crypto_state;
struct pubkey remote_fundingkey, revocation_basepoint, struct basepoints theirbase;
payment_basepoint, delayed_payment_basepoint, struct pubkey remote_fundingkey, their_per_commit_point;
their_per_commit_point;
log_debug(peer->log, "Got opening_accept_finish_response"); log_debug(peer->log, "Got opening_accept_finish_response");
if (!fromwire_opening_accept_finish_resp(resp, NULL, if (!fromwire_opening_accept_finish_resp(resp, NULL,
&funding_txout, &peer->funding_outnum,
&their_config, &their_config,
&first_commit_sig, &first_commit_sig,
&crypto_state, &crypto_state,
&remote_fundingkey, &remote_fundingkey,
&revocation_basepoint, &theirbase.revocation,
&payment_basepoint, &theirbase.payment,
&delayed_payment_basepoint, &theirbase.delayed_payment,
&their_per_commit_point)) { &their_per_commit_point,
&peer->funding_satoshi,
&peer->push_msat)) {
log_broken(peer->log, "bad OPENING_ACCEPT_FINISH_RESP %s", log_broken(peer->log, "bad OPENING_ACCEPT_FINISH_RESP %s",
tal_hex(resp, resp)); tal_hex(resp, resp));
tal_free(peer); tal_free(peer);
@ -675,9 +680,8 @@ static void opening_accept_finish_response(struct subdaemon *opening,
static void opening_accept_response(struct subdaemon *opening, const u8 *resp, static void opening_accept_response(struct subdaemon *opening, const u8 *resp,
struct peer *peer) struct peer *peer)
{ {
struct sha256_double funding_txid; peer->funding_txid = tal(peer, struct sha256_double);
if (!fromwire_opening_accept_resp(resp, NULL, peer->funding_txid)) {
if (!fromwire_opening_accept_resp(resp, NULL, &funding_txid)) {
log_broken(peer->log, "bad OPENING_ACCEPT_RESP %s", log_broken(peer->log, "bad OPENING_ACCEPT_RESP %s",
tal_hex(resp, resp)); tal_hex(resp, resp));
tal_free(peer); tal_free(peer);
@ -685,8 +689,9 @@ static void opening_accept_response(struct subdaemon *opening, const u8 *resp,
} }
log_debug(peer->log, "Watching funding tx %s", log_debug(peer->log, "Watching funding tx %s",
type_to_string(resp, struct sha256_double, &funding_txid)); type_to_string(resp, struct sha256_double,
watch_txid(peer, peer->ld->topology, peer, &funding_txid, peer->funding_txid));
watch_txid(peer, peer->ld->topology, peer, peer->funding_txid,
funding_depth_cb, NULL); funding_depth_cb, NULL);
/* Tell it we're watching. */ /* Tell it we're watching. */
@ -746,8 +751,6 @@ void peer_accept_open(struct peer *peer,
const struct crypto_state *cs, const u8 *from_peer) const struct crypto_state *cs, const u8 *from_peer)
{ {
struct lightningd *ld = peer->ld; struct lightningd *ld = peer->ld;
struct privkey seed;
struct channel_config ours;
u32 max_to_self_delay, max_minimum_depth; u32 max_to_self_delay, max_minimum_depth;
u64 min_effective_htlc_capacity_msat; u64 min_effective_htlc_capacity_msat;
u8 *msg; u8 *msg;
@ -779,15 +782,16 @@ void peer_accept_open(struct peer *peer,
/* We handed off peer fd */ /* We handed off peer fd */
peer->fd = -1; peer->fd = -1;
channel_config(ld, &ours, channel_config(ld, &peer->our_config,
&max_to_self_delay, &max_minimum_depth, &max_to_self_delay, &max_minimum_depth,
&min_effective_htlc_capacity_msat); &min_effective_htlc_capacity_msat);
derive_peer_seed(ld, &seed, peer->id); peer->seed = tal(peer, struct privkey);
msg = towire_opening_init(peer, &ours, derive_peer_seed(ld, peer->seed, peer->id);
msg = towire_opening_init(peer, &peer->our_config,
max_to_self_delay, max_to_self_delay,
min_effective_htlc_capacity_msat, min_effective_htlc_capacity_msat,
cs, &seed); cs, peer->seed);
subdaemon_req(peer->owner, take(msg), -1, NULL, NULL, NULL); subdaemon_req(peer->owner, take(msg), -1, NULL, NULL, NULL);
/* FIXME: Real feerates! */ /* FIXME: Real feerates! */
@ -809,8 +813,6 @@ static void gossip_peer_released(struct subdaemon *gossip,
struct funding_channel *fc) struct funding_channel *fc)
{ {
struct lightningd *ld = fc->peer->ld; struct lightningd *ld = fc->peer->ld;
struct privkey seed;
struct channel_config ours;
u32 max_to_self_delay, max_minimum_depth; u32 max_to_self_delay, max_minimum_depth;
u64 min_effective_htlc_capacity_msat; u64 min_effective_htlc_capacity_msat;
u64 id; u64 id;
@ -842,20 +844,26 @@ static void gossip_peer_released(struct subdaemon *gossip,
/* They took our fd. */ /* They took our fd. */
fc->peer->fd = -1; fc->peer->fd = -1;
channel_config(ld, &ours, channel_config(ld, &fc->peer->our_config,
&max_to_self_delay, &max_minimum_depth, &max_to_self_delay, &max_minimum_depth,
&min_effective_htlc_capacity_msat); &min_effective_htlc_capacity_msat);
derive_peer_seed(ld, &seed, fc->peer->id); fc->peer->seed = tal(fc->peer, struct privkey);
msg = towire_opening_init(fc, &ours, derive_peer_seed(ld, fc->peer->seed, fc->peer->id);
msg = towire_opening_init(fc, &fc->peer->our_config,
max_to_self_delay, max_to_self_delay,
min_effective_htlc_capacity_msat, min_effective_htlc_capacity_msat,
fc->cs, &seed); fc->cs, fc->peer->seed);
subdaemon_req(fc->peer->owner, take(msg), -1, NULL, NULL, NULL); fc->peer->funding_satoshi = fc->satoshi;
/* FIXME: Support push_msat? */ /* FIXME: Support push_msat? */
fc->peer->push_msat = 0;
subdaemon_req(fc->peer->owner, take(msg), -1, NULL, NULL, NULL);
/* FIXME: Real feerate! */ /* FIXME: Real feerate! */
msg = towire_opening_open(fc, fc->satoshi, 0, 15000, max_minimum_depth); msg = towire_opening_open(fc, fc->peer->funding_satoshi,
fc->peer->push_msat,
15000, max_minimum_depth);
subdaemon_req(fc->peer->owner, take(msg), -1, NULL, subdaemon_req(fc->peer->owner, take(msg), -1, NULL,
opening_gen_funding, fc); opening_gen_funding, fc);
} }

12
lightningd/peer_control.h

@ -3,6 +3,7 @@
#include "config.h" #include "config.h"
#include <ccan/compiler/compiler.h> #include <ccan/compiler/compiler.h>
#include <daemon/netaddr.h> #include <daemon/netaddr.h>
#include <lightningd/channel_config.h>
#include <stdbool.h> #include <stdbool.h>
struct crypto_state; struct crypto_state;
@ -40,6 +41,17 @@ struct peer {
/* Json command which made us connect (if any) */ /* Json command which made us connect (if any) */
struct command *connect_cmd; struct command *connect_cmd;
/* Our channel config. */
struct channel_config our_config;
/* Funding txid and amounts (once known) */
struct sha256_double *funding_txid;
u16 funding_outnum;
u64 funding_satoshi, push_msat;
/* Secret seed (FIXME: Move to hsm!) */
struct privkey *seed;
}; };
struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id); struct peer *peer_by_unique_id(struct lightningd *ld, u64 unique_id);

Loading…
Cancel
Save