@@ -81,7 +81,7 @@ Pkt *pkt_open(const tal_t *ctx, const struct peer *peer,
8181 locktime__init (o -> delay );
8282 o -> delay -> locktime_case = LOCKTIME__LOCKTIME_SECONDS ;
8383 o -> delay -> seconds = rel_locktime_to_seconds (& peer -> us .locktime );
84- o -> commitment_fee = peer -> us .commit_fee ;
84+ o -> initial_fee_rate = peer -> us .commit_fee_rate ;
8585 if (anchor == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR )
8686 assert (peer -> us .offer_anchor == CMD_OPEN_WITH_ANCHOR );
8787 else {
@@ -289,8 +289,8 @@ Pkt *accept_pkt_open(const tal_t *ctx,
289289 return pkt_err (ctx , "Delay too great" );
290290 if (o -> min_depth > peer -> dstate -> config .anchor_confirms_max )
291291 return pkt_err (ctx , "min_depth too great" );
292- if (o -> commitment_fee < peer -> dstate -> config .commitment_fee_min )
293- return pkt_err (ctx , "Commitment fee too low" );
292+ if (o -> initial_fee_rate < peer -> dstate -> config .commitment_fee_rate_min )
293+ return pkt_err (ctx , "Commitment fee rate too low" );
294294 if (o -> anch == OPEN_CHANNEL__ANCHOR_OFFER__WILL_CREATE_ANCHOR )
295295 peer -> them .offer_anchor = CMD_OPEN_WITH_ANCHOR ;
296296 else if (o -> anch == OPEN_CHANNEL__ANCHOR_OFFER__WONT_CREATE_ANCHOR )
@@ -304,7 +304,7 @@ Pkt *accept_pkt_open(const tal_t *ctx,
304304 if (!proto_to_rel_locktime (o -> delay , & peer -> them .locktime ))
305305 return pkt_err (ctx , "Malformed locktime" );
306306 peer -> them .mindepth = o -> min_depth ;
307- peer -> them .commit_fee = o -> commitment_fee ;
307+ peer -> them .commit_fee_rate = o -> initial_fee_rate ;
308308 if (!proto_to_pubkey (peer -> dstate -> secpctx ,
309309 o -> commit_key , & peer -> them .commitkey ))
310310 return pkt_err (ctx , "Bad commitkey" );
@@ -326,7 +326,6 @@ Pkt *accept_pkt_anchor(const tal_t *ctx,
326326 const Pkt * pkt )
327327{
328328 const OpenAnchor * a = pkt -> open_anchor ;
329- u64 commitfee ;
330329
331330 /* They must be offering anchor for us to try accepting */
332331 assert (peer -> us .offer_anchor == CMD_OPEN_WITHOUT_ANCHOR );
@@ -336,15 +335,13 @@ Pkt *accept_pkt_anchor(const tal_t *ctx,
336335 peer -> anchor .index = a -> output_index ;
337336 peer -> anchor .satoshis = a -> amount ;
338337
339- /* Create funder's cstate, invert to get ours. */
340- commitfee = commit_fee (peer -> them .commit_fee , peer -> us .commit_fee );
338+ /* Create our cstate. */
341339 peer -> cstate = initial_funding (peer ,
342- peer -> us .offer_anchor ,
340+ peer -> us .offer_anchor == CMD_OPEN_WITH_ANCHOR ,
343341 peer -> anchor .satoshis ,
344- commitfee );
342+ peer -> us . commit_fee_rate );
345343 if (!peer -> cstate )
346344 return pkt_err (ctx , "Insufficient funds for fee" );
347- invert_cstate (peer -> cstate );
348345
349346 /* Now we can make initial (unsigned!) commit txs. */
350347 make_commit_txs (peer , peer ,
@@ -449,18 +446,14 @@ Pkt *accept_pkt_htlc_add(const tal_t *ctx,
449446 }
450447
451448 cur -> cstate = copy_funding (cur , peer -> cstate );
452- if (!funding_delta (peer -> anchor .satoshis ,
453- 0 , cur -> stage .add .htlc .msatoshis ,
454- & cur -> cstate -> b , & cur -> cstate -> a )) {
449+ if (!funding_b_add_htlc (cur -> cstate ,
450+ cur -> stage .add .htlc .msatoshis ,
451+ & cur -> stage .add .htlc .expiry ,
452+ & cur -> stage .add .htlc .rhash )) {
455453 err = pkt_err (ctx , "Cannot afford %" PRIu64 " milli-satoshis" ,
456454 cur -> stage .add .htlc .msatoshis );
457455 goto fail ;
458456 }
459- /* Add the htlc to their side of channel. */
460- funding_add_htlc (& cur -> cstate -> b ,
461- cur -> stage .add .htlc .msatoshis ,
462- & cur -> stage .add .htlc .expiry ,
463- & cur -> stage .add .htlc .rhash );
464457 peer_add_htlc_expiry (peer , & cur -> stage .add .htlc .expiry );
465458
466459 peer_get_revocation_hash (peer , peer -> commit_tx_counter + 1 ,
@@ -498,7 +491,6 @@ Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt)
498491 Pkt * err ;
499492 size_t i ;
500493 struct sha256 rhash ;
501- struct channel_htlc * htlc ;
502494
503495 proto_to_sha256 (f -> revocation_hash , & cur -> their_revocation_hash );
504496 proto_to_sha256 (f -> r_hash , & rhash );
@@ -511,17 +503,10 @@ Pkt *accept_pkt_htlc_fail(const tal_t *ctx, struct peer *peer, const Pkt *pkt)
511503
512504 cur -> stage .fail .fail = HTLC_FAIL ;
513505 cur -> stage .fail .index = i ;
514- htlc = & peer -> cstate -> a .htlcs [i ];
515506
516- /* Removing it should not fail: we regain HTLC amount */
507+ /* We regain HTLC amount */
517508 cur -> cstate = copy_funding (cur , peer -> cstate );
518- if (!funding_delta (peer -> anchor .satoshis ,
519- 0 , - htlc -> msatoshis ,
520- & cur -> cstate -> a , & cur -> cstate -> b )) {
521- fatal ("Unexpected failure fulfilling HTLC of %" PRIu64
522- " milli-satoshis" , htlc -> msatoshis );
523- }
524- funding_remove_htlc (& cur -> cstate -> a , i );
509+ funding_a_fail_htlc (cur -> cstate , i );
525510 /* FIXME: Remove timer. */
526511
527512 peer_get_revocation_hash (peer , peer -> commit_tx_counter + 1 ,
@@ -550,7 +535,6 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx,
550535 Pkt * err ;
551536 size_t i ;
552537 struct sha256 rhash ;
553- struct channel_htlc * htlc ;
554538
555539 cur -> stage .fulfill .fulfill = HTLC_FULFILL ;
556540 proto_to_sha256 (f -> r , & cur -> stage .fulfill .r );
@@ -564,19 +548,10 @@ Pkt *accept_pkt_htlc_fulfill(const tal_t *ctx,
564548 }
565549 cur -> stage .fulfill .index = i ;
566550
567- htlc = & peer -> cstate -> a .htlcs [i ];
568-
569- /* Removing it should not fail: they gain HTLC amount */
551+ /* Removing it: they gain HTLC amount */
570552 cur -> cstate = copy_funding (cur , peer -> cstate );
571- if (!funding_delta (peer -> anchor .satoshis ,
572- - htlc -> msatoshis ,
573- - htlc -> msatoshis ,
574- & cur -> cstate -> a , & cur -> cstate -> b )) {
575- fatal ("Unexpected failure fulfilling HTLC of %" PRIu64
576- " milli-satoshis" , htlc -> msatoshis );
577- }
578- funding_remove_htlc (& cur -> cstate -> a , i );
579-
553+ funding_a_fulfill_htlc (cur -> cstate , i );
554+
580555 peer_get_revocation_hash (peer , peer -> commit_tx_counter + 1 ,
581556 & cur -> our_revocation_hash );
582557
@@ -729,13 +704,16 @@ Pkt *accept_pkt_close_sig(const tal_t *ctx, struct peer *peer, const Pkt *pkt,
729704 struct bitcoin_signature theirsig ;
730705
731706 /* BOLT #2:
732-
707+ *
733708 * The sender MUST set `close_fee` lower than or equal to the fee of the
734709 * final commitment transaction, and MUST set `close_fee` to an even
735710 * number of satoshis.
736711 */
737- if (c -> close_fee & 1 || c -> close_fee > peer -> them .commit_fee )
712+ if ((c -> close_fee & 1 )
713+ || c -> close_fee > commit_tx_fee (peer -> them .commit ,
714+ peer -> anchor .satoshis )) {
738715 return pkt_err (ctx , "Invalid close fee" );
716+ }
739717
740718 /* FIXME: Don't accept tiny fee at all? */
741719
0 commit comments