github.com/decred/dcrlnd@v0.7.6/lncfg/config.go (about)

     1  package lncfg
     2  
     3  import (
     4  	"os"
     5  	"os/user"
     6  	"path/filepath"
     7  	"strings"
     8  )
     9  
    10  const (
    11  	// DefaultConfigFilename is the default configuration file name dcrlnd
    12  	// tries to load.
    13  	DefaultConfigFilename = "dcrlnd.conf"
    14  
    15  	// DefaultMaxPendingChannels is the default maximum number of incoming
    16  	// pending channels permitted per peer.
    17  	DefaultMaxPendingChannels = 1
    18  
    19  	// DefaultIncomingBroadcastDelta defines the number of blocks before the
    20  	// expiry of an incoming htlc at which we force close the channel. We
    21  	// only go to chain if we also have the preimage to actually pull in the
    22  	// htlc. BOLT #2 suggests 7 blocks. We use a few more for extra safety.
    23  	// Within this window we need to get our sweep or 2nd level success tx
    24  	// confirmed, because after that the remote party is also able to claim
    25  	// the htlc using the timeout path.
    26  	DefaultIncomingBroadcastDelta = 10
    27  
    28  	// DefaultFinalCltvRejectDelta defines the number of blocks before the
    29  	// expiry of an incoming exit hop htlc at which we cancel it back
    30  	// immediately. It is an extra safety measure over the final cltv
    31  	// requirement as it is defined in the invoice. It ensures that we
    32  	// cancel back htlcs that, when held on to, may cause us to force close
    33  	// the channel because we enter the incoming broadcast window. Bolt #11
    34  	// suggests 9 blocks here. We use a few more for additional safety.
    35  	//
    36  	// There is still a small gap that remains between receiving the
    37  	// RevokeAndAck and canceling back. If a new block arrives within that
    38  	// window, we may still force close the channel. There is currently no
    39  	// way to reject an UpdateAddHtlc of which we already know that it will
    40  	// push us in the broadcast window.
    41  	DefaultFinalCltvRejectDelta = DefaultIncomingBroadcastDelta + 3
    42  
    43  	// DefaultOutgoingBroadcastDelta defines the number of blocks before the
    44  	// expiry of an outgoing htlc at which we force close the channel. We
    45  	// are not in a hurry to force close, because there is nothing to claim
    46  	// for us. We do need to time the htlc out, because there may be an
    47  	// incoming htlc that will time out too (albeit later). Bolt #2 suggests
    48  	// a value of -1 here, but we allow one block less to prevent potential
    49  	// confusion around the negative value. It means we force close the
    50  	// channel at exactly the htlc expiry height.
    51  	DefaultOutgoingBroadcastDelta = 0
    52  
    53  	// DefaultOutgoingCltvRejectDelta defines the number of blocks before
    54  	// the expiry of an outgoing htlc at which we don't want to offer it to
    55  	// the next peer anymore. If that happens, we cancel back the incoming
    56  	// htlc. This is to prevent the situation where we have an outstanding
    57  	// htlc that brings or will soon bring us inside the outgoing broadcast
    58  	// window and trigger us to force close the channel. Bolt #2 suggests a
    59  	// value of 0. We pad it a bit, to prevent a slow round trip to the next
    60  	// peer and a block arriving during that round trip to trigger force
    61  	// closure.
    62  	DefaultOutgoingCltvRejectDelta = DefaultOutgoingBroadcastDelta + 3
    63  )
    64  
    65  // CleanAndExpandPath expands environment variables and leading ~ in the
    66  // passed path, cleans the result, and returns it.
    67  // This function is taken from https://github.com/decred/dcrd
    68  func CleanAndExpandPath(path string) string {
    69  	if path == "" {
    70  		return ""
    71  	}
    72  
    73  	// Expand initial ~ to OS specific home directory.
    74  	if strings.HasPrefix(path, "~") {
    75  		var homeDir string
    76  		u, err := user.Current()
    77  		if err == nil {
    78  			homeDir = u.HomeDir
    79  		} else {
    80  			homeDir = os.Getenv("HOME")
    81  		}
    82  
    83  		path = strings.Replace(path, "~", homeDir, 1)
    84  	}
    85  
    86  	// NOTE: The os.ExpandEnv doesn't work with Windows-style %VARIABLE%,
    87  	// but the variables can still be expanded via POSIX-style $VARIABLE.
    88  	return filepath.Clean(os.ExpandEnv(path))
    89  }
    90  
    91  // NormalizeNetwork returns the common name of a network type used to create
    92  // file paths. This allows differently versioned networks to use the same path.
    93  func NormalizeNetwork(network string) string {
    94  	if strings.HasPrefix(network, "testnet") {
    95  		return "testnet"
    96  	}
    97  
    98  	return network
    99  }