11 changes: 7 additions & 4 deletions src/test/miner_tests.cpp
Expand Up @@ -64,6 +64,7 @@ CBlockIndex CreateBlockIndex(int nHeight)
CBlockIndex index;
index.nHeight = nHeight;
index.pprev = chainActive.Tip();
index.InitialiseFromPrev();
return index;
}

Expand Down Expand Up @@ -208,7 +209,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
{
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
pblock->nDeploymentSoft = 1;
pblock->nTime = chainActive.Tip()->GetMedianTimePast()+1;
pblock->nTTime = chainActive.Tip()->GetMedianTimePast()+1;
CMutableTransaction txCoinbase(*pblock->vtx[0]);
txCoinbase.nVersion = 1;
txCoinbase.vin[0].scriptSig = CScript();
Expand Down Expand Up @@ -363,6 +364,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
pcoinsTip->SetBestBlock(next->GetBlockHash());
next->pprev = prev;
next->nHeight = prev->nHeight + 1;
next->InitialiseFromPrev();
next->BuildSkip();
chainActive.SetTip(next);
}
Expand All @@ -375,6 +377,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
pcoinsTip->SetBestBlock(next->GetBlockHash());
next->pprev = prev;
next->nHeight = prev->nHeight + 1;
next->InitialiseFromPrev();
next->BuildSkip();
chainActive.SetTip(next);
}
Expand Down Expand Up @@ -423,10 +426,10 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
BOOST_CHECK(!TestSequenceLocks(tx, flags)); // Sequence locks fail

for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTTime += 512; //Trick the MedianTimePast
BOOST_CHECK(SequenceLocks(tx, flags, &prevheights, CreateBlockIndex(chainActive.Tip()->nHeight + 1))); // Sequence locks pass 512 seconds later
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime -= 512; //undo tricked MTP
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTTime -= 512; //undo tricked MTP

// absolute height locked
tx.vin[0].prevout.hash = txFirst[2]->GetHash();
Expand Down Expand Up @@ -473,7 +476,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 3);
// However if we advance height by 1 and time by 512, all of them should be mined
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime += 512; //Trick the MedianTimePast
chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTTime += 512; //Trick the MedianTimePast
chainActive.Tip()->nHeight++;
SetMockTime(chainActive.Tip()->GetMedianTimePast() + 1);

Expand Down
15 changes: 10 additions & 5 deletions src/test/pow_tests.cpp
Expand Up @@ -24,7 +24,8 @@ BOOST_AUTO_TEST_CASE(get_next_work)
int64_t nLastRetargetTime = 1261130161; // Block #30240
CBlockIndex pindexLast;
pindexLast.nHeight = 32255;
pindexLast.nTime = 1262152739; // Block #32255
pindexLast.nHTime = 0;
pindexLast.nTTime = 1262152739; // Block #32255
pindexLast.nBits = 0x1d00ffff;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00d86a);
}
Expand All @@ -38,7 +39,8 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
int64_t nLastRetargetTime = 1231006505; // Block #0
CBlockIndex pindexLast;
pindexLast.nHeight = 2015;
pindexLast.nTime = 1233061996; // Block #2015
pindexLast.nHTime = 0;
pindexLast.nTTime = 1233061996; // Block #2015
pindexLast.nBits = 0x1d00ffff;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00ffff);
}
Expand All @@ -52,7 +54,8 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
int64_t nLastRetargetTime = 1279008237; // Block #66528
CBlockIndex pindexLast;
pindexLast.nHeight = 68543;
pindexLast.nTime = 1279297671; // Block #68543
pindexLast.nHTime = 0;
pindexLast.nTTime = 1279297671; // Block #68543
pindexLast.nBits = 0x1c05a3f4;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1c0168fd);
}
Expand All @@ -66,7 +69,8 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
int64_t nLastRetargetTime = 1263163443; // NOTE: Not an actual block time
CBlockIndex pindexLast;
pindexLast.nHeight = 46367;
pindexLast.nTime = 1269211443; // Block #46367
pindexLast.nHTime = 0;
pindexLast.nTTime = 1269211443; // Block #46367
pindexLast.nBits = 0x1c387f6f;
BOOST_CHECK_EQUAL(CalculateNextWorkRequired(&pindexLast, nLastRetargetTime, params), 0x1d00e1fd);
}
Expand All @@ -80,8 +84,9 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
for (int i = 0; i < 10000; i++) {
blocks[i].pprev = i ? &blocks[i - 1] : NULL;
blocks[i].nHeight = i;
blocks[i].nTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nTTime = 1269211443 + i * params.nPowTargetSpacing;
blocks[i].nBits = 0x207fffff; /* target 0x7fffff000... */
blocks[i].InitialiseFromPrev();
blocks[i].nChainWork = i ? blocks[i - 1].nChainWork + GetBlockProof(blocks[i - 1]) : arith_uint256(0);
}

Expand Down
5 changes: 3 additions & 2 deletions src/test/versionbits_tests.cpp
Expand Up @@ -66,13 +66,14 @@ class VersionBitsTester
Reset();
}

VersionBitsTester& Mine(unsigned int height, int32_t nTime, uint32_t nDeploymentSoft) {
VersionBitsTester& Mine(unsigned int height, int32_t nTTime, uint32_t nDeploymentSoft) {
while (vpblock.size() < height) {
CBlockIndex* pindex = new CBlockIndex();
pindex->nHeight = vpblock.size();
pindex->pprev = vpblock.size() > 0 ? vpblock.back() : NULL;
pindex->nTime = nTime;
pindex->nTTime = nTTime;
pindex->nDeploymentSoft = nDeploymentSoft;
pindex->InitialiseFromPrev();
pindex->BuildSkip();
vpblock.push_back(pindex);
}
Expand Down
2 changes: 1 addition & 1 deletion src/txdb.cpp
Expand Up @@ -193,7 +193,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256
pindexNew->nUndoPos = diskindex.nUndoPos;
pindexNew->nDeploymentSoft= diskindex.nDeploymentSoft;
pindexNew->hashMerkleRoot = diskindex.hashMerkleRoot;
pindexNew->nTime = diskindex.nTime;
pindexNew->nTTime = diskindex.nTTime;
pindexNew->nBits = diskindex.nBits;
pindexNew->nNonce = diskindex.nNonce;
pindexNew->nStatus = diskindex.nStatus;
Expand Down
12 changes: 7 additions & 5 deletions src/validation.cpp
Expand Up @@ -2880,11 +2880,12 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");

// Check timestamp against prev
if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
int64_t nBlockTime = block.GetBlockTime(pindexPrev->GetBlockTime());
if (nBlockTime <= pindexPrev->GetMedianTimePast())
return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");

// Check timestamp
if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
if (nBlockTime > nAdjustedTime + 2 * 60 * 60)
return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");

// Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
Expand All @@ -2910,7 +2911,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co

int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
? pindexPrev->GetMedianTimePast()
: block.GetBlockTime();
: block.GetBlockTime(pindexPrev->GetBlockTime());

// Check that all transactions are finalized
for (const auto& tx : block.vtx) {
Expand Down Expand Up @@ -3099,7 +3100,7 @@ static bool AcceptBlock(const CBlock& block, CValidationState& state, const CCha
CDiskBlockPos blockPos;
if (dbp != NULL)
blockPos = *dbp;
if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, pindex->GetBlockTime(), dbp != NULL))
return error("AcceptBlock(): FindBlockPos failed");
if (dbp == NULL)
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
Expand Down Expand Up @@ -3153,6 +3154,7 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
CBlockIndex indexDummy(block);
indexDummy.pprev = pindexPrev;
indexDummy.nHeight = pindexPrev->nHeight + 1;
indexDummy.InitialiseFromPrev();

// NOTE: CheckBlockHeader is called by CheckBlock
if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
Expand Down Expand Up @@ -3694,7 +3696,7 @@ bool InitBlockIndex(const CChainParams& chainparams)
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
CDiskBlockPos blockPos;
CValidationState state;
if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime(0)))
return error("LoadBlockIndex(): FindBlockPos failed");
if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
return error("LoadBlockIndex(): writing genesis block to disk failed");
Expand Down