mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-29 10:09:08 +02:00
lnd: removing activeNetParams global, passed around instead via configs
This commit removes the activeNetParams global in chainparams.go. This is necessary to isolate code from the lnd package so we can import it for use in tests, other projects, etc.
This commit is contained in:
64
rpcserver.go
64
rpcserver.go
@ -556,7 +556,7 @@ func newRPCServer(cfg *Config, s *server, macService *macaroons.Service,
|
||||
},
|
||||
FindRoute: s.chanRouter.FindRoute,
|
||||
MissionControl: s.missionControl,
|
||||
ActiveNetParams: activeNetParams.Params,
|
||||
ActiveNetParams: cfg.ActiveNetParams.Params,
|
||||
Tower: s.controlTower,
|
||||
MaxTotalTimelock: cfg.MaxOutgoingCltvExpiry,
|
||||
DefaultFinalCltvDelta: uint16(cfg.Bitcoin.TimeLockDelta),
|
||||
@ -580,7 +580,7 @@ func newRPCServer(cfg *Config, s *server, macService *macaroons.Service,
|
||||
// TODO(roasbeef): extend sub-sever config to have both (local vs remote) DB
|
||||
err = subServerCgs.PopulateDependencies(
|
||||
cfg, s.cc, cfg.networkDir, macService, atpl, invoiceRegistry,
|
||||
s.htlcSwitch, activeNetParams.Params, s.chanRouter,
|
||||
s.htlcSwitch, cfg.ActiveNetParams.Params, s.chanRouter,
|
||||
routerBackend, s.nodeSigner, s.remoteChanDB, s.sweeper, tower,
|
||||
s.towerClient, cfg.net.ResolveTCPAddr, genInvoiceFeatures,
|
||||
rpcsLog,
|
||||
@ -918,10 +918,12 @@ func (r *rpcServer) Stop() error {
|
||||
// the outputs themselves. The passed map pairs up an address, to a desired
|
||||
// output value amount. Each address is converted to its corresponding pkScript
|
||||
// to be used within the constructed output(s).
|
||||
func addrPairsToOutputs(addrPairs map[string]int64) ([]*wire.TxOut, error) {
|
||||
func addrPairsToOutputs(addrPairs map[string]int64,
|
||||
params *chaincfg.Params) ([]*wire.TxOut, error) {
|
||||
|
||||
outputs := make([]*wire.TxOut, 0, len(addrPairs))
|
||||
for addr, amt := range addrPairs {
|
||||
addr, err := btcutil.DecodeAddress(addr, activeNetParams.Params)
|
||||
addr, err := btcutil.DecodeAddress(addr, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -989,7 +991,7 @@ func allowCORS(handler http.Handler, origins []string) http.Handler {
|
||||
func (r *rpcServer) sendCoinsOnChain(paymentMap map[string]int64,
|
||||
feeRate chainfee.SatPerKWeight, label string) (*chainhash.Hash, error) {
|
||||
|
||||
outputs, err := addrPairsToOutputs(paymentMap)
|
||||
outputs, err := addrPairsToOutputs(paymentMap, r.cfg.ActiveNetParams.Params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -1036,7 +1038,7 @@ func (r *rpcServer) ListUnspent(ctx context.Context,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rpcUtxos, err := lnrpc.MarshalUtxos(utxos, activeNetParams.Params)
|
||||
rpcUtxos, err := lnrpc.MarshalUtxos(utxos, r.cfg.ActiveNetParams.Params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -1060,7 +1062,7 @@ func (r *rpcServer) EstimateFee(ctx context.Context,
|
||||
in *lnrpc.EstimateFeeRequest) (*lnrpc.EstimateFeeResponse, error) {
|
||||
|
||||
// Create the list of outputs we are spending to.
|
||||
outputs, err := addrPairsToOutputs(in.AddrToAmount)
|
||||
outputs, err := addrPairsToOutputs(in.AddrToAmount, r.cfg.ActiveNetParams.Params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -1131,16 +1133,18 @@ func (r *rpcServer) SendCoins(ctx context.Context,
|
||||
|
||||
// Decode the address receiving the coins, we need to check whether the
|
||||
// address is valid for this network.
|
||||
targetAddr, err := btcutil.DecodeAddress(in.Addr, activeNetParams.Params)
|
||||
targetAddr, err := btcutil.DecodeAddress(
|
||||
in.Addr, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Make the check on the decoded address according to the active network.
|
||||
if !targetAddr.IsForNet(activeNetParams.Params) {
|
||||
if !targetAddr.IsForNet(r.cfg.ActiveNetParams.Params) {
|
||||
return nil, fmt.Errorf("address: %v is not valid for this "+
|
||||
"network: %v", targetAddr.String(),
|
||||
activeNetParams.Params.Name)
|
||||
r.cfg.ActiveNetParams.Params.Name)
|
||||
}
|
||||
|
||||
// If the destination address parses to a valid pubkey, we assume the user
|
||||
@ -1449,7 +1453,7 @@ func (r *rpcServer) ConnectPeer(ctx context.Context,
|
||||
peerAddr := &lnwire.NetAddress{
|
||||
IdentityKey: pubKey,
|
||||
Address: addr,
|
||||
ChainNet: activeNetParams.Net,
|
||||
ChainNet: r.cfg.ActiveNetParams.Net,
|
||||
}
|
||||
|
||||
rpcsLog.Debugf("[connectpeer] requested connection to %x@%s",
|
||||
@ -1812,7 +1816,9 @@ func (r *rpcServer) parseOpenChannelReq(in *lnrpc.OpenChannelRequest,
|
||||
rpcsLog.Debugf("[openchannel]: using fee of %v sat/kw for funding tx",
|
||||
int64(feeRate))
|
||||
|
||||
script, err := parseUpfrontShutdownAddress(in.CloseAddress)
|
||||
script, err := parseUpfrontShutdownAddress(
|
||||
in.CloseAddress, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing upfront shutdown: %v",
|
||||
err)
|
||||
@ -1823,7 +1829,7 @@ func (r *rpcServer) parseOpenChannelReq(in *lnrpc.OpenChannelRequest,
|
||||
// be used to consume updates of the state of the pending channel.
|
||||
return &openChanReq{
|
||||
targetPubkey: nodePubKey,
|
||||
chainHash: *activeNetParams.GenesisHash,
|
||||
chainHash: *r.cfg.ActiveNetParams.GenesisHash,
|
||||
localFundingAmt: localFundingAmt,
|
||||
pushAmt: lnwire.NewMSatFromSatoshis(remoteInitialBalance),
|
||||
minHtlcIn: minHtlcIn,
|
||||
@ -1986,13 +1992,15 @@ func (r *rpcServer) OpenChannelSync(ctx context.Context,
|
||||
// parseUpfrontShutdownScript attempts to parse an upfront shutdown address.
|
||||
// If the address is empty, it returns nil. If it successfully decoded the
|
||||
// address, it returns a script that pays out to the address.
|
||||
func parseUpfrontShutdownAddress(address string) (lnwire.DeliveryAddress, error) {
|
||||
func parseUpfrontShutdownAddress(address string,
|
||||
params *chaincfg.Params) (lnwire.DeliveryAddress, error) {
|
||||
|
||||
if len(address) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
addr, err := btcutil.DecodeAddress(
|
||||
address, activeNetParams.Params,
|
||||
address, params,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid address: %v", err)
|
||||
@ -2196,7 +2204,7 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
|
||||
if len(in.DeliveryAddress) > 0 {
|
||||
// Decode the address provided.
|
||||
addr, err := btcutil.DecodeAddress(
|
||||
in.DeliveryAddress, activeNetParams.Params,
|
||||
in.DeliveryAddress, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid delivery address: %v", err)
|
||||
@ -2437,7 +2445,7 @@ func (r *rpcServer) GetInfo(ctx context.Context,
|
||||
"with current best block in the main chain: %v", err)
|
||||
}
|
||||
|
||||
network := normalizeNetwork(activeNetParams.Name)
|
||||
network := normalizeNetwork(r.cfg.ActiveNetParams.Name)
|
||||
activeChains := make([]*lnrpc.Chain, r.cfg.registeredChains.NumActiveChains())
|
||||
for i, chain := range r.cfg.registeredChains.ActiveChains() {
|
||||
activeChains[i] = &lnrpc.Chain{
|
||||
@ -2488,7 +2496,7 @@ func (r *rpcServer) GetInfo(ctx context.Context,
|
||||
BlockHeight: uint32(bestHeight),
|
||||
BlockHash: bestHash.String(),
|
||||
SyncedToChain: isSynced,
|
||||
Testnet: isTestnet(&activeNetParams),
|
||||
Testnet: isTestnet(&r.cfg.ActiveNetParams),
|
||||
Chains: activeChains,
|
||||
Uris: uris,
|
||||
Alias: nodeAnn.Alias.String(),
|
||||
@ -3432,7 +3440,7 @@ func createRPCOpenChannel(r *rpcServer, graph *channeldb.ChannelGraph,
|
||||
|
||||
if len(dbChannel.LocalShutdownScript) > 0 {
|
||||
_, addresses, _, err := txscript.ExtractPkScriptAddrs(
|
||||
dbChannel.LocalShutdownScript, activeNetParams.Params,
|
||||
dbChannel.LocalShutdownScript, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -3552,8 +3560,7 @@ func (r *rpcServer) createRPCClosedChannel(
|
||||
}
|
||||
|
||||
reports, err := r.server.remoteChanDB.FetchChannelReports(
|
||||
|
||||
*activeNetParams.GenesisHash, &dbChannel.ChanPoint,
|
||||
*r.cfg.ActiveNetParams.GenesisHash, &dbChannel.ChanPoint,
|
||||
)
|
||||
switch err {
|
||||
// If the channel does not have its resolver outcomes stored,
|
||||
@ -4009,7 +4016,7 @@ func (r *rpcServer) extractPaymentIntent(rpcPayReq *rpcPaymentRequest) (rpcPayme
|
||||
// attempt to decode it, populating the payment accordingly.
|
||||
if rpcPayReq.PaymentRequest != "" {
|
||||
payReq, err := zpay32.Decode(
|
||||
rpcPayReq.PaymentRequest, activeNetParams.Params,
|
||||
rpcPayReq.PaymentRequest, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return payIntent, err
|
||||
@ -4526,7 +4533,7 @@ func (r *rpcServer) AddInvoice(ctx context.Context,
|
||||
addInvoiceCfg := &invoicesrpc.AddInvoiceConfig{
|
||||
AddInvoice: r.server.invoices.AddInvoice,
|
||||
IsChannelActive: r.server.htlcSwitch.HasActiveLink,
|
||||
ChainParams: activeNetParams.Params,
|
||||
ChainParams: r.cfg.ActiveNetParams.Params,
|
||||
NodeSigner: r.server.nodeSigner,
|
||||
DefaultCLTVExpiry: defaultDelta,
|
||||
ChanDB: r.server.remoteChanDB,
|
||||
@ -4615,7 +4622,7 @@ func (r *rpcServer) LookupInvoice(ctx context.Context,
|
||||
}))
|
||||
|
||||
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
|
||||
&invoice, activeNetParams.Params,
|
||||
&invoice, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -4656,8 +4663,9 @@ func (r *rpcServer) ListInvoices(ctx context.Context,
|
||||
LastIndexOffset: invoiceSlice.LastIndexOffset,
|
||||
}
|
||||
for i, invoice := range invoiceSlice.Invoices {
|
||||
invoice := invoice
|
||||
resp.Invoices[i], err = invoicesrpc.CreateRPCInvoice(
|
||||
&invoice, activeNetParams.Params,
|
||||
&invoice, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -4684,7 +4692,7 @@ func (r *rpcServer) SubscribeInvoices(req *lnrpc.InvoiceSubscription,
|
||||
select {
|
||||
case newInvoice := <-invoiceClient.NewInvoices:
|
||||
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
|
||||
newInvoice, activeNetParams.Params,
|
||||
newInvoice, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -4696,7 +4704,7 @@ func (r *rpcServer) SubscribeInvoices(req *lnrpc.InvoiceSubscription,
|
||||
|
||||
case settledInvoice := <-invoiceClient.SettledInvoices:
|
||||
rpcInvoice, err := invoicesrpc.CreateRPCInvoice(
|
||||
settledInvoice, activeNetParams.Params,
|
||||
settledInvoice, r.cfg.ActiveNetParams.Params,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -5463,7 +5471,7 @@ func (r *rpcServer) DecodePayReq(ctx context.Context,
|
||||
// Fist we'll attempt to decode the payment request string, if the
|
||||
// request is invalid or the checksum doesn't match, then we'll exit
|
||||
// here with an error.
|
||||
payReq, err := zpay32.Decode(req.PayReq, activeNetParams.Params)
|
||||
payReq, err := zpay32.Decode(req.PayReq, r.cfg.ActiveNetParams.Params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user