funding: remove dead code and sanity check pending chan ID (#7887)

* funding: remove unused field `newChanBarriers`

This commit removes the occurance of `newChanBarriers` as it's not used
anywhere.

* funding: rename method names to clear the funding flow

Slightly refactored the names so it's easier to see which side is
processing what messages.

* lnwallet: sanity check empty pending channel ID

This commit adds a sanity check to make sure an empty pending channel ID
will not be accepted.
This commit is contained in:
Yong authored and GitHub committed 2023-10-09 10:58:18 +02:00
1 parent 80bfd17cf2
commit ec2377db79
4 files changed
+80 -52

No files matched your search

+4 -3
View File
@@ -773,6 +773,7 @@ func testReservationInitiatorBalanceBelowDustCancel(miner *rpctest.Harness,
PushMSat: 0,
Flags: lnwire.FFAnnounceChannel,
CommitType: lnwallet.CommitmentTypeTweakless,
PendingChanID: [32]byte{1},
}
_, err = alice.InitChannelReservation(req)
switch {
@@ -2744,7 +2745,7 @@ var walletTests = []walletTestCase{
testSingleFunderReservationWorkflow(
miner, alice, bob, t,
lnwallet.CommitmentTypeLegacy, nil,
nil, [32]byte{}, 0,
nil, [32]byte{1}, 0,
)
},
},
@@ -2756,7 +2757,7 @@ var walletTests = []walletTestCase{
testSingleFunderReservationWorkflow(
miner, alice, bob, t,
lnwallet.CommitmentTypeTweakless, nil,
nil, [32]byte{}, 0,
nil, [32]byte{1}, 0,
)
},
},
@@ -2768,7 +2769,7 @@ var walletTests = []walletTestCase{
testSingleFunderReservationWorkflow(
miner, alice, bob, t,
lnwallet.CommitmentTypeSimpleTaproot, nil,
nil, [32]byte{}, 0,
nil, [32]byte{1}, 0,
)
},
},
+16 -2
View File
@@ -62,6 +62,14 @@ var (
ErrReservedValueInvalidated = errors.New("reserved wallet balance " +
"invalidated: transaction would leave insufficient funds for " +
"fee bumping anchor channel closings (see debug log for details)")
// ErrEmptyPendingChanID is returned when an empty value is used for
// the pending channel ID.
ErrEmptyPendingChanID = errors.New("pending channel ID is empty")
// ErrDuplicatePendingChanID is returned when an existing pending
// channel ID is registered again.
ErrDuplicatePendingChanID = errors.New("duplicate pending channel ID")
)
// PsbtFundingRequired is a type that implements the error interface and
@@ -670,9 +678,15 @@ func (l *LightningWallet) RegisterFundingIntent(expectedID [32]byte,
l.intentMtx.Lock()
defer l.intentMtx.Unlock()
// Sanity check the pending channel ID is not empty.
var zeroID [32]byte
if expectedID == zeroID {
return ErrEmptyPendingChanID
}
if _, ok := l.fundingIntents[expectedID]; ok {
return fmt.Errorf("pendingChanID(%x) already has intent "+
"registered", expectedID[:])
return fmt.Errorf("%w: already has intent registered: %v",
ErrDuplicatePendingChanID, expectedID[:])
}
l.fundingIntents[expectedID] = shimIntent
+34
View File
@@ -0,0 +1,34 @@
package lnwallet
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestRegisterFundingIntent checks RegisterFundingIntent behaves as expected.
func TestRegisterFundingIntent(t *testing.T) {
t.Parallel()
require := require.New(t)
// Create a testing wallet.
lw, err := NewLightningWallet(Config{})
require.NoError(err)
// Init an empty testing channel ID.
var testID [32]byte
// Call the method with empty ID should give us an error.
err = lw.RegisterFundingIntent(testID, nil)
require.ErrorIs(err, ErrEmptyPendingChanID)
// Modify the ID and call the method again should result in no error.
testID[0] = 1
err = lw.RegisterFundingIntent(testID, nil)
require.NoError(err)
// Call the method using the same ID should give us an error.
err = lw.RegisterFundingIntent(testID, nil)
require.ErrorIs(err, ErrDuplicatePendingChanID)
}