Skip to content

Commit c25b046

Browse files
committed
Backend(LN),End2End: fix force close with lnd
1 parent 9e1538f commit c25b046

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

src/GWallet.Backend.Tests.End2End/LN.fs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,69 @@ type LN() =
608608
return ()
609609
}
610610

611+
[<Test>]
612+
member __.``can force-close channel with lnd``() = Async.RunSynchronously <| async {
613+
let! channelId, clientWallet, bitcoind, electrumServer, lnd, _fundingAmount =
614+
try
615+
OpenChannelWithFundee None
616+
with
617+
| ex ->
618+
Assert.Fail (
619+
sprintf
620+
"Inconclusive: channel-closing inconclusive because Channel open failed, fix this first: %s"
621+
(ex.ToString())
622+
)
623+
failwith "unreachable"
624+
625+
let! _forceCloseTxId = (Lightning.Node.Client clientWallet.NodeClient).ForceCloseChannel channelId
626+
627+
let locallyForceClosedData =
628+
match (clientWallet.ChannelStore.ChannelInfo channelId).Status with
629+
| ChannelStatus.LocallyForceClosed locallyForceClosedData ->
630+
locallyForceClosedData
631+
| status -> failwith (SPrintF1 "unexpected channel status. Expected LocallyForceClosed, got %A" status)
632+
633+
// wait for force-close transaction to appear in mempool
634+
while bitcoind.GetTxIdsInMempool().Length = 0 do
635+
do! Async.Sleep 500
636+
637+
Infrastructure.LogDebug (SPrintF1 "the time lock is %i blocks" locallyForceClosedData.ToSelfDelay)
638+
639+
let! balanceBeforeFundsReclaimed = clientWallet.GetBalance()
640+
641+
// Mine the force-close tx into a block
642+
bitcoind.GenerateBlocksToDummyAddress (BlockHeightOffset32 1u)
643+
644+
// Mine blocks to release time-lock
645+
bitcoind.GenerateBlocksToDummyAddress
646+
(BlockHeightOffset32 (uint32 locallyForceClosedData.ToSelfDelay))
647+
648+
let! spendingTxResult =
649+
let commitmentTx = clientWallet.ChannelStore.GetCommitmentTx channelId
650+
(Lightning.Node.Client clientWallet.NodeClient).CreateRecoveryTxForForceClose
651+
channelId
652+
commitmentTx
653+
654+
let recoveryTx = UnwrapResult spendingTxResult "Local output is dust, recovery tx cannot be created"
655+
656+
let! _recoveryTxId =
657+
ChannelManager.BroadcastRecoveryTxAndCloseChannel recoveryTx clientWallet.ChannelStore
658+
659+
// wait for spending transaction to appear in mempool
660+
while bitcoind.GetTxIdsInMempool().Length = 0 do
661+
do! Async.Sleep 500
662+
663+
// Mine the spending tx into a block
664+
bitcoind.GenerateBlocksToDummyAddress (BlockHeightOffset32 1u)
665+
666+
Infrastructure.LogDebug "waiting for our wallet balance to increase"
667+
let! _balanceAfterFundsReclaimed =
668+
let amount = balanceBeforeFundsReclaimed + Money(1.0m, MoneyUnit.Satoshi)
669+
clientWallet.WaitForBalance amount
670+
671+
TearDown clientWallet bitcoind electrumServer lnd
672+
}
673+
611674
[<Category "G2G_ChannelLocalForceClosing_Funder">]
612675
[<Test>]
613676
member __.``can send monohop payments and handle local force-close of channel (funder)``() = Async.RunSynchronously <| async {

src/GWallet.Backend/UtxoCoin/Lightning/ChannelManagement.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type ChannelInfo =
9696
ChannelStatus.LocallyForceClosed {
9797
Network = serializedChannel.SavedChannelState.StaticChannelConfig.Network
9898
Currency = currency
99-
ToSelfDelay = serializedChannel.SavedChannelState.StaticChannelConfig.LocalParams.ToSelfDelay.Value
99+
ToSelfDelay = serializedChannel.SavedChannelState.StaticChannelConfig.RemoteParams.ToSelfDelay.Value
100100
ForceCloseTxId = forceCloseTxId
101101
ClosingTimestampUtc = UnwrapOption serializedChannel.ClosingTimestampUtc "BUG: closing date is empty after local force close"
102102
}

0 commit comments

Comments
 (0)