Fabcoin Core  0.16.2
P2P Digital Currency
memorydb.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 */
23 #include <boost/test/unit_test.hpp>
24 #include <iostream>
25 #include <libdevcore/MemoryDB.h>
27 
28 using namespace std;
29 using namespace dev;
30 using namespace dev::test;
31 
32 namespace dev { namespace test {
33 
34 
35 } }// Namespace Close
36 
38 
40 {
41  MemoryDB myDB;
42  BOOST_CHECK(myDB.get().empty());
43  bytes value = fromHex("43");
44  myDB.insert(h256(42), &value);
45  BOOST_CHECK(myDB.exists(h256(42)));
46  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
47  BOOST_CHECK(!myDB.kill(h256(43)));
48  BOOST_CHECK(myDB.kill(h256(42)));
49 }
50 
51 BOOST_AUTO_TEST_CASE(purgeMainMem)
52 {
53  MemoryDB myDB;
54  BOOST_CHECK(myDB.get().empty());
55  bytes value = fromHex("43");
56 
57  myDB.insert(h256(42), &value);
58  MemoryDB copy;
59  copy = myDB;
60  BOOST_CHECK(myDB.exists(h256(42)));
61  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
62  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
63  BOOST_CHECK(myDB.kill(h256(42)));
64 
65  BOOST_CHECK(myDB.get() == copy.get());
66  BOOST_CHECK(myDB.exists(h256(42)));
67  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
68 
69  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
70  myDB.purge();
71  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
72  myDB.insert(h256(43), &value);
73  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
74  myDB.clear();
75  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
76 }
77 
78 BOOST_AUTO_TEST_CASE(purgeMainMem_Refs)
79 {
80  MemoryDB myDB;
81  {
82  EnforceRefs enforceRefs(myDB, true);
83 
84  BOOST_CHECK(myDB.get().empty());
85  bytes value = fromHex("43");
86 
87  myDB.insert(h256(42), &value);
88  MemoryDB copy;
89  copy = myDB;
90  BOOST_CHECK(myDB.exists(h256(42)));
91  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
92  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
93  BOOST_CHECK(myDB.kill(h256(42)));
94 
95  BOOST_CHECK(myDB.get() != copy.get());
96  BOOST_CHECK(!myDB.exists(h256(42)));
97  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), std::string());
98 
99  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
100  myDB.purge();
101  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
102  myDB.insert(h256(43), &value);
103  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
104  myDB.clear();
105  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
106  // call EnforceRefs destructor
107  }
108 
109  // do same tests again without EnforceRefs
110  BOOST_CHECK(myDB.get().empty());
111  bytes value = fromHex("43");
112 
113  myDB.insert(h256(42), &value);
114  MemoryDB copy;
115  copy = myDB;
116  BOOST_CHECK(myDB.exists(h256(42)));
117  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
118  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
119  BOOST_CHECK(myDB.kill(h256(42)));
120 
121  BOOST_CHECK(myDB.get() == copy.get());
122  BOOST_CHECK(myDB.exists(h256(42)));
123  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
124 
125  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
126  myDB.purge();
127  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
128  myDB.insert(h256(43), &value);
129  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
130  myDB.clear();
131  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
132 }
133 
135 {
136  class AuxMemDB : public MemoryDB
137  {
138  public:
139  std::unordered_map<h256, std::pair<bytes, bool>> getAux() { return m_aux;}
140  };
141 
142  AuxMemDB myDB;
143  BOOST_CHECK(myDB.get().empty());
144  bytes value = fromHex("43");
145 
146  myDB.insertAux(h256(42), &value);
147  BOOST_CHECK(myDB.lookupAux(h256(42)) == value);
148  BOOST_CHECK_EQUAL(myDB.get().size(), 0);
149  myDB.removeAux(h256(42));
150  BOOST_CHECK(myDB.lookupAux(h256(42)) == value);
151  BOOST_CHECK_EQUAL(myDB.getAux().size(), 1);
152  myDB.purge();
153  BOOST_CHECK(myDB.lookupAux(h256(42)) == bytes());
154  BOOST_CHECK_EQUAL(myDB.getAux().size(), 0);
155  myDB.insertAux(h256(43), &value);
156  BOOST_CHECK_EQUAL(myDB.getAux().size(), 1);
157  myDB.clear();
158  BOOST_CHECK_EQUAL(myDB.getAux().size(), 0);
159 }
160 
162 {
163  MemoryDB myDB;
164  BOOST_CHECK(myDB.get().empty());
165  bytes value = fromHex("43");
166  myDB.insert(h256(42), &value);
167  BOOST_CHECK(myDB.exists(h256(42)));
168  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
169 
170  MemoryDB copyToDB;
171  copyToDB = myDB;
172  BOOST_CHECK(copyToDB.exists(h256(42)));
173  BOOST_CHECK_EQUAL(copyToDB.get().size(), 1);
174  BOOST_CHECK(myDB.keys() == copyToDB.keys());
175  BOOST_CHECK(myDB.get() == copyToDB.get());
176  myDB.insert(h256(43), &value);
177  BOOST_CHECK(myDB.keys() != copyToDB.keys());
178 }
179 
181 {
182  MemoryDB myDB;
183  BOOST_CHECK(myDB.get().empty());
184  bytes value = fromHex("43");
185 
186  myDB.insert(h256(42), &value);
187  BOOST_CHECK(myDB.exists(h256(42)));
188  BOOST_CHECK_EQUAL(myDB.lookup(h256(42)), toString(value[0]));
189  BOOST_CHECK_EQUAL(myDB.get().size(), 1);
190 
191  myDB.insert(h256(0), &value);
192  BOOST_CHECK(myDB.exists(h256(0)));
193  BOOST_CHECK_EQUAL(myDB.lookup(h256(0)), toString(value[0]));
194 
195  myDB.insert(h256(std::numeric_limits<u256>::max()), &value);
198 
199  BOOST_CHECK_EQUAL(myDB.get().size(), 3);
200 }
201 
203 {
204  MemoryDB myDB;
205  BOOST_CHECK(myDB.get().empty());
206  bytes value = fromHex("43");
207  myDB.insert(h256(42), &value);
208  myDB.insert(h256(43), &value);
209  std::ostringstream stream;
210  stream << myDB;
211  BOOST_CHECK_EQUAL(stream.str(), "000000000000000000000000000000000000000000000000000000000000002a: 0x43 43\n000000000000000000000000000000000000000000000000000000000000002b: 0x43 43\n");
212 }
213 
std::unordered_map< h256, std::string > get() const
Definition: MemoryDB.cpp:33
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::hash for asio::adress
Definition: Common.h:323
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
h256Hash keys() const
Definition: MemoryDB.cpp:179
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
Definition: CommonData.cpp:99
bool kill(h256 const &_h)
Definition: MemoryDB.cpp:103
ExecStats::duration max
Definition: ExecStats.cpp:36
std::vector< byte > bytes
Definition: Common.h:75
FixedHash< 32 > h256
Definition: FixedHash.h:340
BOOST_AUTO_TEST_CASE(kill)
Definition: memorydb.cpp:39
void insert(h256 const &_h, bytesConstRef _v)
Definition: MemoryDB.cpp:85
void clear()
Definition: MemoryDB.h:51
void purge()
Definition: MemoryDB.cpp:159
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
bool exists(h256 const &_h) const
Definition: MemoryDB.cpp:74
std::string lookup(h256 const &_h) const
Definition: MemoryDB.cpp:58
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17