2016-01-21 21:11:48 +01:00
|
|
|
#ifndef LIGHTNING_DAEMON_PEER_H
|
|
|
|
#define LIGHTNING_DAEMON_PEER_H
|
|
|
|
#include "config.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#include "bitcoin/locktime.h"
|
2016-04-12 05:37:04 +02:00
|
|
|
#include "bitcoin/privkey.h"
|
2016-01-21 21:11:49 +01:00
|
|
|
#include "bitcoin/pubkey.h"
|
2016-01-21 21:14:27 +01:00
|
|
|
#include "bitcoin/script.h"
|
|
|
|
#include "bitcoin/shadouble.h"
|
|
|
|
#include "funding.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "lightning.pb-c.h"
|
2016-01-21 21:11:48 +01:00
|
|
|
#include "netaddr.h"
|
2016-01-21 21:14:25 +01:00
|
|
|
#include "state.h"
|
2016-01-21 21:14:13 +01:00
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
#include <ccan/list/list.h>
|
2016-01-21 21:15:28 +01:00
|
|
|
#include <ccan/time/time.h>
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-15 07:37:31 +01:00
|
|
|
enum htlc_stage_type {
|
|
|
|
HTLC_ADD,
|
|
|
|
HTLC_FULFILL,
|
|
|
|
HTLC_FAIL
|
|
|
|
};
|
|
|
|
|
|
|
|
struct htlc_add {
|
|
|
|
enum htlc_stage_type add;
|
|
|
|
struct channel_htlc htlc;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct htlc_fulfill {
|
|
|
|
enum htlc_stage_type fulfill;
|
2016-03-31 08:43:20 +02:00
|
|
|
u64 id;
|
2016-03-15 07:37:31 +01:00
|
|
|
struct sha256 r;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct htlc_fail {
|
|
|
|
enum htlc_stage_type fail;
|
2016-03-31 08:43:20 +02:00
|
|
|
u64 id;
|
2016-03-15 07:37:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
union htlc_staging {
|
|
|
|
enum htlc_stage_type type;
|
|
|
|
struct htlc_add add;
|
|
|
|
struct htlc_fulfill fulfill;
|
|
|
|
struct htlc_fail fail;
|
|
|
|
};
|
|
|
|
|
2016-04-12 05:37:04 +02:00
|
|
|
struct anchor_input {
|
|
|
|
struct sha256_double txid;
|
|
|
|
unsigned int index;
|
|
|
|
/* Amount of input (satoshis) */
|
|
|
|
u64 amount;
|
|
|
|
/* Wallet entry to use to spend. */
|
|
|
|
struct wallet *w;
|
|
|
|
};
|
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
struct commit_info {
|
|
|
|
/* Previous one, if any. */
|
|
|
|
struct commit_info *prev;
|
|
|
|
/* Revocation hash. */
|
|
|
|
struct sha256 revocation_hash;
|
|
|
|
/* Commit tx. */
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
/* Channel state for this tx. */
|
|
|
|
struct channel_state *cstate;
|
2016-05-02 08:31:56 +02:00
|
|
|
/* Other side's signature for this commit tx. */
|
|
|
|
struct bitcoin_signature sig;
|
2016-05-02 08:35:56 +02:00
|
|
|
/* Map for permutation: see commit_tx.c */
|
|
|
|
int *map;
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Revocation preimage (if known). */
|
|
|
|
struct sha256 *revocation_preimage;
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
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) */
|
2016-01-21 21:14:13 +01:00
|
|
|
struct rel_locktime locktime;
|
2016-01-21 21:11:49 +01:00
|
|
|
/* Minimum depth of anchor before channel usable. */
|
|
|
|
unsigned int mindepth;
|
|
|
|
/* Commitment fee they're offering (satoshi). */
|
2016-03-24 02:42:43 +01:00
|
|
|
u64 commit_fee_rate;
|
2016-03-15 07:38:35 +01:00
|
|
|
/* Revocation hash for next commit tx. */
|
|
|
|
struct sha256 next_revocation_hash;
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Commit txs: last one is current. */
|
|
|
|
struct commit_info *commit;
|
|
|
|
/* cstate to generate next commitment tx. */
|
|
|
|
struct channel_state *staging_cstate;
|
2016-01-21 21:11:49 +01:00
|
|
|
};
|
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
struct htlc_progress {
|
2016-01-21 21:15:28 +01:00
|
|
|
/* The HTLC we're working on. */
|
2016-03-15 07:37:31 +01:00
|
|
|
union htlc_staging stage;
|
2016-01-21 21:15:27 +01:00
|
|
|
};
|
|
|
|
|
2016-03-30 08:27:41 +02:00
|
|
|
struct out_pkt {
|
|
|
|
Pkt *pkt;
|
|
|
|
void (*ack_cb)(struct peer *peer, void *arg);
|
|
|
|
void *ack_arg;
|
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
struct peer {
|
2016-01-21 21:11:49 +01:00
|
|
|
/* dstate->peers list */
|
2016-01-21 21:11:48 +01:00
|
|
|
struct list_node list;
|
|
|
|
|
2016-01-21 21:14:13 +01:00
|
|
|
/* State in state machine. */
|
|
|
|
enum state state;
|
|
|
|
|
|
|
|
/* Condition of communications */
|
|
|
|
enum state_peercond cond;
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
/* Network connection. */
|
|
|
|
struct io_conn *conn;
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* Current command (or INPUT_NONE) */
|
|
|
|
struct {
|
|
|
|
enum state_input cmd;
|
|
|
|
union input cmddata;
|
|
|
|
struct command *jsoncmd;
|
|
|
|
} curr_cmd;
|
2016-01-21 21:14:25 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* Pending commands. */
|
|
|
|
struct list_head pending_cmd;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Global state. */
|
2016-01-21 21:11:49 +01:00
|
|
|
struct lightningd_state *dstate;
|
2016-01-21 21:11:48 +01:00
|
|
|
|
|
|
|
/* The other end's address. */
|
|
|
|
struct netaddr addr;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Their ID. */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Current received packet. */
|
|
|
|
Pkt *inpkt;
|
2016-01-21 21:14:25 +01:00
|
|
|
|
|
|
|
/* Queue of output packets. */
|
2016-03-30 08:27:41 +02:00
|
|
|
struct out_pkt *outpkt;
|
2016-01-21 21:14:27 +01:00
|
|
|
|
|
|
|
/* Anchor tx output */
|
|
|
|
struct {
|
|
|
|
struct sha256_double txid;
|
|
|
|
unsigned int index;
|
|
|
|
u64 satoshis;
|
2016-04-24 12:25:35 +02:00
|
|
|
u8 *witnessscript;
|
2016-04-12 05:37:04 +02:00
|
|
|
|
|
|
|
/* If we're creating anchor, this tells us where to source it */
|
|
|
|
struct anchor_input *input;
|
|
|
|
|
2016-01-21 21:14:27 +01:00
|
|
|
/* If we created it, we keep entire tx. */
|
|
|
|
const struct bitcoin_tx *tx;
|
2016-01-21 21:15:28 +01:00
|
|
|
struct anchor_watch *watches;
|
2016-01-21 21:14:27 +01:00
|
|
|
} anchor;
|
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
struct {
|
|
|
|
/* Their signature for our current commit sig. */
|
|
|
|
struct bitcoin_signature theirsig;
|
|
|
|
/* The watch we have on a live commit tx. */
|
|
|
|
struct txwatch *watch;
|
|
|
|
} cur_commit;
|
2016-01-21 21:14:27 +01:00
|
|
|
|
2016-01-21 21:15:27 +01:00
|
|
|
/* Number of HTLC updates (== number of previous commit txs) */
|
2016-03-08 01:13:15 +01:00
|
|
|
u64 commit_tx_counter;
|
2016-01-21 21:15:27 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Counter to make unique HTLC ids. */
|
|
|
|
u64 htlc_id_counter;
|
|
|
|
|
2016-03-24 02:39:41 +01:00
|
|
|
struct {
|
|
|
|
/* Our last suggested closing fee. */
|
|
|
|
u64 our_fee;
|
|
|
|
/* If they've offered a signature, these are set: */
|
|
|
|
struct bitcoin_signature *their_sig;
|
|
|
|
/* If their_sig is non-NULL, this is the fee. */
|
|
|
|
u64 their_fee;
|
2016-04-24 12:31:52 +02:00
|
|
|
/* scriptPubKey we/they want for closing. */
|
|
|
|
u8 *our_script, *their_script;
|
2016-03-24 02:39:41 +01:00
|
|
|
} closing;
|
|
|
|
|
|
|
|
/* If not INPUT_NONE, send this when we have no more HTLCs. */
|
|
|
|
enum state_input cleared;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* Current ongoing packetflow */
|
|
|
|
struct io_data *io_data;
|
|
|
|
|
2016-01-21 21:11:48 +01:00
|
|
|
/* What happened. */
|
|
|
|
struct log *log;
|
2016-01-21 21:11:49 +01:00
|
|
|
|
|
|
|
/* Things we're watching for (see watches.c) */
|
|
|
|
struct list_head watches;
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
/* Timeout for close_watch. */
|
|
|
|
struct oneshot *close_watch_timeout;
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
/* Private keys for dealing with this peer. */
|
2016-01-21 21:11:48 +01:00
|
|
|
struct peer_secrets *secrets;
|
2016-01-21 21:11:49 +01:00
|
|
|
|
2016-05-02 08:34:56 +02:00
|
|
|
/* For testing. */
|
|
|
|
bool fake_close;
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
/* Stuff we have in common. */
|
|
|
|
struct peer_visible_state us, them;
|
2016-01-21 21:11:48 +01:00
|
|
|
};
|
|
|
|
|
2016-01-21 21:11:49 +01:00
|
|
|
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum);
|
2016-01-21 21:11:48 +01:00
|
|
|
|
2016-03-31 08:43:20 +02:00
|
|
|
/* Populates very first peer->{us,them}.commit->{tx,cstate} */
|
|
|
|
bool setup_first_commit(struct peer *peer);
|
2016-01-21 21:14:27 +01:00
|
|
|
|
2016-01-21 21:15:28 +01:00
|
|
|
void peer_add_htlc_expiry(struct peer *peer,
|
|
|
|
const struct abs_locktime *expiry);
|
|
|
|
|
2016-03-30 08:27:18 +02:00
|
|
|
struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee);
|
2016-01-21 21:15:28 +01:00
|
|
|
|
2016-03-24 02:42:43 +01:00
|
|
|
uint64_t commit_tx_fee(const struct bitcoin_tx *commit,
|
|
|
|
uint64_t anchor_satoshis);
|
2016-01-21 21:11:48 +01:00
|
|
|
#endif /* LIGHTNING_DAEMON_PEER_H */
|