Skip to content

Commit

Permalink
lightningd: change config-dir from plugin / wallet / hsm POV into <ne…
Browse files Browse the repository at this point in the history
…twork> subdir

Changelog-changed: .lightningd plugins and files moved into <network>/ subdir
Changelog-changed: WARNING: If you don't have a config file, you now may need to specify the network to lightning-cli
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Nov 23, 2019
1 parent dc23c30 commit aab83e7
Show file tree
Hide file tree
Showing 20 changed files with 143 additions and 98 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ interfaces) for more sophisticated use.
`lightningd` can be configured either by passing options via the command line, or via a configuration file.
Command line options will always override the values in the configuration file.

To use a configuration file, create a file named `config` within your lightning directory
(eg. `~/.lightning/config`). See `man -l doc/lightningd-config.5`.
To use a configuration file, create a file named `config` within your top-level lightning directory or network subdirectory
(eg. `~/.lightning/config` or `~/.lightning/bitcoin/config`). See `man -l doc/lightningd-config.5`.

## Further information

Expand Down
13 changes: 5 additions & 8 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ int main(int argc, char *argv[])
jsmntok_t *toks;
const jsmntok_t *result, *error, *id;
const tal_t *ctx = tal(NULL, char);
char *config_filename, *lightning_dir, *rpc_filename;
char *config_filename, *lightning_dir, *net_dir, *rpc_filename;
jsmn_parser parser;
int parserr;
enum format format = DEFAULT_FORMAT;
Expand All @@ -446,7 +446,8 @@ int main(int argc, char *argv[])
setup_option_allocators();

initial_config_opts(ctx, argc, argv,
&config_filename, &lightning_dir, &rpc_filename);
&config_filename, &lightning_dir, &net_dir,
&rpc_filename);

opt_register_noarg("--help|-h", opt_usage_and_exit,
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
Expand Down Expand Up @@ -491,9 +492,9 @@ int main(int argc, char *argv[])
tal_free(page);
}

if (chdir(lightning_dir) != 0)
if (chdir(net_dir) != 0)
err(ERROR_TALKING_TO_LIGHTNINGD, "Moving into '%s'",
lightning_dir);
net_dir);

fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (strlen(rpc_filename) + 1 > sizeof(addr.sun_path))
Expand Down Expand Up @@ -627,8 +628,6 @@ int main(int argc, char *argv[])
default:
abort();
}
tal_free(lightning_dir);
tal_free(rpc_filename);
tal_free(ctx);
opt_free_table();
return 0;
Expand All @@ -641,8 +640,6 @@ int main(int argc, char *argv[])
print_json(resp, error, "");
printf("\n");
}
tal_free(lightning_dir);
tal_free(rpc_filename);
tal_free(ctx);
opt_free_table();
return 1;
Expand Down
9 changes: 9 additions & 0 deletions cli/test/run-large-input.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ int test_connect(int sockfd, const struct sockaddr *addr,
socklen_t addrlen);
int test_getpid(void);
int test_printf(const char *format, ...);
int test_chdir(const char *path);

#define main test_main
#define read test_read
#define socket test_socket
#define connect test_connect
#define getpid test_getpid
#define printf test_printf
#define chdir test_chdir

#include "../lightning-cli.c"
#undef main
Expand Down Expand Up @@ -79,6 +81,11 @@ int test_printf(const char *fmt UNUSED, ...)
return 0;
}

int test_chdir(const char *path)
{
return 0;
}

static char *response;
static size_t response_off, max_read_return;

Expand Down Expand Up @@ -140,5 +147,7 @@ int main(int argc UNUSED, char *argv[])
max_read_return = -1;
assert(test_main(3, fake_argv) == 0);
tal_free(response);
assert(!taken_any());
take_cleanup();
return 0;
}
9 changes: 8 additions & 1 deletion cli/test/run-remove-hint.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ int test_connect(int sockfd, const struct sockaddr *addr,
int test_getpid(void);
int test_printf(const char *format, ...);
int test_fputc(int c, FILE *stream);
int test_chdir(const char *path);

#define main test_main
#define read test_read
Expand All @@ -25,6 +26,7 @@ int test_fputc(int c, FILE *stream);
#define getpid test_getpid
#define printf test_printf
#define fputc test_fputc
#define chdir test_chdir

#include "../lightning-cli.c"
#undef main
Expand Down Expand Up @@ -106,6 +108,11 @@ int test_fputc(int c, FILE *stream)
return (unsigned)c;
}

int test_chdir(const char *path)
{
return 0;
}

int main(int argc UNUSED, char *argv[])
{
setup_locale();
Expand All @@ -114,7 +121,6 @@ int main(int argc UNUSED, char *argv[])

output = tal_strdup(NULL, "");
assert(test_main(3, fake_argv) == 0);
assert(!taken_any());

assert(streq(output, "channels=\n"
"\n"
Expand All @@ -130,6 +136,7 @@ int main(int argc UNUSED, char *argv[])
"num_channels=1\n"
"num_connected=1\n"));
tal_free(output);
assert(!taken_any());
take_cleanup();
return 0;
}
50 changes: 27 additions & 23 deletions common/configdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,14 @@ void setup_option_allocators(void)
}

/* network is NULL for parsing top-level config file. */
static void parse_implied_config_file(const char *config_dir,
static void parse_implied_config_file(const char *config_basedir,
const char *network,
bool early)
{
const char *dir, *filename;

if (config_dir)
dir = path_join(NULL, take(path_cwd(NULL)), config_dir);
if (config_basedir)
dir = path_join(NULL, take(path_cwd(NULL)), config_basedir);
else
dir = default_base_configdir(NULL);

Expand All @@ -227,22 +227,23 @@ static void parse_implied_config_file(const char *config_dir,
* Otherwise we read <lightning-dir>/config then <lightning-dir>/<network>/config
*/
void parse_config_files(const char *config_filename,
const char *config_dir,
const char *config_basedir,
bool early)
{
if (config_filename) {
parse_include(config_filename, true, early);
return;
}

parse_implied_config_file(config_dir, NULL, early);
parse_implied_config_file(config_dir, chainparams->network_name, early);
parse_implied_config_file(config_basedir, NULL, early);
parse_implied_config_file(config_basedir, chainparams->network_name, early);
}

void initial_config_opts(const tal_t *ctx,
int argc, char *argv[],
char **config_filename,
char **config_dir,
char **config_basedir,
char **config_netdir,
char **rpc_filename)
{
options_ctx = ctx;
Expand All @@ -255,11 +256,11 @@ void initial_config_opts(const tal_t *ctx,
"Specify configuration file");

/* Cmdline can also set lightning-dir. */
*config_dir = NULL;
*config_basedir = NULL;
opt_register_early_arg("--lightning-dir=<dir>",
opt_set_talstr, NULL,
config_dir,
"Set working directory. All other files are relative to this");
opt_set_abspath, NULL,
config_basedir,
"Set base directory: network-specific subdirectory is under here");

/* Handle --version (and exit) here too */
opt_register_version();
Expand All @@ -277,19 +278,19 @@ void initial_config_opts(const tal_t *ctx,
if (!*config_filename) {
opt_register_early_arg("--lightning-dir=<dir>",
opt_ignore, opt_show_charp,
config_dir,
"Set working directory. All other files are relative to this");
config_basedir,
"Set base directory: network-specific subdirectory is under here");
} else {
opt_register_early_arg("--lightning-dir=<dir>",
opt_set_talstr, NULL,
config_dir,
"Set working directory. All other files are relative to this");
opt_set_abspath, NULL,
config_basedir,
"Set base directory: network-specific subdirectory is under here");
}

/* Now, config file (or cmdline) can set network and lightning-dir */

/* We need to know network early, so we can set defaults (which normal
* options can change) and default config_dir */
* options can change) and default config_netdir */
opt_register_early_arg("--network", opt_set_network, opt_show_network,
NULL,
"Select the network parameters (bitcoin, testnet,"
Expand All @@ -308,19 +309,22 @@ void initial_config_opts(const tal_t *ctx,
if (*config_filename)
parse_include(*config_filename, true, true);
else
parse_implied_config_file(*config_dir, NULL, true);
parse_implied_config_file(*config_basedir, NULL, true);
opt_early_parse_incomplete(argc, argv, opt_log_stderr_exit);

/* We use a global (in common/utils.h) for the chainparams.
* We default to testnet for now. */
if (!chainparams)
chainparams = chainparams_for_network("testnet");

if (!*config_dir)
*config_dir = default_base_configdir(ctx);
if (!*config_basedir)
*config_basedir = default_base_configdir(ctx);

*config_netdir
= path_join(NULL, *config_basedir, chainparams->network_name);

/* Make sure it's absolute */
*config_dir = path_join(ctx, take(path_cwd(NULL)), take(*config_dir));
*config_netdir = path_join(ctx, take(path_cwd(NULL)), take(*config_netdir));

/* Now, reset and ignore those options from now on. */
opt_free_table();
Expand All @@ -330,8 +334,8 @@ void initial_config_opts(const tal_t *ctx,
"Specify configuration file");
opt_register_early_arg("--lightning-dir=<dir>",
opt_ignore, opt_show_charp,
config_dir,
"Set working directory. All other files are relative to this");
config_basedir,
"Set base directory: network-specific subdirectory is under here");
opt_register_early_arg("--network", opt_ignore, opt_show_network,
NULL,
"Select the network parameters (bitcoin, testnet,"
Expand Down
9 changes: 5 additions & 4 deletions common/configdir.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ void setup_option_allocators(void);
void initial_config_opts(const tal_t *ctx,
int argc, char *argv[],
char **config_filename,
char **config_dir,
char **config_basedir,
char **config_netdir,
char **rpc_filename);

/* If they specify --conf, we just read that.
* If they specify --lightning-dir, we just read <lightning_dir>/config.
* Otherwise, we read ../config (toplevel), and config (network-level) */
* Otherwise, we read basedir/config (toplevel), and basedir/<network>/config
* (network-level) */
void parse_config_files(const char *config_filename,
const char *config_dir,
const char *config_basedir,
bool early);

/* For listconfigs to access. */
Expand Down
18 changes: 9 additions & 9 deletions contrib/pyln-testing/pyln/testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
'lightning-dir': lightning_dir,
'addr': '127.0.0.1:{}'.format(port),
'allow-deprecated-apis': 'false',
'network': env('TEST_NETWORK', 'regtest'),
'network': TEST_NETWORK,
'ignore-fee-limits': 'false',
'bitcoin-rpcuser': BITCOIND_CONFIG['rpcuser'],
'bitcoin-rpcpassword': BITCOIND_CONFIG['rpcpassword'],
Expand All @@ -487,13 +487,13 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
for k, v in opts.items():
self.opts[k] = v

if not os.path.exists(lightning_dir):
os.makedirs(lightning_dir)
if not os.path.exists(os.path.join(lightning_dir, TEST_NETWORK)):
os.makedirs(os.path.join(lightning_dir, TEST_NETWORK))

# Last 32-bytes of final part of dir -> seed.
seed = (bytes(re.search('([^/]+)/*$', lightning_dir).group(1), encoding='utf-8') + bytes(32))[:32]
if not random_hsm:
with open(os.path.join(lightning_dir, 'hsm_secret'), 'wb') as f:
with open(os.path.join(lightning_dir, TEST_NETWORK, 'hsm_secret'), 'wb') as f:
f.write(seed)
if DEVELOPER:
self.opts['dev-fast-gossip'] = None
Expand Down Expand Up @@ -551,7 +551,7 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, may_fail=False,
self.allow_bad_gossip = allow_bad_gossip
self.db = db

socket_path = os.path.join(lightning_dir, "lightning-rpc").format(node_id)
socket_path = os.path.join(lightning_dir, TEST_NETWORK, "lightning-rpc").format(node_id)
self.rpc = LightningRpc(socket_path, self.executor)

self.daemon = LightningD(
Expand All @@ -560,7 +560,7 @@ def __init__(self, node_id, lightning_dir, bitcoind, executor, may_fail=False,
)
# If we have a disconnect string, dump it to a file for daemon.
if disconnect:
self.daemon.disconnect_file = os.path.join(lightning_dir, "dev_disconnect")
self.daemon.disconnect_file = os.path.join(lightning_dir, TEST_NETWORK, "dev_disconnect")
with open(self.daemon.disconnect_file, "w") as f:
f.write("\n".join(disconnect))
self.daemon.opts["dev-disconnect"] = "dev_disconnect"
Expand Down Expand Up @@ -632,7 +632,7 @@ def db_query(self, query):

# Assumes node is stopped!
def db_manip(self, query):
db = sqlite3.connect(os.path.join(self.daemon.lightning_dir, "lightningd.sqlite3"))
db = sqlite3.connect(os.path.join(self.daemon.lightning_dir, TEST_NETWORK, "lightningd.sqlite3"))
db.row_factory = sqlite3.Row
c = db.cursor()
c.execute(query)
Expand Down Expand Up @@ -984,7 +984,7 @@ def get_node(self, node_id=None, options=None, dbfile=None,

# Get the DB backend DSN we should be using for this test and this
# node.
db = self.db_provider.get_db(lightning_dir, self.testname, node_id)
db = self.db_provider.get_db(os.path.join(lightning_dir, TEST_NETWORK), self.testname, node_id)
node = self.node_cls(
node_id, lightning_dir, self.bitcoind, self.executor, db=db,
port=port, options=options, **kwargs
Expand All @@ -995,7 +995,7 @@ def get_node(self, node_id=None, options=None, dbfile=None,

self.nodes.append(node)
if dbfile:
out = open(os.path.join(node.daemon.lightning_dir,
out = open(os.path.join(node.daemon.lightning_dir, TEST_NETWORK,
'lightningd.sqlite3'), 'xb')
with lzma.open(os.path.join('tests/data', dbfile), 'rb') as f:
out.write(f.read())
Expand Down
Loading

0 comments on commit aab83e7

Please sign in to comment.