Browse Source

daemon: rename 'state' to 'dstate' everywhere.

This is the daemon state, not the state machine state.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
fc49e3fd74
  1. 44
      daemon/bitcoind.c
  2. 14
      daemon/bitcoind.h
  3. 14
      daemon/cryptopkt.c
  4. 23
      daemon/dns.c
  5. 6
      daemon/dns.h
  6. 36
      daemon/jsonrpc.c
  7. 6
      daemon/jsonrpc.h
  8. 86
      daemon/lightningd.c
  9. 63
      daemon/peer.c
  10. 6
      daemon/peer.h
  11. 38
      daemon/secrets.c
  12. 2
      daemon/secrets.h
  13. 6
      daemon/timeout.c
  14. 2
      daemon/timeout.h
  15. 50
      daemon/watch.c
  16. 4
      daemon/watch.h

44
daemon/bitcoind.c

@ -33,7 +33,7 @@ static char **gather_args(const tal_t *ctx, const char *cmd, va_list ap)
}
struct bitcoin_cli {
struct lightningd_state *state;
struct lightningd_state *dstate;
int fd;
pid_t pid;
char **args;
@ -83,22 +83,22 @@ static void bcli_finished(struct io_conn *conn, struct bitcoin_cli *bcli)
bcli->args[0], bcli->args[1], WEXITSTATUS(status),
(int)bcli->output_bytes, bcli->output);
assert(bcli->state->bitcoind_in_progress);
bcli->state->bitcoind_in_progress--;
assert(bcli->dstate->bitcoind_in_progress);
bcli->dstate->bitcoind_in_progress--;
bcli->process(bcli);
}
static void
start_bitcoin_cli(struct lightningd_state *state,
start_bitcoin_cli(struct lightningd_state *dstate,
void (*process)(struct bitcoin_cli *),
void *cb, void *cb_arg,
char *cmd, ...)
{
va_list ap;
struct bitcoin_cli *bcli = tal(state, struct bitcoin_cli);
struct bitcoin_cli *bcli = tal(dstate, struct bitcoin_cli);
struct io_conn *conn;
bcli->state = state;
bcli->dstate = dstate;
bcli->process = process;
bcli->cb = cb;
bcli->cb_arg = cb_arg;
@ -110,9 +110,9 @@ start_bitcoin_cli(struct lightningd_state *state,
if (bcli->pid < 0)
fatal("%s exec failed: %s", bcli->args[0], strerror(errno));
conn = io_new_conn(state, bcli->fd, output_init, bcli);
conn = io_new_conn(dstate, bcli->fd, output_init, bcli);
tal_steal(conn, bcli);
state->bitcoind_in_progress++;
dstate->bitcoind_in_progress++;
io_set_finish(conn, bcli_finished, bcli);
}
@ -124,13 +124,13 @@ static void process_importaddress(struct bitcoin_cli *bcli)
(int)bcli->output_bytes, bcli->output);
}
void bitcoind_watch_addr(struct lightningd_state *state,
void bitcoind_watch_addr(struct lightningd_state *dstate,
const struct ripemd160 *redeemhash)
{
char *p2shaddr = p2sh_to_base58(state, state->config.testnet,
char *p2shaddr = p2sh_to_base58(dstate, dstate->config.testnet,
redeemhash);
start_bitcoin_cli(state, process_importaddress, NULL, NULL,
start_bitcoin_cli(dstate, process_importaddress, NULL, NULL,
"importaddress", p2shaddr, "", "false", NULL);
tal_free(p2shaddr);
}
@ -139,7 +139,7 @@ static void process_transactions(struct bitcoin_cli *bcli)
{
const jsmntok_t *tokens, *t, *end;
bool valid;
void (*cb)(struct lightningd_state *state,
void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid,
int confirmations) = bcli->cb;
@ -180,23 +180,23 @@ static void process_transactions(struct bitcoin_cli *bcli)
bcli->output + conftok->start);
/* FIXME: log txid */
log_debug(bcli->state->base_log,
log_debug(bcli->dstate->base_log,
"txid %02x%02x%02x%02x..., conf %li",
txid.sha.u.u8[0], txid.sha.u.u8[1],
txid.sha.u.u8[2], txid.sha.u.u8[3],
conf);
cb(bcli->state, &txid, conf);
cb(bcli->dstate, &txid, conf);
}
}
void bitcoind_poll_transactions(struct lightningd_state *state,
void (*cb)(struct lightningd_state *state,
void bitcoind_poll_transactions(struct lightningd_state *dstate,
void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid,
int confirmations))
{
/* FIXME: Iterate and detect duplicates. */
start_bitcoin_cli(state, process_transactions, cb, NULL,
start_bitcoin_cli(dstate, process_transactions, cb, NULL,
"listtransactions", "*", "100000", "0", "true",
NULL);
}
@ -204,20 +204,20 @@ void bitcoind_poll_transactions(struct lightningd_state *state,
static void process_rawtx(struct bitcoin_cli *bcli)
{
struct bitcoin_tx *tx;
void (*cb)(struct lightningd_state *state,
void (*cb)(struct lightningd_state *dstate,
const struct bitcoin_tx *tx, void *arg) = bcli->cb;
tx = bitcoin_tx_from_hex(bcli, bcli->output, bcli->output_bytes);
if (!tx)
fatal("Unknown txid (output %.*s)",
(int)bcli->output_bytes, (char *)bcli->output);
cb(bcli->state, tx, bcli->cb_arg);
cb(bcli->dstate, tx, bcli->cb_arg);
}
/* FIXME: Cache! */
void bitcoind_txid_lookup_(struct lightningd_state *state,
void bitcoind_txid_lookup_(struct lightningd_state *dstate,
const struct sha256_double *txid,
void (*cb)(struct lightningd_state *state,
void (*cb)(struct lightningd_state *dstate,
const struct bitcoin_tx *tx,
void *arg),
void *arg)
@ -226,6 +226,6 @@ void bitcoind_txid_lookup_(struct lightningd_state *state,
if (!bitcoin_txid_to_hex(txid, txidhex, sizeof(txidhex)))
fatal("Incorrect txid size");
start_bitcoin_cli(state, process_rawtx, cb, arg,
start_bitcoin_cli(dstate, process_rawtx, cb, arg,
"getrawtransaction", txidhex, NULL);
}

14
daemon/bitcoind.h

@ -8,22 +8,22 @@ struct lightningd_state;
struct ripemd160;
struct bitcoin_tx;
void bitcoind_watch_addr(struct lightningd_state *state,
void bitcoind_watch_addr(struct lightningd_state *dstate,
const struct ripemd160 *redeemhash);
void bitcoind_poll_transactions(struct lightningd_state *state,
void (*cb)(struct lightningd_state *state,
void bitcoind_poll_transactions(struct lightningd_state *dstate,
void (*cb)(struct lightningd_state *dstate,
const struct sha256_double *txid,
int confirmations));
void bitcoind_txid_lookup_(struct lightningd_state *state,
void bitcoind_txid_lookup_(struct lightningd_state *dstate,
const struct sha256_double *txid,
void (*cb)(struct lightningd_state *state,
void (*cb)(struct lightningd_state *dstate,
const struct bitcoin_tx *tx, void *),
void *arg);
#define bitcoind_txid_lookup(state, txid, cb, arg) \
bitcoind_txid_lookup_((state), (txid), \
#define bitcoind_txid_lookup(dstate, txid, cb, arg) \
bitcoind_txid_lookup_((dstate), (txid), \
typesafe_cb_preargs(struct io_plan *, void *, \
(cb), (arg), \
struct lightningd_state *, \

14
daemon/cryptopkt.c

@ -372,7 +372,7 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer)
return io_close(conn);
}
if (!proto_to_pubkey(peer->state->secpctx, auth->node_id, &peer->id)) {
if (!proto_to_pubkey(peer->dstate->secpctx, auth->node_id, &peer->id)) {
log_unusual(peer->log, "Invalid auth id");
return io_close(conn);
}
@ -381,7 +381,7 @@ static struct io_plan *check_proof(struct io_conn *conn, struct peer *peer)
sha256_double(&sha,
neg->our_sessionpubkey, sizeof(neg->our_sessionpubkey));
if (!check_signed_hash(peer->state->secpctx, &sha, &sig, &peer->id)) {
if (!check_signed_hash(peer->dstate->secpctx, &sha, &sig, &peer->id)) {
log_unusual(peer->log, "Bad auth signature");
return io_close(conn);
}
@ -429,7 +429,7 @@ static struct io_plan *keys_exchanged(struct io_conn *conn, struct peer *peer)
struct key_negotiate *neg = peer->io_data->neg;
Pkt *auth;
if (!pubkey_from_der(peer->state->secpctx,
if (!pubkey_from_der(peer->dstate->secpctx,
neg->their_sessionpubkey,
sizeof(neg->their_sessionpubkey),
&sessionkey)) {
@ -439,7 +439,7 @@ static struct io_plan *keys_exchanged(struct io_conn *conn, struct peer *peer)
}
/* Derive shared secret. */
if (!secp256k1_ecdh(peer->state->secpctx, shared_secret,
if (!secp256k1_ecdh(peer->dstate->secpctx, shared_secret,
&sessionkey.pubkey, neg->seckey)) {
log_unusual(peer->log, "Bad ECDH");
return io_close(conn);
@ -459,7 +459,7 @@ static struct io_plan *keys_exchanged(struct io_conn *conn, struct peer *peer)
sizeof(neg->their_sessionpubkey), &sig);
/* FIXME: Free auth afterwards. */
auth = authenticate_pkt(peer, &peer->state->id, &sig);
auth = authenticate_pkt(peer, &peer->dstate->id, &sig);
return peer_write_packet(conn, peer, auth, receive_proof);
}
@ -496,9 +496,9 @@ struct io_plan *peer_crypto_setup(struct io_conn *conn, struct peer *peer,
neg = peer->io_data->neg = tal(peer->io_data, struct key_negotiate);
neg->cb = cb;
gen_sessionkey(peer->state->secpctx, neg->seckey, &sessionkey);
gen_sessionkey(peer->dstate->secpctx, neg->seckey, &sessionkey);
secp256k1_ec_pubkey_serialize(peer->state->secpctx,
secp256k1_ec_pubkey_serialize(peer->dstate->secpctx,
neg->our_sessionpubkey, &outputlen,
&sessionkey,
SECP256K1_EC_COMPRESSED);

23
daemon/dns.c

@ -17,7 +17,7 @@
struct dns_async {
size_t use;
struct lightningd_state *state;
struct lightningd_state *dstate;
struct io_plan *(*init)(struct io_conn *, struct lightningd_state *,
void *);
void (*fail)(struct lightningd_state *, void *arg);
@ -71,7 +71,7 @@ static struct io_plan *connected(struct io_conn *conn, struct dns_async *d)
io_set_finish(conn, NULL, NULL);
/* Keep use count, so reap_child won't fail. */
return d->init(conn, d->state, d->arg);
return d->init(conn, d->dstate, d->arg);
}
static void try_connect_one(struct dns_async *d);
@ -107,7 +107,7 @@ static void try_connect_one(struct dns_async *d)
/* Now we can warn if it's overlength */
if (a->addrlen > sizeof(a->saddr)) {
log_broken(d->state->base_log,
log_broken(d->dstate->base_log,
"DNS lookup gave overlength address for %s"
" for family %u, len=%u",
d->name, a->saddr.s.sa_family, a->addrlen);
@ -115,7 +115,7 @@ static void try_connect_one(struct dns_async *d)
/* Might not even be able to create eg. IPv6 sockets */
fd = socket(a->saddr.s.sa_family, a->type, a->protocol);
if (fd >= 0) {
io_new_conn(d->state, fd, init_conn, d);
io_new_conn(d->dstate, fd, init_conn, d);
return;
}
}
@ -123,7 +123,7 @@ static void try_connect_one(struct dns_async *d)
/* We're out of things to try. Fail. */
if (--d->use == 0)
d->fail(d->state, d->arg);
d->fail(d->dstate, d->arg);
}
static struct io_plan *start_connecting(struct io_conn *conn,
@ -157,10 +157,10 @@ static void reap_child(struct io_conn *conn, struct dns_async *d)
waitpid(d->pid, NULL, 0);
/* Last user calls fail. */
if (--d->use == 0)
d->fail(d->state, d->arg);
d->fail(d->dstate, d->arg);
}
struct dns_async *dns_resolve_and_connect_(struct lightningd_state *state,
struct dns_async *dns_resolve_and_connect_(struct lightningd_state *dstate,
const char *name, const char *port,
struct io_plan *(*init)(struct io_conn *,
struct lightningd_state *,
@ -172,7 +172,7 @@ struct dns_async *dns_resolve_and_connect_(struct lightningd_state *state,
struct dns_async *d = tal(NULL, struct dns_async);
struct io_conn *conn;
d->state = state;
d->dstate = dstate;
d->init = init;
d->fail = fail;
d->arg = arg;
@ -180,7 +180,8 @@ struct dns_async *dns_resolve_and_connect_(struct lightningd_state *state,
/* First fork child to get addresses. */
if (pipe(pfds) != 0) {
log_unusual(state->base_log, "Creating pipes for dns lookup: %s",
log_unusual(dstate->base_log,
"Creating pipes for dns lookup: %s",
strerror(errno));
return NULL;
}
@ -189,7 +190,7 @@ struct dns_async *dns_resolve_and_connect_(struct lightningd_state *state,
d->pid = fork();
switch (d->pid) {
case -1:
log_unusual(state->base_log, "forking for dns lookup: %s",
log_unusual(dstate->base_log, "forking for dns lookup: %s",
strerror(errno));
close(pfds[0]);
close(pfds[1]);
@ -202,7 +203,7 @@ struct dns_async *dns_resolve_and_connect_(struct lightningd_state *state,
close(pfds[1]);
d->use = 1;
conn = io_new_conn(state, pfds[0], init_dns_conn, d);
conn = io_new_conn(dstate, pfds[0], init_dns_conn, d);
io_set_finish(conn, reap_child, d);
tal_steal(conn, d);
return d;

6
daemon/dns.h

@ -9,8 +9,8 @@
struct lightningd_state;
struct netaddr;
#define dns_resolve_and_connect(state, name, port, initfn, failfn, arg) \
dns_resolve_and_connect_((state), (name), (port), \
#define dns_resolve_and_connect(dstate, name, port, initfn, failfn, arg) \
dns_resolve_and_connect_((dstate), (name), (port), \
typesafe_cb_preargs(struct io_plan *, void *, \
(initfn), (arg), \
struct io_conn *, \
@ -19,7 +19,7 @@ struct netaddr;
struct lightningd_state *), \
(arg))
struct dns_async *dns_resolve_and_connect_(struct lightningd_state *state,
struct dns_async *dns_resolve_and_connect_(struct lightningd_state *dstate,
const char *name, const char *port,
struct io_plan *(*init)(struct io_conn *,
struct lightningd_state *,

36
daemon/jsonrpc.c

@ -150,7 +150,7 @@ static void json_getlog(struct command *cmd,
const char *buffer, const jsmntok_t *params)
{
struct log_info info;
struct log_record *lr = cmd->state->log_record;
struct log_record *lr = cmd->dstate->log_record;
jsmntok_t *level;
json_get_params(buffer, params, "level", &level, NULL);
@ -254,7 +254,7 @@ void command_success(struct command *cmd, struct json_result *result)
struct json_connection *jcon = cmd->jcon;
if (!jcon) {
log_unusual(cmd->state->base_log,
log_unusual(cmd->dstate->base_log,
"Command returned result after jcon close");
tal_free(cmd);
return;
@ -271,7 +271,7 @@ void command_fail(struct command *cmd, const char *fmt, ...)
va_list ap;
if (!jcon) {
log_unusual(cmd->state->base_log,
log_unusual(cmd->dstate->base_log,
"Command failed after jcon close");
tal_free(cmd);
return;
@ -328,9 +328,9 @@ static void parse_request(struct json_connection *jcon, const jsmntok_t tok[])
/* This is a convenient tal parent for durarion of command
* (which may outlive the conn!). */
jcon->current = tal(jcon->state, struct command);
jcon->current = tal(jcon->dstate, struct command);
jcon->current->jcon = jcon;
jcon->current->state = jcon->state;
jcon->current->dstate = jcon->dstate;
jcon->current->id = tal_strndup(jcon->current,
json_tok_contents(jcon->buffer, id),
json_tok_len(id));
@ -373,7 +373,7 @@ static struct io_plan *write_json(struct io_conn *conn,
if (jcon->stop) {
log_unusual(jcon->log, "JSON-RPC shutdown");
/* Return us to toplevel lightningd.c */
io_break(jcon->state);
io_break(jcon->dstate);
return io_close(conn);
}
@ -407,7 +407,7 @@ again:
toks = json_parse_input(jcon->buffer, jcon->used, &valid);
if (!toks) {
if (!valid) {
log_unusual(jcon->state->base_log,
log_unusual(jcon->dstate->base_log,
"Invalid token in json input: '%.*s'",
(int)jcon->used, jcon->buffer);
return io_close(conn);
@ -447,18 +447,18 @@ read_more:
}
static struct io_plan *jcon_connected(struct io_conn *conn,
struct lightningd_state *state)
struct lightningd_state *dstate)
{
struct json_connection *jcon;
jcon = tal(state, struct json_connection);
jcon->state = state;
jcon = tal(dstate, struct json_connection);
jcon->dstate = dstate;
jcon->used = 0;
jcon->buffer = tal_arr(jcon, char, 64);
jcon->stop = false;
jcon->current = NULL;
jcon->log = new_log(jcon, state->log_record, "%sjcon fd %i:",
log_prefix(state->base_log), io_conn_fd(conn));
jcon->log = new_log(jcon, dstate->log_record, "%sjcon fd %i:",
log_prefix(dstate->base_log), io_conn_fd(conn));
list_head_init(&jcon->output);
io_set_finish(conn, finish_jcon, jcon);
@ -471,13 +471,13 @@ static struct io_plan *jcon_connected(struct io_conn *conn,
}
static struct io_plan *incoming_jcon_connected(struct io_conn *conn,
struct lightningd_state *state)
struct lightningd_state *dstate)
{
log_info(state->base_log, "Connected json input");
return jcon_connected(conn, state);
log_info(dstate->base_log, "Connected json input");
return jcon_connected(conn, dstate);
}
void setup_jsonrpc(struct lightningd_state *state, const char *rpc_filename)
void setup_jsonrpc(struct lightningd_state *dstate, const char *rpc_filename)
{
struct sockaddr_un addr;
int fd, old_umask;
@ -489,7 +489,7 @@ void setup_jsonrpc(struct lightningd_state *state, const char *rpc_filename)
fd = open(rpc_filename, O_RDWR);
if (fd == -1)
err(1, "Opening %s", rpc_filename);
io_new_conn(state, fd, jcon_connected, state);
io_new_conn(dstate, fd, jcon_connected, dstate);
return;
}
@ -513,5 +513,5 @@ void setup_jsonrpc(struct lightningd_state *state, const char *rpc_filename)
if (listen(fd, 1) != 0)
err(1, "Listening on '%s'", rpc_filename);
io_new_listener(state, fd, incoming_jcon_connected, state);
io_new_listener(dstate, fd, incoming_jcon_connected, dstate);
}

6
daemon/jsonrpc.h

@ -8,7 +8,7 @@
* You can allocate off this for temporary objects. */
struct command {
/* The global state */
struct lightningd_state *state;
struct lightningd_state *dstate;
/* The 'id' which we need to include in the response. */
const char *id;
/* The connection, or NULL if it closed. */
@ -17,7 +17,7 @@ struct command {
struct json_connection {
/* The global state */
struct lightningd_state *state;
struct lightningd_state *dstate;
/* Logging for this json connection. */
struct log *log;
@ -53,7 +53,7 @@ void command_success(struct command *cmd, struct json_result *response);
void PRINTF_FMT(2, 3) command_fail(struct command *cmd, const char *fmt, ...);
/* For initialization */
void setup_jsonrpc(struct lightningd_state *state, const char *rpc_filename);
void setup_jsonrpc(struct lightningd_state *dstate, const char *rpc_filename);
/* Commands (from other files) */
extern const struct json_command connect_command;

86
daemon/lightningd.c

@ -63,25 +63,25 @@ static void opt_show_u32(char buf[OPT_SHOW_LEN], const u32 *u)
snprintf(buf, OPT_SHOW_LEN, "%"PRIu32, *u);
}
static void config_register_opts(struct lightningd_state *state)
static void config_register_opts(struct lightningd_state *dstate)
{
opt_register_arg("--locktime", opt_set_u32, opt_show_u32,
&state->config.rel_locktime,
&dstate->config.rel_locktime,
"Seconds before peer can unilaterally spend funds");
opt_register_arg("--max-locktime", opt_set_u32, opt_show_u32,
&state->config.rel_locktime_max,
&dstate->config.rel_locktime_max,
"Maximum seconds peer can lock up our funds");
opt_register_arg("--anchor-confirms", opt_set_u32, opt_show_u32,
&state->config.anchor_confirms,
&dstate->config.anchor_confirms,
"Confirmations required for anchor transaction");
opt_register_arg("--max-anchor-confirms", opt_set_u32, opt_show_u32,
&state->config.anchor_confirms_max,
&dstate->config.anchor_confirms_max,
"Maximum confirmations other side can wait for anchor transaction");
opt_register_arg("--commit-fee", opt_set_u64, opt_show_u64,
&state->config.commitment_fee,
&dstate->config.commitment_fee,
"Satoshis to offer for commitment transaction fee");
opt_register_arg("--min-commit-fee", opt_set_u64, opt_show_u64,
&state->config.commitment_fee_min,
&dstate->config.commitment_fee_min,
"Minimum satoshis to accept for commitment transaction fee");
}
@ -113,21 +113,21 @@ static void default_config(struct config *config)
static struct lightningd_state *lightningd_state(void)
{
struct lightningd_state *state = tal(NULL, struct lightningd_state);
state->log_record = new_log_record(state, 20 * 1024 * 1024, LOG_INFORM);
state->base_log = new_log(state, state->log_record,
"lightningd(%u):", (int)getpid());
list_head_init(&state->peers);
timers_init(&state->timers, time_now());
txwatch_hash_init(&state->txwatches);
txowatch_hash_init(&state->txowatches);
state->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
default_config(&state->config);
state->bitcoind_in_progress = 0;
return state;
struct lightningd_state *dstate = tal(NULL, struct lightningd_state);
dstate->log_record = new_log_record(dstate, 20*1024*1024, LOG_INFORM);
dstate->base_log = new_log(dstate, dstate->log_record,
"lightningd(%u):", (int)getpid());
list_head_init(&dstate->peers);
timers_init(&dstate->timers, time_now());
txwatch_hash_init(&dstate->txwatches);
txowatch_hash_init(&dstate->txowatches);
dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
default_config(&dstate->config);
dstate->bitcoind_in_progress = 0;
return dstate;
}
/* Tal wrappers for opt. */
@ -151,7 +151,7 @@ static void tal_freefn(void *ptr)
int main(int argc, char *argv[])
{
struct lightningd_state *state = lightningd_state();
struct lightningd_state *dstate = lightningd_state();
struct timer *expired;
unsigned int portnum = 0;
@ -164,61 +164,61 @@ int main(int argc, char *argv[])
"Print this message.");
opt_register_arg("--port", opt_set_uintval, NULL, &portnum,
"Port to bind to (otherwise, dynamic port is used)");
opt_register_logging(state->base_log);
opt_register_logging(dstate->base_log);
opt_register_version();
configdir_register_opts(state,
&state->config_dir, &state->rpc_filename);
config_register_opts(state);
configdir_register_opts(dstate,
&dstate->config_dir, &dstate->rpc_filename);
config_register_opts(dstate);
/* Get any configdir options first. */
opt_early_parse(argc, argv, opt_log_stderr_exit);
/* Move to config dir, to save ourselves the hassle of path manip. */
if (chdir(state->config_dir) != 0) {
log_unusual(state->base_log, "Creating lightningd dir %s"
if (chdir(dstate->config_dir) != 0) {
log_unusual(dstate->base_log, "Creating lightningd dir %s"
" (because chdir gave %s)",
state->config_dir, strerror(errno));
if (mkdir(state->config_dir, 0700) != 0)
dstate->config_dir, strerror(errno));
if (mkdir(dstate->config_dir, 0700) != 0)
fatal("Could not make directory %s: %s",
state->config_dir, strerror(errno));
if (chdir(state->config_dir) != 0)
dstate->config_dir, strerror(errno));
if (chdir(dstate->config_dir) != 0)
fatal("Could not change directory %s: %s",
state->config_dir, strerror(errno));
dstate->config_dir, strerror(errno));
}
/* Activate crash log now we're in the right place. */
crashlog_activate(state->base_log);
crashlog_activate(dstate->base_log);
/* Now look for config file */
opt_parse_from_config(state);
opt_parse_from_config(dstate);
opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc != 1)
errx(1, "no arguments accepted");
/* Create RPC socket (if any) */
setup_jsonrpc(state, state->rpc_filename);
setup_jsonrpc(dstate, dstate->rpc_filename);
/* Set up connections from peers. */
setup_listeners(state, portnum);
setup_listeners(dstate, portnum);
/* Set up node ID and private key. */
secrets_init(state);
secrets_init(dstate);
/* Create timer to do watches. */
setup_watch_timer(state);
setup_watch_timer(dstate);
log_info(state->base_log, "Hello world!");
log_info(dstate->base_log, "Hello world!");
/* If io_loop returns NULL, either a timer expired, or all fds closed */
while (!io_loop(&state->timers, &expired) && expired) {
while (!io_loop(&dstate->timers, &expired) && expired) {
struct timeout *to;
to = container_of(expired, struct timeout, timer);
to->cb(to->arg);
}
tal_free(state);
tal_free(dstate);
opt_free_table();
return 0;
}

63
daemon/peer.c

@ -53,24 +53,24 @@ static struct io_plan *peer_test(struct io_conn *conn, struct peer *peer)
static void destroy_peer(struct peer *peer)
{
list_del_from(&peer->state->peers, &peer->list);
list_del_from(&peer->dstate->peers, &peer->list);
}
static struct peer *new_peer(struct lightningd_state *state,
static struct peer *new_peer(struct lightningd_state *dstate,
struct io_conn *conn,
int addr_type, int addr_protocol,
enum state_input offer_anchor,
const char *in_or_out)
{
struct peer *peer = tal(state, struct peer);
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(&state->peers, &peer->list);
list_add(&dstate->peers, &peer->list);
peer->state = state;
peer->dstate = dstate;
peer->addr.type = addr_type;
peer->addr.protocol = addr_protocol;
peer->io_data = NULL;
@ -78,10 +78,10 @@ static struct peer *new_peer(struct lightningd_state *state,
list_head_init(&peer->watches);
peer->us.offer_anchor = offer_anchor;
peer->us.locktime = state->config.rel_locktime;
peer->us.mindepth = state->config.anchor_confirms;
peer->us.locktime = dstate->config.rel_locktime;
peer->us.mindepth = dstate->config.anchor_confirms;
/* FIXME: Make this dynamic. */
peer->us.commit_fee = state->config.commitment_fee;
peer->us.commit_fee = dstate->config.commitment_fee;
/* FIXME: Attach IO logging for this peer. */
tal_add_destructor(peer, destroy_peer);
@ -89,24 +89,24 @@ static struct peer *new_peer(struct lightningd_state *state,
peer->addr.addrlen = sizeof(peer->addr.saddr);
if (getpeername(io_conn_fd(conn), &peer->addr.saddr.s,
&peer->addr.addrlen) != 0) {
log_unusual(state->base_log,
log_unusual(dstate->base_log,
"Could not get address for peer: %s",
strerror(errno));
return tal_free(peer);
}
peer->log = new_log(peer, state->log_record, "%s%s:%s:",
log_prefix(state->base_log), in_or_out,
peer->log = new_log(peer, dstate->log_record, "%s%s:%s:",
log_prefix(dstate->base_log), in_or_out,
netaddr_name(peer, &peer->addr));
return peer;
}
static struct io_plan *peer_connected_out(struct io_conn *conn,
struct lightningd_state *state,
struct lightningd_state *dstate,
struct json_connecting *connect)
{
struct json_result *response;
struct peer *peer = new_peer(state, conn, SOCK_STREAM, IPPROTO_TCP,
struct peer *peer = new_peer(dstate, conn, SOCK_STREAM, IPPROTO_TCP,
CMD_OPEN_WITH_ANCHOR, "out");
if (!peer) {
command_fail(connect->cmd, "Failed to make peer for %s:%s",
@ -125,9 +125,9 @@ static struct io_plan *peer_connected_out(struct io_conn *conn,
}
static struct io_plan *peer_connected_in(struct io_conn *conn,
struct lightningd_state *state)
struct lightningd_state *dstate)
{
struct peer *peer = new_peer(state, conn, SOCK_STREAM, IPPROTO_TCP,
struct peer *peer = new_peer(dstate, conn, SOCK_STREAM, IPPROTO_TCP,
CMD_OPEN_WITHOUT_ANCHOR, "in");
if (!peer)
return io_close(conn);
@ -136,12 +136,12 @@ static struct io_plan *peer_connected_in(struct io_conn *conn,
return peer_crypto_setup(conn, peer, peer_test);
}
static int make_listen_fd(struct lightningd_state *state,
static int make_listen_fd(struct lightningd_state *dstate,
int domain, void *addr, socklen_t len)
{
int fd = socket(domain, SOCK_STREAM, 0);
if (fd < 0) {
log_debug(state->base_log, "Failed to create %u socket: %s",
log_debug(dstate->base_log, "Failed to create %u socket: %s",
domain, strerror(errno));
return -1;
}
@ -149,17 +149,18 @@ static int make_listen_fd(struct lightningd_state *state,
if (!addr || bind(fd, addr, len) == 0) {
if (listen(fd, 5) == 0)
return fd;
log_unusual(state->base_log, "Failed to listen on %u socket: %s",
log_unusual(dstate->base_log,
"Failed to listen on %u socket: %s",
domain, strerror(errno));
} else
log_debug(state->base_log, "Failed to bind on %u socket: %s",
log_debug(dstate->base_log, "Failed to bind on %u socket: %s",
domain, strerror(errno));
close_noerr(fd);
return -1;
}
void setup_listeners(struct lightningd_state *state, unsigned int portnum)
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum)
{
struct sockaddr_in addr;
struct sockaddr_in6 addr6;
@ -176,43 +177,43 @@ void setup_listeners(struct lightningd_state *state, unsigned int portnum)
addr6.sin6_port = htons(portnum);
/* IPv6, since on Linux that (usually) binds to IPv4 too. */
fd1 = make_listen_fd(state, AF_INET6, portnum ? &addr6 : NULL,
fd1 = make_listen_fd(dstate, AF_INET6, portnum ? &addr6 : NULL,
sizeof(addr6));
if (fd1 >= 0) {
struct sockaddr_in6 in6;
len = sizeof(in6);
if (getsockname(fd1, (void *)&in6, &len) != 0) {
log_unusual(state->base_log,
log_unusual(dstate->base_log,
"Failed get IPv6 sockname: %s",
strerror(errno));
close_noerr(fd1);
} else {
addr.sin_port = in6.sin6_port;
listen_port = ntohs(addr.sin_port);
log_info(state->base_log,
log_info(dstate->base_log,
"Creating IPv6 listener on port %u",
listen_port);
io_new_listener(state, fd1, peer_connected_in, state);
io_new_listener(dstate, fd1, peer_connected_in, dstate);
}
}
/* Just in case, aim for the same port... */
fd2 = make_listen_fd(state, AF_INET,
fd2 = make_listen_fd(dstate, AF_INET,
addr.sin_port ? &addr : NULL, sizeof(addr));
if (fd2 >= 0) {
len = sizeof(addr);
if (getsockname(fd2, (void *)&addr, &len) != 0) {
log_unusual(state->base_log,
log_unusual(dstate->base_log,
"Failed get IPv4 sockname: %s",
strerror(errno));
close_noerr(fd2);
} else {
listen_port = ntohs(addr.sin_port);
log_info(state->base_log,
log_info(dstate->base_log,
"Creating IPv4 listener on port %u",
listen_port);
io_new_listener(state, fd2, peer_connected_in, state);
io_new_listener(dstate, fd2, peer_connected_in, dstate);
}
}
@ -220,7 +221,7 @@ void setup_listeners(struct lightningd_state *state, unsigned int portnum)
fatal("Could not bind to a network address");
}
static void peer_failed(struct lightningd_state *state,
static void peer_failed(struct lightningd_state *dstate,
struct json_connecting *connect)
{
/* FIXME: Better diagnostics! */
@ -247,7 +248,7 @@ static void json_connect(struct command *cmd,
host->end - host->start);
connect->port = tal_strndup(connect, buffer + port->start,
port->end - port->start);
if (!dns_resolve_and_connect(cmd->state, connect->name, connect->port,
if (!dns_resolve_and_connect(cmd->dstate, connect->name, connect->port,
peer_connected_out, peer_failed, connect)) {
command_fail(cmd, "DNS failed");
return;
@ -271,7 +272,7 @@ static void json_getpeers(struct command *cmd,
json_object_start(response, NULL);
json_array_start(response, "peers");
list_for_each(&cmd->state->peers, p, list) {
list_for_each(&cmd->dstate->peers, p, list) {
json_object_start(response, NULL);
json_add_string(response, "name", log_prefix(p->log));
json_add_hex(response, "id", p->id.der, pubkey_derlen(&p->id));

6
daemon/peer.h

@ -21,11 +21,11 @@ struct peer_visible_state {
};
struct peer {
/* state->peers list */
/* dstate->peers list */
struct list_node list;
/* Global state. */
struct lightningd_state *state;
struct lightningd_state *dstate;
/* The other end's address. */
struct netaddr addr;
@ -52,6 +52,6 @@ struct peer {
struct peer_visible_state us, them;
};
void setup_listeners(struct lightningd_state *state, unsigned int portnum);
void setup_listeners(struct lightningd_state *dstate, unsigned int portnum);
#endif /* LIGHTNING_DAEMON_PEER_H */

38
daemon/secrets.c

@ -20,7 +20,7 @@
#include <unistd.h>
struct secret {
/* Secret ID of our node; public is state->id. */
/* Secret ID of our node; public is dstate->id. */
struct privkey privkey;
};
@ -30,8 +30,8 @@ void privkey_sign(struct peer *peer, const void *src, size_t len,
struct sha256_double h;
sha256_double(&h, memcheck(src, len), len);
sign_hash(peer->state->secpctx,
&peer->state->secret->privkey, &h, sig);
sign_hash(peer->dstate->secpctx,
&peer->dstate->secret->privkey, &h, sig);
}
struct peer_secrets {
@ -41,13 +41,13 @@ struct peer_secrets {
struct sha256 revocation_seed;
};
static void new_keypair(struct lightningd_state *state,
static void new_keypair(struct lightningd_state *dstate,
struct privkey *privkey, struct pubkey *pubkey)
{
do {
if (RAND_bytes(privkey->secret, sizeof(privkey->secret)) != 1)
fatal("Could not get random bytes for privkey");
} while (!pubkey_from_privkey(state->secpctx,
} while (!pubkey_from_privkey(dstate->secpctx,
privkey, pubkey, SECP256K1_EC_COMPRESSED));
}
@ -55,8 +55,8 @@ void peer_secrets_init(struct peer *peer)
{
peer->secrets = tal(peer, struct peer_secrets);
new_keypair(peer->state, &peer->secrets->commit, &peer->us.commitkey);
new_keypair(peer->state, &peer->secrets->final, &peer->us.finalkey);
new_keypair(peer->dstate, &peer->secrets->commit, &peer->us.commitkey);
new_keypair(peer->dstate, &peer->secrets->final, &peer->us.finalkey);
if (RAND_bytes(peer->secrets->revocation_seed.u.u8,
sizeof(peer->secrets->revocation_seed.u.u8)) != 1)
fatal("Could not get random bytes for revocation seed");
@ -77,26 +77,26 @@ void peer_get_revocation_hash(const struct peer *peer, u64 index,
sha256(rhash, preimage.u.u8, sizeof(preimage.u.u8));
}
void secrets_init(struct lightningd_state *state)
void secrets_init(struct lightningd_state *dstate)
{
int fd;
state->secret = tal(state, struct secret);
dstate->secret = tal(dstate, struct secret);
fd = open("privkey", O_RDONLY);
if (fd < 0) {
if (errno != ENOENT)
fatal("Failed to open privkey: %s", strerror(errno));
log_unusual(state->base_log, "Creating privkey file");
new_keypair(state, &state->secret->privkey, &state->id);
log_unusual(dstate->base_log, "Creating privkey file");
new_keypair(dstate, &dstate->secret->privkey, &dstate->id);
fd = open("privkey", O_CREAT|O_EXCL|O_WRONLY, 0400);
if (fd < 0)
fatal("Failed to create privkey file: %s",
strerror(errno));
if (!write_all(fd, state->secret->privkey.secret,
sizeof(state->secret->privkey.secret))) {
if (!write_all(fd, dstate->secret->privkey.secret,
sizeof(dstate->secret->privkey.secret))) {
unlink_noerr("privkey");
fatal("Failed to write to privkey file: %s",
strerror(errno));
@ -110,15 +110,15 @@ void secrets_init(struct lightningd_state *state)
if (fd < 0)
fatal("Failed to reopen privkey: %s", strerror(errno));
}
if (!read_all(fd, state->secret->privkey.secret,
sizeof(state->secret->privkey.secret)))
if (!read_all(fd, dstate->secret->privkey.secret,
sizeof(dstate->secret->privkey.secret)))
fatal("Failed to read privkey: %s", strerror(errno));
close(fd);
if (!pubkey_from_privkey(state->secpctx,
&state->secret->privkey, &state->id,
if (!pubkey_from_privkey(dstate->secpctx,
&dstate->secret->privkey, &dstate->id,
SECP256K1_EC_COMPRESSED))
fatal("Invalid privkey");
log_info(state->base_log, "ID: ");
log_add_hex(state->base_log, state->id.der, pubkey_derlen(&state->id));
log_info(dstate->base_log, "ID: ");
log_add_hex(dstate->base_log, dstate->id.der, pubkey_derlen(&dstate->id));
}

2
daemon/secrets.h

@ -19,6 +19,6 @@ void peer_get_revocation_hash(const struct peer *peer, u64 index,
void peer_get_revocation_preimage(const struct peer *peer, u64 index,
struct sha256 *preimage);
void secrets_init(struct lightningd_state *state);
void secrets_init(struct lightningd_state *dstate);
#endif /* LIGHTNING_DAEMON_SECRETS_H */

6
daemon/timeout.c

@ -10,9 +10,9 @@ void init_timeout_(struct timeout *t, unsigned int interval,
t->arg = arg;
}
void refresh_timeout(struct lightningd_state *state, struct timeout *t)
void refresh_timeout(struct lightningd_state *dstate, struct timeout *t)
{
timer_del(&state->timers, &t->timer);
timer_add(&state->timers, &t->timer,
timer_del(&dstate->timers, &t->timer);
timer_add(&dstate->timers, &t->timer,
timeabs_add(time_now(), t->interval));
}

2
daemon/timeout.h

@ -18,7 +18,7 @@ struct lightningd_state;
void init_timeout_(struct timeout *t, unsigned int interval,
void (*cb)(void *), void *arg);
void refresh_timeout(struct lightningd_state *state, struct timeout *t);
void refresh_timeout(struct lightningd_state *dstate, struct timeout *t);
#define init_timeout(t, interval, func, arg) \
init_timeout_((t), (interval), \

50
daemon/watch.c

@ -55,7 +55,7 @@ bool txowatch_eq(const struct txowatch *w, const struct txwatch_output *out)
static void destroy_txowatch(struct txowatch *w)
{
txowatch_hash_del(&w->peer->state->txowatches, w);
txowatch_hash_del(&w->peer->dstate->txowatches, w);
}
/* Watch a txo. */
@ -75,7 +75,7 @@ static void insert_txo_watch(struct peer *peer,
w->cb = cb;
w->cbdata = cbdata;
txowatch_hash_add(&w->peer->state->txowatches, w);
txowatch_hash_add(&w->peer->dstate->txowatches, w);
tal_add_destructor(w, destroy_txowatch);
}
@ -96,11 +96,11 @@ bool txwatch_eq(const struct txwatch *w, const struct sha256_double *txid)
static void destroy_txwatch(struct txwatch *w)
{
txwatch_hash_del(&w->state->txwatches, w);
txwatch_hash_del(&w->dstate->txwatches, w);
}
static struct txwatch *insert_txwatch(const tal_t *ctx,
struct lightningd_state *state,
struct lightningd_state *dstate,
struct peer *peer,
const struct sha256_double *txid,
void (*cb)(struct peer *, int, void *),
@ -109,7 +109,7 @@ static struct txwatch *insert_txwatch(const tal_t *ctx,
struct txwatch *w;
/* We could have a null-watch on it because we saw it spend a TXO */
w = txwatch_hash_get(&state->txwatches, txid);
w = txwatch_hash_get(&dstate->txwatches, txid);
if (w) {
assert(!w->cb);
tal_free(w);
@ -118,12 +118,12 @@ static struct txwatch *insert_txwatch(const tal_t *ctx,
w = tal(ctx, struct txwatch);
w->depth = 0;
w->txid = *txid;
w->state = state;
w->dstate = dstate;
w->peer = peer;
w->cb = cb;
w->cbdata = cbdata;
txwatch_hash_add(&w->state->txwatches, w);
txwatch_hash_add(&w->dstate->txwatches, w);
tal_add_destructor(w, destroy_txwatch);
return w;
@ -141,7 +141,7 @@ void add_anchor_watch_(struct peer *peer,
struct ripemd160 redeemhash;
u8 *redeemscript;
insert_txwatch(peer, peer->state, peer, txid, anchor_cb, cbdata);
insert_txwatch(peer, peer->dstate, peer, txid, anchor_cb, cbdata);
insert_txo_watch(peer, txid, out, spend_cb, cbdata);
redeemscript = bitcoin_redeem_2of2(peer, &peer->them.commitkey,
@ -153,7 +153,7 @@ void add_anchor_watch_(struct peer *peer,
/* Telling bitcoind to watch the redeemhash address means
* it'll tell is about the anchor itself (spend to that
* address), and any commit txs (spend from that address).*/
bitcoind_watch_addr(peer->state, &redeemhash);
bitcoind_watch_addr(peer->dstate, &redeemhash);
}
void add_commit_tx_watch_(struct peer *peer,
@ -161,13 +161,13 @@ void add_commit_tx_watch_(struct peer *peer,
void (*cb)(struct peer *peer, int depth, void *),
void *cbdata)
{
insert_txwatch(peer, peer->state, peer, txid, cb, cbdata);
insert_txwatch(peer, peer->dstate, peer, txid, cb, cbdata);
/* We are already watching the anchor txo, so we don't need to
* watch anything else. */
}
static void tx_watched_inputs(struct lightningd_state *state,
static void tx_watched_inputs(struct lightningd_state *dstate,
const struct bitcoin_tx *tx, void *unused)
{
size_t in;
@ -179,20 +179,20 @@ static void tx_watched_inputs(struct lightningd_state *state,
out.txid = tx->input[in].txid;
out.index = tx->input[in].index;
txow = txowatch_hash_get(&state->txowatches, &out);
txow = txowatch_hash_get(&dstate->txowatches, &out);
if (txow)
txow->cb(txow->peer, tx, txow->cbdata);
}
}
static void watched_transaction(struct lightningd_state *state,
static void watched_transaction(struct lightningd_state *dstate,
const struct sha256_double *txid,
int confirmations)
{
struct txwatch *txw;
/* Are we watching this txid directly (or already reported)? */
txw = txwatch_hash_get(&state->txwatches, txid);
txw = txwatch_hash_get(&dstate->txwatches, txid);
if (txw) {
if (confirmations != txw->depth) {
txw->depth = confirmations;
@ -203,28 +203,28 @@ static void watched_transaction(struct lightningd_state *state,
}
/* Don't report about this txid twice. */
insert_txwatch(state, state, NULL, txid, NULL, NULL);
insert_txwatch(dstate, dstate, NULL, txid, NULL, NULL);
/* Maybe it spent an output we're watching? */
bitcoind_txid_lookup(state, txid, tx_watched_inputs, NULL);
bitcoind_txid_lookup(dstate, txid, tx_watched_inputs, NULL);
}
static struct timeout watch_timeout;
static void start_poll_transactions(struct lightningd_state *state)
static void start_poll_transactions(struct lightningd_state *dstate)
{
if (state->bitcoind_in_progress != 0) {
log_unusual(state->base_log,
if (dstate->bitcoind_in_progress != 0) {
log_unusual(dstate->base_log,
"Delaying start poll: %u commands in progress",
state->bitcoind_in_progress);
dstate->bitcoind_in_progress);
} else
bitcoind_poll_transactions(state, watched_transaction);
refresh_timeout(state, &watch_timeout);
bitcoind_poll_transactions(dstate, watched_transaction);
refresh_timeout(dstate, &watch_timeout);
}
void setup_watch_timer(struct lightningd_state *state)
void setup_watch_timer(struct lightningd_state *dstate)
{
init_timeout(&watch_timeout, 30, start_poll_transactions, state);
init_timeout(&watch_timeout, 30, start_poll_transactions, dstate);
/* Run once immediately, in case there are issues. */
start_poll_transactions(state);
start_poll_transactions(dstate);
}

4
daemon/watch.h

@ -40,7 +40,7 @@ HTABLE_DEFINE_TYPE(struct txowatch, txowatch_keyof, txo_hash, txowatch_eq,
txowatch_hash);
struct txwatch {
struct lightningd_state *state;
struct lightningd_state *dstate;
/* Peer who owns us. */
struct peer *peer;
@ -94,5 +94,5 @@ void add_commit_tx_watch_(struct peer *peer,
int depth), \
(cbdata))
void setup_watch_timer(struct lightningd_state *state);
void setup_watch_timer(struct lightningd_state *dstate);
#endif /* LIGHTNING_DAEMON_WATCH_H */

Loading…
Cancel
Save