Fixing issue with ever expanding config.json and also truncating the file correctly when updating.

This commit is contained in:
clemahieu 2016-04-21 22:00:26 -05:00
commit 4efe57bb44
4 changed files with 135 additions and 119 deletions

View file

@ -593,7 +593,7 @@ packet_delay_microseconds (5000),
bootstrap_fraction_numerator (1),
creation_rebroadcast (2),
rebroadcast_delay (15),
receive_minimum (rai::Mrai_ratio),
receive_minimum (rai::rai_ratio),
inactive_supply (0),
password_fanout (1024),
io_threads (std::max <unsigned> (4, std::thread::hardware_concurrency ())),
@ -630,7 +630,7 @@ work_threads (std::max <unsigned> (4, std::thread::hardware_concurrency ()))
void rai::node_config::serialize_json (boost::property_tree::ptree & tree_a) const
{
tree_a.put ("version", "4");
tree_a.put ("version", "5");
tree_a.put ("peering_port", std::to_string (peering_port));
tree_a.put ("packet_delay_microseconds", std::to_string (packet_delay_microseconds));
tree_a.put ("bootstrap_fraction_numerator", std::to_string (bootstrap_fraction_numerator));
@ -711,6 +711,13 @@ bool rai::node_config::upgrade_json (unsigned version, boost::property_tree::ptr
result = true;
break;
case 4:
tree_a.erase ("receive_minimum");
tree_a.put ("receive_minimum", rai::rai_ratio.convert_to <std::string> ());
tree_a.erase ("version");
tree_a.put ("version", "5");
result = true;
break;
case 5:
break;
default:
throw std::runtime_error ("Unknown node_config version");

View file

@ -100,44 +100,36 @@ void rai_daemon::daemon::run ()
auto working (rai::working_path ());
boost::filesystem::create_directories (working);
rai_daemon::daemon_config config (working);
auto config_path ((working / "config.json").string ());
auto config_path ((working / "config.json"));
std::fstream config_file;
rai::open_or_create (config_file, config_path);
std::unique_ptr <rai::thread_runner> runner;
if (!config_file.fail ())
{
auto error (rai::fetch_object (config, config_file));
if (!error)
auto error (rai::fetch_object (config, config_path, config_file));
if (!error)
{
auto service (boost::make_shared <boost::asio::io_service> ());
rai::work_pool work (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging));
rai::alarm alarm (*service);
rai::node_init init;
auto node (std::make_shared <rai::node> (init, *service, working, alarm, config.node, work));
auto pool (boost::make_shared <boost::network::utils::thread_pool> (node->config.io_threads));
if (!init.error ())
{
auto service (boost::make_shared <boost::asio::io_service> ());
rai::work_pool work (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging));
rai::alarm alarm (*service);
rai::node_init init;
auto node (std::make_shared <rai::node> (init, *service, working, alarm, config.node, work));
auto pool (boost::make_shared <boost::network::utils::thread_pool> (node->config.io_threads));
if (!init.error ())
node->start ();
rai::rpc rpc (service, pool, *node, config.rpc);
if (config.rpc_enable)
{
node->start ();
rai::rpc rpc (service, pool, *node, config.rpc);
if (config.rpc_enable)
{
rpc.start ();
}
runner.reset (new rai::thread_runner (*service, node->config.io_threads));
runner->join ();
}
else
{
std::cerr << "Error initializing node\n";
rpc.start ();
}
runner.reset (new rai::thread_runner (*service, node->config.io_threads));
runner->join ();
}
else
{
std::cerr << "Error deserializing config\n";
std::cerr << "Error initializing node\n";
}
}
else
{
std::cerr << "Error loading configuration\n";
}
}
else
{
std::cerr << "Error deserializing config\n";
}
}

View file

@ -165,105 +165,97 @@ int run_wallet (int argc, char * const * argv)
auto working (rai::working_path ());
boost::filesystem::create_directories (working);
qt_wallet_config config (working);
auto config_path ((working / "config.json").string ());
std::fstream config_file;
rai::open_or_create (config_file, config_path);
auto config_path ((working / "config.json"));
int result (0);
if (!config_file.fail ())
{
auto error (rai::fetch_object (config, config_file));
if (!error)
std::fstream config_file;
auto error (rai::fetch_object (config, config_path, config_file));
if (!error)
{
QApplication application (argc, const_cast <char **> (argv));
rai::set_application_icon (application);
auto service (boost::make_shared <boost::asio::io_service> ());
rai::work_pool work (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging));
rai::alarm alarm (*service);
rai::node_init init;
auto node (std::make_shared <rai::node> (init, *service, working, alarm, config.node, work));
auto pool (boost::make_shared <boost::network::utils::thread_pool> (node->config.io_threads));
if (!init.error ())
{
QApplication application (argc, const_cast <char **> (argv));
rai::set_application_icon (application);
auto service (boost::make_shared <boost::asio::io_service> ());
rai::work_pool work (rai::opencl_work::create (config.opencl_enable, config.opencl, config.node.logging));
rai::alarm alarm (*service);
rai::node_init init;
auto node (std::make_shared <rai::node> (init, *service, working, alarm, config.node, work));
auto pool (boost::make_shared <boost::network::utils::thread_pool> (node->config.io_threads));
if (!init.error ())
if (config.account.is_zero ())
{
if (config.account.is_zero ())
auto wallet (node->wallets.create (config.wallet));
config.account = wallet->deterministic_insert ();
assert (wallet->exists (config.account));
error = config.serialize_json_stream (config_file);
}
if (!error)
{
auto wallet (node->wallets.open (config.wallet));
if (wallet != nullptr)
{
auto wallet (node->wallets.create (config.wallet));
config.account = wallet->deterministic_insert ();
assert (wallet->exists (config.account));
error = config.serialize_json_stream (config_file);
}
if (!error)
{
auto wallet (node->wallets.open (config.wallet));
if (wallet != nullptr)
if (wallet->exists (config.account))
{
if (wallet->exists (config.account))
node->start ();
rai::rpc rpc (service, pool, *node, config.rpc);
if (config.rpc_enable)
{
node->start ();
rai::rpc rpc (service, pool, *node, config.rpc);
if (config.rpc_enable)
{
rpc.start ();
}
std::unique_ptr <rai_qt::wallet> gui (new rai_qt::wallet (application, *node, wallet, config.account));
gui->client_window->show ();
rai::thread_runner runner (*service, node->config.io_threads);
QObject::connect (&application, &QApplication::aboutToQuit, [&] ()
{
rpc.stop ();
node->stop ();
});
try
{
result = application.exec ();
}
catch (...)
{
result = -1;
assert (false);
}
runner.join ();
config_file.seekg (0);
auto account (config.account);
if (!rai::fetch_object (config, config_file))
{
if (account != config.account)
{
config.account = account;
config_file.close ();
config_file.open (config_path, std::ios_base::out | std::ios_base::trunc);
error = config.serialize_json_stream (config_file);
}
}
rpc.start ();
}
else
std::unique_ptr <rai_qt::wallet> gui (new rai_qt::wallet (application, *node, wallet, config.account));
gui->client_window->show ();
rai::thread_runner runner (*service, node->config.io_threads);
QObject::connect (&application, &QApplication::aboutToQuit, [&] ()
{
std::cerr << "Wallet account doesn't exist\n";
rpc.stop ();
node->stop ();
});
try
{
result = application.exec ();
}
catch (...)
{
result = -1;
assert (false);
}
runner.join ();
config_file.seekg (0);
auto account (config.account);
if (!rai::fetch_object (config, config_file))
{
if (account != config.account)
{
config.account = account;
config_file.close ();
config_file.open (config_path.string (), std::ios_base::out | std::ios_base::trunc);
error = config.serialize_json_stream (config_file);
}
}
}
else
{
std::cerr << "Wallet id doesn't exist\n";
std::cerr << "Wallet account doesn't exist\n";
}
}
else
{
std::cerr << "Error writing config file\n";
std::cerr << "Wallet id doesn't exist\n";
}
}
else
{
std::cerr << "Error initializing node\n";
std::cerr << "Error writing config file\n";
}
}
else
{
std::cerr << "Error deserializing config\n";
std::cerr << "Error initializing node\n";
}
}
else
{
std::cerr << "Unable to open config file\n";
}
}
else
{
std::cerr << "Error deserializing config\n";
}
return result;
}

View file

@ -72,23 +72,48 @@ bool fetch_object (T & object, std::iostream & stream_a)
{
auto updated (false);
error = object.deserialize_json (updated, tree);
if (!error && updated)
}
return error;
}
// Reads a json object from the stream and if was changed, write the object back to the stream
template <typename T>
bool fetch_object (T & object, boost::filesystem::path const & path_a, std::fstream & stream_a)
{
bool error (false);
rai::open_or_create (stream_a, path_a.string ());
if (!stream_a.fail ())
{
boost::property_tree::ptree tree;
try
{
auto end (stream_a.tellp ());
stream_a.seekp (0);
try
{
boost::property_tree::write_json (stream_a, tree);
while (stream_a.tellp () != end)
{
stream_a << ' ';
}
}
catch (std::runtime_error const &)
boost::property_tree::read_json (stream_a, tree);
}
catch (std::runtime_error const &)
{
auto pos (stream_a.tellg ());
if (pos != std::streampos(0))
{
error = true;
}
}
if (!error)
{
auto updated (false);
error = object.deserialize_json (updated, tree);
if (!error && updated)
{
stream_a.close ();
stream_a.open (path_a.string (), std::ios_base::out | std::ios_base::trunc);
try
{
boost::property_tree::write_json (stream_a, tree);
}
catch (std::runtime_error const &)
{
error = true;
}
}
}
}
return error;
}