OverlayDB.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 OverlayDB.cpp
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #if !defined(ETH_EMSCRIPTEN)
  19. #include <thread>
  20. #include <libdevcore/db.h>
  21. #include <libdevcore/Common.h>
  22. #include "OverlayDB.h"
  23. using namespace std;
  24. using namespace dev;
  25. namespace dev
  26. {
  27. h256 const EmptyTrie = sha3(rlp(""));
  28. OverlayDB::~OverlayDB()
  29. {
  30. if (m_db.use_count() == 1 && m_db.get())
  31. ctrace << "Closing state DB";
  32. }
  33. class WriteBatchNoter: public ldb::WriteBatch::Handler
  34. {
  35. virtual void Put(ldb::Slice const& _key, ldb::Slice const& _value) { cnote << "Put" << toHex(bytesConstRef(_key)) << "=>" << toHex(bytesConstRef(_value)); }
  36. virtual void Delete(ldb::Slice const& _key) { cnote << "Delete" << toHex(bytesConstRef(_key)); }
  37. };
  38. void OverlayDB::commit()
  39. {
  40. if (m_db)
  41. {
  42. ldb::WriteBatch batch;
  43. // cnote << "Committing nodes to disk DB:";
  44. #if DEV_GUARDED_DB
  45. DEV_READ_GUARDED(x_this)
  46. #endif
  47. {
  48. for (auto const& i: m_main)
  49. {
  50. if (i.second.second)
  51. batch.Put(ldb::Slice((char const*)i.first.data(), i.first.size), ldb::Slice(i.second.first.data(), i.second.first.size()));
  52. // cnote << i.first << "#" << m_main[i.first].second;
  53. }
  54. for (auto const& i: m_aux)
  55. if (i.second.second)
  56. {
  57. bytes b = i.first.asBytes();
  58. b.push_back(255); // for aux
  59. batch.Put(bytesConstRef(&b), bytesConstRef(&i.second.first));
  60. }
  61. }
  62. for (unsigned i = 0; i < 10; ++i)
  63. {
  64. ldb::Status o = m_db->Write(m_writeOptions, &batch);
  65. if (o.ok())
  66. break;
  67. if (i == 9)
  68. {
  69. cwarn << "Fail writing to state database. Bombing out.";
  70. exit(-1);
  71. }
  72. cwarn << "Error writing to state database: " << o.ToString();
  73. WriteBatchNoter n;
  74. batch.Iterate(&n);
  75. cwarn << "Sleeping for" << (i + 1) << "seconds, then retrying.";
  76. this_thread::sleep_for(chrono::seconds(i + 1));
  77. }
  78. #if DEV_GUARDED_DB
  79. DEV_WRITE_GUARDED(x_this)
  80. #endif
  81. {
  82. m_aux.clear();
  83. m_main.clear();
  84. }
  85. }
  86. }
  87. bytes OverlayDB::lookupAux(h256 const& _h) const
  88. {
  89. bytes ret = MemoryDB::lookupAux(_h);
  90. if (!ret.empty() || !m_db)
  91. return ret;
  92. std::string v;
  93. bytes b = _h.asBytes();
  94. b.push_back(255); // for aux
  95. m_db->Get(m_readOptions, bytesConstRef(&b), &v);
  96. if (v.empty())
  97. cwarn << "Aux not found: " << _h;
  98. return asBytes(v);
  99. }
  100. void OverlayDB::rollback()
  101. {
  102. #if DEV_GUARDED_DB
  103. WriteGuard l(x_this);
  104. #endif
  105. m_main.clear();
  106. }
  107. std::string OverlayDB::lookup(h256 const& _h) const
  108. {
  109. std::string ret = MemoryDB::lookup(_h);
  110. if (ret.empty() && m_db)
  111. m_db->Get(m_readOptions, ldb::Slice((char const*)_h.data(), 32), &ret);
  112. return ret;
  113. }
  114. bool OverlayDB::exists(h256 const& _h) const
  115. {
  116. if (MemoryDB::exists(_h))
  117. return true;
  118. std::string ret;
  119. if (m_db)
  120. m_db->Get(m_readOptions, ldb::Slice((char const*)_h.data(), 32), &ret);
  121. return !ret.empty();
  122. }
  123. void OverlayDB::kill(h256 const& _h)
  124. {
  125. #if ETH_PARANOIA || 1
  126. if (!MemoryDB::kill(_h))
  127. {
  128. std::string ret;
  129. if (m_db)
  130. m_db->Get(m_readOptions, ldb::Slice((char const*)_h.data(), 32), &ret);
  131. // No point node ref decreasing for EmptyTrie since we never bother incrementing it in the first place for
  132. // empty storage tries.
  133. if (ret.empty() && _h != EmptyTrie)
  134. cnote << "Decreasing DB node ref count below zero with no DB node. Probably have a corrupt Trie." << _h;
  135. // TODO: for 1.1: ref-counted triedb.
  136. }
  137. #else
  138. MemoryDB::kill(_h);
  139. #endif
  140. }
  141. bool OverlayDB::deepkill(h256 const& _h)
  142. {
  143. // kill in memoryDB
  144. kill(_h);
  145. //kill in overlayDB
  146. ldb::Status s = m_db->Delete(m_writeOptions, ldb::Slice((char const*)_h.data(), 32));
  147. if (s.ok())
  148. return true;
  149. else
  150. return false;
  151. }
  152. }
  153. #endif // ETH_EMSCRIPTEN