mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-02-23 06:35:07 +01:00
lnwallet+htlcswitch: add NewCommitState struct, modify send/recv sig to accept
In this commit, we add a new NewCommitState struct. This preps us for the future change wherein a partial signature is also added to the mix. All related tests and type signatures have also been updated accordingly.
This commit is contained in:
parent
72d41ae408
commit
11c62e3951
9 changed files with 342 additions and 322 deletions
|
@ -978,7 +978,7 @@ func initBreachedState(t *testing.T) (*BreachArbiter,
|
||||||
if _, err := bob.ReceiveHTLC(htlc); err != nil {
|
if _, err := bob.ReceiveHTLC(htlc); err != nil {
|
||||||
t.Fatalf("bob unable to recv add htlc: %v", err)
|
t.Fatalf("bob unable to recv add htlc: %v", err)
|
||||||
}
|
}
|
||||||
if err := forceStateTransition(alice, bob); err != nil {
|
if err := lnwallet.ForceStateTransition(alice, bob); err != nil {
|
||||||
t.Fatalf("Can't update the channel state: %v", err)
|
t.Fatalf("Can't update the channel state: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -996,7 +996,7 @@ func initBreachedState(t *testing.T) (*BreachArbiter,
|
||||||
if _, err := bob.ReceiveHTLC(htlc2); err != nil {
|
if _, err := bob.ReceiveHTLC(htlc2); err != nil {
|
||||||
t.Fatalf("bob unable to recv add htlc: %v", err)
|
t.Fatalf("bob unable to recv add htlc: %v", err)
|
||||||
}
|
}
|
||||||
if err := forceStateTransition(alice, bob); err != nil {
|
if err := lnwallet.ForceStateTransition(alice, bob); err != nil {
|
||||||
t.Fatalf("Can't update the channel state: %v", err)
|
t.Fatalf("Can't update the channel state: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2436,45 +2436,3 @@ func createHTLC(data int, amount lnwire.MilliSatoshi) (*lnwire.UpdateAddHTLC, [3
|
||||||
Expiry: uint32(5),
|
Expiry: uint32(5),
|
||||||
}, returnPreimage
|
}, returnPreimage
|
||||||
}
|
}
|
||||||
|
|
||||||
// forceStateTransition executes the necessary interaction between the two
|
|
||||||
// commitment state machines to transition to a new state locking in any
|
|
||||||
// pending updates.
|
|
||||||
// TODO(conner) remove code duplication
|
|
||||||
func forceStateTransition(chanA, chanB *lnwallet.LightningChannel) error {
|
|
||||||
aliceSig, aliceHtlcSigs, _, err := chanA.SignNextCommitment()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err = chanB.ReceiveNewCommitment(aliceSig, aliceHtlcSigs); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
bobRevocation, _, _, err := chanB.RevokeCurrentCommitment()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
bobSig, bobHtlcSigs, _, err := chanB.SignNextCommitment()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, _, _, err = chanA.ReceiveRevocation(bobRevocation)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if err := chanA.ReceiveNewCommitment(bobSig, bobHtlcSigs); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
aliceRevocation, _, _, err := chanA.RevokeCurrentCommitment()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, _, _, _, err = chanB.ReceiveRevocation(aliceRevocation)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -145,7 +145,7 @@ func TestChainWatcherRemoteUnilateralClosePendingCommit(t *testing.T) {
|
||||||
|
|
||||||
// With the HTLC added, we'll now manually initiate a state transition
|
// With the HTLC added, we'll now manually initiate a state transition
|
||||||
// from Alice to Bob.
|
// from Alice to Bob.
|
||||||
_, _, _, err = aliceChannel.SignNextCommitment()
|
_, err = aliceChannel.SignNextCommitment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1899,7 +1899,10 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) {
|
||||||
// We just received a new updates to our local commitment
|
// We just received a new updates to our local commitment
|
||||||
// chain, validate this new commitment, closing the link if
|
// chain, validate this new commitment, closing the link if
|
||||||
// invalid.
|
// invalid.
|
||||||
err = l.channel.ReceiveNewCommitment(msg.CommitSig, msg.HtlcSigs)
|
err = l.channel.ReceiveNewCommitment(&lnwallet.CommitSigs{
|
||||||
|
CommitSig: msg.CommitSig,
|
||||||
|
HtlcSigs: msg.HtlcSigs,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If we were unable to reconstruct their proposed
|
// If we were unable to reconstruct their proposed
|
||||||
// commitment, then we'll examine the type of error. If
|
// commitment, then we'll examine the type of error. If
|
||||||
|
@ -2215,7 +2218,7 @@ func (l *channelLink) updateCommitTx() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
theirCommitSig, htlcSigs, pendingHTLCs, err := l.channel.SignNextCommitment()
|
newCommit, err := l.channel.SignNextCommitment()
|
||||||
if err == lnwallet.ErrNoWindow {
|
if err == lnwallet.ErrNoWindow {
|
||||||
l.cfg.PendingCommitTicker.Resume()
|
l.cfg.PendingCommitTicker.Resume()
|
||||||
l.log.Trace("PendingCommitTicker resumed")
|
l.log.Trace("PendingCommitTicker resumed")
|
||||||
|
@ -2247,7 +2250,7 @@ func (l *channelLink) updateCommitTx() error {
|
||||||
// pending).
|
// pending).
|
||||||
newUpdate := &contractcourt.ContractUpdate{
|
newUpdate := &contractcourt.ContractUpdate{
|
||||||
HtlcKey: contractcourt.RemotePendingHtlcSet,
|
HtlcKey: contractcourt.RemotePendingHtlcSet,
|
||||||
Htlcs: pendingHTLCs,
|
Htlcs: newCommit.PendingHTLCs,
|
||||||
}
|
}
|
||||||
err = l.cfg.NotifyContractUpdate(newUpdate)
|
err = l.cfg.NotifyContractUpdate(newUpdate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -2262,9 +2265,9 @@ func (l *channelLink) updateCommitTx() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
commitSig := &lnwire.CommitSig{
|
commitSig := &lnwire.CommitSig{
|
||||||
ChanID: l.ChanID(),
|
ChanID: l.ChanID(),
|
||||||
CommitSig: theirCommitSig,
|
CommitSig: newCommit.CommitSig,
|
||||||
HtlcSigs: htlcSigs,
|
HtlcSigs: newCommit.HtlcSigs,
|
||||||
}
|
}
|
||||||
l.cfg.Peer.SendMessage(false, commitSig)
|
l.cfg.Peer.SendMessage(false, commitSig)
|
||||||
|
|
||||||
|
|
|
@ -93,14 +93,14 @@ func (l *linkTestContext) receiveHtlcAliceToBob() {
|
||||||
func (l *linkTestContext) sendCommitSigBobToAlice(expHtlcs int) {
|
func (l *linkTestContext) sendCommitSigBobToAlice(expHtlcs int) {
|
||||||
l.t.Helper()
|
l.t.Helper()
|
||||||
|
|
||||||
sig, htlcSigs, _, err := l.bobChannel.SignNextCommitment()
|
sigs, err := l.bobChannel.SignNextCommitment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.t.Fatalf("error signing commitment: %v", err)
|
l.t.Fatalf("error signing commitment: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
commitSig := &lnwire.CommitSig{
|
commitSig := &lnwire.CommitSig{
|
||||||
CommitSig: sig,
|
CommitSig: sigs.CommitSig,
|
||||||
HtlcSigs: htlcSigs,
|
HtlcSigs: sigs.HtlcSigs,
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(commitSig.HtlcSigs) != expHtlcs {
|
if len(commitSig.HtlcSigs) != expHtlcs {
|
||||||
|
@ -141,9 +141,10 @@ func (l *linkTestContext) receiveCommitSigAliceToBob(expHtlcs int) {
|
||||||
|
|
||||||
comSig := l.receiveCommitSigAlice(expHtlcs)
|
comSig := l.receiveCommitSigAlice(expHtlcs)
|
||||||
|
|
||||||
err := l.bobChannel.ReceiveNewCommitment(
|
err := l.bobChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{
|
||||||
comSig.CommitSig, comSig.HtlcSigs,
|
CommitSig: comSig.CommitSig,
|
||||||
)
|
HtlcSigs: comSig.HtlcSigs,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.t.Fatalf("bob failed receiving commitment: %v", err)
|
l.t.Fatalf("bob failed receiving commitment: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2062,8 +2062,10 @@ func handleStateUpdate(link *channelLink,
|
||||||
|
|
||||||
// Let the remote channel receive the commit sig, and
|
// Let the remote channel receive the commit sig, and
|
||||||
// respond with a revocation + commitsig.
|
// respond with a revocation + commitsig.
|
||||||
err := remoteChannel.ReceiveNewCommitment(
|
err := remoteChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{
|
||||||
commitSig.CommitSig, commitSig.HtlcSigs)
|
CommitSig: commitSig.CommitSig,
|
||||||
|
HtlcSigs: commitSig.HtlcSigs,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -2074,13 +2076,13 @@ func handleStateUpdate(link *channelLink,
|
||||||
}
|
}
|
||||||
link.HandleChannelUpdate(remoteRev)
|
link.HandleChannelUpdate(remoteRev)
|
||||||
|
|
||||||
remoteSig, remoteHtlcSigs, _, err := remoteChannel.SignNextCommitment()
|
remoteSigs, err := remoteChannel.SignNextCommitment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
commitSig = &lnwire.CommitSig{
|
commitSig = &lnwire.CommitSig{
|
||||||
CommitSig: remoteSig,
|
CommitSig: remoteSigs.CommitSig,
|
||||||
HtlcSigs: remoteHtlcSigs,
|
HtlcSigs: remoteSigs.HtlcSigs,
|
||||||
}
|
}
|
||||||
link.HandleChannelUpdate(commitSig)
|
link.HandleChannelUpdate(commitSig)
|
||||||
|
|
||||||
|
@ -2125,14 +2127,14 @@ func updateState(batchTick chan time.Time, link *channelLink,
|
||||||
|
|
||||||
// The remote is triggering the state update, emulate this by
|
// The remote is triggering the state update, emulate this by
|
||||||
// signing and sending CommitSig to the link.
|
// signing and sending CommitSig to the link.
|
||||||
remoteSig, remoteHtlcSigs, _, err := remoteChannel.SignNextCommitment()
|
remoteSigs, err := remoteChannel.SignNextCommitment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
commitSig := &lnwire.CommitSig{
|
commitSig := &lnwire.CommitSig{
|
||||||
CommitSig: remoteSig,
|
CommitSig: remoteSigs.CommitSig,
|
||||||
HtlcSigs: remoteHtlcSigs,
|
HtlcSigs: remoteSigs.HtlcSigs,
|
||||||
}
|
}
|
||||||
link.HandleChannelUpdate(commitSig)
|
link.HandleChannelUpdate(commitSig)
|
||||||
|
|
||||||
|
@ -2165,8 +2167,10 @@ func updateState(batchTick chan time.Time, link *channelLink,
|
||||||
return fmt.Errorf("expected CommitSig, got %T", msg)
|
return fmt.Errorf("expected CommitSig, got %T", msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = remoteChannel.ReceiveNewCommitment(
|
err = remoteChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{
|
||||||
commitSig.CommitSig, commitSig.HtlcSigs)
|
CommitSig: commitSig.CommitSig,
|
||||||
|
HtlcSigs: commitSig.HtlcSigs,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -3239,7 +3243,10 @@ func TestChannelLinkTrimCircuitsRemoteCommit(t *testing.T) {
|
||||||
t.Fatalf("alice did not send commitment signature")
|
t.Fatalf("alice did not send commitment signature")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := bobChan.ReceiveNewCommitment(sig.CommitSig, sig.HtlcSigs)
|
err := bobChan.ReceiveNewCommitment(&lnwallet.CommitSigs{
|
||||||
|
CommitSig: sig.CommitSig,
|
||||||
|
HtlcSigs: sig.HtlcSigs,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to receive new commitment: %v", err)
|
t.Fatalf("unable to receive new commitment: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -4839,9 +4846,10 @@ func TestChannelLinkNoEmptySig(t *testing.T) {
|
||||||
ctx.sendCommitSigBobToAlice(1)
|
ctx.sendCommitSigBobToAlice(1)
|
||||||
|
|
||||||
// Now send Bob the signature from Alice covering both htlcs.
|
// Now send Bob the signature from Alice covering both htlcs.
|
||||||
err = bobChannel.ReceiveNewCommitment(
|
err = bobChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{
|
||||||
commitSigAlice.CommitSig, commitSigAlice.HtlcSigs,
|
CommitSig: commitSigAlice.CommitSig,
|
||||||
)
|
HtlcSigs: commitSigAlice.HtlcSigs,
|
||||||
|
})
|
||||||
require.NoError(t, err, "bob failed receiving commitment")
|
require.NoError(t, err, "bob failed receiving commitment")
|
||||||
|
|
||||||
// Both Alice and Bob revoke their previous commitment txes.
|
// Both Alice and Bob revoke their previous commitment txes.
|
||||||
|
@ -5443,8 +5451,7 @@ func TestChannelLinkFail(t *testing.T) {
|
||||||
|
|
||||||
// Sign a commitment that will include
|
// Sign a commitment that will include
|
||||||
// signature for the HTLC just sent.
|
// signature for the HTLC just sent.
|
||||||
sig, htlcSigs, _, err :=
|
sigs, err := remoteChannel.SignNextCommitment()
|
||||||
remoteChannel.SignNextCommitment()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error signing commitment: %v",
|
t.Fatalf("error signing commitment: %v",
|
||||||
err)
|
err)
|
||||||
|
@ -5453,8 +5460,8 @@ func TestChannelLinkFail(t *testing.T) {
|
||||||
// Remove the HTLC sig, such that the commit
|
// Remove the HTLC sig, such that the commit
|
||||||
// sig will be invalid.
|
// sig will be invalid.
|
||||||
commitSig := &lnwire.CommitSig{
|
commitSig := &lnwire.CommitSig{
|
||||||
CommitSig: sig,
|
CommitSig: sigs.CommitSig,
|
||||||
HtlcSigs: htlcSigs[1:],
|
HtlcSigs: sigs.HtlcSigs[1:],
|
||||||
}
|
}
|
||||||
|
|
||||||
c.HandleChannelUpdate(commitSig)
|
c.HandleChannelUpdate(commitSig)
|
||||||
|
@ -5485,8 +5492,7 @@ func TestChannelLinkFail(t *testing.T) {
|
||||||
|
|
||||||
// Sign a commitment that will include
|
// Sign a commitment that will include
|
||||||
// signature for the HTLC just sent.
|
// signature for the HTLC just sent.
|
||||||
sig, htlcSigs, _, err :=
|
sigs, err := remoteChannel.SignNextCommitment()
|
||||||
remoteChannel.SignNextCommitment()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("error signing commitment: %v",
|
t.Fatalf("error signing commitment: %v",
|
||||||
err)
|
err)
|
||||||
|
@ -5494,7 +5500,7 @@ func TestChannelLinkFail(t *testing.T) {
|
||||||
|
|
||||||
// Flip a bit on the signature, rendering it
|
// Flip a bit on the signature, rendering it
|
||||||
// invalid.
|
// invalid.
|
||||||
sigCopy := sig.Copy()
|
sigCopy := sigs.CommitSig.Copy()
|
||||||
copyBytes := sigCopy.RawBytes()
|
copyBytes := sigCopy.RawBytes()
|
||||||
copyBytes[19] ^= 1
|
copyBytes[19] ^= 1
|
||||||
modifiedSig, err := lnwire.NewSigFromWireECDSA(
|
modifiedSig, err := lnwire.NewSigFromWireECDSA(
|
||||||
|
@ -5503,7 +5509,7 @@ func TestChannelLinkFail(t *testing.T) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
commitSig := &lnwire.CommitSig{
|
commitSig := &lnwire.CommitSig{
|
||||||
CommitSig: modifiedSig,
|
CommitSig: modifiedSig,
|
||||||
HtlcSigs: htlcSigs,
|
HtlcSigs: sigs.HtlcSigs,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.HandleChannelUpdate(commitSig)
|
c.HandleChannelUpdate(commitSig)
|
||||||
|
|
|
@ -3716,6 +3716,30 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CommitSig holds the set of related signatures for a new commitment
|
||||||
|
// transaction state.
|
||||||
|
type CommitSigs struct {
|
||||||
|
// CommitSig is the normal commitment signature. This will only be a
|
||||||
|
// non-zero commitment signature for taproot channels.
|
||||||
|
CommitSig lnwire.Sig
|
||||||
|
|
||||||
|
// HtlcSigs is the set of signatures for all HTLCs in the commitment
|
||||||
|
// transaction. Depending on the channel type, these will either be
|
||||||
|
// ECDSA or Schnorr signatures.
|
||||||
|
HtlcSigs []lnwire.Sig
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewCommitState wraps the various signatures needed to properly
|
||||||
|
// propose/accept a new commitment state. This includes the signer's nonce for
|
||||||
|
// musig2 channels.
|
||||||
|
type NewCommitState struct {
|
||||||
|
*CommitSigs
|
||||||
|
|
||||||
|
// PendingHTLCs is the set of new/pending HTLCs produced by this
|
||||||
|
// commitment state.
|
||||||
|
PendingHTLCs []channeldb.HTLC
|
||||||
|
}
|
||||||
|
|
||||||
// SignNextCommitment signs a new commitment which includes any previous
|
// SignNextCommitment signs a new commitment which includes any previous
|
||||||
// unsettled HTLCs, any new HTLCs, and any modifications to prior HTLCs
|
// unsettled HTLCs, any new HTLCs, and any modifications to prior HTLCs
|
||||||
// committed in previous commitment updates. Signing a new commitment
|
// committed in previous commitment updates. Signing a new commitment
|
||||||
|
@ -3727,8 +3751,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter,
|
||||||
// any). The HTLC signatures are sorted according to the BIP 69 order of the
|
// any). The HTLC signatures are sorted according to the BIP 69 order of the
|
||||||
// HTLC's on the commitment transaction. Finally, the new set of pending HTLCs
|
// HTLC's on the commitment transaction. Finally, the new set of pending HTLCs
|
||||||
// for the remote party's commitment are also returned.
|
// for the remote party's commitment are also returned.
|
||||||
func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) {
|
||||||
[]channeldb.HTLC, error) {
|
|
||||||
|
|
||||||
lc.Lock()
|
lc.Lock()
|
||||||
defer lc.Unlock()
|
defer lc.Unlock()
|
||||||
|
@ -3755,7 +3778,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
if unacked || commitPoint == nil {
|
if unacked || commitPoint == nil {
|
||||||
lc.log.Tracef("waiting for remote ack=%v, nil "+
|
lc.log.Tracef("waiting for remote ack=%v, nil "+
|
||||||
"RemoteNextRevocation: %v", unacked, commitPoint == nil)
|
"RemoteNextRevocation: %v", unacked, commitPoint == nil)
|
||||||
return sig, htlcSigs, nil, ErrNoWindow
|
return nil, ErrNoWindow
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine the last update on the remote log that has been locked in.
|
// Determine the last update on the remote log that has been locked in.
|
||||||
|
@ -3770,7 +3793,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
remoteACKedIndex, lc.localUpdateLog.logIndex, true, nil, nil,
|
remoteACKedIndex, lc.localUpdateLog.logIndex, true, nil, nil,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the next commitment point for the remote party. This will be
|
// Grab the next commitment point for the remote party. This will be
|
||||||
|
@ -3793,7 +3816,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
remoteACKedIndex, remoteHtlcIndex, keyRing,
|
remoteACKedIndex, remoteHtlcIndex, keyRing,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
lc.log.Tracef("extending remote chain to height %v, "+
|
lc.log.Tracef("extending remote chain to height %v, "+
|
||||||
|
@ -3824,7 +3847,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
&lc.channelState.RemoteChanCfg, newCommitView,
|
&lc.channelState.RemoteChanCfg, newCommitView,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
lc.sigPool.SubmitSignBatch(sigBatch)
|
lc.sigPool.SubmitSignBatch(sigBatch)
|
||||||
|
|
||||||
|
@ -3835,12 +3858,12 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
rawSig, err := lc.Signer.SignOutputRaw(newCommitView.txn, lc.signDesc)
|
rawSig, err := lc.Signer.SignOutputRaw(newCommitView.txn, lc.signDesc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
close(cancelChan)
|
close(cancelChan)
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
sig, err = lnwire.NewSigFromSignature(rawSig)
|
sig, err = lnwire.NewSigFromSignature(rawSig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
close(cancelChan)
|
close(cancelChan)
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We'll need to send over the signatures to the remote party in the
|
// We'll need to send over the signatures to the remote party in the
|
||||||
|
@ -3860,7 +3883,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
// jobs.
|
// jobs.
|
||||||
if jobResp.Err != nil {
|
if jobResp.Err != nil {
|
||||||
close(cancelChan)
|
close(cancelChan)
|
||||||
return sig, htlcSigs, nil, jobResp.Err
|
return nil, jobResp.Err
|
||||||
}
|
}
|
||||||
|
|
||||||
htlcSigs = append(htlcSigs, jobResp.Sig)
|
htlcSigs = append(htlcSigs, jobResp.Sig)
|
||||||
|
@ -3871,11 +3894,11 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
// can retransmit it if necessary.
|
// can retransmit it if necessary.
|
||||||
commitDiff, err := lc.createCommitDiff(newCommitView, sig, htlcSigs)
|
commitDiff, err := lc.createCommitDiff(newCommitView, sig, htlcSigs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
err = lc.channelState.AppendRemoteCommitChain(commitDiff)
|
err = lc.channelState.AppendRemoteCommitChain(commitDiff)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return sig, htlcSigs, nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(roasbeef): check that one eclair bug
|
// TODO(roasbeef): check that one eclair bug
|
||||||
|
@ -3886,7 +3909,13 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig,
|
||||||
// latest commitment update.
|
// latest commitment update.
|
||||||
lc.remoteCommitChain.addCommitment(newCommitView)
|
lc.remoteCommitChain.addCommitment(newCommitView)
|
||||||
|
|
||||||
return sig, htlcSigs, commitDiff.Commitment.Htlcs, nil
|
return &NewCommitState{
|
||||||
|
CommitSigs: &CommitSigs{
|
||||||
|
CommitSig: sig,
|
||||||
|
HtlcSigs: htlcSigs,
|
||||||
|
},
|
||||||
|
PendingHTLCs: commitDiff.Commitment.Htlcs,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ProcessChanSyncMsg processes a ChannelReestablish message sent by the remote
|
// ProcessChanSyncMsg processes a ChannelReestablish message sent by the remote
|
||||||
|
@ -4044,19 +4073,21 @@ func (lc *LightningChannel) ProcessChanSyncMsg(
|
||||||
// revocation, but also initiate a state transition to re-sync
|
// revocation, but also initiate a state transition to re-sync
|
||||||
// them.
|
// them.
|
||||||
if lc.OweCommitment() {
|
if lc.OweCommitment() {
|
||||||
commitSig, htlcSigs, _, err := lc.SignNextCommitment()
|
newCommit, err := lc.SignNextCommitment()
|
||||||
switch {
|
switch {
|
||||||
|
|
||||||
// If we signed this state, then we'll accumulate
|
// If we signed this state, then we'll accumulate
|
||||||
// another update to send over.
|
// another update to send over.
|
||||||
case err == nil:
|
case err == nil:
|
||||||
updates = append(updates, &lnwire.CommitSig{
|
commitSig := &lnwire.CommitSig{
|
||||||
ChanID: lnwire.NewChanIDFromOutPoint(
|
ChanID: lnwire.NewChanIDFromOutPoint(
|
||||||
&lc.channelState.FundingOutpoint,
|
&lc.channelState.FundingOutpoint,
|
||||||
),
|
),
|
||||||
CommitSig: commitSig,
|
CommitSig: newCommit.CommitSig,
|
||||||
HtlcSigs: htlcSigs,
|
HtlcSigs: newCommit.HtlcSigs,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
updates = append(updates, commitSig)
|
||||||
|
|
||||||
// If we get a failure due to not knowing their next
|
// If we get a failure due to not knowing their next
|
||||||
// point, then this is fine as they'll either send
|
// point, then this is fine as they'll either send
|
||||||
|
@ -4535,8 +4566,7 @@ var _ error = (*InvalidCommitSigError)(nil)
|
||||||
// to our local commitment chain. Once we send a revocation for our prior
|
// to our local commitment chain. Once we send a revocation for our prior
|
||||||
// state, then this newly added commitment becomes our current accepted channel
|
// state, then this newly added commitment becomes our current accepted channel
|
||||||
// state.
|
// state.
|
||||||
func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
|
func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error {
|
||||||
htlcSigs []lnwire.Sig) error {
|
|
||||||
|
|
||||||
lc.Lock()
|
lc.Lock()
|
||||||
defer lc.Unlock()
|
defer lc.Unlock()
|
||||||
|
@ -4631,7 +4661,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
|
||||||
leaseExpiry = lc.channelState.ThawHeight
|
leaseExpiry = lc.channelState.ThawHeight
|
||||||
}
|
}
|
||||||
verifyJobs, err := genHtlcSigValidationJobs(
|
verifyJobs, err := genHtlcSigValidationJobs(
|
||||||
localCommitmentView, keyRing, htlcSigs,
|
localCommitmentView, keyRing, commitSigs.HtlcSigs,
|
||||||
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
lc.channelState.ChanType, lc.channelState.IsInitiator,
|
||||||
leaseExpiry, &lc.channelState.LocalChanCfg,
|
leaseExpiry, &lc.channelState.LocalChanCfg,
|
||||||
&lc.channelState.RemoteChanCfg,
|
&lc.channelState.RemoteChanCfg,
|
||||||
|
@ -4648,7 +4678,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
|
||||||
// signature.
|
// signature.
|
||||||
verifyKey := lc.channelState.RemoteChanCfg.MultiSigKey.PubKey
|
verifyKey := lc.channelState.RemoteChanCfg.MultiSigKey.PubKey
|
||||||
|
|
||||||
cSig, err := commitSig.ToSignature()
|
cSig, err := commitSigs.CommitSig.ToSignature()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -4663,7 +4693,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
|
||||||
localCommitTx.Serialize(&txBytes)
|
localCommitTx.Serialize(&txBytes)
|
||||||
return &InvalidCommitSigError{
|
return &InvalidCommitSigError{
|
||||||
commitHeight: nextHeight,
|
commitHeight: nextHeight,
|
||||||
commitSig: commitSig.ToSignatureBytes(),
|
commitSig: commitSigs.CommitSig.ToSignatureBytes(),
|
||||||
sigHash: sigHash,
|
sigHash: sigHash,
|
||||||
commitTx: txBytes.Bytes(),
|
commitTx: txBytes.Bytes(),
|
||||||
}
|
}
|
||||||
|
@ -4703,7 +4733,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig,
|
||||||
|
|
||||||
// The signature checks out, so we can now add the new commitment to
|
// The signature checks out, so we can now add the new commitment to
|
||||||
// our local commitment chain.
|
// our local commitment chain.
|
||||||
localCommitmentView.sig = commitSig.ToSignatureBytes()
|
localCommitmentView.sig = commitSigs.CommitSig.ToSignatureBytes()
|
||||||
lc.localCommitChain.addCommitment(localCommitmentView)
|
lc.localCommitChain.addCommitment(localCommitmentView)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -5721,7 +5751,7 @@ func (lc *LightningChannel) RemoteUpfrontShutdownScript() lnwire.DeliveryAddress
|
||||||
// AbsoluteThawHeight determines a frozen channel's absolute thaw height. If
|
// AbsoluteThawHeight determines a frozen channel's absolute thaw height. If
|
||||||
// the channel is not frozen, then 0 is returned.
|
// the channel is not frozen, then 0 is returned.
|
||||||
//
|
//
|
||||||
// An error is returned if the channel is penidng, or is an unconfirmed zero
|
// An error is returned if the channel is pending, or is an unconfirmed zero
|
||||||
// conf channel.
|
// conf channel.
|
||||||
func (lc *LightningChannel) AbsoluteThawHeight() (uint32, error) {
|
func (lc *LightningChannel) AbsoluteThawHeight() (uint32, error) {
|
||||||
return lc.channelState.AbsoluteThawHeight()
|
return lc.channelState.AbsoluteThawHeight()
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -495,11 +495,12 @@ func calcStaticFee(chanType channeldb.ChannelType, numHTLCs int) btcutil.Amount
|
||||||
// pending updates. This method is useful when testing interactions between two
|
// pending updates. This method is useful when testing interactions between two
|
||||||
// live state machines.
|
// live state machines.
|
||||||
func ForceStateTransition(chanA, chanB *LightningChannel) error {
|
func ForceStateTransition(chanA, chanB *LightningChannel) error {
|
||||||
aliceSig, aliceHtlcSigs, _, err := chanA.SignNextCommitment()
|
aliceNewCommit, err := chanA.SignNextCommitment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = chanB.ReceiveNewCommitment(aliceSig, aliceHtlcSigs); err != nil {
|
err = chanB.ReceiveNewCommitment(aliceNewCommit.CommitSigs)
|
||||||
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -507,7 +508,7 @@ func ForceStateTransition(chanA, chanB *LightningChannel) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
bobSig, bobHtlcSigs, _, err := chanB.SignNextCommitment()
|
bobNewCommit, err := chanB.SignNextCommitment()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -515,7 +516,7 @@ func ForceStateTransition(chanA, chanB *LightningChannel) error {
|
||||||
if _, _, _, _, err := chanA.ReceiveRevocation(bobRevocation); err != nil {
|
if _, _, _, _, err := chanA.ReceiveRevocation(bobRevocation); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := chanA.ReceiveNewCommitment(bobSig, bobHtlcSigs); err != nil {
|
if err := chanA.ReceiveNewCommitment(bobNewCommit.CommitSigs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -356,10 +356,10 @@ func testVectors(t *testing.T, chanType channeldb.ChannelType, test testCase) {
|
||||||
|
|
||||||
// Execute commit dance to arrive at the point where the local node has
|
// Execute commit dance to arrive at the point where the local node has
|
||||||
// received the test commitment and the remote signature.
|
// received the test commitment and the remote signature.
|
||||||
localSig, localHtlcSigs, _, err := localChannel.SignNextCommitment()
|
localNewCommit, err := localChannel.SignNextCommitment()
|
||||||
require.NoError(t, err, "local unable to sign commitment")
|
require.NoError(t, err, "local unable to sign commitment")
|
||||||
|
|
||||||
err = remoteChannel.ReceiveNewCommitment(localSig, localHtlcSigs)
|
err = remoteChannel.ReceiveNewCommitment(localNewCommit.CommitSigs)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
revMsg, _, _, err := remoteChannel.RevokeCurrentCommitment()
|
revMsg, _, _, err := remoteChannel.RevokeCurrentCommitment()
|
||||||
|
@ -368,18 +368,22 @@ func testVectors(t *testing.T, chanType channeldb.ChannelType, test testCase) {
|
||||||
_, _, _, _, err = localChannel.ReceiveRevocation(revMsg)
|
_, _, _, _, err = localChannel.ReceiveRevocation(revMsg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
remoteSig, remoteHtlcSigs, _, err := remoteChannel.SignNextCommitment()
|
remoteNewCommit, err := remoteChannel.SignNextCommitment()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
require.Equal(t, test.RemoteSigHex,
|
require.Equal(
|
||||||
hex.EncodeToString(remoteSig.ToSignatureBytes()))
|
t, test.RemoteSigHex,
|
||||||
|
hex.EncodeToString(remoteNewCommit.CommitSig.ToSignatureBytes()),
|
||||||
|
)
|
||||||
|
|
||||||
for i, sig := range remoteHtlcSigs {
|
for i, sig := range remoteNewCommit.HtlcSigs {
|
||||||
require.Equal(t, test.HtlcDescs[i].RemoteSigHex,
|
require.Equal(
|
||||||
hex.EncodeToString(sig.ToSignatureBytes()))
|
t, test.HtlcDescs[i].RemoteSigHex,
|
||||||
|
hex.EncodeToString(sig.ToSignatureBytes()),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = localChannel.ReceiveNewCommitment(remoteSig, remoteHtlcSigs)
|
err = localChannel.ReceiveNewCommitment(remoteNewCommit.CommitSigs)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
_, _, _, err = localChannel.RevokeCurrentCommitment()
|
_, _, _, err = localChannel.RevokeCurrentCommitment()
|
||||||
|
|
Loading…
Add table
Reference in a new issue