overlaydb.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file trie.cpp
  15. * @author Christoph Jentzsch <cj@ethdev.com>
  16. * @date 2015
  17. * OverlayDB tests.
  18. */
  19. #include <boost/test/unit_test.hpp>
  20. #include <libdevcore/TransientDirectory.h>
  21. #include <libdevcore/OverlayDB.h>
  22. #include <test/TestHelper.h>
  23. using namespace std;
  24. using namespace dev;
  25. using namespace dev::test;
  26. BOOST_FIXTURE_TEST_SUITE(OverlayDBTests, TestOutputHelper)
  27. BOOST_AUTO_TEST_CASE(basicUsage)
  28. {
  29. ldb::Options o;
  30. o.max_open_files = 256;
  31. o.create_if_missing = true;
  32. ldb::DB* db = nullptr;
  33. TransientDirectory td;
  34. ldb::Status status = ldb::DB::Open(o, td.path(), &db);
  35. BOOST_REQUIRE(status.ok() && db);
  36. OverlayDB odb(db);
  37. BOOST_CHECK(!odb.get().size());
  38. // commit nothing
  39. odb.commit();
  40. bytes value = fromHex("43");
  41. BOOST_CHECK(!odb.get().size());
  42. odb.insert(h256(42), &value);
  43. BOOST_CHECK(odb.get().size());
  44. BOOST_CHECK(odb.exists(h256(42)));
  45. BOOST_CHECK_EQUAL(odb.lookup(h256(42)), toString(value[0]));
  46. odb.commit();
  47. BOOST_CHECK(!odb.get().size());
  48. BOOST_CHECK(odb.exists(h256(42)));
  49. BOOST_CHECK_EQUAL(odb.lookup(h256(42)), toString(value[0]));
  50. odb.insert(h256(41), &value);
  51. odb.commit();
  52. BOOST_CHECK(!odb.get().size());
  53. BOOST_CHECK(odb.exists(h256(41)));
  54. BOOST_CHECK_EQUAL(odb.lookup(h256(41)), toString(value[0]));
  55. BOOST_CHECK(odb.deepkill(h256(41)));
  56. BOOST_CHECK(!odb.exists(h256(41)));
  57. BOOST_CHECK_EQUAL(odb.lookup(h256(41)), string());
  58. }
  59. BOOST_AUTO_TEST_CASE(auxMem)
  60. {
  61. ldb::Options o;
  62. o.max_open_files = 256;
  63. o.create_if_missing = true;
  64. ldb::DB* db = nullptr;
  65. TransientDirectory td;
  66. ldb::Status status = ldb::DB::Open(o, td.path(), &db);
  67. BOOST_REQUIRE(status.ok() && db);
  68. OverlayDB odb(db);
  69. bytes value = fromHex("43");
  70. bytes valueAux = fromHex("44");
  71. odb.insert(h256(42), &value);
  72. odb.insert(h256(0), &value);
  73. odb.insert(h256(numeric_limits<u256>::max()), &value);
  74. odb.insertAux(h256(42), &valueAux);
  75. odb.insertAux(h256(0), &valueAux);
  76. odb.insertAux(h256(numeric_limits<u256>::max()), &valueAux);
  77. odb.commit();
  78. BOOST_CHECK(!odb.get().size());
  79. BOOST_CHECK(odb.exists(h256(42)));
  80. BOOST_CHECK_EQUAL(odb.lookup(h256(42)), toString(value[0]));
  81. BOOST_CHECK(odb.exists(h256(0)));
  82. BOOST_CHECK_EQUAL(odb.lookup(h256(0)), toString(value[0]));
  83. BOOST_CHECK(odb.exists(h256(std::numeric_limits<u256>::max())));
  84. BOOST_CHECK_EQUAL(odb.lookup(h256(std::numeric_limits<u256>::max())), toString(value[0]));
  85. BOOST_CHECK(odb.lookupAux(h256(42)) == valueAux);
  86. BOOST_CHECK(odb.lookupAux(h256(0)) == valueAux);
  87. BOOST_CHECK(odb.lookupAux(h256(std::numeric_limits<u256>::max())) == valueAux);
  88. }
  89. BOOST_AUTO_TEST_CASE(rollback)
  90. {
  91. ldb::Options o;
  92. o.max_open_files = 256;
  93. o.create_if_missing = true;
  94. ldb::DB* db = nullptr;
  95. TransientDirectory td;
  96. ldb::Status status = ldb::DB::Open(o, td.path(), &db);
  97. BOOST_REQUIRE(status.ok() && db);
  98. OverlayDB odb(db);
  99. bytes value = fromHex("42");
  100. odb.insert(h256(43), &value);
  101. BOOST_CHECK(odb.get().size());
  102. odb.rollback();
  103. BOOST_CHECK(!odb.get().size());
  104. }
  105. BOOST_AUTO_TEST_SUITE_END()