Skip to content

Commit 7c0af81

Browse files
darosiorniftynei
authored andcommitted
bcli: use a more urgent feerate for HTLCs and penalty transactions
A CONSERVATIVE/3 target for them. Some noisy changes to the tests as we had to update the estimatesmartfee mock. Changelog-Changed: We now use a higher feerate for resolving onchain HTLCs and for penalty transactions
1 parent 77e7bee commit 7c0af81

File tree

6 files changed

+121
-65
lines changed

6 files changed

+121
-65
lines changed

contrib/pyln-testing/pyln/testing/utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,12 @@ def mock_estimatesmartfee(r):
860860
params = r['params']
861861
if params == [2, 'CONSERVATIVE']:
862862
feerate = feerates[0] * 4
863-
elif params == [4, 'ECONOMICAL']:
863+
elif params == [3, 'CONSERVATIVE']:
864864
feerate = feerates[1] * 4
865-
elif params == [100, 'ECONOMICAL']:
865+
elif params == [4, 'ECONOMICAL']:
866866
feerate = feerates[2] * 4
867+
elif params == [100, 'ECONOMICAL']:
868+
feerate = feerates[3] * 4
867869
else:
868870
raise ValueError()
869871
return {
@@ -878,13 +880,14 @@ def mock_estimatesmartfee(r):
878880
# Technically, this waits until it's called, not until it's processed.
879881
# We wait until all three levels have been called.
880882
if wait_for_effect:
881-
wait_for(lambda: self.daemon.rpcproxy.mock_counts['estimatesmartfee'] >= 3)
883+
wait_for(lambda:
884+
self.daemon.rpcproxy.mock_counts['estimatesmartfee'] >= 4)
882885

883886
# force new feerates by restarting and thus skipping slow smoothed process
884887
# Note: testnode must be created with: opts={'may_reconnect': True}
885888
def force_feerates(self, rate):
886889
assert(self.may_reconnect)
887-
self.set_feerates([rate] * 3, False)
890+
self.set_feerates([rate] * 4, False)
888891
self.restart()
889892
self.daemon.wait_for_log('peer_out WIRE_UPDATE_FEE')
890893
assert(self.rpc.feerates('perkw')['perkw']['opening'] == rate)
@@ -1005,7 +1008,7 @@ def get_nodes(self, num_nodes, opts=None):
10051008
return [j.result() for j in jobs]
10061009

10071010
def get_node(self, node_id=None, options=None, dbfile=None,
1008-
feerates=(15000, 7500, 3750), start=True,
1011+
feerates=(15000, 11000, 7500, 3750), start=True,
10091012
wait_for_bitcoind_sync=True, expect_fail=False, **kwargs):
10101013

10111014
node_id = self.get_node_id() if not node_id else node_id

plugins/bcli.c

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ static struct command_result *process_getblockchaininfo(struct bitcoin_cli *bcli
456456

457457
struct estimatefees_stash {
458458
/* FIXME: We use u64 but lightningd will store them as u32. */
459-
u64 urgent, normal, slow;
459+
u64 very_urgent, urgent, normal, slow;
460460
};
461461

462462
static struct command_result *
@@ -532,10 +532,10 @@ static struct command_result *estimatefees_final_step(struct bitcoin_cli *bcli)
532532
response = jsonrpc_stream_success(bcli->cmd);
533533
json_add_u64(response, "opening", stash->normal);
534534
json_add_u64(response, "mutual_close", stash->normal);
535-
json_add_u64(response, "unilateral_close", stash->urgent);
535+
json_add_u64(response, "unilateral_close", stash->very_urgent);
536536
json_add_u64(response, "delayed_to_us", stash->normal);
537-
json_add_u64(response, "htlc_resolution", stash->normal);
538-
json_add_u64(response, "penalty", stash->normal);
537+
json_add_u64(response, "htlc_resolution", stash->urgent);
538+
json_add_u64(response, "penalty", stash->urgent);
539539
/* We divide the slow feerate for the minimum acceptable, lightningd
540540
* will use floor if it's hit, though. */
541541
json_add_u64(response, "min_acceptable", stash->slow / 2);
@@ -546,13 +546,13 @@ static struct command_result *estimatefees_final_step(struct bitcoin_cli *bcli)
546546
* margin (say 5x the expected fee requirement)
547547
*/
548548
json_add_u64(response, "max_acceptable",
549-
stash->urgent * bitcoind->max_fee_multiplier);
549+
stash->very_urgent * bitcoind->max_fee_multiplier);
550550

551551
return command_finished(bcli->cmd, response);
552552
}
553553

554554
/* We got the response for the normal feerate, now treat the slow one. */
555-
static struct command_result *estimatefees_third_step(struct bitcoin_cli *bcli)
555+
static struct command_result *estimatefees_fourth_step(struct bitcoin_cli *bcli)
556556
{
557557
struct command_result *err;
558558
struct estimatefees_stash *stash = bcli->stash;
@@ -575,7 +575,7 @@ static struct command_result *estimatefees_third_step(struct bitcoin_cli *bcli)
575575
}
576576

577577
/* We got the response for the urgent feerate, now treat the normal one. */
578-
static struct command_result *estimatefees_second_step(struct bitcoin_cli *bcli)
578+
static struct command_result *estimatefees_third_step(struct bitcoin_cli *bcli)
579579
{
580580
struct command_result *err;
581581
struct estimatefees_stash *stash = bcli->stash;
@@ -591,6 +591,29 @@ static struct command_result *estimatefees_second_step(struct bitcoin_cli *bcli)
591591

592592
params[0] = "4";
593593
params[1] = "ECONOMICAL";
594+
start_bitcoin_cli(NULL, bcli->cmd, estimatefees_fourth_step, true,
595+
BITCOIND_LOW_PRIO, "estimatesmartfee", params, stash);
596+
597+
return command_still_pending(bcli->cmd);
598+
}
599+
600+
/* We got the response for the very urgent feerate, now treat the urgent one. */
601+
static struct command_result *estimatefees_second_step(struct bitcoin_cli *bcli)
602+
{
603+
struct command_result *err;
604+
struct estimatefees_stash *stash = bcli->stash;
605+
const char **params = tal_arr(bcli->cmd, const char *, 2);
606+
607+
/* If we cannot estimatefees, no need to continue bothering bitcoind. */
608+
if (*bcli->exitstatus != 0)
609+
return estimatefees_null_response(bcli);
610+
611+
err = estimatefees_parse_feerate(bcli, &stash->very_urgent);
612+
if (err)
613+
return err;
614+
615+
params[0] = "3";
616+
params[1] = "CONSERVATIVE";
594617
start_bitcoin_cli(NULL, bcli->cmd, estimatefees_third_step, true,
595618
BITCOIND_LOW_PRIO, "estimatesmartfee", params, stash);
596619

@@ -726,8 +749,9 @@ static struct command_result *getchaininfo(struct command *cmd,
726749
return command_still_pending(cmd);
727750
}
728751

729-
/* Get the current feerates. We use an urgent feerate for unilateral_close and max,
730-
* a slow feerate for min, and a normal for all others.
752+
/* Get the current feerates. We us an urgent feerate for unilateral_close and max,
753+
* a slightly less urgent feerate for htlc_resolution and penalty transactions,
754+
* a slow feerate for min, and a normal one for all others.
731755
*
732756
* Calls `estimatesmartfee` with targets 2/CONSERVATIVE (urgent),
733757
* 4/ECONOMICAL (normal), and 100/ECONOMICAL (slow) then returns the

tests/test_closing.py

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ def test_closing_torture(node_factory, executor, bitcoind):
216216
def test_closing_different_fees(node_factory, bitcoind, executor):
217217
l1 = node_factory.get_node()
218218

219-
# Default feerate = 15000/7500/1000
219+
# Default feerate = 15000/11000/7500/1000
220220
# It will start at the second number, accepting anything above the first.
221-
feerates = [[20000, 15000, 7400], [8000, 1001, 100]]
221+
feerates = [[20000, 11000, 15000, 7400], [8000, 6000, 1001, 100]]
222222
amounts = [0, 545999, 546000]
223223
num_peers = len(feerates) * len(amounts)
224224

@@ -362,10 +362,10 @@ def test_closing_specified_destination(node_factory, bitcoind, chainparams):
362362

363363
def closing_fee(node_factory, bitcoind, chainparams, opts):
364364
rate = opts['funder_feerate_per_kw']
365-
funder = node_factory.get_node(feerates=(rate, rate, rate))
365+
funder = node_factory.get_node(feerates=(rate, rate, rate, rate))
366366

367367
rate = opts['fundee_feerate_per_kw']
368-
fundee = node_factory.get_node(feerates=(rate, rate, rate))
368+
fundee = node_factory.get_node(feerates=(rate, rate, rate, rate))
369369

370370
funder_id = funder.info['id']
371371
fundee_id = fundee.info['id']
@@ -446,7 +446,9 @@ def test_penalty_inhtlc(node_factory, bitcoind, executor, chainparams):
446446
"""Test penalty transaction with an incoming HTLC"""
447447
# We suppress each one after first commit; HTLC gets added not fulfilled.
448448
# Feerates identical so we don't get gratuitous commit to update them
449-
l1 = node_factory.get_node(disconnect=['=WIRE_COMMITMENT_SIGNED-nocommit'], may_fail=True, feerates=(7500, 7500, 7500), allow_broken_log=True)
449+
l1 = node_factory.get_node(disconnect=['=WIRE_COMMITMENT_SIGNED-nocommit'],
450+
may_fail=True, feerates=(7500, 7500, 7500, 7500),
451+
allow_broken_log=True)
450452
l2 = node_factory.get_node(disconnect=['=WIRE_COMMITMENT_SIGNED-nocommit'])
451453

452454
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -487,6 +489,7 @@ def test_penalty_inhtlc(node_factory, bitcoind, executor, chainparams):
487489
bitcoind.generate_block(1)
488490

489491
l2.daemon.wait_for_log(' to ONCHAIN')
492+
490493
# FIXME: l1 should try to stumble along!
491494
wait_for(lambda: len(l2.getactivechannels()) == 0)
492495

@@ -509,7 +512,7 @@ def test_penalty_inhtlc(node_factory, bitcoind, executor, chainparams):
509512
outputs = l2.rpc.listfunds()['outputs']
510513
assert [o['status'] for o in outputs] == ['confirmed'] * 2
511514
# Allow some lossage for fees.
512-
slack = 27000 if chainparams['elements'] else 15000
515+
slack = 30000 if chainparams['elements'] else 20000
513516
assert sum(o['value'] for o in outputs) < 10**6
514517
assert sum(o['value'] for o in outputs) > 10**6 - slack
515518

@@ -519,7 +522,9 @@ def test_penalty_outhtlc(node_factory, bitcoind, executor, chainparams):
519522
"""Test penalty transaction with an outgoing HTLC"""
520523
# First we need to get funds to l2, so suppress after second.
521524
# Feerates identical so we don't get gratuitous commit to update them
522-
l1 = node_factory.get_node(disconnect=['=WIRE_COMMITMENT_SIGNED*3-nocommit'], may_fail=True, feerates=(7500, 7500, 7500), allow_broken_log=True)
525+
l1 = node_factory.get_node(disconnect=['=WIRE_COMMITMENT_SIGNED*3-nocommit'],
526+
may_fail=True, feerates=(7500, 7500, 7500, 7500),
527+
allow_broken_log=True)
523528
l2 = node_factory.get_node(disconnect=['=WIRE_COMMITMENT_SIGNED*3-nocommit'])
524529

525530
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -589,7 +594,7 @@ def test_penalty_outhtlc(node_factory, bitcoind, executor, chainparams):
589594
outputs = l2.rpc.listfunds()['outputs']
590595
assert [o['status'] for o in outputs] == ['confirmed'] * 3
591596
# Allow some lossage for fees.
592-
slack = 27000 if chainparams['elements'] else 15000
597+
slack = 30000 if chainparams['elements'] else 20000
593598
assert sum(o['value'] for o in outputs) < 10**6
594599
assert sum(o['value'] for o in outputs) > 10**6 - slack
595600

@@ -687,7 +692,8 @@ def test_onchaind_replay(node_factory, bitcoind):
687692
disconnects = ['+WIRE_REVOKE_AND_ACK', 'permfail']
688693
options = {'watchtime-blocks': 201, 'cltv-delta': 101}
689694
# Feerates identical so we don't get gratuitous commit to update them
690-
l1 = node_factory.get_node(options=options, disconnect=disconnects, feerates=(7500, 7500, 7500))
695+
l1 = node_factory.get_node(options=options, disconnect=disconnects,
696+
feerates=(7500, 7500, 7500, 7500))
691697
l2 = node_factory.get_node(options=options)
692698

693699
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -740,7 +746,8 @@ def test_onchain_dust_out(node_factory, bitcoind, executor):
740746
# HTLC 1->2, 1 fails after it's irrevocably committed
741747
disconnects = ['@WIRE_REVOKE_AND_ACK', 'permfail']
742748
# Feerates identical so we don't get gratuitous commit to update them
743-
l1 = node_factory.get_node(disconnect=disconnects, feerates=(7500, 7500, 7500))
749+
l1 = node_factory.get_node(disconnect=disconnects,
750+
feerates=(7500, 7500, 7500, 7500))
744751
l2 = node_factory.get_node()
745752

746753
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -803,7 +810,8 @@ def test_onchain_timeout(node_factory, bitcoind, executor):
803810
# HTLC 1->2, 1 fails just after it's irrevocably committed
804811
disconnects = ['+WIRE_REVOKE_AND_ACK*3', 'permfail']
805812
# Feerates identical so we don't get gratuitous commit to update them
806-
l1 = node_factory.get_node(disconnect=disconnects, feerates=(7500, 7500, 7500))
813+
l1 = node_factory.get_node(disconnect=disconnects,
814+
feerates=(7500, 7500, 7500, 7500))
807815
l2 = node_factory.get_node()
808816

809817
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -1038,7 +1046,8 @@ def test_onchain_all_dust(node_factory, bitcoind, executor):
10381046
# is generated on-the-fly, and is thus feerate sensitive.
10391047
disconnects = ['-WIRE_UPDATE_FAIL_HTLC', 'permfail']
10401048
# Feerates identical so we don't get gratuitous commit to update them
1041-
l1 = node_factory.get_node(options={'dev-no-reconnect': None}, feerates=(7500, 7500, 7500))
1049+
l1 = node_factory.get_node(options={'dev-no-reconnect': None},
1050+
feerates=(7500, 7500, 7500, 7500))
10421051
l2 = node_factory.get_node(disconnect=disconnects)
10431052

10441053
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -1060,7 +1069,7 @@ def test_onchain_all_dust(node_factory, bitcoind, executor):
10601069
l2.wait_for_channel_onchain(l1.info['id'])
10611070

10621071
# Make l1's fees really high (and wait for it to exceed 50000)
1063-
l1.set_feerates((100000, 100000, 100000))
1072+
l1.set_feerates((100000, 100000, 100000, 100000))
10641073
l1.daemon.wait_for_log('Feerate estimate for unilateral_close set to [56789][0-9]{4}')
10651074

10661075
bitcoind.generate_block(1)
@@ -1097,12 +1106,12 @@ def test_onchain_different_fees(node_factory, bitcoind, executor):
10971106
p1 = executor.submit(l1.pay, l2, 1000000000)
10981107
l1.daemon.wait_for_log('htlc 0: RCVD_ADD_ACK_COMMIT->SENT_ADD_ACK_REVOCATION')
10991108

1100-
l1.set_feerates((16000, 7500, 3750))
1109+
l1.set_feerates((16000, 11000, 7500, 3750))
11011110
p2 = executor.submit(l1.pay, l2, 900000000)
11021111
l1.daemon.wait_for_log('htlc 1: RCVD_ADD_ACK_COMMIT->SENT_ADD_ACK_REVOCATION')
11031112

11041113
# Restart with different feerate for second HTLC.
1105-
l1.set_feerates((5000, 5000, 3750))
1114+
l1.set_feerates((5000, 5000, 5000, 3750))
11061115
l1.restart()
11071116
l1.daemon.wait_for_log('peer_out WIRE_UPDATE_FEE')
11081117

@@ -1156,7 +1165,8 @@ def test_permfail_new_commit(node_factory, bitcoind, executor):
11561165
# Test case where we have two possible commits: it will use new one.
11571166
disconnects = ['-WIRE_REVOKE_AND_ACK', 'permfail']
11581167
# Feerates identical so we don't get gratuitous commit to update them
1159-
l1 = node_factory.get_node(options={'dev-no-reconnect': None}, feerates=(7500, 7500, 7500))
1168+
l1 = node_factory.get_node(options={'dev-no-reconnect': None},
1169+
feerates=(7500, 7500, 7500, 7500))
11601170
l2 = node_factory.get_node(disconnect=disconnects)
11611171

11621172
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -1457,7 +1467,8 @@ def test_permfail_htlc_in(node_factory, bitcoind, executor):
14571467
# Test case where we fail with unsettled incoming HTLC.
14581468
disconnects = ['-WIRE_UPDATE_FULFILL_HTLC', 'permfail']
14591469
# Feerates identical so we don't get gratuitous commit to update them
1460-
l1 = node_factory.get_node(options={'dev-no-reconnect': None}, feerates=(7500, 7500, 7500))
1470+
l1 = node_factory.get_node(options={'dev-no-reconnect': None},
1471+
feerates=(7500, 7500, 7500, 7500))
14611472
l2 = node_factory.get_node(disconnect=disconnects)
14621473

14631474
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
@@ -1503,7 +1514,8 @@ def test_permfail_htlc_out(node_factory, bitcoind, executor):
15031514
disconnects = ['+WIRE_REVOKE_AND_ACK', 'permfail']
15041515
l1 = node_factory.get_node(options={'dev-no-reconnect': None})
15051516
# Feerates identical so we don't get gratuitous commit to update them
1506-
l2 = node_factory.get_node(disconnect=disconnects, feerates=(7500, 7500, 7500))
1517+
l2 = node_factory.get_node(disconnect=disconnects,
1518+
feerates=(7500, 7500, 7500, 7500))
15071519

15081520
l1.rpc.connect(l2.info['id'], 'localhost', l2.port)
15091521
l2.daemon.wait_for_log('openingd-chan#1: Handed peer, entering loop'.format(l1.info['id']))

0 commit comments

Comments
 (0)