From d8af789bbb9d86e814ead74555a5d6bcbd65dad4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 31 Aug 2016 16:06:32 +0930 Subject: [PATCH] channel: remove enum channel_side, rename htlc_side to side. We had enum channel_side (OURS, THEIRS) for which end of a channel we had, and htlc_side (LOCAL, REMOTE) for who proposed the HTLC. Combine these both into simply "enum side". Signed-off-by: Rusty Russell --- daemon/channel.c | 44 ++++++++++++++++------------------ daemon/channel.h | 40 +++++++++++++++++++++---------- daemon/commit_tx.c | 20 ++++++++-------- daemon/commit_tx.h | 8 +++---- daemon/db.c | 53 ++++++++++++++++++++--------------------- daemon/db.h | 2 +- daemon/feechange.h | 6 +++-- daemon/htlc.h | 25 +++++-------------- daemon/log.c | 4 ++-- daemon/output_to_htlc.c | 2 +- daemon/output_to_htlc.h | 2 +- daemon/packets.c | 2 +- daemon/peer.c | 36 ++++++++++++++-------------- state.c | 4 ++-- 14 files changed, 125 insertions(+), 123 deletions(-) diff --git a/daemon/channel.c b/daemon/channel.c index eb1e77caf..261ae033f 100644 --- a/daemon/channel.c +++ b/daemon/channel.c @@ -123,7 +123,7 @@ static bool change_funding(uint64_t anchor_satoshis, struct channel_state *initial_cstate(const tal_t *ctx, uint64_t anchor_satoshis, uint64_t fee_rate, - enum channel_side funding) + enum side funding) { uint64_t fee_msat; struct channel_state *cstate = talz(ctx, struct channel_state); @@ -161,7 +161,7 @@ struct channel_state *initial_cstate(const tal_t *ctx, /* FIXME: Write exact variant! */ uint64_t approx_max_feerate(const struct channel_state *cstate, - enum channel_side side) + enum side side) { uint64_t max_funds; @@ -171,7 +171,7 @@ uint64_t approx_max_feerate(const struct channel_state *cstate, } bool can_afford_feerate(const struct channel_state *cstate, uint64_t fee_rate, - enum channel_side side) + enum side side) { u64 fee_msat = calculate_fee_msat(cstate->num_nondust, fee_rate); @@ -185,7 +185,7 @@ void adjust_fee(struct channel_state *cstate, uint64_t fee_rate) fee_msat = calculate_fee_msat(cstate->num_nondust, fee_rate); - recalculate_fees(&cstate->side[OURS], &cstate->side[THEIRS], fee_msat); + recalculate_fees(&cstate->side[LOCAL], &cstate->side[REMOTE], fee_msat); } bool force_fee(struct channel_state *cstate, uint64_t fee) @@ -193,8 +193,8 @@ bool force_fee(struct channel_state *cstate, uint64_t fee) /* Beware overflow! */ if (fee > 0xFFFFFFFFFFFFFFFFULL / 1000) return false; - recalculate_fees(&cstate->side[OURS], &cstate->side[THEIRS], fee * 1000); - return cstate->side[OURS].fee_msat + cstate->side[THEIRS].fee_msat == fee * 1000; + recalculate_fees(&cstate->side[LOCAL], &cstate->side[REMOTE], fee * 1000); + return cstate->side[LOCAL].fee_msat + cstate->side[REMOTE].fee_msat == fee * 1000; } /* Add a HTLC to @creator if it can afford it. */ @@ -204,8 +204,8 @@ bool cstate_add_htlc(struct channel_state *cstate, const struct htlc *htlc, size_t nondust; struct channel_oneside *creator, *recipient; - creator = &cstate->side[htlc_channel_side(htlc)]; - recipient = &cstate->side[!htlc_channel_side(htlc)]; + creator = &cstate->side[htlc_owner(htlc)]; + recipient = &cstate->side[!htlc_owner(htlc)]; /* Remember to count the new one in total txsize if not dust! */ nondust = cstate->num_nondust; @@ -224,8 +224,8 @@ bool cstate_add_htlc(struct channel_state *cstate, const struct htlc *htlc, /* Remove htlc from creator, credit it to beneficiary. */ static void remove_htlc(struct channel_state *cstate, - enum channel_side creator, - enum channel_side beneficiary, + enum side creator, + enum side beneficiary, const struct htlc *htlc) { size_t nondust; @@ -252,14 +252,12 @@ static void remove_htlc(struct channel_state *cstate, void cstate_fail_htlc(struct channel_state *cstate, const struct htlc *htlc) { - remove_htlc(cstate, htlc_channel_side(htlc), htlc_channel_side(htlc), - htlc); + remove_htlc(cstate, htlc_owner(htlc), htlc_owner(htlc), htlc); } void cstate_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc) { - remove_htlc(cstate, htlc_channel_side(htlc), !htlc_channel_side(htlc), - htlc); + remove_htlc(cstate, htlc_owner(htlc), !htlc_owner(htlc), htlc); } struct channel_state *copy_cstate(const tal_t *ctx, @@ -272,7 +270,7 @@ void force_add_htlc(struct channel_state *cstate, const struct htlc *htlc) { struct channel_oneside *creator; - creator = &cstate->side[htlc_channel_side(htlc)]; + creator = &cstate->side[htlc_owner(htlc)]; creator->num_htlcs++; creator->pay_msat -= htlc->msatoshis; @@ -282,40 +280,40 @@ void force_add_htlc(struct channel_state *cstate, const struct htlc *htlc) } static void force_remove_htlc(struct channel_state *cstate, - enum channel_side beneficiary, + enum side beneficiary, const struct htlc *htlc) { cstate->side[beneficiary].pay_msat += htlc->msatoshis; - cstate->side[htlc_channel_side(htlc)].num_htlcs--; + cstate->side[htlc_owner(htlc)].num_htlcs--; if (!is_dust(htlc->msatoshis / 1000)) cstate->num_nondust--; } void force_fail_htlc(struct channel_state *cstate, const struct htlc *htlc) { - force_remove_htlc(cstate, htlc_channel_side(htlc), htlc); + force_remove_htlc(cstate, htlc_owner(htlc), htlc); } void force_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc) { - force_remove_htlc(cstate, !htlc_channel_side(htlc), htlc); + force_remove_htlc(cstate, !htlc_owner(htlc), htlc); } bool balance_after_force(struct channel_state *cstate) { /* We should not spend more than anchor */ - if (cstate->side[OURS].pay_msat + cstate->side[THEIRS].pay_msat + if (cstate->side[LOCAL].pay_msat + cstate->side[REMOTE].pay_msat > cstate->anchor * 1000) return false; /* Check for wrap. */ - if (cstate->side[OURS].pay_msat > cstate->anchor * 1000) + if (cstate->side[LOCAL].pay_msat > cstate->anchor * 1000) return false; - if (cstate->side[THEIRS].pay_msat > cstate->anchor * 1000) + if (cstate->side[REMOTE].pay_msat > cstate->anchor * 1000) return false; if (cstate->num_nondust - > cstate->side[OURS].num_htlcs + cstate->side[THEIRS].num_htlcs) + > cstate->side[LOCAL].num_htlcs + cstate->side[REMOTE].num_htlcs) return false; /* Recalc fees. */ diff --git a/daemon/channel.h b/daemon/channel.h index 0fc2893fe..48ae2e21b 100644 --- a/daemon/channel.h +++ b/daemon/channel.h @@ -2,12 +2,19 @@ #define LIGHTNING_DAEMON_CHANNEL_H #include "config.h" #include "bitcoin/locktime.h" +#include #include +#include #include #include struct htlc; +enum side { + LOCAL, + REMOTE +}; + struct channel_oneside { /* Payment and fee is in millisatoshi. */ uint32_t pay_msat, fee_msat; @@ -15,13 +22,6 @@ struct channel_oneside { unsigned int num_htlcs; }; -enum channel_side { - /* Output for us, htlcs we offered to them. */ - OURS, - /* Output for them, htlcs they offered to us. */ - THEIRS -}; - struct channel_state { /* Satoshis paid by anchor. */ uint64_t anchor; @@ -44,7 +44,7 @@ struct channel_state { struct channel_state *initial_cstate(const tal_t *ctx, uint64_t anchor_satoshis, uint64_t fee_rate, - enum channel_side side); + enum side side); /** * copy_cstate: Make a deep copy of channel_state @@ -92,21 +92,21 @@ void cstate_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc); /** * approx_max_feerate: what's the most side could raise fee rate to? * @cstate: The channel state - * @side: OURS or THEIRS + * @side: LOCAL or REMOTE * * This is not exact! To check if their offer is valid, use can_afford_feerate. */ uint64_t approx_max_feerate(const struct channel_state *cstate, - enum channel_side side); + enum side side); /** * can_afford_feerate: could this side pay for the fee if changed to fee_rate? * @cstate: The channel state * @fee_rate: the new fee rate proposed - * @side: OURS or THEIRS + * @side: LOCAL or REMOTE */ bool can_afford_feerate(const struct channel_state *cstate, uint64_t fee_rate, - enum channel_side side); + enum side side); /** * adjust_fee: Change fee rate. @@ -139,4 +139,20 @@ void force_fail_htlc(struct channel_state *cstate, const struct htlc *htlc); void force_fulfill_htlc(struct channel_state *cstate, const struct htlc *htlc); bool balance_after_force(struct channel_state *cstate); +static inline const char *side_to_str(enum side side) +{ + switch (side) { + case LOCAL: return "LOCAL"; + case REMOTE: return "REMOTE"; + } + abort(); +} + +static inline enum side str_to_side(const char *str) +{ + if (streq(str, "LOCAL")) + return LOCAL; + assert(streq(str, "REMOTE")); + return REMOTE; +} #endif /* LIGHTNING_DAEMON_CHANNEL_H */ diff --git a/daemon/commit_tx.c b/daemon/commit_tx.c index 2e4058fe2..6fe73c235 100644 --- a/daemon/commit_tx.c +++ b/daemon/commit_tx.c @@ -20,7 +20,7 @@ u8 *wscript_for_htlc(const tal_t *ctx, const struct peer *peer, const struct htlc *h, const struct sha256 *rhash, - enum htlc_side side) + enum side side) { const struct peer_visible_state *this_side, *other_side; u8 *(*fn)(const tal_t *, secp256k1_context *, @@ -63,7 +63,7 @@ static size_t count_htlcs(const struct htlc_map *htlcs, int flag) u8 *commit_output_to_us(const tal_t *ctx, const struct peer *peer, const struct sha256 *rhash, - enum htlc_side side, + enum side side, u8 **wscript) { u8 *tmp; @@ -90,7 +90,7 @@ u8 *commit_output_to_us(const tal_t *ctx, u8 *commit_output_to_them(const tal_t *ctx, const struct peer *peer, const struct sha256 *rhash, - enum htlc_side side, + enum side side, u8 **wscript) { u8 *tmp; @@ -132,7 +132,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, struct peer *peer, const struct sha256 *rhash, const struct channel_state *cstate, - enum htlc_side side, + enum side side, bool *otherside_only) { struct bitcoin_tx *tx; @@ -164,28 +164,28 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, tx->output_count = 0; pays_to[LOCAL] = add_output(tx, commit_output_to_us(tx, peer, rhash, side, NULL), - cstate->side[OURS].pay_msat / 1000, + cstate->side[LOCAL].pay_msat / 1000, &total); if (pays_to[LOCAL]) log_debug(peer->log, "Pays %u to local: %s", - cstate->side[OURS].pay_msat / 1000, + cstate->side[LOCAL].pay_msat / 1000, tal_hexstr(tx, tx->output[tx->output_count-1].script, tx->output[tx->output_count-1].script_length)); else log_debug(peer->log, "DOES NOT pay %u to local", - cstate->side[OURS].pay_msat / 1000); + cstate->side[LOCAL].pay_msat / 1000); pays_to[REMOTE] = add_output(tx, commit_output_to_them(tx, peer, rhash, side, NULL), - cstate->side[THEIRS].pay_msat / 1000, + cstate->side[REMOTE].pay_msat / 1000, &total); if (pays_to[REMOTE]) log_debug(peer->log, "Pays %u to remote: %s", - cstate->side[THEIRS].pay_msat / 1000, + cstate->side[REMOTE].pay_msat / 1000, tal_hexstr(tx, tx->output[tx->output_count-1].script, tx->output[tx->output_count-1].script_length)); else log_debug(peer->log, "DOES NOT pay %u to remote", - cstate->side[THEIRS].pay_msat / 1000); + cstate->side[REMOTE].pay_msat / 1000); /* If their tx doesn't pay to them, or our tx doesn't pay to us... */ *otherside_only = !pays_to[side]; diff --git a/daemon/commit_tx.h b/daemon/commit_tx.h index dd0cdb1f2..33ef97509 100644 --- a/daemon/commit_tx.h +++ b/daemon/commit_tx.h @@ -12,20 +12,20 @@ u8 *wscript_for_htlc(const tal_t *ctx, const struct peer *peer, const struct htlc *h, const struct sha256 *rhash, - enum htlc_side side); + enum side side); /* Returns scriptpubkey: *wscript is NULL if it's a direct p2wpkh. */ u8 *commit_output_to_us(const tal_t *ctx, const struct peer *peer, const struct sha256 *rhash, - enum htlc_side side, + enum side side, u8 **wscript); /* Returns scriptpubkey: *wscript is NULL if it's a direct p2wpkh. */ u8 *commit_output_to_them(const tal_t *ctx, const struct peer *peer, const struct sha256 *rhash, - enum htlc_side side, + enum side side, u8 **wscript); /* Create commitment tx to spend the anchor tx output; doesn't fill in @@ -34,6 +34,6 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, struct peer *peer, const struct sha256 *rhash, const struct channel_state *cstate, - enum htlc_side side, + enum side side, bool *otherside_only); #endif diff --git a/daemon/db.c b/daemon/db.c index 54371874f..6123c87c7 100644 --- a/daemon/db.c +++ b/daemon/db.c @@ -384,10 +384,10 @@ static void load_peer_commit_info(struct peer *peer) fatal("load_peer_commit_info:step gave %i cols, not 7", sqlite3_column_count(stmt)); - if (streq(sqlite3_column_str(stmt, 1), "OURS")) + if (streq(sqlite3_column_str(stmt, 1), "LOCAL")) cip = &peer->local.commit; else { - if (!streq(sqlite3_column_str(stmt, 1), "THEIRS")) + if (!streq(sqlite3_column_str(stmt, 1), "REMOTE")) fatal("load_peer_commit_info:bad side %s", sqlite3_column_str(stmt, 1)); cip = &peer->remote.commit; @@ -439,9 +439,9 @@ static void load_peer_commit_info(struct peer *peer) * and do normally-impossible things in intermediate states. So we * mangle cstate balances manually. */ static void apply_htlc(struct channel_state *cstate, const struct htlc *htlc, - enum htlc_side side) + enum side side) { - const char *sidestr = (side == LOCAL ? "LOCAL" : "REMOTE"); + const char *sidestr = side_to_str(side); if (!htlc_has(htlc, HTLC_FLAG(side,HTLC_F_WAS_COMMITTED))) return; @@ -485,13 +485,13 @@ static void load_peer_htlcs(struct peer *peer) peer->local.commit_fee_rate, peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR ? - OURS : THEIRS); + LOCAL : REMOTE); peer->remote.commit->cstate = initial_cstate(peer, peer->anchor.satoshis, peer->remote.commit_fee_rate, peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR ? - OURS : THEIRS); + LOCAL : REMOTE); /* We rebuild cstate by running *every* HTLC through. */ while ((err = sqlite3_step(stmt)) != SQLITE_DONE) { @@ -614,19 +614,19 @@ static void load_peer_htlcs(struct peer *peer) peer->remote.staging_cstate = copy_cstate(peer, peer->remote.commit->cstate); peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate); log_debug(peer->log, "Local staging: pay %u/%u fee %u/%u htlcs %u/%u", - peer->local.staging_cstate->side[OURS].pay_msat, - peer->local.staging_cstate->side[THEIRS].pay_msat, - peer->local.staging_cstate->side[OURS].fee_msat, - peer->local.staging_cstate->side[THEIRS].fee_msat, - peer->local.staging_cstate->side[OURS].num_htlcs, - peer->local.staging_cstate->side[THEIRS].num_htlcs); + peer->local.staging_cstate->side[LOCAL].pay_msat, + peer->local.staging_cstate->side[REMOTE].pay_msat, + peer->local.staging_cstate->side[LOCAL].fee_msat, + peer->local.staging_cstate->side[REMOTE].fee_msat, + peer->local.staging_cstate->side[LOCAL].num_htlcs, + peer->local.staging_cstate->side[REMOTE].num_htlcs); log_debug(peer->log, "Remote staging: pay %u/%u fee %u/%u htlcs %u/%u", - peer->remote.staging_cstate->side[OURS].pay_msat, - peer->remote.staging_cstate->side[THEIRS].pay_msat, - peer->remote.staging_cstate->side[OURS].fee_msat, - peer->remote.staging_cstate->side[THEIRS].fee_msat, - peer->remote.staging_cstate->side[OURS].num_htlcs, - peer->remote.staging_cstate->side[THEIRS].num_htlcs); + peer->remote.staging_cstate->side[LOCAL].pay_msat, + peer->remote.staging_cstate->side[REMOTE].pay_msat, + peer->remote.staging_cstate->side[LOCAL].fee_msat, + peer->remote.staging_cstate->side[REMOTE].fee_msat, + peer->remote.staging_cstate->side[LOCAL].num_htlcs, + peer->remote.staging_cstate->side[REMOTE].num_htlcs); tal_free(ctx); } @@ -1076,8 +1076,9 @@ bool db_set_anchor(struct peer *peer) goto out; errmsg = db_exec(ctx, peer->dstate, - "INSERT INTO commit_info VALUES(x'%s', 'OURS', 0, x'%s', %"PRIi64", %s, NULL);", + "INSERT INTO commit_info VALUES(x'%s', '%s', 0, x'%s', %"PRIi64", %s, NULL);", peerid, + side_to_str(LOCAL), tal_hexstr(ctx, &peer->local.commit->revocation_hash, sizeof(peer->local.commit->revocation_hash)), peer->local.commit->order, @@ -1087,8 +1088,9 @@ bool db_set_anchor(struct peer *peer) goto out; errmsg = db_exec(ctx, peer->dstate, - "INSERT INTO commit_info VALUES(x'%s', 'THEIRS', 0, x'%s', %"PRIi64", %s, NULL);", + "INSERT INTO commit_info VALUES(x'%s', '%s', 0, x'%s', %"PRIi64", %s, NULL);", peerid, + side_to_str(REMOTE), tal_hexstr(ctx, &peer->remote.commit->revocation_hash, sizeof(peer->remote.commit->revocation_hash)), peer->remote.commit->order, @@ -1422,22 +1424,19 @@ bool db_htlc_failed(struct peer *peer, const struct htlc *htlc) return !errmsg; } -bool db_new_commit_info(struct peer *peer, enum channel_side side, +bool db_new_commit_info(struct peer *peer, enum side side, const struct sha256 *prev_rhash) { struct commit_info *ci; - const char *sidestr; const char *errmsg, *ctx = tal(peer, char); const char *peerid = pubkey_to_hexstr(ctx, peer->dstate->secpctx, peer->id); log_debug(peer->log, "%s(%s)", __func__, peerid); assert(peer->dstate->db->in_transaction); - if (side == OURS) { - sidestr = "OURS"; + if (side == LOCAL) { ci = peer->local.commit; } else { - sidestr = "THEIRS"; ci = peer->remote.commit; } @@ -1449,7 +1448,7 @@ bool db_new_commit_info(struct peer *peer, enum channel_side side, sig_to_sql(ctx, peer->dstate->secpctx, ci->sig), ci->order, sql_hex_or_null(ctx, prev_rhash, sizeof(*prev_rhash)), - peerid, sidestr); + peerid, side_to_str(side)); if (errmsg) log_broken(peer->log, "%s:%s", __func__, errmsg); tal_free(ctx); @@ -1467,7 +1466,7 @@ bool db_remove_their_prev_revocation_hash(struct peer *peer) assert(peer->dstate->db->in_transaction); // CREATE TABLE commit_info (peer "SQL_PUBKEY", side TEXT, commit_num INT, revocation_hash "SQL_SHA256", xmit_order INT, sig "SQL_SIGNATURE", prev_revocation_hash "SQL_SHA256", PRIMARY KEY(peer, side)); - errmsg = db_exec(ctx, peer->dstate, "UPDATE commit_info SET prev_revocation_hash=NULL WHERE peer=x'%s' AND side='THEIRS' and prev_revocation_hash IS NOT NULL;", + errmsg = db_exec(ctx, peer->dstate, "UPDATE commit_info SET prev_revocation_hash=NULL WHERE peer=x'%s' AND side='REMOTE' and prev_revocation_hash IS NOT NULL;", peerid); if (errmsg) log_broken(peer->log, "%s:%s", __func__, errmsg); diff --git a/daemon/db.h b/daemon/db.h index 566b7521b..4be6ba57c 100644 --- a/daemon/db.h +++ b/daemon/db.h @@ -39,7 +39,7 @@ bool db_update_htlc_state(struct peer *peer, const struct htlc *htlc, bool db_update_feechange_state(struct peer *peer, const struct feechange *f, enum htlc_state oldstate); -bool db_new_commit_info(struct peer *peer, enum channel_side side, +bool db_new_commit_info(struct peer *peer, enum side side, const struct sha256 *prev_rhash); bool db_remove_their_prev_revocation_hash(struct peer *peer); bool db_update_next_revocation_hash(struct peer *peer); diff --git a/daemon/feechange.h b/daemon/feechange.h index 0a29f01b5..92f0ca74e 100644 --- a/daemon/feechange.h +++ b/daemon/feechange.h @@ -1,8 +1,10 @@ #ifndef LIGHTNING_DAEMON_FEECHANGE_H #define LIGHTNING_DAEMON_FEECHANGE_H #include "config.h" +#include "channel.h" #include "feechange_state.h" -#include "htlc.h" + +struct peer; struct feechange { /* What's the status */ @@ -11,7 +13,7 @@ struct feechange { u64 fee_rate; }; -static inline enum htlc_side feechange_side(enum feechange_state state) +static inline enum side feechange_side(enum feechange_state state) { if (state <= SENT_FEECHANGE_ACK_REVOCATION) { return LOCAL; diff --git a/daemon/htlc.h b/daemon/htlc.h index d40d882b0..21841e313 100644 --- a/daemon/htlc.h +++ b/daemon/htlc.h @@ -27,11 +27,6 @@ #define HTLC_F_WAS_COMMITTED 0x10 /* Each of the above flags applies to both sides */ -enum htlc_side { - LOCAL, - REMOTE -}; - #define HTLC_FLAG(side,flag) ((flag) << ((side) * 5)) #define HTLC_REMOTE_F_PENDING HTLC_FLAG(REMOTE,HTLC_F_PENDING) @@ -49,11 +44,11 @@ enum htlc_side { struct htlc { /* Useful for debugging, and decoding via ->src. */ struct peer *peer; - /* Block number where we abort if it's still live (OURS only) */ + /* Block number where we abort if it's still live (LOCAL only) */ u32 deadline; /* What's the status. */ enum htlc_state state; - /* The unique ID for this peer and this direction (ours or theirs) */ + /* The unique ID for this peer and this direction (LOCAL or REMOTE) */ u64 id; /* The amount in millisatoshi. */ u64 msatoshis; @@ -67,7 +62,7 @@ struct htlc { /* FIXME: We could union these together: */ /* Routing information sent with this HTLC. */ const u8 *routing; - /* Previous HTLC (if any) which made us offer this (OURS only) */ + /* Previous HTLC (if any) which made us offer this (LOCAL only) */ struct htlc *src; const u8 *fail; }; @@ -85,7 +80,7 @@ static inline bool htlc_has(const struct htlc *h, int flag) return htlc_state_flags(h->state) & flag; } -static inline enum htlc_side htlc_state_owner(enum htlc_state state) +static inline enum side htlc_state_owner(enum htlc_state state) { if (state < RCVD_ADD_HTLC) { assert((htlc_state_flags(state) @@ -100,19 +95,11 @@ static inline enum htlc_side htlc_state_owner(enum htlc_state state) } } -static inline enum htlc_side htlc_owner(const struct htlc *h) +static inline enum side htlc_owner(const struct htlc *h) { return htlc_state_owner(h->state); } -/* FIXME: Transitional function. */ -static inline enum channel_side htlc_channel_side(const struct htlc *h) -{ - if (htlc_owner(h) == LOCAL) - return OURS; - return THEIRS; -} - void htlc_undostate(struct htlc *h, enum htlc_state oldstate, enum htlc_state newstate); @@ -131,7 +118,7 @@ static inline size_t htlc_hash(u64 id) } HTABLE_DEFINE_TYPE(struct htlc, htlc_key, htlc_hash, htlc_cmp, htlc_map); -static inline struct htlc *htlc_get(struct htlc_map *htlcs, u64 id, enum htlc_side owner) +static inline struct htlc *htlc_get(struct htlc_map *htlcs, u64 id, enum side owner) { struct htlc *h; struct htlc_map_iter it; diff --git a/daemon/log.c b/daemon/log.c index fc463da4e..79827216d 100644 --- a/daemon/log.c +++ b/daemon/log.c @@ -346,9 +346,9 @@ static char *to_string_(const tal_t *ctx, u.cstate->fee_rate, u.cstate->num_nondust, to_string(ctx, lr, struct channel_oneside, - &u.cstate->side[OURS]), + &u.cstate->side[LOCAL]), to_string(ctx, lr, struct channel_oneside, - &u.cstate->side[THEIRS])); + &u.cstate->side[REMOTE])); } return s; diff --git a/daemon/output_to_htlc.c b/daemon/output_to_htlc.c index 265c4fa5b..653e469b6 100644 --- a/daemon/output_to_htlc.c +++ b/daemon/output_to_htlc.c @@ -16,7 +16,7 @@ struct htlc_output_map { struct htlc_output_map *get_htlc_output_map(const tal_t *ctx, const struct peer *peer, const struct sha256 *rhash, - enum htlc_side side, + enum side side, unsigned int commit_num) { struct htlc_map_iter it; diff --git a/daemon/output_to_htlc.h b/daemon/output_to_htlc.h index 7d950c53b..5b99b9eb7 100644 --- a/daemon/output_to_htlc.h +++ b/daemon/output_to_htlc.h @@ -10,7 +10,7 @@ struct sha256; struct htlc_output_map *get_htlc_output_map(const tal_t *ctx, const struct peer *peer, const struct sha256 *rhash, - enum htlc_side side, + enum side side, unsigned int commit_num); /* If this scriptPubkey pays to a HTLC, get the full wscript */ diff --git a/daemon/packets.c b/daemon/packets.c index 1eaccd34f..31eb2303d 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -398,7 +398,7 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt, struct htlc **h) * A node MUST NOT add a HTLC if it would result in it * offering more than 300 HTLCs in the remote commitment transaction. */ - if (peer->remote.staging_cstate->side[THEIRS].num_htlcs == 300) + if (peer->remote.staging_cstate->side[REMOTE].num_htlcs == 300) return pkt_err(peer, "Too many HTLCs"); /* BOLT #2: diff --git a/daemon/peer.c b/daemon/peer.c index 4d844f52d..7b5d17c5e 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -652,7 +652,7 @@ static void retry_all_routing(struct peer *restarted_peer) static bool adjust_cstate_side(struct channel_state *cstate, struct htlc *h, enum htlc_state old, enum htlc_state new, - enum htlc_side side) + enum side side) { int oldf = htlc_state_flags(old), newf = htlc_state_flags(new); bool old_committed, new_committed; @@ -701,7 +701,7 @@ static void adjust_cstate_fee_side(struct channel_state *cstate, const struct feechange *f, enum feechange_state old, enum feechange_state new, - enum htlc_side side) + enum side side) { /* We applied changes to staging_cstate when we first received * feechange packet, so we could make sure it was valid. Don't @@ -1034,7 +1034,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt) peer->local.commit = ci; peer->local.commit->order = peer->order_counter++; - if (!db_new_commit_info(peer, OURS, NULL)) { + if (!db_new_commit_info(peer, LOCAL, NULL)) { db_abort_transaction(peer); return pkt_err(peer, "Database error"); } @@ -1745,7 +1745,7 @@ const char *command_htlc_add(struct peer *peer, u64 msatoshis, * A node MUST NOT add a HTLC if it would result in it * offering more than 300 HTLCs in the remote commitment transaction. */ - if (peer->remote.staging_cstate->side[OURS].num_htlcs == 300) { + if (peer->remote.staging_cstate->side[LOCAL].num_htlcs == 300) { log_unusual(peer->log, "add_htlc: fail: already at limit"); *error_code = SERVICE_UNAVAILABLE_503; return "channel full"; @@ -2236,7 +2236,7 @@ static void do_commit(struct peer *peer, struct command *jsoncmd) tal_free(peer->remote.commit); peer->remote.commit = ci; peer->remote.commit->order = peer->order_counter++; - if (!db_new_commit_info(peer, THEIRS, peer->their_prev_revocation_hash)) + if (!db_new_commit_info(peer, REMOTE, peer->their_prev_revocation_hash)) goto database_error; /* We don't need to remember their commit if we don't give sig. */ @@ -3023,7 +3023,7 @@ static bool outputscript_eq(const struct bitcoin_tx_output *out, static bool map_onchain_outputs(struct peer *peer, const struct sha256 *rhash, const struct bitcoin_tx *tx, - enum htlc_side side, + enum side side, unsigned int commit_num) { u8 *to_us, *to_them, *to_them_wscript, *to_us_wscript; @@ -3216,7 +3216,7 @@ static enum watch_result our_htlc_timeout_depth(struct peer *peer, static enum watch_result our_htlc_depth(struct peer *peer, unsigned int depth, const struct sha256_double *txid, - enum htlc_side whose_commit, + enum side whose_commit, unsigned int out_num) { struct htlc *h = peer->onchain.htlcs[out_num]; @@ -3949,8 +3949,8 @@ struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee) log_debug(peer->log, "creating close-tx with fee %"PRIu64" amounts %u/%u to ", fee, - cstate.side[OURS].pay_msat / 1000, - cstate.side[THEIRS].pay_msat / 1000); + cstate.side[LOCAL].pay_msat / 1000, + cstate.side[REMOTE].pay_msat / 1000); log_add_struct(peer->log, "%s", struct pubkey, &peer->local.finalkey); log_add_struct(peer->log, "/%s", struct pubkey, &peer->remote.finalkey); @@ -3960,8 +3960,8 @@ struct bitcoin_tx *peer_create_close_tx(struct peer *peer, u64 fee) &peer->anchor.txid, peer->anchor.index, peer->anchor.satoshis, - cstate.side[OURS].pay_msat / 1000, - cstate.side[THEIRS].pay_msat / 1000); + cstate.side[LOCAL].pay_msat / 1000, + cstate.side[REMOTE].pay_msat / 1000); } /* Creation the bitcoin anchor tx, spending output user provided. */ @@ -4035,7 +4035,7 @@ bool setup_first_commit(struct peer *peer) peer->local.commit_fee_rate, peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR ? - OURS : THEIRS); + LOCAL : REMOTE); if (!peer->local.commit->cstate) return false; @@ -4044,7 +4044,7 @@ bool setup_first_commit(struct peer *peer) peer->remote.commit_fee_rate, peer->local.offer_anchor == CMD_OPEN_WITH_ANCHOR ? - OURS : THEIRS); + LOCAL : REMOTE); if (!peer->remote.commit->cstate) return false; @@ -4171,7 +4171,7 @@ static void json_add_abstime(struct json_result *response, static void json_add_htlcs(struct json_result *response, const char *id, struct peer *peer, - enum htlc_side owner) + enum side owner) { struct htlc_map_iter it; struct htlc *h; @@ -4228,10 +4228,10 @@ static void json_getpeers(struct command *cmd, } last = p->local.commit->cstate; - json_add_num(response, "our_amount", last->side[OURS].pay_msat); - json_add_num(response, "our_fee", last->side[OURS].fee_msat); - json_add_num(response, "their_amount", last->side[THEIRS].pay_msat); - json_add_num(response, "their_fee", last->side[THEIRS].fee_msat); + json_add_num(response, "our_amount", last->side[LOCAL].pay_msat); + json_add_num(response, "our_fee", last->side[LOCAL].fee_msat); + json_add_num(response, "their_amount", last->side[REMOTE].pay_msat); + json_add_num(response, "their_fee", last->side[REMOTE].fee_msat); json_add_htlcs(response, "our_htlcs", p, LOCAL); json_add_htlcs(response, "their_htlcs", p, REMOTE); json_object_end(response); diff --git a/state.c b/state.c index 3247b85a7..4372c8823 100644 --- a/state.c +++ b/state.c @@ -164,7 +164,7 @@ enum state state(struct peer *peer, db_abort_transaction(peer); goto err_breakdown; } - if (!db_new_commit_info(peer, THEIRS, NULL)) { + if (!db_new_commit_info(peer, REMOTE, NULL)) { err = pkt_err(peer, "database error"); peer_open_complete(peer, err->error->problem); db_abort_transaction(peer); @@ -230,7 +230,7 @@ enum state state(struct peer *peer, db_abort_transaction(peer); goto err_breakdown; } - if (!db_new_commit_info(peer, OURS, NULL)) { + if (!db_new_commit_info(peer, LOCAL, NULL)) { bitcoin_release_anchor(peer, INPUT_NONE); err = pkt_err(peer, "database error"); peer_open_complete(peer, err->error->problem);