chanstate: use channel types in backups

Move static channel backup construction to chanstate channel types.

The package still imports channeldb where it uses real database
concerns, including address sourcing and duplicate-channel recovery.
The backup payload and live-channel source boundaries no longer depend
on the channeldb compatibility aliases.
This commit is contained in:
ziggie 2026-05-15 13:37:24 -03:00
parent c58d51d593
commit dac8a8fe53
No known key found for this signature in database
GPG key ID: 1AFF9C4DCED6D666
5 changed files with 30 additions and 27 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/chanstate"
"github.com/lightningnetwork/lnd/fn/v2"
)
@ -14,11 +15,11 @@ import (
// commitment transaction broadcast.
type LiveChannelSource interface {
// FetchAllChannels returns all known live channels.
FetchAllChannels() ([]*channeldb.OpenChannel, error)
FetchAllChannels() ([]*chanstate.OpenChannel, error)
// FetchChannel attempts to locate a live channel identified by the
// passed chanPoint. Optionally an existing db tx can be supplied.
FetchChannel(chanPoint wire.OutPoint) (*channeldb.OpenChannel, error)
FetchChannel(chanPoint wire.OutPoint) (*chanstate.OpenChannel, error)
}
// assembleChanBackup attempts to assemble a static channel backup for the
@ -26,7 +27,7 @@ type LiveChannelSource interface {
// the channel, as well as addressing information so we can find the peer and
// reconnect to them to initiate the protocol.
func assembleChanBackup(ctx context.Context, addrSource channeldb.AddrSource,
openChan *channeldb.OpenChannel) (*Single, error) {
openChan *chanstate.OpenChannel) (*Single, error) {
log.Debugf("Crafting backup for ChannelPoint(%v)",
openChan.FundingOutpoint)
@ -55,7 +56,7 @@ func assembleChanBackup(ctx context.Context, addrSource channeldb.AddrSource,
// in loss of funds! This may happen if an outdated channel backup is attempted
// to be used to force close the channel.
func buildCloseTxInputs(
targetChan *channeldb.OpenChannel) fn.Option[CloseTxInputs] {
targetChan *chanstate.OpenChannel) fn.Option[CloseTxInputs] {
log.Debugf("Crafting CloseTxInputs for ChannelPoint(%v)",
targetChan.FundingOutpoint)

View file

@ -8,12 +8,12 @@ import (
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/chanstate"
"github.com/stretchr/testify/require"
)
type mockChannelSource struct {
chans map[wire.OutPoint]*channeldb.OpenChannel
chans map[wire.OutPoint]*chanstate.OpenChannel
failQuery bool
@ -22,17 +22,19 @@ type mockChannelSource struct {
func newMockChannelSource() *mockChannelSource {
return &mockChannelSource{
chans: make(map[wire.OutPoint]*channeldb.OpenChannel),
chans: make(map[wire.OutPoint]*chanstate.OpenChannel),
addrs: make(map[[33]byte][]net.Addr),
}
}
func (m *mockChannelSource) FetchAllChannels() ([]*channeldb.OpenChannel, error) {
func (m *mockChannelSource) FetchAllChannels() (
[]*chanstate.OpenChannel, error) {
if m.failQuery {
return nil, fmt.Errorf("fail")
}
chans := make([]*channeldb.OpenChannel, 0, len(m.chans))
chans := make([]*chanstate.OpenChannel, 0, len(m.chans))
for _, channel := range m.chans {
chans = append(chans, channel)
}
@ -41,7 +43,7 @@ func (m *mockChannelSource) FetchAllChannels() ([]*channeldb.OpenChannel, error)
}
func (m *mockChannelSource) FetchChannel(chanPoint wire.OutPoint) (
*channeldb.OpenChannel, error) {
*chanstate.OpenChannel, error) {
if m.failQuery {
return nil, fmt.Errorf("fail")

View file

@ -10,7 +10,7 @@ import (
"sync/atomic"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/chanstate"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnutils"
)
@ -31,7 +31,7 @@ type Swapper interface {
// ChannelWithAddrs bundles an open channel along with all the addresses for
// the channel peer.
type ChannelWithAddrs struct {
*channeldb.OpenChannel
*chanstate.OpenChannel
// Addrs is the set of addresses that we can use to reach the target
// peer.

View file

@ -11,7 +11,7 @@ import (
"github.com/btcsuite/btcd/btcutil/v2"
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/chanstate"
"github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnencrypt"
@ -169,7 +169,7 @@ type Single struct {
//
// NOTE: Of the items in the ChannelConstraints, we only write the CSV
// delay.
LocalChanCfg channeldb.ChannelConfig
LocalChanCfg chanstate.ChannelConfig
// RemoteChanCfg is the remote channel confirmation. We store this as
// well since we'll need some of their keys to re-derive things like
@ -178,7 +178,7 @@ type Single struct {
//
// NOTE: Of the items in the ChannelConstraints, we only write the CSV
// delay.
RemoteChanCfg channeldb.ChannelConfig
RemoteChanCfg chanstate.ChannelConfig
// ShaChainRootDesc describes how to derive the private key that was
// used as the shachain root for this channel.
@ -234,7 +234,7 @@ type CloseTxInputs struct {
// connect to the channel peer. If possible, we include the data needed to
// produce a force close transaction from the most recent state using externally
// provided private key.
func NewSingle(channel *channeldb.OpenChannel,
func NewSingle(channel *chanstate.OpenChannel,
nodeAddrs []net.Addr) Single {
var shaChainRootDesc keychain.KeyDescriptor

View file

@ -12,7 +12,7 @@ import (
"github.com/btcsuite/btcd/chainhash/v2"
"github.com/btcsuite/btcd/wire/v2"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/chanstate"
"github.com/lightningnetwork/lnd/fn/v2"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnencrypt"
@ -135,7 +135,7 @@ func assertSingleEqual(t *testing.T, a, b Single) {
}
}
func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
func genRandomOpenChannelShell() (*chanstate.OpenChannel, error) {
var testPriv [32]byte
if _, err := rand.Read(testPriv[:]); err != nil {
return nil, err
@ -162,11 +162,11 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
isInitiator = true
}
chanType := channeldb.ChannelType(rand.Intn(1 << 12))
chanType := chanstate.ChannelType(rand.Intn(1 << 12))
localCfg := channeldb.ChannelConfig{
ChannelStateBounds: channeldb.ChannelStateBounds{},
CommitmentParams: channeldb.CommitmentParams{
localCfg := chanstate.ChannelConfig{
ChannelStateBounds: chanstate.ChannelStateBounds{},
CommitmentParams: chanstate.CommitmentParams{
CsvDelay: uint16(rand.Int63()),
},
MultiSigKey: keychain.KeyDescriptor{
@ -201,8 +201,8 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
},
}
remoteCfg := channeldb.ChannelConfig{
CommitmentParams: channeldb.CommitmentParams{
remoteCfg := chanstate.ChannelConfig{
CommitmentParams: chanstate.CommitmentParams{
CsvDelay: uint16(rand.Int63()),
},
MultiSigKey: keychain.KeyDescriptor{
@ -222,14 +222,14 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
},
}
var localCommit channeldb.ChannelCommitment
var localCommit chanstate.ChannelCommitment
if chanType.IsTaproot() {
var commitSig [64]byte
if _, err := rand.Read(commitSig[:]); err != nil {
return nil, err
}
localCommit = channeldb.ChannelCommitment{
localCommit = chanstate.ChannelCommitment{
CommitTx: sampleCommitTx,
CommitSig: commitSig[:],
CommitHeight: rand.Uint64(),
@ -245,7 +245,7 @@ func genRandomOpenChannelShell() (*channeldb.OpenChannel, error) {
tapscriptRootOption = fn.Some(tapscriptRoot)
}
return &channeldb.OpenChannel{
return &chanstate.OpenChannel{
ChainHash: chainHash,
ChanType: chanType,
IsInitiator: isInitiator,