Ethereum  PoC-8
The C++ Implementation of Ethereum
StateCacheDB.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 #pragma once
19 
20 #include "Common.h"
21 #include "Log.h"
22 #include "RLP.h"
23 
24 namespace dev
25 {
27 {
28  friend class EnforceRefs;
29 
30 public:
32  StateCacheDB(StateCacheDB const& _c) { operator=(_c); }
33 
35 
36  virtual ~StateCacheDB() = default;
37 
38  void clear()
39  {
40  m_main.clear();
41  m_aux.clear();
42  } // WARNING !!!! didn't originally clear m_refCount!!!
43  std::unordered_map<h256, std::string> get() const;
44 
45  std::string lookup(h256 const& _h) const;
46  bool exists(h256 const& _h) const;
47  void insert(h256 const& _h, bytesConstRef _v);
48  bool kill(h256 const& _h);
49  void purge();
50 
51  bytes lookupAux(h256 const& _h) const;
52  void removeAux(h256 const& _h);
53  void insertAux(h256 const& _h, bytesConstRef _v);
54 
55  h256Hash keys() const;
56 
57 protected:
58 #if DEV_GUARDED_DB
59  mutable SharedMutex x_this;
60 #endif
61  std::unordered_map<h256, std::pair<std::string, unsigned>> m_main;
62  std::unordered_map<h256, std::pair<bytes, bool>> m_aux;
63 
64  mutable bool m_enforceRefs = false;
65 };
66 
68 {
69 public:
70  EnforceRefs(StateCacheDB const& _o, bool _r) : m_o(_o), m_r(_o.m_enforceRefs)
71  {
72  _o.m_enforceRefs = _r;
73  }
74  ~EnforceRefs() { m_o.m_enforceRefs = m_r; }
75 
76 private:
77  StateCacheDB const& m_o;
78  bool m_r;
79 };
80 
81 inline std::ostream& operator<<(std::ostream& _out, StateCacheDB const& _m)
82 {
83  for (auto const& i : _m.get())
84  {
85  _out << i.first << ": ";
86  _out << RLP(i.second);
87  _out << " " << toHex(i.second);
88  _out << std::endl;
89  }
90  return _out;
91 }
92 
93 } // namespace dev
dev::EnforceRefs::EnforceRefs
EnforceRefs(StateCacheDB const &_o, bool _r)
Definition: StateCacheDB.h:70
dev::SharedMutex
boost::shared_mutex SharedMutex
Definition: Guards.h:39
dev::vector_ref< byte const >
dev::StateCacheDB::get
std::unordered_map< h256, std::string > get() const
Definition: StateCacheDB.cpp:27
dev::StateCacheDB::exists
bool exists(h256 const &_h) const
Definition: StateCacheDB.cpp:68
dev::StateCacheDB::lookup
std::string lookup(h256 const &_h) const
Definition: StateCacheDB.cpp:52
dev::FixedHash< 32 >
dev::operator<<
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:77
Common.h
dev::h256Hash
std::unordered_set< h256 > h256Hash
Definition: FixedHash.h:365
dev::StateCacheDB::StateCacheDB
StateCacheDB()
Definition: StateCacheDB.h:31
dev::StateCacheDB::StateCacheDB
StateCacheDB(StateCacheDB const &_c)
Definition: StateCacheDB.h:32
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::StateCacheDB::kill
bool kill(h256 const &_h)
Definition: StateCacheDB.cpp:97
dev::StateCacheDB::insert
void insert(h256 const &_h, bytesConstRef _v)
Definition: StateCacheDB.cpp:79
dev::StateCacheDB::purge
void purge()
Definition: StateCacheDB.cpp:153
dev::StateCacheDB::m_aux
std::unordered_map< h256, std::pair< bytes, bool > > m_aux
Definition: StateCacheDB.h:62
dev::StateCacheDB::operator=
StateCacheDB & operator=(StateCacheDB const &_c)
Definition: StateCacheDB.cpp:39
dev::StateCacheDB::m_main
std::unordered_map< h256, std::pair< std::string, unsigned > > m_main
Definition: StateCacheDB.h:61
dev::EnforceRefs::~EnforceRefs
~EnforceRefs()
Definition: StateCacheDB.h:74
dev::StateCacheDB::clear
void clear()
Definition: StateCacheDB.h:38
dev::EnforceRefs
Definition: StateCacheDB.h:68
dev::StateCacheDB::lookupAux
bytes lookupAux(h256 const &_h) const
Definition: StateCacheDB.cpp:126
dev::StateCacheDB
Definition: StateCacheDB.h:27
dev::StateCacheDB::~StateCacheDB
virtual ~StateCacheDB()=default
dev::StateCacheDB::removeAux
void removeAux(h256 const &_h)
Definition: StateCacheDB.cpp:137
dev::StateCacheDB::keys
h256Hash keys() const
Definition: StateCacheDB.cpp:173
dev
Definition: Address.cpp:21
dev::toHex
std::string toHex(Iterator _it, Iterator _end, std::string const &_prefix)
Definition: CommonData.h:46
dev::RLP
Definition: RLP.h:48
dev::StateCacheDB::insertAux
void insertAux(h256 const &_h, bytesConstRef _v)
Definition: StateCacheDB.cpp:145
Log.h
RLP.h
dev::StateCacheDB::m_enforceRefs
bool m_enforceRefs
Definition: StateCacheDB.h:64