From 11c62e395127c0caeba172b6d0220e83c0514d14 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 19 Jan 2023 18:27:07 -0800 Subject: [PATCH] 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. --- contractcourt/breacharbiter_test.go | 46 +-- contractcourt/chain_watcher_test.go | 2 +- htlcswitch/link.go | 15 +- htlcswitch/link_isolated_test.go | 13 +- htlcswitch/link_test.go | 50 ++-- lnwallet/channel.go | 78 +++-- lnwallet/channel_test.go | 429 +++++++++++++++------------- lnwallet/test_utils.go | 9 +- lnwallet/transactions_test.go | 22 +- 9 files changed, 342 insertions(+), 322 deletions(-) diff --git a/contractcourt/breacharbiter_test.go b/contractcourt/breacharbiter_test.go index 6d17135dc..becc4883b 100644 --- a/contractcourt/breacharbiter_test.go +++ b/contractcourt/breacharbiter_test.go @@ -978,7 +978,7 @@ func initBreachedState(t *testing.T) (*BreachArbiter, if _, err := bob.ReceiveHTLC(htlc); err != nil { 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) } @@ -996,7 +996,7 @@ func initBreachedState(t *testing.T) (*BreachArbiter, if _, err := bob.ReceiveHTLC(htlc2); err != nil { 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) } @@ -2436,45 +2436,3 @@ func createHTLC(data int, amount lnwire.MilliSatoshi) (*lnwire.UpdateAddHTLC, [3 Expiry: uint32(5), }, 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 -} diff --git a/contractcourt/chain_watcher_test.go b/contractcourt/chain_watcher_test.go index 74559dbcf..489a60518 100644 --- a/contractcourt/chain_watcher_test.go +++ b/contractcourt/chain_watcher_test.go @@ -145,7 +145,7 @@ func TestChainWatcherRemoteUnilateralClosePendingCommit(t *testing.T) { // With the HTLC added, we'll now manually initiate a state transition // from Alice to Bob. - _, _, _, err = aliceChannel.SignNextCommitment() + _, err = aliceChannel.SignNextCommitment() if err != nil { t.Fatal(err) } diff --git a/htlcswitch/link.go b/htlcswitch/link.go index d025cdfff..f2f0f789e 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1899,7 +1899,10 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { // We just received a new updates to our local commitment // chain, validate this new commitment, closing the link if // 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 we were unable to reconstruct their proposed // commitment, then we'll examine the type of error. If @@ -2215,7 +2218,7 @@ func (l *channelLink) updateCommitTx() error { return nil } - theirCommitSig, htlcSigs, pendingHTLCs, err := l.channel.SignNextCommitment() + newCommit, err := l.channel.SignNextCommitment() if err == lnwallet.ErrNoWindow { l.cfg.PendingCommitTicker.Resume() l.log.Trace("PendingCommitTicker resumed") @@ -2247,7 +2250,7 @@ func (l *channelLink) updateCommitTx() error { // pending). newUpdate := &contractcourt.ContractUpdate{ HtlcKey: contractcourt.RemotePendingHtlcSet, - Htlcs: pendingHTLCs, + Htlcs: newCommit.PendingHTLCs, } err = l.cfg.NotifyContractUpdate(newUpdate) if err != nil { @@ -2262,9 +2265,9 @@ func (l *channelLink) updateCommitTx() error { } commitSig := &lnwire.CommitSig{ - ChanID: l.ChanID(), - CommitSig: theirCommitSig, - HtlcSigs: htlcSigs, + ChanID: l.ChanID(), + CommitSig: newCommit.CommitSig, + HtlcSigs: newCommit.HtlcSigs, } l.cfg.Peer.SendMessage(false, commitSig) diff --git a/htlcswitch/link_isolated_test.go b/htlcswitch/link_isolated_test.go index 7204a5873..6b616d2d8 100644 --- a/htlcswitch/link_isolated_test.go +++ b/htlcswitch/link_isolated_test.go @@ -93,14 +93,14 @@ func (l *linkTestContext) receiveHtlcAliceToBob() { func (l *linkTestContext) sendCommitSigBobToAlice(expHtlcs int) { l.t.Helper() - sig, htlcSigs, _, err := l.bobChannel.SignNextCommitment() + sigs, err := l.bobChannel.SignNextCommitment() if err != nil { l.t.Fatalf("error signing commitment: %v", err) } commitSig := &lnwire.CommitSig{ - CommitSig: sig, - HtlcSigs: htlcSigs, + CommitSig: sigs.CommitSig, + HtlcSigs: sigs.HtlcSigs, } if len(commitSig.HtlcSigs) != expHtlcs { @@ -141,9 +141,10 @@ func (l *linkTestContext) receiveCommitSigAliceToBob(expHtlcs int) { comSig := l.receiveCommitSigAlice(expHtlcs) - err := l.bobChannel.ReceiveNewCommitment( - comSig.CommitSig, comSig.HtlcSigs, - ) + err := l.bobChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{ + CommitSig: comSig.CommitSig, + HtlcSigs: comSig.HtlcSigs, + }) if err != nil { l.t.Fatalf("bob failed receiving commitment: %v", err) } diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 3a6510f16..4b792704b 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -2062,8 +2062,10 @@ func handleStateUpdate(link *channelLink, // Let the remote channel receive the commit sig, and // respond with a revocation + commitsig. - err := remoteChannel.ReceiveNewCommitment( - commitSig.CommitSig, commitSig.HtlcSigs) + err := remoteChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{ + CommitSig: commitSig.CommitSig, + HtlcSigs: commitSig.HtlcSigs, + }) if err != nil { return err } @@ -2074,13 +2076,13 @@ func handleStateUpdate(link *channelLink, } link.HandleChannelUpdate(remoteRev) - remoteSig, remoteHtlcSigs, _, err := remoteChannel.SignNextCommitment() + remoteSigs, err := remoteChannel.SignNextCommitment() if err != nil { return err } commitSig = &lnwire.CommitSig{ - CommitSig: remoteSig, - HtlcSigs: remoteHtlcSigs, + CommitSig: remoteSigs.CommitSig, + HtlcSigs: remoteSigs.HtlcSigs, } 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 // signing and sending CommitSig to the link. - remoteSig, remoteHtlcSigs, _, err := remoteChannel.SignNextCommitment() + remoteSigs, err := remoteChannel.SignNextCommitment() if err != nil { return err } commitSig := &lnwire.CommitSig{ - CommitSig: remoteSig, - HtlcSigs: remoteHtlcSigs, + CommitSig: remoteSigs.CommitSig, + HtlcSigs: remoteSigs.HtlcSigs, } link.HandleChannelUpdate(commitSig) @@ -2165,8 +2167,10 @@ func updateState(batchTick chan time.Time, link *channelLink, return fmt.Errorf("expected CommitSig, got %T", msg) } - err = remoteChannel.ReceiveNewCommitment( - commitSig.CommitSig, commitSig.HtlcSigs) + err = remoteChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{ + CommitSig: commitSig.CommitSig, + HtlcSigs: commitSig.HtlcSigs, + }) if err != nil { return err } @@ -3239,7 +3243,10 @@ func TestChannelLinkTrimCircuitsRemoteCommit(t *testing.T) { 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 { t.Fatalf("unable to receive new commitment: %v", err) } @@ -4839,9 +4846,10 @@ func TestChannelLinkNoEmptySig(t *testing.T) { ctx.sendCommitSigBobToAlice(1) // Now send Bob the signature from Alice covering both htlcs. - err = bobChannel.ReceiveNewCommitment( - commitSigAlice.CommitSig, commitSigAlice.HtlcSigs, - ) + err = bobChannel.ReceiveNewCommitment(&lnwallet.CommitSigs{ + CommitSig: commitSigAlice.CommitSig, + HtlcSigs: commitSigAlice.HtlcSigs, + }) require.NoError(t, err, "bob failed receiving commitment") // Both Alice and Bob revoke their previous commitment txes. @@ -5443,8 +5451,7 @@ func TestChannelLinkFail(t *testing.T) { // Sign a commitment that will include // signature for the HTLC just sent. - sig, htlcSigs, _, err := - remoteChannel.SignNextCommitment() + sigs, err := remoteChannel.SignNextCommitment() if err != nil { t.Fatalf("error signing commitment: %v", err) @@ -5453,8 +5460,8 @@ func TestChannelLinkFail(t *testing.T) { // Remove the HTLC sig, such that the commit // sig will be invalid. commitSig := &lnwire.CommitSig{ - CommitSig: sig, - HtlcSigs: htlcSigs[1:], + CommitSig: sigs.CommitSig, + HtlcSigs: sigs.HtlcSigs[1:], } c.HandleChannelUpdate(commitSig) @@ -5485,8 +5492,7 @@ func TestChannelLinkFail(t *testing.T) { // Sign a commitment that will include // signature for the HTLC just sent. - sig, htlcSigs, _, err := - remoteChannel.SignNextCommitment() + sigs, err := remoteChannel.SignNextCommitment() if err != nil { t.Fatalf("error signing commitment: %v", err) @@ -5494,7 +5500,7 @@ func TestChannelLinkFail(t *testing.T) { // Flip a bit on the signature, rendering it // invalid. - sigCopy := sig.Copy() + sigCopy := sigs.CommitSig.Copy() copyBytes := sigCopy.RawBytes() copyBytes[19] ^= 1 modifiedSig, err := lnwire.NewSigFromWireECDSA( @@ -5503,7 +5509,7 @@ func TestChannelLinkFail(t *testing.T) { require.NoError(t, err) commitSig := &lnwire.CommitSig{ CommitSig: modifiedSig, - HtlcSigs: htlcSigs, + HtlcSigs: sigs.HtlcSigs, } c.HandleChannelUpdate(commitSig) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 5230991b8..26b244099 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -3716,6 +3716,30 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, 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 // unsettled HTLCs, any new HTLCs, and any modifications to prior HTLCs // 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 // HTLC's on the commitment transaction. Finally, the new set of pending HTLCs // for the remote party's commitment are also returned. -func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, - []channeldb.HTLC, error) { +func (lc *LightningChannel) SignNextCommitment() (*NewCommitState, error) { lc.Lock() defer lc.Unlock() @@ -3755,7 +3778,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, if unacked || commitPoint == nil { lc.log.Tracef("waiting for remote ack=%v, 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. @@ -3770,7 +3793,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, remoteACKedIndex, lc.localUpdateLog.logIndex, true, nil, nil, ) if err != nil { - return sig, htlcSigs, nil, err + return nil, err } // 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, ) if err != nil { - return sig, htlcSigs, nil, err + return nil, err } 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, ) if err != nil { - return sig, htlcSigs, nil, err + return nil, err } 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) if err != nil { close(cancelChan) - return sig, htlcSigs, nil, err + return nil, err } sig, err = lnwire.NewSigFromSignature(rawSig) if err != nil { close(cancelChan) - return sig, htlcSigs, nil, err + return nil, err } // 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. if jobResp.Err != nil { close(cancelChan) - return sig, htlcSigs, nil, jobResp.Err + return nil, jobResp.Err } htlcSigs = append(htlcSigs, jobResp.Sig) @@ -3871,11 +3894,11 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, // can retransmit it if necessary. commitDiff, err := lc.createCommitDiff(newCommitView, sig, htlcSigs) if err != nil { - return sig, htlcSigs, nil, err + return nil, err } err = lc.channelState.AppendRemoteCommitChain(commitDiff) if err != nil { - return sig, htlcSigs, nil, err + return nil, err } // TODO(roasbeef): check that one eclair bug @@ -3886,7 +3909,13 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, // latest commitment update. 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 @@ -4044,19 +4073,21 @@ func (lc *LightningChannel) ProcessChanSyncMsg( // revocation, but also initiate a state transition to re-sync // them. if lc.OweCommitment() { - commitSig, htlcSigs, _, err := lc.SignNextCommitment() + newCommit, err := lc.SignNextCommitment() switch { // If we signed this state, then we'll accumulate // another update to send over. case err == nil: - updates = append(updates, &lnwire.CommitSig{ + commitSig := &lnwire.CommitSig{ ChanID: lnwire.NewChanIDFromOutPoint( &lc.channelState.FundingOutpoint, ), - CommitSig: commitSig, - HtlcSigs: htlcSigs, - }) + CommitSig: newCommit.CommitSig, + HtlcSigs: newCommit.HtlcSigs, + } + + updates = append(updates, commitSig) // If we get a failure due to not knowing their next // 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 // state, then this newly added commitment becomes our current accepted channel // state. -func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, - htlcSigs []lnwire.Sig) error { +func (lc *LightningChannel) ReceiveNewCommitment(commitSigs *CommitSigs) error { lc.Lock() defer lc.Unlock() @@ -4631,7 +4661,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, leaseExpiry = lc.channelState.ThawHeight } verifyJobs, err := genHtlcSigValidationJobs( - localCommitmentView, keyRing, htlcSigs, + localCommitmentView, keyRing, commitSigs.HtlcSigs, lc.channelState.ChanType, lc.channelState.IsInitiator, leaseExpiry, &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, @@ -4648,7 +4678,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, // signature. verifyKey := lc.channelState.RemoteChanCfg.MultiSigKey.PubKey - cSig, err := commitSig.ToSignature() + cSig, err := commitSigs.CommitSig.ToSignature() if err != nil { return err } @@ -4663,7 +4693,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, localCommitTx.Serialize(&txBytes) return &InvalidCommitSigError{ commitHeight: nextHeight, - commitSig: commitSig.ToSignatureBytes(), + commitSig: commitSigs.CommitSig.ToSignatureBytes(), sigHash: sigHash, 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 // our local commitment chain. - localCommitmentView.sig = commitSig.ToSignatureBytes() + localCommitmentView.sig = commitSigs.CommitSig.ToSignatureBytes() lc.localCommitChain.addCommitment(localCommitmentView) return nil @@ -5721,7 +5751,7 @@ func (lc *LightningChannel) RemoteUpfrontShutdownScript() lnwire.DeliveryAddress // AbsoluteThawHeight determines a frozen channel's absolute thaw height. If // 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. func (lc *LightningChannel) AbsoluteThawHeight() (uint32, error) { return lc.channelState.AbsoluteThawHeight() diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 93bf4c747..d11257f45 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -100,13 +100,13 @@ func testAddSettleWorkflow(t *testing.T, tweakless, // we expect the messages to be ordered, Bob will receive the HTLC we // just sent before he receives this signature, so the signature will // cover the HTLC. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // Bob receives this signature message, and checks that this covers the // state he has in his remote log. This includes the HTLC just sent // from Alice. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's new commitment") // Bob revokes his prior commitment given to him by Alice, since he now @@ -118,7 +118,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless, // This signature will cover the HTLC, since Bob will first send the // revocation just created. The revocation also acks every received // HTLC up to the point where Alice sent here signature. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign alice's commitment") // Alice then processes this revocation, sending her own revocation for @@ -138,7 +138,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless, // Alice then processes bob's signature, and since she just received // the revocation, she expect this signature to cover everything up to // the point where she sent her signature, including the HTLC. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to process bob's new commitment") // Alice then generates a revocation for bob. @@ -222,14 +222,14 @@ func testAddSettleWorkflow(t *testing.T, tweakless, t.Fatalf("alice unable to accept settle of outbound htlc: %v", err) } - bobSig2, bobHtlcSigs2, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign settle commitment") - err = aliceChannel.ReceiveNewCommitment(bobSig2, bobHtlcSigs2) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to process bob's new commitment") aliceRevocation2, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to generate revocation") - aliceSig2, aliceHtlcSigs2, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign new commitment") fwdPkg, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation2) @@ -243,7 +243,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless, "should forward none", len(fwdPkg.SettleFails)) } - err = bobChannel.ReceiveNewCommitment(aliceSig2, aliceHtlcSigs2) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's new commitment") bobRevocation2, _, finalHtlcs, err := bobChannel. @@ -417,10 +417,10 @@ func TestChannelZeroAddLocalHeight(t *testing.T) { // Bob should send a commitment signature to Alice. // <----sig------ - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) // Alice should reply with a revocation. @@ -451,12 +451,12 @@ func TestChannelZeroAddLocalHeight(t *testing.T) { // Bob should now send a commitment signature to Alice. // <----sig----- - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err) // Alice should accept the commitment. Previously she would // force close here. - err = newAliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = newAliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) } @@ -596,10 +596,10 @@ func testCommitHTLCSigTieBreak(t *testing.T, restart bool) { // tie-breaking for commitment sorting won't affect the commitment // signed by Alice because received HTLC scripts commit to the CLTV // directly, so the outputs will have different scriptPubkeys. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign alice's commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive alice's commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() @@ -611,19 +611,20 @@ func testCommitHTLCSigTieBreak(t *testing.T, restart bool) { // the offered HTLC scripts he adds for Alice will need to have the // tie-breaking applied because the CLTV is not committed, but instead // implicit via the construction of the second-level transactions. - bobSig, bobHtlcSigs, bobHtlcs, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign bob's commitment") - if len(bobHtlcs) != numHtlcs { - t.Fatalf("expected %d htlcs, got: %v", numHtlcs, len(bobHtlcs)) + if len(bobNewCommit.PendingHTLCs) != numHtlcs { + t.Fatalf("expected %d htlcs, got: %v", numHtlcs, + len(bobNewCommit.PendingHTLCs)) } // Ensure that our HTLCs appear in the reverse order from which they // were added by inspecting each's outpoint index. We expect the output // indexes to be in descending order, i.e. the first HTLC added had the // highest CLTV and should end up last. - lastIndex := bobHtlcs[0].OutputIndex - for i, htlc := range bobHtlcs[1:] { + lastIndex := bobNewCommit.PendingHTLCs[0].OutputIndex + for i, htlc := range bobNewCommit.PendingHTLCs[1:] { if htlc.OutputIndex >= lastIndex { t.Fatalf("htlc %d output index %d is not descending", i, htlc.OutputIndex) @@ -657,7 +658,7 @@ func testCommitHTLCSigTieBreak(t *testing.T, restart bool) { // Finally, have Alice validate the signatures to ensure that she is // expecting the signatures in the proper order. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive bob's commitment") } @@ -1433,17 +1434,19 @@ func TestHTLCSigNumber(t *testing.T) { // =================================================================== aliceChannel, bobChannel := createChanWithHTLC(aboveDust, aboveDust) - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "Error signing next commitment") - if len(aliceHtlcSigs) != 2 { + if len(aliceNewCommit.HtlcSigs) != 2 { t.Fatalf("expected 2 htlc sig, instead got %v", - len(aliceHtlcSigs)) + len(aliceNewCommit.HtlcSigs)) } // Now discard one signature from the htlcSig slice. Bob should reject // the commitment because of this. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs[1:]) + aliceNewCommitCopy := *aliceNewCommit + aliceNewCommitCopy.HtlcSigs = aliceNewCommitCopy.HtlcSigs[1:] + err = bobChannel.ReceiveNewCommitment(aliceNewCommitCopy.CommitSigs) if err == nil { t.Fatalf("Expected Bob to reject signatures") } @@ -1454,17 +1457,19 @@ func TestHTLCSigNumber(t *testing.T) { // =================================================================== aliceChannel, bobChannel = createChanWithHTLC(aboveDust) - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "Error signing next commitment") - if len(aliceHtlcSigs) != 1 { + if len(aliceNewCommit.HtlcSigs) != 1 { t.Fatalf("expected 1 htlc sig, instead got %v", - len(aliceHtlcSigs)) + len(aliceNewCommit.HtlcSigs)) } // Now just give Bob an empty htlcSig slice. He should reject the // commitment because of this. - err = bobChannel.ReceiveNewCommitment(aliceSig, []lnwire.Sig{}) + aliceCommitCopy := *aliceNewCommit.CommitSigs + aliceCommitCopy.HtlcSigs = []lnwire.Sig{} + err = bobChannel.ReceiveNewCommitment(&aliceCommitCopy) if err == nil { t.Fatalf("Expected Bob to reject signatures") } @@ -1474,17 +1479,17 @@ func TestHTLCSigNumber(t *testing.T) { // ============================================================== aliceChannel, bobChannel = createChanWithHTLC(belowDust) - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "Error signing next commitment") // Since the HTLC is below Bob's dust limit, Alice won't need to send // any signatures for this HTLC. - if len(aliceHtlcSigs) != 0 { + if len(aliceNewCommit.HtlcSigs) != 0 { t.Fatalf("expected no htlc sigs, instead got %v", - len(aliceHtlcSigs)) + len(aliceNewCommit.HtlcSigs)) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "Bob failed receiving commitment") // ================================================================ @@ -1492,17 +1497,17 @@ func TestHTLCSigNumber(t *testing.T) { // ================================================================ aliceChannel, bobChannel = createChanWithHTLC(aboveDust) - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "Error signing next commitment") // Since the HTLC is above Bob's dust limit, Alice should send a // signature for this HTLC. - if len(aliceHtlcSigs) != 1 { + if len(aliceNewCommit.HtlcSigs) != 1 { t.Fatalf("expected 1 htlc sig, instead got %v", - len(aliceHtlcSigs)) + len(aliceNewCommit.HtlcSigs)) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "Bob failed receiving commitment") // ==================================================================== @@ -1513,20 +1518,22 @@ func TestHTLCSigNumber(t *testing.T) { // Alice should produce only one signature, since one HTLC is below // dust. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "Error signing next commitment") - if len(aliceHtlcSigs) != 1 { + if len(aliceNewCommit.HtlcSigs) != 1 { t.Fatalf("expected 1 htlc sig, instead got %v", - len(aliceHtlcSigs)) + len(aliceNewCommit.HtlcSigs)) } // Add an extra signature. - aliceHtlcSigs = append(aliceHtlcSigs, aliceHtlcSigs[0]) + aliceNewCommit.HtlcSigs = append( + aliceNewCommit.HtlcSigs, aliceNewCommit.HtlcSigs[0], + ) // Bob should reject these signatures since they don't match the number // of HTLCs above dust. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err == nil { t.Fatalf("Expected Bob to reject signatures") } @@ -2268,13 +2275,13 @@ func TestUpdateFeeFail(t *testing.T) { // Alice sends signature for commitment that does not cover any fee // update. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // Bob verifies this commit, meaning that he checks that it is // consistent everything he has received. This should fail, since he got // the fee update, but Alice never sent it. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err == nil { t.Fatalf("expected bob to fail receiving alice's signature") } @@ -2316,15 +2323,15 @@ func TestUpdateFeeConcurrentSig(t *testing.T) { } // Alice signs a commitment, and sends this to bob. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommits, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // At the same time, Bob signs a commitment. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommits, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign alice's commitment") // ...that Alice receives. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommits.CommitSigs) require.NoError(t, err, "alice unable to process bob's new commitment") // Now let Bob receive the fee update + commitment that Alice sent. @@ -2335,7 +2342,7 @@ func TestUpdateFeeConcurrentSig(t *testing.T) { // Bob receives this signature message, and verifies that it is // consistent with the state he had for Alice, including the received // HTLC and fee update. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommits.CommitSigs) require.NoError(t, err, "bob unable to process alice's new commitment") if chainfee.SatPerKWeight(bobChannel.channelState.LocalCommitment.FeePerKw) == fee { @@ -2392,13 +2399,13 @@ func TestUpdateFeeSenderCommits(t *testing.T) { // Alice signs a commitment, which will cover everything sent to Bob // (the HTLC and the fee update), and everything acked by Bob (nothing // so far). - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommits, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // Bob receives this signature message, and verifies that it is // consistent with the state he had for Alice, including the received // HTLC and fee update. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommits.CommitSigs) require.NoError(t, err, "bob unable to process alice's new commitment") if chainfee.SatPerKWeight( @@ -2422,7 +2429,7 @@ func TestUpdateFeeSenderCommits(t *testing.T) { // Bob commits to all updates he has received from Alice. This includes // the HTLC he received, and the fee update. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign alice's commitment") // Alice receives the revocation of the old one, and can now assume @@ -2433,7 +2440,7 @@ func TestUpdateFeeSenderCommits(t *testing.T) { // Alice receives new signature from Bob, and assumes this covers the // changes. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to process bob's new commitment") if chainfee.SatPerKWeight( @@ -2501,12 +2508,12 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { // Bob commits to every change he has sent since last time (none). He // does not commit to the received HTLC and fee update, since Alice // cannot know if he has received them. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // Alice receives this signature message, and verifies that it is // consistent with the remote state, not including any of the updates. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's new commitment") // Alice can revoke the prior commitment she had, this will ack @@ -2522,12 +2529,12 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { // Alice will sign next commitment. Since she sent the revocation, she // also ack'ed everything received, but in this case this is nothing. // Since she sent the two updates, this signature will cover those two. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign alice's commitment") // Bob gets the signature for the new commitment from Alice. He assumes // this covers everything received from alice, including the two updates. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "alice unable to process bob's new commitment") if chainfee.SatPerKWeight( @@ -2552,7 +2559,7 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { // Bob will send a new signature, which will cover what he just acked: // the HTLC and fee update. - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // Alice receives revocation from Bob, and can now be sure that Bob @@ -2562,7 +2569,7 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { // Alice will receive the signature from Bob, which will cover what was // just acked by his revocation. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to process bob's new commitment") if chainfee.SatPerKWeight( @@ -2642,7 +2649,7 @@ func TestUpdateFeeMultipleUpdates(t *testing.T) { // Alice signs a commitment, which will cover everything sent to Bob // (the HTLC and the fee update), and everything acked by Bob (nothing // so far). - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") bobChannel.ReceiveUpdateFee(fee1) @@ -2652,7 +2659,7 @@ func TestUpdateFeeMultipleUpdates(t *testing.T) { // Bob receives this signature message, and verifies that it is // consistent with the state he had for Alice, including the received // HTLC and fee update. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's new commitment") if chainfee.SatPerKWeight( @@ -2688,7 +2695,7 @@ func TestUpdateFeeMultipleUpdates(t *testing.T) { // Bob commits to all updates he has received from Alice. This includes // the HTLC he received, and the fee update. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign alice's commitment") // Alice receives the revocation of the old one, and can now assume that @@ -2699,7 +2706,8 @@ func TestUpdateFeeMultipleUpdates(t *testing.T) { // Alice receives new signature from Bob, and assumes this covers the // changes. - if err := aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs); err != nil { + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) + if err != nil { t.Fatalf("alice unable to process bob's new commitment: %v", err) } @@ -2991,7 +2999,7 @@ func TestChanSyncOweCommitment(t *testing.T) { // Now we'll begin the core of the test itself. Alice will extend a new // commitment to Bob, but the connection drops before Bob can process // it. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // Bob doesn't get this message so upon reconnection, they need to @@ -3058,20 +3066,20 @@ func TestChanSyncOweCommitment(t *testing.T) { t.Fatalf("expected a CommitSig message, instead have %v", spew.Sdump(aliceMsgsToSend[4])) } - if commitSigMsg.CommitSig != aliceSig { + if commitSigMsg.CommitSig != aliceNewCommit.CommitSig { t.Fatalf("commit sig msgs don't match: expected %x got %x", - aliceSig, commitSigMsg.CommitSig) + aliceNewCommit.CommitSig, commitSigMsg.CommitSig) } - if len(commitSigMsg.HtlcSigs) != len(aliceHtlcSigs) { + if len(commitSigMsg.HtlcSigs) != len(aliceNewCommit.HtlcSigs) { t.Fatalf("wrong number of htlc sigs: expected %v, got %v", - len(aliceHtlcSigs), len(commitSigMsg.HtlcSigs)) + len(aliceNewCommit.HtlcSigs), + len(commitSigMsg.HtlcSigs)) } for i, htlcSig := range commitSigMsg.HtlcSigs { - if htlcSig != aliceHtlcSigs[i] { + if htlcSig != aliceNewCommit.HtlcSigs[i] { t.Fatalf("htlc sig msgs don't match: "+ "expected %x got %x", - aliceHtlcSigs[i], - htlcSig) + aliceNewCommit.HtlcSigs[i], htlcSig) } } } @@ -3101,15 +3109,15 @@ func TestChanSyncOweCommitment(t *testing.T) { // At this point, we should be able to resume the prior state update // without any issues, resulting in Alice settling the 3 htlc's, and // adding one of her own. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob commitment") - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") @@ -3249,12 +3257,13 @@ func TestChanSyncOweCommitmentPendingRemote(t *testing.T) { t.Fatalf("unable to settle htlc: %v", err) } - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() if err != nil { t.Fatalf("unable to sign commitment: %v", err) } - - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment( + aliceNewCommit.CommitSigs, + ) if err != nil { t.Fatalf("unable to receive commitment: %v", err) } @@ -3280,16 +3289,17 @@ func TestChanSyncOweCommitmentPendingRemote(t *testing.T) { require.NoError(t, err, "unable to restart bob") // Bob signs the commitment he owes. - bobCommit, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // This commitment is expected to contain no htlcs anymore. - if len(bobHtlcSigs) != 0 { - t.Fatalf("no htlcs expected, but got %v", len(bobHtlcSigs)) + if len(bobNewCommit.HtlcSigs) != 0 { + t.Fatalf("no htlcs expected, but got %v", + len(bobNewCommit.HtlcSigs)) } // Get Alice to revoke and trigger Bob to compact his logs. - err = aliceChannel.ReceiveNewCommitment(bobCommit, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -3354,19 +3364,19 @@ func TestChanSyncOweRevocation(t *testing.T) { // // Alice signs the next state, then Bob receives and sends his // revocation message. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob commitment") - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") // At this point, we'll simulate the connection breaking down by Bob's @@ -3504,16 +3514,16 @@ func TestChanSyncOweRevocationAndCommit(t *testing.T) { // Progressing the exchange: Alice will send her signature, Bob will // receive, send a revocation and also a signature for Alice's state. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommits, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommits.CommitSigs) require.NoError(t, err, "bob unable to process alice's commitment") // Bob generates the revoke and sig message, but the messages don't // reach Alice before the connection dies. bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob commitment") - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") // If we now attempt to resync, then Alice should conclude that she @@ -3555,19 +3565,22 @@ func TestChanSyncOweRevocationAndCommit(t *testing.T) { t.Fatalf("expected bob to re-send commit sig, instead sending: %v", spew.Sdump(bobMsgsToSend[1])) } - if bobReCommitSigMsg.CommitSig != bobSig { + if bobReCommitSigMsg.CommitSig != bobNewCommit.CommitSig { t.Fatalf("commit sig msgs don't match: expected %x got %x", - bobSig, bobReCommitSigMsg.CommitSig) + bobNewCommit.CommitSigs.CommitSig, + bobReCommitSigMsg.CommitSig) } - if len(bobReCommitSigMsg.HtlcSigs) != len(bobHtlcSigs) { + if len(bobReCommitSigMsg.HtlcSigs) != len(bobNewCommit.HtlcSigs) { t.Fatalf("wrong number of htlc sigs: expected %v, got %v", - len(bobHtlcSigs), len(bobReCommitSigMsg.HtlcSigs)) + len(bobNewCommit.HtlcSigs), + len(bobReCommitSigMsg.HtlcSigs)) } for i, htlcSig := range bobReCommitSigMsg.HtlcSigs { - if htlcSig != aliceHtlcSigs[i] { + if htlcSig != bobNewCommit.HtlcSigs[i] { t.Fatalf("htlc sig msgs don't match: "+ "expected %x got %x", - bobHtlcSigs[i], htlcSig) + htlcSig, + bobNewCommit.HtlcSigs[i]) } } } @@ -3586,8 +3599,8 @@ func TestChanSyncOweRevocationAndCommit(t *testing.T) { // messages, and send her final revocation. _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) - require.NoError(t, err, "alice unable to rev bob's commitment") + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) + require.NoError(t, err, "alice unable to recv bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) @@ -3649,10 +3662,10 @@ func TestChanSyncOweRevocationAndCommitForceTransition(t *testing.T) { require.NoError(t, err, "unable to recv bob's htlc") // Bob signs the new state update, and sends the signature to Alice. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") // Alice revokes her current state, but doesn't immediately send a @@ -3673,9 +3686,9 @@ func TestChanSyncOweRevocationAndCommitForceTransition(t *testing.T) { // Progressing the exchange: Alice will send her signature, with Bob // processing the new state locally. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommits, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommits.CommitSigs) require.NoError(t, err, "bob unable to process alice's commitment") // Bob then sends his revocation message, but before Alice can process @@ -3771,9 +3784,10 @@ func TestChanSyncOweRevocationAndCommitForceTransition(t *testing.T) { // message to Bob. _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") - err = aliceChannel.ReceiveNewCommitment( - bobSigMsg.CommitSig, bobSigMsg.HtlcSigs, - ) + err = aliceChannel.ReceiveNewCommitment(&CommitSigs{ + CommitSig: bobSigMsg.CommitSig, + HtlcSigs: bobSigMsg.HtlcSigs, + }) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err = aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") @@ -3858,11 +3872,11 @@ func TestChanSyncFailure(t *testing.T) { t.Fatalf("unable to recv bob's htlc: %v", err) } - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() if err != nil { t.Fatalf("unable to sign next commit: %v", err) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err != nil { t.Fatalf("unable to receive commit sig: %v", err) } @@ -4078,7 +4092,7 @@ func TestChannelRetransmissionFeeUpdate(t *testing.T) { // Now, Alice will send a new commitment to Bob, but we'll simulate a // connection failure, so Bob doesn't get her signature. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // Restart both channels to simulate a connection restart. @@ -4137,19 +4151,20 @@ func TestChannelRetransmissionFeeUpdate(t *testing.T) { t.Fatalf("expected a CommitSig message, instead have %v", spew.Sdump(aliceMsgsToSend[1])) } - if commitSigMsg.CommitSig != aliceSig { + if commitSigMsg.CommitSig != aliceNewCommit.CommitSig { t.Fatalf("commit sig msgs don't match: expected %x got %x", - aliceSig, commitSigMsg.CommitSig) + aliceNewCommit.CommitSig, commitSigMsg.CommitSig) } - if len(commitSigMsg.HtlcSigs) != len(aliceHtlcSigs) { + if len(commitSigMsg.HtlcSigs) != len(aliceNewCommit.HtlcSigs) { t.Fatalf("wrong number of htlc sigs: expected %v, got %v", - len(aliceHtlcSigs), len(commitSigMsg.HtlcSigs)) + len(aliceNewCommit.HtlcSigs), + len(commitSigMsg.HtlcSigs)) } for i, htlcSig := range commitSigMsg.HtlcSigs { - if htlcSig != aliceHtlcSigs[i] { + if htlcSig != aliceNewCommit.HtlcSigs[i] { t.Fatalf("htlc sig msgs don't match: "+ "expected %x got %x", - aliceHtlcSigs[i], htlcSig) + aliceNewCommit.HtlcSigs[i], htlcSig) } } @@ -4159,15 +4174,15 @@ func TestChannelRetransmissionFeeUpdate(t *testing.T) { t.Fatalf("unable to update fee for Bob's channel: %v", err) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "bob unable to process alice's commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob commitment") - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") @@ -4304,7 +4319,7 @@ func TestFeeUpdateOldDiskFormat(t *testing.T) { // Now, Alice will send a new commitment to Bob, but we'll simulate a // connection failure, so Bob doesn't get the signature. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommitSig, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // Before restarting Alice, to mimic the old format, we fetch the @@ -4357,15 +4372,15 @@ func TestFeeUpdateOldDiskFormat(t *testing.T) { // We send Alice's commitment signatures, and finish the state // transition. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommitSig.CommitSigs) require.NoError(t, err, "bob unable to process alice's commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob commitment") - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommitSigs, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommitSigs.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") @@ -4996,7 +5011,7 @@ func TestSignCommitmentFailNotLockedIn(t *testing.T) { // If we now try to initiate a state update, then it should fail as // Alice is unable to actually create a new state. - _, _, _, err = aliceChannel.SignNextCommitment() + _, err = aliceChannel.SignNextCommitment() if err != ErrNoWindow { t.Fatalf("expected ErrNoWindow, instead have: %v", err) } @@ -5034,11 +5049,11 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // We'll now manually initiate a state transition between Alice and // bob. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5063,12 +5078,12 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Now, have Bob initiate a transition to lock in the Adds sent by // Alice. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5108,11 +5123,11 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // We'll now initiate another state transition, but this time Bob will // lead. - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5143,11 +5158,11 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Now, begin another state transition led by Alice, and fail the second // HTLC part-way through the dance. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5207,11 +5222,11 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Have Alice initiate a state transition, which does not include the // HTLCs just re-added to the channel state. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5236,12 +5251,12 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { } // Now initiate a final update from Bob to lock in the final Fail. - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5268,11 +5283,11 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Finally, have Bob initiate a state transition that locks in the Fail // added after the restart. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() if err != nil { t.Fatal(err) } - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err != nil { t.Fatal(err) } @@ -5326,19 +5341,21 @@ func TestInvalidCommitSigError(t *testing.T) { } // Alice will now attempt to initiate a state transition. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign new commit") // Before the signature gets to Bob, we'll mutate it, such that the // signature is now actually invalid. - aliceSigCopy := aliceSig.Copy() + aliceSigCopy := aliceNewCommit.CommitSig.Copy() aliceSigCopyBytes := aliceSigCopy.RawBytes() - aliceSigCopyBytes[0] ^= 88 - aliceSig, err = lnwire.NewSigFromWireECDSA(aliceSigCopyBytes) + aliceSigCopyBytes[0] ^= 80 + aliceNewCommit.CommitSig, err = lnwire.NewSigFromWireECDSA( + aliceSigCopyBytes, + ) require.NoError(t, err) // Bob should reject this new state, and return the proper error. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err == nil { t.Fatalf("bob accepted invalid state but shouldn't have") } @@ -5530,7 +5547,7 @@ func TestChannelUnilateralClosePendingCommit(t *testing.T) { // With the HTLC added, we'll now manually initiate a state transition // from Alice to Bob. - _, _, _, err = aliceChannel.SignNextCommitment() + _, err = aliceChannel.SignNextCommitment() if err != nil { t.Fatal(err) } @@ -5770,9 +5787,9 @@ func TestMaxAcceptedHTLCs(t *testing.T) { } // Add a commitment to Bob's commitment chain. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign next commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to recv new commitment") // The next HTLC should fail with ErrMaxHTLCNumber. The index is incremented @@ -5875,18 +5892,18 @@ func TestMaxAsynchronousHtlcs(t *testing.T) { t.Fatalf("unable to receive fail htlc: %v", err) } - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign next commitment") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive new commitment") // Cover the HTLC referenced with id equal to numHTLCs-1 with a new // signature (step 3). - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign next commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive new commitment") // Both sides exchange revocations as in step 4 & 5. @@ -5913,10 +5930,10 @@ func TestMaxAsynchronousHtlcs(t *testing.T) { // Receiving the commitment should succeed as in step 7 since space was // made. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign next commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive new commitment") } @@ -6566,11 +6583,11 @@ func TestChannelRestoreUpdateLogs(t *testing.T) { } // Let Alice sign a new state, which will include the HTLC just sent. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // Bob receives this commitment signature, and revokes his old state. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") @@ -6596,7 +6613,7 @@ func TestChannelRestoreUpdateLogs(t *testing.T) { // and remote commit chains are updated in an async fashion. Since the // remote chain was updated with the latest state (since Bob sent the // revocation earlier) we can keep advancing the remote commit chain. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // After Alice has signed this commitment, her local commitment will @@ -6740,9 +6757,9 @@ func TestChannelRestoreUpdateLogsFailedHTLC(t *testing.T) { restoreAndAssert(t, aliceChannel, 1, 0, 0, 0) // Bob sends a signature. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") // When Alice receives Bob's new commitment, the logs will stay the @@ -6767,9 +6784,9 @@ func TestChannelRestoreUpdateLogsFailedHTLC(t *testing.T) { // Now send a signature from Alice. This will give Bob a new commitment // where the HTLC is removed. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") // When sending a new commitment, Alice will add a pending commit to @@ -6834,7 +6851,7 @@ func TestDuplicateFailRejection(t *testing.T) { // We'll now have Bob sign a new commitment to lock in the HTLC fail // for Alice. - _, _, _, err = bobChannel.SignNextCommitment() + _, err = bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commit") // We'll now force a restart for Bob and Alice, so we can test the @@ -6901,7 +6918,7 @@ func TestDuplicateSettleRejection(t *testing.T) { // We'll now have Bob sign a new commitment to lock in the HTLC fail // for Alice. - _, _, _, err = bobChannel.SignNextCommitment() + _, err = bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commit") // We'll now force a restart for Bob and Alice, so we can test the @@ -6985,7 +7002,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { } // Let Alice sign a new state, which will include the HTLC just sent. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // The HTLC should only be on the pending remote commitment, so the @@ -6995,7 +7012,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { ) // Bob receives this commitment signature, and revokes his old state. - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") @@ -7017,7 +7034,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { // Now let Bob send the commitment signature making the HTLC lock in on // Alice's commitment. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // At this stage Bob has a pending remote commitment. Make sure @@ -7025,7 +7042,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { // heights. bobChannel = restoreAndAssertCommitHeights(t, bobChannel, true, 0, 1, 1) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") @@ -7055,7 +7072,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { // Send a new signature from Alice to Bob, making Alice have a pending // remote commitment. - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // A restoration should keep the add heights iof the first HTLC, and @@ -7067,7 +7084,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { t, aliceChannel, false, 1, 0, 2, ) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") bobRevocation, _, _, err = bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") @@ -7094,7 +7111,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { // Sign a new state for Alice, making Bob have a pending remote // commitment. - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // The signing of a new commitment for Alice should have given the new @@ -7103,7 +7120,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { bobChannel = restoreAndAssertCommitHeights(t, bobChannel, true, 1, 2, 2) // Alice should receive the commitment and send over a revocation. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") aliceRevocation, _, _, err = aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") @@ -7131,7 +7148,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { require.NoError(t, err, "unable to recv htlc cancel") // Now Bob signs for the fail update. - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commitment") // Bob has a pending commitment for Alice, it shouldn't affect the add @@ -7140,7 +7157,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { _ = restoreAndAssertCommitHeights(t, bobChannel, true, 1, 2, 2) // Alice receives commitment, sends revocation. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") _, _, _, err = aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") @@ -7192,15 +7209,15 @@ func TestForceCloseBorkedState(t *testing.T) { // Do the commitment dance until Bob sends a revocation so Alice is // able to receive the revocation, and then also make a new state // herself. - aliceSigs, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commit") - err = bobChannel.ReceiveNewCommitment(aliceSigs, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") revokeMsg, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob commitment") - bobSigs, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "unable to sign commit") - err = aliceChannel.ReceiveNewCommitment(bobSigs, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "unable to receive commitment") // Now that we have a new Alice channel, we'll force close once to @@ -7232,7 +7249,7 @@ func TestForceCloseBorkedState(t *testing.T) { // We manually advance the commitment tail here since the above // ReceiveRevocation call will fail before it's actually advanced. aliceChannel.remoteCommitChain.advanceTail() - _, _, _, err = aliceChannel.SignNextCommitment() + _, err = aliceChannel.SignNextCommitment() if err != channeldb.ErrChanBorked { t.Fatalf("sign commitment should have failed: %v", err) } @@ -7501,11 +7518,11 @@ func TestChannelFeeRateFloor(t *testing.T) { } // Check that alice can still sign commitments. - sig, htlcSigs, _, err := alice.SignNextCommitment() + aliceNewCommit, err := alice.SignNextCommitment() require.NoError(t, err, "alice unable to sign commitment") // Check that bob can still receive commitments. - err = bob.ReceiveNewCommitment(sig, htlcSigs) + err = bob.ReceiveNewCommitment(aliceNewCommit.CommitSigs) if err != nil { t.Fatalf("bob unable to process alice's new commitment: %v", err) @@ -8808,9 +8825,9 @@ func TestChannelUnsignedAckedFailure(t *testing.T) { // Bob should send a commitment signature to Alice. // <----sig------ - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) // Alice should reply with a revocation. @@ -8823,7 +8840,7 @@ func TestChannelUnsignedAckedFailure(t *testing.T) { // Alice should sign the next commitment and go down before // sending it. // -----sig-----X - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err) newAliceChannel, err := NewLightningChannel( @@ -8834,7 +8851,7 @@ func TestChannelUnsignedAckedFailure(t *testing.T) { // Bob receives Alice's signature. // -----sig-----> - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) // Bob revokes his current commitment and sends a revocation @@ -8857,9 +8874,9 @@ func TestChannelUnsignedAckedFailure(t *testing.T) { // Bob sends the final signature to Alice and Alice should not // reject it, given that we properly restore the unsigned acked // updates and therefore our update log is structured correctly. - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err) - err = newAliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = newAliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) } @@ -8918,9 +8935,9 @@ func TestChannelLocalUnsignedUpdatesFailure(t *testing.T) { // Alice should send a commitment signature to Bob. // -----sig----> - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) // Bob should reply with a revocation and Alice should save the fail as @@ -8942,9 +8959,9 @@ func TestChannelLocalUnsignedUpdatesFailure(t *testing.T) { // Bob sends the final signature and Alice should not reject it. // <----sig----- - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err) - err = newAliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = newAliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) } @@ -9004,9 +9021,9 @@ func TestChannelSignedAckRegression(t *testing.T) { require.NoError(t, err) // -----sig----> - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) // <----rev----- @@ -9016,9 +9033,9 @@ func TestChannelSignedAckRegression(t *testing.T) { require.NoError(t, err) // <----sig----- - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) // Create an HTLC that Alice will send to Bob. @@ -9031,9 +9048,9 @@ func TestChannelSignedAckRegression(t *testing.T) { require.NoError(t, err) // -----sig----> - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) // <----rev----- @@ -9061,11 +9078,11 @@ func TestChannelSignedAckRegression(t *testing.T) { // Bob should no longer fail to sign this commitment due to faulty // update logs. // <----sig----- - bobSig, bobHtlcSigs, _, err = newBobChannel.SignNextCommitment() + bobNewCommit, err = newBobChannel.SignNextCommitment() require.NoError(t, err) // Alice should receive the new commitment without hiccups. - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) } @@ -9135,9 +9152,9 @@ func TestIsChannelClean(t *testing.T) { // removed from both commitments. // ---sig---> - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9149,9 +9166,9 @@ func TestIsChannelClean(t *testing.T) { assertCleanOrDirty(false, aliceChannel, bobChannel, t) // <---sig--- - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9170,9 +9187,9 @@ func TestIsChannelClean(t *testing.T) { assertCleanOrDirty(false, aliceChannel, bobChannel, t) // <---sig--- - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9184,9 +9201,9 @@ func TestIsChannelClean(t *testing.T) { assertCleanOrDirty(false, aliceChannel, bobChannel, t) // ---sig---> - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9208,9 +9225,9 @@ func TestIsChannelClean(t *testing.T) { assertCleanOrDirty(false, aliceChannel, bobChannel, t) // ---sig---> - aliceSig, aliceHtlcSigs, _, err = aliceChannel.SignNextCommitment() + aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9222,9 +9239,9 @@ func TestIsChannelClean(t *testing.T) { assertCleanOrDirty(false, aliceChannel, bobChannel, t) // <---sig--- - bobSig, bobHtlcSigs, _, err = bobChannel.SignNextCommitment() + bobNewCommit, err = bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9359,9 +9376,9 @@ func testGetDustSum(t *testing.T, chantype channeldb.ChannelType) { checkDust(bobChannel, htlc2Amt, htlc2Amt) // Alice signs for this HTLC and neither perspective should change. - aliceSig, aliceHtlcSigs, _, err := aliceChannel.SignNextCommitment() + aliceNewCommit, err := aliceChannel.SignNextCommitment() require.NoError(t, err) - err = bobChannel.ReceiveNewCommitment(aliceSig, aliceHtlcSigs) + err = bobChannel.ReceiveNewCommitment(aliceNewCommit.CommitSigs) require.NoError(t, err) checkDust(aliceChannel, htlc2Amt, htlc1Amt+htlc2Amt) checkDust(bobChannel, htlc2Amt, htlc2Amt) @@ -9378,9 +9395,9 @@ func testGetDustSum(t *testing.T, chantype channeldb.ChannelType) { // The rest of the dance is completed and neither perspective should // change. - bobSig, bobHtlcSigs, _, err := bobChannel.SignNextCommitment() + bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err) - err = aliceChannel.ReceiveNewCommitment(bobSig, bobHtlcSigs) + err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err) aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) diff --git a/lnwallet/test_utils.go b/lnwallet/test_utils.go index d28511feb..2559633dd 100644 --- a/lnwallet/test_utils.go +++ b/lnwallet/test_utils.go @@ -495,11 +495,12 @@ func calcStaticFee(chanType channeldb.ChannelType, numHTLCs int) btcutil.Amount // pending updates. This method is useful when testing interactions between two // live state machines. func ForceStateTransition(chanA, chanB *LightningChannel) error { - aliceSig, aliceHtlcSigs, _, err := chanA.SignNextCommitment() + aliceNewCommit, err := chanA.SignNextCommitment() if err != nil { return err } - if err = chanB.ReceiveNewCommitment(aliceSig, aliceHtlcSigs); err != nil { + err = chanB.ReceiveNewCommitment(aliceNewCommit.CommitSigs) + if err != nil { return err } @@ -507,7 +508,7 @@ func ForceStateTransition(chanA, chanB *LightningChannel) error { if err != nil { return err } - bobSig, bobHtlcSigs, _, err := chanB.SignNextCommitment() + bobNewCommit, err := chanB.SignNextCommitment() if err != nil { return err } @@ -515,7 +516,7 @@ func ForceStateTransition(chanA, chanB *LightningChannel) error { if _, _, _, _, err := chanA.ReceiveRevocation(bobRevocation); err != nil { return err } - if err := chanA.ReceiveNewCommitment(bobSig, bobHtlcSigs); err != nil { + if err := chanA.ReceiveNewCommitment(bobNewCommit.CommitSigs); err != nil { return err } diff --git a/lnwallet/transactions_test.go b/lnwallet/transactions_test.go index e2263fccb..6a28aa6ba 100644 --- a/lnwallet/transactions_test.go +++ b/lnwallet/transactions_test.go @@ -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 // 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") - err = remoteChannel.ReceiveNewCommitment(localSig, localHtlcSigs) + err = remoteChannel.ReceiveNewCommitment(localNewCommit.CommitSigs) require.NoError(t, err) revMsg, _, _, err := remoteChannel.RevokeCurrentCommitment() @@ -368,18 +368,22 @@ func testVectors(t *testing.T, chanType channeldb.ChannelType, test testCase) { _, _, _, _, err = localChannel.ReceiveRevocation(revMsg) require.NoError(t, err) - remoteSig, remoteHtlcSigs, _, err := remoteChannel.SignNextCommitment() + remoteNewCommit, err := remoteChannel.SignNextCommitment() require.NoError(t, err) - require.Equal(t, test.RemoteSigHex, - hex.EncodeToString(remoteSig.ToSignatureBytes())) + require.Equal( + t, test.RemoteSigHex, + hex.EncodeToString(remoteNewCommit.CommitSig.ToSignatureBytes()), + ) - for i, sig := range remoteHtlcSigs { - require.Equal(t, test.HtlcDescs[i].RemoteSigHex, - hex.EncodeToString(sig.ToSignatureBytes())) + for i, sig := range remoteNewCommit.HtlcSigs { + require.Equal( + t, test.HtlcDescs[i].RemoteSigHex, + hex.EncodeToString(sig.ToSignatureBytes()), + ) } - err = localChannel.ReceiveNewCommitment(remoteSig, remoteHtlcSigs) + err = localChannel.ReceiveNewCommitment(remoteNewCommit.CommitSigs) require.NoError(t, err) _, _, _, err = localChannel.RevokeCurrentCommitment()