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>
This commit is contained in:
Rusty Russell 2016-11-09 08:04:24 +10:30
parent 847ce8b092
commit f71f0da19c
5 changed files with 25 additions and 49 deletions

View File

@ -345,11 +345,8 @@ static void load_peer_visible_state(struct peer *peer)
if (visible_set)
fatal("load_peer_visible_state: two states for %s", select);
visible_set = true;
if (sqlite3_column_int64(stmt, 1))
peer->remote.offer_anchor = CMD_OPEN_WITH_ANCHOR;
else
peer->remote.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
peer->remote.offer_anchor = sqlite3_column_int(stmt, 1);
pubkey_from_sql(peer->dstate->secpctx, stmt, 2,
&peer->remote.commitkey);
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->anchor.satoshis,
peer->local.commit_fee_rate,
peer->local.offer_anchor
== CMD_OPEN_WITH_ANCHOR ?
peer->local.offer_anchor ?
LOCAL : REMOTE);
peer->remote.commit->cstate = initial_cstate(peer->remote.commit,
peer->anchor.satoshis,
peer->remote.commit_fee_rate,
peer->local.offer_anchor
== CMD_OPEN_WITH_ANCHOR ?
peer->local.offer_anchor ?
LOCAL : REMOTE);
/* 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. */
static void restore_peer_local_visible_state(struct peer *peer)
{
if (peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR)
peer->local.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
else
peer->local.offer_anchor = CMD_OPEN_WITH_ANCHOR;
peer->local.offer_anchor = !peer->remote.offer_anchor;
/* peer->local.commitkey and peer->local.finalkey set by
* 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);
l = new_log(dstate, dstate->log_record, "%s:", idstr);
tal_free(idstr);
peer = new_peer(dstate, l, state, sqlite3_column_int(stmt, 2) ?
CMD_OPEN_WITH_ANCHOR : CMD_OPEN_WITHOUT_ANCHOR);
peer = new_peer(dstate, l, state, sqlite3_column_int(stmt, 2));
peer->htlc_id_counter = 0;
peer->id = tal_dup(peer, struct pubkey, &id);
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,
"INSERT INTO their_visible_state VALUES (x'%s', %s, x'%s', x'%s', %u, %u, %"PRIu64", x'%s');",
peerid,
sql_bool(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR),
sql_bool(peer->remote.offer_anchor),
pubkey_to_hexstr(ctx, peer->dstate->secpctx,
&peer->remote.commitkey),
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");",
peerid,
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);
db_exec(__func__, peer->dstate,

View File

@ -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->initial_fee_rate = peer->local.commit_fee_rate;
if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR)
assert(peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR);
assert(peer->local.offer_anchor);
else {
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;
@ -321,17 +321,16 @@ Pkt *accept_pkt_open(struct peer *peer, const Pkt *pkt,
o->initial_fee_rate, feerate,
peer->dstate->config.commitment_fee_max_percent);
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)
peer->remote.offer_anchor = CMD_OPEN_WITHOUT_ANCHOR;
peer->remote.offer_anchor = false;
else
return pkt_err(peer, "Unknown offer anchor value %u",
o->anch);
if (peer->remote.offer_anchor == peer->local.offer_anchor)
return pkt_err(peer, "Exactly one side can offer anchor (we %s)",
peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR
? "do" : "don't");
peer->local.offer_anchor ? "do" : "don't");
if (!proto_to_rel_locktime(o->delay, &peer->remote.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;
/* They must be offering anchor for us to try accepting */
assert(peer->local.offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
assert(peer->remote.offer_anchor == CMD_OPEN_WITH_ANCHOR);
assert(!peer->local.offer_anchor);
assert(peer->remote.offer_anchor);
if (anchor_too_large(a->amount))
return pkt_err(peer, "Anchor millisatoshis exceeds 32 bits");

View File

@ -2338,7 +2338,7 @@ static struct io_plan *peer_crypto_on(struct io_conn *conn, struct peer *peer)
assert(peer->state == STATE_INIT);
/* 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,
__func__, false);
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 log *log,
enum state state,
enum state_input offer_anchor)
bool offer_anchor)
{
struct peer *peer = tal(dstate, struct peer);
assert(offer_anchor == CMD_OPEN_WITH_ANCHOR
|| offer_anchor == CMD_OPEN_WITHOUT_ANCHOR);
peer->state = state;
peer->connected = false;
peer->id = NULL;
@ -2701,7 +2698,7 @@ static struct io_plan *crypto_on_out(struct io_conn *conn,
}
/* 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,
iod, id, true)) {
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 */
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,
iod, id, false))
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->anchor.satoshis,
peer->local.commit_fee_rate,
peer->local.offer_anchor
== CMD_OPEN_WITH_ANCHOR ?
peer->local.offer_anchor ?
LOCAL : REMOTE);
if (!peer->local.commit->cstate)
return false;
@ -4138,8 +4134,7 @@ bool setup_first_commit(struct peer *peer)
peer->remote.commit->cstate = initial_cstate(peer->remote.commit,
peer->anchor.satoshis,
peer->remote.commit_fee_rate,
peer->local.offer_anchor
== CMD_OPEN_WITH_ANCHOR ?
peer->local.offer_anchor ?
LOCAL : REMOTE);
if (!peer->remote.commit->cstate)
return false;
@ -4159,7 +4154,7 @@ bool setup_first_commit(struct peer *peer)
assert(to_them_only != 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);
peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate);

View File

@ -57,9 +57,8 @@ struct commit_info {
};
struct peer_visible_state {
/* CMD_OPEN_WITH_ANCHOR or CMD_OPEN_WITHOUT_ANCHOR */
/* FIXME: Make a bool. */
enum state_input offer_anchor;
/* Is this side funding the channel? */
bool 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 (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 log *log,
enum state state,
enum state_input offer_anchor);
bool offer_anchor);
/* Populates very first peer->{local,remote}.commit->{tx,cstate} */
bool setup_first_commit(struct peer *peer);

View File

@ -94,14 +94,6 @@ enum state_input {
*/
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
};
#endif /* LIGHTNING_STATE_TYPES_H */