From 3b2188d6898075a6272ff27149a0429f190ea218 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Wed, 13 May 2020 15:47:45 +0200 Subject: [PATCH] multi: move exported items to lncfg --- config.go | 59 +----------- fundingmanager_test.go | 3 +- lncfg/config.go | 89 +++++++++++++++++++ ...d_multi-hop_htlc_local_chain_claim_test.go | 3 +- .../lnd_multi-hop_htlc_local_timeout_test.go | 3 +- ...ulti-hop_htlc_receiver_chain_claim_test.go | 3 +- ..._multi-hop_htlc_remote_chain_claim_test.go | 3 +- lntest/itest/lnd_test.go | 3 +- server.go | 8 +- 9 files changed, 108 insertions(+), 66 deletions(-) create mode 100644 lncfg/config.go diff --git a/config.go b/config.go index 6134c981..f2f9de88 100644 --- a/config.go +++ b/config.go @@ -35,10 +35,6 @@ import ( ) const ( - // DefaultConfigFilename is the default configuration file name lnd - // tries to load. - DefaultConfigFilename = "lnd.conf" - defaultDataDirname = "data" defaultChainSubDirname = "chain" defaultGraphSubDirname = "graph" @@ -56,10 +52,6 @@ const ( defaultPeerPort = 9735 defaultRPCHost = "localhost" - // DefaultMaxPendingChannels is the default maximum number of incoming - // pending channels permitted per peer. - DefaultMaxPendingChannels = 1 - defaultNoSeedBackup = false defaultPaymentsExpirationGracePeriod = time.Duration(0) defaultTrickleDelay = 90 * 1000 @@ -78,51 +70,6 @@ const ( defaultTorV2PrivateKeyFilename = "v2_onion_private_key" defaultTorV3PrivateKeyFilename = "v3_onion_private_key" - // DefaultIncomingBroadcastDelta defines the number of blocks before the - // expiry of an incoming htlc at which we force close the channel. We - // only go to chain if we also have the preimage to actually pull in the - // htlc. BOLT #2 suggests 7 blocks. We use a few more for extra safety. - // Within this window we need to get our sweep or 2nd level success tx - // confirmed, because after that the remote party is also able to claim - // the htlc using the timeout path. - DefaultIncomingBroadcastDelta = 10 - - // defaultFinalCltvRejectDelta defines the number of blocks before the - // expiry of an incoming exit hop htlc at which we cancel it back - // immediately. It is an extra safety measure over the final cltv - // requirement as it is defined in the invoice. It ensures that we - // cancel back htlcs that, when held on to, may cause us to force close - // the channel because we enter the incoming broadcast window. Bolt #11 - // suggests 9 blocks here. We use a few more for additional safety. - // - // There is still a small gap that remains between receiving the - // RevokeAndAck and canceling back. If a new block arrives within that - // window, we may still force close the channel. There is currently no - // way to reject an UpdateAddHtlc of which we already know that it will - // push us in the broadcast window. - defaultFinalCltvRejectDelta = DefaultIncomingBroadcastDelta + 3 - - // DefaultOutgoingBroadcastDelta defines the number of blocks before the - // expiry of an outgoing htlc at which we force close the channel. We - // are not in a hurry to force close, because there is nothing to claim - // for us. We do need to time the htlc out, because there may be an - // incoming htlc that will time out too (albeit later). Bolt #2 suggests - // a value of -1 here, but we allow one block less to prevent potential - // confusion around the negative value. It means we force close the - // channel at exactly the htlc expiry height. - DefaultOutgoingBroadcastDelta = 0 - - // defaultOutgoingCltvRejectDelta defines the number of blocks before - // the expiry of an outgoing htlc at which we don't want to offer it to - // the next peer anymore. If that happens, we cancel back the incoming - // htlc. This is to prevent the situation where we have an outstanding - // htlc that brings or will soon bring us inside the outgoing broadcast - // window and trigger us to force close the channel. Bolt #2 suggests a - // value of 0. We pad it a bit, to prevent a slow round trip to the next - // peer and a block arriving during that round trip to trigger force - // closure. - defaultOutgoingCltvRejectDelta = DefaultOutgoingBroadcastDelta + 3 - // minTimeLockDelta is the minimum timelock we require for incoming // HTLCs on our channels. minTimeLockDelta = 4 @@ -146,7 +93,7 @@ var ( // DefaultConfigFile is the default full path of lnd's configuration // file. - DefaultConfigFile = filepath.Join(DefaultLndDir, DefaultConfigFilename) + DefaultConfigFile = filepath.Join(DefaultLndDir, lncfg.DefaultConfigFilename) defaultDataDir = filepath.Join(DefaultLndDir, defaultDataDirname) defaultLogDir = filepath.Join(DefaultLndDir, defaultLogDirname) @@ -352,7 +299,7 @@ func DefaultConfig() Config { EstimateMode: defaultBitcoindEstimateMode, }, UnsafeDisconnect: true, - MaxPendingChannels: DefaultMaxPendingChannels, + MaxPendingChannels: lncfg.DefaultMaxPendingChannels, NoSeedBackup: defaultNoSeedBackup, MinBackoff: defaultMinBackoff, MaxBackoff: defaultMaxBackoff, @@ -440,7 +387,7 @@ func LoadConfig() (*Config, error) { if configFileDir != DefaultLndDir { if configFilePath == DefaultConfigFile { configFilePath = filepath.Join( - configFileDir, DefaultConfigFilename, + configFileDir, lncfg.DefaultConfigFilename, ) } } diff --git a/fundingmanager_test.go b/fundingmanager_test.go index 45a9bdee..db870d70 100644 --- a/fundingmanager_test.go +++ b/fundingmanager_test.go @@ -21,6 +21,7 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/chanacceptor" @@ -413,7 +414,7 @@ func createTestFundingManager(t *testing.T, privKey *btcec.PrivateKey, }, ZombieSweeperInterval: 1 * time.Hour, ReservationTimeout: 1 * time.Nanosecond, - MaxPendingChannels: DefaultMaxPendingChannels, + MaxPendingChannels: lncfg.DefaultMaxPendingChannels, NotifyOpenChannelEvent: evt.NotifyOpenChannelEvent, OpenChannelPredicate: chainedAcceptor, NotifyPendingOpenChannelEvent: evt.NotifyPendingOpenChannelEvent, diff --git a/lncfg/config.go b/lncfg/config.go new file mode 100644 index 00000000..f0a1905a --- /dev/null +++ b/lncfg/config.go @@ -0,0 +1,89 @@ +package lncfg + +import ( + "os" + "os/user" + "path/filepath" + "strings" +) + +const ( + // DefaultConfigFilename is the default configuration file name lnd + // tries to load. + DefaultConfigFilename = "lnd.conf" + + // DefaultMaxPendingChannels is the default maximum number of incoming + // pending channels permitted per peer. + DefaultMaxPendingChannels = 1 + + // DefaultIncomingBroadcastDelta defines the number of blocks before the + // expiry of an incoming htlc at which we force close the channel. We + // only go to chain if we also have the preimage to actually pull in the + // htlc. BOLT #2 suggests 7 blocks. We use a few more for extra safety. + // Within this window we need to get our sweep or 2nd level success tx + // confirmed, because after that the remote party is also able to claim + // the htlc using the timeout path. + DefaultIncomingBroadcastDelta = 10 + + // DefaultFinalCltvRejectDelta defines the number of blocks before the + // expiry of an incoming exit hop htlc at which we cancel it back + // immediately. It is an extra safety measure over the final cltv + // requirement as it is defined in the invoice. It ensures that we + // cancel back htlcs that, when held on to, may cause us to force close + // the channel because we enter the incoming broadcast window. Bolt #11 + // suggests 9 blocks here. We use a few more for additional safety. + // + // There is still a small gap that remains between receiving the + // RevokeAndAck and canceling back. If a new block arrives within that + // window, we may still force close the channel. There is currently no + // way to reject an UpdateAddHtlc of which we already know that it will + // push us in the broadcast window. + DefaultFinalCltvRejectDelta = DefaultIncomingBroadcastDelta + 3 + + // DefaultOutgoingBroadcastDelta defines the number of blocks before the + // expiry of an outgoing htlc at which we force close the channel. We + // are not in a hurry to force close, because there is nothing to claim + // for us. We do need to time the htlc out, because there may be an + // incoming htlc that will time out too (albeit later). Bolt #2 suggests + // a value of -1 here, but we allow one block less to prevent potential + // confusion around the negative value. It means we force close the + // channel at exactly the htlc expiry height. + DefaultOutgoingBroadcastDelta = 0 + + // DefaultOutgoingCltvRejectDelta defines the number of blocks before + // the expiry of an outgoing htlc at which we don't want to offer it to + // the next peer anymore. If that happens, we cancel back the incoming + // htlc. This is to prevent the situation where we have an outstanding + // htlc that brings or will soon bring us inside the outgoing broadcast + // window and trigger us to force close the channel. Bolt #2 suggests a + // value of 0. We pad it a bit, to prevent a slow round trip to the next + // peer and a block arriving during that round trip to trigger force + // closure. + DefaultOutgoingCltvRejectDelta = DefaultOutgoingBroadcastDelta + 3 +) + +// CleanAndExpandPath expands environment variables and leading ~ in the +// passed path, cleans the result, and returns it. +// This function is taken from https://github.com/btcsuite/btcd +func CleanAndExpandPath(path string) string { + if path == "" { + return "" + } + + // Expand initial ~ to OS specific home directory. + if strings.HasPrefix(path, "~") { + var homeDir string + u, err := user.Current() + if err == nil { + homeDir = u.HomeDir + } else { + homeDir = os.Getenv("HOME") + } + + path = strings.Replace(path, "~", homeDir, 1) + } + + // NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%, + // but the variables can still be expanded via POSIX-style $VARIABLE. + return filepath.Clean(os.ExpandEnv(path)) +} diff --git a/lntest/itest/lnd_multi-hop_htlc_local_chain_claim_test.go b/lntest/itest/lnd_multi-hop_htlc_local_chain_claim_test.go index 16ac3987..f63ef57d 100644 --- a/lntest/itest/lnd_multi-hop_htlc_local_chain_claim_test.go +++ b/lntest/itest/lnd_multi-hop_htlc_local_chain_claim_test.go @@ -10,6 +10,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lntest" @@ -136,7 +137,7 @@ func testMultiHopHtlcLocalChainClaim(net *lntest.NetworkHarness, t *harnessTest, // We'll now mine enough blocks so Carol decides that she needs to go // on-chain to claim the HTLC as Bob has been inactive. numBlocks := padCLTV(uint32(invoiceReq.CltvExpiry - - lnd.DefaultIncomingBroadcastDelta)) + lncfg.DefaultIncomingBroadcastDelta)) if _, err := net.Miner.Node.Generate(numBlocks); err != nil { t.Fatalf("unable to generate blocks") diff --git a/lntest/itest/lnd_multi-hop_htlc_local_timeout_test.go b/lntest/itest/lnd_multi-hop_htlc_local_timeout_test.go index 1a7feed4..d751bca1 100644 --- a/lntest/itest/lnd_multi-hop_htlc_local_timeout_test.go +++ b/lntest/itest/lnd_multi-hop_htlc_local_timeout_test.go @@ -12,6 +12,7 @@ import ( "github.com/btcsuite/btcutil" "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lntest" "github.com/lightningnetwork/lnd/lntest/wait" @@ -101,7 +102,7 @@ func testMultiHopHtlcLocalTimeout(net *lntest.NetworkHarness, t *harnessTest, // timeout. With the default outgoing broadcast delta of zero, this will // be the same height as the htlc expiry height. numBlocks := padCLTV( - uint32(finalCltvDelta - lnd.DefaultOutgoingBroadcastDelta), + uint32(finalCltvDelta - lncfg.DefaultOutgoingBroadcastDelta), ) if _, err := net.Miner.Node.Generate(numBlocks); err != nil { t.Fatalf("unable to generate blocks: %v", err) diff --git a/lntest/itest/lnd_multi-hop_htlc_receiver_chain_claim_test.go b/lntest/itest/lnd_multi-hop_htlc_receiver_chain_claim_test.go index d7335311..8669383f 100644 --- a/lntest/itest/lnd_multi-hop_htlc_receiver_chain_claim_test.go +++ b/lntest/itest/lnd_multi-hop_htlc_receiver_chain_claim_test.go @@ -10,6 +10,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lntest" @@ -118,7 +119,7 @@ func testMultiHopReceiverChainClaim(net *lntest.NetworkHarness, t *harnessTest, // chain in order to sweep her HTLC since the value is high enough. // TODO(roasbeef): modify once go to chain policy changes numBlocks := padCLTV(uint32( - invoiceReq.CltvExpiry - lnd.DefaultIncomingBroadcastDelta, + invoiceReq.CltvExpiry - lncfg.DefaultIncomingBroadcastDelta, )) if _, err := net.Miner.Node.Generate(numBlocks); err != nil { t.Fatalf("unable to generate blocks") diff --git a/lntest/itest/lnd_multi-hop_htlc_remote_chain_claim_test.go b/lntest/itest/lnd_multi-hop_htlc_remote_chain_claim_test.go index c73a066b..e4f61b19 100644 --- a/lntest/itest/lnd_multi-hop_htlc_remote_chain_claim_test.go +++ b/lntest/itest/lnd_multi-hop_htlc_remote_chain_claim_test.go @@ -10,6 +10,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lntest" @@ -148,7 +149,7 @@ func testMultiHopHtlcRemoteChainClaim(net *lntest.NetworkHarness, t *harnessTest // We'll now mine enough blocks so Carol decides that she needs to go // on-chain to claim the HTLC as Bob has been inactive. numBlocks := padCLTV(uint32(invoiceReq.CltvExpiry- - lnd.DefaultIncomingBroadcastDelta) - defaultCSV) + lncfg.DefaultIncomingBroadcastDelta) - defaultCSV) if _, err := net.Miner.Node.Generate(numBlocks); err != nil { t.Fatalf("unable to generate blocks") diff --git a/lntest/itest/lnd_test.go b/lntest/itest/lnd_test.go index 12a31881..ef7d0a32 100644 --- a/lntest/itest/lnd_test.go +++ b/lntest/itest/lnd_test.go @@ -34,6 +34,7 @@ import ( "github.com/lightningnetwork/lnd/chanbackup" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/input" + "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" "github.com/lightningnetwork/lnd/lnrpc/routerrpc" @@ -6695,7 +6696,7 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe func testMaxPendingChannels(net *lntest.NetworkHarness, t *harnessTest) { ctxb := context.Background() - maxPendingChannels := lnd.DefaultMaxPendingChannels + 1 + maxPendingChannels := lncfg.DefaultMaxPendingChannels + 1 amount := lnd.MaxBtcFundingAmount // Create a new node (Carol) with greater number of max pending diff --git a/server.go b/server.go index 27086b1e..6c4ab2b1 100644 --- a/server.go +++ b/server.go @@ -399,7 +399,7 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, } registryConfig := invoices.RegistryConfig{ - FinalCltvRejectDelta: defaultFinalCltvRejectDelta, + FinalCltvRejectDelta: lncfg.DefaultFinalCltvRejectDelta, HtlcHoldDuration: invoices.DefaultHtlcHoldDuration, Clock: clock.NewDefaultClock(), AcceptKeySend: cfg.AcceptKeySend, @@ -867,8 +867,8 @@ func newServer(listenAddrs []net.Addr, chanDB *channeldb.DB, s.chainArb = contractcourt.NewChainArbitrator(contractcourt.ChainArbitratorConfig{ ChainHash: *activeNetParams.GenesisHash, - IncomingBroadcastDelta: DefaultIncomingBroadcastDelta, - OutgoingBroadcastDelta: DefaultOutgoingBroadcastDelta, + IncomingBroadcastDelta: lncfg.DefaultIncomingBroadcastDelta, + OutgoingBroadcastDelta: lncfg.DefaultOutgoingBroadcastDelta, NewSweepAddr: newSweepPkScriptGen(cc.wallet), PublishTx: cc.wallet.PublishTransaction, DeliverResolutionMsg: func(msgs ...contractcourt.ResolutionMsg) error { @@ -2782,7 +2782,7 @@ func (s *server) peerConnected(conn net.Conn, connReq *connmgr.ConnReq, p, err := newPeer( conn, connReq, s, peerAddr, inbound, initFeatures, legacyFeatures, cfg.ChanEnableTimeout, - defaultOutgoingCltvRejectDelta, errBuffer, + lncfg.DefaultOutgoingCltvRejectDelta, errBuffer, ) if err != nil { srvrLog.Errorf("unable to create peer %v", err)