funding: add 64-bit id to HTLCs.

This gives us a reliable way to distinguish HTLCs, even in the face of
duplicate R values.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-03-30 16:55:03 +10:30
parent b6943b9198
commit 8ed68179a5
4 changed files with 33 additions and 9 deletions

View File

@ -449,7 +449,7 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx,
if (!funding_b_add_htlc(cur->cstate,
cur->stage.add.htlc.msatoshis,
&cur->stage.add.htlc.expiry,
&cur->stage.add.htlc.rhash)) {
&cur->stage.add.htlc.rhash, 0)) {
err = pkt_err(ctx, "Cannot afford %"PRIu64" milli-satoshis",
cur->stage.add.htlc.msatoshis);
goto fail;

View File

@ -1473,7 +1473,7 @@ static void do_newhtlc(struct peer *peer, struct newhtlc *newhtlc)
* execute. */
cstate = copy_funding(newhtlc, peer->cstate);
if (!funding_a_add_htlc(cstate, newhtlc->htlc.msatoshis,
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash)) {
&newhtlc->htlc.expiry, &newhtlc->htlc.rhash, 0)) {
command_fail(newhtlc->jsoncmd,
"Cannot afford %"PRIu64" milli-satoshis",
newhtlc->htlc.msatoshis);

View File

@ -224,7 +224,7 @@ static bool add_htlc(const struct channel_state *cstate,
struct channel_oneside *creator,
struct channel_oneside *recipient,
u32 msatoshis, const struct abs_locktime *expiry,
const struct sha256 *rhash)
const struct sha256 *rhash, uint64_t id)
{
size_t n, nondust;
@ -246,6 +246,7 @@ static bool add_htlc(const struct channel_state *cstate,
creator->htlcs[n].msatoshis = msatoshis;
creator->htlcs[n].expiry = *expiry;
creator->htlcs[n].rhash = *rhash;
creator->htlcs[n].id = id;
memcheck(&creator->htlcs[n].msatoshis,
sizeof(creator->htlcs[n].msatoshis));
memcheck(&creator->htlcs[n].rhash, sizeof(creator->htlcs[n].rhash));
@ -288,18 +289,18 @@ static void remove_htlc(const struct channel_state *cstate,
bool funding_a_add_htlc(struct channel_state *cstate,
u32 msatoshis, const struct abs_locktime *expiry,
const struct sha256 *rhash)
const struct sha256 *rhash, uint64_t id)
{
return add_htlc(cstate, &cstate->a, &cstate->b,
msatoshis, expiry, rhash);
msatoshis, expiry, rhash, id);
}
bool funding_b_add_htlc(struct channel_state *cstate,
u32 msatoshis, const struct abs_locktime *expiry,
const struct sha256 *rhash)
const struct sha256 *rhash, uint64_t id)
{
return add_htlc(cstate, &cstate->b, &cstate->a,
msatoshis, expiry, rhash);
msatoshis, expiry, rhash, id);
}
void funding_a_fail_htlc(struct channel_state *cstate, size_t index)
@ -334,6 +335,17 @@ size_t funding_find_htlc(struct channel_oneside *creator,
return i;
}
size_t funding_htlc_by_id(struct channel_oneside *creator, uint64_t id)
{
size_t i;
for (i = 0; i < tal_count(creator->htlcs); i++) {
if (creator->htlcs[i].id == id)
break;
}
return i;
}
struct channel_state *copy_funding(const tal_t *ctx,
const struct channel_state *cstate)
{

View File

@ -7,6 +7,7 @@
#include <stdbool.h>
struct channel_htlc {
u64 id;
u64 msatoshis;
struct abs_locktime expiry;
struct sha256 rhash;
@ -55,6 +56,7 @@ struct channel_state *copy_funding(const tal_t *ctx,
* @msatoshis: Millisatoshi A is putting into a HTLC
* @expiry: time it expires
* @rhash: hash of redeem secret
* @id: 64-bit ID for htlc
*
* If A can't afford the HTLC (or still owes its half of the fees),
* this will return false and leave @cstate unchanged. Otherwise
@ -63,11 +65,11 @@ struct channel_state *copy_funding(const tal_t *ctx,
*/
bool funding_a_add_htlc(struct channel_state *cstate,
u32 msatoshis, const struct abs_locktime *expiry,
const struct sha256 *rhash);
const struct sha256 *rhash, uint64_t id);
bool funding_b_add_htlc(struct channel_state *cstate,
u32 msatoshis, const struct abs_locktime *expiry,
const struct sha256 *rhash);
const struct sha256 *rhash, uint64_t id);
/**
* funding_a_fail_htlc: remove an HTLC from A's side of cstate, funds to A
@ -126,6 +128,16 @@ void invert_cstate(struct channel_state *cstate);
size_t funding_find_htlc(struct channel_oneside *creator,
const struct sha256 *rhash);
/**
* funding_htlc_by_id: find an HTLC on this side of the channel by ID.
* @creator: channel_state->a or channel_state->b, whichever originated htlc
* @id: id for HTLC.
*
* Returns a number < tal_count(creator->htlcs), or == tal_count(creator->htlcs)
* on fail.
*/
size_t funding_htlc_by_id(struct channel_oneside *creator, uint64_t id);
/**
* fee_for_feerate: calculate the fee (in satoshi) for a given fee_rate.
* @txsize: transaction size in bytes.