You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

skiplist_tests.cpp 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright (c) 2014 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "main.h"
  5. #include "random.h"
  6. #include "util.h"
  7. #include "test/test_bitcoin.h"
  8. #include <vector>
  9. #include <boost/test/unit_test.hpp>
  10. #define SKIPLIST_LENGTH 300000
  11. BOOST_FIXTURE_TEST_SUITE(skiplist_tests, BasicTestingSetup)
  12. BOOST_AUTO_TEST_CASE(skiplist_test)
  13. {
  14. std::vector<CBlockIndex> vIndex(SKIPLIST_LENGTH);
  15. for (int i=0; i<SKIPLIST_LENGTH; i++) {
  16. vIndex[i].nHeight = i;
  17. vIndex[i].pprev = (i == 0) ? NULL : &vIndex[i - 1];
  18. vIndex[i].BuildSkip();
  19. }
  20. for (int i=0; i<SKIPLIST_LENGTH; i++) {
  21. if (i > 0) {
  22. BOOST_CHECK(vIndex[i].pskip == &vIndex[vIndex[i].pskip->nHeight]);
  23. BOOST_CHECK(vIndex[i].pskip->nHeight < i);
  24. } else {
  25. BOOST_CHECK(vIndex[i].pskip == NULL);
  26. }
  27. }
  28. for (int i=0; i < 1000; i++) {
  29. int from = insecure_rand() % (SKIPLIST_LENGTH - 1);
  30. int to = insecure_rand() % (from + 1);
  31. BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
  32. BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
  33. BOOST_CHECK(vIndex[from].GetAncestor(0) == &vIndex[0]);
  34. }
  35. }
  36. BOOST_AUTO_TEST_CASE(getlocator_test)
  37. {
  38. // Build a main chain 100000 blocks long.
  39. std::vector<uint256> vHashMain(100000);
  40. std::vector<CBlockIndex> vBlocksMain(100000);
  41. for (unsigned int i=0; i<vBlocksMain.size(); i++) {
  42. vHashMain[i] = ArithToUint256(i); // Set the hash equal to the height, so we can quickly check the distances.
  43. vBlocksMain[i].nHeight = i;
  44. vBlocksMain[i].pprev = i ? &vBlocksMain[i - 1] : NULL;
  45. vBlocksMain[i].phashBlock = &vHashMain[i];
  46. vBlocksMain[i].BuildSkip();
  47. BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksMain[i].GetBlockHash()).GetLow64(), vBlocksMain[i].nHeight);
  48. BOOST_CHECK(vBlocksMain[i].pprev == NULL || vBlocksMain[i].nHeight == vBlocksMain[i].pprev->nHeight + 1);
  49. }
  50. // Build a branch that splits off at block 49999, 50000 blocks long.
  51. std::vector<uint256> vHashSide(50000);
  52. std::vector<CBlockIndex> vBlocksSide(50000);
  53. for (unsigned int i=0; i<vBlocksSide.size(); i++) {
  54. vHashSide[i] = ArithToUint256(i + 50000 + (arith_uint256(1) << 128)); // Add 1<<128 to the hashes, so GetLow64() still returns the height.
  55. vBlocksSide[i].nHeight = i + 50000;
  56. vBlocksSide[i].pprev = i ? &vBlocksSide[i - 1] : &vBlocksMain[49999];
  57. vBlocksSide[i].phashBlock = &vHashSide[i];
  58. vBlocksSide[i].BuildSkip();
  59. BOOST_CHECK_EQUAL((int)UintToArith256(vBlocksSide[i].GetBlockHash()).GetLow64(), vBlocksSide[i].nHeight);
  60. BOOST_CHECK(vBlocksSide[i].pprev == NULL || vBlocksSide[i].nHeight == vBlocksSide[i].pprev->nHeight + 1);
  61. }
  62. // Build a CChain for the main branch.
  63. CChain chain;
  64. chain.SetTip(&vBlocksMain.back());
  65. // Test 100 random starting points for locators.
  66. for (int n=0; n<100; n++) {
  67. int r = insecure_rand() % 150000;
  68. CBlockIndex* tip = (r < 100000) ? &vBlocksMain[r] : &vBlocksSide[r - 100000];
  69. CBlockLocator locator = chain.GetLocator(tip);
  70. // The first result must be the block itself, the last one must be genesis.
  71. BOOST_CHECK(locator.vHave.front() == tip->GetBlockHash());
  72. BOOST_CHECK(locator.vHave.back() == vBlocksMain[0].GetBlockHash());
  73. // Entries 1 through 11 (inclusive) go back one step each.
  74. for (unsigned int i = 1; i < 12 && i < locator.vHave.size() - 1; i++) {
  75. BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i]).GetLow64(), tip->nHeight - i);
  76. }
  77. // The further ones (excluding the last one) go back with exponential steps.
  78. unsigned int dist = 2;
  79. for (unsigned int i = 12; i < locator.vHave.size() - 1; i++) {
  80. BOOST_CHECK_EQUAL(UintToArith256(locator.vHave[i - 1]).GetLow64() - UintToArith256(locator.vHave[i]).GetLow64(), dist);
  81. dist *= 2;
  82. }
  83. }
  84. }
  85. BOOST_AUTO_TEST_SUITE_END()