diff --git a/daemon/packets.c b/daemon/packets.c index 01fc69f8c..ee04e8282 100644 --- a/daemon/packets.c +++ b/daemon/packets.c @@ -172,7 +172,8 @@ void queue_pkt_htlc_add(struct peer *peer, u64 id, u64 msatoshis, const struct sha256 *rhash, - u32 expiry) + u32 expiry, + const u8 *route) { UpdateAddHtlc *u = tal(peer, UpdateAddHtlc); union htlc_staging stage; @@ -197,11 +198,14 @@ void queue_pkt_htlc_add(struct peer *peer, * changeset for its remote commitment */ htlc = funding_add_htlc(peer->remote.staging_cstate, - msatoshis, &locktime, rhash, id, OURS); + msatoshis, &locktime, rhash, id, + route, tal_count(route), OURS); if (!htlc) fatal("Could not add HTLC?"); stage.add.add = HTLC_ADD; + /* FIXME: This assumes stage's lifetime >= htlc, since we copy + * htlc.route pointer. Why not just make stage.add.htlc a ptr? */ stage.add.htlc = *htlc; add_unacked(&peer->remote, &stage); @@ -347,7 +351,10 @@ static void apply_changeset(struct peer *peer, changes[i].add.htlc.msatoshis, &changes[i].add.htlc.expiry, &changes[i].add.htlc.rhash, - changes[i].add.htlc.id, side)) + changes[i].add.htlc.id, + changes[i].add.htlc.routing, + tal_count(changes[i].add.htlc.routing), + side)) fatal("Adding HTLC to %s failed", side == OURS ? "ours" : "theirs"); continue; @@ -654,7 +661,9 @@ 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, - u->amount_msat, &expiry, &rhash, u->id, THEIRS); + u->amount_msat, &expiry, &rhash, u->id, + u->route->info.data, u->route->info.len, + THEIRS); /* BOLT #2: * diff --git a/daemon/peer.c b/daemon/peer.c index f1477fe55..891d68bc3 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -10,11 +10,13 @@ #include "lightningd.h" #include "log.h" #include "names.h" +#include "onion.h" #include "payment.h" #include "peer.h" #include "permute_tx.h" #include "protobuf_convert.h" #include "pseudorand.h" +#include "routing.h" #include "secrets.h" #include "state.h" #include "timeout.h" @@ -554,7 +556,8 @@ static bool command_htlc_fulfill(struct peer *peer, static bool command_htlc_add(struct peer *peer, u64 msatoshis, unsigned int expiry, - const struct sha256 *rhash) + const struct sha256 *rhash, + const u8 *route) { struct channel_state *cstate; struct abs_locktime locktime; @@ -600,7 +603,9 @@ static bool command_htlc_add(struct peer *peer, u64 msatoshis, */ cstate = copy_funding(peer, peer->remote.staging_cstate); if (!funding_add_htlc(cstate, msatoshis, - &locktime, rhash, peer->htlc_id_counter, OURS)) { + &locktime, rhash, peer->htlc_id_counter, + route, tal_count(route), + OURS)) { log_unusual(peer->log, "add_htlc: fail: Cannot afford %"PRIu64 " milli-satoshis in their commit tx", msatoshis); @@ -610,7 +615,9 @@ static bool command_htlc_add(struct peer *peer, u64 msatoshis, cstate = copy_funding(peer, peer->local.staging_cstate); if (!funding_add_htlc(cstate, msatoshis, - &locktime, rhash, peer->htlc_id_counter, OURS)) { + &locktime, rhash, peer->htlc_id_counter, + route, tal_count(route), + OURS)) { log_unusual(peer->log, "add_htlc: fail: Cannot afford %"PRIu64 " milli-satoshis in our commit tx", msatoshis); @@ -619,7 +626,7 @@ static bool command_htlc_add(struct peer *peer, u64 msatoshis, tal_free(cstate); queue_pkt_htlc_add(peer, peer->htlc_id_counter, - msatoshis, rhash, expiry); + msatoshis, rhash, expiry, route); /* Make sure we never offer the same one twice. */ peer->htlc_id_counter++; @@ -2491,6 +2498,15 @@ const struct json_command getpeers_command = { "Returns a 'peers' array" }; +/* A zero-fee single route to this peer. */ +static const u8 *dummy_single_route(const tal_t *ctx, + const struct peer *peer, + u64 msatoshis) +{ + struct node_connection **path = tal_arr(ctx, struct node_connection *, 0); + return onion_create(ctx, path, msatoshis, 0); +} + static void json_newhtlc(struct command *cmd, const char *buffer, const jsmntok_t *params) { @@ -2543,7 +2559,8 @@ static void json_newhtlc(struct command *cmd, return; } - if (!command_htlc_add(peer, msatoshis, expiry, &rhash)) { + if (!command_htlc_add(peer, msatoshis, expiry, &rhash, + dummy_single_route(cmd, peer, msatoshis))) { command_fail(cmd, "could not add htlc"); return; } diff --git a/funding.c b/funding.c index fb7610d0a..793c7c0a5 100644 --- a/funding.c +++ b/funding.c @@ -214,9 +214,11 @@ bool force_fee(struct channel_state *cstate, uint64_t fee) struct channel_htlc *funding_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash, uint64_t id, + const struct sha256 *rhash, + uint64_t id, + const u8 *routing, + size_t routing_len, enum channel_side side) - { size_t n, nondust; struct channel_oneside *creator, *recipient; @@ -240,6 +242,8 @@ struct channel_htlc *funding_add_htlc(struct channel_state *cstate, creator->htlcs[n].expiry = *expiry; creator->htlcs[n].rhash = *rhash; creator->htlcs[n].id = id; + creator->htlcs[n].routing = tal_dup_arr(cstate, u8, routing, + routing_len, 0); memcheck(&creator->htlcs[n].msatoshis, sizeof(creator->htlcs[n].msatoshis)); memcheck(&creator->htlcs[n].rhash, sizeof(creator->htlcs[n].rhash)); @@ -276,6 +280,7 @@ static void remove_htlc(struct channel_state *cstate, abort(); /* Actually remove the HTLC. */ + tal_free(htlc->routing); memmove(htlc, htlc + 1, (end - htlc - 1) * sizeof(*htlc)); tal_resize(&cstate->side[creator].htlcs, n-1); cstate->changes++; @@ -325,11 +330,19 @@ struct channel_state *copy_funding(const tal_t *ctx, const struct channel_state *cstate) { struct channel_state *cs = tal_dup(ctx, struct channel_state, cstate); - size_t i; + size_t i, j; - for (i = 0; i < ARRAY_SIZE(cs->side); i++) + for (i = 0; i < ARRAY_SIZE(cs->side); i++) { cs->side[i].htlcs = tal_dup_arr(cs, struct channel_htlc, cs->side[i].htlcs, tal_count(cs->side[i].htlcs), 0); + for (j = 0; j < tal_count(cs->side[i].htlcs); j++) { + struct channel_htlc *h = &cs->side[i].htlcs[j]; + h->routing = tal_dup_arr(cs, u8, + h->routing, + tal_count(h->routing), + 0); + } + } return cs; } diff --git a/funding.h b/funding.h index f8a84e8d6..fb74a019d 100644 --- a/funding.h +++ b/funding.h @@ -11,6 +11,7 @@ struct channel_htlc { u64 msatoshis; struct abs_locktime expiry; struct sha256 rhash; + const u8 *routing; }; struct channel_oneside { @@ -66,6 +67,8 @@ struct channel_state *copy_funding(const tal_t *ctx, * @expiry: time it expires * @rhash: hash of redeem secret * @id: 64-bit ID for htlc + * @routing: onion routing blob + * @routing_len: onion routing blob length * @side: OURS or THEIRS * * If that direction can't afford the HTLC (or still owes its half of the fees), @@ -77,7 +80,10 @@ struct channel_state *copy_funding(const tal_t *ctx, struct channel_htlc *funding_add_htlc(struct channel_state *cstate, u32 msatoshis, const struct abs_locktime *expiry, - const struct sha256 *rhash, uint64_t id, + const struct sha256 *rhash, + uint64_t id, + const u8 *routing, + size_t routing_len, enum channel_side side); /** * funding_fail_htlc: remove an HTLC, funds to the side which offered it. diff --git a/state.h b/state.h index 4f130ca57..39ec448a8 100644 --- a/state.h +++ b/state.h @@ -104,7 +104,8 @@ void queue_pkt_htlc_add(struct peer *peer, u64 id, u64 msatoshis, const struct sha256 *rhash, - u32 expiry); + u32 expiry, + const u8 *route); void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r); void queue_pkt_htlc_fail(struct peer *peer, u64 id); void queue_pkt_commit(struct peer *peer);