mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-29 23:45:51 +00:00
Remove HashMaps
This commit is contained in:
@@ -3293,8 +3293,6 @@
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\types\CryptoIdentifier.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\types\HashMaps.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\types\IdentifierStorage.h">
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\types\IdentifierType.h">
|
||||
|
||||
@@ -4512,9 +4512,6 @@
|
||||
<ClInclude Include="..\..\src\ripple\types\CryptoIdentifier.h">
|
||||
<Filter>ripple\types</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\types\HashMaps.h">
|
||||
<Filter>ripple\types</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\ripple\types\IdentifierStorage.h">
|
||||
<Filter>ripple\types</Filter>
|
||||
</ClInclude>
|
||||
|
||||
@@ -376,9 +376,6 @@ public:
|
||||
|
||||
add (m_ledgerMaster->getPropertySource ());
|
||||
add (*m_rpcHTTPServer);
|
||||
|
||||
// VFALCO TODO remove these once the call is thread safe.
|
||||
HashMaps::getInstance ().initializeNonce <size_t> ();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
@@ -96,21 +96,6 @@ SHAMap::~SHAMap ()
|
||||
logTimedDestroy <SHAMap> (root, "root node");
|
||||
}
|
||||
|
||||
void SHAMapNodeID::setMHash () const
|
||||
{
|
||||
using namespace std;
|
||||
|
||||
std::size_t h = HashMaps::getInstance ().getNonce <std::size_t> ()
|
||||
+ (mDepth * HashMaps::goldenRatio);
|
||||
|
||||
const unsigned int* ptr = reinterpret_cast <const unsigned int*> (mNodeID.begin ());
|
||||
|
||||
for (int i = (mDepth + 7) / 8; i != 0; --i)
|
||||
h = (h * HashMaps::goldenRatio) ^ *ptr++;
|
||||
|
||||
mHash = h;
|
||||
}
|
||||
|
||||
SHAMap::pointer SHAMap::snapShot (bool isMutable)
|
||||
{
|
||||
SHAMap::pointer ret = std::make_shared<SHAMap> (mType,
|
||||
|
||||
@@ -55,6 +55,34 @@ SHAMapNodeID::Masks (int depth)
|
||||
return masks->entry[depth];
|
||||
}
|
||||
|
||||
std::size_t
|
||||
SHAMapNodeID::calculate_hash (uint256 const& node, int depth)
|
||||
{
|
||||
struct HashParams
|
||||
{
|
||||
HashParams ()
|
||||
: golden_ratio (0x9e3779b9)
|
||||
{
|
||||
RandomNumbers::getInstance ().fill (&cookie_value);
|
||||
}
|
||||
|
||||
// The cookie value protects us against algorithmic complexity attacks.
|
||||
std::size_t cookie_value;
|
||||
std::size_t golden_ratio;
|
||||
};
|
||||
|
||||
static beast::static_initializer <HashParams> params;
|
||||
|
||||
std::size_t h = params->cookie_value + (depth * params->golden_ratio);
|
||||
|
||||
auto ptr = reinterpret_cast <const unsigned int*> (node.cbegin ());
|
||||
|
||||
for (int i = (depth + 7) / 8; i != 0; --i)
|
||||
h = (h * params->golden_ratio) ^ *ptr++;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
// canonicalize the hash to a node ID for this depth
|
||||
SHAMapNodeID::SHAMapNodeID (int depth, uint256 const& hash)
|
||||
: mNodeID (hash), mDepth (depth), mHash (0)
|
||||
@@ -76,14 +104,11 @@ SHAMapNodeID::SHAMapNodeID (void const* ptr, int len) : mHash (0)
|
||||
|
||||
std::string SHAMapNodeID::getString () const
|
||||
{
|
||||
static boost::format NodeID ("NodeID(%s,%s)");
|
||||
|
||||
if ((mDepth == 0) && (mNodeID.isZero ()))
|
||||
return "NodeID(root)";
|
||||
|
||||
return str (boost::format (NodeID)
|
||||
% beast::lexicalCastThrow <std::string> (mDepth)
|
||||
% to_string (mNodeID));
|
||||
return "NodeID(" + std::to_string (mDepth) +
|
||||
"," + to_string (mNodeID) + ")";
|
||||
}
|
||||
|
||||
uint256 SHAMapNodeID::getNodeID (int depth, uint256 const& hash)
|
||||
|
||||
@@ -73,7 +73,7 @@ public:
|
||||
size_t getMHash () const
|
||||
{
|
||||
if (mHash == 0)
|
||||
setMHash ();
|
||||
mHash = calculate_hash (mNodeID, mDepth);
|
||||
return mHash;
|
||||
}
|
||||
|
||||
@@ -129,7 +129,9 @@ private:
|
||||
uint256 const&
|
||||
Masks (int depth);
|
||||
|
||||
void setMHash () const;
|
||||
static
|
||||
std::size_t
|
||||
calculate_hash (uint256 const& node, int depth);
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -1,134 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
This file is part of rippled: https://github.com/ripple/rippled
|
||||
Copyright (c) 2012, 2013 Ripple Labs Inc.
|
||||
|
||||
Permission to use, copy, modify, and/or distribute this software for any
|
||||
purpose with or without fee is hereby granted, provided that the above
|
||||
copyright notice and this permission notice appear in all copies.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_TYPES_HASHMAPS_H_INCLUDED
|
||||
#define RIPPLE_TYPES_HASHMAPS_H_INCLUDED
|
||||
|
||||
namespace ripple {
|
||||
|
||||
/** Management helper of hash functions used in hash map containers.
|
||||
|
||||
The nonce is used to prevent attackers from feeding carefully crafted
|
||||
inputs in order to cause denegerate hash map data structures. This is
|
||||
done by seeding the hashing function with a random number generated
|
||||
at program startup.
|
||||
*/
|
||||
class HashMaps
|
||||
{
|
||||
public:
|
||||
/** Golden ratio constant used in hashing functions.
|
||||
|
||||
The magic number is supposed to be 32 random bits, where each is
|
||||
equally likely to be 0 or 1, and with no simple correlation between
|
||||
the bits. A common way to find a string of such bits is to use the
|
||||
binary expansion of an irrational number; in this case, that number
|
||||
is the reciprocal of the golden ratio:
|
||||
|
||||
@code
|
||||
|
||||
phi = (1 + sqrt(5)) / 2
|
||||
2^32 / phi = 0x9e3779b9
|
||||
|
||||
@endcode
|
||||
|
||||
References:
|
||||
|
||||
http://stackoverflow.com/questions/4948780/magic-number-in-boosthash-combine
|
||||
http://burtleburtle.net/bob/hash/doobs.html
|
||||
*/
|
||||
static std::size_t const goldenRatio = 0x9e3779b9;
|
||||
|
||||
/** Retrieve the singleton.
|
||||
|
||||
@return The global instance of the singleton.
|
||||
*/
|
||||
static HashMaps const& getInstance ()
|
||||
{
|
||||
static HashMaps instance;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
/** Instantiate a nonce for a type.
|
||||
|
||||
@note This may be used during program initialization
|
||||
to avoid concurrency issues. Only C++11 provides thread
|
||||
safety guarantees for function-local static objects.
|
||||
*/
|
||||
template <class T>
|
||||
void initializeNonce () const
|
||||
{
|
||||
getNonceHolder <T> ();
|
||||
}
|
||||
|
||||
/** Get the nonce for a type.
|
||||
|
||||
The nonces are generated when they are first used.
|
||||
This code is thread safe.
|
||||
*/
|
||||
template <class T>
|
||||
T getNonce () const
|
||||
{
|
||||
return getNonceHolder <T> ().getNonce ();
|
||||
}
|
||||
|
||||
private:
|
||||
HashMaps () = default;
|
||||
~HashMaps () = default;
|
||||
|
||||
HashMaps (HashMaps const&) = delete;
|
||||
HashMaps& operator= (HashMaps const&) = delete;
|
||||
|
||||
/** Creates and holds a nonce for a type.
|
||||
*/
|
||||
template <class T>
|
||||
class NonceHolder
|
||||
{
|
||||
public:
|
||||
NonceHolder ()
|
||||
{
|
||||
// VFALCO NOTE this can be dangerous if T is an object type
|
||||
RandomNumbers::getInstance ().fill (&m_nonce);
|
||||
}
|
||||
|
||||
inline T getNonce () const
|
||||
{
|
||||
return m_nonce;
|
||||
}
|
||||
|
||||
private:
|
||||
T m_nonce;
|
||||
};
|
||||
|
||||
/** Retrieve the nonce holder for a type.
|
||||
|
||||
@note This routine will be called concurrently.
|
||||
*/
|
||||
template <class T>
|
||||
NonceHolder <T> const& getNonceHolder () const
|
||||
{
|
||||
static NonceHolder <T> nonceHolder;
|
||||
|
||||
return nonceHolder;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -46,7 +46,6 @@
|
||||
#include <ripple/types/strHex.h>
|
||||
#include <ripple/types/UInt160.h>
|
||||
#include <ripple/types/RandomNumbers.h>
|
||||
#include <ripple/types/HashMaps.h>
|
||||
|
||||
#include <ripple/types/IdentifierType.h>
|
||||
#include <ripple/types/IdentifierStorage.h>
|
||||
|
||||
Reference in New Issue
Block a user