@ -100,4 +100,47 @@ BOOST_AUTO_TEST_CASE(getlocator_test)
}
}
BOOST_AUTO_TEST_CASE ( findearliestatleast_test )
{
std : : vector < uint256 > vHashMain ( 100000 ) ;
std : : vector < CBlockIndex > vBlocksMain ( 100000 ) ;
for ( unsigned int i = 0 ; i < vBlocksMain . size ( ) ; i + + ) {
vHashMain [ i ] = ArithToUint256 ( i ) ; // Set the hash equal to the height
vBlocksMain [ i ] . nHeight = i ;
vBlocksMain [ i ] . pprev = i ? & vBlocksMain [ i - 1 ] : NULL ;
vBlocksMain [ i ] . phashBlock = & vHashMain [ i ] ;
vBlocksMain [ i ] . BuildSkip ( ) ;
if ( i < 10 ) {
vBlocksMain [ i ] . nTime = i ;
vBlocksMain [ i ] . nTimeMax = i ;
} else {
// randomly choose something in the range [MTP, MTP*2]
int64_t medianTimePast = vBlocksMain [ i ] . GetMedianTimePast ( ) ;
int r = insecure_rand ( ) % medianTimePast ;
vBlocksMain [ i ] . nTime = r + medianTimePast ;
vBlocksMain [ i ] . nTimeMax = std : : max ( vBlocksMain [ i ] . nTime , vBlocksMain [ i - 1 ] . nTimeMax ) ;
}
}
// Check that we set nTimeMax up correctly.
unsigned int curTimeMax = 0 ;
for ( unsigned int i = 0 ; i < vBlocksMain . size ( ) ; + + i ) {
curTimeMax = std : : max ( curTimeMax , vBlocksMain [ i ] . nTime ) ;
BOOST_CHECK ( curTimeMax = = vBlocksMain [ i ] . nTimeMax ) ;
}
// Build a CChain for the main branch.
CChain chain ;
chain . SetTip ( & vBlocksMain . back ( ) ) ;
// Verify that FindEarliestAtLeast is correct.
for ( unsigned int i = 0 ; i < 10000 ; + + i ) {
// Pick a random element in vBlocksMain.
int r = insecure_rand ( ) % vBlocksMain . size ( ) ;
int64_t test_time = vBlocksMain [ r ] . nTime ;
CBlockIndex * ret = chain . FindEarliestAtLeast ( test_time ) ;
BOOST_CHECK ( ret - > nTimeMax > = test_time ) ;
BOOST_CHECK ( ( ret - > pprev = = NULL ) | | ret - > pprev - > nTimeMax < test_time ) ;
BOOST_CHECK ( vBlocksMain [ r ] . GetAncestor ( ret - > nHeight ) = = ret ) ;
}
}
BOOST_AUTO_TEST_SUITE_END ( )