funding: make funding_htlc_by_id() return pointer, not offset.

While the pointer is only valid until the funding changes, that's also
true of the offset; and a pointer has a convenient "not found"
sentinal value.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-06-29 06:49:20 +09:30
parent 156d1be9ed
commit eeb9b9de84
3 changed files with 67 additions and 61 deletions

View File

@@ -208,7 +208,7 @@ void queue_pkt_htlc_add(struct peer *peer, const struct channel_htlc *htlc)
void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r)
{
UpdateFulfillHtlc *f = tal(peer, UpdateFulfillHtlc);
size_t n;
struct channel_htlc *htlc;
union htlc_staging stage;
update_fulfill_htlc__init(f);
@@ -220,9 +220,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
*/
n = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS);
assert(n != -1);
funding_fulfill_htlc(peer->remote.staging_cstate, n, THEIRS);
htlc = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS);
assert(htlc);
funding_fulfill_htlc(peer->remote.staging_cstate, htlc, THEIRS);
stage.fulfill.fulfill = HTLC_FULFILL;
stage.fulfill.id = f->id;
@@ -237,7 +237,7 @@ void queue_pkt_htlc_fulfill(struct peer *peer, u64 id, const struct rval *r)
void queue_pkt_htlc_fail(struct peer *peer, u64 id)
{
UpdateFailHtlc *f = tal(peer, UpdateFailHtlc);
size_t n;
struct channel_htlc *htlc;
union htlc_staging stage;
update_fail_htlc__init(f);
@@ -252,9 +252,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
*/
n = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS);
assert(n != -1);
funding_fail_htlc(peer->remote.staging_cstate, n, THEIRS);
htlc = funding_htlc_by_id(peer->remote.staging_cstate, f->id, THEIRS);
assert(htlc);
funding_fail_htlc(peer->remote.staging_cstate, htlc, THEIRS);
stage.fail.fail = HTLC_FAIL;
stage.fail.id = f->id;
@@ -328,14 +328,14 @@ static void apply_changeset(struct peer *peer,
size_t num_changes)
{
size_t i;
size_t n;
struct channel_htlc *htlc;
for (i = 0; i < num_changes; i++) {
switch (changes[i].type) {
case HTLC_ADD:
n = funding_htlc_by_id(which->staging_cstate,
changes[i].add.htlc.id, side);
if (n != -1)
htlc = funding_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,
@@ -347,20 +347,20 @@ static void apply_changeset(struct peer *peer,
side == OURS ? "ours" : "theirs");
continue;
case HTLC_FAIL:
n = funding_htlc_by_id(which->staging_cstate,
htlc = funding_htlc_by_id(which->staging_cstate,
changes[i].fail.id, !side);
if (n == -1)
if (!htlc)
fatal("Can't fail non-exisent HTLC id %"PRIu64,
changes[i].fail.id);
funding_fail_htlc(which->staging_cstate, n, !side);
funding_fail_htlc(which->staging_cstate, htlc, !side);
continue;
case HTLC_FULFILL:
n = funding_htlc_by_id(which->staging_cstate,
changes[i].fulfill.id, !side);
if (n == -1)
htlc = funding_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, n, !side);
funding_fulfill_htlc(which->staging_cstate, htlc, !side);
continue;
}
abort();
@@ -628,13 +628,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) != -1
|| funding_htlc_by_id(peer->remote.commit->cstate, u->id, THEIRS) != -1) {
if (funding_htlc_by_id(peer->remote.staging_cstate, u->id, THEIRS)
|| funding_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) != -1
|| funding_htlc_by_id(peer->local.commit->cstate, u->id, THEIRS) != -1) {
if (funding_htlc_by_id(peer->local.staging_cstate, u->id, THEIRS)
|| funding_htlc_by_id(peer->local.commit->cstate, u->id, THEIRS)) {
return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id);
}
@@ -672,9 +672,10 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
return NULL;
}
static Pkt *find_commited_htlc(struct peer *peer, uint64_t id, size_t *n_local)
static Pkt *find_commited_htlc(struct peer *peer, uint64_t id,
struct channel_htlc **local_htlc)
{
size_t n;
struct channel_htlc *htlc;
/* BOLT #2:
*
@@ -682,13 +683,13 @@ static Pkt *find_commited_htlc(struct peer *peer, uint64_t id, size_t *n_local)
* current commitment transaction, and MUST fail the
* connection if it does not.
*/
n = funding_htlc_by_id(peer->local.commit->cstate, id, OURS);
if (n == -1)
htlc = funding_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. */
*n_local = funding_htlc_by_id(peer->local.staging_cstate, id, OURS);
if (*n_local == -1)
*local_htlc = funding_htlc_by_id(peer->local.staging_cstate, id, OURS);
if (!*local_htlc)
return pkt_err(peer, "Already removed HTLC %"PRIu64, id);
return NULL;
@@ -697,17 +698,17 @@ static Pkt *find_commited_htlc(struct peer *peer, uint64_t id, size_t *n_local)
Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
{
const UpdateFailHtlc *f = pkt->update_fail_htlc;
size_t n_local;
struct channel_htlc *htlc;
Pkt *err;
union htlc_staging stage;
err = find_commited_htlc(peer, f->id, &n_local);
err = find_commited_htlc(peer, f->id, &htlc);
if (err)
return err;
/* FIXME: Save reason. */
funding_fail_htlc(peer->local.staging_cstate, n_local, OURS);
funding_fail_htlc(peer->local.staging_cstate, htlc, OURS);
/* BOLT #2:
*
@@ -723,13 +724,13 @@ Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
{
const UpdateFulfillHtlc *f = pkt->update_fulfill_htlc;
size_t n_local;
struct channel_htlc *htlc;
struct sha256 rhash;
struct rval r;
Pkt *err;
union htlc_staging stage;
err = find_commited_htlc(peer, f->id, &n_local);
err = find_commited_htlc(peer, f->id, &htlc);
if (err)
return err;
@@ -737,7 +738,7 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
proto_to_rval(f->r, &r);
sha256(&rhash, &r, sizeof(r));
if (!structeq(&rhash, &peer->local.staging_cstate->side[OURS].htlcs[n_local].rhash))
if (!structeq(&rhash, &htlc->rhash))
return pkt_err(peer, "Invalid r for %"PRIu64, f->id);
/* BOLT #2:
@@ -745,7 +746,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, n_local, OURS);
funding_fulfill_htlc(peer->local.staging_cstate, htlc, OURS);
stage.fulfill.fulfill = HTLC_FULFILL;
stage.fulfill.id = f->id;