Merge branch 'develop' of github.com:jedmccaleb/NewCoin into develop

This commit is contained in:
JoelKatz
2013-05-13 06:46:08 -07:00
9 changed files with 261 additions and 4 deletions

View File

@@ -471,6 +471,47 @@ Json::Value RPCParser::parseAccountRaw(const Json::Value& jvParams, bool bPeer)
return jvRequest;
}
// proof_create [<difficulty>] [<secret>]
Json::Value RPCParser::parseProofCreate(const Json::Value& jvParams)
{
Json::Value jvRequest;
if (jvParams.size() >= 1)
jvRequest["difficulty"] = jvParams[0u].asInt();
if (jvParams.size() >= 2)
jvRequest["secret"] = jvParams[1u].asString();
return jvRequest;
}
// proof_solve <token>
Json::Value RPCParser::parseProofSolve(const Json::Value& jvParams)
{
Json::Value jvRequest;
jvRequest["token"] = jvParams[0u].asString();
return jvRequest;
}
// proof_verify <token> <solution> [<difficulty>] [<secret>]
Json::Value RPCParser::parseProofVerify(const Json::Value& jvParams)
{
Json::Value jvRequest;
jvRequest["token"] = jvParams[0u].asString();
jvRequest["solution"] = jvParams[1u].asString();
if (jvParams.size() >= 3)
jvRequest["difficulty"] = jvParams[2u].asInt();
if (jvParams.size() >= 4)
jvRequest["secret"] = jvParams[3u].asString();
return jvRequest;
}
// ripple_path_find <json> [<ledger>]
Json::Value RPCParser::parseRipplePathFind(const Json::Value& jvParams)
{
@@ -698,6 +739,9 @@ Json::Value RPCParser::parseCommand(std::string strMethod, Json::Value jvParams)
{ "peers", &RPCParser::parseAsIs, 0, 0 },
{ "ping", &RPCParser::parseAsIs, 0, 0 },
// { "profile", &RPCParser::parseProfile, 1, 9 },
{ "proof_create", &RPCParser::parseProofCreate, 0, 2 },
{ "proof_solve", &RPCParser::parseProofSolve, 1, 1 },
{ "proof_verify", &RPCParser::parseProofVerify, 2, 4 },
{ "random", &RPCParser::parseAsIs, 0, 0 },
{ "ripple_path_find", &RPCParser::parseRipplePathFind, 1, 2 },
{ "sign", &RPCParser::parseSignSubmit, 2, 2 },

View File

@@ -34,6 +34,9 @@ protected:
#endif
Json::Value parseLogLevel(const Json::Value& jvParams);
Json::Value parseOwnerInfo(const Json::Value& jvParams);
Json::Value parseProofCreate(const Json::Value& jvParams);
Json::Value parseProofSolve(const Json::Value& jvParams);
Json::Value parseProofVerify(const Json::Value& jvParams);
Json::Value parseRandom(const Json::Value& jvParams);
Json::Value parseRipplePathFind(const Json::Value& jvParams);
Json::Value parseSMS(const Json::Value& jvParams);

View File

@@ -5,6 +5,7 @@
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
#include <boost/regex.hpp>
#include <openssl/rand.h>
@@ -13,8 +14,53 @@
SETUP_LOG();
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;
}
const uint256 ProofOfWork::sMinTarget("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
const int ProofOfWork::sMaxIterations(1 << 23);
const int ProofOfWork::sMaxDifficulty(30);
ProofOfWork::ProofOfWork(const std::string& token)
{
std::vector<std::string> fields;
boost::split(fields, token, boost::algorithm::is_any_of("-"));
if (fields.size() != 5)
throw std::runtime_error("invalid token");
mToken = token;
mChallenge.SetHex(fields[0]);
mTarget.SetHex(fields[1]);
mIterations = lexical_cast_s<int>(fields[2]);
}
bool ProofOfWork::isValid() const
{
@@ -109,6 +155,14 @@ bool ProofOfWork::checkSolution(const uint256& solution) const
return getSHA512Half(buf2) <= mTarget;
}
bool ProofOfWork::validateToken(const std::string& strToken)
{
static boost::regex reToken("[[:xdigit:]]{64}-[[:xdigit:]]{64}-[[:digit:]]+-[[:digit:]]+-[[:xdigit:]]{64}");
boost::smatch smMatch;
return boost::regex_match(strToken, smMatch, reToken);
}
ProofOfWorkGenerator::ProofOfWorkGenerator() : mValidTime(180)
{
setDifficulty(1);
@@ -243,7 +297,7 @@ struct PowEntry
int iterations;
};
PowEntry PowEntries[31] =
PowEntry PowEntries[ProofOfWork::sMaxDifficulty + 1] =
{ // target iterations hashes memory
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 65536 }, // 1451874, 2 MB
{ "0CFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 98304 }, // 2177811, 3 MB
@@ -300,7 +354,7 @@ int ProofOfWorkGenerator::getPowEntry(const uint256& target, int iterations)
void ProofOfWorkGenerator::setDifficulty(int i)
{
assert((i >= 0) && (i <= 30));
assert((i >= 0) && (i <= ProofOfWork::sMaxDifficulty));
time_t now = time(NULL);
boost::mutex::scoped_lock sl(mLock);

View File

@@ -20,6 +20,8 @@ enum POWResult
powTOOEASY = 5, // the difficulty increased too much while you solved it
};
bool powResultInfo(POWResult powCode, std::string& strToken, std::string& strHuman);
class ProofOfWork
{
protected:
@@ -33,12 +35,16 @@ protected:
static const int sMaxIterations;
public:
static const int sMaxDifficulty;
typedef boost::shared_ptr<ProofOfWork> pointer;
ProofOfWork(const std::string& token, int iterations, const uint256& challenge, const uint256& target) :
mToken(token), mChallenge(challenge), mTarget(target), mIterations(iterations)
{ ; }
ProofOfWork(const std::string& token);
bool isValid() const;
uint256 solve(int maxIterations = 2 * sMaxIterations) const;
@@ -50,6 +56,8 @@ public:
// approximate number of hashes needed to solve
static uint64 getDifficulty(const uint256& target, int iterations);
uint64 getDifficulty() const { return getDifficulty(mTarget, mIterations); }
static bool validateToken(const std::string& strToken);
};
class ProofOfWorkGenerator
@@ -81,6 +89,9 @@ public:
void loadLow();
void sweep(void);
const uint256& getSecret() const { return mSecret; }
void setSecret(const uint256& secret) { mSecret = secret; }
static int getPowEntry(const uint256& target, int iterations);
};

View File

@@ -23,6 +23,7 @@
#include "InstanceCounter.h"
#include "Offer.h"
#include "PFRequest.h"
#include "ProofOfWork.h"
SETUP_LOG();
@@ -877,6 +878,141 @@ Json::Value RPCHandler::doProfile(Json::Value jvRequest, int& cost, ScopedLock&
return obj;
}
// {
// // if either of these parameters is set, a custom generator is used
// difficulty: <number> // optional
// secret: <secret> // optional
// }
Json::Value RPCHandler::doProofCreate(Json::Value jvRequest, int& cost, ScopedLock& MasterLockHolder)
{
// XXX: Add ability to create proof with arbitrary time
Json::Value jvResult(Json::objectValue);
if (jvRequest.isMember("difficulty") || jvRequest.isMember("secret"))
{
ProofOfWorkGenerator pgGen;
if (jvRequest.isMember("difficulty"))
{
if (!jvRequest["difficulty"].isIntegral())
return rpcError(rpcINVALID_PARAMS);
int iDifficulty = jvRequest["difficulty"].asInt();
if (iDifficulty < 0 || iDifficulty > ProofOfWork::sMaxDifficulty)
return rpcError(rpcINVALID_PARAMS);
pgGen.setDifficulty(iDifficulty);
}
if (jvRequest.isMember("secret"))
{
uint256 uSecret(jvRequest["secret"].asString());
pgGen.setSecret(uSecret);
}
jvResult["token"] = pgGen.getProof().getToken();
jvResult["secret"] = pgGen.getSecret().GetHex();
} else {
jvResult["token"] = theApp->getPowGen().getProof().getToken();
}
return jvResult;
}
// {
// token: <token>
// }
Json::Value RPCHandler::doProofSolve(Json::Value jvRequest, int& cost, ScopedLock& MasterLockHolder)
{
Json::Value jvResult;
if (!jvRequest.isMember("token"))
return rpcError(rpcINVALID_PARAMS);
std::string strToken = jvRequest["token"].asString();
if (!ProofOfWork::validateToken(strToken))
return rpcError(rpcINVALID_PARAMS);
ProofOfWork powProof(strToken);
uint256 uSolution = powProof.solve();
jvResult["solution"] = uSolution.GetHex();
return jvResult;
}
// {
// token: <token>
// solution: <solution>
// // if either of these parameters is set, a custom verifier is used
// difficulty: <number> // optional
// secret: <secret> // optional
// }
Json::Value RPCHandler::doProofVerify(Json::Value jvRequest, int& cost, ScopedLock& MasterLockHolder)
{
// XXX Add ability to check proof against arbitrary time
Json::Value jvResult;
if (!jvRequest.isMember("token"))
return rpcError(rpcINVALID_PARAMS);
if (!jvRequest.isMember("solution"))
return rpcError(rpcINVALID_PARAMS);
std::string strToken = jvRequest["token"].asString();
uint256 uSolution(jvRequest["solution"].asString());
POWResult prResult;
if (jvRequest.isMember("difficulty") || jvRequest.isMember("secret"))
{
ProofOfWorkGenerator pgGen;
if (jvRequest.isMember("difficulty"))
{
if (!jvRequest["difficulty"].isIntegral())
return rpcError(rpcINVALID_PARAMS);
int iDifficulty = jvRequest["difficulty"].asInt();
if (iDifficulty < 0 || iDifficulty > ProofOfWork::sMaxDifficulty)
return rpcError(rpcINVALID_PARAMS);
pgGen.setDifficulty(iDifficulty);
}
if (jvRequest.isMember("secret"))
{
uint256 uSecret(jvRequest["secret"].asString());
pgGen.setSecret(uSecret);
}
prResult = pgGen.checkProof(strToken, uSolution);
jvResult["secret"] = pgGen.getSecret().GetHex();
}
else
{
// XXX Proof should not be marked as used from this
prResult = theApp->getPowGen().checkProof(strToken, uSolution);
}
std::string sToken;
std::string sHuman;
powResultInfo(prResult, sToken, sHuman);
jvResult["proof_result"] = sToken;
jvResult["proof_result_code"] = prResult;
jvResult["proof_result_message"] = sHuman;
return jvResult;
}
// {
// account: <account>|<nickname>|<account_public_key>
// account_index: <number> // optional, defaults to 0.
@@ -3343,6 +3479,9 @@ Json::Value RPCHandler::doCommand(const Json::Value& jvRequest, int iRole, int &
{ "path_find", &RPCHandler::doPathFind, false, optCurrent },
{ "ping", &RPCHandler::doPing, false, optNone },
// { "profile", &RPCHandler::doProfile, false, optCurrent },
{ "proof_create", &RPCHandler::doProofCreate, false, optNone },
{ "proof_solve", &RPCHandler::doProofSolve, true, optNone },
{ "proof_verify", &RPCHandler::doProofVerify, true, optNone },
{ "random", &RPCHandler::doRandom, false, optNone },
{ "ripple_path_find", &RPCHandler::doRipplePathFind, false, optCurrent },
{ "sign", &RPCHandler::doSign, false, optCurrent },

View File

@@ -72,6 +72,9 @@ class RPCHandler
Json::Value doPathFind(Json::Value params, int& cost, ScopedLock& mlh);
Json::Value doPing(Json::Value params, int& cost, ScopedLock& mlh);
Json::Value doProfile(Json::Value params, int& cost, ScopedLock& mlh);
Json::Value doProofCreate(Json::Value params, int& cost, ScopedLock& mlh);
Json::Value doProofSolve(Json::Value params, int& cost, ScopedLock& mlh);
Json::Value doProofVerify(Json::Value params, int& cost, ScopedLock& mlh);
Json::Value doRandom(Json::Value jvRequest, int& cost, ScopedLock& mlh);
Json::Value doRipplePathFind(Json::Value jvRequest, int& cost, ScopedLock& mlh);
Json::Value doServerInfo(Json::Value params, int& cost, ScopedLock& mlh); // for humans

View File

@@ -91,6 +91,9 @@ void printHelp(const po::options_description& desc)
cerr << " ledger_header <ledger>" << endl;
cerr << " logrotate " << endl;
cerr << " peers" << endl;
cerr << " proof_create [<difficulty>] [<secret>]" << endl;
cerr << " proof_solve <token>" << endl;
cerr << " proof_verify <token> <solution> [<difficulty>] [<secret>]" << endl;
cerr << " random" << endl;
cerr << " ripple ..." << endl;
cerr << " ripple_path_find <json> [<ledger>]" << endl;