PartitionCheck tests for shorter block times

This commit is contained in:
Eirik Ogilvie-Wigley 2019-07-31 12:20:15 -06:00
parent 1188c9adf7
commit e358e89db9
1 changed files with 48 additions and 22 deletions

View File

@ -19,6 +19,7 @@
#include "streams.h"
#include "util.h"
#include "utilstrencodings.h"
#include "utiltest.h"
#include "test/test_bitcoin.h"
@ -385,41 +386,40 @@ BOOST_AUTO_TEST_CASE(AlertDisablesRPC)
static bool falseFunc(const CChainParams&) { return false; }
BOOST_AUTO_TEST_CASE(PartitionAlert)
void PartitionAlertTestImpl(const Consensus::Params& params, int startTime, int expectedTotal, int expectedSlow, int expectedFast)
{
// Test PartitionCheck
CCriticalSection csDummy;
CBlockIndex indexDummy[400];
CChainParams& params = Params(CBaseChainParams::MAIN);
int64_t nPowTargetSpacing = params.GetConsensus().PoWTargetSpacing(0); // First 400 blocks are pre-Blossom
CBlockIndex indexDummy[800];
// Generate fake blockchain timestamps relative to
// an arbitrary time:
int64_t now = 1427379054;
SetMockTime(now);
for (int i = 0; i < 400; i++)
int64_t start = startTime;
for (int i = 0; i < 800; i++)
{
indexDummy[i].phashBlock = NULL;
if (i == 0) indexDummy[i].pprev = NULL;
else indexDummy[i].pprev = &indexDummy[i-1];
indexDummy[i].pprev = i ? &indexDummy[i-1] : NULL;
indexDummy[i].nHeight = i;
indexDummy[i].nTime = now - (400-i)*nPowTargetSpacing;
indexDummy[i].nTime = i ? indexDummy[i - 1].nTime + params.PoWTargetSpacing(i) : start;
// Other members don't matter, the partition check code doesn't
// use them
}
// Generate fake blockchain timestamps relative to
// an arbitrary time:
int64_t now = indexDummy[799].nTime + params.PoWTargetSpacing(800);
SetMockTime(now);
// Test 1: chain with blocks every nPowTargetSpacing seconds,
// as normal, no worries:
strMiscWarning = "";
PartitionCheck(falseFunc, csDummy, &indexDummy[399]);
PartitionCheck(falseFunc, csDummy, &indexDummy[799]);
BOOST_CHECK_EQUAL("", strMiscWarning);
// Test 2: go 3.5 hours without a block, expect a warning:
now += 3*60*60+30*60;
SetMockTime(now);
strMiscWarning = "";
PartitionCheck(falseFunc, csDummy, &indexDummy[399]);
BOOST_CHECK_EQUAL("WARNING: check your network connection, 12 blocks received in the last 4 hours (96 expected)", strMiscWarning);
PartitionCheck(falseFunc, csDummy, &indexDummy[799]);
std::string expectedSlowErr = strprintf("WARNING: check your network connection, %d blocks received in the last 4 hours (%d expected)", expectedSlow, expectedTotal);
BOOST_CHECK_EQUAL(expectedSlowErr, strMiscWarning);
BOOST_TEST_MESSAGE(std::string("Got alert text: ")+strMiscWarning);
// Test 3: test the "partition alerts only go off once per day"
@ -427,24 +427,50 @@ BOOST_AUTO_TEST_CASE(PartitionAlert)
now += 60*10;
SetMockTime(now);
strMiscWarning = "";
PartitionCheck(falseFunc, csDummy, &indexDummy[399]);
PartitionCheck(falseFunc, csDummy, &indexDummy[799]);
BOOST_CHECK_EQUAL("", strMiscWarning);
// Test 4: get 2.5 times as many blocks as expected:
now += 60*60*24; // Pretend it is a day later
start = now + 60*60*24; // Pretend it is a day later
for (int i = 0; i < 800; i++) {
// Tweak chain timestamps:
indexDummy[i].nTime = i ? indexDummy[i - 1].nTime + params.PoWTargetSpacing(i) * 2/5 : start;
}
now = indexDummy[799].nTime + params.PoWTargetSpacing(0) * 2/5;
SetMockTime(now);
int64_t quickSpacing = nPowTargetSpacing*2/5;
for (int i = 0; i < 400; i++) // Tweak chain timestamps:
indexDummy[i].nTime = now - (400-i)*quickSpacing;
strMiscWarning = "";
PartitionCheck(falseFunc, csDummy, &indexDummy[399]);
BOOST_CHECK_EQUAL("WARNING: abnormally high number of blocks generated, 240 blocks received in the last 4 hours (96 expected)", strMiscWarning);
PartitionCheck(falseFunc, csDummy, &indexDummy[799]);
std::string expectedFastErr = strprintf("WARNING: abnormally high number of blocks generated, %d blocks received in the last 4 hours (%d expected)", expectedFast, expectedTotal);
BOOST_CHECK_EQUAL(expectedFastErr, strMiscWarning);
BOOST_TEST_MESSAGE(std::string("Got alert text: ")+strMiscWarning);
strMiscWarning = "";
SetMockTime(0);
}
BOOST_AUTO_TEST_CASE(PartitionAlert)
{
CChainParams& params = Params(CBaseChainParams::MAIN);
PartitionAlertTestImpl(params.GetConsensus(), 1000000000, 96, 12, 240);
}
BOOST_AUTO_TEST_CASE(PartitionAlertBlossomOnly)
{
PartitionAlertTestImpl(RegtestActivateBlossom(false), 1500000000, 96 * 2, 12 * 2, 240 * 2);
RegtestDeactivateBlossom();
}
BOOST_AUTO_TEST_CASE(PartitionAlertBlossomActivates)
{
// 48 pre blossom blocks, 96 blossom blocks will take 48 * 150s + 96 * 75s = 4hrs
// in the slow case, all of the blocks will be blossom blocks
// in the fast case, 96 blocks will be blossom => 96 * 75s * 2/5 = 2880s spent on on blossom
// => (14400 - 2880) / (150 * 2/5) = 11520 / 60 = 192 pre blossom blocks
PartitionAlertTestImpl(RegtestActivateBlossom(false, 799 - 96), 2000000000, 144, 12 * 2, 192 + 96);
RegtestDeactivateBlossom();
}
BOOST_AUTO_TEST_SUITE_END()
#endif