Skip to content

Commit 95e95b6

Browse files
committed
WIP - track local signatures of remote commitments
1 parent 0612949 commit 95e95b6

File tree

3 files changed

+95
-61
lines changed

3 files changed

+95
-61
lines changed

src/DotNetLightning.Core/Channel/Channel.fs

Lines changed: 69 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -246,33 +246,39 @@ module Channel =
246246
let remoteSigPairOfLocalTx = (theirFundingPk, TransactionSignature(msg.Signature.Value, SigHash.All))
247247
let sigPairs = seq [ remoteSigPairOfLocalTx; ]
248248
Transactions.checkTxFinalized signedLocalCommitTx state.LocalCommitTx.WhichInput sigPairs |> expectTransactionError
249-
let commitments = { Commitments.LocalParams = state.LocalParams
250-
RemoteParams = state.RemoteParams
251-
ChannelFlags = state.ChannelFlags
252-
FundingScriptCoin =
253-
let amount = state.FundingTx.Value.Outputs.[int state.LastSent.FundingOutputIndex.Value].Value
254-
ChannelHelpers.getFundingScriptCoin
255-
state.LocalParams.ChannelPubKeys.FundingPubKey
256-
remoteChannelKeys.FundingPubKey
257-
state.LastSent.FundingTxId
258-
state.LastSent.FundingOutputIndex
259-
amount
260-
LocalCommit = { Index = CommitmentNumber.FirstCommitment;
261-
Spec = state.LocalSpec;
262-
PublishableTxs = { PublishableTxs.CommitTx = finalizedLocalCommitTx
263-
HTLCTxs = [] }
264-
PendingHTLCSuccessTxs = [] }
265-
RemoteCommit = state.RemoteCommit
266-
LocalChanges = LocalChanges.Zero
267-
RemoteChanges = RemoteChanges.Zero
268-
LocalNextHTLCId = HTLCId.Zero
269-
RemoteNextHTLCId = HTLCId.Zero
270-
OriginChannels = Map.empty
271-
// we will receive their next per-commitment point in the next msg, so we temporarily put a random byte array
272-
RemoteNextCommitInfo = DataEncoders.HexEncoder().DecodeData("0101010101010101010101010101010101010101010101010101010101010101") |> fun h -> new Key(h) |> fun k -> k.PubKey |> PerCommitmentPoint |> RemoteNextCommitInfo.Revoked
273-
RemotePerCommitmentSecrets = PerCommitmentSecretStore()
274-
ChannelId =
275-
msg.ChannelId }
249+
let commitments = {
250+
Commitments.LocalParams = state.LocalParams
251+
RemoteParams = state.RemoteParams
252+
ChannelFlags = state.ChannelFlags
253+
FundingScriptCoin =
254+
let amount = state.FundingTx.Value.Outputs.[int state.LastSent.FundingOutputIndex.Value].Value
255+
ChannelHelpers.getFundingScriptCoin
256+
state.LocalParams.ChannelPubKeys.FundingPubKey
257+
remoteChannelKeys.FundingPubKey
258+
state.LastSent.FundingTxId
259+
state.LastSent.FundingOutputIndex
260+
amount
261+
LocalCommit = {
262+
Index = CommitmentNumber.FirstCommitment
263+
Spec = state.LocalSpec
264+
PublishableTxs = {
265+
PublishableTxs.CommitTx = finalizedLocalCommitTx
266+
HTLCTxs = []
267+
}
268+
PendingHTLCSuccessTxs = []
269+
}
270+
RemoteCommit = state.RemoteCommit
271+
LocalChanges = LocalChanges.Zero
272+
RemoteChanges = RemoteChanges.Zero
273+
LocalNextHTLCId = HTLCId.Zero
274+
RemoteNextHTLCId = HTLCId.Zero
275+
OriginChannels = Map.empty
276+
// we will receive their next per-commitment point in the next msg, so we temporarily put a random byte array
277+
RemoteNextCommitInfo = DataEncoders.HexEncoder().DecodeData("0101010101010101010101010101010101010101010101010101010101010101") |> fun h -> new Key(h) |> fun k -> k.PubKey |> PerCommitmentPoint |> RemoteNextCommitInfo.Revoked
278+
RemotePerCommitmentSecrets = PerCommitmentSecretStore()
279+
ChannelId = msg.ChannelId
280+
LocalSignaturesOfRemoteCommitments = [ state.LastSent.Signature ]
281+
}
276282
let nextState = { WaitForFundingConfirmedData.Commitments = commitments
277283
Deferred = None
278284
LastSent = Choice1Of2 state.LastSent
@@ -346,33 +352,42 @@ module Channel =
346352
cs.ChannelPrivKeys.SignWithFundingPrivKey remoteCommitTx.Value
347353
let channelId = OutPoint(msg.FundingTxId.Value, uint32 msg.FundingOutputIndex.Value).ToChannelId()
348354
let msgToSend: FundingSignedMsg = { ChannelId = channelId; Signature = !>localSigOfRemoteCommit.Signature }
349-
let commitments = { Commitments.LocalParams = state.LocalParams
350-
RemoteParams = state.RemoteParams
351-
ChannelFlags = state.ChannelFlags
352-
FundingScriptCoin =
353-
ChannelHelpers.getFundingScriptCoin
354-
state.LocalParams.ChannelPubKeys.FundingPubKey
355-
remoteChannelKeys.FundingPubKey
356-
msg.FundingTxId
357-
msg.FundingOutputIndex
358-
state.FundingSatoshis
359-
LocalCommit = { LocalCommit.Index = CommitmentNumber.FirstCommitment
360-
Spec = localSpec
361-
PublishableTxs = { PublishableTxs.CommitTx = finalizedCommitTx;
362-
HTLCTxs = [] }
363-
PendingHTLCSuccessTxs = [] }
364-
RemoteCommit = { RemoteCommit.Index = CommitmentNumber.FirstCommitment
365-
Spec = remoteSpec
366-
TxId = remoteCommitTx.Value.GetGlobalTransaction().GetTxId()
367-
RemotePerCommitmentPoint = state.RemoteFirstPerCommitmentPoint }
368-
LocalChanges = LocalChanges.Zero
369-
RemoteChanges = RemoteChanges.Zero
370-
LocalNextHTLCId = HTLCId.Zero
371-
RemoteNextHTLCId = HTLCId.Zero
372-
OriginChannels = Map.empty
373-
RemoteNextCommitInfo = DataEncoders.HexEncoder().DecodeData("0101010101010101010101010101010101010101010101010101010101010101") |> fun h -> new Key(h) |> fun k -> k.PubKey |> PerCommitmentPoint |> RemoteNextCommitInfo.Revoked
374-
RemotePerCommitmentSecrets = PerCommitmentSecretStore()
375-
ChannelId = channelId }
355+
let commitments = {
356+
Commitments.LocalParams = state.LocalParams
357+
RemoteParams = state.RemoteParams
358+
ChannelFlags = state.ChannelFlags
359+
FundingScriptCoin =
360+
ChannelHelpers.getFundingScriptCoin
361+
state.LocalParams.ChannelPubKeys.FundingPubKey
362+
remoteChannelKeys.FundingPubKey
363+
msg.FundingTxId
364+
msg.FundingOutputIndex
365+
state.FundingSatoshis
366+
LocalCommit = {
367+
LocalCommit.Index = CommitmentNumber.FirstCommitment
368+
Spec = localSpec
369+
PublishableTxs = {
370+
PublishableTxs.CommitTx = finalizedCommitTx
371+
HTLCTxs = []
372+
}
373+
PendingHTLCSuccessTxs = []
374+
}
375+
RemoteCommit = {
376+
RemoteCommit.Index = CommitmentNumber.FirstCommitment
377+
Spec = remoteSpec
378+
TxId = remoteCommitTx.Value.GetGlobalTransaction().GetTxId()
379+
RemotePerCommitmentPoint = state.RemoteFirstPerCommitmentPoint
380+
}
381+
LocalChanges = LocalChanges.Zero
382+
RemoteChanges = RemoteChanges.Zero
383+
LocalNextHTLCId = HTLCId.Zero
384+
RemoteNextHTLCId = HTLCId.Zero
385+
OriginChannels = Map.empty
386+
RemoteNextCommitInfo = DataEncoders.HexEncoder().DecodeData("0101010101010101010101010101010101010101010101010101010101010101") |> fun h -> new Key(h) |> fun k -> k.PubKey |> PerCommitmentPoint |> RemoteNextCommitInfo.Revoked
387+
RemotePerCommitmentSecrets = PerCommitmentSecretStore()
388+
ChannelId = channelId
389+
LocalSignaturesOfRemoteCommitments = [ !>localSigOfRemoteCommit.Signature ]
390+
}
376391
let nextState = { WaitForFundingConfirmedData.Commitments = commitments
377392
Deferred = None
378393
LastSent = msgToSend |> Choice2Of2

src/DotNetLightning.Core/Channel/Commitments.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ type Commitments = {
130130
RemoteNextCommitInfo: RemoteNextCommitInfo
131131
RemotePerCommitmentSecrets: PerCommitmentSecretStore
132132
ChannelId: ChannelId
133+
LocalSignaturesOfRemoteCommitments: List<LNECDSASignature>
133134
}
134135
with
135136
static member LocalChanges_: Lens<_, _> =

src/DotNetLightning.Core/Channel/CommitmentsModule.fs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,24 @@ module internal Commitments =
303303
SentAfterLocalCommitmentIndex = cm.LocalCommit.Index
304304
ReSignASAP = false
305305
}
306-
{ cm with RemoteNextCommitInfo = RemoteNextCommitInfo.Waiting(nextRemoteCommitInfo)
307-
LocalChanges = { cm.LocalChanges with Proposed = []; Signed = cm.LocalChanges.Proposed }
308-
RemoteChanges = { cm.RemoteChanges with ACKed = []; Signed = cm.RemoteChanges.ACKed } }
306+
{
307+
cm with
308+
RemoteNextCommitInfo = RemoteNextCommitInfo.Waiting(nextRemoteCommitInfo)
309+
LocalChanges = {
310+
cm.LocalChanges with
311+
Proposed = []
312+
Signed = cm.LocalChanges.Proposed
313+
}
314+
RemoteChanges = {
315+
cm.RemoteChanges with
316+
ACKed = []
317+
Signed = cm.RemoteChanges.ACKed
318+
}
319+
LocalSignaturesOfRemoteCommitments =
320+
List.append
321+
cm.LocalSignaturesOfRemoteCommitments
322+
[ !> signature.Signature ]
323+
}
309324
return [ WeAcceptedOperationSign (msg, nextCommitments) ]
310325
}
311326
| RemoteNextCommitInfo.Waiting _ ->
@@ -409,10 +424,13 @@ module internal Commitments =
409424
|> Map.toSeq |> Seq.map (fun (k, _) -> k) |> Set.ofSeq
410425
Set.difference t1 t2
411426
let originChannels1 = cm.OriginChannels |> Map.filter(fun k _ -> Set.contains k completedOutgoingHTLCs)
412-
{ cm with LocalCommit = localCommit1
413-
LocalChanges = ourChanges1
414-
RemoteChanges = theirChanges1
415-
OriginChannels = originChannels1 }
427+
{
428+
cm with
429+
LocalCommit = localCommit1
430+
LocalChanges = ourChanges1
431+
RemoteChanges = theirChanges1
432+
OriginChannels = originChannels1
433+
}
416434
return [ WeAcceptedCommitmentSigned(nextMsg, nextCommitments) ;]
417435
}
418436

0 commit comments

Comments
 (0)