mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Remove the state file for the random number generator
This commit is contained in:
committed by
Nik Bougalis
parent
cb71d493a0
commit
5e96da51f9
@@ -30,7 +30,6 @@
|
|||||||
#include <ripple/core/DatabaseCon.h>
|
#include <ripple/core/DatabaseCon.h>
|
||||||
#include <ripple/core/TerminateHandler.h>
|
#include <ripple/core/TerminateHandler.h>
|
||||||
#include <ripple/core/TimeKeeper.h>
|
#include <ripple/core/TimeKeeper.h>
|
||||||
#include <ripple/crypto/csprng.h>
|
|
||||||
#include <ripple/json/to_string.h>
|
#include <ripple/json/to_string.h>
|
||||||
#include <ripple/net/RPCCall.h>
|
#include <ripple/net/RPCCall.h>
|
||||||
#include <ripple/resource/Fees.h>
|
#include <ripple/resource/Fees.h>
|
||||||
@@ -68,15 +67,6 @@ namespace po = boost::program_options;
|
|||||||
|
|
||||||
namespace ripple {
|
namespace ripple {
|
||||||
|
|
||||||
boost::filesystem::path
|
|
||||||
getEntropyFile(Config const& config)
|
|
||||||
{
|
|
||||||
auto const path = config.legacy("database_path");
|
|
||||||
if (path.empty ())
|
|
||||||
return {};
|
|
||||||
return boost::filesystem::path (path) / "random.seed";
|
|
||||||
}
|
|
||||||
|
|
||||||
bool
|
bool
|
||||||
adjustDescriptorLimit(int needed, beast::Journal j)
|
adjustDescriptorLimit(int needed, beast::Journal j)
|
||||||
{
|
{
|
||||||
@@ -489,13 +479,6 @@ int run (int argc, char** argv)
|
|||||||
config->setup (configFile, bool (vm.count ("quiet")),
|
config->setup (configFile, bool (vm.count ("quiet")),
|
||||||
bool(vm.count("silent")), bool(vm.count("standalone")));
|
bool(vm.count("silent")), bool(vm.count("standalone")));
|
||||||
|
|
||||||
{
|
|
||||||
// Stir any previously saved entropy into the pool:
|
|
||||||
auto entropy = getEntropyFile (*config);
|
|
||||||
if (!entropy.empty ())
|
|
||||||
crypto_prng().load_state(entropy.string ());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (vm.count("vacuum"))
|
if (vm.count("vacuum"))
|
||||||
{
|
{
|
||||||
DatabaseCon::Setup dbSetup = setup_DatabaseCon(*config);
|
DatabaseCon::Setup dbSetup = setup_DatabaseCon(*config);
|
||||||
@@ -738,11 +721,6 @@ int run (int argc, char** argv)
|
|||||||
// Block until we get a stop RPC.
|
// Block until we get a stop RPC.
|
||||||
app->run();
|
app->run();
|
||||||
|
|
||||||
// Try to write out some entropy to use the next time we start.
|
|
||||||
auto entropy = getEntropyFile (app->config());
|
|
||||||
if (!entropy.empty ())
|
|
||||||
crypto_prng().save_state(entropy.string ());
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -81,9 +81,6 @@ public:
|
|||||||
/** Returns the full path and filename of the debug log file. */
|
/** Returns the full path and filename of the debug log file. */
|
||||||
boost::filesystem::path getDebugLogFile () const;
|
boost::filesystem::path getDebugLogFile () const;
|
||||||
|
|
||||||
/** Returns the full path and filename of the entropy seed file. */
|
|
||||||
boost::filesystem::path getEntropyFile () const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
boost::filesystem::path CONFIG_FILE;
|
boost::filesystem::path CONFIG_FILE;
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -61,14 +61,6 @@ public:
|
|||||||
void
|
void
|
||||||
mix_entropy (void* buffer = nullptr, std::size_t count = 0);
|
mix_entropy (void* buffer = nullptr, std::size_t count = 0);
|
||||||
|
|
||||||
/** Load entropy from the specified file */
|
|
||||||
void
|
|
||||||
load_state (std::string const& file);
|
|
||||||
|
|
||||||
/** Save entropy to the specified file */
|
|
||||||
void
|
|
||||||
save_state (std::string const& file);
|
|
||||||
|
|
||||||
/** Generate a random integer */
|
/** Generate a random integer */
|
||||||
result_type
|
result_type
|
||||||
operator()();
|
operator()();
|
||||||
|
|||||||
@@ -50,27 +50,6 @@ csprng_engine::~csprng_engine ()
|
|||||||
RAND_cleanup ();
|
RAND_cleanup ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
csprng_engine::load_state (std::string const& file)
|
|
||||||
{
|
|
||||||
if (!file.empty())
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock (mutex_);
|
|
||||||
RAND_load_file (file.c_str (), kilobytes(1));
|
|
||||||
RAND_write_file (file.c_str ());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
csprng_engine::save_state (std::string const& file)
|
|
||||||
{
|
|
||||||
if (!file.empty())
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock (mutex_);
|
|
||||||
RAND_write_file (file.c_str ());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
csprng_engine::mix_entropy (void* buffer, std::size_t count)
|
csprng_engine::mix_entropy (void* buffer, std::size_t count)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -49,74 +49,10 @@ class CryptoPRNG_test : public beast::unit_test::suite
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
testSaveLoad()
|
|
||||||
{
|
|
||||||
testcase ("Save/Load State");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// create a temporary path to write crypto state files
|
|
||||||
beast::temp_dir td;
|
|
||||||
|
|
||||||
auto stateFile = boost::filesystem::path {td.file("cryptostate")};
|
|
||||||
auto& engine = crypto_prng();
|
|
||||||
engine.save_state(stateFile.string());
|
|
||||||
|
|
||||||
size_t size_before_load;
|
|
||||||
std::string data_before_load, data_after_load;
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::system::error_code ec;
|
|
||||||
size_before_load = file_size(stateFile, ec);
|
|
||||||
if(! BEAST_EXPECTS(!ec, ec.message()))
|
|
||||||
return;
|
|
||||||
if(! BEAST_EXPECT(size_before_load > 0))
|
|
||||||
return;
|
|
||||||
|
|
||||||
std::ifstream ifs(
|
|
||||||
stateFile.string(),
|
|
||||||
std::ios::in | std::ios::binary);
|
|
||||||
data_before_load =
|
|
||||||
std::string{std::istreambuf_iterator<char>{ifs}, {}};
|
|
||||||
BEAST_EXPECT(data_before_load.size() == size_before_load);
|
|
||||||
}
|
|
||||||
|
|
||||||
engine.load_state(stateFile.string());
|
|
||||||
|
|
||||||
// load_state actually causes a new state file to be written
|
|
||||||
// ...verify it has changed
|
|
||||||
|
|
||||||
{
|
|
||||||
boost::system::error_code ec;
|
|
||||||
size_t size_after_load = file_size(stateFile, ec);
|
|
||||||
if(! BEAST_EXPECTS(!ec, ec.message()))
|
|
||||||
return;
|
|
||||||
BEAST_EXPECT(size_after_load == size_before_load);
|
|
||||||
|
|
||||||
std::ifstream ifs(
|
|
||||||
stateFile.string(),
|
|
||||||
std::ios::in | std::ios::binary);
|
|
||||||
data_after_load =
|
|
||||||
std::string{std::istreambuf_iterator<char>{ifs}, {}};
|
|
||||||
BEAST_EXPECT(data_after_load.size() == size_after_load);
|
|
||||||
BEAST_EXPECT(data_after_load != data_before_load);
|
|
||||||
}
|
|
||||||
|
|
||||||
// verify the loaded engine works
|
|
||||||
engine();
|
|
||||||
pass();
|
|
||||||
}
|
|
||||||
catch(std::exception&)
|
|
||||||
{
|
|
||||||
fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void run () override
|
void run () override
|
||||||
{
|
{
|
||||||
testGetValues();
|
testGetValues();
|
||||||
testSaveLoad();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user