mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-03 09:25:51 +00:00
Refactor ProofOfWork
This commit is contained in:
@@ -253,13 +253,13 @@
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ripple_ProofOfWork.cpp">
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ProofOfWork.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ripple_ProofOfWorkFactory.cpp">
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ProofOfWorkFactory.cpp">
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
|
||||
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
|
||||
@@ -1381,6 +1381,7 @@
|
||||
<ClInclude Include="..\..\modules\ripple_app\main\ripple_LocalCredentials.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\main\ripple_RippleMain.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\NetworkOPs.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\PowResult.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_AccountItem.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_AccountItems.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_AccountState.h" />
|
||||
@@ -1388,13 +1389,12 @@
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IFeatures.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IFeeVote.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IHashRouter.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IProofOfWorkFactory.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ProofOfWorkFactory.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IValidations.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_NicknameState.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_Offer.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_OrderBook.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_ProofOfWork.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_ProofOfWorkFactory.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ProofOfWork.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_SerializedLedger.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_SerializedTransaction.h" />
|
||||
<ClInclude Include="..\..\modules\ripple_app\node\ripple_HyperLevelDBBackendFactory.h" />
|
||||
|
||||
@@ -648,12 +648,6 @@
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ripple_OrderBook.cpp">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ripple_ProofOfWork.cpp">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ripple_ProofOfWorkFactory.cpp">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ripple_SerializedLedger.cpp">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClCompile>
|
||||
@@ -873,6 +867,12 @@
|
||||
<ClCompile Include="..\..\modules\ripple_net\rpc\InfoSub.cpp">
|
||||
<Filter>[1] Ripple\ripple_net\rpc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ProofOfWorkFactory.cpp">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\modules\ripple_app\misc\ProofOfWork.cpp">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\modules\ripple_app\ripple_app.h">
|
||||
@@ -1409,9 +1409,6 @@
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IHashRouter.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IProofOfWorkFactory.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_IValidations.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
@@ -1424,12 +1421,6 @@
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_OrderBook.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_ProofOfWork.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_ProofOfWorkFactory.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ripple_SerializedLedger.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
@@ -1694,6 +1685,15 @@
|
||||
<ClInclude Include="..\..\modules\ripple_net\basics\impl\RPCServerImp.h">
|
||||
<Filter>[1] Ripple\ripple_net\basics\impl</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\PowResult.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ProofOfWork.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\modules\ripple_app\misc\ProofOfWorkFactory.h">
|
||||
<Filter>[1] Ripple\ripple_app\misc</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<CustomBuild Include="..\..\modules\ripple_data\protocol\ripple.proto">
|
||||
|
||||
@@ -174,7 +174,7 @@ public:
|
||||
, mHashRouter (IHashRouter::New (IHashRouter::getDefaultHoldTime ()))
|
||||
, mValidations (IValidations::New ())
|
||||
, mUNL (UniqueNodeList::New ())
|
||||
, mProofOfWorkFactory (IProofOfWorkFactory::New ())
|
||||
, mProofOfWorkFactory (ProofOfWorkFactory::New ())
|
||||
, m_loadManager (LoadManager::New ())
|
||||
// VFALCO End new stuff
|
||||
// VFALCO TODO replace all NULL with nullptr
|
||||
@@ -322,7 +322,7 @@ public:
|
||||
return *mUNL;
|
||||
}
|
||||
|
||||
IProofOfWorkFactory& getProofOfWorkFactory ()
|
||||
ProofOfWorkFactory& getProofOfWorkFactory ()
|
||||
{
|
||||
return *mProofOfWorkFactory;
|
||||
}
|
||||
@@ -698,7 +698,7 @@ private:
|
||||
ScopedPointer <IHashRouter> mHashRouter;
|
||||
ScopedPointer <IValidations> mValidations;
|
||||
ScopedPointer <UniqueNodeList> mUNL;
|
||||
ScopedPointer <IProofOfWorkFactory> mProofOfWorkFactory;
|
||||
ScopedPointer <ProofOfWorkFactory> mProofOfWorkFactory;
|
||||
ScopedPointer <Peers> m_peers;
|
||||
ScopedPointer <LoadManager> m_loadManager;
|
||||
ScopedPointer <PeerDoor> m_peerDoor;
|
||||
|
||||
@@ -13,7 +13,6 @@ class IFeeVote;
|
||||
class IHashRouter;
|
||||
class ILoadFeeTrack;
|
||||
class Peers;
|
||||
class IProofOfWorkFactory;
|
||||
class UniqueNodeList;
|
||||
class IValidations;
|
||||
class Validators;
|
||||
@@ -25,6 +24,7 @@ class LedgerMaster;
|
||||
class LoadManager;
|
||||
class NetworkOPs;
|
||||
class OrderBookDB;
|
||||
class ProofOfWorkFactory;
|
||||
class SerializedLedgerEntry;
|
||||
class TransactionMaster;
|
||||
class TXQueue;
|
||||
@@ -84,7 +84,7 @@ public:
|
||||
virtual ILoadFeeTrack& getFeeTrack () = 0;
|
||||
virtual LoadManager& getLoadManager () = 0;
|
||||
virtual Peers& getPeers () = 0;
|
||||
virtual IProofOfWorkFactory& getProofOfWorkFactory () = 0;
|
||||
virtual ProofOfWorkFactory& getProofOfWorkFactory () = 0;
|
||||
virtual UniqueNodeList& getUNL () = 0;
|
||||
virtual IValidations& getValidations () = 0;
|
||||
|
||||
|
||||
20
modules/ripple_app/misc/PowResult.h
Normal file
20
modules/ripple_app/misc/PowResult.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_POWRESULT_H_INCLUDED
|
||||
#define RIPPLE_POWRESULT_H_INCLUDED
|
||||
|
||||
enum PowResult
|
||||
{
|
||||
powOK = 0,
|
||||
powREUSED = 1, // already submitted
|
||||
powBADNONCE = 2, // you didn't solve it
|
||||
powEXPIRED = 3, // time is up
|
||||
powCORRUPT = 4,
|
||||
powTOOEASY = 5, // the difficulty increased too much while you solved it
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -6,42 +6,8 @@
|
||||
|
||||
SETUP_LOG (ProofOfWork)
|
||||
|
||||
bool powResultInfo (POWResult powCode, std::string& strToken, std::string& strHuman)
|
||||
{
|
||||
static struct
|
||||
{
|
||||
POWResult powCode;
|
||||
const char* cpToken;
|
||||
const char* cpHuman;
|
||||
} powResultInfoA[] =
|
||||
{
|
||||
{ powREUSED, "powREUSED", "Proof-of-work has already been used." },
|
||||
{ powBADNONCE, "powBADNONCE", "The solution does not meet the required difficulty." },
|
||||
{ powEXPIRED, "powEXPIRED", "Token is expired." },
|
||||
{ powCORRUPT, "powCORRUPT", "Invalid token." },
|
||||
{ powTOOEASY, "powTOOEASY", "Difficulty has increased since token was issued." },
|
||||
|
||||
{ powOK, "powOK", "Valid proof-of-work." },
|
||||
};
|
||||
|
||||
int iIndex = NUMBER (powResultInfoA);
|
||||
|
||||
while (iIndex-- && powResultInfoA[iIndex].powCode != powCode)
|
||||
;
|
||||
|
||||
if (iIndex >= 0)
|
||||
{
|
||||
strToken = powResultInfoA[iIndex].cpToken;
|
||||
strHuman = powResultInfoA[iIndex].cpHuman;
|
||||
}
|
||||
|
||||
return iIndex >= 0;
|
||||
}
|
||||
|
||||
// VFALCO TODO Move these to a header because they are used by ripple_ProofOfWorkFactory.cpp
|
||||
// VFALCO TODO Move these to a header
|
||||
const uint256 ProofOfWork::sMinTarget ("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
||||
const int ProofOfWork::sMaxIterations (1 << 23);
|
||||
const int ProofOfWork::sMaxDifficulty (30);
|
||||
|
||||
ProofOfWork::ProofOfWork (const std::string& token,
|
||||
int iterations,
|
||||
@@ -70,7 +36,7 @@ ProofOfWork::ProofOfWork (const std::string& token)
|
||||
|
||||
bool ProofOfWork::isValid () const
|
||||
{
|
||||
if ((mIterations <= sMaxIterations) && (mTarget >= sMinTarget))
|
||||
if ((mIterations <= kMaxIterations) && (mTarget >= sMinTarget))
|
||||
return true;
|
||||
|
||||
WriteLog (lsWARNING, ProofOfWork) << "Invalid PoW: " << mIterations << ", " << mTarget;
|
||||
@@ -80,10 +46,10 @@ bool ProofOfWork::isValid () const
|
||||
uint64 ProofOfWork::getDifficulty (uint256 const& target, int iterations)
|
||||
{
|
||||
// calculate the approximate number of hashes required to solve this proof of work
|
||||
if ((iterations > sMaxIterations) || (target < sMinTarget))
|
||||
if ((iterations > kMaxIterations) || (target < sMinTarget))
|
||||
{
|
||||
WriteLog (lsINFO, ProofOfWork) << "Iterations:" << iterations;
|
||||
WriteLog (lsINFO, ProofOfWork) << "MaxIterat: " << sMaxIterations;
|
||||
WriteLog (lsINFO, ProofOfWork) << "MaxIterat: " << kMaxIterations;
|
||||
WriteLog (lsINFO, ProofOfWork) << "Target: " << target;
|
||||
WriteLog (lsINFO, ProofOfWork) << "MinTarget: " << sMinTarget;
|
||||
throw std::runtime_error ("invalid proof of work target/iteration");
|
||||
@@ -149,7 +115,7 @@ uint256 ProofOfWork::solve (int maxIterations) const
|
||||
|
||||
bool ProofOfWork::checkSolution (uint256 const& solution) const
|
||||
{
|
||||
if (mIterations > sMaxIterations)
|
||||
if (mIterations > kMaxIterations)
|
||||
return false;
|
||||
|
||||
std::vector<uint256> buf1;
|
||||
@@ -179,3 +145,35 @@ bool ProofOfWork::validateToken (const std::string& strToken)
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
bool ProofOfWork::calcResultInfo (PowResult powCode, std::string& strToken, std::string& strHuman)
|
||||
{
|
||||
static struct
|
||||
{
|
||||
PowResult powCode;
|
||||
const char* cpToken;
|
||||
const char* cpHuman;
|
||||
} powResultInfoA[] =
|
||||
{
|
||||
{ powREUSED, "powREUSED", "Proof-of-work has already been used." },
|
||||
{ powBADNONCE, "powBADNONCE", "The solution does not meet the required difficulty." },
|
||||
{ powEXPIRED, "powEXPIRED", "Token is expired." },
|
||||
{ powCORRUPT, "powCORRUPT", "Invalid token." },
|
||||
{ powTOOEASY, "powTOOEASY", "Difficulty has increased since token was issued." },
|
||||
|
||||
{ powOK, "powOK", "Valid proof-of-work." },
|
||||
};
|
||||
|
||||
int iIndex = NUMBER (powResultInfoA);
|
||||
|
||||
while (iIndex-- && powResultInfoA[iIndex].powCode != powCode)
|
||||
;
|
||||
|
||||
if (iIndex >= 0)
|
||||
{
|
||||
strToken = powResultInfoA[iIndex].cpToken;
|
||||
strHuman = powResultInfoA[iIndex].cpHuman;
|
||||
}
|
||||
|
||||
return iIndex >= 0;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,10 @@
|
||||
class ProofOfWork : LeakChecked <ProofOfWork>
|
||||
{
|
||||
public:
|
||||
static const int sMaxDifficulty;
|
||||
enum
|
||||
{
|
||||
kMaxIterations = (1 << 23)
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr <ProofOfWork> pointer;
|
||||
|
||||
@@ -23,7 +26,7 @@ public:
|
||||
|
||||
bool isValid () const;
|
||||
|
||||
uint256 solve (int maxIterations = 2 * sMaxIterations) const;
|
||||
uint256 solve (int maxIterations = 2 * kMaxIterations) const;
|
||||
bool checkSolution (uint256 const& solution) const;
|
||||
|
||||
const std::string& getToken () const
|
||||
@@ -45,6 +48,8 @@ public:
|
||||
|
||||
static bool validateToken (const std::string& strToken);
|
||||
|
||||
static bool calcResultInfo (PowResult powCode, std::string& strToken, std::string& strHuman);
|
||||
|
||||
private:
|
||||
std::string mToken;
|
||||
uint256 mChallenge;
|
||||
@@ -52,7 +57,7 @@ private:
|
||||
int mIterations;
|
||||
|
||||
static const uint256 sMinTarget;
|
||||
static const int sMaxIterations;
|
||||
static const int maxIterations;
|
||||
};
|
||||
|
||||
#endif
|
||||
409
modules/ripple_app/misc/ProofOfWorkFactory.cpp
Normal file
409
modules/ripple_app/misc/ProofOfWorkFactory.cpp
Normal file
@@ -0,0 +1,409 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
class ProofOfWorkFactoryImp
|
||||
: public ProofOfWorkFactory
|
||||
, public LeakChecked <ProofOfWorkFactoryImp>
|
||||
{
|
||||
public:
|
||||
typedef boost::bimap< boost::bimaps::multiset_of<time_t>, boost::bimaps::unordered_set_of<uint256> > powMap_t;
|
||||
typedef powMap_t::value_type powMap_vt;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
ProofOfWorkFactoryImp ()
|
||||
: mLock (this, "PoWFactory", __FILE__, __LINE__)
|
||||
, mValidTime (180)
|
||||
{
|
||||
setDifficulty (1);
|
||||
RandomNumbers::getInstance ().fillBytes (mSecret.begin (), mSecret.size ());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
enum
|
||||
{
|
||||
numPowEntries = kMaxDifficulty + 1
|
||||
};
|
||||
|
||||
struct PowEntry
|
||||
{
|
||||
const char* target;
|
||||
int iterations;
|
||||
};
|
||||
|
||||
typedef std::vector <PowEntry> PowEntries;
|
||||
|
||||
static PowEntries const& getPowEntries ()
|
||||
{
|
||||
struct StaticPowEntries
|
||||
{
|
||||
StaticPowEntries ()
|
||||
{
|
||||
// VFALCO TODO Make this array know its own size.
|
||||
//
|
||||
PowEntry entries [numPowEntries] =
|
||||
{
|
||||
// target iterations hashes memory
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 65536 }, // 1451874, 2 MB
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 2177811, 3 MB
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 3538944, 3 MB
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 4355623, 6 MB
|
||||
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 131072}, // 4718592, 4 MB
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 5807497, 8 MB
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 7077888, 6 MB
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 9437184, 8 MB
|
||||
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 14155776, 12MB
|
||||
{ "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 28311552, 12MB
|
||||
{ "00CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 92919965, 8 MB
|
||||
{ "00CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 139379948, 12MB
|
||||
|
||||
{ "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 150994944, 8 MB
|
||||
{ "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 226492416, 12MB
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 49152 }, // 278759896, 1.5MB
|
||||
{ "003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 301989888, 8 MB
|
||||
|
||||
{ "003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 452984832, 12MB
|
||||
{ "0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 905969664, 3 MB
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 1115039586, 6 MB
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 1486719448 8 MB
|
||||
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 2230079172 12MB
|
||||
{ "0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 2415919104, 8 MB
|
||||
{ "0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 3623878656, 12MB
|
||||
{ "0003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 7247757312, 12MB
|
||||
|
||||
{ "0000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 23787511177, 8 MB
|
||||
{ "0000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 35681266766, 12MB
|
||||
{ "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 131072}, // 38654705664, 4 MB
|
||||
{ "00007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 38654705664, 8 MB
|
||||
|
||||
{ "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 57982058496, 6 MB
|
||||
{ "00007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 57982058496, 12MB
|
||||
{ "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 77309411328, 8 MB
|
||||
};
|
||||
|
||||
data.reserve (numPowEntries);
|
||||
|
||||
for (int i = 0; i < numPowEntries; ++i)
|
||||
data.push_back (entries [i]);
|
||||
}
|
||||
|
||||
std::vector <PowEntry> data;
|
||||
};
|
||||
|
||||
static StaticPowEntries list;
|
||||
|
||||
return list.data;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
static int getPowEntry (uint256 const& target, int iterations)
|
||||
{
|
||||
PowEntries const& entries (getPowEntries ());
|
||||
|
||||
for (int i = 0; i < numPowEntries; ++i)
|
||||
{
|
||||
if (entries [i].iterations == iterations)
|
||||
{
|
||||
uint256 t;
|
||||
t.SetHex (entries [i].target);
|
||||
|
||||
if (t == target)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
ProofOfWork getProof ()
|
||||
{
|
||||
// challenge - target - iterations - time - validator
|
||||
static boost::format f ("%s-%s-%d-%d");
|
||||
|
||||
int now = static_cast<int> (time (NULL) / 4);
|
||||
|
||||
uint256 challenge;
|
||||
RandomNumbers::getInstance ().fillBytes (challenge.begin (), challenge.size ());
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
std::string s = boost::str (boost::format (f) % challenge.GetHex () % mTarget.GetHex () % mIterations % now);
|
||||
std::string c = mSecret.GetHex () + s;
|
||||
s += "-" + Serializer::getSHA512Half (c).GetHex ();
|
||||
|
||||
return ProofOfWork (s, mIterations, challenge, mTarget);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
PowResult checkProof (const std::string& token, uint256 const& solution)
|
||||
{
|
||||
// VFALCO COmmented this out because Dave said it wasn't used
|
||||
// and also we dont have the lexicalCast from a vector of strings to a time_t
|
||||
|
||||
// challenge - target - iterations - time - validator
|
||||
|
||||
std::vector<std::string> fields;
|
||||
boost::split (fields, token, boost::algorithm::is_any_of ("-"));
|
||||
|
||||
if (fields.size () != 5)
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " is corrupt";
|
||||
return powCORRUPT;
|
||||
}
|
||||
|
||||
std::string v = mSecret.GetHex () + fields[0] + "-" + fields[1] + "-" + fields[2] + "-" + fields[3];
|
||||
|
||||
if (fields[4] != Serializer::getSHA512Half (v).GetHex ())
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has a bad token";
|
||||
return powCORRUPT;
|
||||
}
|
||||
|
||||
uint256 challenge, target;
|
||||
challenge.SetHex (fields[0]);
|
||||
target.SetHex (fields[1]);
|
||||
|
||||
time_t t;
|
||||
#if 0
|
||||
// Broken with lexicalCast<> changes
|
||||
t = lexicalCast <time_t> (fields[3]);
|
||||
#else
|
||||
t = static_cast <time_t> (lexicalCast <uint64> (fields [3]));
|
||||
#endif
|
||||
|
||||
time_t now = time (NULL);
|
||||
|
||||
int iterations = lexicalCast <int> (fields[2]);
|
||||
|
||||
{
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if ((t * 4) > (now + mValidTime))
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has expired";
|
||||
return powEXPIRED;
|
||||
}
|
||||
|
||||
if (((iterations != mIterations) || (target != mTarget)) && getPowEntry (target, iterations) < (mPowEntry - 2))
|
||||
{
|
||||
// difficulty has increased more than two times since PoW requested
|
||||
WriteLog (lsINFO, ProofOfWork) << "Difficulty has increased since PoW requested";
|
||||
return powTOOEASY;
|
||||
}
|
||||
}
|
||||
|
||||
ProofOfWork pow (token, iterations, challenge, target);
|
||||
|
||||
if (!pow.checkSolution (solution))
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has a bad nonce";
|
||||
return powBADNONCE;
|
||||
}
|
||||
|
||||
{
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if (!mSolvedChallenges.insert (powMap_vt (now, challenge)).second)
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has been reused";
|
||||
return powREUSED;
|
||||
}
|
||||
}
|
||||
|
||||
return powOK;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void sweep ()
|
||||
{
|
||||
time_t expire = time (NULL) - mValidTime;
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
do
|
||||
{
|
||||
powMap_t::left_map::iterator it = mSolvedChallenges.left.begin ();
|
||||
|
||||
if (it == mSolvedChallenges.left.end ())
|
||||
return;
|
||||
|
||||
if (it->first >= expire)
|
||||
return;
|
||||
|
||||
mSolvedChallenges.left.erase (it);
|
||||
}
|
||||
while (1);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void loadHigh ()
|
||||
{
|
||||
time_t now = time (NULL);
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if (mLastDifficultyChange == now)
|
||||
return;
|
||||
|
||||
if (mPowEntry == 30)
|
||||
return;
|
||||
|
||||
++mPowEntry;
|
||||
mLastDifficultyChange = now;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void loadLow ()
|
||||
{
|
||||
time_t now = time (NULL);
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if (mLastDifficultyChange == now)
|
||||
return;
|
||||
|
||||
if (mPowEntry == 0)
|
||||
return;
|
||||
|
||||
--mPowEntry;
|
||||
mLastDifficultyChange = now;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void setDifficulty (int i)
|
||||
{
|
||||
assert ((i >= 0) && (i <= kMaxDifficulty));
|
||||
time_t now = time (NULL);
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
mPowEntry = i;
|
||||
PowEntries const& entries (getPowEntries ());
|
||||
mIterations = entries [i].iterations;
|
||||
mTarget.SetHex (entries [i].target);
|
||||
mLastDifficultyChange = now;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
uint64 getDifficulty ()
|
||||
{
|
||||
return ProofOfWork::getDifficulty (mTarget, mIterations);
|
||||
}
|
||||
|
||||
uint256 const& getSecret () const
|
||||
{
|
||||
return mSecret;
|
||||
}
|
||||
|
||||
void setSecret (uint256 const& secret)
|
||||
{
|
||||
mSecret = secret;
|
||||
}
|
||||
|
||||
private:
|
||||
typedef RippleMutex LockType;
|
||||
typedef LockType::ScopedLockType ScopedLockType;
|
||||
LockType mLock;
|
||||
|
||||
uint256 mSecret;
|
||||
int mIterations;
|
||||
uint256 mTarget;
|
||||
time_t mLastDifficultyChange;
|
||||
int mValidTime;
|
||||
int mPowEntry;
|
||||
|
||||
powMap_t mSolvedChallenges;
|
||||
};
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
ProofOfWorkFactory* ProofOfWorkFactory::New ()
|
||||
{
|
||||
ScopedPointer <ProofOfWorkFactory> object (new ProofOfWorkFactoryImp);
|
||||
return object.release ();
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class ProofOfWorkTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
ProofOfWorkTests () : UnitTest ("ProofOfWork", "ripple", runManual)
|
||||
{
|
||||
}
|
||||
|
||||
void runTest ()
|
||||
{
|
||||
using namespace ripple;
|
||||
|
||||
ProofOfWorkFactoryImp gen;
|
||||
ProofOfWork pow = gen.getProof ();
|
||||
|
||||
String s;
|
||||
|
||||
s << "solve difficulty " << String (pow.getDifficulty ());
|
||||
beginTestCase ("solve");
|
||||
|
||||
uint256 solution = pow.solve (16777216);
|
||||
|
||||
expect (! solution.isZero (), "Should be solved");
|
||||
|
||||
expect (pow.checkSolution (solution), "Should be checked");
|
||||
|
||||
// Why is this emitted?
|
||||
//WriteLog (lsDEBUG, ProofOfWork) << "A bad nonce error is expected";
|
||||
|
||||
PowResult r = gen.checkProof (pow.getToken (), uint256 ());
|
||||
|
||||
expect (r == powBADNONCE, "Should show bad nonce for empty solution");
|
||||
|
||||
expect (gen.checkProof (pow.getToken (), solution) == powOK, "Solution should check with issuer");
|
||||
|
||||
//WriteLog (lsDEBUG, ProofOfWork) << "A reused nonce error is expected";
|
||||
|
||||
expect (gen.checkProof (pow.getToken (), solution) == powREUSED, "Reuse solution should be detected");
|
||||
|
||||
#ifdef SOLVE_POWS
|
||||
|
||||
for (int i = 0; i < 12; ++i)
|
||||
{
|
||||
gen.setDifficulty (i);
|
||||
ProofOfWork pow = gen.getProof ();
|
||||
WriteLog (lsINFO, ProofOfWork) << "Level: " << i << ", Estimated difficulty: " << pow.getDifficulty ();
|
||||
uint256 solution = pow.solve (131072);
|
||||
|
||||
if (solution.isZero ())
|
||||
{
|
||||
//WriteLog (lsINFO, ProofOfWork) << "Giving up";
|
||||
}
|
||||
else
|
||||
{
|
||||
//WriteLog (lsINFO, ProofOfWork) << "Solution found";
|
||||
|
||||
if (gen.checkProof (pow.getToken (), solution) != powOK)
|
||||
{
|
||||
//WriteLog (lsFATAL, ProofOfWork) << "Solution fails";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
static ProofOfWorkTests proofOfWorkTests;
|
||||
41
modules/ripple_app/misc/ProofOfWorkFactory.h
Normal file
41
modules/ripple_app/misc/ProofOfWorkFactory.h
Normal file
@@ -0,0 +1,41 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_PROOFOFWORKFACTORY_H_INCLUDED
|
||||
#define RIPPLE_PROOFOFWORKFACTORY_H_INCLUDED
|
||||
|
||||
class ProofOfWorkFactory
|
||||
{
|
||||
public:
|
||||
enum
|
||||
{
|
||||
kMaxDifficulty = 30,
|
||||
};
|
||||
|
||||
static ProofOfWorkFactory* New ();
|
||||
|
||||
virtual ~ProofOfWorkFactory () { }
|
||||
|
||||
virtual ProofOfWork getProof () = 0;
|
||||
|
||||
virtual PowResult checkProof (const std::string& token, uint256 const& solution) = 0;
|
||||
|
||||
virtual uint64 getDifficulty () = 0;
|
||||
|
||||
virtual void setDifficulty (int i) = 0;
|
||||
|
||||
virtual void loadHigh () = 0;
|
||||
|
||||
virtual void loadLow () = 0;
|
||||
|
||||
virtual void sweep () = 0;
|
||||
|
||||
virtual uint256 const& getSecret () const = 0;
|
||||
|
||||
virtual void setSecret (uint256 const& secret) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_IPROOFOFWORKFACTORY_H
|
||||
#define RIPPLE_IPROOFOFWORKFACTORY_H
|
||||
|
||||
enum POWResult
|
||||
{
|
||||
powOK = 0,
|
||||
powREUSED = 1, // already submitted
|
||||
powBADNONCE = 2, // you didn't solve it
|
||||
powEXPIRED = 3, // time is up
|
||||
powCORRUPT = 4,
|
||||
powTOOEASY = 5, // the difficulty increased too much while you solved it
|
||||
};
|
||||
|
||||
// VFALCO TODO move this to the class as a static member and rename it
|
||||
bool powResultInfo (POWResult powCode, std::string& strToken, std::string& strHuman);
|
||||
|
||||
class IProofOfWorkFactory : LeakChecked <IProofOfWorkFactory>
|
||||
{
|
||||
public:
|
||||
typedef boost::bimap< boost::bimaps::multiset_of<time_t>, boost::bimaps::unordered_set_of<uint256> > powMap_t;
|
||||
typedef powMap_t::value_type powMap_vt;
|
||||
|
||||
public:
|
||||
static IProofOfWorkFactory* New ();
|
||||
|
||||
virtual ~IProofOfWorkFactory () { }
|
||||
|
||||
// VFALCO TODO which members can be const?
|
||||
|
||||
virtual ProofOfWork getProof () = 0;
|
||||
|
||||
virtual POWResult checkProof (const std::string& token, uint256 const& solution) = 0;
|
||||
|
||||
virtual uint64 getDifficulty () = 0;
|
||||
|
||||
virtual void setDifficulty (int i) = 0;
|
||||
|
||||
virtual void loadHigh () = 0;
|
||||
|
||||
virtual void loadLow () = 0;
|
||||
|
||||
virtual void sweep () = 0;
|
||||
|
||||
virtual uint256 const& getSecret () const = 0;
|
||||
|
||||
virtual void setSecret (uint256 const& secret) = 0;
|
||||
|
||||
public:
|
||||
static int getPowEntry (uint256 const& target, int iterations);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// vim:ts=4
|
||||
@@ -1,314 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
ProofOfWorkFactory::ProofOfWorkFactory ()
|
||||
: mLock (this, "PoWFactory", __FILE__, __LINE__)
|
||||
, mValidTime (180)
|
||||
{
|
||||
setDifficulty (1);
|
||||
RandomNumbers::getInstance ().fillBytes (mSecret.begin (), mSecret.size ());
|
||||
}
|
||||
|
||||
ProofOfWork ProofOfWorkFactory::getProof ()
|
||||
{
|
||||
// challenge - target - iterations - time - validator
|
||||
static boost::format f ("%s-%s-%d-%d");
|
||||
|
||||
int now = static_cast<int> (time (NULL) / 4);
|
||||
|
||||
uint256 challenge;
|
||||
RandomNumbers::getInstance ().fillBytes (challenge.begin (), challenge.size ());
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
std::string s = boost::str (boost::format (f) % challenge.GetHex () % mTarget.GetHex () % mIterations % now);
|
||||
std::string c = mSecret.GetHex () + s;
|
||||
s += "-" + Serializer::getSHA512Half (c).GetHex ();
|
||||
|
||||
return ProofOfWork (s, mIterations, challenge, mTarget);
|
||||
}
|
||||
|
||||
POWResult ProofOfWorkFactory::checkProof (const std::string& token, uint256 const& solution)
|
||||
{
|
||||
// VFALCO COmmented this out because Dave said it wasn't used
|
||||
// and also we dont have the lexicalCast from a vector of strings to a time_t
|
||||
|
||||
// challenge - target - iterations - time - validator
|
||||
|
||||
std::vector<std::string> fields;
|
||||
boost::split (fields, token, boost::algorithm::is_any_of ("-"));
|
||||
|
||||
if (fields.size () != 5)
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " is corrupt";
|
||||
return powCORRUPT;
|
||||
}
|
||||
|
||||
std::string v = mSecret.GetHex () + fields[0] + "-" + fields[1] + "-" + fields[2] + "-" + fields[3];
|
||||
|
||||
if (fields[4] != Serializer::getSHA512Half (v).GetHex ())
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has a bad token";
|
||||
return powCORRUPT;
|
||||
}
|
||||
|
||||
uint256 challenge, target;
|
||||
challenge.SetHex (fields[0]);
|
||||
target.SetHex (fields[1]);
|
||||
|
||||
time_t t;
|
||||
#if 0
|
||||
// Broken with lexicalCast<> changes
|
||||
t = lexicalCast <time_t> (fields[3]);
|
||||
#else
|
||||
t = static_cast <time_t> (lexicalCast <uint64> (fields [3]));
|
||||
#endif
|
||||
|
||||
time_t now = time (NULL);
|
||||
|
||||
int iterations = lexicalCast <int> (fields[2]);
|
||||
|
||||
{
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if ((t * 4) > (now + mValidTime))
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has expired";
|
||||
return powEXPIRED;
|
||||
}
|
||||
|
||||
if (((iterations != mIterations) || (target != mTarget)) && getPowEntry (target, iterations) < (mPowEntry - 2))
|
||||
{
|
||||
// difficulty has increased more than two times since PoW requested
|
||||
WriteLog (lsINFO, ProofOfWork) << "Difficulty has increased since PoW requested";
|
||||
return powTOOEASY;
|
||||
}
|
||||
}
|
||||
|
||||
ProofOfWork pow (token, iterations, challenge, target);
|
||||
|
||||
if (!pow.checkSolution (solution))
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has a bad nonce";
|
||||
return powBADNONCE;
|
||||
}
|
||||
|
||||
{
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if (!mSolvedChallenges.insert (powMap_vt (now, challenge)).second)
|
||||
{
|
||||
WriteLog (lsDEBUG, ProofOfWork) << "PoW " << token << " has been reused";
|
||||
return powREUSED;
|
||||
}
|
||||
}
|
||||
|
||||
return powOK;
|
||||
}
|
||||
|
||||
void ProofOfWorkFactory::sweep ()
|
||||
{
|
||||
time_t expire = time (NULL) - mValidTime;
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
do
|
||||
{
|
||||
powMap_t::left_map::iterator it = mSolvedChallenges.left.begin ();
|
||||
|
||||
if (it == mSolvedChallenges.left.end ())
|
||||
return;
|
||||
|
||||
if (it->first >= expire)
|
||||
return;
|
||||
|
||||
mSolvedChallenges.left.erase (it);
|
||||
}
|
||||
while (1);
|
||||
}
|
||||
|
||||
void ProofOfWorkFactory::loadHigh ()
|
||||
{
|
||||
time_t now = time (NULL);
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if (mLastDifficultyChange == now)
|
||||
return;
|
||||
|
||||
if (mPowEntry == 30)
|
||||
return;
|
||||
|
||||
++mPowEntry;
|
||||
mLastDifficultyChange = now;
|
||||
}
|
||||
|
||||
void ProofOfWorkFactory::loadLow ()
|
||||
{
|
||||
time_t now = time (NULL);
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
|
||||
if (mLastDifficultyChange == now)
|
||||
return;
|
||||
|
||||
if (mPowEntry == 0)
|
||||
return;
|
||||
|
||||
--mPowEntry;
|
||||
mLastDifficultyChange = now;
|
||||
}
|
||||
|
||||
struct PowEntry
|
||||
{
|
||||
const char* target;
|
||||
int iterations;
|
||||
};
|
||||
|
||||
PowEntry PowEntries[ProofOfWork::sMaxDifficulty + 1] =
|
||||
{
|
||||
// target iterations hashes memory
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 65536 }, // 1451874, 2 MB
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 2177811, 3 MB
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 3538944, 3 MB
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 4355623, 6 MB
|
||||
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 131072}, // 4718592, 4 MB
|
||||
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 5807497, 8 MB
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 7077888, 6 MB
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 9437184, 8 MB
|
||||
|
||||
{ "07FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 14155776, 12MB
|
||||
{ "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 28311552, 12MB
|
||||
{ "00CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 92919965, 8 MB
|
||||
{ "00CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 139379948, 12MB
|
||||
|
||||
{ "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 150994944, 8 MB
|
||||
{ "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 226492416, 12MB
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 49152 }, // 278759896, 1.5MB
|
||||
{ "003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 301989888, 8 MB
|
||||
|
||||
{ "003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 452984832, 12MB
|
||||
{ "0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 905969664, 3 MB
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 1115039586, 6 MB
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 1486719448 8 MB
|
||||
|
||||
{ "000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 2230079172 12MB
|
||||
{ "0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 2415919104, 8 MB
|
||||
{ "0007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 3623878656, 12MB
|
||||
{ "0003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 7247757312, 12MB
|
||||
|
||||
{ "0000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 23787511177, 8 MB
|
||||
{ "0000CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 35681266766, 12MB
|
||||
{ "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 131072}, // 38654705664, 4 MB
|
||||
{ "00007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 38654705664, 8 MB
|
||||
|
||||
{ "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 196608}, // 57982058496, 6 MB
|
||||
{ "00007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 393216}, // 57982058496, 12MB
|
||||
{ "00003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 262144}, // 77309411328, 8 MB
|
||||
};
|
||||
|
||||
int ProofOfWorkFactory::getPowEntry (uint256 const& target, int iterations)
|
||||
{
|
||||
for (int i = 0; i < 31; ++i)
|
||||
if (PowEntries[i].iterations == iterations)
|
||||
{
|
||||
uint256 t;
|
||||
t.SetHex (PowEntries[i].target);
|
||||
|
||||
if (t == target)
|
||||
return i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
void ProofOfWorkFactory::setDifficulty (int i)
|
||||
{
|
||||
assert ((i >= 0) && (i <= ProofOfWork::sMaxDifficulty));
|
||||
time_t now = time (NULL);
|
||||
|
||||
ScopedLockType sl (mLock, __FILE__, __LINE__);
|
||||
mPowEntry = i;
|
||||
mIterations = PowEntries[i].iterations;
|
||||
mTarget.SetHex (PowEntries[i].target);
|
||||
mLastDifficultyChange = now;
|
||||
}
|
||||
|
||||
IProofOfWorkFactory* IProofOfWorkFactory::New ()
|
||||
{
|
||||
return new ProofOfWorkFactory;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
class ProofOfWorkTests : public UnitTest
|
||||
{
|
||||
public:
|
||||
ProofOfWorkTests () : UnitTest ("ProofOfWork", "ripple", runManual)
|
||||
{
|
||||
}
|
||||
|
||||
void runTest ()
|
||||
{
|
||||
using namespace ripple;
|
||||
|
||||
ProofOfWorkFactory gen;
|
||||
ProofOfWork pow = gen.getProof ();
|
||||
|
||||
String s;
|
||||
|
||||
s << "solve difficulty " << String (pow.getDifficulty ());
|
||||
beginTestCase ("solve");
|
||||
|
||||
uint256 solution = pow.solve (16777216);
|
||||
|
||||
expect (! solution.isZero (), "Should be solved");
|
||||
|
||||
expect (pow.checkSolution (solution), "Should be checked");
|
||||
|
||||
// Why is this emitted?
|
||||
//WriteLog (lsDEBUG, ProofOfWork) << "A bad nonce error is expected";
|
||||
|
||||
POWResult r = gen.checkProof (pow.getToken (), uint256 ());
|
||||
|
||||
expect (r == powBADNONCE, "Should show bad nonce for empty solution");
|
||||
|
||||
expect (gen.checkProof (pow.getToken (), solution) == powOK, "Solution should check with issuer");
|
||||
|
||||
//WriteLog (lsDEBUG, ProofOfWork) << "A reused nonce error is expected";
|
||||
|
||||
expect (gen.checkProof (pow.getToken (), solution) == powREUSED, "Reuse solution should be detected");
|
||||
|
||||
#ifdef SOLVE_POWS
|
||||
|
||||
for (int i = 0; i < 12; ++i)
|
||||
{
|
||||
gen.setDifficulty (i);
|
||||
ProofOfWork pow = gen.getProof ();
|
||||
WriteLog (lsINFO, ProofOfWork) << "Level: " << i << ", Estimated difficulty: " << pow.getDifficulty ();
|
||||
uint256 solution = pow.solve (131072);
|
||||
|
||||
if (solution.isZero ())
|
||||
{
|
||||
//WriteLog (lsINFO, ProofOfWork) << "Giving up";
|
||||
}
|
||||
else
|
||||
{
|
||||
//WriteLog (lsINFO, ProofOfWork) << "Solution found";
|
||||
|
||||
if (gen.checkProof (pow.getToken (), solution) != powOK)
|
||||
{
|
||||
//WriteLog (lsFATAL, ProofOfWork) << "Solution fails";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
static ProofOfWorkTests proofOfWorkTests;
|
||||
@@ -1,57 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
/*
|
||||
Copyright (c) 2011-2013, OpenCoin, Inc.
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#ifndef RIPPLE_PROOFOFWORKFACTORY_RIPPLEHEADER
|
||||
#define RIPPLE_PROOFOFWORKFACTORY_RIPPLEHEADER
|
||||
|
||||
// PRIVATE HEADER
|
||||
|
||||
class ProofOfWorkFactory
|
||||
: public IProofOfWorkFactory
|
||||
, LeakChecked <ProofOfWorkFactory>
|
||||
{
|
||||
public:
|
||||
ProofOfWorkFactory ();
|
||||
|
||||
ProofOfWork getProof ();
|
||||
POWResult checkProof (const std::string& token, uint256 const& solution);
|
||||
uint64 getDifficulty ()
|
||||
{
|
||||
return ProofOfWork::getDifficulty (mTarget, mIterations);
|
||||
}
|
||||
void setDifficulty (int i);
|
||||
|
||||
void loadHigh ();
|
||||
void loadLow ();
|
||||
void sweep (void);
|
||||
|
||||
uint256 const& getSecret () const
|
||||
{
|
||||
return mSecret;
|
||||
}
|
||||
void setSecret (uint256 const& secret)
|
||||
{
|
||||
mSecret = secret;
|
||||
}
|
||||
|
||||
static int getPowEntry (uint256 const& target, int iterations);
|
||||
|
||||
private:
|
||||
typedef RippleMutex LockType;
|
||||
typedef LockType::ScopedLockType ScopedLockType;
|
||||
LockType mLock;
|
||||
|
||||
uint256 mSecret;
|
||||
int mIterations;
|
||||
uint256 mTarget;
|
||||
time_t mLastDifficultyChange;
|
||||
int mValidTime;
|
||||
int mPowEntry;
|
||||
|
||||
powMap_t mSolvedChallenges;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1779,7 +1779,7 @@ void PeerImp::recvProofWork (protocol::TMProofWork& packet)
|
||||
|
||||
uint256 response;
|
||||
memcpy (response.begin (), packet.response ().data (), 256 / 8);
|
||||
POWResult r = getApp().getProofOfWorkFactory ().checkProof (packet.token (), response);
|
||||
PowResult r = getApp().getProofOfWorkFactory ().checkProof (packet.token (), response);
|
||||
|
||||
if (r == powOK)
|
||||
{
|
||||
|
||||
@@ -30,6 +30,9 @@ namespace ripple
|
||||
#include "main/ripple_FatalErrorReporter.cpp"
|
||||
|
||||
# include "rpc/RPCHandler.h"
|
||||
# include "misc/PowResult.h"
|
||||
# include "misc/ProofOfWork.h"
|
||||
# include "misc/ProofOfWorkFactory.h"
|
||||
#include "rpc/RPCHandler.cpp"
|
||||
# include "rpc/RPCServerHandler.h"
|
||||
#include "rpc/RPCServerHandler.cpp"
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
// purely abstract and move implementation into .cpp files.
|
||||
//
|
||||
|
||||
#if 1
|
||||
#include <boost/algorithm/string.hpp>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/array.hpp>
|
||||
@@ -44,7 +45,6 @@
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/ptr_container/ptr_vector.hpp>
|
||||
#include <boost/ref.hpp>
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
#include <boost/smart_ptr/shared_ptr.hpp>
|
||||
#include <boost/thread/condition_variable.hpp>
|
||||
@@ -55,6 +55,7 @@
|
||||
#include <boost/unordered_map.hpp>
|
||||
#include <boost/unordered_set.hpp>
|
||||
#include <boost/weak_ptr.hpp>
|
||||
#endif
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -95,7 +96,6 @@ namespace ripple
|
||||
#include "ledger/Ledger.h"
|
||||
#include "ledger/SerializedValidation.h"
|
||||
#include "main/ripple_LoadManager.h"
|
||||
#include "misc/ripple_ProofOfWork.h"
|
||||
#include "misc/ripple_OrderBook.h"
|
||||
#include "shamap/ripple_SHAMapSyncFilters.h"
|
||||
#include "misc/ripple_IFeatures.h"
|
||||
@@ -103,7 +103,6 @@ namespace ripple
|
||||
#include "misc/ripple_IHashRouter.h"
|
||||
#include "peers/ripple_Peer.h"
|
||||
#include "peers/ripple_Peers.h"
|
||||
#include "misc/ripple_IProofOfWorkFactory.h"
|
||||
#include "peers/ripple_ClusterNodeStatus.h"
|
||||
#include "peers/ripple_UniqueNodeList.h"
|
||||
#include "misc/ripple_IValidations.h"
|
||||
|
||||
@@ -13,9 +13,11 @@ namespace ripple
|
||||
|
||||
#include "peers/ripple_PeerSet.cpp"
|
||||
#include "misc/ripple_OrderBook.cpp"
|
||||
#include "misc/ripple_ProofOfWork.cpp"
|
||||
#include "misc/ripple_ProofOfWorkFactory.h"
|
||||
#include "misc/ripple_ProofOfWorkFactory.cpp" // requires ProofOfWork.cpp for ProofOfWork::sMaxDifficulty
|
||||
# include "misc/PowResult.h"
|
||||
# include "misc/ProofOfWork.h"
|
||||
# include "misc/ProofOfWorkFactory.h"
|
||||
#include "misc/ProofOfWorkFactory.cpp"
|
||||
#include "misc/ProofOfWork.cpp"
|
||||
#include "misc/ripple_SerializedTransaction.cpp"
|
||||
|
||||
#include "shamap/ripple_SHAMapSyncFilters.cpp" // requires Application
|
||||
|
||||
@@ -19,6 +19,9 @@ namespace ripple
|
||||
#include "tx/OfferCreateTransactor.cpp"
|
||||
#include "tx/WalletAddTransactor.cpp"
|
||||
#include "misc/ripple_FeeVote.cpp"
|
||||
# include "misc/PowResult.h"
|
||||
# include "misc/ProofOfWork.h"
|
||||
# include "misc/ProofOfWorkFactory.h"
|
||||
#include "peers/ripple_Peer.cpp"
|
||||
|
||||
}
|
||||
|
||||
@@ -893,7 +893,7 @@ Json::Value RPCHandler::doProofCreate (Json::Value params, LoadType* loadType, A
|
||||
if (params.isMember ("difficulty") || params.isMember ("secret"))
|
||||
{
|
||||
// VFALCO TODO why aren't we using the app's factory?
|
||||
beast::ScopedPointer <IProofOfWorkFactory> pgGen (IProofOfWorkFactory::New ());
|
||||
beast::ScopedPointer <ProofOfWorkFactory> pgGen (ProofOfWorkFactory::New ());
|
||||
|
||||
if (params.isMember ("difficulty"))
|
||||
{
|
||||
@@ -902,7 +902,7 @@ Json::Value RPCHandler::doProofCreate (Json::Value params, LoadType* loadType, A
|
||||
|
||||
int iDifficulty = params["difficulty"].asInt ();
|
||||
|
||||
if (iDifficulty < 0 || iDifficulty > ProofOfWork::sMaxDifficulty)
|
||||
if (iDifficulty < 0 || iDifficulty > ProofOfWorkFactory::kMaxDifficulty)
|
||||
return rpcError (rpcINVALID_PARAMS);
|
||||
|
||||
pgGen->setDifficulty (iDifficulty);
|
||||
@@ -974,12 +974,12 @@ Json::Value RPCHandler::doProofVerify (Json::Value params, LoadType* loadType, A
|
||||
std::string strToken = params["token"].asString ();
|
||||
uint256 uSolution (params["solution"].asString ());
|
||||
|
||||
POWResult prResult;
|
||||
PowResult prResult;
|
||||
|
||||
if (params.isMember ("difficulty") || params.isMember ("secret"))
|
||||
{
|
||||
// VFALCO TODO why aren't we using the app's factory?
|
||||
beast::ScopedPointer <IProofOfWorkFactory> pgGen (IProofOfWorkFactory::New ());
|
||||
beast::ScopedPointer <ProofOfWorkFactory> pgGen (ProofOfWorkFactory::New ());
|
||||
|
||||
if (params.isMember ("difficulty"))
|
||||
{
|
||||
@@ -988,7 +988,7 @@ Json::Value RPCHandler::doProofVerify (Json::Value params, LoadType* loadType, A
|
||||
|
||||
int iDifficulty = params["difficulty"].asInt ();
|
||||
|
||||
if (iDifficulty < 0 || iDifficulty > ProofOfWork::sMaxDifficulty)
|
||||
if (iDifficulty < 0 || iDifficulty > ProofOfWorkFactory::kMaxDifficulty)
|
||||
return rpcError (rpcINVALID_PARAMS);
|
||||
|
||||
pgGen->setDifficulty (iDifficulty);
|
||||
@@ -1013,7 +1013,7 @@ Json::Value RPCHandler::doProofVerify (Json::Value params, LoadType* loadType, A
|
||||
std::string sToken;
|
||||
std::string sHuman;
|
||||
|
||||
powResultInfo (prResult, sToken, sHuman);
|
||||
ProofOfWork::calcResultInfo (prResult, sToken, sHuman);
|
||||
|
||||
jvResult["proof_result"] = sToken;
|
||||
jvResult["proof_result_code"] = prResult;
|
||||
|
||||
@@ -47,7 +47,7 @@ message TMProofWork
|
||||
optional bytes challenge = 4;
|
||||
optional bytes response = 5;
|
||||
|
||||
enum POWResult
|
||||
enum PowResult
|
||||
{
|
||||
powrOK = 0;
|
||||
powrREUSED = 1;
|
||||
@@ -56,7 +56,7 @@ message TMProofWork
|
||||
powrINVALID = 4;
|
||||
powrDISCONNECT = 5; // We are disconnecting
|
||||
}
|
||||
optional POWResult result = 6;
|
||||
optional PowResult result = 6;
|
||||
}
|
||||
|
||||
// Sent on connect
|
||||
|
||||
Reference in New Issue
Block a user