Skip to content

Commit

Permalink
Merge pull request dashpay#8 from barrystyle/0.14-chaintrust
Browse files Browse the repository at this point in the history
Migrate to use of nChainTrust from nChainWork.
  • Loading branch information
barrystyle authored Aug 17, 2019
2 parents f8232fa + 0a7793a commit 56fc0fb
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 96 deletions.
33 changes: 8 additions & 25 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void CBlockIndex::BuildSkip()
pskip = pprev->GetAncestor(GetSkipHeight(nHeight));
}

arith_uint256 GetBlockProof(const CBlockIndex& block)
arith_uint256 GetBlockTrust(const CBlockIndex& block)
{
arith_uint256 bnTarget;
bool fNegative;
Expand All @@ -142,23 +142,23 @@ arith_uint256 GetBlockProof(const CBlockIndex& block)
if (fNegative || fOverflow || bnTarget == 0)
return 0;
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
// as it's too large for an arith_uint256. However, as 2**256 is at least as large
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
// or ~bnTarget / (nTarget+1) + 1.
return (~bnTarget / (bnTarget + 1)) + 1;
// or ~bnTarget / (bnTarget+1) + 1.
return block.IsProofOfStake() ? (~bnTarget / (bnTarget + 1)) + 1 : 1;
}

int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
{
arith_uint256 r;
int sign = 1;
if (to.nChainWork > from.nChainWork) {
r = to.nChainWork - from.nChainWork;
if (to.nChainTrust > from.nChainTrust) {
r = to.nChainTrust - from.nChainTrust;
} else {
r = from.nChainWork - to.nChainWork;
r = from.nChainTrust - to.nChainTrust;
sign = -1;
}
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockTrust(tip);
if (r.bits() > 63) {
return sign * std::numeric_limits<int64_t>::max();
}
Expand All @@ -183,20 +183,3 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
assert(pa == pb);
return pa;
}

arith_uint256 CBlockIndex::GetBlockTrust() const
{
arith_uint256 bnTarget;
bnTarget.SetCompact(nBits);
if (bnTarget <= 0)
return 0;

if (IsProofOfStake()) {
// Return trust score as usual
return (arith_uint256(1) << 256) / (bnTarget + 1);
} else {
// Calculate work amount for block
arith_uint256 bnPoWTrust = ((~arith_uint256(0) >> 20) / (bnTarget + 1));
return bnPoWTrust > 1 ? bnPoWTrust : 1;
}
}
13 changes: 5 additions & 8 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ class CBlockIndex
//! pointer to the index of some further predecessor of this block
CBlockIndex* pskip;

//ppcoin: trust score of block chain
uint256 bnChainTrust;

//! height of the entry in the chain. The genesis block has height 0
int nHeight;

Expand All @@ -197,7 +194,7 @@ class CBlockIndex
unsigned int nUndoPos;

//! (memory only) Total amount of work (expected number of hashes) in the chain up to and including this block
arith_uint256 nChainWork;
arith_uint256 nChainTrust;

//! Number of transactions in this block.
//! Note: in a potential headers-first mode, this number cannot be relied upon
Expand Down Expand Up @@ -250,19 +247,19 @@ class CBlockIndex
nFile = 0;
nDataPos = 0;
nUndoPos = 0;
nChainWork = arith_uint256();
nChainTrust = arith_uint256();
nTx = 0;
nChainTx = 0;
nStatus = 0;
nSequenceId = 0;
nTimeMax = 0;

nMint = 0;
bnChainTrust = uint256();
nMoneySupply = 0;
nFlags = 0;
nStakeModifier = 0;
nStakeModifierChecksum = 0;
hashProofOfStake = uint256();
prevoutStake.SetNull();
nStakeTime = 0;

Expand All @@ -289,7 +286,7 @@ class CBlockIndex
nNonce = block.nNonce;

//Proof of Stake
bnChainTrust = uint256();
nChainTrust = arith_uint256();
nMint = 0;
nMoneySupply = 0;
nFlags = 0;
Expand Down Expand Up @@ -440,7 +437,7 @@ class CBlockIndex
const CBlockIndex* GetAncestor(int height) const;
};

arith_uint256 GetBlockProof(const CBlockIndex& block);
arith_uint256 GetBlockTrust(const CBlockIndex& block);
/** Return the time it would take to redo the work difference between from and to, assuming the current hashrate corresponds to the difficulty at tip, in seconds. */
int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params&);

Expand Down
2 changes: 1 addition & 1 deletion src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ class CMainParams : public CChainParams {
consensus.vDeployments[Consensus::DEPLOYMENT_DIP0008].nThreshold = 3226; // 80% of 4032

// The best chain should have at least this much work.
consensus.nMinimumChainWork = uint256S("0x00000000000000000000000000000000000000000000002ca70cb14917f702ff"); // #216000
consensus.nMinimumChainWork = uint256S("0x0000000000000000000000000000000000000000000000000000000000000000");

// By default assume that the signatures in ancestors of this block are valid.
consensus.defaultAssumeValid = uint256S("0x00000000000038a52e7bafb7fb091ed2989ec8bdd7a550db9925300687805d87"); // #216000
Expand Down
5 changes: 3 additions & 2 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ static bool SelectBlockFromCandidates(
const CBlockIndex** pindexSelected)
{
bool fSelected = false;
arith_uint256 hashBest;
arith_uint256 hashBest = 0;
*pindexSelected = nullptr;
for(const auto &item : vSortedByTimestamp)
{
Expand Down Expand Up @@ -156,7 +156,8 @@ bool ComputeNextStakeModifier(const CBlockIndex* pindexCurrent, uint64_t &nStake
int64_t nModifierTime = 0;
if (!GetLastStakeModifier(pindexPrev, nStakeModifier, nModifierTime))
return error("ComputeNextStakeModifier: unable to get last modifier");
LogPrintf("ComputeNextStakeModifier: prev modifier=0x%016x time=%s epoch=%u\n", nStakeModifier, DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nModifierTime).c_str(), (unsigned int)nModifierTime);
if (GetBoolArg("-debug", false))
LogPrintf("ComputeNextStakeModifier: prev modifier=0x%016x time=%s epoch=%u\n", nStakeModifier, DateTimeStrFormat("%Y-%m-%d %H:%M:%S", nModifierTime).c_str(), (unsigned int)nModifierTime);
if (nModifierTime / params.nModifierInterval >= pindexPrev->GetBlockTime() / params.nModifierInterval)
return true;

Expand Down
16 changes: 8 additions & 8 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ void ProcessBlockAvailability(NodeId nodeid) {

if (!state->hashLastUnknownBlock.IsNull()) {
BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
if (itOld != mapBlockIndex.end() && itOld->second->nChainTrust > 0) {
if (state->pindexBestKnownBlock == NULL || itOld->second->nChainTrust >= state->pindexBestKnownBlock->nChainTrust)
state->pindexBestKnownBlock = itOld->second;
state->hashLastUnknownBlock.SetNull();
}
Expand All @@ -430,9 +430,9 @@ void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
ProcessBlockAvailability(nodeid);

BlockMap::iterator it = mapBlockIndex.find(hash);
if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
if (it != mapBlockIndex.end() && it->second->nChainTrust > 0) {
// An actually better block was announced.
if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
if (state->pindexBestKnownBlock == NULL || it->second->nChainTrust >= state->pindexBestKnownBlock->nChainTrust)
state->pindexBestKnownBlock = it->second;
} else {
// An unknown block was announced; just assume that the latest one is the best one.
Expand Down Expand Up @@ -524,7 +524,7 @@ void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<con
// Make sure pindexBestKnownBlock is up to date, we'll need it.
ProcessBlockAvailability(nodeid);

if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < UintToArith256(consensusParams.nMinimumChainWork)) {
if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainTrust < chainActive.Tip()->nChainTrust || state->pindexBestKnownBlock->nChainTrust < UintToArith256(consensusParams.nMinimumChainWork)) {
// This peer has nothing interesting.
return;
}
Expand Down Expand Up @@ -2437,7 +2437,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr

// If this was a new header with more work than our tip, update the
// peer's last block announcement time
if (received_new_header && pindex->nChainWork > chainActive.Tip()->nChainWork) {
if (received_new_header && pindex->nChainTrust > chainActive.Tip()->nChainTrust) {
nodestate->m_last_block_announcement = GetTime();
}

Expand All @@ -2447,7 +2447,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
return true;

if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
if (pindex->nChainTrust <= chainActive.Tip()->nChainTrust || // We know something better
pindex->nTx != 0) { // We had this block at some point, but pruned it
if (fAlreadyInFlight) {
// We requested this block for some reason, but our mempool will probably be useless
Expand Down Expand Up @@ -2749,7 +2749,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
// If this set of headers is valid and ends in a block with at least as
// much work as our tip, download as much as possible.
if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainTrust <= pindexLast->nChainTrust) {
std::vector<const CBlockIndex *> vToFetch;
const CBlockIndex *pindexWalk = pindexLast;
// Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
Expand Down
7 changes: 7 additions & 0 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

#include <math.h>

const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake)
{
while (pindex && pindex->pprev && (pindex->IsProofOfStake() != fProofOfStake))
pindex = pindex->pprev;
return pindex;
}

unsigned int DualKGW3(const CBlockIndex* pindexLast, bool fProofOfStake, const Consensus::Params& params)
{
const CBlockIndex *BlockLastSolved = pindexLast;
Expand Down
1 change: 1 addition & 0 deletions src/pow.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class CBlockHeader;
class CBlockIndex;
class uint256;

const CBlockIndex* GetLastBlockIndex(const CBlockIndex* pindex, bool fProofOfStake);
unsigned int DualKGW3(const CBlockIndex* pindexLast, bool fProofOfStake, const Consensus::Params& params);
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const Consensus::Params& params);
unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);
Expand Down
9 changes: 5 additions & 4 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
result.push_back(Pair("nonce", (uint64_t)blockindex->nNonce));
result.push_back(Pair("bits", strprintf("%08x", blockindex->nBits)));
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
result.push_back(Pair("chainwork", blockindex->nChainTrust.GetHex()));
result.push_back(Pair("nTx", (uint64_t)blockindex->nTx));

if (blockindex->pprev)
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
Expand Down Expand Up @@ -159,7 +160,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
result.push_back(Pair("nonce", (uint64_t)block.nNonce));
result.push_back(Pair("bits", strprintf("%08x", block.nBits)));
result.push_back(Pair("difficulty", GetDifficulty(blockindex)));
result.push_back(Pair("chainwork", blockindex->nChainWork.GetHex()));
result.push_back(Pair("chaintrust", blockindex->nChainTrust.GetHex()));

if (blockindex->pprev)
result.push_back(Pair("previousblockhash", blockindex->pprev->GetBlockHash().GetHex()));
Expand Down Expand Up @@ -1322,7 +1323,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
obj.push_back(Pair("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()));
obj.push_back(Pair("verificationprogress", GuessVerificationProgress(Params().TxData(), chainActive.Tip())));
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
obj.push_back(Pair("chaintrust", chainActive.Tip()->nChainTrust.GetHex()));
obj.push_back(Pair("pruned", fPruneMode));

const Consensus::Params& consensusParams = Params().GetConsensus();
Expand Down Expand Up @@ -1462,7 +1463,7 @@ UniValue getchaintips(const JSONRPCRequest& request)
obj.push_back(Pair("height", block->nHeight));
obj.push_back(Pair("hash", block->phashBlock->GetHex()));
obj.push_back(Pair("difficulty", GetDifficulty(block)));
obj.push_back(Pair("chainwork", block->nChainWork.GetHex()));
obj.push_back(Pair("chaintrust", block->nChainTrust.GetHex()));
obj.push_back(Pair("branchlen", branchLen));
obj.push_back(Pair("forkpoint", pindexFork->phashBlock->GetHex()));

Expand Down
2 changes: 1 addition & 1 deletion src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ UniValue GetNetworkHashPS(int lookup, int height) {
if (minTime == maxTime)
return 0;

arith_uint256 workDiff = pb->nChainWork - pb0->nChainWork;
arith_uint256 workDiff = pb->nChainTrust - pb0->nChainTrust;
int64_t timeDiff = maxTime - minTime;

return workDiff.getdouble() / timeDiff;
Expand Down
2 changes: 1 addition & 1 deletion src/test/pow_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
blocks[i].nHeight = i;
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
blocks[i].nChainTrust = i ? blocks[i - 1].nChainTrust + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
}

for (int j = 0; j < 1000; j++) {
Expand Down
Loading

0 comments on commit 56fc0fb

Please sign in to comment.