@@ -295,10 +295,12 @@ static void send_offer(struct per_peer_state *pps,
295
295
enum side opener ,
296
296
struct amount_sat our_dust_limit ,
297
297
struct amount_sat fee_to_offer ,
298
- const struct bitcoin_outpoint * wrong_funding )
298
+ const struct bitcoin_outpoint * wrong_funding ,
299
+ struct tlv_closing_signed_tlvs_fee_range * tlv_fees )
299
300
{
300
301
struct bitcoin_tx * tx ;
301
302
struct bitcoin_signature our_sig ;
303
+ struct tlv_closing_signed_tlvs * close_tlvs ;
302
304
u8 * msg ;
303
305
304
306
/* BOLT #2:
@@ -338,8 +340,14 @@ static void send_offer(struct per_peer_state *pps,
338
340
status_debug ("sending fee offer %s" ,
339
341
type_to_string (tmpctx , struct amount_sat , & fee_to_offer ));
340
342
343
+ /* Add the new close_tlvs with our fee range */
344
+ close_tlvs = tlv_closing_signed_tlvs_new (msg );
345
+ close_tlvs -> fee_range = tlv_fees ;
346
+
341
347
assert (our_sig .sighash_type == SIGHASH_ALL );
342
- msg = towire_closing_signed (NULL , channel_id , fee_to_offer , & our_sig .s );
348
+ msg = towire_closing_signed (NULL , channel_id , fee_to_offer , & our_sig .s ,
349
+ close_tlvs );
350
+
343
351
sync_crypto_write (pps , take (msg ));
344
352
}
345
353
@@ -376,13 +384,15 @@ receive_offer(struct per_peer_state *pps,
376
384
struct amount_sat our_dust_limit ,
377
385
struct amount_sat min_fee_to_accept ,
378
386
const struct bitcoin_outpoint * wrong_funding ,
379
- struct bitcoin_txid * closing_txid )
387
+ struct bitcoin_txid * closing_txid ,
388
+ struct tlv_closing_signed_tlvs_fee_range * * tlv_fees )
380
389
{
381
390
u8 * msg ;
382
391
struct channel_id their_channel_id ;
383
392
struct amount_sat received_fee ;
384
393
struct bitcoin_signature their_sig ;
385
394
struct bitcoin_tx * tx ;
395
+ struct tlv_closing_signed_tlvs * close_tlvs ;
386
396
387
397
/* Wait for them to say something interesting */
388
398
do {
@@ -406,8 +416,10 @@ receive_offer(struct per_peer_state *pps,
406
416
} while (!msg );
407
417
408
418
their_sig .sighash_type = SIGHASH_ALL ;
419
+ close_tlvs = tlv_closing_signed_tlvs_new (msg );
409
420
if (!fromwire_closing_signed (msg , & their_channel_id ,
410
- & received_fee , & their_sig .s ))
421
+ & received_fee , & their_sig .s ,
422
+ close_tlvs ))
411
423
peer_failed_warn (pps , channel_id ,
412
424
"Expected closing_signed: %s" ,
413
425
tal_hex (tmpctx , msg ));
@@ -481,6 +493,11 @@ receive_offer(struct per_peer_state *pps,
481
493
status_debug ("Received fee offer %s" ,
482
494
type_to_string (tmpctx , struct amount_sat , & received_fee ));
483
495
496
+ if (close_tlvs )
497
+ * tlv_fees = close_tlvs -> fee_range ;
498
+ else
499
+ * tlv_fees = NULL ;
500
+
484
501
/* Master sorts out what is best offer, we just tell it any above min */
485
502
if (amount_sat_greater_eq (received_fee , min_fee_to_accept )) {
486
503
status_debug ("...offer is reasonable" );
@@ -546,6 +563,23 @@ static void adjust_feerange(struct feerange *feerange,
546
563
"Overflow in updating fee range" );
547
564
}
548
565
566
+ static bool
567
+ evaluate_their_offer (struct amount_sat their_offer ,
568
+ const struct tlv_closing_signed_tlvs_fee_range * our_range ,
569
+ const struct tlv_closing_signed_tlvs_fee_range * their_range )
570
+ {
571
+ /* They'd better be within their own range */
572
+ if (amount_sat_greater (their_offer , their_range -> max_fee_satoshis ))
573
+ return false;
574
+ if (amount_sat_less (their_offer , their_range -> min_fee_satoshis ))
575
+ return false;
576
+ if (amount_sat_greater (their_offer , our_range -> max_fee_satoshis ))
577
+ return false;
578
+ if (amount_sat_less (their_offer , our_range -> min_fee_satoshis ))
579
+ return false;
580
+ return true;
581
+ }
582
+
549
583
/* Figure out what we should offer now. */
550
584
static struct amount_sat
551
585
adjust_offer (struct per_peer_state * pps , const struct channel_id * channel_id ,
@@ -669,6 +703,7 @@ int main(int argc, char *argv[])
669
703
u8 * channel_reestablish ;
670
704
struct secret last_remote_per_commit_secret ;
671
705
struct bitcoin_outpoint * wrong_funding ;
706
+ struct tlv_closing_signed_tlvs_fee_range * our_feerange , * * their_feerange ;
672
707
673
708
subdaemon_setup (argc , argv );
674
709
@@ -687,7 +722,8 @@ int main(int argc, char *argv[])
687
722
& out [LOCAL ],
688
723
& out [REMOTE ],
689
724
& our_dust_limit ,
690
- & min_fee_to_accept , & commitment_fee ,
725
+ & min_fee_to_accept ,
726
+ & commitment_fee ,
691
727
& offer [LOCAL ],
692
728
& scriptpubkey [LOCAL ],
693
729
& scriptpubkey [REMOTE ],
@@ -706,6 +742,13 @@ int main(int argc, char *argv[])
706
742
/* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = hsmd */
707
743
per_peer_state_set_fds (notleak (pps ), 3 , 4 , 5 );
708
744
745
+ /* Write values into tlv for updated closing fee neg */
746
+ their_feerange = tal (ctx , struct tlv_closing_signed_tlvs_fee_range * );
747
+ * their_feerange = NULL ;
748
+ our_feerange = tal (ctx , struct tlv_closing_signed_tlvs_fee_range );
749
+ our_feerange -> min_fee_satoshis = min_fee_to_accept ;
750
+ our_feerange -> max_fee_satoshis = commitment_fee ;
751
+
709
752
snprintf (fee_negotiation_step_str , sizeof (fee_negotiation_step_str ),
710
753
"%" PRIu64 "%s" , fee_negotiation_step ,
711
754
fee_negotiation_step_unit ==
@@ -763,7 +806,8 @@ int main(int argc, char *argv[])
763
806
funding , out , opener ,
764
807
our_dust_limit ,
765
808
offer [LOCAL ],
766
- wrong_funding );
809
+ wrong_funding ,
810
+ our_feerange );
767
811
} else {
768
812
if (i == 0 )
769
813
peer_billboard (false, "Waiting for their initial"
@@ -785,7 +829,69 @@ int main(int argc, char *argv[])
785
829
our_dust_limit ,
786
830
min_fee_to_accept ,
787
831
wrong_funding ,
788
- & closing_txid );
832
+ & closing_txid ,
833
+ their_feerange );
834
+
835
+ if (* their_feerange ) {
836
+ if (!evaluate_their_offer (offer [REMOTE ],
837
+ our_feerange ,
838
+ * their_feerange )) {
839
+ peer_billboard (true,
840
+ "Unable to agree on"
841
+ " a feerate."
842
+ " Our range %s-%s,"
843
+ " their range %s-%s" ,
844
+ type_to_string (tmpctx ,
845
+ struct amount_sat ,
846
+ & our_feerange -> min_fee_satoshis ),
847
+ type_to_string (tmpctx ,
848
+ struct amount_sat ,
849
+ & our_feerange -> max_fee_satoshis ),
850
+ type_to_string (tmpctx ,
851
+ struct amount_sat ,
852
+ & (* their_feerange )-> min_fee_satoshis ),
853
+ type_to_string (tmpctx ,
854
+ struct amount_sat ,
855
+ & (* their_feerange )-> max_fee_satoshis ));
856
+ goto exit_thru_the_giftshop ;
857
+ }
858
+
859
+ peer_billboard (true, "Using quick-close."
860
+ " Updated our offer to match"
861
+ " theirs (was %s, now %s" ,
862
+ type_to_string (tmpctx ,
863
+ struct amount_sat ,
864
+ & offer [LOCAL ]),
865
+ type_to_string (tmpctx ,
866
+ struct amount_sat ,
867
+ & offer [REMOTE ]));
868
+
869
+ /* We mirror it back */
870
+ if (i == 1 && !amount_sat_eq (offer [LOCAL ],
871
+ offer [REMOTE ])) {
872
+ send_offer (pps , chainparams ,
873
+ & channel_id ,
874
+ funding_pubkey ,
875
+ funding_wscript ,
876
+ scriptpubkey ,
877
+ & funding_txid ,
878
+ funding_txout ,
879
+ funding , out , opener ,
880
+ our_dust_limit ,
881
+ offer [REMOTE ],
882
+ wrong_funding ,
883
+ our_feerange );
884
+ }
885
+
886
+ /* BOLT-9a0b1f68d6c6feefe2c459e28adee3475d02a62e #2:
887
+ * The receiving node:
888
+ * - if `fee_satoshis` matches its previously sent
889
+ * `fee_range`:
890
+ * - SHOULD use `fee_satoshis` to sign and
891
+ * broadcast the final closing transaction
892
+ */
893
+ offer [LOCAL ] = offer [REMOTE ];
894
+ }
789
895
}
790
896
}
791
897
@@ -814,7 +920,8 @@ int main(int argc, char *argv[])
814
920
funding , out , opener ,
815
921
our_dust_limit ,
816
922
offer [LOCAL ],
817
- wrong_funding );
923
+ wrong_funding ,
924
+ our_feerange );
818
925
} else {
819
926
peer_billboard (false, "Waiting for another"
820
927
" closing fee offer:"
@@ -831,7 +938,8 @@ int main(int argc, char *argv[])
831
938
our_dust_limit ,
832
939
min_fee_to_accept ,
833
940
wrong_funding ,
834
- & closing_txid );
941
+ & closing_txid ,
942
+ their_feerange );
835
943
}
836
944
837
945
whose_turn = !whose_turn ;
@@ -841,6 +949,7 @@ int main(int argc, char *argv[])
841
949
offer [LOCAL ],
842
950
type_to_string (tmpctx , struct bitcoin_txid , & closing_txid ));
843
951
952
+ exit_thru_the_giftshop :
844
953
#if DEVELOPER
845
954
/* We don't listen for master commands, so always check memleak here */
846
955
tal_free (wrong_funding );
0 commit comments