Browse Source

Merge pull request #2760 from ethereum/fixnonce

Don't use disk for the Nonce - just KISS
cl-refactor
Gav Wood 10 years ago
parent
commit
7faadaf421
  1. 21
      libdevcrypto/Common.cpp
  2. 6
      libdevcrypto/Common.h
  3. 5
      test/TestUtils.cpp
  4. 8
      test/TestUtils.h
  5. 2
      test/libdevcrypto/SecretStore.cpp
  6. 2
      test/libdevcrypto/crypto.cpp

21
libdevcrypto/Common.cpp

@ -308,33 +308,14 @@ h256 crypto::kdf(Secret const& _priv, h256 const& _hash)
return s;
}
string const& Nonce::seedFilePath(string const& _filePath)
{
static mutex x_seedFile;
static string s_seedFile;
Guard l(x_seedFile);
if (s_seedFile.empty())
s_seedFile = _filePath.empty() ? getDataDir() + "/seed" : _filePath;
return s_seedFile;
}
Secret Nonce::next()
{
Guard l(x_value);
if (!m_value)
{
bytesSec b = contentsSec(seedFilePath());
if (b.size() == 32)
b.ref().populate(m_value.writable().ref());
else
m_value = Secret::random();
m_value = Secret::random();
if (!m_value)
BOOST_THROW_EXCEPTION(InvalidState());
// prevent seed reuse if process terminates abnormally
// this might throw
writeFile(seedFilePath(), bytes());
}
m_value = sha3Secure(m_value.ref());
return sha3(~m_value);

6
libdevcrypto/Common.h

@ -203,14 +203,8 @@ public:
/// Returns the next nonce (might be read from a file).
static Secret get() { static Nonce s; return s.next(); }
/// @returns path of the seed file. FOR TESTS ONLY: optionally set path to @_filePath.
static std::string const& seedFilePath(std::string const& _filePath = std::string());
private:
Nonce() = default;
/// Destructor. IO operation may throw.
~Nonce() { if (m_value && next()) dev::writeFile(seedFilePath(), m_value.ref()); }
/// @returns the next nonce.
Secret next();

5
test/TestUtils.cpp

@ -120,8 +120,3 @@ void ParallelClientBaseFixture::enumerateClients(std::function<void(Json::Value
});
}
MoveNonceToTempDir::MoveNonceToTempDir()
{
crypto::Nonce::seedFilePath(m_dir.path() + "/seed");
}

8
test/TestUtils.h

@ -79,13 +79,5 @@ struct JsonRpcFixture: public ClientBaseFixture
};
struct MoveNonceToTempDir
{
MoveNonceToTempDir();
~MoveNonceToTempDir() {}
private:
TransientDirectory m_dir;
};
}
}

2
test/libdevcrypto/SecretStore.cpp

@ -37,8 +37,6 @@ using namespace dev::test;
namespace js = json_spirit;
namespace fs = boost::filesystem;
BOOST_GLOBAL_FIXTURE( MoveNonceToTempDir )
BOOST_AUTO_TEST_SUITE(KeyStore)
BOOST_AUTO_TEST_CASE(basic_tests)

2
test/libdevcrypto/crypto.cpp

@ -40,8 +40,6 @@ using namespace dev::test;
using namespace dev::crypto;
using namespace CryptoPP;
BOOST_GLOBAL_FIXTURE( MoveNonceToTempDir )
BOOST_AUTO_TEST_SUITE(devcrypto)
static Secp256k1PP s_secp256k1;

Loading…
Cancel
Save