2017-05-03 05:16:37 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-18 21:49:32 +03:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-05-03 05:16:37 +03:00
|
|
|
"sync"
|
2017-05-18 21:49:32 +03:00
|
|
|
"time"
|
2017-05-03 05:16:37 +03:00
|
|
|
|
2017-05-18 21:49:32 +03:00
|
|
|
"github.com/lightninglabs/neutrino"
|
2017-05-03 05:16:37 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2017-05-18 21:49:32 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
|
2017-05-25 03:46:37 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs/neutrinonotify"
|
2017-05-18 21:49:32 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2017-06-17 01:12:34 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2017-05-03 05:16:37 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2017-05-18 21:49:32 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
|
|
|
|
"github.com/lightningnetwork/lnd/routing/chainview"
|
2017-05-03 05:16:37 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg/chainhash"
|
2017-08-25 04:54:17 +03:00
|
|
|
"github.com/roasbeef/btcd/rpcclient"
|
2017-11-23 22:36:12 +03:00
|
|
|
"github.com/roasbeef/btcutil"
|
2017-05-25 03:46:37 +03:00
|
|
|
"github.com/roasbeef/btcwallet/chain"
|
2017-05-18 21:49:32 +03:00
|
|
|
"github.com/roasbeef/btcwallet/walletdb"
|
2017-05-03 05:16:37 +03:00
|
|
|
)
|
|
|
|
|
2017-07-31 02:01:44 +03:00
|
|
|
// defaultChannelConstraints is the default set of channel constraints that are
|
|
|
|
// meant to be used when initially funding a channel.
|
|
|
|
//
|
|
|
|
// TODO(roasbeef): have one for both chains
|
2017-12-17 16:33:49 +03:00
|
|
|
// TODO(halseth): make configurable at startup?
|
2017-07-31 02:01:44 +03:00
|
|
|
var defaultChannelConstraints = channeldb.ChannelConstraints{
|
|
|
|
DustLimit: lnwallet.DefaultDustLimit(),
|
|
|
|
MaxAcceptedHtlcs: lnwallet.MaxHTLCNumber / 2,
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:53:04 +03:00
|
|
|
// chainCode is an enum-like structure for keeping track of the chains
|
|
|
|
// currently supported within lnd.
|
2017-05-03 05:16:37 +03:00
|
|
|
type chainCode uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// bitcoinChain is Bitcoin's testnet chain.
|
|
|
|
bitcoinChain chainCode = iota
|
|
|
|
|
|
|
|
// litecoinChain is Litecoin's testnet chain.
|
|
|
|
litecoinChain
|
|
|
|
)
|
|
|
|
|
|
|
|
// String returns a string representation of the target chainCode.
|
|
|
|
func (c chainCode) String() string {
|
|
|
|
switch c {
|
|
|
|
case bitcoinChain:
|
|
|
|
return "bitcoin"
|
|
|
|
case litecoinChain:
|
|
|
|
return "litecoin"
|
|
|
|
default:
|
|
|
|
return "kekcoin"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// chainControl couples the three primary interfaces lnd utilizes for a
|
|
|
|
// particular chain together. A single chainControl instance will exist for all
|
|
|
|
// the chains lnd is currently active on.
|
|
|
|
type chainControl struct {
|
2017-05-18 21:49:32 +03:00
|
|
|
chainIO lnwallet.BlockChainIO
|
|
|
|
|
|
|
|
feeEstimator lnwallet.FeeEstimator
|
|
|
|
|
|
|
|
signer lnwallet.Signer
|
|
|
|
|
|
|
|
msgSigner lnwallet.MessageSigner
|
|
|
|
|
2017-05-03 05:16:37 +03:00
|
|
|
chainNotifier chainntnfs.ChainNotifier
|
|
|
|
|
2017-05-18 21:49:32 +03:00
|
|
|
chainView chainview.FilteredChainView
|
|
|
|
|
2017-05-03 05:16:37 +03:00
|
|
|
wallet *lnwallet.LightningWallet
|
2017-06-17 01:12:34 +03:00
|
|
|
|
|
|
|
routingPolicy htlcswitch.ForwardingPolicy
|
2017-05-03 05:16:37 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 03:46:37 +03:00
|
|
|
// newChainControlFromConfig attempts to create a chainControl instance
|
|
|
|
// according to the parameters in the passed lnd configuration. Currently two
|
|
|
|
// branches of chainControl instances exist: one backed by a running btcd
|
|
|
|
// full-node, and the other backed by a running neutrino light client instance.
|
2017-10-12 12:22:33 +03:00
|
|
|
func newChainControlFromConfig(cfg *config, chanDB *channeldb.DB,
|
2017-10-20 05:53:19 +03:00
|
|
|
privateWalletPw, publicWalletPw []byte) (*chainControl, func(), error) {
|
|
|
|
|
2017-05-18 21:49:32 +03:00
|
|
|
// Set the RPC config from the "home" chain. Multi-chain isn't yet
|
|
|
|
// active, so we'll restrict usage to a particular chain for now.
|
|
|
|
homeChainConfig := cfg.Bitcoin
|
|
|
|
if registeredChains.PrimaryChain() == litecoinChain {
|
|
|
|
homeChainConfig = cfg.Litecoin
|
|
|
|
}
|
|
|
|
ltndLog.Infof("Primary chain is set to: %v",
|
|
|
|
registeredChains.PrimaryChain())
|
|
|
|
|
2017-09-04 02:53:04 +03:00
|
|
|
cc := &chainControl{}
|
2017-05-18 21:49:32 +03:00
|
|
|
|
2017-06-17 01:12:34 +03:00
|
|
|
switch registeredChains.PrimaryChain() {
|
|
|
|
case bitcoinChain:
|
2017-12-17 16:33:49 +03:00
|
|
|
cc.routingPolicy = htlcswitch.ForwardingPolicy{
|
|
|
|
MinHTLC: cfg.Bitcoin.MinHTLC,
|
|
|
|
BaseFee: cfg.Bitcoin.BaseFee,
|
|
|
|
FeeRate: cfg.Bitcoin.FeeRate,
|
|
|
|
TimeLockDelta: cfg.Bitcoin.TimeLockDelta,
|
|
|
|
}
|
2017-09-04 02:53:04 +03:00
|
|
|
cc.feeEstimator = lnwallet.StaticFeeEstimator{
|
|
|
|
FeeRate: 50,
|
|
|
|
}
|
2017-06-17 01:12:34 +03:00
|
|
|
case litecoinChain:
|
2017-12-17 16:33:49 +03:00
|
|
|
cc.routingPolicy = htlcswitch.ForwardingPolicy{
|
|
|
|
MinHTLC: cfg.Litecoin.MinHTLC,
|
|
|
|
BaseFee: cfg.Litecoin.BaseFee,
|
|
|
|
FeeRate: cfg.Litecoin.FeeRate,
|
|
|
|
TimeLockDelta: cfg.Litecoin.TimeLockDelta,
|
|
|
|
}
|
2017-09-04 02:53:04 +03:00
|
|
|
cc.feeEstimator = lnwallet.StaticFeeEstimator{
|
|
|
|
FeeRate: 100,
|
|
|
|
}
|
2017-06-17 01:12:34 +03:00
|
|
|
default:
|
|
|
|
return nil, nil, fmt.Errorf("Default routing policy for "+
|
|
|
|
"chain %v is unknown", registeredChains.PrimaryChain())
|
|
|
|
}
|
|
|
|
|
2017-09-04 02:53:04 +03:00
|
|
|
walletConfig := &btcwallet.Config{
|
2017-10-20 05:53:19 +03:00
|
|
|
PrivatePass: privateWalletPw,
|
|
|
|
PublicPass: publicWalletPw,
|
2017-09-04 02:53:04 +03:00
|
|
|
DataDir: homeChainConfig.ChainDir,
|
|
|
|
NetParams: activeNetParams.Params,
|
|
|
|
FeeEstimator: cc.feeEstimator,
|
|
|
|
}
|
|
|
|
|
2017-05-18 21:49:32 +03:00
|
|
|
var (
|
2018-01-09 04:31:37 +03:00
|
|
|
err error
|
|
|
|
cleanUp func()
|
|
|
|
rawRPCConn *chain.RPCClient
|
2017-05-18 21:49:32 +03:00
|
|
|
)
|
|
|
|
|
2017-09-04 02:53:04 +03:00
|
|
|
// If spv mode is active, then we'll be using a distinct set of
|
2017-05-18 21:49:32 +03:00
|
|
|
// chainControl interfaces that interface directly with the p2p network
|
|
|
|
// of the selected chain.
|
2017-06-06 01:44:18 +03:00
|
|
|
if cfg.NeutrinoMode.Active {
|
2017-05-25 03:46:37 +03:00
|
|
|
// First we'll open the database file for neutrino, creating
|
|
|
|
// the database if needed.
|
2017-05-18 22:20:35 +03:00
|
|
|
dbName := filepath.Join(cfg.DataDir, "neutrino.db")
|
2017-05-18 21:49:32 +03:00
|
|
|
nodeDatabase, err := walletdb.Create("bdb", dbName)
|
|
|
|
if err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 03:46:37 +03:00
|
|
|
// With the database open, we can now create an instance of the
|
|
|
|
// neutrino light client. We pass in relevant configuration
|
|
|
|
// parameters required.
|
2017-05-18 21:49:32 +03:00
|
|
|
config := neutrino.Config{
|
2017-05-19 00:53:45 +03:00
|
|
|
DataDir: cfg.DataDir,
|
2017-05-18 21:49:32 +03:00
|
|
|
Database: nodeDatabase,
|
|
|
|
ChainParams: *activeNetParams.Params,
|
2017-06-06 01:44:18 +03:00
|
|
|
AddPeers: cfg.NeutrinoMode.AddPeers,
|
|
|
|
ConnectPeers: cfg.NeutrinoMode.ConnectPeers,
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
neutrino.WaitForMoreCFHeaders = time.Second * 1
|
|
|
|
neutrino.MaxPeers = 8
|
|
|
|
neutrino.BanDuration = 5 * time.Second
|
|
|
|
svc, err := neutrino.NewChainService(config)
|
|
|
|
if err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, fmt.Errorf("unable to create neutrino: %v", err)
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
svc.Start()
|
|
|
|
|
2017-05-25 03:46:37 +03:00
|
|
|
// Next we'll create the instances of the ChainNotifier and
|
|
|
|
// FilteredChainView interface which is backed by the neutrino
|
|
|
|
// light client.
|
|
|
|
cc.chainNotifier, err = neutrinonotify.New(svc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cc.chainView, err = chainview.NewCfFilteredChainView(svc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2017-05-18 21:49:32 +03:00
|
|
|
|
2017-05-25 03:46:37 +03:00
|
|
|
// Finally, we'll set the chain source for btcwallet, and
|
|
|
|
// create our clean up function which simply closes the
|
|
|
|
// database.
|
2017-06-05 07:06:52 +03:00
|
|
|
walletConfig.ChainSource = chain.NewNeutrinoClient(svc)
|
2017-05-25 03:46:37 +03:00
|
|
|
cleanUp = func() {
|
|
|
|
defer nodeDatabase.Close()
|
|
|
|
}
|
2017-05-18 21:49:32 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise, we'll be speaking directly via RPC to a node.
|
|
|
|
//
|
|
|
|
// So first we'll load btcd/ltcd's TLS cert for the RPC
|
|
|
|
// connection. If a raw cert was specified in the config, then
|
|
|
|
// we'll set that directly. Otherwise, we attempt to read the
|
|
|
|
// cert from the path specified in the config.
|
|
|
|
var rpcCert []byte
|
|
|
|
if homeChainConfig.RawRPCCert != "" {
|
|
|
|
rpcCert, err = hex.DecodeString(homeChainConfig.RawRPCCert)
|
|
|
|
if err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
certFile, err := os.Open(homeChainConfig.RPCCert)
|
|
|
|
if err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
rpcCert, err = ioutil.ReadAll(certFile)
|
|
|
|
if err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
if err := certFile.Close(); err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the specified host for the btcd/ltcd RPC server already
|
|
|
|
// has a port specified, then we use that directly. Otherwise,
|
|
|
|
// we assume the default port according to the selected chain
|
|
|
|
// parameters.
|
|
|
|
var btcdHost string
|
|
|
|
if strings.Contains(homeChainConfig.RPCHost, ":") {
|
|
|
|
btcdHost = homeChainConfig.RPCHost
|
|
|
|
} else {
|
|
|
|
btcdHost = fmt.Sprintf("%v:%v", homeChainConfig.RPCHost,
|
|
|
|
activeNetParams.rpcPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
btcdUser := homeChainConfig.RPCUser
|
|
|
|
btcdPass := homeChainConfig.RPCPass
|
2017-08-25 04:54:17 +03:00
|
|
|
rpcConfig := &rpcclient.ConnConfig{
|
2017-05-18 21:49:32 +03:00
|
|
|
Host: btcdHost,
|
|
|
|
Endpoint: "ws",
|
|
|
|
User: btcdUser,
|
|
|
|
Pass: btcdPass,
|
|
|
|
Certificates: rpcCert,
|
|
|
|
DisableTLS: false,
|
|
|
|
DisableConnectOnNew: true,
|
|
|
|
DisableAutoReconnect: false,
|
|
|
|
}
|
|
|
|
cc.chainNotifier, err = btcdnotify.New(rpcConfig)
|
|
|
|
if err != nil {
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, we'll create an instance of the default chain view to be
|
|
|
|
// used within the routing layer.
|
|
|
|
cc.chainView, err = chainview.NewBtcdFilteredChainView(*rpcConfig)
|
|
|
|
if err != nil {
|
|
|
|
srvrLog.Errorf("unable to create chain view: %v", err)
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
2017-05-25 03:46:37 +03:00
|
|
|
|
|
|
|
// Create a special websockets rpc client for btcd which will be used
|
|
|
|
// by the wallet for notifications, calls, etc.
|
2017-06-06 05:44:54 +03:00
|
|
|
chainRPC, err := chain.NewRPCClient(activeNetParams.Params, btcdHost,
|
2017-05-25 03:46:37 +03:00
|
|
|
btcdUser, btcdPass, rpcCert, false, 20)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-06 05:44:54 +03:00
|
|
|
walletConfig.ChainSource = chainRPC
|
2018-01-09 04:31:37 +03:00
|
|
|
rawRPCConn = chainRPC
|
2017-11-23 22:36:12 +03:00
|
|
|
|
2017-11-30 03:15:00 +03:00
|
|
|
// If we're not in simnet or regtest mode, then we'll attempt
|
|
|
|
// to use a proper fee estimator for testnet.
|
|
|
|
if !cfg.Bitcoin.SimNet && !cfg.Litecoin.SimNet &&
|
|
|
|
!cfg.Bitcoin.RegTest && !cfg.Litecoin.RegTest {
|
|
|
|
|
2017-11-23 22:36:12 +03:00
|
|
|
ltndLog.Infof("Initializing btcd backed fee estimator")
|
|
|
|
|
|
|
|
// Finally, we'll re-initialize the fee estimator, as
|
|
|
|
// if we're using btcd as a backend, then we can use
|
|
|
|
// live fee estimates, rather than a statically coded
|
|
|
|
// value.
|
|
|
|
fallBackFeeRate := btcutil.Amount(25)
|
|
|
|
cc.feeEstimator, err = lnwallet.NewBtcdFeeEstimator(
|
|
|
|
*rpcConfig, fallBackFeeRate,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := cc.feeEstimator.Start(); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wc, err := btcwallet.New(*walletConfig)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to create wallet controller: %v\n", err)
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cc.msgSigner = wc
|
|
|
|
cc.signer = wc
|
|
|
|
cc.chainIO = wc
|
|
|
|
|
|
|
|
// Create, and start the lnwallet, which handles the core payment
|
|
|
|
// channel logic, and exposes control via proxy state machines.
|
2017-07-31 02:01:44 +03:00
|
|
|
walletCfg := lnwallet.Config{
|
|
|
|
Database: chanDB,
|
|
|
|
Notifier: cc.chainNotifier,
|
|
|
|
WalletController: wc,
|
|
|
|
Signer: cc.signer,
|
|
|
|
FeeEstimator: cc.feeEstimator,
|
|
|
|
ChainIO: cc.chainIO,
|
|
|
|
DefaultConstraints: defaultChannelConstraints,
|
|
|
|
NetParams: *activeNetParams.Params,
|
|
|
|
}
|
|
|
|
wallet, err := lnwallet.NewLightningWallet(walletCfg)
|
2017-05-18 21:49:32 +03:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("unable to create wallet: %v\n", err)
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
if err := wallet.Startup(); err != nil {
|
|
|
|
fmt.Printf("unable to start wallet: %v\n", err)
|
2017-05-25 03:46:37 +03:00
|
|
|
return nil, nil, err
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ltndLog.Info("LightningWallet opened")
|
|
|
|
|
|
|
|
cc.wallet = wallet
|
|
|
|
|
2018-01-09 04:31:37 +03:00
|
|
|
// As a final check, if we're using the RPC backend, we'll ensure that
|
|
|
|
// the btcd node has the txindex set. Atm, this is required in order to
|
|
|
|
// properly perform historical confirmation+spend dispatches.
|
|
|
|
if !cfg.NeutrinoMode.Active {
|
|
|
|
// In order to check to see if we have the txindex up to date
|
|
|
|
// and active, we'll try to fetch the first transaction in the
|
|
|
|
// latest block via the index. If this doesn't succeed, then we
|
|
|
|
// know it isn't active (or just not yet up to date).
|
|
|
|
bestHash, _, err := cc.chainIO.GetBestBlock()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to get current "+
|
|
|
|
"best hash: %v", err)
|
|
|
|
}
|
|
|
|
bestBlock, err := cc.chainIO.GetBlock(bestHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("unable to get current "+
|
|
|
|
"block hash: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
firstTxHash := bestBlock.Transactions[0].TxHash()
|
|
|
|
_, err = rawRPCConn.GetRawTransaction(&firstTxHash)
|
|
|
|
if err != nil {
|
|
|
|
// If the node doesn't have the txindex set, then we'll
|
|
|
|
// halt startup, as we can't proceed in this state.
|
|
|
|
return nil, nil, fmt.Errorf("btcd detected to not " +
|
|
|
|
"have --txindex active, cannot proceed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 03:46:37 +03:00
|
|
|
return cc, cleanUp, nil
|
2017-05-18 21:49:32 +03:00
|
|
|
}
|
|
|
|
|
2017-05-03 05:16:37 +03:00
|
|
|
var (
|
|
|
|
// bitcoinGenesis is the genesis hash of Bitcoin's testnet chain.
|
|
|
|
bitcoinGenesis = chainhash.Hash([chainhash.HashSize]byte{
|
2017-12-03 05:04:03 +03:00
|
|
|
0x43, 0x49, 0x7f, 0xd7, 0xf8, 0x26, 0x95, 0x71,
|
|
|
|
0x08, 0xf4, 0xa3, 0x0f, 0xd9, 0xce, 0xc3, 0xae,
|
|
|
|
0xba, 0x79, 0x97, 0x20, 0x84, 0xe9, 0x0e, 0xad,
|
|
|
|
0x01, 0xea, 0x33, 0x09, 0x00, 0x00, 0x00, 0x00,
|
2017-05-03 05:16:37 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// litecoinGenesis is the genesis hash of Litecoin's testnet4 chain.
|
|
|
|
litecoinGenesis = chainhash.Hash([chainhash.HashSize]byte{
|
|
|
|
0xa0, 0x29, 0x3e, 0x4e, 0xeb, 0x3d, 0xa6, 0xe6,
|
|
|
|
0xf5, 0x6f, 0x81, 0xed, 0x59, 0x5f, 0x57, 0x88,
|
|
|
|
0x0d, 0x1a, 0x21, 0x56, 0x9e, 0x13, 0xee, 0xfd,
|
|
|
|
0xd9, 0x51, 0x28, 0x4b, 0x5a, 0x62, 0x66, 0x49,
|
|
|
|
})
|
|
|
|
|
|
|
|
// chainMap is a simple index that maps a chain's genesis hash to the
|
|
|
|
// chainCode enum for that chain.
|
|
|
|
chainMap = map[chainhash.Hash]chainCode{
|
|
|
|
bitcoinGenesis: bitcoinChain,
|
|
|
|
litecoinGenesis: litecoinChain,
|
|
|
|
}
|
|
|
|
|
|
|
|
// reverseChainMap is the inverse of the chainMap above: it maps the
|
|
|
|
// chain enum for a chain to its genesis hash.
|
|
|
|
reverseChainMap = map[chainCode]chainhash.Hash{
|
|
|
|
bitcoinChain: bitcoinGenesis,
|
|
|
|
litecoinChain: litecoinGenesis,
|
|
|
|
}
|
2017-09-04 02:53:28 +03:00
|
|
|
|
|
|
|
// chainDNSSeeds is a map of a chain's hash to the set of DNS seeds
|
|
|
|
// that will be use to bootstrap peers upon first startup.
|
|
|
|
//
|
2017-10-31 05:02:48 +03:00
|
|
|
// The first item in the array is the primary host we'll use to attempt
|
|
|
|
// the SRV lookup we require. If we're unable to receive a response
|
|
|
|
// over UDP, then we'll fall back to manual TCP resolution. The second
|
|
|
|
// item in the array is a special A record that we'll query in order to
|
|
|
|
// receive the IP address of the current authoritative DNS server for
|
|
|
|
// the network seed.
|
|
|
|
//
|
2017-09-04 02:53:28 +03:00
|
|
|
// TODO(roasbeef): extend and collapse these and chainparams.go into
|
|
|
|
// struct like chaincfg.Params
|
2017-10-31 05:02:48 +03:00
|
|
|
chainDNSSeeds = map[chainhash.Hash][][2]string{
|
2017-09-04 03:04:53 +03:00
|
|
|
bitcoinGenesis: {
|
2017-10-31 05:02:48 +03:00
|
|
|
{
|
|
|
|
"nodes.lightning.directory",
|
|
|
|
"soa.nodes.lightning.directory",
|
|
|
|
},
|
2017-09-04 02:53:28 +03:00
|
|
|
},
|
|
|
|
}
|
2017-05-03 05:16:37 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// chainRegistry keeps track of the current chains
|
|
|
|
type chainRegistry struct {
|
|
|
|
sync.RWMutex
|
|
|
|
|
|
|
|
activeChains map[chainCode]*chainControl
|
|
|
|
netParams map[chainCode]*bitcoinNetParams
|
|
|
|
|
|
|
|
primaryChain chainCode
|
|
|
|
}
|
|
|
|
|
|
|
|
// newChainRegistry creates a new chainRegistry.
|
|
|
|
func newChainRegistry() *chainRegistry {
|
|
|
|
return &chainRegistry{
|
|
|
|
activeChains: make(map[chainCode]*chainControl),
|
|
|
|
netParams: make(map[chainCode]*bitcoinNetParams),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterChain assigns an active chainControl instance to a target chain
|
|
|
|
// identified by its chainCode.
|
|
|
|
func (c *chainRegistry) RegisterChain(newChain chainCode, cc *chainControl) {
|
|
|
|
c.Lock()
|
|
|
|
c.activeChains[newChain] = cc
|
|
|
|
c.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupChain attempts to lookup an active chainControl instance for the
|
|
|
|
// target chain.
|
|
|
|
func (c *chainRegistry) LookupChain(targetChain chainCode) (*chainControl, bool) {
|
|
|
|
c.RLock()
|
|
|
|
cc, ok := c.activeChains[targetChain]
|
|
|
|
c.RUnlock()
|
|
|
|
return cc, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupChainByHash attempts to look up an active chainControl which
|
|
|
|
// corresponds to the passed genesis hash.
|
|
|
|
func (c *chainRegistry) LookupChainByHash(chainHash chainhash.Hash) (*chainControl, bool) {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
targetChain, ok := chainMap[chainHash]
|
|
|
|
if !ok {
|
|
|
|
return nil, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
cc, ok := c.activeChains[targetChain]
|
|
|
|
return cc, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterPrimaryChain sets a target chain as the "home chain" for lnd.
|
|
|
|
func (c *chainRegistry) RegisterPrimaryChain(cc chainCode) {
|
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
|
|
|
|
|
|
|
c.primaryChain = cc
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrimaryChain returns the primary chain for this running lnd instance. The
|
|
|
|
// primary chain is considered the "home base" while the other registered
|
|
|
|
// chains are treated as secondary chains.
|
|
|
|
func (c *chainRegistry) PrimaryChain() chainCode {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
return c.primaryChain
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActiveChains returns the total number of active chains.
|
|
|
|
func (c *chainRegistry) ActiveChains() []chainCode {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
chains := make([]chainCode, 0, len(c.activeChains))
|
|
|
|
for activeChain := range c.activeChains {
|
|
|
|
chains = append(chains, activeChain)
|
|
|
|
}
|
|
|
|
|
|
|
|
return chains
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumActiveChains returns the total number of active chains.
|
|
|
|
func (c *chainRegistry) NumActiveChains() uint32 {
|
|
|
|
c.RLock()
|
|
|
|
defer c.RUnlock()
|
|
|
|
|
|
|
|
return uint32(len(c.activeChains))
|
|
|
|
}
|