Skip to content

Commit

Permalink
daemon: rename 'state' to 'dstate' everywhere.
Browse files Browse the repository at this point in the history
This is the daemon state, not the state machine state.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jan 21, 2016
1 parent 5ac1d5d commit fc49e3f
Show file tree
Hide file tree
Showing 16 changed files with 201 additions and 199 deletions.
44 changes: 22 additions & 22 deletions daemon/bitcoind.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

Expand All @@ -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);
}
Expand All @@ -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;

Expand Down Expand Up @@ -180,44 +180,44 @@ 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);
}

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)
Expand All @@ -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 changes: 7 additions & 7 deletions daemon/bitcoind.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *, \
Expand Down
14 changes: 7 additions & 7 deletions daemon/cryptopkt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
23 changes: 12 additions & 11 deletions daemon/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -107,23 +107,23 @@ 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);
} else {
/* 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;
}
}
}

/* 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,
Expand Down Expand Up @@ -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 *,
Expand All @@ -172,15 +172,16 @@ 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;
d->name = tal_fmt(d, "%s:%s", name, port);

/* 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;
}
Expand All @@ -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]);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions daemon/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *, \
Expand All @@ -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 *,
Expand Down
Loading

0 comments on commit fc49e3f

Please sign in to comment.