Remove ambguity WRT loading and passing of Config

pull/1312/head
Stephen Shelton 4 years ago
parent f607b99dbe
commit 0f074cff8c
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -99,7 +99,7 @@ run_main_context(const fs::path confFile, const llarp::RuntimeOptions opts)
conf.Load(confFile, opts.isRouter, confFile.parent_path());
ctx = std::make_shared<llarp::Context>();
ctx->Configure(opts, {}, confFile);
ctx->Configure(conf);
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);

@ -39,7 +39,6 @@ namespace llarp
std::unique_ptr<CryptoManager> cryptoManager = nullptr;
std::unique_ptr<AbstractRouter> router = nullptr;
std::shared_ptr<Logic> logic = nullptr;
std::unique_ptr<Config> config = nullptr;
std::unique_ptr<llarp_nodedb> nodedb = nullptr;
llarp_ev_loop_ptr mainloop;
std::string nodedb_dir;
@ -59,9 +58,11 @@ namespace llarp
void
HandleSignal(int sig);
bool
Configure(
const RuntimeOptions& opts, std::optional<fs::path> dataDir, const fs::path& configfile);
/// Configure given the specified config.
///
/// note: consider using std::move() when passing conf in.
void
Configure(Config conf);
bool
IsUp() const;
@ -90,6 +91,9 @@ namespace llarp
llarp_ev_loop_ptr __netloop,
std::shared_ptr<Logic> logic);
protected:
std::unique_ptr<Config> config = nullptr;
private:
void
SigINT();

@ -27,42 +27,17 @@ namespace llarp
return logic && LogicCall(logic, f);
}
bool
Context::Configure(
const RuntimeOptions& opts, std::optional<fs::path> dataDir, const fs::path& configfile)
void
Context::Configure(Config conf)
{
LogWarn("Context::Configure()");
if (nullptr == config.get())
config = std::make_unique<Config>();
if (nullptr != config.get())
throw std::runtime_error("Config already exists");
fs::path defaultDataDir = dataDir ? *dataDir : GetDefaultDataDir();
// TODO: DRY / refactor to use exceptions
if (configfile.empty())
{
if (not config->LoadDefault(opts.isRouter, defaultDataDir))
{
config.release();
llarp::LogError("failed to load default config");
return false;
}
}
else
{
if (!config->Load(configfile.c_str(), opts.isRouter, defaultDataDir))
{
config.release();
llarp::LogError("failed to load config file ", configfile);
return false;
}
}
config = std::make_unique<Config>(std::move(conf));
logic = std::make_shared<Logic>();
nodedb_dir = fs::path(config->router.m_dataDir / nodedb_dirname).string();
return true;
}
bool
@ -87,6 +62,10 @@ namespace llarp
void
Context::Setup(const RuntimeOptions& opts)
{
/// Call one of the Configure() methods before calling Setup()
if (not config)
throw std::runtime_error("Cannot call Setup() on context without a Config");
llarp::LogInfo(llarp::VERSION_FULL, " ", llarp::RELEASE_MOTTO);
llarp::LogInfo("starting up");
if (mainloop == nullptr)
@ -106,7 +85,7 @@ namespace llarp
nodedb = std::make_unique<llarp_nodedb>(
nodedb_dir, [r = router.get()](auto call) { r->QueueDiskIO(std::move(call)); });
if (!router->Configure(config.get(), opts.isRouter, nodedb.get()))
if (!router->Configure(*config.get(), opts.isRouter, nodedb.get()))
throw std::runtime_error("Failed to configure router");
// must be done after router is made so we can use its disk io worker

@ -159,7 +159,7 @@ namespace llarp
Sign(Signature& sig, const llarp_buffer_t& buf) const = 0;
virtual bool
Configure(Config* conf, bool isRouter, llarp_nodedb* nodedb) = 0;
Configure(const Config& conf, bool isRouter, llarp_nodedb* nodedb) = 0;
virtual bool
IsServiceNode() const = 0;
@ -197,19 +197,10 @@ namespace llarp
/// connect to N random routers
virtual void
ConnectToRandomRouters(int N) = 0;
/// inject configuration and reconfigure router
virtual bool
Reconfigure(Config* conf) = 0;
virtual bool
TryConnectAsync(RouterContact rc, uint16_t tries) = 0;
/// validate new configuration against old one
/// return true on 100% valid
/// return false if not 100% valid
virtual bool
ValidateConfig(Config* conf) const = 0;
/// called by link when a remote session has no more sessions open
virtual void
SessionClosed(RouterID remote) = 0;

@ -238,18 +238,16 @@ namespace llarp
}
bool
Router::Configure(Config* conf, bool isRouter, llarp_nodedb* nodedb)
Router::Configure(const Config& conf, bool isRouter, llarp_nodedb* nodedb)
{
// we need this first so we can start lmq to fetch keys
if (conf)
{
whitelistRouters = conf->lokid.whitelistRouters;
if (whitelistRouters)
lokidRPCAddr = lokimq::address(conf->lokid.lokidRPCAddr);
whitelistRouters = conf.lokid.whitelistRouters;
if (whitelistRouters)
lokidRPCAddr = lokimq::address(conf.lokid.lokidRPCAddr);
enableRPCServer = conf.api.m_enableRPCServer;
if (enableRPCServer)
rpcBindAddr = lokimq::address(conf.api.m_rpcBindAddr);
enableRPCServer = conf->api.m_enableRPCServer;
rpcBindAddr = lokimq::address(conf->api.m_rpcBindAddr);
}
if (not StartRpcServer())
throw std::runtime_error("Failed to start rpc server");
@ -263,13 +261,11 @@ namespace llarp
}
// fetch keys
if (conf)
{
if (not m_keyManager->initialize(*conf, true, isRouter))
throw std::runtime_error("KeyManager failed to initialize");
if (!FromConfig(conf))
throw std::runtime_error("FromConfig() failed");
}
if (not m_keyManager->initialize(conf, true, isRouter))
throw std::runtime_error("KeyManager failed to initialize");
if (!FromConfig(conf))
throw std::runtime_error("FromConfig() failed");
if (!InitOutboundLinks())
throw std::runtime_error("InitOutboundLinks() failed");
@ -386,12 +382,12 @@ namespace llarp
}
bool
Router::FromConfig(Config* conf)
Router::FromConfig(const Config& conf)
{
// Set netid before anything else
if (!conf->router.m_netId.empty() && strcmp(conf->router.m_netId.c_str(), llarp::DEFAULT_NETID))
if (!conf.router.m_netId.empty() && strcmp(conf.router.m_netId.c_str(), llarp::DEFAULT_NETID))
{
const auto& netid = conf->router.m_netId;
const auto& netid = conf.router.m_netId;
llarp::LogWarn(
"!!!! you have manually set netid to be '",
netid,
@ -406,36 +402,36 @@ namespace llarp
}
// IWP config
m_OutboundPort = conf->links.m_OutboundLink.port;
m_OutboundPort = conf.links.m_OutboundLink.port;
// Router config
_rc.SetNick(conf->router.m_nickname);
_outboundSessionMaker.maxConnectedRouters = conf->router.m_maxConnectedRouters;
_outboundSessionMaker.minConnectedRouters = conf->router.m_minConnectedRouters;
_rc.SetNick(conf.router.m_nickname);
_outboundSessionMaker.maxConnectedRouters = conf.router.m_maxConnectedRouters;
_outboundSessionMaker.minConnectedRouters = conf.router.m_minConnectedRouters;
encryption_keyfile = m_keyManager->m_encKeyPath;
our_rc_file = m_keyManager->m_rcPath;
transport_keyfile = m_keyManager->m_transportKeyPath;
ident_keyfile = m_keyManager->m_idKeyPath;
_ourAddress = conf->router.m_publicAddress;
_ourAddress = conf.router.m_publicAddress;
RouterContact::BlockBogons = conf->router.m_blockBogons;
RouterContact::BlockBogons = conf.router.m_blockBogons;
// Lokid Config
usingSNSeed = conf->lokid.usingSNSeed;
whitelistRouters = conf->lokid.whitelistRouters;
lokidRPCAddr = lokimq::address(conf->lokid.lokidRPCAddr);
usingSNSeed = conf.lokid.usingSNSeed;
whitelistRouters = conf.lokid.whitelistRouters;
lokidRPCAddr = lokimq::address(conf.lokid.lokidRPCAddr);
if (usingSNSeed)
ident_keyfile = conf->lokid.ident_keyfile;
ident_keyfile = conf.lokid.ident_keyfile;
// TODO: add config flag for "is service node"
if (conf->links.m_InboundLinks.size())
if (conf.links.m_InboundLinks.size())
{
m_isServiceNode = true;
}
networkConfig = conf->network;
networkConfig = conf.network;
/// build a set of strictConnectPubkeys (
/// TODO: make this consistent with config -- do we support multiple strict connections
@ -458,21 +454,21 @@ namespace llarp
throw std::invalid_argument(stringify("invalid key for strict-connect: ", val));
}
std::vector<fs::path> configRouters = conf->connect.routers;
std::vector<fs::path> configRouters = conf.connect.routers;
configRouters.insert(
configRouters.end(), conf->bootstrap.routers.begin(), conf->bootstrap.routers.end());
configRouters.end(), conf.bootstrap.routers.begin(), conf.bootstrap.routers.end());
// if our conf had no bootstrap files specified, try the default location of
// <DATA_DIR>/bootstrap.signed. If this isn't present, leave a useful error message
if (configRouters.size() == 0 and not m_isServiceNode)
{
// TODO: use constant
fs::path defaultBootstrapFile = conf->router.m_dataDir / "bootstrap.signed";
fs::path defaultBootstrapFile = conf.router.m_dataDir / "bootstrap.signed";
if (fs::exists(defaultBootstrapFile))
{
configRouters.push_back(defaultBootstrapFile);
}
else if (not conf->bootstrap.skipBootstrap)
else if (not conf.bootstrap.skipBootstrap)
{
LogError("No bootstrap files specified in config file, and the default");
LogError("bootstrap file ", defaultBootstrapFile, " does not exist.");
@ -547,7 +543,7 @@ namespace llarp
m_isServiceNode);
// create inbound links, if we are a service node
for (const LinksConfig::LinkInfo& serverConfig : conf->links.m_InboundLinks)
for (const LinksConfig::LinkInfo& serverConfig : conf.links.m_InboundLinks)
{
auto server = iwp::NewInboundLink(
m_keyManager,
@ -572,15 +568,15 @@ namespace llarp
}
// Network config
if (conf->network.m_enableProfiling.has_value() and not*conf->network.m_enableProfiling)
if (conf.network.m_enableProfiling.has_value() and not*conf.network.m_enableProfiling)
{
routerProfiling().Disable();
LogWarn("router profiling explicitly disabled");
}
if (!conf->network.m_routerProfilesFile.empty())
if (!conf.network.m_routerProfilesFile.empty())
{
routerProfilesFile = conf->network.m_routerProfilesFile;
routerProfilesFile = conf.network.m_routerProfilesFile;
routerProfiling().Load(routerProfilesFile.c_str());
llarp::LogInfo("setting profiles to ", routerProfilesFile);
}
@ -588,15 +584,15 @@ namespace llarp
// API config
if (not IsServiceNode())
{
hiddenServiceContext().AddEndpoint(*conf);
hiddenServiceContext().AddEndpoint(conf);
}
// peer stats
if (conf->router.m_enablePeerStats)
if (conf.router.m_enablePeerStats)
{
LogInfo("Initializing peerdb...");
m_peerDb = std::make_shared<PeerDb>();
m_peerDb->configure(conf->router);
m_peerDb->configure(conf.router);
}
else
{
@ -605,10 +601,10 @@ namespace llarp
// Logging config
LogContext::Instance().Initialize(
conf->logging.m_logLevel,
conf->logging.m_logType,
conf->logging.m_logFile,
conf->router.m_nickname,
conf.logging.m_logLevel,
conf.logging.m_logType,
conf.logging.m_logFile,
conf.router.m_nickname,
util::memFn(&AbstractRouter::QueueDiskIO, this));
return true;
@ -1165,19 +1161,6 @@ namespace llarp
return true;
}
bool
Router::ValidateConfig(Config* /*conf*/) const
{
return true;
}
bool
Router::Reconfigure(Config*)
{
// TODO: implement me
return true;
}
bool
Router::TryConnectAsync(RouterContact rc, uint16_t tries)
{

@ -345,7 +345,7 @@ namespace llarp
Close();
bool
Configure(Config* conf, bool isRouter, llarp_nodedb* nodedb = nullptr) override;
Configure(const Config& conf, bool isRouter, llarp_nodedb* nodedb = nullptr) override;
bool
StartRpcServer() override;
@ -392,19 +392,9 @@ namespace llarp
void
try_connect(fs::path rcfile);
/// inject configuration and reconfigure router
bool
Reconfigure(Config* conf) override;
bool
TryConnectAsync(RouterContact rc, uint16_t tries) override;
/// validate new configuration against old one
/// return true on 100% valid
/// return false if not 100% valid
bool
ValidateConfig(Config* conf) const override;
/// send to remote router or queue for sending
/// returns false on overflow
/// returns true on successful queue
@ -524,7 +514,7 @@ namespace llarp
UpdateOurRC(bool rotateKeys = false);
bool
FromConfig(Config* conf);
FromConfig(const Config& conf);
void
MessageSent(const RouterID& remote, SendStatus status);

@ -22,8 +22,7 @@ namespace tooling
opts.isRouter = isRouter;
Context_ptr context = std::make_shared<HiveContext>(this);
context->config = std::make_unique<llarp::Config>(*config.get());
context->Configure(opts, {}, {});
context->Configure(*config);
context->Setup(opts);
auto routerId = llarp::RouterID(context->router->pubkey());

@ -11,22 +11,24 @@ static const llarp::RuntimeOptions opts = {.background = false, .debug = false,
std::shared_ptr<llarp::Context>
make_context()
{
auto context = std::make_shared<llarp::Context>();
REQUIRE(context->Configure(opts, {}, {}) == true);
REQUIRE(context->config != nullptr);
REQUIRE(context->config->LoadDefault(true, fs::current_path()));
llarp::Config conf;
conf.LoadDefault(true, fs::current_path());
// set testing defaults
context->config->network.m_endpointType = "null";
context->config->bootstrap.skipBootstrap = true;
context->config->api.m_enableRPCServer = false;
conf.network.m_endpointType = "null";
conf.bootstrap.skipBootstrap = true;
conf.api.m_enableRPCServer = false;
// make a fake inbound link
context->config->links.m_InboundLinks.emplace_back();
auto& link = context->config->links.m_InboundLinks.back();
conf.links.m_InboundLinks.emplace_back();
auto& link = conf.links.m_InboundLinks.back();
link.interface = llarp::net::LoopbackInterfaceName();
link.addressFamily = AF_INET;
link.port = 0;
// configure
auto context = std::make_shared<llarp::Context>();
REQUIRE_NOTHROW(context->Configure(std::move(conf)));
return context;
}

@ -11,14 +11,15 @@ llarp::RuntimeOptions opts = {false, false, false};
static std::shared_ptr<llarp::Context>
make_context(std::optional<fs::path> keyfile)
{
auto context = std::make_shared<llarp::Context>();
REQUIRE(context->Configure(opts, {}, {}) == true);
REQUIRE(context->config != nullptr);
llarp::Config conf;
conf.LoadDefault(opts.isRouter, {});
conf.network.m_endpointType = "null";
conf.network.m_keyfile = keyfile;
conf.bootstrap.skipBootstrap = true;
conf.api.m_enableRPCServer = false;
context->config->network.m_endpointType = "null";
context->config->network.m_keyfile = keyfile;
context->config->bootstrap.skipBootstrap = true;
context->config->api.m_enableRPCServer = false;
auto context = std::make_shared<llarp::Context>();
REQUIRE_NOTHROW(context->Configure(std::move(conf)));
return context;
}

Loading…
Cancel
Save