lnd/chainparams/store.go
Oli 8047149c6a
multi: upgrade to btcd v2 modules
Migrate all btcd dependencies to the new per-package v2 modules (wire/v2,
txscript/v2, chaincfg/v2, chainhash/v2, btcutil/v2, psbt/v2, btcec/v2)
introduced by btcd v0.26.0, and pin the tagged ecosystem versions:
btcwallet v0.17.0, neutrino v0.18.0 and lightning-onion v1.4.0.

The bulk of the import rewrite was produced by the scripted diff from
https://github.com/btcsuite/btcd/pull/2547 (followed by 'make rpc'). The
address symbols that moved out of btcutil into the new address package
are imported as btcaddr where a local "address" variable would otherwise
shadow them. The go.mod/go.sum updates and the remaining manual
compilation fixes are folded into this single commit so it builds on its
own (the migration was previously split into a reproducible scripted-diff
plus follow-ups, intended to be squashed on merge).
2026-06-24 10:58:36 -07:00

114 lines
3.3 KiB
Go

package chainparams
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg/v2"
"github.com/lightningnetwork/lnd/lncfg"
"github.com/lightningnetwork/lnd/sqldb"
"github.com/lightningnetwork/lnd/sqldb/sqlc"
)
// ErrNetworkMismatch is returned by ValidateNetwork when the network stored in
// the database does not match the network lnd is configured to use.
var ErrNetworkMismatch = errors.New("database network mismatch")
// SQLChainParamQueries defines the SQL queries required by Store.
type SQLChainParamQueries interface {
InsertChainNetwork(ctx context.Context, network string) error
GetChainNetwork(ctx context.Context) (string, error)
}
// BatchedChainParamQueries is a version of SQLChainParamQueries that is
// capable of batched database operations.
type BatchedChainParamQueries interface {
SQLChainParamQueries
sqldb.BatchedTx[SQLChainParamQueries]
}
// Store is a database-backed store that persists and retrieves chain-level
// parameters such as the network the database was initialised for.
type Store struct {
db BatchedChainParamQueries
}
// NewStore creates a new chain params Store backed by the given BaseDB.
func NewStore(db *sqldb.BaseDB) *Store {
executor := sqldb.NewTransactionExecutor(
db, func(tx *sql.Tx) SQLChainParamQueries {
return db.WithTx(tx)
},
)
return &Store{db: executor}
}
// ValidateNetwork checks that the network stored in the chain_params table
// matches the provided network. On the first call the network is persisted so
// that subsequent restarts can detect an accidental network switch.
func (s *Store) ValidateNetwork(ctx context.Context,
net *chaincfg.Params) error {
network, err := normalizeNetworkName(net)
if err != nil {
return err
}
return s.db.ExecTx(
ctx, sqldb.WriteTxOpt(),
func(tx SQLChainParamQueries) error {
// Insert the network only if the chain_params table is
// still empty. This is a no-op on every startup after
// the first.
err := tx.InsertChainNetwork(ctx, network)
if err != nil {
return fmt.Errorf("unable to set network in "+
"chain_params: %w", err)
}
// Read back whatever is stored. This is either the
// value we just inserted (first startup) or a value
// from a previous run.
storedNetwork, err := tx.GetChainNetwork(ctx)
if err != nil {
return fmt.Errorf("unable to read network "+
"from chain_params: %w", err)
}
if storedNetwork != network {
return fmt.Errorf("%w: the database was "+
"previously used with network '%s', "+
"but lnd is now configured for "+
"network '%s'. To fix this, either "+
"point lnd at a different database "+
"or reconfigure lnd to use "+
"network '%s'", ErrNetworkMismatch,
storedNetwork, network, storedNetwork)
}
return nil
}, sqldb.NoOpReset,
)
}
// normalizeNetworkName returns the stable network identifier persisted in the
// chain_params table.
func normalizeNetworkName(net *chaincfg.Params) (string, error) {
if net == nil {
return "", fmt.Errorf("chain parameters must not be nil")
}
network := lncfg.NormalizeNetwork(net.Name)
if network == "" {
return "", fmt.Errorf("chain parameters must define a network")
}
return network, nil
}
// Compile-time check that *sqlc.Queries implements SQLChainParamQueries.
var _ SQLChainParamQueries = (*sqlc.Queries)(nil)