#ifndef PROOF_OF_WORK__H #define PROOF_OF_WORK__H #include #include #include #include #include #include "uint256.h" enum POWResult { powOK = 0, powREUSED = 1, powBADNONCE = 2, powBADTOKEN = 3, powEXPIRED = 4, powCORRUPT = 5, }; class ProofOfWork { protected: std::string mToken; uint256 mChallenge; uint256 mTarget; int mIterations; static const uint256 sMinTarget; static const int sMaxIterations; public: ProofOfWork(const std::string& token, int iterations, const uint256& challenge, const uint256& target) : mToken(token), mChallenge(challenge), mTarget(target), mIterations(iterations) { ; } bool isValid() const; uint256 solve(int maxIterations) 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); } }; class ProofOfWorkGenerator { public: typedef boost::bimap< boost::bimaps::multiset_of, boost::bimaps::unordered_set_of > powMap_t; typedef powMap_t::value_type powMap_vt; protected: uint256 mSecret; int mIterations; uint256 mTarget; time_t mLastDifficultyChange; int mValidTime; powMap_t mSolvedChallenges; boost::mutex mLock; public: ProofOfWorkGenerator(); ProofOfWork getProof(); POWResult checkProof(const std::string& token, const uint256& solution); uint64 getDifficulty() { return ProofOfWork::getDifficulty(mTarget, mIterations); } void loadHigh(); void loadLow(); void sweep(void); }; #endif // vim:ts=4