Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stakemodifier routines into AddToBlockIndex; #3

Merged
merged 1 commit into from
Jul 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ class CTestNetParams : public CChainParams {
consensus.BIP65Height = 0;
consensus.BIP66Height = 0;
consensus.DIP0001Height = 1;
consensus.DIP0003Height = 1;
consensus.DIP0003Height = 50;
consensus.DIP0003EnforcementHeight = 250;
consensus.DIP0003EnforcementHash = uint256S("0x0000000000000000000000000000000000000000000000000000000000000000");
consensus.powLimit = uint256S("0000fffff0000000000000000000000000000000000000000000000000000000");
Expand Down Expand Up @@ -485,9 +485,9 @@ class CTestNetParams : public CChainParams {
nDefaultPort = 29999;
nPruneAfterHeight = 1000;

genesis = CreateGenesisBlock(1563589000, 62255, 0x1f00ffff, 1, 0 * COIN);
genesis = CreateGenesisBlock(1564329000, 1273, 0x1f00ffff, 1, 0 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();
assert(consensus.hashGenesisBlock == uint256S("0x0000b67f0d64e977df72e86de5ef38e46b03d163790ed9a12e43240915be7197"));
assert(consensus.hashGenesisBlock == uint256S("0x0000aa69de17dfd6989df2a1350ccfe8ed6b591a8d373f2b7a79c1ad3b94e70a"));

vFixedSeeds.clear();
vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));
Expand Down
6 changes: 4 additions & 2 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ static bool GetLastStakeModifier(const CBlockIndex* pindex, uint64_t& nStakeModi
return error("GetLastStakeModifier: null pindex");
while (pindex && pindex->pprev && !pindex->GeneratedStakeModifier())
pindex = pindex->pprev;
if (!pindex->GeneratedStakeModifier())
return error("GetLastStakeModifier: no generation at genesis block");
if (!pindex->GeneratedStakeModifier()) {
nStakeModifier = 0;
return true;
}
nStakeModifier = pindex->nStakeModifier;
nModifierTime = pindex->GetBlockTime();
return true;
Expand Down
98 changes: 33 additions & 65 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ CCriticalSection cs_main;
BlockMap mapBlockIndex;
PrevBlockMap mapPrevBlockIndex;
CChain chainActive;
std::set<std::pair<COutPoint, unsigned int>> setStakeSeen;
CBlockIndex *pindexBestHeader = NULL;
CWaitableCriticalSection csBestBlock;
CConditionVariable cvBlockChange;
Expand Down Expand Up @@ -3157,52 +3158,6 @@ bool ResetBlockFailureFlags(CBlockIndex *pindex) {
return true;
}

bool AcceptProofOfStakeBlock(const CBlock &block, CBlockIndex *pindex)
{
if (!pindex)
return false;

if (block.IsProofOfStake()) {
pindex->SetProofOfStake();
pindex->prevoutStake = block.vtx[1]->vin[0].prevout;
pindex->nStakeTime = block.nTime;
} else {
pindex->prevoutStake.SetNull();
pindex->nStakeTime = 0;
}

// ppcoin: compute chain trust score
pindex->bnChainTrust = (pindex->pprev ? pindex->pprev->bnChainTrust : ArithToUint256(0 + pindex->GetBlockTrust()));

// ppcoin: compute stake entropy bit for stake modifier
if (!pindex->SetStakeEntropyBit(pindex->GetStakeEntropyBit()))
LogPrintf("AcceptProofOfStakeBlock() : SetStakeEntropyBit() failed \n");

// ppcoin: record proof-of-stake hash value
if (pindex->IsProofOfStake()) {
uint256 hash = block.GetHash();
if (!mapProofOfStake.count(hash))
LogPrintf("AcceptProofOfStakeBlock() : hashProofOfStake not found in map \n");
pindex->hashProofOfStake = mapProofOfStake[hash];
}

// ppcoin: compute stake modifier
uint64_t nStakeModifier = 0;
bool fGeneratedStakeModifier = false;
if (!ComputeNextStakeModifier(pindex, nStakeModifier, fGeneratedStakeModifier))
LogPrintf("AcceptProofOfStakeBlock() : ComputeNextStakeModifier() failed \n");
pindex->SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
pindex->nStakeModifierChecksum = GetStakeModifierChecksum(pindex);
if (!CheckStakeModifierCheckpoints(pindex->nHeight, pindex->nStakeModifierChecksum)) {
LogPrintf("AcceptProofOfStakeBlock() : Rejected by stake modifier checkpoint height=%d, modifier=0x%016llx, checksum=0x%08x\n",
pindex->nHeight, nStakeModifier, pindex->nStakeModifierChecksum);
return false;
}

setDirtyBlockIndex.insert(pindex);
return true;
}

CBlockIndex* AddToBlockIndex(const CBlockHeader& block, enum BlockStatus nStatus = BLOCK_VALID_TREE)
{
// Check for duplicate
Expand All @@ -3219,32 +3174,50 @@ CBlockIndex* AddToBlockIndex(const CBlockHeader& block, enum BlockStatus nStatus
// competitive advantage.
pindexNew->nSequenceId = 0;
BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;

//mark as PoS seen
if (pindexNew->IsProofOfStake())
setStakeSeen.insert(std::make_pair(pindexNew->prevoutStake, pindexNew->nTime));

pindexNew->phashBlock = &((*mi).first);
BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
if (miPrev != mapBlockIndex.end())
{
pindexNew->pprev = (*miPrev).second;
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
pindexNew->BuildSkip();

// ppcoin: compute chain trust score
pindexNew->bnChainTrust = (pindexNew->pprev ? pindexNew->pprev->bnChainTrust : ArithToUint256(0 + pindexNew->GetBlockTrust()));

// ppcoin: compute stake entropy bit for stake modifier
if (!pindexNew->SetStakeEntropyBit(pindexNew->GetStakeEntropyBit()))
LogPrintf("AddToBlockIndex() : SetStakeEntropyBit() failed \n");

// ppcoin: record proof-of-stake hash value
if (pindexNew->IsProofOfStake()) {
if (!mapProofOfStake.count(hash))
LogPrintf("AddToBlockIndex() : hashProofOfStake not found in map \n");
pindexNew->hashProofOfStake = mapProofOfStake[hash];
}

// ppcoin: compute stake modifier
uint64_t nStakeModifier = 0;
bool fGeneratedStakeModifier = false;
if (!ComputeNextStakeModifier(pindexNew->pprev, nStakeModifier, fGeneratedStakeModifier))
LogPrintf("AddToBlockIndex() : ComputeNextStakeModifier() failed \n");
pindexNew->SetStakeModifier(nStakeModifier, fGeneratedStakeModifier);
pindexNew->nStakeModifierChecksum = GetStakeModifierChecksum(pindexNew);
if (!CheckStakeModifierCheckpoints(pindexNew->nHeight, pindexNew->nStakeModifierChecksum))
LogPrintf("AddToBlockIndex() : Rejected by stake modifier checkpoint height=%d, modifier=%s \n", pindexNew->nHeight, boost::lexical_cast<std::string>(nStakeModifier));
}
pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
if (nStatus & BLOCK_VALID_MASK) {
pindexNew->RaiseValidity(nStatus);
if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
pindexBestHeader = pindexNew;
} else {
pindexNew->RaiseValidity(BLOCK_VALID_TREE); // required validity level
pindexNew->nStatus |= nStatus;
}
pindexNew->RaiseValidity(BLOCK_VALID_TREE);
if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
pindexBestHeader = pindexNew;

setDirtyBlockIndex.insert(pindexNew);

// track prevBlockHash -> pindex (multimap)
if (pindexNew->pprev) {
mapPrevBlockIndex.emplace(pindexNew->pprev->GetBlockHash(), pindexNew);
}

return pindexNew;
}

Expand Down Expand Up @@ -3756,9 +3729,6 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
return error("%s: %s", __func__, FormatStateMessage(state));
}

if (!AcceptProofOfStakeBlock(block, pindex))
return false;

// Header is valid/has work, merkle tree is good...RELAY NOW
// (but if it does not build on our best tip, let the SendMessages loop relay it)
if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
Expand Down Expand Up @@ -4396,8 +4366,6 @@ static bool AddGenesisBlock(const CChainParams& chainparams, const CBlock& block
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
return error("%s: writing genesis block to disk failed", __func__);
CBlockIndex *pindex = AddToBlockIndex(block);
if (!AcceptProofOfStakeBlock(block, pindex))
return error("%s: genesis block not accepted", __func__);
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
return error("%s: genesis block not accepted", __func__);
return true;
Expand Down