Skip to content

Commit

Permalink
Refactor CBlockIndex::cpid from uint128 to NN::Cpid
Browse files Browse the repository at this point in the history
The upgrade of uint256.h from Bitcoin removes the uint128 type. This
replaces the uint128 "cpid" field on CBlockIndex with NN::Cpid.
  • Loading branch information
cyrossignol committed Oct 20, 2019
1 parent 54ed020 commit 0943eee
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 46 deletions.
20 changes: 10 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -3089,9 +3089,9 @@ bool DisconnectBlocksBatch(CTxDB& txdb, list<CTransaction>& 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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
59 changes: 36 additions & 23 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}


Expand Down Expand Up @@ -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<CDiskBlockIndex*>(this)->SetCPID(cpid_hex);
const_cast<CDiskBlockIndex*>(this)->SetMiningId(NN::MiningId::Parse(cpid_hex));
}

READWRITE(nResearchSubsidy);
Expand Down
6 changes: 3 additions & 3 deletions src/rpcblockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/rpcdataacq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 );
Expand Down
14 changes: 7 additions & 7 deletions src/txdb-leveldb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 0943eee

Please sign in to comment.