Skip to content

Commit cf7a7a7

Browse files
committed
funding: use sides[OURS/THEIRS] instead of a and b.
This is a little clearer, and handling arrays is easier than separate variables. Signed-off-by: Rusty Russell <[email protected]>
1 parent 84f5a82 commit cf7a7a7

File tree

5 files changed

+227
-238
lines changed

5 files changed

+227
-238
lines changed

commit_tx.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
5252
uint64_t total;
5353

5454
/* Now create commitment tx: one input, two outputs (plus htlcs) */
55-
tx = bitcoin_tx(ctx, 1, 2 + tal_count(cstate->a.htlcs)
56-
+ tal_count(cstate->b.htlcs));
55+
tx = bitcoin_tx(ctx, 1, 2 + tal_count(cstate->side[OURS].htlcs)
56+
+ tal_count(cstate->side[THEIRS].htlcs));
5757

5858
/* Our input spends the anchor tx output. */
5959
tx->input[0].txid = *anchor_txid;
@@ -67,28 +67,28 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
6767
rhash);
6868
tx->output[0].script = scriptpubkey_p2wsh(tx, redeemscript);
6969
tx->output[0].script_length = tal_count(tx->output[0].script);
70-
tx->output[0].amount = cstate->a.pay_msat / 1000;
70+
tx->output[0].amount = cstate->side[OURS].pay_msat / 1000;
7171

7272
/* Second output is a P2WPKH payment to them. */
7373
tx->output[1].script = scriptpubkey_p2wpkh(tx, their_final);
7474
tx->output[1].script_length = tal_count(tx->output[1].script);
75-
tx->output[1].amount = cstate->b.pay_msat / 1000;
75+
tx->output[1].amount = cstate->side[THEIRS].pay_msat / 1000;
7676

7777
/* First two outputs done, now for the HTLCs. */
7878
total = tx->output[0].amount + tx->output[1].amount;
7979
num = 2;
8080

8181
/* HTLCs we've sent. */
82-
for (i = 0; i < tal_count(cstate->a.htlcs); i++) {
83-
if (!add_htlc(tx, num, &cstate->a.htlcs[i],
82+
for (i = 0; i < tal_count(cstate->side[OURS].htlcs); i++) {
83+
if (!add_htlc(tx, num, &cstate->side[OURS].htlcs[i],
8484
our_final, their_final,
8585
rhash, their_locktime, bitcoin_redeem_htlc_send))
8686
return tal_free(tx);
8787
total += tx->output[num++].amount;
8888
}
8989
/* HTLCs we've received. */
90-
for (i = 0; i < tal_count(cstate->b.htlcs); i++) {
91-
if (!add_htlc(tx, num, &cstate->b.htlcs[i],
90+
for (i = 0; i < tal_count(cstate->side[THEIRS].htlcs); i++) {
91+
if (!add_htlc(tx, num, &cstate->side[THEIRS].htlcs[i],
9292
our_final, their_final,
9393
rhash, their_locktime, bitcoin_redeem_htlc_recv))
9494
return tal_free(tx);

daemon/packets.c

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ static void add_our_htlc_ourside(struct peer *peer, void *arg)
184184
struct channel_htlc *htlc = arg;
185185

186186
/* FIXME: must add even if can't pay fee any more! */
187-
if (!funding_a_add_htlc(peer->local.staging_cstate,
188-
htlc->msatoshis, &htlc->expiry,
189-
&htlc->rhash, htlc->id))
187+
if (!funding_add_htlc(peer->local.staging_cstate,
188+
htlc->msatoshis, &htlc->expiry,
189+
&htlc->rhash, htlc->id, OURS))
190190
fatal("FIXME: Failed to add htlc %"PRIu64" to self on ack",
191191
htlc->id);
192192
tal_free(htlc);
@@ -209,11 +209,11 @@ void queue_pkt_htlc_add(struct peer *peer,
209209
routing__init(u->route);
210210

211211
/* We're about to send this, so their side will have it from now on. */
212-
if (!funding_b_add_htlc(peer->remote.staging_cstate,
213-
htlc_prog->stage.add.htlc.msatoshis,
214-
&htlc_prog->stage.add.htlc.expiry,
215-
&htlc_prog->stage.add.htlc.rhash,
216-
htlc_prog->stage.add.htlc.id))
212+
if (!funding_add_htlc(peer->remote.staging_cstate,
213+
htlc_prog->stage.add.htlc.msatoshis,
214+
&htlc_prog->stage.add.htlc.expiry,
215+
&htlc_prog->stage.add.htlc.rhash,
216+
htlc_prog->stage.add.htlc.id, THEIRS))
217217
fatal("Could not add HTLC?");
218218

219219
peer_add_htlc_expiry(peer, &htlc_prog->stage.add.htlc.expiry);
@@ -230,8 +230,9 @@ static void fulfill_their_htlc_ourside(struct peer *peer, void *arg)
230230
{
231231
size_t n;
232232

233-
n = funding_htlc_by_id(&peer->local.staging_cstate->b, ptr2int(arg));
234-
funding_b_fulfill_htlc(peer->local.staging_cstate, n);
233+
n = funding_htlc_by_id(peer->local.staging_cstate, ptr2int(arg), THEIRS);
234+
assert(n != -1);
235+
funding_fulfill_htlc(peer->local.staging_cstate, n, THEIRS);
235236
}
236237

237238
void queue_pkt_htlc_fulfill(struct peer *peer,
@@ -247,8 +248,9 @@ void queue_pkt_htlc_fulfill(struct peer *peer,
247248
f->r = sha256_to_proto(f, &htlc_prog->stage.fulfill.r);
248249

249250
/* We're about to send this, so their side will have it from now on. */
250-
n = funding_htlc_by_id(&peer->remote.staging_cstate->a, f->id);
251-
funding_a_fulfill_htlc(peer->remote.staging_cstate, n);
251+
n = funding_htlc_by_id(peer->remote.staging_cstate, f->id, OURS);
252+
assert(n != -1);
253+
funding_fulfill_htlc(peer->remote.staging_cstate, n, OURS);
252254
their_commit_changed(peer);
253255

254256
queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FULFILL_HTLC, f,
@@ -260,8 +262,9 @@ static void fail_their_htlc_ourside(struct peer *peer, void *arg)
260262
{
261263
size_t n;
262264

263-
n = funding_htlc_by_id(&peer->local.staging_cstate->b, ptr2int(arg));
264-
funding_b_fail_htlc(peer->local.staging_cstate, n);
265+
n = funding_htlc_by_id(peer->local.staging_cstate, ptr2int(arg), THEIRS);
266+
assert(n != -1);
267+
funding_fail_htlc(peer->local.staging_cstate, n, THEIRS);
265268
}
266269

267270
void queue_pkt_htlc_fail(struct peer *peer,
@@ -279,8 +282,9 @@ void queue_pkt_htlc_fail(struct peer *peer,
279282
fail_reason__init(f->reason);
280283

281284
/* We're about to send this, so their side will have it from now on. */
282-
n = funding_htlc_by_id(&peer->remote.staging_cstate->a, f->id);
283-
funding_a_fail_htlc(peer->remote.staging_cstate, n);
285+
n = funding_htlc_by_id(peer->remote.staging_cstate, f->id, OURS);
286+
assert(n != -1);
287+
funding_fail_htlc(peer->remote.staging_cstate, n, OURS);
284288

285289
their_commit_changed(peer);
286290
queue_pkt_with_ack(peer, PKT__PKT_UPDATE_FAIL_HTLC, f,
@@ -309,10 +313,10 @@ void queue_pkt_commit(struct peer *peer)
309313
&ci->map);
310314

311315
log_debug(peer->log, "Signing tx for %u/%u msatoshis, %zu/%zu htlcs",
312-
ci->cstate->a.pay_msat,
313-
ci->cstate->b.pay_msat,
314-
tal_count(ci->cstate->a.htlcs),
315-
tal_count(ci->cstate->b.htlcs));
316+
ci->cstate->side[OURS].pay_msat,
317+
ci->cstate->side[THEIRS].pay_msat,
318+
tal_count(ci->cstate->side[OURS].htlcs),
319+
tal_count(ci->cstate->side[THEIRS].htlcs));
316320

317321
/* BOLT #2:
318322
*
@@ -563,23 +567,21 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
563567
* A node MUST NOT add a HTLC if it would result in it
564568
* offering more than 300 HTLCs in either commitment transaction.
565569
*/
566-
if (tal_count(peer->remote.staging_cstate->a.htlcs) == 300
567-
|| tal_count(peer->local.staging_cstate->b.htlcs) == 300)
570+
if (tal_count(peer->remote.staging_cstate->side[OURS].htlcs) == 300
571+
|| tal_count(peer->local.staging_cstate->side[THEIRS].htlcs) == 300)
568572
return pkt_err(peer, "Too many HTLCs");
569573

570574
/* BOLT #2:
571575
*
572576
* A node MUST NOT set `id` equal to another HTLC which is in
573577
* the current staged commitment transaction.
574578
*/
575-
if (funding_htlc_by_id(&peer->remote.staging_cstate->a, u->id)
576-
< tal_count(peer->remote.staging_cstate->a.htlcs))
579+
if (funding_htlc_by_id(peer->remote.staging_cstate, u->id, OURS) != -1)
577580
return pkt_err(peer, "HTLC id %"PRIu64" clashes for you", u->id);
578581

579582
/* FIXME: Assert this... */
580583
/* Note: these should be in sync, so this should be redundant! */
581-
if (funding_htlc_by_id(&peer->local.staging_cstate->b, u->id)
582-
< tal_count(peer->local.staging_cstate->b.htlcs))
584+
if (funding_htlc_by_id(peer->local.staging_cstate, u->id, THEIRS) != -1)
583585
return pkt_err(peer, "HTLC id %"PRIu64" clashes for us", u->id);
584586

585587
/* BOLT #2:
@@ -593,16 +595,16 @@ Pkt *accept_pkt_htlc_add(struct peer *peer, const Pkt *pkt)
593595
/* FIXME: This is wrong! We may have already added more txs to
594596
* them.staging_cstate, driving that fee up.
595597
* We should check against the last version they acknowledged. */
596-
if (!funding_a_add_htlc(peer->remote.staging_cstate,
597-
u->amount_msat, &expiry, &rhash, u->id))
598+
if (!funding_add_htlc(peer->remote.staging_cstate,
599+
u->amount_msat, &expiry, &rhash, u->id, OURS))
598600
return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis"
599601
" in your commitment tx",
600602
u->amount_msat);
601603

602604
/* If we fail here, we've already changed them.staging_cstate, so
603605
* MUST terminate. */
604-
if (!funding_b_add_htlc(peer->local.staging_cstate,
605-
u->amount_msat, &expiry, &rhash, u->id))
606+
if (!funding_add_htlc(peer->local.staging_cstate,
607+
u->amount_msat, &expiry, &rhash, u->id, THEIRS))
606608
return pkt_err(peer, "Cannot afford %"PRIu64" milli-satoshis"
607609
" in our commitment tx",
608610
u->amount_msat);
@@ -623,19 +625,19 @@ static Pkt *find_commited_htlc(struct peer *peer, uint64_t id,
623625
* current commitment transaction, and MUST fail the
624626
* connection if it does not.
625627
*/
626-
*n_us = funding_htlc_by_id(&peer->local.commit->cstate->a, id);
627-
if (*n_us == tal_count(peer->local.commit->cstate->a.htlcs))
628+
*n_us = funding_htlc_by_id(peer->local.commit->cstate, id, OURS);
629+
if (*n_us == -1)
628630
return pkt_err(peer, "Did not find HTLC %"PRIu64, id);
629631

630632
/* They must not fail/fulfill twice, so it should be in staging, too. */
631-
*n_us = funding_htlc_by_id(&peer->local.staging_cstate->a, id);
632-
if (*n_us == tal_count(peer->local.staging_cstate->a.htlcs))
633+
*n_us = funding_htlc_by_id(peer->local.staging_cstate, id, OURS);
634+
if (*n_us == -1)
633635
return pkt_err(peer, "Already removed HTLC %"PRIu64, id);
634636

635637
/* FIXME: Assert this... */
636638
/* Note: these should match. */
637-
*n_them = funding_htlc_by_id(&peer->remote.staging_cstate->b, id);
638-
if (*n_them == tal_count(peer->remote.staging_cstate->b.htlcs))
639+
*n_them = funding_htlc_by_id(peer->remote.staging_cstate, id, THEIRS);
640+
if (*n_them == -1)
639641
return pkt_err(peer, "Did not find your HTLC %"PRIu64, id);
640642

641643
return NULL;
@@ -653,8 +655,8 @@ Pkt *accept_pkt_htlc_fail(struct peer *peer, const Pkt *pkt)
653655

654656
/* FIXME: Save reason. */
655657

656-
funding_a_fail_htlc(peer->local.staging_cstate, n_us);
657-
funding_b_fail_htlc(peer->remote.staging_cstate, n_them);
658+
funding_fail_htlc(peer->local.staging_cstate, n_us, OURS);
659+
funding_fail_htlc(peer->remote.staging_cstate, n_them, THEIRS);
658660
their_commit_changed(peer);
659661
return NULL;
660662
}
@@ -674,14 +676,14 @@ Pkt *accept_pkt_htlc_fulfill(struct peer *peer, const Pkt *pkt)
674676
proto_to_sha256(f->r, &r);
675677
sha256(&rhash, &r, sizeof(r));
676678

677-
if (!structeq(&rhash, &peer->local.staging_cstate->a.htlcs[n_us].rhash))
679+
if (!structeq(&rhash, &peer->local.staging_cstate->side[OURS].htlcs[n_us].rhash))
678680
return pkt_err(peer, "Invalid r for %"PRIu64, f->id);
679681

680682
/* Same ID must have same rhash */
681-
assert(structeq(&rhash, &peer->remote.staging_cstate->b.htlcs[n_them].rhash));
683+
assert(structeq(&rhash, &peer->remote.staging_cstate->side[THEIRS].htlcs[n_them].rhash));
682684

683-
funding_a_fulfill_htlc(peer->local.staging_cstate, n_us);
684-
funding_b_fulfill_htlc(peer->remote.staging_cstate, n_them);
685+
funding_fulfill_htlc(peer->local.staging_cstate, n_us, OURS);
686+
funding_fulfill_htlc(peer->remote.staging_cstate, n_them, THEIRS);
685687
their_commit_changed(peer);
686688
return NULL;
687689
}

0 commit comments

Comments
 (0)