mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-09-13 05:59:51 +00:00
Replace lightning_settle_delay with enable_htlc_settle (asyncio.Event)
This commit is contained in:
parent
ce81957d25
commit
077f778632
5 changed files with 27 additions and 16 deletions
|
@ -992,6 +992,11 @@ class Commands:
|
||||||
self.network.config.fee_estimates = ast.literal_eval(fees)
|
self.network.config.fee_estimates = ast.literal_eval(fees)
|
||||||
self.network.notify('fee')
|
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')
|
@command('n')
|
||||||
async def clear_ln_blacklist(self):
|
async def clear_ln_blacklist(self):
|
||||||
self.network.path_finder.blacklist.clear()
|
self.network.path_finder.blacklist.clear()
|
||||||
|
|
|
@ -1282,7 +1282,7 @@ class Peer(Logger):
|
||||||
await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason)
|
await self.fail_htlc(chan, htlc.htlc_id, onion_packet, reason)
|
||||||
return
|
return
|
||||||
#self.network.trigger_callback('htlc_added', htlc, invoice, RECEIVED)
|
#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)
|
await self._fulfill_htlc(chan, htlc.htlc_id, preimage)
|
||||||
|
|
||||||
async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
|
async def _fulfill_htlc(self, chan: Channel, htlc_id: int, preimage: bytes):
|
||||||
|
|
|
@ -385,6 +385,9 @@ class LNWallet(LNWorker):
|
||||||
self.sweep_address = wallet.get_receiving_address()
|
self.sweep_address = wallet.get_receiving_address()
|
||||||
self.lock = threading.RLock()
|
self.lock = threading.RLock()
|
||||||
self.logs = defaultdict(list) # type: Dict[str, List[PaymentAttemptLog]] # key is RHASH
|
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!
|
# note: accessing channels (besides simple lookup) needs self.lock!
|
||||||
self.channels = {}
|
self.channels = {}
|
||||||
|
|
|
@ -162,7 +162,7 @@ if [[ $1 == "breach" ]]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [[ $1 == "redeem_htlcs" ]]; then
|
if [[ $1 == "redeem_htlcs" ]]; then
|
||||||
$bob setconfig lightning_settle_delay 10
|
$bob enable_htlc_settle false
|
||||||
wait_for_balance alice 1
|
wait_for_balance alice 1
|
||||||
echo "alice opens channel"
|
echo "alice opens channel"
|
||||||
bob_node=$($bob nodeid)
|
bob_node=$($bob nodeid)
|
||||||
|
@ -172,10 +172,9 @@ if [[ $1 == "redeem_htlcs" ]]; then
|
||||||
# alice pays bob
|
# alice pays bob
|
||||||
invoice=$($bob add_lightning_request 0.05 -m "test")
|
invoice=$($bob add_lightning_request 0.05 -m "test")
|
||||||
$alice lnpay $invoice --timeout=1 || true
|
$alice lnpay $invoice --timeout=1 || true
|
||||||
sleep 1
|
|
||||||
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
||||||
if [[ "$settled" != "0" ]]; then
|
if [[ "$settled" != "0" ]]; then
|
||||||
echo 'SETTLE_DELAY did not work'
|
echo 'enable_htlc_settle did not work'
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
# bob goes away
|
# bob goes away
|
||||||
|
@ -205,7 +204,7 @@ fi
|
||||||
|
|
||||||
|
|
||||||
if [[ $1 == "breach_with_unspent_htlc" ]]; then
|
if [[ $1 == "breach_with_unspent_htlc" ]]; then
|
||||||
$bob setconfig lightning_settle_delay 3
|
$bob enable_htlc_settle false
|
||||||
wait_for_balance alice 1
|
wait_for_balance alice 1
|
||||||
echo "alice opens channel"
|
echo "alice opens channel"
|
||||||
bob_node=$($bob nodeid)
|
bob_node=$($bob nodeid)
|
||||||
|
@ -217,14 +216,14 @@ if [[ $1 == "breach_with_unspent_htlc" ]]; then
|
||||||
$alice lnpay $invoice --timeout=1 || true
|
$alice lnpay $invoice --timeout=1 || true
|
||||||
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
||||||
if [[ "$settled" != "0" ]]; then
|
if [[ "$settled" != "0" ]]; then
|
||||||
echo "SETTLE_DELAY did not work, $settled != 0"
|
echo "enable_htlc_settle did not work, $settled != 0"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
ctx=$($alice get_channel_ctx $channel)
|
ctx=$($alice get_channel_ctx $channel)
|
||||||
sleep 5
|
$bob enable_htlc_settle true
|
||||||
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
||||||
if [[ "$settled" != "1" ]]; then
|
if [[ "$settled" != "1" ]]; then
|
||||||
echo "SETTLE_DELAY did not work, $settled != 1"
|
echo "enable_htlc_settle did not work, $settled != 1"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "alice breaches with old ctx"
|
echo "alice breaches with old ctx"
|
||||||
|
@ -234,7 +233,7 @@ fi
|
||||||
|
|
||||||
|
|
||||||
if [[ $1 == "breach_with_spent_htlc" ]]; then
|
if [[ $1 == "breach_with_spent_htlc" ]]; then
|
||||||
$bob setconfig lightning_settle_delay 3
|
$bob enable_htlc_settle false
|
||||||
wait_for_balance alice 1
|
wait_for_balance alice 1
|
||||||
echo "alice opens channel"
|
echo "alice opens channel"
|
||||||
bob_node=$($bob nodeid)
|
bob_node=$($bob nodeid)
|
||||||
|
@ -247,14 +246,14 @@ if [[ $1 == "breach_with_spent_htlc" ]]; then
|
||||||
ctx=$($alice get_channel_ctx $channel)
|
ctx=$($alice get_channel_ctx $channel)
|
||||||
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
||||||
if [[ "$settled" != "0" ]]; then
|
if [[ "$settled" != "0" ]]; then
|
||||||
echo "SETTLE_DELAY did not work, $settled != 0"
|
echo "enable_htlc_settle did not work, $settled != 0"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
cp /tmp/alice/regtest/wallets/default_wallet /tmp/alice/regtest/wallets/toxic_wallet
|
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')
|
settled=$($alice list_channels | jq '.[] | .local_htlcs | .settles | length')
|
||||||
if [[ "$settled" != "1" ]]; then
|
if [[ "$settled" != "1" ]]; then
|
||||||
echo "SETTLE_DELAY did not work, $settled != 1"
|
echo "enable_htlc_settle did not work, $settled != 1"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo $($bob getbalance)
|
echo $($bob getbalance)
|
||||||
|
|
|
@ -91,6 +91,9 @@ class MockLNWallet:
|
||||||
self.pending_payments = defaultdict(asyncio.Future)
|
self.pending_payments = defaultdict(asyncio.Future)
|
||||||
chan.lnworker = self
|
chan.lnworker = self
|
||||||
chan.node_id = remote_keypair.pubkey
|
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):
|
def get_invoice_status(self, key):
|
||||||
pass
|
pass
|
||||||
|
@ -295,7 +298,7 @@ class TestPeer(ElectrumTestCase):
|
||||||
w2.network.config.set_key('dynamic_fees', False)
|
w2.network.config.set_key('dynamic_fees', False)
|
||||||
w1.network.config.set_key('fee_per_kb', 5000)
|
w1.network.config.set_key('fee_per_kb', 5000)
|
||||||
w2.network.config.set_key('fee_per_kb', 1000)
|
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)
|
pay_req = self.prepare_invoice(w2)
|
||||||
lnaddr = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
|
lnaddr = lndecode(pay_req, expected_hrp=constants.net.SEGWIT_HRP)
|
||||||
async def pay():
|
async def pay():
|
||||||
|
@ -304,12 +307,13 @@ class TestPeer(ElectrumTestCase):
|
||||||
# alice sends htlc
|
# alice sends htlc
|
||||||
route = await w1._create_route_from_invoice(decoded_invoice=lnaddr)
|
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())
|
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
|
# alice closes
|
||||||
await p1.close_channel(alice_channel.channel_id)
|
await p1.close_channel(alice_channel.channel_id)
|
||||||
gath.cancel()
|
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():
|
async def f():
|
||||||
await gath
|
await gath
|
||||||
with self.assertRaises(concurrent.futures.CancelledError):
|
with self.assertRaises(concurrent.futures.CancelledError):
|
||||||
|
|
Loading…
Add table
Reference in a new issue