From f994a44827c85285cc645eb2777162e25892ba29 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 30 Jun 2016 09:08:11 +0930 Subject: [PATCH] daemon/peer: keep our own node connection information. Note that the base fee is in millisatoshi, the proportional fee is in microsatoshi per satoshi. ie. 1,000,000 means charge 1 satoshi for every satoshi carried. Signed-off-by: Rusty Russell --- daemon/lightningd.c | 36 +++++++++++++++++++++++++++++++++++- daemon/lightningd.h | 4 ++++ daemon/peer.c | 17 ++++++++++++++++- daemon/peer.h | 3 +++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/daemon/lightningd.c b/daemon/lightningd.c index 5bba5da82..bb0f4aa00 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -59,6 +59,22 @@ static char *opt_set_u32(const char *arg, u32 *u) return NULL; } +static char *opt_set_s32(const char *arg, s32 *u) +{ + char *endp; + long l; + + /* This is how the manpage says to do it. Yech. */ + errno = 0; + l = strtol(arg, &endp, 0); + if (*endp || !arg[0]) + return tal_fmt(NULL, "'%s' is not a number", arg); + *u = l; + if (errno || *u != l) + return tal_fmt(NULL, "'%s' is out of range", arg); + return NULL; +} + static void opt_show_u64(char buf[OPT_SHOW_LEN], const u64 *u) { snprintf(buf, OPT_SHOW_LEN, "%"PRIu64, *u); @@ -69,6 +85,11 @@ static void opt_show_u32(char buf[OPT_SHOW_LEN], const u32 *u) snprintf(buf, OPT_SHOW_LEN, "%"PRIu32, *u); } +static void opt_show_s32(char buf[OPT_SHOW_LEN], const s32 *u) +{ + snprintf(buf, OPT_SHOW_LEN, "%"PRIi32, *u); +} + static void config_register_opts(struct lightningd_state *dstate) { opt_register_arg("--locktime-blocks", opt_set_u32, opt_show_u32, @@ -107,6 +128,13 @@ static void config_register_opts(struct lightningd_state *dstate) opt_register_arg("--commit-time", opt_set_time, opt_show_time, &dstate->config.commit_time, "Time after changes before sending out COMMIT"); + opt_register_arg("--fee-base", opt_set_u32, opt_show_u32, + &dstate->config.fee_base, + "Millisatoshi minimum to charge for HTLC"); + opt_register_arg("--fee-per-satoshi", opt_set_s32, opt_show_s32, + &dstate->config.fee_per_satoshi, + "Microsatoshi fee for every satoshi in HTLC"); + } static void default_config(struct config *config) @@ -157,6 +185,11 @@ static void default_config(struct config *config) /* Send commit 10msec after receiving; almost immediately. */ config->commit_time = time_from_msec(10); + + /* Discourage dust payments */ + config->fee_base = 546000; + /* Take 0.001% */ + config->fee_per_satoshi = 10; } static void check_config(struct lightningd_state *dstate) @@ -294,7 +327,8 @@ int main(int argc, char *argv[]) /* Set up node ID and private key. */ secrets_init(dstate); - + new_node(dstate, &dstate->id); + /* Initialize block topology. */ setup_topology(dstate); diff --git a/daemon/lightningd.h b/daemon/lightningd.h index 09b86e26f..f88379c28 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -40,6 +40,10 @@ struct config { /* Minimum/maximum time for an expiring HTLC (blocks). */ u32 min_htlc_expiry, max_htlc_expiry; + + /* Fee rates. */ + u32 fee_base; + s32 fee_per_satoshi; /* How long between polling bitcoind. */ struct timerel poll_time; diff --git a/daemon/peer.c b/daemon/peer.c index d5694ac3b..82e749f85 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -110,8 +110,22 @@ void peer_open_complete(struct peer *peer, const char *problem) { if (problem) log_unusual(peer->log, "peer open failed: %s", problem); - else + else { + struct lightningd_state *dstate = peer->dstate; + struct node *n; + log_debug(peer->log, "peer open complete"); + assert(!peer->nc); + n = get_node(dstate, &peer->id); + if (!n) + n = new_node(dstate, &peer->id); + peer->nc = add_connection(dstate, + get_node(dstate, &dstate->id), n, + dstate->config.fee_base, + dstate->config.fee_per_satoshi, + dstate->config.min_htlc_expiry, + dstate->config.min_htlc_expiry); + } } static void set_peer_state(struct peer *peer, enum state newstate, @@ -824,6 +838,7 @@ static struct peer *new_peer(struct lightningd_state *dstate, peer->closing_onchain.resolved = NULL; peer->closing_onchain.ci = NULL; peer->commit_timer = NULL; + peer->nc = 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; diff --git a/daemon/peer.h b/daemon/peer.h index 11a9bd6df..de0e4171d 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -211,6 +211,9 @@ struct peer { /* 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;