Files
xahaud/src/cpp/ripple/ProofOfWork.h
Vinnie Falco ee49051e1c Downgrade access specification from protected to private in most places
Conflicts:
	src/cpp/ripple/FeatureTable.h
	src/cpp/ripple/HashedObject.h
	src/cpp/ripple/NetworkOPs.h
2013-06-06 20:44:58 -07:00

98 lines
2.4 KiB
C++

#ifndef PROOF_OF_WORK__H
#define PROOF_OF_WORK__H
#include <string>
#include <boost/thread/mutex.hpp>
#include <boost/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/multiset_of.hpp>
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
};
bool powResultInfo(POWResult powCode, std::string& strToken, std::string& strHuman);
class ProofOfWork
{
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;
bool checkSolution(const uint256& solution) const;
const std::string& getToken() const { return mToken; }
const uint256& getChallenge() const { return mChallenge; }
// 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);
private:
std::string mToken;
uint256 mChallenge;
uint256 mTarget;
int mIterations;
static const uint256 sMinTarget;
static const int sMaxIterations;
};
class ProofOfWorkGenerator
{
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:
ProofOfWorkGenerator();
ProofOfWork getProof();
POWResult checkProof(const std::string& token, const uint256& solution);
uint64 getDifficulty() { return ProofOfWork::getDifficulty(mTarget, mIterations); }
void setDifficulty(int i);
void loadHigh();
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);
private:
uint256 mSecret;
int mIterations;
uint256 mTarget;
time_t mLastDifficultyChange;
int mValidTime;
int mPowEntry;
powMap_t mSolvedChallenges;
boost::mutex mLock;
};
#endif
// vim:ts=4