diff --git a/lnwallet/channel.go b/lnwallet/channel.go index b1523db6..4c5bace6 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -880,13 +880,15 @@ func (lc *LightningChannel) diskCommitToMemCommit(isLocal bool, if localCommitPoint != nil { localCommitKeys = DeriveCommitmentKeys( localCommitPoint, true, tweaklessCommit, - lc.localChanCfg, lc.remoteChanCfg, + &lc.channelState.LocalChanCfg, + &lc.channelState.RemoteChanCfg, ) } if remoteCommitPoint != nil { remoteCommitKeys = DeriveCommitmentKeys( remoteCommitPoint, false, tweaklessCommit, - lc.localChanCfg, lc.remoteChanCfg, + &lc.channelState.LocalChanCfg, + &lc.channelState.RemoteChanCfg, ) } @@ -1360,10 +1362,6 @@ type LightningChannel struct { channelState *channeldb.OpenChannel - localChanCfg *channeldb.ChannelConfig - - remoteChanCfg *channeldb.ChannelConfig - // [local|remote]Log is a (mostly) append-only log storing all the HTLC // updates to this channel. The log is walked backwards as HTLC updates // are applied in order to re-construct a commitment transaction from a @@ -1416,8 +1414,6 @@ func NewLightningChannel(signer input.Signer, remoteCommitChain: newCommitmentChain(), localCommitChain: newCommitmentChain(), channelState: state, - localChanCfg: &state.LocalChanCfg, - remoteChanCfg: &state.RemoteChanCfg, localUpdateLog: localUpdateLog, remoteUpdateLog: remoteUpdateLog, ChanPoint: &state.FundingOutpoint, @@ -1449,8 +1445,10 @@ func NewLightningChannel(signer input.Signer, // createSignDesc derives the SignDescriptor for commitment transactions from // other fields on the LightningChannel. func (lc *LightningChannel) createSignDesc() error { - localKey := lc.localChanCfg.MultiSigKey.PubKey.SerializeCompressed() - remoteKey := lc.remoteChanCfg.MultiSigKey.PubKey.SerializeCompressed() + localKey := lc.channelState.LocalChanCfg.MultiSigKey.PubKey. + SerializeCompressed() + remoteKey := lc.channelState.RemoteChanCfg.MultiSigKey.PubKey. + SerializeCompressed() multiSigScript, err := input.GenMultiSigScript(localKey, remoteKey) if err != nil { @@ -1462,7 +1460,7 @@ func (lc *LightningChannel) createSignDesc() error { return err } lc.signDesc = &input.SignDescriptor{ - KeyDesc: lc.localChanCfg.MultiSigKey, + KeyDesc: lc.channelState.LocalChanCfg.MultiSigKey, WitnessScript: multiSigScript, Output: &wire.TxOut{ PkScript: fundingPkScript, @@ -1722,7 +1720,7 @@ func (lc *LightningChannel) restoreCommitState( tweaklessCommit := lc.channelState.ChanType.IsTweakless() pendingRemoteKeyChain = DeriveCommitmentKeys( pendingCommitPoint, false, tweaklessCommit, - lc.localChanCfg, lc.remoteChanCfg, + &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, ) } @@ -2324,9 +2322,9 @@ func (lc *LightningChannel) fetchCommitmentView(remoteChain bool, // be counted for the purpose of fee calculation. var dustLimit btcutil.Amount if remoteChain { - dustLimit = lc.remoteChanCfg.DustLimit + dustLimit = lc.channelState.RemoteChanCfg.DustLimit } else { - dustLimit = lc.localChanCfg.DustLimit + dustLimit = lc.channelState.LocalChanCfg.DustLimit } c := &commitment{ @@ -2434,11 +2432,11 @@ func (lc *LightningChannel) createCommitmentTx(c *commitment, delayBalance, p2wkhBalance btcutil.Amount ) if c.isOurs { - delay = uint32(lc.localChanCfg.CsvDelay) + delay = uint32(lc.channelState.LocalChanCfg.CsvDelay) delayBalance = ourBalance.ToSatoshis() p2wkhBalance = theirBalance.ToSatoshis() } else { - delay = uint32(lc.remoteChanCfg.CsvDelay) + delay = uint32(lc.channelState.RemoteChanCfg.CsvDelay) delayBalance = theirBalance.ToSatoshis() p2wkhBalance = ourBalance.ToSatoshis() } @@ -3143,12 +3141,12 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, switch { case ourBalance < ourInitialBalance && ourBalance < lnwire.NewMSatFromSatoshis( - lc.localChanCfg.ChanReserve): + lc.channelState.LocalChanCfg.ChanReserve): return ErrBelowChanReserve case theirBalance < theirInitialBalance && theirBalance < lnwire.NewMSatFromSatoshis( - lc.remoteChanCfg.ChanReserve): + lc.channelState.RemoteChanCfg.ChanReserve): return ErrBelowChanReserve } @@ -3199,7 +3197,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, // First check that the remote updates won't violate it's channel // constraints. err := validateUpdates( - filteredView.theirUpdates, lc.remoteChanCfg, + filteredView.theirUpdates, &lc.channelState.RemoteChanCfg, ) if err != nil { return err @@ -3208,7 +3206,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, // Secondly check that our updates won't violate our channel // constraints. err = validateUpdates( - filteredView.ourUpdates, lc.localChanCfg, + filteredView.ourUpdates, &lc.channelState.LocalChanCfg, ) if err != nil { return err @@ -3276,7 +3274,7 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, []ch // construct the commitment state. keyRing := DeriveCommitmentKeys( commitPoint, false, lc.channelState.ChanType.IsTweakless(), - lc.localChanCfg, lc.remoteChanCfg, + &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, ) // Create a new commitment view which will calculate the evaluated @@ -3313,7 +3311,8 @@ func (lc *LightningChannel) SignNextCommitment() (lnwire.Sig, []lnwire.Sig, []ch // commitment state. We do so in two phases: first we generate and // submit the set of signature jobs to the worker pool. sigBatch, cancelChan, err := genRemoteHtlcSigJobs(keyRing, - lc.localChanCfg, lc.remoteChanCfg, newCommitView, + &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, + newCommitView, ) if err != nil { return sig, htlcSigs, nil, err @@ -3696,10 +3695,10 @@ func (lc *LightningChannel) computeView(view *htlcView, remoteChain bool, *htlcView) { commitChain := lc.localCommitChain - dustLimit := lc.localChanCfg.DustLimit + dustLimit := lc.channelState.LocalChanCfg.DustLimit if remoteChain { commitChain = lc.remoteCommitChain - dustLimit = lc.remoteChanCfg.DustLimit + dustLimit = lc.channelState.RemoteChanCfg.DustLimit } // Since the fetched htlc view will include all updates added after the @@ -4033,7 +4032,7 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, commitPoint := input.ComputeCommitmentPoint(commitSecret[:]) keyRing := DeriveCommitmentKeys( commitPoint, true, lc.channelState.ChanType.IsTweakless(), - lc.localChanCfg, lc.remoteChanCfg, + &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, ) // With the current commitment point re-calculated, construct the new @@ -4081,8 +4080,8 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, // pool to verify each of the HTLc signatures presented. Once // generated, we'll submit these jobs to the worker pool. verifyJobs, err := genHtlcSigValidationJobs( - localCommitmentView, keyRing, htlcSigs, lc.localChanCfg, - lc.remoteChanCfg, + localCommitmentView, keyRing, htlcSigs, + &lc.channelState.LocalChanCfg, &lc.channelState.RemoteChanCfg, ) if err != nil { return err @@ -4095,8 +4094,8 @@ func (lc *LightningChannel) ReceiveNewCommitment(commitSig lnwire.Sig, // we'll ensure that the newly constructed commitment state has a valid // signature. verifyKey := btcec.PublicKey{ - X: lc.remoteChanCfg.MultiSigKey.PubKey.X, - Y: lc.remoteChanCfg.MultiSigKey.PubKey.Y, + X: lc.channelState.RemoteChanCfg.MultiSigKey.PubKey.X, + Y: lc.channelState.RemoteChanCfg.MultiSigKey.PubKey.Y, Curve: btcec.S256(), } cSig, err := commitSig.ToSignature() @@ -5046,8 +5045,10 @@ func (lc *LightningChannel) getSignedCommitTx() (*wire.MsgTx, error) { // With the final signature generated, create the witness stack // required to spend from the multi-sig output. - ourKey := lc.localChanCfg.MultiSigKey.PubKey.SerializeCompressed() - theirKey := lc.remoteChanCfg.MultiSigKey.PubKey.SerializeCompressed() + ourKey := lc.channelState.LocalChanCfg.MultiSigKey.PubKey. + SerializeCompressed() + theirKey := lc.channelState.RemoteChanCfg.MultiSigKey.PubKey. + SerializeCompressed() commitTx.TxIn[0].Witness = input.SpendMultiSig( lc.signDesc.WitnessScript, ourKey, @@ -5911,8 +5912,8 @@ func (lc *LightningChannel) CreateCloseProposal(proposedFee btcutil.Amount, } closeTx := CreateCooperativeCloseTx( - lc.fundingTxIn(), lc.localChanCfg.DustLimit, - lc.remoteChanCfg.DustLimit, ourBalance, theirBalance, + lc.fundingTxIn(), lc.channelState.LocalChanCfg.DustLimit, + lc.channelState.RemoteChanCfg.DustLimit, ourBalance, theirBalance, localDeliveryScript, remoteDeliveryScript, ) @@ -5982,8 +5983,8 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig []byte, // on this active channel back to both parties. In this current model, // the initiator pays full fees for the cooperative close transaction. closeTx := CreateCooperativeCloseTx( - lc.fundingTxIn(), lc.localChanCfg.DustLimit, - lc.remoteChanCfg.DustLimit, ourBalance, theirBalance, + lc.fundingTxIn(), lc.channelState.LocalChanCfg.DustLimit, + lc.channelState.RemoteChanCfg.DustLimit, ourBalance, theirBalance, localDeliveryScript, remoteDeliveryScript, ) @@ -5998,8 +5999,10 @@ func (lc *LightningChannel) CompleteCooperativeClose(localSig, remoteSig []byte, // Finally, construct the witness stack minding the order of the // pubkeys+sigs on the stack. - ourKey := lc.localChanCfg.MultiSigKey.PubKey.SerializeCompressed() - theirKey := lc.remoteChanCfg.MultiSigKey.PubKey.SerializeCompressed() + ourKey := lc.channelState.LocalChanCfg.MultiSigKey.PubKey. + SerializeCompressed() + theirKey := lc.channelState.RemoteChanCfg.MultiSigKey.PubKey. + SerializeCompressed() witness := input.SpendMultiSig(lc.signDesc.WitnessScript, ourKey, localSig, theirKey, remoteSig) closeTx.TxIn[0].Witness = witness @@ -6446,7 +6449,7 @@ func (lc *LightningChannel) ActiveHtlcs() []channeldb.HTLC { // LocalChanReserve returns our local ChanReserve requirement for the remote party. func (lc *LightningChannel) LocalChanReserve() btcutil.Amount { - return lc.localChanCfg.ChanReserve + return lc.channelState.LocalChanCfg.ChanReserve } // NextLocalHtlcIndex returns the next unallocated local htlc index. To ensure @@ -6471,5 +6474,5 @@ func (lc *LightningChannel) RemoteCommitHeight() uint64 { // FwdMinHtlc returns the minimum HTLC value required by the remote node, i.e. // the minimum value HTLC we can forward on this channel. func (lc *LightningChannel) FwdMinHtlc() lnwire.MilliSatoshi { - return lc.localChanCfg.MinHTLC + return lc.channelState.LocalChanCfg.MinHTLC } diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 665917cf..c0467af9 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -615,7 +615,7 @@ func TestForceClose(t *testing.T) { // Alice's listed CSV delay should also match the delay that was // pre-committed to at channel opening. if aliceCommitResolution.MaturityDelay != - uint32(aliceChannel.localChanCfg.CsvDelay) { + uint32(aliceChannel.channelState.LocalChanCfg.CsvDelay) { t.Fatalf("alice: incorrect local CSV delay in ForceCloseSummary, "+ "expected %v, got %v", @@ -816,10 +816,10 @@ func TestForceCloseDustOutput(t *testing.T) { // We set both node's channel reserves to 0, to make sure // they can create small dust ouputs without going under // their channel reserves. - aliceChannel.localChanCfg.ChanReserve = 0 - bobChannel.localChanCfg.ChanReserve = 0 - aliceChannel.remoteChanCfg.ChanReserve = 0 - bobChannel.remoteChanCfg.ChanReserve = 0 + aliceChannel.channelState.LocalChanCfg.ChanReserve = 0 + bobChannel.channelState.LocalChanCfg.ChanReserve = 0 + aliceChannel.channelState.RemoteChanCfg.ChanReserve = 0 + bobChannel.channelState.RemoteChanCfg.ChanReserve = 0 htlcAmount := lnwire.NewMSatFromSatoshis(500) @@ -1268,8 +1268,8 @@ func TestChannelBalanceDustLimit(t *testing.T) { // To allow Alice's balance to get beneath her dust limit, set the // channel reserve to be 0. - aliceChannel.localChanCfg.ChanReserve = 0 - bobChannel.remoteChanCfg.ChanReserve = 0 + aliceChannel.channelState.LocalChanCfg.ChanReserve = 0 + bobChannel.channelState.RemoteChanCfg.ChanReserve = 0 // This amount should leave an amount larger than Alice's dust limit // once fees have been subtracted, but smaller than Bob's dust limit. @@ -2553,7 +2553,7 @@ func TestAddHTLCNegativeBalance(t *testing.T) { // We set the channel reserve to 0, such that we can add HTLCs all the // way to a negative balance. - aliceChannel.localChanCfg.ChanReserve = 0 + aliceChannel.channelState.LocalChanCfg.ChanReserve = 0 // First, we'll add 3 HTLCs of 1 BTC each to Alice's commitment. const numHTLCs = 3 @@ -5346,14 +5346,14 @@ func TestMaxAcceptedHTLCs(t *testing.T) { // Set the remote's required MaxAcceptedHtlcs. This means that alice // can only offer the remote up to numHTLCs HTLCs. - aliceChannel.localChanCfg.MaxAcceptedHtlcs = numHTLCs - bobChannel.remoteChanCfg.MaxAcceptedHtlcs = numHTLCs + aliceChannel.channelState.LocalChanCfg.MaxAcceptedHtlcs = numHTLCs + bobChannel.channelState.RemoteChanCfg.MaxAcceptedHtlcs = numHTLCs // Similarly, set the remote config's MaxAcceptedHtlcs. This means // that the remote will be aware that Alice will only accept up to // numHTLCsRecevied at a time. - aliceChannel.remoteChanCfg.MaxAcceptedHtlcs = numHTLCsReceived - bobChannel.localChanCfg.MaxAcceptedHtlcs = numHTLCsReceived + aliceChannel.channelState.RemoteChanCfg.MaxAcceptedHtlcs = numHTLCsReceived + bobChannel.channelState.LocalChanCfg.MaxAcceptedHtlcs = numHTLCsReceived // Each HTLC amount is 0.1 BTC. htlcAmt := lnwire.NewMSatFromSatoshis(0.1 * btcutil.SatoshiPerBitcoin) @@ -5409,8 +5409,8 @@ func TestMaxPendingAmount(t *testing.T) { // We set the max pending amount of Alice's config. This mean that she // cannot offer Bob HTLCs with a total value above this limit at a given // time. - aliceChannel.localChanCfg.MaxPendingAmount = maxPending - bobChannel.remoteChanCfg.MaxPendingAmount = maxPending + aliceChannel.channelState.LocalChanCfg.MaxPendingAmount = maxPending + bobChannel.channelState.RemoteChanCfg.MaxPendingAmount = maxPending // First, we'll add 2 HTLCs of 1.5 BTC each to Alice's commitment. // This won't trigger Alice's ErrMaxPendingAmount error. @@ -5498,20 +5498,20 @@ func TestChanReserve(t *testing.T) { // Alice will need to keep her reserve above aliceMinReserve, // so set this limit to here local config. - aliceChannel.localChanCfg.ChanReserve = aliceMinReserve + aliceChannel.channelState.LocalChanCfg.ChanReserve = aliceMinReserve // During channel opening Bob will also get to know Alice's // minimum reserve, and this will be found in his remote // config. - bobChannel.remoteChanCfg.ChanReserve = aliceMinReserve + bobChannel.channelState.RemoteChanCfg.ChanReserve = aliceMinReserve // We set Bob's channel reserve to a value that is larger than // his current balance in the channel. This will ensure that // after a channel is first opened, Bob can still receive HTLCs // even though his balance is less than his channel reserve. bobMinReserve := btcutil.Amount(6 * btcutil.SatoshiPerBitcoin) - bobChannel.localChanCfg.ChanReserve = bobMinReserve - aliceChannel.remoteChanCfg.ChanReserve = bobMinReserve + bobChannel.channelState.LocalChanCfg.ChanReserve = bobMinReserve + aliceChannel.channelState.RemoteChanCfg.ChanReserve = bobMinReserve return aliceChannel, bobChannel, cleanUp } @@ -5713,8 +5713,8 @@ func TestMinHTLC(t *testing.T) { // Setting the min value in Alice's local config means that the // remote will not accept any HTLCs of value less than specified. - aliceChannel.localChanCfg.MinHTLC = minValue - bobChannel.remoteChanCfg.MinHTLC = minValue + aliceChannel.channelState.LocalChanCfg.MinHTLC = minValue + bobChannel.channelState.RemoteChanCfg.MinHTLC = minValue // First, we will add an HTLC of 0.5 BTC. This will not trigger // ErrBelowMinHTLC. diff --git a/lnwallet/transactions_test.go b/lnwallet/transactions_test.go index ba6d0726..47022e86 100644 --- a/lnwallet/transactions_test.go +++ b/lnwallet/transactions_test.go @@ -421,10 +421,8 @@ func TestCommitmentAndHTLCTransactions(t *testing.T) { // Construct a LightningChannel manually because we don't have nor need all // of the dependencies. channel := LightningChannel{ - channelState: &channelState, - Signer: signer, - localChanCfg: &channelState.LocalChanCfg, - remoteChanCfg: &channelState.RemoteChanCfg, + channelState: &channelState, + Signer: signer, } err = channel.createSignDesc() if err != nil { @@ -845,8 +843,8 @@ func TestCommitmentAndHTLCTransactions(t *testing.T) { // commitment tx. htlcResolutions, err := extractHtlcResolutions( chainfee.SatPerKWeight(test.commitment.FeePerKw), true, signer, - htlcs, keys, channel.localChanCfg, channel.remoteChanCfg, - commitTx.TxHash(), + htlcs, keys, &channel.channelState.LocalChanCfg, + &channel.channelState.RemoteChanCfg, commitTx.TxHash(), ) if err != nil { t.Errorf("Case %d: Failed to extract HTLC resolutions: %v", i, err)