Browse Source

offer_anchor: store a bool, not am enum state_input.

Since we no longer feed it into state.c, we can just us a bool.
And that's the last of the CMD_* in the enum state_input, so remove them
all.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
f71f0da19c
  1. 25
      daemon/db.c
  2. 15
      daemon/packets.c
  3. 19
      daemon/peer.c
  4. 7
      daemon/peer.h
  5. 8
      state_types.h

25
daemon/db.c

@ -345,11 +345,8 @@ static void load_peer_visible_state(struct peer *peer)
if (visible_set) if (visible_set)
fatal("load_peer_visible_state: two states for %s", select); fatal("load_peer_visible_state: two states for %s", select);
visible_set = true; visible_set = true;
if (sqlite3_column_int64(stmt, 1)) peer->remote.offer_anchor = sqlite3_column_int(stmt, 1);
peer->remote.offer_anchor = CMD_OPEN_WITH_ANCHOR;
else
peer->remote.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
pubkey_from_sql(peer->dstate->secpctx, stmt, 2, pubkey_from_sql(peer->dstate->secpctx, stmt, 2,
&peer->remote.commitkey); &peer->remote.commitkey);
pubkey_from_sql(peer->dstate->secpctx, stmt, 3, pubkey_from_sql(peer->dstate->secpctx, stmt, 3,
@ -508,14 +505,12 @@ static void load_peer_htlcs(struct peer *peer)
peer->local.commit->cstate = initial_cstate(peer->local.commit, peer->local.commit->cstate = initial_cstate(peer->local.commit,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->local.commit_fee_rate, peer->local.commit_fee_rate,
peer->local.offer_anchor peer->local.offer_anchor ?
== CMD_OPEN_WITH_ANCHOR ?
LOCAL : REMOTE); LOCAL : REMOTE);
peer->remote.commit->cstate = initial_cstate(peer->remote.commit, peer->remote.commit->cstate = initial_cstate(peer->remote.commit,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->remote.commit_fee_rate, peer->remote.commit_fee_rate,
peer->local.offer_anchor peer->local.offer_anchor ?
== CMD_OPEN_WITH_ANCHOR ?
LOCAL : REMOTE); LOCAL : REMOTE);
/* We rebuild cstate by running *every* HTLC through. */ /* We rebuild cstate by running *every* HTLC through. */
@ -866,10 +861,7 @@ static void load_peer_closing(struct peer *peer)
/* FIXME: much of this is redundant. */ /* FIXME: much of this is redundant. */
static void restore_peer_local_visible_state(struct peer *peer) static void restore_peer_local_visible_state(struct peer *peer)
{ {
if (peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR) peer->local.offer_anchor = !peer->remote.offer_anchor;
peer->local.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
else
peer->local.offer_anchor = CMD_OPEN_WITH_ANCHOR;
/* peer->local.commitkey and peer->local.finalkey set by /* peer->local.commitkey and peer->local.finalkey set by
* peer_set_secrets_from_db(). */ * peer_set_secrets_from_db(). */
@ -944,8 +936,7 @@ static void db_load_peers(struct lightningd_state *dstate)
idstr = pubkey_to_hexstr(dstate, dstate->secpctx, &id); idstr = pubkey_to_hexstr(dstate, dstate->secpctx, &id);
l = new_log(dstate, dstate->log_record, "%s:", idstr); l = new_log(dstate, dstate->log_record, "%s:", idstr);
tal_free(idstr); tal_free(idstr);
peer = new_peer(dstate, l, state, sqlite3_column_int(stmt, 2) ? peer = new_peer(dstate, l, state, sqlite3_column_int(stmt, 2));
CMD_OPEN_WITH_ANCHOR : CMD_OPEN_WITHOUT_ANCHOR);
peer->htlc_id_counter = 0; peer->htlc_id_counter = 0;
peer->id = tal_dup(peer, struct pubkey, &id); peer->id = tal_dup(peer, struct pubkey, &id);
peer->local.commit_fee_rate = sqlite3_column_int64(stmt, 3); peer->local.commit_fee_rate = sqlite3_column_int64(stmt, 3);
@ -1370,7 +1361,7 @@ bool db_set_visible_state(struct peer *peer)
db_exec(__func__, peer->dstate, db_exec(__func__, peer->dstate,
"INSERT INTO their_visible_state VALUES (x'%s', %s, x'%s', x'%s', %u, %u, %"PRIu64", x'%s');", "INSERT INTO their_visible_state VALUES (x'%s', %s, x'%s', x'%s', %u, %u, %"PRIu64", x'%s');",
peerid, peerid,
sql_bool(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR), sql_bool(peer->remote.offer_anchor),
pubkey_to_hexstr(ctx, peer->dstate->secpctx, pubkey_to_hexstr(ctx, peer->dstate->secpctx,
&peer->remote.commitkey), &peer->remote.commitkey),
pubkey_to_hexstr(ctx, peer->dstate->secpctx, pubkey_to_hexstr(ctx, peer->dstate->secpctx,
@ -1415,7 +1406,7 @@ bool db_create_peer(struct peer *peer)
"INSERT INTO peers VALUES (x'%s', '%s', %s, %"PRIi64");", "INSERT INTO peers VALUES (x'%s', '%s', %s, %"PRIi64");",
peerid, peerid,
state_name(peer->state), state_name(peer->state),
sql_bool(peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR), sql_bool(peer->local.offer_anchor),
peer->local.commit_fee_rate); peer->local.commit_fee_rate);
db_exec(__func__, peer->dstate, db_exec(__func__, peer->dstate,

15
daemon/packets.c

@ -89,10 +89,10 @@ void queue_pkt_open(struct peer *peer, OpenChannel__AnchorOffer anchor)
o->delay->blocks = rel_locktime_to_blocks(&peer->local.locktime); o->delay->blocks = rel_locktime_to_blocks(&peer->local.locktime);
o->initial_fee_rate = peer->local.commit_fee_rate; o->initial_fee_rate = peer->local.commit_fee_rate;
if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR) if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
assert(peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR); assert(peer->local.offer_anchor);
else { else {
assert(anchor == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR); assert(anchor == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR);
assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); assert(!peer->local.offer_anchor);
} }
o->anch = anchor; o->anch = anchor;
@ -321,17 +321,16 @@ Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt,
o->initial_fee_rate, feerate, o->initial_fee_rate, feerate,
peer->dstate->config.commitment_fee_max_percent); peer->dstate->config.commitment_fee_max_percent);
if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR) if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
peer->remote.offer_anchor = CMD_OPEN_WITH_ANCHOR; peer->remote.offer_anchor = true;
else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR) else if (o->anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR)
peer->remote.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR; peer->remote.offer_anchor = false;
else else
return pkt_err(peer, "Unknown offer anchor value %u", return pkt_err(peer, "Unknown offer anchor value %u",
o->anch); o->anch);
if (peer->remote.offer_anchor == peer->local.offer_anchor) if (peer->remote.offer_anchor == peer->local.offer_anchor)
return pkt_err(peer, "Exactly one side can offer anchor (we %s)", return pkt_err(peer, "Exactly one side can offer anchor (we %s)",
peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR peer->local.offer_anchor ? "do" : "don't");
? "do" : "don't");
if (!proto_to_rel_locktime(o->delay, &peer->remote.locktime)) if (!proto_to_rel_locktime(o->delay, &peer->remote.locktime))
return pkt_err(peer, "Malformed locktime"); return pkt_err(peer, "Malformed locktime");
@ -354,8 +353,8 @@ Pkt *accept_pkt_anchor(struct peer *peer, const Pkt *pkt)
const OpenAnchor *a = pkt->open_anchor; const OpenAnchor *a = pkt->open_anchor;
/* They must be offering anchor for us to try accepting */ /* They must be offering anchor for us to try accepting */
assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR); assert(!peer->local.offer_anchor);
assert(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR); assert(peer->remote.offer_anchor);
if (anchor_too_large(a->amount)) if (anchor_too_large(a->amount))
return pkt_err(peer, "Anchor millisatoshis exceeds 32 bits"); return pkt_err(peer, "Anchor millisatoshis exceeds 32 bits");

19
daemon/peer.c

@ -2338,7 +2338,7 @@ static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
assert(peer->state == STATE_INIT); assert(peer->state == STATE_INIT);
/* FIXME: Start timeout, and close peer if they don't progress! */ /* FIXME: Start timeout, and close peer if they don't progress! */
if (peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR) { if (peer->local.offer_anchor) {
set_peer_state(peer, STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR, set_peer_state(peer, STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR,
__func__, false); __func__, false);
anchor = OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR; anchor = OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR;
@ -2469,13 +2469,10 @@ static bool peer_reconnected(struct peer *peer,
struct peer *new_peer(struct lightningd_state *dstate, struct peer *new_peer(struct lightningd_state *dstate,
struct log *log, struct log *log,
enum state state, enum state state,
enum state_input offer_anchor) bool offer_anchor)
{ {
struct peer *peer = tal(dstate, struct peer); struct peer *peer = tal(dstate, struct peer);
assert(offer_anchor == CMD_OPEN_WITH_ANCHOR
|| offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
peer->state = state; peer->state = state;
peer->connected = false; peer->connected = false;
peer->id = NULL; peer->id = NULL;
@ -2701,7 +2698,7 @@ static struct io_plan *crypto_on_out(struct io_conn *conn,
} }
/* Initiator currently funds channel */ /* Initiator currently funds channel */
peer = new_peer(dstate, log, STATE_INIT, CMD_OPEN_WITH_ANCHOR); peer = new_peer(dstate, log, STATE_INIT, true);
if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP, if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
iod, id, true)) { iod, id, true)) {
command_fail(connect->cmd, "Failed to make peer for %s:%s", command_fail(connect->cmd, "Failed to make peer for %s:%s",
@ -2762,7 +2759,7 @@ static struct io_plan *crypto_on_in(struct io_conn *conn,
} }
/* Initiator currently funds channel */ /* Initiator currently funds channel */
peer = new_peer(dstate, log, STATE_INIT, CMD_OPEN_WITHOUT_ANCHOR); peer = new_peer(dstate, log, STATE_INIT, false);
if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP, if (!peer_first_connected(peer, conn, SOCK_STREAM, IPPROTO_TCP,
iod, id, false)) iod, id, false))
return io_close(conn); return io_close(conn);
@ -4129,8 +4126,7 @@ bool setup_first_commit(struct peer *peer)
peer->local.commit->cstate = initial_cstate(peer->local.commit, peer->local.commit->cstate = initial_cstate(peer->local.commit,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->local.commit_fee_rate, peer->local.commit_fee_rate,
peer->local.offer_anchor peer->local.offer_anchor ?
== CMD_OPEN_WITH_ANCHOR ?
LOCAL : REMOTE); LOCAL : REMOTE);
if (!peer->local.commit->cstate) if (!peer->local.commit->cstate)
return false; return false;
@ -4138,8 +4134,7 @@ bool setup_first_commit(struct peer *peer)
peer->remote.commit->cstate = initial_cstate(peer->remote.commit, peer->remote.commit->cstate = initial_cstate(peer->remote.commit,
peer->anchor.satoshis, peer->anchor.satoshis,
peer->remote.commit_fee_rate, peer->remote.commit_fee_rate,
peer->local.offer_anchor peer->local.offer_anchor ?
== CMD_OPEN_WITH_ANCHOR ?
LOCAL : REMOTE); LOCAL : REMOTE);
if (!peer->remote.commit->cstate) if (!peer->remote.commit->cstate)
return false; return false;
@ -4159,7 +4154,7 @@ bool setup_first_commit(struct peer *peer)
assert(to_them_only != to_us_only); assert(to_them_only != to_us_only);
/* If we offer anchor, their commit is to-us only. */ /* If we offer anchor, their commit is to-us only. */
assert(to_us_only == (peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR)); assert(to_us_only == peer->local.offer_anchor);
bitcoin_txid(peer->remote.commit->tx, &peer->remote.commit->txid); bitcoin_txid(peer->remote.commit->tx, &peer->remote.commit->txid);
peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate); peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate);

7
daemon/peer.h

@ -57,9 +57,8 @@ struct commit_info {
}; };
struct peer_visible_state { struct peer_visible_state {
/* CMD_OPEN_WITH_ANCHOR or CMD_OPEN_WITHOUT_ANCHOR */ /* Is this side funding the channel? */
/* FIXME: Make a bool. */ bool offer_anchor;
enum state_input offer_anchor;
/* Key for commitment tx inputs, then key for commitment tx outputs */ /* Key for commitment tx inputs, then key for commitment tx outputs */
struct pubkey commitkey, finalkey; struct pubkey commitkey, finalkey;
/* How long to they want the other's outputs locked (blocks) */ /* How long to they want the other's outputs locked (blocks) */
@ -248,7 +247,7 @@ struct peer *find_peer_by_pkhash(struct lightningd_state *dstate, const u8 *pkha
struct peer *new_peer(struct lightningd_state *dstate, struct peer *new_peer(struct lightningd_state *dstate,
struct log *log, struct log *log,
enum state state, enum state state,
enum state_input offer_anchor); bool offer_anchor);
/* Populates very first peer->{local,remote}.commit->{tx,cstate} */ /* Populates very first peer->{local,remote}.commit->{tx,cstate} */
bool setup_first_commit(struct peer *peer); bool setup_first_commit(struct peer *peer);

8
state_types.h

@ -94,14 +94,6 @@ enum state_input {
*/ */
INPUT_CLOSE_COMPLETE_TIMEOUT, INPUT_CLOSE_COMPLETE_TIMEOUT,
/* Commands */
CMD_OPEN_WITH_ANCHOR,
CMD_OPEN_WITHOUT_ANCHOR,
CMD_SEND_HTLC_ADD,
CMD_SEND_HTLC_FULFILL,
CMD_SEND_HTLC_FAIL,
CMD_SEND_COMMIT,
INPUT_MAX INPUT_MAX
}; };
#endif /* LIGHTNING_STATE_TYPES_H */ #endif /* LIGHTNING_STATE_TYPES_H */

Loading…
Cancel
Save