Skip to content

Commit

Permalink
Fix staking minimum age detection
Browse files Browse the repository at this point in the history
nBlockTime is not a reliable source for determining when a generated block was created, as it is often '0'.
This fix adds extra logic to guard against this by using GetAdjustedTime() as a fallback option.

Additional debugging output has also been written that can be enabled with the `-debug` or `debug=1`
runtime options.
  • Loading branch information
Fuzzbawls committed Aug 16, 2016
1 parent e50abfd commit 25fca70
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions src/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,21 @@ int64 GetWeight(int64 nIntervalBeginning, int64 nIntervalEnd)
//
// Maximum TimeWeight is 30 days.

//Tranz We are going to want to change this to fix the max weight. Requires a hard fork
//New Code:
//return min(nIntervalEnd - nIntervalBeginning - nStakeMinAge, (int64)nStakeMaxAge);

if (nIntervalBeginning < nForkTime)
return min(nIntervalEnd - nIntervalBeginning, (int64)nStakeMaxAge) - nStakeMinAgeOld;

return min(nIntervalEnd - nIntervalBeginning, (int64)nStakeMaxAge) - nStakeMinAgeNew; // Changed

}

//Tranz We are going to want to change this to fix the max weight. Requires a hard fork
//New Code:
//return min(nIntervalEnd - nIntervalBeginning - nStakeMinAge, (int64)nStakeMaxAge);

if (nIntervalBeginning < nForkTime) {
if (fDebug)
printf("GetWeight(): Weight=%lld\n", min(nIntervalEnd - nIntervalBeginning, (int64)nStakeMaxAge) - nStakeMinAgeOld);
return min(nIntervalEnd - nIntervalBeginning, (int64)nStakeMaxAge) - nStakeMinAgeOld;
} else {
if (fDebug)
printf("GetWeight(): Weight=%lld\n", min(nIntervalEnd - nIntervalBeginning, (int64)nStakeMaxAge) - nStakeMinAgeNew);
return min(nIntervalEnd - nIntervalBeginning, (int64)nStakeMaxAge) - nStakeMinAgeNew; // Changed
}
}

// Get the last stake modifier and its generation time from a given block
static bool GetLastStakeModifier(const CBlockIndex* pindex, uint64& nStakeModifier, int64& nModifierTime)
{
Expand Down Expand Up @@ -234,13 +238,13 @@ static bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64& nStakeModifier
nStakeModifierTime = pindexFrom->GetBlockTime();
int64 nStakeModifierSelectionInterval = GetStakeModifierSelectionInterval();
const CBlockIndex* pindex = pindexFrom;
unsigned int nStakeMinAge;
if (pindexFrom->nTime > nForkTime)
nStakeMinAge = nStakeMinAgeNew;
else
nStakeMinAge = nStakeMinAgeOld;
unsigned int nStakeMinAge;

if (pindexFrom->nTime > nForkTime)
nStakeMinAge = nStakeMinAgeNew;
else
nStakeMinAge = nStakeMinAgeOld;

// loop to find the stake modifier later by a selection interval
while (nStakeModifierTime < pindexFrom->GetBlockTime() + nStakeModifierSelectionInterval)
{
Expand Down Expand Up @@ -288,19 +292,32 @@ static bool GetKernelStakeModifier(uint256 hashBlockFrom, uint64& nStakeModifier
//
bool CheckStakeKernelHash(unsigned int nBits, const CBlock& blockFrom, unsigned int nTxPrevOffset, const CTransaction& txPrev, const COutPoint& prevout, unsigned int nTimeTx, uint256& hashProofOfStake, bool fPrintProofOfStake, unsigned int nBlockTime)
{
unsigned int nStakeMinAge;

if (nBlockTime > nForkTime)
nStakeMinAge = nStakeMinAgeNew;
else
nStakeMinAge = nStakeMinAgeOld;

unsigned int nStakeMinAge;

if (nBlockTime == 0)
nBlockTime = GetAdjustedTime();

if (nBlockTime > nForkTime)
nStakeMinAge = nStakeMinAgeNew;
else
nStakeMinAge = nStakeMinAgeOld;

if (nTimeTx < txPrev.nTime) // Transaction timestamp violation
return error("CheckStakeKernelHash() : nTime violation");

unsigned int nTimeBlockFrom = blockFrom.GetBlockTime(); // Changed
if (nTimeBlockFrom + nStakeMinAge > nTimeTx) // Min age requirement

// Min age requirement
if (nTimeBlockFrom + nStakeMinAge > nTimeTx) {
if (fDebug)
printf("nBlockTime=%s, nTimeBlockFrom=%s, nStakeMinAge=%u, nTimeCombined=%s, nTimeTx=%s\n",
DateTimeStrFormat(nBlockTime).c_str(),
DateTimeStrFormat(nTimeBlockFrom).c_str(),
nStakeMinAge,
DateTimeStrFormat(nTimeBlockFrom + nStakeMinAge).c_str(),
DateTimeStrFormat(nTimeTx).c_str());
return error("CheckStakeKernelHash() : min age violation");
}

CBigNum bnTargetPerCoinDay;
bnTargetPerCoinDay.SetCompact(nBits);
Expand Down

0 comments on commit 25fca70

Please sign in to comment.