diff --git a/Makefile b/Makefile index 747eb871d..ada35bc6f 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,6 @@ CORE_SRC := \ close_tx.c \ commit_tx.c \ find_p2sh_out.c \ - funding.c \ lightning.pb-c.c \ opt_bits.c \ permute_tx.c \ @@ -154,7 +153,6 @@ BITCOIN_HEADERS := bitcoin/address.h \ CORE_HEADERS := close_tx.h \ commit_tx.h \ find_p2sh_out.h \ - funding.h \ names.h \ opt_bits.h \ overflows.h \ diff --git a/commit_tx.c b/commit_tx.c index db3e55927..6eff9d96e 100644 --- a/commit_tx.c +++ b/commit_tx.c @@ -4,7 +4,7 @@ #include "bitcoin/shadouble.h" #include "bitcoin/tx.h" #include "commit_tx.h" -#include "funding.h" +#include "daemon/channel.h" #include "overflows.h" #include "permute_tx.h" #include "remove_dust.h" diff --git a/commit_tx.h b/commit_tx.h index 9d3b2b74a..a94caf9b9 100644 --- a/commit_tx.h +++ b/commit_tx.h @@ -1,7 +1,7 @@ #ifndef LIGHTNING_COMMIT_TX_H #define LIGHTNING_COMMIT_TX_H #include "config.h" -#include "funding.h" +#include "daemon/channel.h" struct channel_state; struct sha256_double; diff --git a/daemon/Makefile b/daemon/Makefile index eee698adc..01a25a507 100644 --- a/daemon/Makefile +++ b/daemon/Makefile @@ -16,6 +16,7 @@ DAEMON_LIB_OBJS := $(DAEMON_LIB_SRC:.c=.o) DAEMON_SRC := \ daemon/bitcoind.c \ daemon/chaintopology.c \ + daemon/channel.c \ daemon/controlled_time.c \ daemon/cryptopkt.c \ daemon/dns.c \ @@ -46,6 +47,7 @@ DAEMON_JSMN_HEADERS := daemon/jsmn/jsmn.h DAEMON_HEADERS := \ daemon/bitcoind.h \ daemon/chaintopology.h \ + daemon/channel.h \ daemon/configdir.h \ daemon/controlled_time.h \ daemon/cryptopkt.h \ diff --git a/funding.c b/daemon/channel.c similarity index 95% rename from funding.c rename to daemon/channel.c index 793c7c0a5..466516a01 100644 --- a/funding.c +++ b/daemon/channel.c @@ -1,4 +1,4 @@ -#include "funding.h" +#include "channel.h" #include #include #include @@ -127,7 +127,7 @@ static bool change_funding(uint64_t anchor_satoshis, return true; } -struct channel_state *initial_funding(const tal_t *ctx, +struct channel_state *initial_cstate(const tal_t *ctx, uint64_t anchor_satoshis, uint32_t fee_rate, enum channel_side funding) @@ -211,7 +211,7 @@ bool force_fee(struct channel_state *cstate, uint64_t fee) } /* Add a HTLC to @creator if it can afford it. */ -struct channel_htlc *funding_add_htlc(struct channel_state *cstate, +struct channel_htlc *cstate_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, const struct sha256 *rhash, @@ -286,21 +286,21 @@ static void remove_htlc(struct channel_state *cstate, cstate->changes++; } -void funding_fail_htlc(struct channel_state *cstate, +void cstate_fail_htlc(struct channel_state *cstate, struct channel_htlc *htlc, enum channel_side side) { remove_htlc(cstate, side, side, htlc); } -void funding_fulfill_htlc(struct channel_state *cstate, +void cstate_fulfill_htlc(struct channel_state *cstate, struct channel_htlc *htlc, enum channel_side side) { remove_htlc(cstate, side, !side, htlc); } -size_t funding_find_htlc(const struct channel_state *cstate, +size_t cstate_find_htlc(const struct channel_state *cstate, const struct sha256 *rhash, enum channel_side side) { @@ -313,7 +313,7 @@ size_t funding_find_htlc(const struct channel_state *cstate, return -1; } -struct channel_htlc *funding_htlc_by_id(const struct channel_state *cstate, +struct channel_htlc *cstate_htlc_by_id(const struct channel_state *cstate, uint64_t id, enum channel_side side) { @@ -326,7 +326,7 @@ struct channel_htlc *funding_htlc_by_id(const struct channel_state *cstate, return NULL; } -struct channel_state *copy_funding(const tal_t *ctx, +struct channel_state *copy_cstate(const tal_t *ctx, const struct channel_state *cstate) { struct channel_state *cs = tal_dup(ctx, struct channel_state, cstate); diff --git a/funding.h b/daemon/channel.h similarity index 77% rename from funding.h rename to daemon/channel.h index fb74a019d..e1909350a 100644 --- a/funding.h +++ b/daemon/channel.h @@ -1,5 +1,5 @@ -#ifndef LIGHTNING_FUNDING_H -#define LIGHTNING_FUNDING_H +#ifndef LIGHTNING_DAEMON_CHANNEL_H +#define LIGHTNING_DAEMON_CHANNEL_H #include "config.h" #include "bitcoin/locktime.h" #include @@ -39,7 +39,7 @@ struct channel_state { }; /** - * initial_funding: Given initial fees and funding anchor, what is initial state? + * initial_cstate: Given initial fees and funding anchor, what is initial state? * @ctx: tal context to allocate return value from. * @anchor_satoshis: The anchor amount. * @fee_rate: amount to pay in fees per kb (in satoshi). @@ -47,21 +47,21 @@ struct channel_state { * * Returns state, or NULL if malformed. */ -struct channel_state *initial_funding(const tal_t *ctx, - uint64_t anchor_satoshis, - uint32_t fee_rate, - enum channel_side side); +struct channel_state *initial_cstate(const tal_t *ctx, + uint64_t anchor_satoshis, + uint32_t fee_rate, + enum channel_side side); /** - * copy_funding: Make a deep copy of channel_state + * copy_cstate: Make a deep copy of channel_state * @ctx: tal context to allocate return value from. * @cstate: state to copy. */ -struct channel_state *copy_funding(const tal_t *ctx, - const struct channel_state *cstate); +struct channel_state *copy_cstate(const tal_t *ctx, + const struct channel_state *cstate); /** - * funding_add_htlc: append an HTLC to cstate if it can afford it + * cstate_add_htlc: append an HTLC to cstate if it can afford it * @cstate: The channel state * @msatoshis: Millisatoshi going into a HTLC * @expiry: time it expires @@ -77,7 +77,7 @@ struct channel_state *copy_funding(const tal_t *ctx, * fee_msat are adjusted accordingly; &cstate->side[dir].htlcs[] * is returned. */ -struct channel_htlc *funding_add_htlc(struct channel_state *cstate, +struct channel_htlc *cstate_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, const struct sha256 *rhash, @@ -86,7 +86,7 @@ struct channel_htlc *funding_add_htlc(struct channel_state *cstate, size_t routing_len, enum channel_side side); /** - * funding_fail_htlc: remove an HTLC, funds to the side which offered it. + * cstate_fail_htlc: remove an HTLC, funds to the side which offered it. * @cstate: The channel state * @htlc: the htlc in cstate->side[dir].htlcs[]. * @side: OURS or THEIRS @@ -94,12 +94,12 @@ struct channel_htlc *funding_add_htlc(struct channel_state *cstate, * This will remove the @index'th entry in cstate->side[dir].htlcs[], and credit * the value of the HTLC (back) to cstate->side[dir]. */ -void funding_fail_htlc(struct channel_state *cstate, +void cstate_fail_htlc(struct channel_state *cstate, struct channel_htlc *htlc, enum channel_side side); /** - * funding_fulfill_htlc: remove an HTLC, funds to side which accepted it. + * cstate_fulfill_htlc: remove an HTLC, funds to side which accepted it. * @cstate: The channel state * @htlc: the htlc in cstate->side[dir].htlcs[]. * @side: OURS or THEIRS @@ -107,7 +107,7 @@ void funding_fail_htlc(struct channel_state *cstate, * This will remove the @index'th entry in cstate->side[dir].htlcs[], and credit * the value of the HTLC to cstate->side[!dir]. */ -void funding_fulfill_htlc(struct channel_state *cstate, +void cstate_fulfill_htlc(struct channel_state *cstate, struct channel_htlc *htlc, enum channel_side side); @@ -130,26 +130,26 @@ void adjust_fee(struct channel_state *cstate, uint32_t fee_rate); bool force_fee(struct channel_state *cstate, uint64_t fee); /** - * funding_find_htlc: find an HTLC on this side of the channel. + * cstate_find_htlc: find an HTLC on this side of the channel. * @cstate: The channel state * @rhash: hash of redeem secret * @side: OURS or THEIRS * * Returns a number < tal_count(cstate->side[dir].htlcs), or -1 on fail. */ -size_t funding_find_htlc(const struct channel_state *cstate, +size_t cstate_find_htlc(const struct channel_state *cstate, const struct sha256 *rhash, enum channel_side side); /** - * funding_htlc_by_id: find an HTLC on this side of the channel by ID. + * cstate_htlc_by_id: find an HTLC on this side of the channel by ID. * @cstate: The channel state * @id: id for HTLC. * @side: OURS or THEIRS * * Returns a pointer into cstate->side[@side].htlcs, or NULL. */ -struct channel_htlc *funding_htlc_by_id(const struct channel_state *cstate, +struct channel_htlc *cstate_htlc_by_id(const struct channel_state *cstate, uint64_t id, enum channel_side side); @@ -169,4 +169,4 @@ uint64_t fee_by_feerate(size_t txsize, uint32_t fee_rate); */ bool is_dust_amount(uint64_t satoshis); -#endif /* LIGHTNING_FUNDING_H */ +#endif /* LIGHTNING_DAEMON_CHANNEL_H */ diff --git a/daemon/packets.c b/daemon/packets.c index 0ff3ffddc..9a915ad23 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -198,7 +198,7 @@ void queue_pkt_htlc_add(struct peer *peer, * The sending node MUST add the HTLC addition to the unacked * changeset for its remote commitment */ - htlc = funding_add_htlc(peer->remote.staging_cstate, + htlc = cstate_add_htlc(peer->remote.staging_cstate, msatoshis, &locktime, rhash, id, route, tal_count(route), OURS); if (!htlc) @@ -230,9 +230,9 @@ void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r) * The sending node MUST add the HTLC fulfill/fail to the * unacked changeset for its remote commitment */ - htlc = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS); + htlc = cstate_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS); assert(htlc); - funding_fulfill_htlc(peer->remote.staging_cstate, htlc, THEIRS); + cstate_fulfill_htlc(peer->remote.staging_cstate, htlc, THEIRS); stage.fulfill.fulfill = HTLC_FULFILL; stage.fulfill.id = f->id; @@ -262,9 +262,9 @@ void queue_pkt_htlc_fail(struct peer *peer, u64 id) * The sending node MUST add the HTLC fulfill/fail to the * unacked changeset for its remote commitment */ - htlc = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS); + htlc = cstate_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS); assert(htlc); - funding_fail_htlc(peer->remote.staging_cstate, htlc, THEIRS); + cstate_fail_htlc(peer->remote.staging_cstate, htlc, THEIRS); stage.fail.fail = HTLC_FAIL; stage.fail.id = f->id; @@ -289,7 +289,7 @@ void queue_pkt_commit(struct peer *peer) * A sending node MUST apply all remote acked and unacked * changes except unacked fee changes to the remote commitment * before generating `sig`. */ - ci->cstate = copy_funding(ci, peer->remote.staging_cstate); + ci->cstate = copy_cstate(ci, peer->remote.staging_cstate); ci->tx = create_commit_tx(ci, &peer->local.finalkey, &peer->remote.finalkey, @@ -343,12 +343,12 @@ static void apply_changeset(struct peer *peer, for (i = 0; i < num_changes; i++) { switch (changes[i].type) { case HTLC_ADD: - htlc = funding_htlc_by_id(which->staging_cstate, + htlc = cstate_htlc_by_id(which->staging_cstate, changes[i].add.htlc.id, side); if (htlc) fatal("Can't add duplicate HTLC id %"PRIu64, changes[i].add.htlc.id); - if (!funding_add_htlc(which->staging_cstate, + if (!cstate_add_htlc(which->staging_cstate, changes[i].add.htlc.msatoshis, &changes[i].add.htlc.expiry, &changes[i].add.htlc.rhash, @@ -360,20 +360,20 @@ static void apply_changeset(struct peer *peer, side == OURS ? "ours" : "theirs"); continue; case HTLC_FAIL: - htlc = funding_htlc_by_id(which->staging_cstate, + htlc = cstate_htlc_by_id(which->staging_cstate, changes[i].fail.id, !side); if (!htlc) fatal("Can't fail non-exisent HTLC id %"PRIu64, changes[i].fail.id); - funding_fail_htlc(which->staging_cstate, htlc, !side); + cstate_fail_htlc(which->staging_cstate, htlc, !side); continue; case HTLC_FULFILL: - htlc = funding_htlc_by_id(which->staging_cstate, + htlc = cstate_htlc_by_id(which->staging_cstate, changes[i].fulfill.id, !side); if (!htlc) fatal("Can't fulfill non-exisent HTLC id %"PRIu64, changes[i].fulfill.id); - funding_fulfill_htlc(which->staging_cstate, htlc, !side); + cstate_fulfill_htlc(which->staging_cstate, htlc, !side); continue; } abort(); @@ -647,13 +647,13 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt) /* Note that it's not *our* problem if they do this, it's * theirs (future confusion). Nonetheless, we detect and * error for them. */ - if (funding_htlc_by_id(peer->remote.staging_cstate, u->id, THEIRS) - || funding_htlc_by_id(peer->remote.commit->cstate, u->id, THEIRS)) { + if (cstate_htlc_by_id(peer->remote.staging_cstate, u->id, THEIRS) + || cstate_htlc_by_id(peer->remote.commit->cstate, u->id, THEIRS)) { return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id); } - if (funding_htlc_by_id(peer->local.staging_cstate, u->id, THEIRS) - || funding_htlc_by_id(peer->local.commit->cstate, u->id, THEIRS)) { + if (cstate_htlc_by_id(peer->local.staging_cstate, u->id, THEIRS) + || cstate_htlc_by_id(peer->local.commit->cstate, u->id, THEIRS)) { return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id); } @@ -661,7 +661,7 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt) * * ...and the receiving node MUST add the HTLC addition to the * unacked changeset for its local commitment. */ - htlc = funding_add_htlc(peer->local.staging_cstate, + htlc = cstate_add_htlc(peer->local.staging_cstate, u->amount_msat, &expiry, &rhash, u->id, u->route->info.data, u->route->info.len, THEIRS); @@ -701,12 +701,12 @@ static Pkt *find_commited_htlc(struct peer *peer, uint64_t id, * current commitment transaction, and MUST fail the * connection if it does not. */ - htlc = funding_htlc_by_id(peer->local.commit->cstate, id, OURS); + htlc = cstate_htlc_by_id(peer->local.commit->cstate, id, OURS); if (!htlc) return pkt_err(peer, "Did not find HTLC %"PRIu64, id); /* They must not fail/fulfill twice, so it should be in staging, too. */ - *local_htlc = funding_htlc_by_id(peer->local.staging_cstate, id, OURS); + *local_htlc = cstate_htlc_by_id(peer->local.staging_cstate, id, OURS); if (!*local_htlc) return pkt_err(peer, "Already removed HTLC %"PRIu64, id); @@ -726,7 +726,7 @@ Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt) /* FIXME: Save reason. */ - funding_fail_htlc(peer->local.staging_cstate, htlc, OURS); + cstate_fail_htlc(peer->local.staging_cstate, htlc, OURS); /* BOLT #2: * @@ -764,7 +764,7 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt) * ... and the receiving node MUST add the HTLC fulfill/fail * to the unacked changeset for its local commitment. */ - funding_fulfill_htlc(peer->local.staging_cstate, htlc, OURS); + cstate_fulfill_htlc(peer->local.staging_cstate, htlc, OURS); stage.fulfill.fulfill = HTLC_FULFILL; stage.fulfill.id = f->id; @@ -790,7 +790,7 @@ Pkt *accept_pkt_commit(struct peer *peer, const Pkt *pkt) * changes except unacked fee changes to the local commitment */ /* (We already applied them to staging_cstate as we went) */ - ci->cstate = copy_funding(ci, peer->local.staging_cstate); + ci->cstate = copy_cstate(ci, peer->local.staging_cstate); ci->tx = create_commit_tx(ci, &peer->local.finalkey, &peer->remote.finalkey, diff --git a/daemon/peer.c b/daemon/peer.c index 2a04654b1..698651af6 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -602,8 +602,8 @@ static bool command_htlc_add(struct peer *peer, u64 msatoshis, * A node MUST NOT offer `amount_msat` it cannot pay for in * both commitment transactions at the current `fee_rate` */ - cstate = copy_funding(peer, peer->remote.staging_cstate); - if (!funding_add_htlc(cstate, msatoshis, + cstate = copy_cstate(peer, peer->remote.staging_cstate); + if (!cstate_add_htlc(cstate, msatoshis, &locktime, rhash, peer->htlc_id_counter, route, tal_count(route), OURS)) { @@ -614,8 +614,8 @@ static bool command_htlc_add(struct peer *peer, u64 msatoshis, } tal_free(cstate); - cstate = copy_funding(peer, peer->local.staging_cstate); - if (!funding_add_htlc(cstate, msatoshis, + cstate = copy_cstate(peer, peer->local.staging_cstate); + if (!cstate_add_htlc(cstate, msatoshis, &locktime, rhash, peer->htlc_id_counter, route, tal_count(route), OURS)) { @@ -2359,35 +2359,35 @@ void peer_both_committed_to(struct peer *peer, type = "ADD"; htlc_id = changes[i].add.htlc.id; owner = owner_name(side); - assert(funding_htlc_by_id(peer->remote.commit->cstate, htlc_id, + assert(cstate_htlc_by_id(peer->remote.commit->cstate, htlc_id, side)); - assert(funding_htlc_by_id(peer->local.commit->cstate, htlc_id, + assert(cstate_htlc_by_id(peer->local.commit->cstate, htlc_id, side)); goto print; case HTLC_FAIL: type = "FAIL"; htlc_id = changes[i].fail.id; owner = owner_name(!side); - assert(!funding_htlc_by_id(peer->remote.commit->cstate, htlc_id, + assert(!cstate_htlc_by_id(peer->remote.commit->cstate, htlc_id, !side)); - assert(!funding_htlc_by_id(peer->local.commit->cstate, htlc_id, + assert(!cstate_htlc_by_id(peer->local.commit->cstate, htlc_id, !side)); - assert(funding_htlc_by_id(peer->remote.commit->prev->cstate, + assert(cstate_htlc_by_id(peer->remote.commit->prev->cstate, htlc_id, !side) - || funding_htlc_by_id(peer->local.commit->prev->cstate, + || cstate_htlc_by_id(peer->local.commit->prev->cstate, htlc_id, !side)); goto print; case HTLC_FULFILL: type = "FULFILL"; htlc_id = changes[i].fulfill.id; owner = owner_name(!side); - assert(!funding_htlc_by_id(peer->remote.commit->cstate, htlc_id, + assert(!cstate_htlc_by_id(peer->remote.commit->cstate, htlc_id, !side)); - assert(!funding_htlc_by_id(peer->local.commit->cstate, htlc_id, + assert(!cstate_htlc_by_id(peer->local.commit->cstate, htlc_id, !side)); - assert(funding_htlc_by_id(peer->remote.commit->prev->cstate, + assert(cstate_htlc_by_id(peer->remote.commit->prev->cstate, htlc_id, !side) - || funding_htlc_by_id(peer->local.commit->prev->cstate, + || cstate_htlc_by_id(peer->local.commit->prev->cstate, htlc_id, !side)); goto print; } @@ -2424,7 +2424,7 @@ bool setup_first_commit(struct peer *peer) assert(!peer->remote.commit->tx); /* Revocation hashes already filled in, from pkt_open */ - peer->local.commit->cstate = initial_funding(peer, + peer->local.commit->cstate = initial_cstate(peer, peer->anchor.satoshis, peer->local.commit_fee_rate, peer->local.offer_anchor @@ -2433,7 +2433,7 @@ bool setup_first_commit(struct peer *peer) if (!peer->local.commit->cstate) return false; - peer->remote.commit->cstate = initial_funding(peer, + peer->remote.commit->cstate = initial_cstate(peer, peer->anchor.satoshis, peer->remote.commit_fee_rate, peer->local.offer_anchor @@ -2468,8 +2468,8 @@ bool setup_first_commit(struct peer *peer) THEIRS, &peer->remote.commit->map); - peer->local.staging_cstate = copy_funding(peer, peer->local.commit->cstate); - peer->remote.staging_cstate = copy_funding(peer, peer->remote.commit->cstate); + peer->local.staging_cstate = copy_cstate(peer, peer->local.commit->cstate); + peer->remote.staging_cstate = copy_cstate(peer, peer->remote.commit->cstate); return true; } @@ -2652,10 +2652,10 @@ static size_t find_their_committed_htlc(struct peer *peer, const struct sha256 *rhash) { /* Must be in last committed cstate. */ - if (funding_find_htlc(peer->remote.commit->cstate, rhash, THEIRS) == -1) + if (cstate_find_htlc(peer->remote.commit->cstate, rhash, THEIRS) == -1) return -1; - return funding_find_htlc(peer->remote.staging_cstate, rhash, THEIRS); + return cstate_find_htlc(peer->remote.staging_cstate, rhash, THEIRS); } static void json_fulfillhtlc(struct command *cmd, diff --git a/daemon/peer.h b/daemon/peer.h index 70c2b01c2..3aee2528a 100644 --- a/daemon/peer.h +++ b/daemon/peer.h @@ -6,7 +6,7 @@ #include "bitcoin/pubkey.h" #include "bitcoin/script.h" #include "bitcoin/shadouble.h" -#include "funding.h" +#include "channel.h" #include "lightning.pb-c.h" #include "netaddr.h" #include "protobuf_convert.h"