From 0943eeee891e1f28858c071f84c1b4e8ac20df64 Mon Sep 17 00:00:00 2001 From: Cy Rossignol Date: Wed, 16 Oct 2019 23:19:26 -0500 Subject: [PATCH] Refactor CBlockIndex::cpid from uint128 to NN::Cpid The upgrade of uint256.h from Bitcoin removes the uint128 type. This replaces the uint128 "cpid" field on CBlockIndex with NN::Cpid. --- src/main.cpp | 20 +++++++-------- src/main.h | 59 ++++++++++++++++++++++++++----------------- src/rpcblockchain.cpp | 6 ++--- src/rpcdataacq.cpp | 6 ++--- src/txdb-leveldb.cpp | 14 +++++----- 5 files changed, 59 insertions(+), 46 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 286384d98f..b164cb7bc4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,6 @@ #include "rpcclient.h" #include "beacon.h" #include "miner.h" -#include "neuralnet/cpid.h" #include "neuralnet/neuralnet.h" #include "neuralnet/researcher.h" #include "neuralnet/superblock.h" @@ -2738,11 +2737,12 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck, boo // Gridcoin: Store verified magnitude and CPID in block index (7-11-2015) if(IsResearchAgeEnabled(pindex->nHeight)) { - pindex->SetCPID(cpid); + pindex->SetMiningId(claim.m_mining_id); pindex->nMagnitude = claim.m_magnitude; pindex->nResearchSubsidy = claim.m_research_subsidy; pindex->nInterestSubsidy = claim.m_block_subsidy; pindex->nIsSuperBlock = claim.ContainsSuperblock() ? 1 : 0; + // Must scan transactions after CoinStake to know if this is a contract. int iPos = 0; pindex->nIsContract = 0; @@ -2814,7 +2814,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck, boo if(!is_claim_valid(nStakeReward, OUT_POR, OUT_INTEREST, nFees)) { - GetLifetimeCPID(pindex->GetCPID()); // Rescan... + GetLifetimeCPID(pindex->GetMiningId().ToString()); // Rescan... GetProofOfStakeReward(nCoinAge, nFees, cpid, true, 2, nTime, pindex, OUT_POR, OUT_INTEREST, dAccrualAge, dMagnitudeUnit, dAvgMagnitude); @@ -3089,9 +3089,9 @@ bool DisconnectBlocksBatch(CTxDB& txdb, list& vResurrect, unsigned if(pindexBest->IsUserCPID()) { // remeber the cpid to re-read later - vRereadCPIDs.insert(pindexBest->GetCPID()); + vRereadCPIDs.insert(pindexBest->GetMiningId().ToString()); // The user has no longer staked this block. - RemoveCPIDBlockHash(pindexBest->GetCPID(), pindexBest); + RemoveCPIDBlockHash(pindexBest->GetMiningId().ToString(), pindexBest); } // New best block @@ -3331,7 +3331,7 @@ bool ReorganizeChain(CTxDB& txdb, unsigned &cnt_dis, unsigned &cnt_con, CBlock & } if(pindex->IsUserCPID()) // is this needed? - GetLifetimeCPID(pindex->cpid.GetHex()); + GetLifetimeCPID(pindex->GetMiningId().ToString()); } if (fDebug && (cnt_dis>0 || cnt_con>1)) @@ -4643,7 +4643,7 @@ void AddResearchMagnitude(CBlockIndex* pIndex) try { - const std::string& cpid = pIndex->GetCPID(); + const std::string& cpid = pIndex->GetMiningId().ToString(); StructCPID& stMag = GetInitializedStructCPID2(cpid, mvMagnitudesCopy); stMag.InterestSubsidy += pIndex->nInterestSubsidy; stMag.ResearchSubsidy += pIndex->nResearchSubsidy; @@ -4725,7 +4725,7 @@ bool GetEarliestStakeTime(std::string grcaddress, std::string cpid) } else { - myCPID = pblockindex->GetCPID(); + myCPID = pblockindex->GetMiningId().ToString(); } if (cpid == myCPID && nCPIDTime==0 && IsResearcher(myCPID)) { @@ -4759,7 +4759,7 @@ void AddRARewardBlock(const CBlockIndex* pindex) // this is from LoadBlockIndex if (pindex->nResearchSubsidy > 0 && pindex->IsUserCPID()) { - const std::string& cpid = pindex->GetCPID(); + const std::string& cpid = pindex->GetMiningId().ToString(); StructCPID& stCPID = GetInitializedStructCPID2(cpid,mvResearchAge); @@ -4807,7 +4807,7 @@ void RescanLifetimeCPID(StructCPID& stCPID) // Ensure that the block is valid if(pblockindex == NULL || pblockindex->IsInMainChain() == false || - pblockindex->GetCPID() != stCPID.cpid) + pblockindex->GetMiningId().ToString() != stCPID.cpid) throw error("RescanLifetimeCPID: Invalid block %s in vRewardBlocs of %s", pblockindex? pblockindex->GetBlockHash().GetHex() :"null", stCPID.cpid ); const uint256& uHash = pblockindex->GetBlockHash(); diff --git a/src/main.h b/src/main.h index 8f73025e19..0253c37d22 100644 --- a/src/main.h +++ b/src/main.h @@ -9,6 +9,7 @@ #include "util.h" #include "net.h" #include "neuralnet/claim.h" +#include "neuralnet/cpid.h" #include "sync.h" #include "script.h" #include "scrypt.h" @@ -1369,14 +1370,14 @@ class CBlockIndex int64_t nMint; int64_t nMoneySupply; - // Gridcoin (7-11-2015) Add new Accrual Fields to block index - uint128 cpid; - double nResearchSubsidy; - double nInterestSubsidy; - double nMagnitude; - // Indicators (9-13-2015) - unsigned int nIsSuperBlock; - unsigned int nIsContract; + // Gridcoin (7-11-2015) Add new Accrual Fields to block index + NN::Cpid cpid; + double nResearchSubsidy; + double nInterestSubsidy; + double nMagnitude; + // Indicators (9-13-2015) + unsigned int nIsSuperBlock; + unsigned int nIsContract; unsigned int nFlags; // ppcoin: block index flags enum @@ -1561,27 +1562,39 @@ class CBlockIndex nFlags |= BLOCK_STAKE_MODIFIER; } - void SetCPID(const std::string& cpid_hex) + void SetMiningId(NN::MiningId mining_id) { - // Clear current CPID state. - cpid = 0; nFlags &= ~(EMPTY_CPID | INVESTOR_CPID); - if(cpid_hex.empty()) + + if (const auto cpid_option = mining_id.TryCpid()) { + cpid = *cpid_option; + return; + } + + cpid = NN::Cpid(); + + if (mining_id.Which() == NN::MiningId::Kind::INVALID) { nFlags |= EMPTY_CPID; - else if(cpid_hex == "INVESTOR") + } else { nFlags |= INVESTOR_CPID; - else - cpid.SetHex(cpid_hex); + } + } + + void SetCPID(NN::Cpid new_cpid) + { + nFlags &= ~(EMPTY_CPID | INVESTOR_CPID); + + cpid = new_cpid; } - std::string GetCPID() const + NN::MiningId GetMiningId() const { - if(nFlags & EMPTY_CPID) - return ""; - else if(nFlags & INVESTOR_CPID) - return "INVESTOR"; + if (nFlags & EMPTY_CPID) + return NN::MiningId(); + else if (nFlags & INVESTOR_CPID) + return NN::MiningId::ForInvestor(); else - return cpid.GetHex(); + return NN::MiningId(cpid); } @@ -1667,11 +1680,11 @@ class CDiskBlockIndex : public CBlockIndex READWRITE(blockHash); //7-11-2015 - Gridcoin - New Accrual Fields (Note, Removing the determinstic block number to make this happen all the time): - std::string cpid_hex = GetCPID(); + std::string cpid_hex = GetMiningId().ToString(); READWRITE(cpid_hex); if (ser_action.ForRead()) { - const_cast(this)->SetCPID(cpid_hex); + const_cast(this)->SetMiningId(NN::MiningId::Parse(cpid_hex)); } READWRITE(nResearchSubsidy); diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index 7ff243564f..fddee90da4 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -1224,7 +1224,7 @@ UniValue lifetime(const UniValue& params, bool fHelp) UniValue c(UniValue::VOBJ); UniValue res(UniValue::VOBJ); - std::string cpid = NN::GetPrimaryCpid(); + const NN::MiningId mining_id = NN::Researcher::Get()->Id(); std::string Narr = ToString(GetAdjustedTime()); c.pushKV("Lifetime Payments Report", Narr); @@ -1244,11 +1244,11 @@ UniValue lifetime(const UniValue& params, bool fHelp) if (pindex == pindexBest) break; - if (pindex->GetCPID() == cpid && (pindex->nResearchSubsidy > 0)) + if (pindex->GetMiningId() == mining_id && (pindex->nResearchSubsidy > 0)) res.pushKV(ToString(pindex->nHeight), RoundToString(pindex->nResearchSubsidy, 2)); } //8-14-2015 - StructCPID& stCPID = GetInitializedStructCPID2(cpid, mvResearchAge); + StructCPID& stCPID = GetInitializedStructCPID2(mining_id.ToString(), mvResearchAge); res.pushKV("RA Magnitude Sum", stCPID.TotalMagnitude); res.pushKV("RA Accuracy", stCPID.Accuracy); diff --git a/src/rpcdataacq.cpp b/src/rpcdataacq.cpp index eb84d12c93..556c09f0e8 100644 --- a/src/rpcdataacq.cpp +++ b/src/rpcdataacq.cpp @@ -402,7 +402,7 @@ UniValue rpc_getsupervotes(const UniValue& params, bool fHelp) UniValue result2(UniValue::VOBJ); result2.pushKV("neuralhash", claim.m_quorum_hash.ToString()); result2.pushKV("weight", weight ); - result2.pushKV("cpid", cur->GetCPID() ); + result2.pushKV("cpid", cur->GetMiningId().ToString() ); result2.pushKV("organization", claim.m_organization); result2.pushKV("cversion", claim.m_client_version); if(mode>=2) @@ -674,7 +674,7 @@ UniValue rpc_getrecentblocks(const UniValue& params, bool fHelp) if(detail>=2 && detail<20) { - line+="<|>"+cur->GetCPID() + line+="<|>"+cur->GetMiningId().ToString() + "<|>"+RoundToString(cur->nResearchSubsidy,4) + "<|>"+RoundToString(cur->nInterestSubsidy,4); } @@ -687,7 +687,7 @@ UniValue rpc_getrecentblocks(const UniValue& params, bool fHelp) result2.pushKV("issuperblock", (bool)cur->nIsSuperBlock ); result2.pushKV("iscontract", (bool)cur->nIsContract ); result2.pushKV("ismodifier", (bool)cur->GeneratedStakeModifier() ); - result2.pushKV("cpid", cur->GetCPID() ); + result2.pushKV("cpid", cur->GetMiningId().ToString() ); result2.pushKV("research", cur->nResearchSubsidy ); result2.pushKV("interest", cur->nInterestSubsidy ); result2.pushKV("magnitude", cur->nMagnitude ); diff --git a/src/txdb-leveldb.cpp b/src/txdb-leveldb.cpp index 8d526cf210..ce6a4eb90f 100644 --- a/src/txdb-leveldb.cpp +++ b/src/txdb-leveldb.cpp @@ -42,17 +42,17 @@ void RepairZeroCpidIndex(CBlockIndex* const pindex) return; } - const std::string mining_id = claim->m_mining_id.ToString(); - - if (mining_id != pindex->GetCPID()) + if (claim->m_mining_id != pindex->GetMiningId()) { if(fDebug) LogPrintf("WARNING: BlockIndex CPID %s did not match %s in block {%s %d}", - pindex->GetCPID(), mining_id, - pindex->GetBlockHash().GetHex(), pindex->nHeight ); + pindex->GetMiningId().ToString(), + claim->m_mining_id.ToString(), + pindex->GetBlockHash().GetHex(), + pindex->nHeight); /* Repair the cpid field */ - pindex->SetCPID(mining_id); + pindex->SetMiningId(claim->m_mining_id); #if 0 if(!WriteBlockIndex(CDiskBlockIndex(pindex))) @@ -662,7 +662,7 @@ bool CTxDB::LoadBlockIndex() if(!IsResearchAgeEnabled(pindex->nHeight)) continue; - if( pindex->IsUserCPID() && pindex->cpid == uint128() ) + if( pindex->IsUserCPID() && pindex->cpid.IsZero() ) { RepairZeroCpidIndex(pindex); }