mirror of
https://github.com/XRPLF/rippled.git
synced 2025-11-21 11:35:53 +00:00
Merge branch 'master' of github.com:jedmccaleb/NewCoin
This commit is contained in:
@@ -2,9 +2,14 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <boost/test/unit_test.hpp>
|
||||||
|
|
||||||
#include <openssl/rand.h>
|
#include <openssl/rand.h>
|
||||||
|
|
||||||
#include "Serializer.h"
|
#include "Serializer.h"
|
||||||
|
#include "Log.h"
|
||||||
|
|
||||||
|
SETUP_LOG();
|
||||||
|
|
||||||
const uint256 ProofOfWork::sMinTarget("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
const uint256 ProofOfWork::sMinTarget("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
||||||
const int ProofOfWork::sMaxIterations(1 << 23);
|
const int ProofOfWork::sMaxIterations(1 << 23);
|
||||||
@@ -16,26 +21,38 @@ bool ProofOfWork::isValid() const
|
|||||||
|
|
||||||
uint64 ProofOfWork::getDifficulty(const uint256& target, int iterations)
|
uint64 ProofOfWork::getDifficulty(const uint256& target, int iterations)
|
||||||
{ // calculate the approximate number of hashes required to solve this proof of work
|
{ // calculate the approximate number of hashes required to solve this proof of work
|
||||||
if ((iterations > sMaxIterations) || (target < sMinTarget));
|
if ((iterations > sMaxIterations) || (target < sMinTarget))
|
||||||
|
{
|
||||||
|
cLog(lsINFO) << "Iterations:" << iterations;
|
||||||
|
cLog(lsINFO) << "MaxIterat: " << sMaxIterations;
|
||||||
|
cLog(lsINFO) << "Target: " << target;
|
||||||
|
cLog(lsINFO) << "MinTarget: " << sMinTarget;
|
||||||
throw std::runtime_error("invalid proof of work target/iteration");
|
throw std::runtime_error("invalid proof of work target/iteration");
|
||||||
|
}
|
||||||
|
|
||||||
// more iterations means more hashes per iteration but also a larger final hash
|
// more iterations means more hashes per iteration but also a larger final hash
|
||||||
uint64 difficulty = iterations * (iterations / 4 + 1);
|
uint64 difficulty = iterations * (iterations / 4 + 1);
|
||||||
|
|
||||||
// Multiply the number of hashes needed by 16 for each leading zero in the hex difficulty
|
// Multiply the number of hashes needed by 256 for each leading zero byte in the hex difficulty
|
||||||
const unsigned char *ptr = target.begin();
|
const unsigned char *ptr = target.end() - 1;
|
||||||
while (*ptr == 0)
|
while (*ptr == 0)
|
||||||
{
|
{
|
||||||
difficulty *= 16;
|
cLog(lsINFO) << "getDif: " << (int) *ptr;
|
||||||
ptr++;
|
difficulty *= 256;
|
||||||
|
--ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the first digit after a zero isn't an F, multiply
|
// If the first digit after a zero isn't an F, multiply
|
||||||
difficulty *= (16 - *ptr);
|
difficulty *= (256 - *ptr);
|
||||||
|
|
||||||
return difficulty;
|
return difficulty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint256 getSHA512Half(const std::vector<uint256>& vec)
|
||||||
|
{
|
||||||
|
return Serializer::getSHA512Half(vec.front().begin(), vec.size() * (256 / 8));
|
||||||
|
}
|
||||||
|
|
||||||
uint256 ProofOfWork::solve(int maxIterations) const
|
uint256 ProofOfWork::solve(int maxIterations) const
|
||||||
{
|
{
|
||||||
if (!isValid())
|
if (!isValid())
|
||||||
@@ -44,23 +61,49 @@ uint256 ProofOfWork::solve(int maxIterations) const
|
|||||||
uint256 nonce;
|
uint256 nonce;
|
||||||
RAND_bytes(nonce.begin(), nonce.size());
|
RAND_bytes(nonce.begin(), nonce.size());
|
||||||
|
|
||||||
Serializer s1, s2;
|
std::vector<uint256> buf2;
|
||||||
std::vector<unsigned char> buf;
|
buf2.resize(mIterations);
|
||||||
buf.reserve((256 / 8) * mIterations);
|
|
||||||
|
|
||||||
while (maxIterations > 8)
|
std::vector<uint256> buf1;
|
||||||
|
buf1.resize(3);
|
||||||
|
buf1[0] = mChallenge;
|
||||||
|
|
||||||
|
while (maxIterations > 0)
|
||||||
{
|
{
|
||||||
s1.add256(mChallenge);
|
buf1[1] = nonce;
|
||||||
s1.add256(nonce);
|
buf1[2] = uint256();
|
||||||
// uint256 base = s1.getSHA512Half();
|
for (int i = (mIterations - 1); i >= 0; --i)
|
||||||
|
|
||||||
for (int i = 0; i < mIterations; ++i)
|
|
||||||
{
|
{
|
||||||
// WRITEME
|
if (buf1.size() != 3)
|
||||||
|
Log(lsINFO) << "buf1.size=" << buf1.size();
|
||||||
|
buf1[2] = getSHA512Half(buf1);
|
||||||
|
buf2[i] = buf1[2];
|
||||||
}
|
}
|
||||||
|
if (buf2.size() != mIterations)
|
||||||
|
Log(lsINFO) << "buf2.size=" << buf2.size();
|
||||||
|
|
||||||
s1.erase();
|
uint256 hash = getSHA512Half(buf2);
|
||||||
nonce++;
|
if (hash <= mTarget)
|
||||||
|
return nonce;
|
||||||
|
|
||||||
|
++nonce;
|
||||||
|
--maxIterations;
|
||||||
}
|
}
|
||||||
return uint256();
|
return uint256();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE(ProofOfWork_suite)
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE( ProofOfWork_test )
|
||||||
|
{
|
||||||
|
ProofOfWork pow("test", 32, uint256(),
|
||||||
|
uint256("00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"));
|
||||||
|
cLog(lsINFO) << "Estimated difficulty: " << pow.getDifficulty();
|
||||||
|
uint256 solution = pow.solve(16777216);
|
||||||
|
if (solution.isZero())
|
||||||
|
BOOST_FAIL("Unable to solve proof of work");
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
|
// vim:ts=4
|
||||||
|
|||||||
@@ -10,6 +10,15 @@
|
|||||||
|
|
||||||
#include "uint256.h"
|
#include "uint256.h"
|
||||||
|
|
||||||
|
enum POWResult
|
||||||
|
{
|
||||||
|
powOK = 0,
|
||||||
|
powREUSED = 1,
|
||||||
|
powBADNONCE = 2,
|
||||||
|
powBADTOKEN = 3,
|
||||||
|
powEXPIRED = 4,
|
||||||
|
};
|
||||||
|
|
||||||
class ProofOfWork
|
class ProofOfWork
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
@@ -48,6 +57,7 @@ protected:
|
|||||||
int mIterations;
|
int mIterations;
|
||||||
uint256 mTarget;
|
uint256 mTarget;
|
||||||
time_t mLastDifficultyChange;
|
time_t mLastDifficultyChange;
|
||||||
|
int mValidTime;
|
||||||
|
|
||||||
powMap_t mSolvedChallenges;
|
powMap_t mSolvedChallenges;
|
||||||
boost::mutex mLock;
|
boost::mutex mLock;
|
||||||
@@ -64,3 +74,5 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// vim:ts=4
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// These classes all store their values internally
|
// These classes all store their values internally
|
||||||
// in little-endian form
|
// in big-endian form
|
||||||
|
|
||||||
inline int Testuint256AdHoc(std::vector<std::string> vArg);
|
inline int Testuint256AdHoc(std::vector<std::string> vArg);
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ protected:
|
|||||||
enum { WIDTH=BITS/32 };
|
enum { WIDTH=BITS/32 };
|
||||||
|
|
||||||
// This is really big-endian in byte order.
|
// This is really big-endian in byte order.
|
||||||
// We use unsigned int for speed.
|
// We sometimes use unsigned int for speed.
|
||||||
unsigned int pn[WIDTH];
|
unsigned int pn[WIDTH];
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -73,7 +73,7 @@ public:
|
|||||||
zero();
|
zero();
|
||||||
|
|
||||||
// Put in least significant bits.
|
// Put in least significant bits.
|
||||||
((uint64_t *) end())[-1] = htobe64(uHost);
|
((uint64_t *) end())[-1] = htobe64(uHost);
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -105,10 +105,12 @@ public:
|
|||||||
base_uint& operator++()
|
base_uint& operator++()
|
||||||
{
|
{
|
||||||
// prefix operator
|
// prefix operator
|
||||||
int i = WIDTH;
|
for (int i = WIDTH - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
while (i-- && !++pn[i])
|
pn[i] = htobe32(be32toh(pn[i]) + 1);
|
||||||
;
|
if (pn[i] != 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -124,10 +126,13 @@ public:
|
|||||||
|
|
||||||
base_uint& operator--()
|
base_uint& operator--()
|
||||||
{
|
{
|
||||||
int i = WIDTH;
|
for (int i = WIDTH - 1; i >= 0; --i)
|
||||||
|
{
|
||||||
while (i-- && !pn[i]--)
|
uint32 prev = pn[i];
|
||||||
;
|
pn[i] = htobe32(be32toh(pn[i]) - 1);
|
||||||
|
if (prev != 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -169,10 +174,14 @@ public:
|
|||||||
const unsigned char* pAEnd = a.end();
|
const unsigned char* pAEnd = a.end();
|
||||||
const unsigned char* pB = b.begin();
|
const unsigned char* pB = b.begin();
|
||||||
|
|
||||||
while (pA != pAEnd && *pA == *pB)
|
while (*pA == *pB)
|
||||||
pA++, pB++;
|
{
|
||||||
|
if (++pA == pAEnd)
|
||||||
|
return 0;
|
||||||
|
++pB;
|
||||||
|
}
|
||||||
|
|
||||||
return pA == pAEnd ? 0 : *pA < *pB ? -1 : *pA > *pB ? 1 : 0;
|
return (*pA < *pB) ? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend inline bool operator<(const base_uint& a, const base_uint& b)
|
friend inline bool operator<(const base_uint& a, const base_uint& b)
|
||||||
|
|||||||
Reference in New Issue
Block a user