MemoryDB.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 MemoryDB.h
  15. * @author Gav Wood <i@gavwood.com>
  16. * @date 2014
  17. */
  18. #pragma once
  19. #include <unordered_map>
  20. #include "Common.h"
  21. #include "Guards.h"
  22. #include "FixedHash.h"
  23. #include "Log.h"
  24. #include "RLP.h"
  25. #include "SHA3.h"
  26. namespace dev
  27. {
  28. struct DBChannel: public LogChannel { static const char* name(); static const int verbosity = 18; };
  29. struct DBWarn: public LogChannel { static const char* name(); static const int verbosity = 1; };
  30. #define dbdebug clog(DBChannel)
  31. #define dbwarn clog(DBWarn)
  32. class MemoryDB
  33. {
  34. friend class EnforceRefs;
  35. public:
  36. MemoryDB() {}
  37. MemoryDB(MemoryDB const& _c) { operator=(_c); }
  38. MemoryDB& operator=(MemoryDB const& _c);
  39. void clear() { m_main.clear(); m_aux.clear(); } // WARNING !!!! didn't originally clear m_refCount!!!
  40. std::unordered_map<h256, std::string> get() const;
  41. std::string lookup(h256 const& _h) const;
  42. bool exists(h256 const& _h) const;
  43. void insert(h256 const& _h, bytesConstRef _v);
  44. bool kill(h256 const& _h);
  45. void purge();
  46. bytes lookupAux(h256 const& _h) const;
  47. void removeAux(h256 const& _h);
  48. void insertAux(h256 const& _h, bytesConstRef _v);
  49. h256Hash keys() const;
  50. protected:
  51. #if DEV_GUARDED_DB
  52. mutable SharedMutex x_this;
  53. #endif
  54. std::unordered_map<h256, std::pair<std::string, unsigned>> m_main;
  55. std::unordered_map<h256, std::pair<bytes, bool>> m_aux;
  56. mutable bool m_enforceRefs = false;
  57. };
  58. class EnforceRefs
  59. {
  60. public:
  61. EnforceRefs(MemoryDB const& _o, bool _r): m_o(_o), m_r(_o.m_enforceRefs) { _o.m_enforceRefs = _r; }
  62. ~EnforceRefs() { m_o.m_enforceRefs = m_r; }
  63. private:
  64. MemoryDB const& m_o;
  65. bool m_r;
  66. };
  67. inline std::ostream& operator<<(std::ostream& _out, MemoryDB const& _m)
  68. {
  69. for (auto const& i: _m.get())
  70. {
  71. _out << i.first << ": ";
  72. _out << RLP(i.second);
  73. _out << " " << toHex(i.second);
  74. _out << std::endl;
  75. }
  76. return _out;
  77. }
  78. }