diff --git a/src/blsct/pos/proof.cpp b/src/blsct/pos/proof.cpp index 4cf8fc937a94e..3449ec4f6b990 100644 --- a/src/blsct/pos/proof.cpp +++ b/src/blsct/pos/proof.cpp @@ -53,17 +53,20 @@ ProofOfStake::ProofOfStake(const Points& staked_commitments, const Scalar& eta_f rangeProof.Vs.Clear(); } -bool ProofOfStake::Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint32_t& prev_time, const uint64_t& stake_modifier, const uint32_t& time, const unsigned int& next_target) const +ProofOfStake::VerificationResult ProofOfStake::Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint32_t& prev_time, const uint64_t& stake_modifier, const uint32_t& time, const unsigned int& next_target) const { return Verify(staked_commitments, eta_fiat_shamir, eta_phi, CalculateKernelHash(prev_time, stake_modifier, setMemProof.phi, time), next_target); } -bool ProofOfStake::Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint256& kernel_hash, const unsigned int& next_target) const +ProofOfStake::VerificationResult ProofOfStake::Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint256& kernel_hash, const unsigned int& next_target) const { auto setup = SetMemProofSetup::Get(); auto setmemres = SetProver::Verify(setup, staked_commitments, eta_fiat_shamir, eta_phi, setMemProof); + if (!setmemres) + return ProofOfStake::SM_INVALID; + // std::cout << __func__ << ": Verifying Setmem proof with" // << "\n\t staked_commitments=" << staked_commitments.GetString() // << "\n\t eta_fiat_shamir=" << eta_fiat_shamir.GetString() @@ -73,7 +76,10 @@ bool ProofOfStake::Verify(const Points& staked_commitments, const Scalar& eta_fi auto kernelhashres = ProofOfStake::VerifyKernelHash(rangeProof, kernel_hash, next_target, eta_phi, setMemProof.phi); - return setmemres && kernelhashres; + if (!kernelhashres) + return ProofOfStake::RP_INVALID; + + return ProofOfStake::VALID; } bool ProofOfStake::VerifyKernelHash(const RangeProof& range_proof, const uint256& kernel_hash, const unsigned int& next_target, const blsct::Message& eta_phi, const Point& phi) diff --git a/src/blsct/pos/proof.h b/src/blsct/pos/proof.h index 1d16979d74488..d6dcaeccf8f59 100644 --- a/src/blsct/pos/proof.h +++ b/src/blsct/pos/proof.h @@ -35,8 +35,33 @@ class ProofOfStake ProofOfStake(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const Scalar& m, const Scalar& f, const uint32_t& prev_time, const uint64_t& stake_modifier, const uint32_t& time, const unsigned int& next_target); - bool Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint256& kernelHash, const unsigned int& posTarget) const; - bool Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint32_t& prev_time, const uint64_t& stake_modifier, const uint32_t& time, const unsigned int& next_target) const; + enum VerificationResult : uint32_t { + NONE = 0, + VALID = 1, + RP_INVALID = 2, + SM_INVALID = 3, + }; + + static std::string VerificationResultToString(const VerificationResult& res) + { + switch (res) { + case VALID: + return "Valid"; + break; + case RP_INVALID: + return "Invalid Range Proof"; + break; + case SM_INVALID: + return "Invalid Set Membership Proof"; + break; + default: + return "None"; + } + } + + VerificationResult + Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint256& kernelHash, const unsigned int& posTarget) const; + VerificationResult Verify(const Points& staked_commitments, const Scalar& eta_fiat_shamir, const blsct::Message& eta_phi, const uint32_t& prev_time, const uint64_t& stake_modifier, const uint32_t& time, const unsigned int& next_target) const; static bool VerifyKernelHash(const RangeProof& range_proof, const uint256& kernel_hash, const unsigned int& next_target, const blsct::Message& eta_phi, const Point& phi); static bool VerifyKernelHash(const RangeProof& range_proof, const uint256& min_value, const blsct::Message& eta_phi, const Point& phi); diff --git a/src/blsct/pos/proof_logic.cpp b/src/blsct/pos/proof_logic.cpp index 34677910a9608..c4e108a528ae5 100644 --- a/src/blsct/pos/proof_logic.cpp +++ b/src/blsct/pos/proof_logic.cpp @@ -4,6 +4,8 @@ #include #include +#include +#include using Arith = Mcl; using Point = Arith::Point; @@ -19,6 +21,9 @@ ProofOfStake ProofOfStakeLogic::Create(const CCoinsViewCache& cache, const Scala auto eta_phi = blsct::CalculateSetMemProofGeneratorSeed(pindexPrev); auto next_target = blsct::GetNextTargetRequired(pindexPrev, &block, params); + + LogPrint(BCLog::POPS, "Creating PoPS:\n Eta fiat shamir: %s\n Eta phi: %s\n Next Target: %d\n Staked Commitments:%s\n", HexStr(eta_fiat_shamir), HexStr(eta_phi), next_target, staked_commitments.GetString()); + return ProofOfStake(staked_commitments, eta_fiat_shamir, eta_phi, m, f, pindexPrev->nTime, pindexPrev->nStakeModifier, block.nTime, next_target); } @@ -27,6 +32,7 @@ bool ProofOfStakeLogic::Verify(const CCoinsViewCache& cache, const CBlockIndex* auto staked_commitments = cache.GetStakedCommitments().GetElements(); if (staked_commitments.Size() < 2) { + LogPrint(BCLog::POPS, "PoPS rejected. Staked commitments size is %d\n", staked_commitments.Size()); return false; } @@ -36,8 +42,12 @@ bool ProofOfStakeLogic::Verify(const CCoinsViewCache& cache, const CBlockIndex* auto kernel_hash = blsct::CalculateKernelHash(pindexPrev, block); auto next_target = blsct::GetNextTargetRequired(pindexPrev, &block, params); + LogPrint(BCLog::POPS, "Verifying PoPS:\n Eta fiat shamir: %s\n Eta phi: %s\n Kernel Hash: %s\n Next Target: %d\n Staked Commitments:%s\n", HexStr(eta_fiat_shamir), HexStr(eta_phi), kernel_hash.ToString(), next_target, staked_commitments.GetString()); + auto res = block.posProof.Verify(staked_commitments, eta_fiat_shamir, eta_phi, kernel_hash, next_target); - return res; + LogPrint(BCLog::POPS, "Result: %s\n", VerificationResultToString(res)); + + return res == blsct::ProofOfStake::VerificationResult::VALID; } } // namespace blsct \ No newline at end of file diff --git a/src/logging.cpp b/src/logging.cpp index aa3ec14097aa1..9a4189e9dcb00 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -148,43 +148,44 @@ struct CLogCategoryDesc { }; const CLogCategoryDesc LogCategories[] = -{ - {BCLog::NONE, "0"}, - {BCLog::NONE, ""}, - {BCLog::NET, "net"}, - {BCLog::TOR, "tor"}, - {BCLog::MEMPOOL, "mempool"}, - {BCLog::HTTP, "http"}, - {BCLog::BENCH, "bench"}, - {BCLog::ZMQ, "zmq"}, - {BCLog::WALLETDB, "walletdb"}, - {BCLog::RPC, "rpc"}, - {BCLog::ESTIMATEFEE, "estimatefee"}, - {BCLog::ADDRMAN, "addrman"}, - {BCLog::SELECTCOINS, "selectcoins"}, - {BCLog::REINDEX, "reindex"}, - {BCLog::CMPCTBLOCK, "cmpctblock"}, - {BCLog::RAND, "rand"}, - {BCLog::PRUNE, "prune"}, - {BCLog::PROXY, "proxy"}, - {BCLog::MEMPOOLREJ, "mempoolrej"}, - {BCLog::LIBEVENT, "libevent"}, - {BCLog::COINDB, "coindb"}, - {BCLog::LEVELDB, "leveldb"}, - {BCLog::VALIDATION, "validation"}, - {BCLog::I2P, "i2p"}, - {BCLog::IPC, "ipc"}, + { + {BCLog::NONE, "0"}, + {BCLog::NONE, ""}, + {BCLog::NET, "net"}, + {BCLog::TOR, "tor"}, + {BCLog::MEMPOOL, "mempool"}, + {BCLog::HTTP, "http"}, + {BCLog::BENCH, "bench"}, + {BCLog::ZMQ, "zmq"}, + {BCLog::WALLETDB, "walletdb"}, + {BCLog::RPC, "rpc"}, + {BCLog::ESTIMATEFEE, "estimatefee"}, + {BCLog::ADDRMAN, "addrman"}, + {BCLog::SELECTCOINS, "selectcoins"}, + {BCLog::REINDEX, "reindex"}, + {BCLog::CMPCTBLOCK, "cmpctblock"}, + {BCLog::RAND, "rand"}, + {BCLog::PRUNE, "prune"}, + {BCLog::PROXY, "proxy"}, + {BCLog::MEMPOOLREJ, "mempoolrej"}, + {BCLog::LIBEVENT, "libevent"}, + {BCLog::COINDB, "coindb"}, + {BCLog::LEVELDB, "leveldb"}, + {BCLog::VALIDATION, "validation"}, + {BCLog::I2P, "i2p"}, + {BCLog::IPC, "ipc"}, #ifdef DEBUG_LOCKCONTENTION - {BCLog::LOCK, "lock"}, + {BCLog::LOCK, "lock"}, #endif - {BCLog::UTIL, "util"}, - {BCLog::BLOCKSTORAGE, "blockstorage"}, - {BCLog::TXRECONCILIATION, "txreconciliation"}, - {BCLog::SCAN, "scan"}, - {BCLog::DANDELION, "dandelion"}, - {BCLog::TXPACKAGES, "txpackages"}, - {BCLog::ALL, "1"}, - {BCLog::ALL, "all"}, + {BCLog::UTIL, "util"}, + {BCLog::BLOCKSTORAGE, "blockstorage"}, + {BCLog::TXRECONCILIATION, "txreconciliation"}, + {BCLog::SCAN, "scan"}, + {BCLog::DANDELION, "dandelion"}, + {BCLog::TXPACKAGES, "txpackages"}, + {BCLog::POPS, "pops"}, + {BCLog::ALL, "1"}, + {BCLog::ALL, "all"}, }; bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str) @@ -287,6 +288,8 @@ std::string LogCategoryToStr(BCLog::LogFlags category) return "dandelion"; case BCLog::LogFlags::TXPACKAGES: return "txpackages"; + case BCLog::LogFlags::POPS: + return "pops"; case BCLog::LogFlags::ALL: return "all"; } diff --git a/src/logging.h b/src/logging.h index a439dc5ccaebd..780d1c9c60a7d 100644 --- a/src/logging.h +++ b/src/logging.h @@ -36,42 +36,43 @@ struct LogCategory { }; namespace BCLog { - enum LogFlags : uint32_t { - NONE = 0, - NET = (1 << 0), - TOR = (1 << 1), - MEMPOOL = (1 << 2), - HTTP = (1 << 3), - BENCH = (1 << 4), - ZMQ = (1 << 5), - WALLETDB = (1 << 6), - RPC = (1 << 7), - ESTIMATEFEE = (1 << 8), - ADDRMAN = (1 << 9), - SELECTCOINS = (1 << 10), - REINDEX = (1 << 11), - CMPCTBLOCK = (1 << 12), - RAND = (1 << 13), - PRUNE = (1 << 14), - PROXY = (1 << 15), - MEMPOOLREJ = (1 << 16), - LIBEVENT = (1 << 17), - COINDB = (1 << 18), - LEVELDB = (1 << 20), - VALIDATION = (1 << 21), - I2P = (1 << 22), - IPC = (1 << 23), +enum LogFlags : uint64_t { + NONE = 0, + NET = (1 << 0), + TOR = (1 << 1), + MEMPOOL = (1 << 2), + HTTP = (1 << 3), + BENCH = (1 << 4), + ZMQ = (1 << 5), + WALLETDB = (1 << 6), + RPC = (1 << 7), + ESTIMATEFEE = (1 << 8), + ADDRMAN = (1 << 9), + SELECTCOINS = (1 << 10), + REINDEX = (1 << 11), + CMPCTBLOCK = (1 << 12), + RAND = (1 << 13), + PRUNE = (1 << 14), + PROXY = (1 << 15), + MEMPOOLREJ = (1 << 16), + LIBEVENT = (1 << 17), + COINDB = (1 << 18), + LEVELDB = (1 << 20), + VALIDATION = (1 << 21), + I2P = (1 << 22), + IPC = (1 << 23), #ifdef DEBUG_LOCKCONTENTION - LOCK = (1 << 24), + LOCK = (1 << 24), #endif - UTIL = (1 << 25), - BLOCKSTORAGE = (1 << 26), - TXRECONCILIATION = (1 << 27), - SCAN = (1 << 28), - DANDELION = (1 << 30), - TXPACKAGES = (1 << 29), - ALL = ~(uint32_t)0, - }; + UTIL = (1 << 25), + BLOCKSTORAGE = (1 << 26), + TXRECONCILIATION = (1 << 27), + SCAN = (1 << 28), + TXPACKAGES = (1 << 29), + DANDELION = (1 << 30), + POPS = (1ULL << 31), + ALL = ~(uint64_t)0, +}; enum class Level { Trace = 0, // High-volume or detailed logging for development/debugging Debug, // Reasonably noisy logging, but still usable in production diff --git a/src/validation.cpp b/src/validation.cpp index 44dac1e3cf52a..7d94f77d5b3af 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -3997,7 +3997,7 @@ std::vector ChainstateManager::GenerateCoinbaseCommitment(CBlock& bool HasValidProofOfWork(const std::vector& headers, const Consensus::Params& consensusParams) { return std::all_of(headers.cbegin(), headers.cend(), - [&](const auto& header) { return header.IsProofOfStake() ? true : CheckProofOfWork(header.GetHash(), header.nBits, consensusParams); }); + [&](const auto& header) { return header.IsProofOfStake() ? true : CheckProofOfWork(header.GetHash(), header.nBits, consensusParams);}); } arith_uint256 CalculateHeadersWork(const std::vector& headers)