23 #include <boost/test/unit_test.hpp> 35 "DifficultyTest[N]" : { 36 "parentTimestamp" : "[PSTAMP]", 37 "parentDifficulty" : "[PDIFF]", 38 "currentTimestamp" : "[СSTAMP]", 39 "currentBlockNumber" : "[CNUM]", 40 "currentDifficulty" : "[CDIFF]" 50 if (_bi.
number() > frontierDiff)
59 block_diff += a * max<int>(
b, -99);
60 block_diff +=
u256(1) << (unsigned)c;
61 block_diff = max<bigint>(minimumDifficulty, block_diff);
63 BOOST_CHECK_MESSAGE(difficulty == block_diff,
"Homestead Check Calculated diff = " << difficulty <<
" expected diff = " << block_diff << _testName);
68 u256 minimumDifficulty;
69 u256 difficultyBoundDivisor;
72 case Network::MainNetwork:
73 case Network::FrontierTest:
74 case Network::HomesteadTest:
75 case Network::Ropsten:
76 case Network::TransitionnetTest:
78 minimumDifficulty = 131072;
79 difficultyBoundDivisor = 2048;
82 cerr <<
"testing undefined network difficulty";
83 durationLimit = _p.
u256Param(
"durationLimit");
84 minimumDifficulty = _p.
u256Param(
"minimumDifficulty");
85 difficultyBoundDivisor = _p.
u256Param(
"difficultyBoundDivisor");
97 block_diff +=
u256(1) << (unsigned)c;
98 block_diff = max<bigint>(minimumDifficulty, block_diff);
100 BOOST_CHECK_MESSAGE(difficulty == block_diff,
"Check Calculated diff = " << difficulty <<
" expected diff = " << block_diff << _testName);
107 ostringstream finalTest;
108 finalTest <<
"{" << std::endl;
111 for (
int stampDelta = 0; stampDelta < 45; stampDelta+=2)
113 for (
u256 blockNumber = 1; blockNumber < 1500000; blockNumber += 25000)
117 string testName =
"DifficultyTest"+
toString(testN);
123 u256 cStamp = pStamp + stampDelta;
124 u256 cNum = blockNumber;
136 std::map<string, string> replaceMap;
137 replaceMap[
"[N]"] =
toString(testN);
145 finalTest << tmptest;
149 finalTest << std::endl <<
"}";
150 string testFile = finalTest.str();
151 testFile = testFile.replace(testFile.find_last_of(
","), 1,
"");
160 BOOST_REQUIRE_MESSAGE(s.length() > 0,
"Contents of '" << _testFileFullName <<
"' is empty. Have you cloned the 'tests' repo branch develop?");
167 string testname = i.first;
190 BOOST_AUTO_TEST_SUITE(DifficultyTests)
195 testFileFullName +=
"/BasicTests/difficultyFrontier.json";
203 testDifficulty(testFileFullName, sealEngine, Network::FrontierTest);
209 testFileFullName +=
"/BasicTests/difficultyRopsten.json";
223 testFileFullName +=
"/BasicTests/difficultyHomestead.json";
231 testDifficulty(testFileFullName, sealEngine, Network::HomesteadTest);
237 testFileFullName +=
"/BasicTests/difficultyMainNetwork.json";
245 testDifficulty(testFileFullName, sealEngine, Network::MainNetwork);
251 testFileFullName +=
"/BasicTests/difficultyCustomMainNetwork.json";
258 u256 homsteadBlockNumber = 1000000;
259 std::vector<u256> blockNumberVector = {homsteadBlockNumber - 100000, homsteadBlockNumber, homsteadBlockNumber + 100000};
260 std::vector<u256> parentDifficultyVector = {1000, 2048, 4000, 1000000};
261 std::vector<int> timestampDeltaVector = {0, 1, 8, 10, 13, 20, 100, 800, 1000, 1500};
264 ostringstream finalTest;
265 finalTest <<
"{" << std::endl;
267 for (
size_t bN = 0; bN < blockNumberVector.size(); bN++)
268 for (
size_t pdN = 0; pdN < parentDifficultyVector.size(); pdN++)
269 for (
size_t tsN = 0; tsN < timestampDeltaVector.size(); tsN++)
272 int stampDelta = timestampDeltaVector.at(tsN);
273 u256 blockNumber = blockNumberVector.at(bN);
274 u256 pDiff = parentDifficultyVector.at(pdN);
277 u256 cStamp = pStamp + stampDelta;
278 u256 cNum = blockNumber;
290 std::map<string, string> replaceMap;
291 replaceMap[
"[N]"] =
toString(testN);
299 finalTest << tmptest;
302 finalTest << std::endl <<
"}";
303 string testFile = finalTest.str();
304 testFile = testFile.replace(testFile.find_last_of(
","), 1,
"");
308 testDifficulty(testFileFullName, sealEngine, Network::MainNetwork);
314 testPath +=
"/BasicTests/difficulty.json";
const Object & get_obj() const
std::string toCompactHex(u256 val, HexPrefix prefix=HexPrefix::DontAdd, unsigned _min=0)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
void testDifficulty(string const &_testFileFullName, Ethash &_sealEngine, Network _n)
static Options const & get(int argc=0, char **argv=0)
Get reference to options The first time used, options are parsed with argc, argv. ...
ChainOperationParams const & chainParams() const
void fillDifficulty(string const &_testFileFullName, Ethash &_sealEngine)
void writeFile(std::string const &_file, bytesConstRef _data, bool _writeDeleteRename=false)
Write the given binary data into the given file, replacing the file if it pre-exists.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend<>> bigint
std::hash for asio::adress
std::string contentsString(std::string const &_file)
Retrieve and returns the contents of the given file as a std::string.
int Add(word *C, const word *A, const word *B, size_t N)
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
BOOST_AUTO_TEST_CASE(difficultyTestsFrontier)
std::string const c_testDifficulty
bool read_string(const String_type &s, Value_type &value)
std::string getTestPath()
static void parseTestWithTypes(std::string &_test, std::map< std::string, std::string > const &_varMap)
Replace keywords in given string with values.
void checkCalculatedDifficulty(BlockHeader const &_bi, BlockHeader const &_parent, Network _n, ChainOperationParams const &_p, string const &_testName="")
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string's (byte) data.
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
u256 calculateDifficulty(BlockHeader const &_bi, BlockHeader const &_parent) const
mConfig::Object_type mObject
u256 u256Param(std::string const &_name) const
Convenience method to get an otherParam as a u256 int.
std::string const & genesisInfo(Network _n)
static u256 randomUniInt(u256 _maxVal=0)
#define BOOST_CHECK_EQUAL(v1, v2)
void setChainParams(ChainOperationParams const &_params)
#define BOOST_AUTO_TEST_SUITE_END()
u256 toInt(json_spirit::mValue const &_v)
Helper functions to work with json::spirit and test files.
static void initTest(int _maxTests=1)
static bool passTest(json_spirit::mObject &_o, std::string &_testName)