Fabcoin Core  0.16.2
P2P Digital Currency
OverlayDB.cpp
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 */
21 #if !defined(ETH_EMSCRIPTEN)
22 
23 #include <thread>
24 #include <libdevcore/db.h>
25 #include <libdevcore/Common.h>
26 #include "OverlayDB.h"
27 using namespace std;
28 using namespace dev;
29 
30 namespace dev
31 {
32 
33 h256 const EmptyTrie = sha3(rlp(""));
34 
35 OverlayDB::~OverlayDB()
36 {
37  if (m_db.use_count() == 1 && m_db.get())
38  ctrace << "Closing state DB";
39 }
40 
42 {
43  virtual void Put(ldb::Slice const& _key, ldb::Slice const& _value) { cnote << "Put" << toHex(bytesConstRef(_key)) << "=>" << toHex(bytesConstRef(_value)); }
44  virtual void Delete(ldb::Slice const& _key) { cnote << "Delete" << toHex(bytesConstRef(_key)); }
45 };
46 
48 {
49  if (m_db)
50  {
51  ldb::WriteBatch batch;
52 // cnote << "Committing nodes to disk DB:";
53 #if DEV_GUARDED_DB
54  DEV_READ_GUARDED(x_this)
55 #endif
56  {
57  for (auto const& i: m_main)
58  {
59  if (i.second.second)
60  batch.Put(ldb::Slice((char const*)i.first.data(), i.first.size), ldb::Slice(i.second.first.data(), i.second.first.size()));
61 // cnote << i.first << "#" << m_main[i.first].second;
62  }
63  for (auto const& i: m_aux)
64  if (i.second.second)
65  {
66  bytes b = i.first.asBytes();
67  b.push_back(255); // for aux
68  batch.Put(bytesConstRef(&b), bytesConstRef(&i.second.first));
69  }
70  }
71 
72  for (unsigned i = 0; i < 10; ++i)
73  {
74  ldb::Status o = m_db->Write(m_writeOptions, &batch);
75  if (o.ok())
76  break;
77  if (i == 9)
78  {
79  cwarn << "Fail writing to state database. Bombing out.";
80  exit(-1);
81  }
82  cwarn << "Error writing to state database: " << o.ToString();
84  batch.Iterate(&n);
85  cwarn << "Sleeping for" << (i + 1) << "seconds, then retrying.";
86  this_thread::sleep_for(chrono::seconds(i + 1));
87  }
88 #if DEV_GUARDED_DB
89  DEV_WRITE_GUARDED(x_this)
90 #endif
91  {
92  m_aux.clear();
93  m_main.clear();
94  }
95  }
96 }
97 
98 bytes OverlayDB::lookupAux(h256 const& _h) const
99 {
100  bytes ret = MemoryDB::lookupAux(_h);
101  if (!ret.empty() || !m_db)
102  return ret;
103  std::string v;
104  bytes b = _h.asBytes();
105  b.push_back(255); // for aux
106  m_db->Get(m_readOptions, bytesConstRef(&b), &v);
107  if (v.empty())
108  cwarn << "Aux not found: " << _h;
109  return asBytes(v);
110 }
111 
112 void OverlayDB::rollback()
113 {
114 #if DEV_GUARDED_DB
115  WriteGuard l(x_this);
116 #endif
117  m_main.clear();
118 }
119 
120 std::string OverlayDB::lookup(h256 const& _h) const
121 {
122  std::string ret = MemoryDB::lookup(_h);
123  if (ret.empty() && m_db)
124  m_db->Get(m_readOptions, ldb::Slice((char const*)_h.data(), 32), &ret);
125  return ret;
126 }
127 
128 bool OverlayDB::exists(h256 const& _h) const
129 {
130  if (MemoryDB::exists(_h))
131  return true;
132  std::string ret;
133  if (m_db)
134  m_db->Get(m_readOptions, ldb::Slice((char const*)_h.data(), 32), &ret);
135  return !ret.empty();
136 }
137 
138 void OverlayDB::kill(h256 const& _h)
139 {
140 #if ETH_PARANOIA || 1
141  if (!MemoryDB::kill(_h))
142  {
143  std::string ret;
144  if (m_db)
145  m_db->Get(m_readOptions, ldb::Slice((char const*)_h.data(), 32), &ret);
146  // No point node ref decreasing for EmptyTrie since we never bother incrementing it in the first place for
147  // empty storage tries.
148  if (ret.empty() && _h != EmptyTrie)
149  cnote << "Decreasing DB node ref count below zero with no DB node. Probably have a corrupt Trie." << _h;
150 
151  // TODO: for 1.1: ref-counted triedb.
152  }
153 #else
154  MemoryDB::kill(_h);
155 #endif
156 }
157 
158 bool OverlayDB::deepkill(h256 const& _h)
159 {
160  // kill in memoryDB
161  kill(_h);
162 
163  //kill in overlayDB
164  ldb::Status s = m_db->Delete(m_writeOptions, ldb::Slice((char const*)_h.data(), 32));
165  if (s.ok())
166  return true;
167  else
168  return false;
169 }
170 
171 }
172 
173 #endif // ETH_EMSCRIPTEN
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
#define ctrace
Definition: Log.h:305
byte * data()
Definition: FixedHash.h:139
bytes rlp(_T _t)
Export a single item in RLP format, returning a byte array.
Definition: RLP.h:467
h256 const EmptyTrie
Definition: OverlayDB.cpp:33
std::hash for asio::adress
Definition: Common.h:323
std::shared_ptr< typename Signal< Args... >::HandlerAux > Handler
Definition: Common.h:181
#define DEV_WRITE_GUARDED(MUTEX)
Definition: Guards.h:148
#define DEV_READ_GUARDED(MUTEX)
Definition: Guards.h:146
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
bytes asBytes(std::string const &_b)
Converts a string to a byte array containing the string&#39;s (byte) data.
Definition: CommonData.h:92
#define cwarn
Definition: Log.h:304
#define b(i, j)
boost::unique_lock< boost::shared_mutex > WriteGuard
Definition: Guards.h:47
bytes asBytes() const
Definition: FixedHash.h:145
#define cnote
Definition: Log.h:303
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
virtual void Put(ldb::Slice const &_key, ldb::Slice const &_value)
Definition: OverlayDB.cpp:43
virtual void Delete(ldb::Slice const &_key)
Definition: OverlayDB.cpp:44
AddressHash commit(AccountMap const &_cache, SecureTrieDB< Address, DB > &_state)
Definition: State.h:345