Skip to content

Commit

Permalink
[PoS] Keep v1 miner until hard-fork
Browse files Browse the repository at this point in the history
  • Loading branch information
random-zebra committed Nov 20, 2019
1 parent ec45670 commit 0bcf0c1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
49 changes: 39 additions & 10 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ bool GetHashProofOfStake(const CBlockIndex* pindexPrev, CStakeInput* stake, cons

bool Stake(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int nBits, int64_t& nTimeTx, uint256& hashProofOfStake)
{
int prevHeight = pindexPrev->nHeight;
const int nHeight = pindexPrev->nHeight + 1;

// get stake input pindex
CBlockIndex* pindexFrom = stakeInput->GetIndexFrom();
Expand All @@ -362,21 +362,50 @@ bool Stake(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int
const int nHeightBlockFrom = pindexFrom->nHeight;

// check for maturity (min age/depth) requirements
if (!Params().HasStakeMinAgeOrDepth(prevHeight + 1, nTimeTx, nHeightBlockFrom, nTimeBlockFrom))
if (!Params().HasStakeMinAgeOrDepth(nHeight, nTimeTx, nHeightBlockFrom, nTimeBlockFrom))
return error("%s : min age violation - height=%d - nTimeTx=%d, nTimeBlockFrom=%d, nHeightBlockFrom=%d",
__func__, prevHeight + 1, nTimeTx, nTimeBlockFrom, nHeightBlockFrom);
__func__, nHeight, nTimeTx, nTimeBlockFrom, nHeightBlockFrom);

// Time protocol V2: one-try
if (Params().IsTimeProtocolV2(nHeight)) {
nTimeTx = GetCurrentTimeSlot();
mapHashedBlocks.clear();
mapHashedBlocks[pindexPrev->nHeight] = nTimeTx; //store a time stamp of when we last hashed on this block
return CheckStakeKernelHash(pindexPrev, nBits, stakeInput, nTimeTx, hashProofOfStake);
}

nTimeTx = GetCurrentTimeSlot();
// Time protocol V1: iterate the hashing (can be removed after hard-fork)
return StakeV1(pindexPrev, stakeInput, nBits, nTimeTx, hashProofOfStake);
}

// new block came in, move on
if (chainActive.Height() != prevHeight)
return false;
bool StakeV1(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int nBits, int64_t& nTimeTx, uint256& hashProofOfStake)
{
bool fSuccess = false;
nTimeTx = pindexPrev->nTime;
unsigned int nTryTime = nTimeTx;
// iterate from nTimeTx up to nTimeTx + nHashDrift
// but not after the max allowed future blocktime drift (3 minutes for PoS)
const unsigned int maxTime = GetAdjustedTime() + 180;

while (nTryTime < maxTime) {
//new block came in, move on
if (chainActive.Height() != pindexPrev->nHeight) break;

++nTryTime;
// if stake hash does not meet the target then continue to next iteration
if (!CheckStakeKernelHash(pindexPrev, nBits, stakeInput, nTryTime, hashProofOfStake))
continue;

// if we made it this far, then we have successfully found a valid kernel hash
fSuccess = true;
nTimeTx = nTryTime;
break;
}

mapHashedBlocks.clear();
mapHashedBlocks[chainActive.Tip()->nHeight] = nTimeTx; //store a time stamp of when we last hashed on this block
mapHashedBlocks[pindexPrev->nHeight] = GetTime(); //store a time stamp of when we last hashed on this block

// check stake hash target protocol
return CheckStakeKernelHash(pindexPrev, nBits, stakeInput, nTimeTx, hashProofOfStake);
return fSuccess;
}

bool ContextualCheckZerocoinStake(int nPreviousBlockHeight, CStakeInput* stake)
Expand Down
1 change: 1 addition & 0 deletions src/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64_t& nStakeModifier, int
bool ComputeNextStakeModifier(const CBlockIndex* pindexPrev, uint64_t& nStakeModifier, bool& fGeneratedStakeModifier);
uint256 ComputeStakeModifier(const CBlockIndex* pindexPrev, const uint256& kernel);
bool Stake(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int nBits, int64_t& nTimeTx, uint256& hashProofOfStake);
bool StakeV1(const CBlockIndex* pindexPrev, CStakeInput* stakeInput, unsigned int nBits, int64_t& nTimeTx, uint256& hashProofOfStake);

// Initialize the stake input object
bool initStakeInput(const CBlock block, std::unique_ptr<CStakeInput>& stake, int nPreviousBlockHeight);
Expand Down
12 changes: 7 additions & 5 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,14 +699,16 @@ void BitcoinMiner(CWallet* pwallet, bool fProofOfStake)
}
}

const bool fTimeV2 = Params().IsTimeProtocolV2(chainActive.Height()+1);
//search our map of hashed blocks, see if bestblock has been hashed yet
if (mapHashedBlocks.count(chainActive.Tip()->nHeight) && !fLastLoopOrphan)
const int chainHeight = chainActive.Height();
if (mapHashedBlocks.count(chainHeight) && !fLastLoopOrphan)
{
int64_t tipHashTime = mapHashedBlocks[chainActive.Tip()->nHeight];
// 1 second check until we get to the next time slot
while (GetCurrentTimeSlot() <= tipHashTime)
int64_t tipHashTime = mapHashedBlocks[chainHeight];
if ( (!fTimeV2 && GetTime() < tipHashTime + 22) ||
(fTimeV2 && GetCurrentTimeSlot() <= tipHashTime) )
{
MilliSleep(1000);
MilliSleep(2000);
continue;
}
}
Expand Down

0 comments on commit 0bcf0c1

Please sign in to comment.