Skip to content

Commit bb6d403

Browse files
committed
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.
1 parent 464796c commit bb6d403

File tree

5 files changed

+48
-36
lines changed

5 files changed

+48
-36
lines changed

src/main.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "rpcclient.h"
2121
#include "beacon.h"
2222
#include "miner.h"
23-
#include "neuralnet/cpid.h"
2423
#include "neuralnet/neuralnet.h"
2524
#include "neuralnet/researcher.h"
2625
#include "neuralnet/superblock.h"
@@ -2736,7 +2735,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck, boo
27362735
// Gridcoin: Store verified magnitude and CPID in block index (7-11-2015)
27372736
if(IsResearchAgeEnabled(pindex->nHeight))
27382737
{
2739-
pindex->SetCPID(bb.cpid);
2738+
pindex->SetMiningId(NN::MiningId::Parse(bb.cpid));
27402739
pindex->nMagnitude = bb.Magnitude;
27412740
pindex->nResearchSubsidy = bb.ResearchSubsidy;
27422741
pindex->nInterestSubsidy = bb.InterestSubsidy;
@@ -2827,7 +2826,7 @@ bool CBlock::ConnectBlock(CTxDB& txdb, CBlockIndex* pindex, bool fJustCheck, boo
28272826

28282827
if(!is_claim_valid(nStakeReward, OUT_POR, OUT_INTEREST, nFees))
28292828
{
2830-
GetLifetimeCPID(pindex->GetCPID()); // Rescan...
2829+
GetLifetimeCPID(pindex->GetMiningId().ToString()); // Rescan...
28312830
GetProofOfStakeReward(nCoinAge, nFees, bb.cpid, true, 2, nTime,
28322831
pindex, "connectblock_researcher_doublecheck", OUT_POR, OUT_INTEREST, dAccrualAge, dMagnitudeUnit, dAvgMagnitude);
28332832

@@ -3102,9 +3101,9 @@ bool DisconnectBlocksBatch(CTxDB& txdb, list<CTransaction>& vResurrect, unsigned
31023101
if(pindexBest->IsUserCPID())
31033102
{
31043103
// remeber the cpid to re-read later
3105-
vRereadCPIDs.insert(pindexBest->GetCPID());
3104+
vRereadCPIDs.insert(pindexBest->GetMiningId().ToString());
31063105
// The user has no longer staked this block.
3107-
RemoveCPIDBlockHash(pindexBest->GetCPID(), pindexBest);
3106+
RemoveCPIDBlockHash(pindexBest->GetMiningId().ToString(), pindexBest);
31083107
}
31093108

31103109
// New best block
@@ -3344,7 +3343,7 @@ bool ReorganizeChain(CTxDB& txdb, unsigned &cnt_dis, unsigned &cnt_con, CBlock &
33443343
}
33453344

33463345
if(pindex->IsUserCPID()) // is this needed?
3347-
GetLifetimeCPID(pindex->cpid.GetHex());
3346+
GetLifetimeCPID(pindex->GetMiningId().ToString());
33483347
}
33493348

33503349
if (fDebug && (cnt_dis>0 || cnt_con>1))
@@ -4700,7 +4699,7 @@ void AddResearchMagnitude(CBlockIndex* pIndex)
47004699

47014700
try
47024701
{
4703-
const std::string& cpid = pIndex->GetCPID();
4702+
const std::string& cpid = pIndex->GetMiningId().ToString();
47044703
StructCPID& stMag = GetInitializedStructCPID2(cpid, mvMagnitudesCopy);
47054704
stMag.InterestSubsidy += pIndex->nInterestSubsidy;
47064705
stMag.ResearchSubsidy += pIndex->nResearchSubsidy;
@@ -4783,7 +4782,7 @@ bool GetEarliestStakeTime(std::string grcaddress, std::string cpid)
47834782
}
47844783
else
47854784
{
4786-
myCPID = pblockindex->GetCPID();
4785+
myCPID = pblockindex->GetMiningId().ToString();
47874786
}
47884787
if (cpid == myCPID && nCPIDTime==0 && IsResearcher(myCPID))
47894788
{
@@ -4817,7 +4816,7 @@ void AddRARewardBlock(const CBlockIndex* pindex)
48174816
// this is from LoadBlockIndex
48184817
if (pindex->nResearchSubsidy > 0 && pindex->IsUserCPID())
48194818
{
4820-
const std::string& cpid = pindex->GetCPID();
4819+
const std::string& cpid = pindex->GetMiningId().ToString();
48214820

48224821
StructCPID& stCPID = GetInitializedStructCPID2(cpid,mvResearchAge);
48234822

@@ -4865,7 +4864,7 @@ void RescanLifetimeCPID(StructCPID& stCPID)
48654864
// Ensure that the block is valid
48664865
if(pblockindex == NULL ||
48674866
pblockindex->IsInMainChain() == false ||
4868-
pblockindex->GetCPID() != stCPID.cpid)
4867+
pblockindex->GetMiningId().ToString() != stCPID.cpid)
48694868
throw error("RescanLifetimeCPID: Invalid block %s in vRewardBlocs of %s", pblockindex? pblockindex->GetBlockHash().GetHex() :"null", stCPID.cpid );
48704869

48714870
const uint256& uHash = pblockindex->GetBlockHash();

src/main.h

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "arith_uint256.h"
99
#include "util.h"
1010
#include "net.h"
11+
#include "neuralnet/cpid.h"
1112
#include "sync.h"
1213
#include "script.h"
1314
#include "scrypt.h"
@@ -1324,7 +1325,7 @@ class CBlockIndex
13241325
int64_t nMint;
13251326
int64_t nMoneySupply;
13261327
// Gridcoin (7-11-2015) Add new Accrual Fields to block index
1327-
uint128 cpid;
1328+
NN::Cpid cpid;
13281329
double nResearchSubsidy;
13291330
double nInterestSubsidy;
13301331
double nMagnitude;
@@ -1515,27 +1516,39 @@ class CBlockIndex
15151516
nFlags |= BLOCK_STAKE_MODIFIER;
15161517
}
15171518

1518-
void SetCPID(const std::string& cpid_hex)
1519+
void SetMiningId(NN::MiningId mining_id)
15191520
{
1520-
// Clear current CPID state.
1521-
cpid = 0;
15221521
nFlags &= ~(EMPTY_CPID | INVESTOR_CPID);
1523-
if(cpid_hex.empty())
1522+
1523+
if (const auto cpid_option = mining_id.TryCpid()) {
1524+
cpid = *cpid_option;
1525+
return;
1526+
}
1527+
1528+
cpid = NN::Cpid();
1529+
1530+
if (mining_id.Which() == NN::MiningId::Kind::INVALID) {
15241531
nFlags |= EMPTY_CPID;
1525-
else if(cpid_hex == "INVESTOR")
1532+
} else {
15261533
nFlags |= INVESTOR_CPID;
1527-
else
1528-
cpid.SetHex(cpid_hex);
1534+
}
1535+
}
1536+
1537+
void SetCPID(NN::Cpid new_cpid)
1538+
{
1539+
nFlags &= ~(EMPTY_CPID | INVESTOR_CPID);
1540+
1541+
cpid = new_cpid;
15291542
}
15301543

1531-
std::string GetCPID() const
1544+
NN::MiningId GetMiningId() const
15321545
{
1533-
if(nFlags & EMPTY_CPID)
1534-
return "";
1535-
else if(nFlags & INVESTOR_CPID)
1536-
return "INVESTOR";
1546+
if (nFlags & EMPTY_CPID)
1547+
return NN::MiningId();
1548+
else if (nFlags & INVESTOR_CPID)
1549+
return NN::MiningId::ForInvestor();
15371550
else
1538-
return cpid.GetHex();
1551+
return NN::MiningId(cpid);
15391552
}
15401553

15411554

@@ -1621,11 +1634,11 @@ class CDiskBlockIndex : public CBlockIndex
16211634
READWRITE(blockHash);
16221635

16231636
//7-11-2015 - Gridcoin - New Accrual Fields (Note, Removing the determinstic block number to make this happen all the time):
1624-
std::string cpid_hex = GetCPID();
1637+
std::string cpid_hex = GetMiningId().ToString();
16251638
READWRITE(cpid_hex);
16261639

16271640
if (ser_action.ForRead()) {
1628-
const_cast<CDiskBlockIndex*>(this)->SetCPID(cpid_hex);
1641+
const_cast<CDiskBlockIndex*>(this)->SetMiningId(NN::MiningId::Parse(cpid_hex));
16291642
}
16301643

16311644
READWRITE(nResearchSubsidy);

src/rpcblockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,7 @@ UniValue lifetime(const UniValue& params, bool fHelp)
11881188
UniValue c(UniValue::VOBJ);
11891189
UniValue res(UniValue::VOBJ);
11901190

1191-
std::string cpid = NN::GetPrimaryCpid();
1191+
const NN::MiningId mining_id = NN::Researcher::Get()->Id();
11921192
std::string Narr = ToString(GetAdjustedTime());
11931193

11941194
c.pushKV("Lifetime Payments Report", Narr);
@@ -1208,11 +1208,11 @@ UniValue lifetime(const UniValue& params, bool fHelp)
12081208
if (pindex == pindexBest)
12091209
break;
12101210

1211-
if (pindex->GetCPID() == cpid && (pindex->nResearchSubsidy > 0))
1211+
if (pindex->GetMiningId() == mining_id && (pindex->nResearchSubsidy > 0))
12121212
res.pushKV(ToString(pindex->nHeight), RoundToString(pindex->nResearchSubsidy, 2));
12131213
}
12141214
//8-14-2015
1215-
StructCPID& stCPID = GetInitializedStructCPID2(cpid, mvResearchAge);
1215+
StructCPID& stCPID = GetInitializedStructCPID2(mining_id.ToString(), mvResearchAge);
12161216

12171217
res.pushKV("RA Magnitude Sum", stCPID.TotalMagnitude);
12181218
res.pushKV("RA Accuracy", stCPID.Accuracy);

src/rpcdataacq.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ UniValue rpc_getsupervotes(const UniValue& params, bool fHelp)
399399
UniValue result2(UniValue::VOBJ);
400400
result2.pushKV("neuralhash", bb.NeuralHash );
401401
result2.pushKV("weight", weight );
402-
result2.pushKV("cpid", cur->GetCPID() );
402+
result2.pushKV("cpid", cur->GetMiningId().ToString() );
403403
result2.pushKV("organization", bb.Organization );
404404
result2.pushKV("cversion", bb.clientversion );
405405
if(mode>=2)
@@ -672,7 +672,7 @@ UniValue rpc_getrecentblocks(const UniValue& params, bool fHelp)
672672

673673
if(detail>=2 && detail<20)
674674
{
675-
line+="<|>"+cur->GetCPID()
675+
line+="<|>"+cur->GetMiningId().ToString()
676676
+ "<|>"+RoundToString(cur->nResearchSubsidy,4)
677677
+ "<|>"+RoundToString(cur->nInterestSubsidy,4);
678678
}
@@ -685,7 +685,7 @@ UniValue rpc_getrecentblocks(const UniValue& params, bool fHelp)
685685
result2.pushKV("issuperblock", (bool)cur->nIsSuperBlock );
686686
result2.pushKV("iscontract", (bool)cur->nIsContract );
687687
result2.pushKV("ismodifier", (bool)cur->GeneratedStakeModifier() );
688-
result2.pushKV("cpid", cur->GetCPID() );
688+
result2.pushKV("cpid", cur->GetMiningId().ToString() );
689689
result2.pushKV("research", cur->nResearchSubsidy );
690690
result2.pushKV("interest", cur->nInterestSubsidy );
691691
result2.pushKV("magnitude", cur->nMagnitude );

src/txdb-leveldb.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,19 +626,19 @@ bool CTxDB::LoadBlockIndex()
626626
if(!IsResearchAgeEnabled(pindex->nHeight))
627627
continue;
628628

629-
if( pindex->IsUserCPID() && pindex->cpid == uint128() )
629+
if( pindex->IsUserCPID() && pindex->cpid.IsZero() )
630630
{
631631
/* There were reports of 0000 cpid in index where INVESTOR should have been. Check */
632632
auto bb = GetBoincBlockByIndex(pindex);
633-
if( bb.cpid != pindex->GetCPID() )
633+
if( bb.cpid != pindex->GetMiningId().ToString() )
634634
{
635635
if(fDebug)
636636
LogPrintf("WARNING: BlockIndex CPID %s did not match %s in block {%s %d}",
637-
pindex->GetCPID(), bb.cpid,
637+
pindex->GetMiningId().ToString(), bb.cpid,
638638
pindex->GetBlockHash().GetHex(), pindex->nHeight );
639639

640640
/* Repair the cpid field */
641-
pindex->SetCPID(bb.cpid);
641+
pindex->SetMiningId(NN::MiningId::Parse(bb.cpid));
642642

643643
#if 0
644644
if(!WriteBlockIndex(CDiskBlockIndex(pindex)))

0 commit comments

Comments
 (0)