Replace lightning_settle_delay with enable_htlc_settle (asyncio.Event)

This commit is contained in:
ThomasV 2020-02-27 20:53:50 +01:00
parent ce81957d25
commit 077f778632
5 changed files with 27 additions and 16 deletions

View file

@ -992,6 +992,11 @@ class Commands:
self.network.config.fee_estimates = ast.literal_eval(fees)
self.network.notify('fee')
@command('wn')
async def enable_htlc_settle(self, b: bool, wallet: Abstract_Wallet = None):
e = wallet.lnworker.enable_htlc_settle
e.set() if b else e.clear()
@command('n')
async def clear_ln_blacklist(self):
self.network.path_finder.blacklist.clear()

View file

@ -1282,7 +1282,7 @@ class Peer(Logger):
await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason)
return
#self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED)
await asyncio.sleep(self.network.config.get('lightning_settle_delay', 0))
await self.lnworker.enable_htlc_settle.wait()
await self._fulfill_htlc(chan, htlc.htlc_id, preimage)
async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):

View file

@ -385,6 +385,9 @@ class LNWallet(LNWorker):
self.sweep_address = wallet.get_receiving_address()
self.lock = threading.RLock()
self.logs = defaultdict(list) # type: Dict[str, List[PaymentAttemptLog]] # key is RHASH
# used in tests
self.enable_htlc_settle = asyncio.Event()
self.enable_htlc_settle.set()
# note: accessing channels (besides simple lookup) needs self.lock!
self.channels = {}

View file

@ -162,7 +162,7 @@ if [[ $1 == "breach" ]]; then
fi
if [[ $1 == "redeem_htlcs" ]]; then
$bob setconfig lightning_settle_delay 10
$bob enable_htlc_settle false
wait_for_balance alice 1
echo "alice opens channel"
bob_node=$($bob nodeid)
@ -172,10 +172,9 @@ if [[ $1 == "redeem_htlcs" ]]; then
# alice pays bob
invoice=$($bob add_lightning_request 0.05 -m "test")
$alice lnpay $invoice --timeout=1 || true
sleep 1
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo 'SETTLE_DELAY did not work'
echo 'enable_htlc_settle did not work'
exit 1
fi
# bob goes away
@ -205,7 +204,7 @@ fi
if [[ $1 == "breach_with_unspent_htlc" ]]; then
$bob setconfig lightning_settle_delay 3
$bob enable_htlc_settle false
wait_for_balance alice 1
echo "alice opens channel"
bob_node=$($bob nodeid)
@ -217,14 +216,14 @@ if [[ $1 == "breach_with_unspent_htlc" ]]; then
$alice lnpay $invoice --timeout=1 || true
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo "SETTLE_DELAY did not work, $settled != 0"
echo "enable_htlc_settle did not work, $settled != 0"
exit 1
fi
ctx=$($alice get_channel_ctx $channel)
sleep 5
$bob enable_htlc_settle true
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "1" ]]; then
echo "SETTLE_DELAY did not work, $settled != 1"
echo "enable_htlc_settle did not work, $settled != 1"
exit 1
fi
echo "alice breaches with old ctx"
@ -234,7 +233,7 @@ fi
if [[ $1 == "breach_with_spent_htlc" ]]; then
$bob setconfig lightning_settle_delay 3
$bob enable_htlc_settle false
wait_for_balance alice 1
echo "alice opens channel"
bob_node=$($bob nodeid)
@ -247,14 +246,14 @@ if [[ $1 == "breach_with_spent_htlc" ]]; then
ctx=$($alice get_channel_ctx $channel)
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "0" ]]; then
echo "SETTLE_DELAY did not work, $settled != 0"
echo "enable_htlc_settle did not work, $settled != 0"
exit 1
fi
cp /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/toxic_wallet
sleep 5
$bob enable_htlc_settle true
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
if [[ "$settled" != "1" ]]; then
echo "SETTLE_DELAY did not work, $settled != 1"
echo "enable_htlc_settle did not work, $settled != 1"
exit 1
fi
echo $($bob getbalance)

View file

@ -91,6 +91,9 @@ class MockLNWallet:
self.pending_payments = defaultdict(asyncio.Future)
chan.lnworker = self
chan.node_id = remote_keypair.pubkey
# used in tests
self.enable_htlc_settle = asyncio.Event()
self.enable_htlc_settle.set()
def get_invoice_status(self, key):
pass
@ -295,7 +298,7 @@ class TestPeer(ElectrumTestCase):
w2.network.config.set_key('dynamic_fees', False)
w1.network.config.set_key('fee_per_kb', 5000)
w2.network.config.set_key('fee_per_kb', 1000)
w2.network.config.set_key('lightning_settle_delay', 0.1)
w2.enable_htlc_settle.clear()
pay_req = self.prepare_invoice(w2)
lnaddr = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
async def pay():
@ -304,12 +307,13 @@ class TestPeer(ElectrumTestCase):
# alice sends htlc
route = await w1._create_route_from_invoice(decoded_invoice=lnaddr)
htlc = await p1.pay(route, alice_channel, int(lnaddr.amount * COIN * 1000), lnaddr.paymenthash, lnaddr.get_min_final_cltv_expiry())
# time to settle
#await asyncio.sleep(1)
# alice closes
await p1.close_channel(alice_channel.channel_id)
gath.cancel()
gath = asyncio.gather(pay(), p1._message_loop(), p2._message_loop())
async def set_settle():
await asyncio.sleep(0.1)
w2.enable_htlc_settle.set()
gath = asyncio.gather(pay(), set_settle(), p1._message_loop(), p2._message_loop())
async def f():
await gath
with self.assertRaises(concurrent.futures.CancelledError):