Skip to content

Commit

Permalink
Merge pull request #154 from nav-io/origin/pops-log
Browse files Browse the repository at this point in the history
Add PoPS proof log
  • Loading branch information
aguycalled authored Jul 11, 2024
2 parents 949d3be + c2d089c commit 4a4f0ff
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 76 deletions.
12 changes: 9 additions & 3 deletions src/blsct/pos/proof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arith>::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()
Expand All @@ -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)
Expand Down
29 changes: 27 additions & 2 deletions src/blsct/pos/proof.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion src/blsct/pos/proof_logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <blsct/pos/pos.h>
#include <blsct/pos/proof_logic.h>
#include <logging.h>
#include <util/strencodings.h>

using Arith = Mcl;
using Point = Arith::Point;
Expand All @@ -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);
}

Expand All @@ -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;
}

Expand All @@ -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
73 changes: 38 additions & 35 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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";
}
Expand Down
69 changes: 35 additions & 34 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3997,7 +3997,7 @@ std::vector<unsigned char> ChainstateManager::GenerateCoinbaseCommitment(CBlock&
bool HasValidProofOfWork(const std::vector<CBlockHeader>& 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<CBlockHeader>& headers)
Expand Down

0 comments on commit 4a4f0ff

Please sign in to comment.