diff --git a/daemon/Makefile b/daemon/Makefile index e5ec42420..563ccc26a 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -96,6 +96,7 @@ DAEMON_HEADERS := \ daemon/packets.h \ daemon/pay.h \ daemon/peer.h \ + daemon/peer_internal.h \ daemon/pseudorand.h \ daemon/routing.h \ daemon/secrets.h \ diff --git a/daemon/commit_tx.c b/daemon/commit_tx.c index 8231bbc5c..bdb3e0a51 100644 --- a/daemon/commit_tx.c +++ b/daemon/commit_tx.c @@ -10,6 +10,7 @@ #include "log.h" #include "overflows.h" #include "peer.h" +#include "peer_internal.h" #include "permute_tx.h" #include "remove_dust.h" #include "utils.h" diff --git a/daemon/cryptopkt.c b/daemon/cryptopkt.c index d60f597f2..c11a8fc8a 100644 --- a/daemon/cryptopkt.c +++ b/daemon/cryptopkt.c @@ -6,6 +6,7 @@ #include "log.h" #include "names.h" #include "peer.h" +#include "peer_internal.h" #include "protobuf_convert.h" #include "secrets.h" #include "utils.h" diff --git a/daemon/db.c b/daemon/db.c index b7bf4ef3a..45b4ae514 100644 --- a/daemon/db.c +++ b/daemon/db.c @@ -10,6 +10,7 @@ #include "names.h" #include "netaddr.h" #include "pay.h" +#include "peer_internal.h" #include "routing.h" #include "secrets.h" #include "utils.h" diff --git a/daemon/feechange.c b/daemon/feechange.c index afc1b589c..3024a3267 100644 --- a/daemon/feechange.c +++ b/daemon/feechange.c @@ -2,6 +2,7 @@ #include "feechange.h" #include "log.h" #include "peer.h" +#include "peer_internal.h" #include #include #include "gen_feechange_state_names.h" @@ -109,7 +110,7 @@ void feechange_changestate(struct peer *peer, enum feechange_state newstate, bool db_commit) { - log_debug(peer->log, "feechange: %s->%s", + peer_debug(peer, "feechange: %s->%s", feechange_state_name(f->state), feechange_state_name(newstate)); assert(f->state == oldstate); diff --git a/daemon/htlc.c b/daemon/htlc.c index 8402da123..db51dc42c 100644 --- a/daemon/htlc.c +++ b/daemon/htlc.c @@ -2,6 +2,7 @@ #include "htlc.h" #include "log.h" #include "peer.h" +#include "peer_internal.h" #include "type_to_string.h" #include #include @@ -12,7 +13,7 @@ void htlc_changestate(struct htlc *h, enum htlc_state newstate, bool db_commit) { - log_debug(h->peer->log, "htlc %"PRIu64": %s->%s", h->id, + peer_debug(h->peer, "htlc %"PRIu64": %s->%s", h->id, htlc_state_name(h->state), htlc_state_name(newstate)); assert(h->state == oldstate); diff --git a/daemon/irc_announce.c b/daemon/irc_announce.c index 9328d1098..4b6a55061 100644 --- a/daemon/irc_announce.c +++ b/daemon/irc_announce.c @@ -5,6 +5,7 @@ #include "daemon/lightningd.h" #include "daemon/log.h" #include "daemon/peer.h" +#include "daemon/peer_internal.h" #include "daemon/routing.h" #include "daemon/secrets.h" #include "daemon/timeout.h" diff --git a/daemon/output_to_htlc.c b/daemon/output_to_htlc.c index df2753b9a..c91d7fbe1 100644 --- a/daemon/output_to_htlc.c +++ b/daemon/output_to_htlc.c @@ -1,6 +1,7 @@ #include "commit_tx.h" #include "output_to_htlc.h" #include "peer.h" +#include "peer_internal.h" /* FIXME: Array makes this O(n^2). Use a hash table. */ struct wscript_by_wpkh { diff --git a/daemon/p2p_announce.c b/daemon/p2p_announce.c index 144250549..4b8dcca02 100644 --- a/daemon/p2p_announce.c +++ b/daemon/p2p_announce.c @@ -4,6 +4,7 @@ #include "daemon/p2p_announce.h" #include "daemon/packets.h" #include "daemon/peer.h" +#include "daemon/peer_internal.h" #include "daemon/routing.h" #include "daemon/secrets.h" #include "daemon/timeout.h" diff --git a/daemon/packets.c b/daemon/packets.c index 1c0995dff..5b8833713 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -11,6 +11,7 @@ #include "names.h" #include "packets.h" #include "peer.h" +#include "peer_internal.h" #include "protobuf_convert.h" #include "secrets.h" #include "state.h" diff --git a/daemon/pay.c b/daemon/pay.c index 2c926ebb7..a270d08c9 100644 --- a/daemon/pay.c +++ b/daemon/pay.c @@ -6,6 +6,7 @@ #include "log.h" #include "pay.h" #include "peer.h" +#include "peer_internal.h" #include "routing.h" #include "sphinx.h" #include diff --git a/daemon/peer.c b/daemon/peer.c index 38ceb5632..941246f58 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -17,6 +17,7 @@ #include "packets.h" #include "pay.h" #include "peer.h" +#include "peer_internal.h" #include "permute_tx.h" #include "protobuf_convert.h" #include "pseudorand.h" @@ -64,6 +65,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_debug(struct peer *peer, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + logv(peer->log, LOG_DBG, fmt, ap); + va_end(ap); +} + void peer_add_their_commit(struct peer *peer, const struct sha256_double *txid, u64 commit_num) { diff --git a/daemon/peer.h b/daemon/peer.h index 20f152740..9f12702a1 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -20,209 +20,9 @@ #include #include -struct anchor_input { - struct sha256_double txid; - unsigned int index; - /* Amount of input (satoshis), and output (satoshis) */ - u64 in_amount, out_amount; - /* Wallet entry to use to spend. */ - struct pubkey walletkey; -}; - -/* 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 { - /* Commit number (0 == from open) */ - u64 commit_num; - /* Revocation hash. */ - struct sha256 revocation_hash; - /* Commit tx & txid */ - struct bitcoin_tx *tx; - struct sha256_double txid; - /* Channel state for this tx. */ - struct channel_state *cstate; - /* Other side's signature for last commit tx (if known) */ - secp256k1_ecdsa_signature *sig; - /* Order which commit was sent (theirs) / revocation was sent (ours) */ - s64 order; -}; - -struct peer_visible_state { - /* Is this side funding the channel? */ - bool 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 (blocks) */ - struct rel_locktime locktime; - /* Minimum depth of anchor before channel usable. */ - unsigned int mindepth; - /* Commitment fee they're offering (satoshi). */ - u64 commit_fee_rate; - /* Revocation hash for next commit tx. */ - struct sha256 next_revocation_hash; - /* Commit txs: last one is current. */ - struct commit_info *commit; - - /* cstate to generate next commitment tx. */ - struct channel_state *staging_cstate; -}; - -struct peer { - /* dstate->peers list */ - struct list_node list; - - /* State in state machine. */ - enum state state; - - /* Network connection. */ - struct io_conn *conn; - - /* Are we connected now? (Crypto handshake completed). */ - bool connected; - - /* If we're doing an open, this is the command which triggered it */ - struct command *open_jsoncmd; - - /* If we're doing a commit, this is the command which triggered it */ - struct command *commit_jsoncmd; - - /* Global state. */ - struct lightningd_state *dstate; - - /* Their ID. */ - struct pubkey *id; - - /* Order counter for transmission of revocations/commitments. */ - s64 order_counter; - - /* Current received packet. */ - Pkt *inpkt; - - /* Queue of output packets. */ - Pkt **outpkt; - - /* Their commitments we have signed (which could appear on chain). */ - struct list_head their_commits; - - /* Number of commitment signatures we've received. */ - u64 their_commitsigs; - - /* Anchor tx output */ - struct { - struct sha256_double txid; - unsigned int index; - u64 satoshis; - u8 *witnessscript; - - /* Minimum possible depth for anchor */ - unsigned int min_depth; - - /* If we're creating anchor, this tells us where to source it */ - struct anchor_input *input; - - /* If we created it, we keep entire tx. */ - const struct bitcoin_tx *tx; - - /* Depth to trigger anchor if still opening, or -1. */ - int ok_depth; - - /* Did we create anchor? */ - bool ours; - } anchor; - - struct { - /* Their signature for our current commit sig. */ - secp256k1_ecdsa_signature theirsig; - /* The watch we have on a live commit tx. */ - struct txwatch *watch; - } cur_commit; - - /* Counter to make unique HTLC ids. */ - u64 htlc_id_counter; - - /* Mutual close info. */ - struct { - /* Our last suggested closing fee. */ - u64 our_fee; - /* If they've offered a signature, these are set: */ - secp256k1_ecdsa_signature *their_sig; - /* If their_sig is non-NULL, this is the fee. */ - u64 their_fee; - /* scriptPubKey we/they want for closing. */ - u8 *our_script, *their_script; - /* Last sent (in case we need to retransmit) */ - s64 shutdown_order, closing_order; - /* How many closing sigs have we receieved? */ - u32 sigs_in; - } closing; - - /* If we're closing on-chain */ - struct { - /* Everything (watches, resolved[], etc) tal'ed off this: - * The commit which spends the anchor tx. */ - const struct bitcoin_tx *tx; - struct sha256_double txid; - - /* If >= 0, indicates which txout is to us and to them. */ - int to_us_idx, to_them_idx; - /* Maps what txouts are HTLCs (NULL implies to_us/them_idx). */ - struct htlc **htlcs; - /* Witness scripts for each output (where appropriate) */ - const u8 **wscripts; - /* The tx which resolves each txout. */ - const struct bitcoin_tx **resolved; - } onchain; - - /* All HTLCs. */ - struct htlc_map htlcs; - - /* We only track one feechange per state: last one counts. */ - struct feechange *feechanges[FEECHANGE_STATE_INVALID]; - - /* Current ongoing packetflow */ - struct io_data *io_data; - - /* What happened. */ - struct log *log; - - /* Things we're watching for (see watches.c) */ - struct list_head watches; - - /* Timeout for collecting changes before sending commit. */ - struct oneshot *commit_timer; - - /* Private keys for dealing with this peer. */ - struct peer_secrets *secrets; - - /* Our route connection to peer: NULL until we are in normal mode. */ - struct node_connection *nc; - - /* For testing. */ - bool fake_close; - bool output_enabled; - - /* Stuff we have in common. */ - struct peer_visible_state local, remote; - - /* If we have sent a new commit tx, but not received their revocation */ - struct sha256 *their_prev_revocation_hash; - - /* this is where we will store their revocation preimages*/ - struct shachain their_preimages; - - /* High water mark for the staggered broadcast */ - u64 broadcast_index; -}; +struct log; +struct lightningd_state; +struct peer; /* Mapping for id -> network address. */ struct peer_address { @@ -233,6 +33,9 @@ struct peer_address { void setup_listeners(struct lightningd_state *dstate); +void peer_debug(struct peer *peer, const char *fmt, ...) + PRINTF_FMT(2,3); + struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id); struct peer *find_peer_by_pkhash(struct lightningd_state *dstate, const u8 *pkhash); diff --git a/daemon/peer_internal.h b/daemon/peer_internal.h new file mode 100644 index 000000000..8ba7934dc --- /dev/null +++ b/daemon/peer_internal.h @@ -0,0 +1,210 @@ +/* This header holds structure definitions for struct peer, which must + * not be exposed to ../lightningd/ */ +#ifndef LIGHTNING_DAEMON_PEER_INTERNAL_H +#define LIGHTNING_DAEMON_PEER_INTERNAL_H +#include "config.h" + +struct anchor_input { + struct sha256_double txid; + unsigned int index; + /* Amount of input (satoshis), and output (satoshis) */ + u64 in_amount, out_amount; + /* Wallet entry to use to spend. */ + struct pubkey walletkey; +}; + +/* 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 { + /* Commit number (0 == from open) */ + u64 commit_num; + /* Revocation hash. */ + struct sha256 revocation_hash; + /* Commit tx & txid */ + struct bitcoin_tx *tx; + struct sha256_double txid; + /* Channel state for this tx. */ + struct channel_state *cstate; + /* Other side's signature for last commit tx (if known) */ + secp256k1_ecdsa_signature *sig; + /* Order which commit was sent (theirs) / revocation was sent (ours) */ + s64 order; +}; + +struct peer_visible_state { + /* Is this side funding the channel? */ + bool 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 (blocks) */ + struct rel_locktime locktime; + /* Minimum depth of anchor before channel usable. */ + unsigned int mindepth; + /* Commitment fee they're offering (satoshi). */ + u64 commit_fee_rate; + /* Revocation hash for next commit tx. */ + struct sha256 next_revocation_hash; + /* Commit txs: last one is current. */ + struct commit_info *commit; + + /* cstate to generate next commitment tx. */ + struct channel_state *staging_cstate; +}; + +struct peer { + /* dstate->peers list */ + struct list_node list; + + /* State in state machine. */ + enum state state; + + /* Network connection. */ + struct io_conn *conn; + + /* Are we connected now? (Crypto handshake completed). */ + bool connected; + + /* If we're doing an open, this is the command which triggered it */ + struct command *open_jsoncmd; + + /* If we're doing a commit, this is the command which triggered it */ + struct command *commit_jsoncmd; + + /* Global state. */ + struct lightningd_state *dstate; + + /* Their ID. */ + struct pubkey *id; + + /* Order counter for transmission of revocations/commitments. */ + s64 order_counter; + + /* Current received packet. */ + Pkt *inpkt; + + /* Queue of output packets. */ + Pkt **outpkt; + + /* Their commitments we have signed (which could appear on chain). */ + struct list_head their_commits; + + /* Number of commitment signatures we've received. */ + u64 their_commitsigs; + + /* Anchor tx output */ + struct { + struct sha256_double txid; + unsigned int index; + u64 satoshis; + u8 *witnessscript; + + /* Minimum possible depth for anchor */ + unsigned int min_depth; + + /* If we're creating anchor, this tells us where to source it */ + struct anchor_input *input; + + /* If we created it, we keep entire tx. */ + const struct bitcoin_tx *tx; + + /* Depth to trigger anchor if still opening, or -1. */ + int ok_depth; + + /* Did we create anchor? */ + bool ours; + } anchor; + + struct { + /* Their signature for our current commit sig. */ + secp256k1_ecdsa_signature theirsig; + /* The watch we have on a live commit tx. */ + struct txwatch *watch; + } cur_commit; + + /* Counter to make unique HTLC ids. */ + u64 htlc_id_counter; + + /* Mutual close info. */ + struct { + /* Our last suggested closing fee. */ + u64 our_fee; + /* If they've offered a signature, these are set: */ + secp256k1_ecdsa_signature *their_sig; + /* If their_sig is non-NULL, this is the fee. */ + u64 their_fee; + /* scriptPubKey we/they want for closing. */ + u8 *our_script, *their_script; + /* Last sent (in case we need to retransmit) */ + s64 shutdown_order, closing_order; + /* How many closing sigs have we receieved? */ + u32 sigs_in; + } closing; + + /* If we're closing on-chain */ + struct { + /* Everything (watches, resolved[], etc) tal'ed off this: + * The commit which spends the anchor tx. */ + const struct bitcoin_tx *tx; + struct sha256_double txid; + + /* If >= 0, indicates which txout is to us and to them. */ + int to_us_idx, to_them_idx; + /* Maps what txouts are HTLCs (NULL implies to_us/them_idx). */ + struct htlc **htlcs; + /* Witness scripts for each output (where appropriate) */ + const u8 **wscripts; + /* The tx which resolves each txout. */ + const struct bitcoin_tx **resolved; + } onchain; + + /* All HTLCs. */ + struct htlc_map htlcs; + + /* We only track one feechange per state: last one counts. */ + struct feechange *feechanges[FEECHANGE_STATE_INVALID]; + + /* Current ongoing packetflow */ + struct io_data *io_data; + + /* What happened. */ + struct log *log; + + /* Things we're watching for (see watches.c) */ + struct list_head watches; + + /* Timeout for collecting changes before sending commit. */ + struct oneshot *commit_timer; + + /* Private keys for dealing with this peer. */ + struct peer_secrets *secrets; + + /* Our route connection to peer: NULL until we are in normal mode. */ + struct node_connection *nc; + + /* For testing. */ + bool fake_close; + bool output_enabled; + + /* Stuff we have in common. */ + struct peer_visible_state local, remote; + + /* If we have sent a new commit tx, but not received their revocation */ + struct sha256 *their_prev_revocation_hash; + + /* this is where we will store their revocation preimages*/ + struct shachain their_preimages; + + /* High water mark for the staggered broadcast */ + u64 broadcast_index; +}; +#endif /* LIGHTNING_DAEMON_PEER_INTERNAL_H */ diff --git a/daemon/secrets.c b/daemon/secrets.c index 4fd892952..c1f367a83 100644 --- a/daemon/secrets.c +++ b/daemon/secrets.c @@ -4,6 +4,7 @@ #include "lightningd.h" #include "log.h" #include "peer.h" +#include "peer_internal.h" #include "secrets.h" #include "utils.h" #include diff --git a/daemon/test/run-maxfee.c b/daemon/test/run-maxfee.c index d05bc8296..89eb519f8 100644 --- a/daemon/test/run-maxfee.c +++ b/daemon/test/run-maxfee.c @@ -16,6 +16,12 @@ void db_update_htlc_state(struct peer *peer UNNEEDED, const struct htlc *htlc UN void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) { fprintf(stderr, "log_ called!\n"); abort(); } +/* Generated stub for peer_debug */ +void peer_debug(struct peer *peer UNNEEDED, const char *fmt UNNEEDED, ...) + +{ fprintf(stderr, "peer_debug called!\n"); abort(); } +/* Could not find declaration for tal_hexstr */ +/* Could not find declaration for type_to_string_ */ /* AUTOGENERATED MOCKS END */ static void test_maxfee(size_t htlcs, u64 funds) diff --git a/daemon/watch.c b/daemon/watch.c index 0e7fcacda..838689957 100644 --- a/daemon/watch.c +++ b/daemon/watch.c @@ -167,7 +167,7 @@ void txwatch_fire(struct chain_topology *topo, if (txw && depth != txw->depth) { enum watch_result r; - log_debug(txw->peer->log, + peer_debug(txw->peer, "Got depth change %u for %02x%02x%02x...\n", txw->depth, txw->txid.sha.u.u8[0], @@ -195,7 +195,7 @@ void txowatch_fire(struct chain_topology *topo, enum watch_result r; bitcoin_txid(tx, &txid); - log_debug(txow->peer->log, + peer_debug(txow->peer, "Got UTXO spend for %02x%02x%02x:%u: %02x%02x%02x%02x...\n", txow->out.txid.sha.u.u8[0], txow->out.txid.sha.u.u8[1], diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index f536ed495..461ed6159 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -42,6 +42,12 @@ struct peer *find_peer(struct lightningd_state *dstate, const struct pubkey *id) FIXME_IMPLEMENT(); } +void peer_debug(struct peer *peer, const char *fmt, ...); +void peer_debug(struct peer *peer, const char *fmt, ...) +{ + FIXME_IMPLEMENT(); +} + size_t get_tx_depth(const struct chain_topology *topo, const struct sha256_double *txid) {