diff --git a/newcoin.vcxproj b/newcoin.vcxproj
index 56aa43c99..32ee35c9e 100644
--- a/newcoin.vcxproj
+++ b/newcoin.vcxproj
@@ -20,12 +20,14 @@
Application
true
MultiByte
+ v110
Application
false
true
MultiByte
+ v110
@@ -49,13 +51,13 @@
Level3
Disabled
BOOST_TEST_ALTERNATIVE_INIT_API;BOOST_TEST_NO_MAIN;_CRT_SECURE_NO_WARNINGS;_WIN32_WINNT=0x0501;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
- .\src;..\OpenSSL\include;..\boost_1_47_0;..\protobuf-2.4.1\src\
+ .\;..\OpenSSL\include;..\boost_1_52_0;..\protobuf\src
ProgramDatabase
Console
true
- ..\OpenSSL\lib\VC;..\boost_1_47_0\stage\lib;..\protobuf-2.4.1\vsprojects\Debug
+ ..\OpenSSL\lib\VC;..\boost_1_52_0\stage\lib;..\protobuf\vsprojects\Debug
ssleay32MDd.lib;libeay32MTd.lib;libprotobuf.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
@@ -137,9 +139,7 @@
-
- .\;..\OpenSSL\include;..\boost_1_47_0;..\protobuf-2.4.1\src\
-
+
@@ -175,14 +175,16 @@
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
@@ -287,7 +289,7 @@
Document
- /code/protoc-2.4.1-win32/protoc -I=..\newcoin --cpp_out=\code\newcoin\ ..\newcoin/src/cpp/ripple/ripple.proto
+ /code/protobuf/protoc -I=..\newcoin --cpp_out=\code\newcoin\ ..\newcoin/src/cpp/ripple/ripple.proto
\code\newcoin\src\ripple.pb.h
diff --git a/newcoin.vcxproj.filters b/newcoin.vcxproj.filters
index 33693e035..0380497b0 100644
--- a/newcoin.vcxproj.filters
+++ b/newcoin.vcxproj.filters
@@ -33,30 +33,6 @@
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
-
- Source Files\websocketpp
-
Source Files\database
@@ -318,6 +294,36 @@
Source Files\database
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
+
+ Source Files\websocketpp
+
diff --git a/src/cpp/ripple/LedgerConsensus.cpp b/src/cpp/ripple/LedgerConsensus.cpp
index 9f9e09945..e5a877b3b 100644
--- a/src/cpp/ripple/LedgerConsensus.cpp
+++ b/src/cpp/ripple/LedgerConsensus.cpp
@@ -181,9 +181,9 @@ bool TransactionAcquire::takeNodes(const std::list& nodeIDs,
}
void LCTransaction::setVote(const uint160& peer, bool votesYes)
-{ // Tracke a peer's yes/no vote on a particular disputed transaction
- std::pair::iterator, bool> res =
- mVotes.insert(std::make_pair(peer, votesYes));
+{ // Track a peer's yes/no vote on a particular disputed transaction
+ std::pair::iterator, bool> res =
+ mVotes.insert(std::pair(peer, votesYes));
if (res.second)
{ // new vote
diff --git a/src/cpp/ripple/ProofOfWork.cpp b/src/cpp/ripple/ProofOfWork.cpp
index 9ccb29eb2..f31723a11 100644
--- a/src/cpp/ripple/ProofOfWork.cpp
+++ b/src/cpp/ripple/ProofOfWork.cpp
@@ -2,9 +2,16 @@
#include
+#include
+
+#include
+
#include
#include "Serializer.h"
+#include "Log.h"
+
+SETUP_LOG();
const uint256 ProofOfWork::sMinTarget("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
const int ProofOfWork::sMaxIterations(1 << 23);
@@ -16,26 +23,35 @@ bool ProofOfWork::isValid() const
uint64 ProofOfWork::getDifficulty(const uint256& target, int iterations)
{ // 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");
+ }
// more iterations means more hashes per iteration but also a larger final hash
- uint64 difficulty = iterations * (iterations / 4 + 1);
+ uint64 difficulty = iterations + (iterations / 4);
- // 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 difficulty
const unsigned char *ptr = target.begin();
while (*ptr == 0)
{
- difficulty *= 16;
- ptr++;
+ difficulty *= 256;
+ ++ptr;
}
-
- // If the first digit after a zero isn't an F, multiply
- difficulty *= (16 - *ptr);
+ difficulty = (difficulty * 256) / (*ptr + 1);
return difficulty;
}
+static uint256 getSHA512Half(const std::vector& vec)
+{
+ return Serializer::getSHA512Half(vec.front().begin(), vec.size() * (256 / 8));
+}
+
uint256 ProofOfWork::solve(int maxIterations) const
{
if (!isValid())
@@ -44,23 +60,94 @@ uint256 ProofOfWork::solve(int maxIterations) const
uint256 nonce;
RAND_bytes(nonce.begin(), nonce.size());
- Serializer s1, s2;
- std::vector buf;
- buf.reserve((256 / 8) * mIterations);
+ std::vector buf2;
+ buf2.resize(mIterations);
- while (maxIterations > 8)
+ std::vector buf1;
+ buf1.resize(3);
+ buf1[0] = mChallenge;
+
+ while (maxIterations > 0)
{
- s1.add256(mChallenge);
- s1.add256(nonce);
-// uint256 base = s1.getSHA512Half();
-
- for (int i = 0; i < mIterations; ++i)
+ buf1[1] = nonce;
+ buf1[2] = uint256();
+ for (int i = (mIterations - 1); i >= 0; --i)
{
- // WRITEME
+ buf1[2] = getSHA512Half(buf1);
+ buf2[i] = buf1[2];
}
- s1.erase();
- nonce++;
+ if (getSHA512Half(buf2) <= mTarget)
+ return nonce;
+
+ ++nonce;
+ --maxIterations;
}
return uint256();
}
+
+bool ProofOfWork::checkSolution(const uint256& solution) const
+{
+ if (mIterations > sMaxIterations)
+ return false;
+
+ std::vector buf1;
+ buf1.push_back(mChallenge);
+ buf1.push_back(solution);
+ buf1.push_back(uint256());
+
+ std::vector buf2;
+ buf2.resize(mIterations);
+ for (int i = (mIterations - 1); i >= 0; --i)
+ {
+ buf1[2] = getSHA512Half(buf1);
+ buf2[i] = buf1[2];
+ }
+ return getSHA512Half(buf2) <= mTarget;
+}
+
+ProofOfWorkGenerator::ProofOfWorkGenerator() :
+ mIterations(128),
+ mTarget("0003FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"),
+ mLastDifficultyChange(time(NULL)),
+ mValidTime(180)
+{
+ RAND_bytes(mSecret.begin(), mSecret.size());
+}
+
+ProofOfWork ProofOfWorkGenerator::getProof()
+{
+ // challenge - target - iterations - time - validator
+ static boost::format f("%s-%s-%d-%d");
+
+ int now = static_cast(time(NULL) / 4);
+
+ uint256 challenge;
+ RAND_bytes(challenge.begin(), challenge.size());
+
+ boost::mutex::scoped_lock sl(mLock);
+
+ std::string s = boost::str(f % challenge.GetHex() % mTarget.GetHex() % mIterations % now);
+ std::string c = mSecret.GetHex() + s;
+ s += "-" + Serializer::getSHA512Half(c).GetHex();
+
+ return ProofOfWork(s, mIterations, challenge, mTarget);
+}
+
+BOOST_AUTO_TEST_SUITE(ProofOfWork_suite)
+
+BOOST_AUTO_TEST_CASE( ProofOfWork_test )
+{
+ ProofOfWorkGenerator gen;
+ ProofOfWork pow = gen.getProof();
+ cLog(lsINFO) << "Estimated difficulty: " << pow.getDifficulty();
+ uint256 solution = pow.solve(16777216);
+ if (solution.isZero())
+ BOOST_FAIL("Unable to solve proof of work");
+ if (!pow.checkSolution(solution))
+ BOOST_FAIL("Solution did not check");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+// vim:ts=4
diff --git a/src/cpp/ripple/ProofOfWork.h b/src/cpp/ripple/ProofOfWork.h
index 1391be91c..0e9a9337f 100644
--- a/src/cpp/ripple/ProofOfWork.h
+++ b/src/cpp/ripple/ProofOfWork.h
@@ -10,6 +10,15 @@
#include "uint256.h"
+enum POWResult
+{
+ powOK = 0,
+ powREUSED = 1,
+ powBADNONCE = 2,
+ powBADTOKEN = 3,
+ powEXPIRED = 4,
+};
+
class ProofOfWork
{
protected:
@@ -48,12 +57,13 @@ protected:
int mIterations;
uint256 mTarget;
time_t mLastDifficultyChange;
+ int mValidTime;
powMap_t mSolvedChallenges;
boost::mutex mLock;
public:
- ProofOfWorkGenerator(const uint256& secret);
+ ProofOfWorkGenerator();
ProofOfWork getProof();
bool checkProof(const std::string& token, const uint256& solution);
@@ -64,3 +74,5 @@ public:
};
#endif
+
+// vim:ts=4
diff --git a/src/cpp/ripple/RPC.h b/src/cpp/ripple/RPC.h
index f4f75f79f..7d0406a20 100644
--- a/src/cpp/ripple/RPC.h
+++ b/src/cpp/ripple/RPC.h
@@ -1,3 +1,6 @@
+#ifndef __RPC_h__
+#define __RPC_h__
+
#include
#include