@@ -25,20 +25,34 @@ class dbwrapper_error : public std::runtime_error
2525
2626void HandleError (const leveldb::Status& status);
2727
28+ class CDBWrapper ;
29+
30+ /* * These should be considered an implementation detail of the specific database.
31+ */
32+ namespace dbwrapper_private {
33+
34+ /* * Work around circular dependency, as well as for testing in dbwrapper_tests.
35+ * Database obfuscation should be considered an implementation detail of the
36+ * specific database.
37+ */
38+ const std::vector<unsigned char >& GetObfuscateKey (const CDBWrapper &w);
39+
40+ };
41+
2842/* * Batch of changes queued to be written to a CDBWrapper */
2943class CDBBatch
3044{
3145 friend class CDBWrapper ;
3246
3347private:
48+ const CDBWrapper &parent;
3449 leveldb::WriteBatch batch;
35- const std::vector<unsigned char > *obfuscate_key;
3650
3751public:
3852 /* *
39- * @param[in] obfuscate_key If passed, XOR data with this key.
53+ * @param[in] parent CDBWrapper that this batch is to be submitted to
4054 */
41- CDBBatch (const std::vector< unsigned char > *obfuscate_key ) : obfuscate_key(obfuscate_key ) { };
55+ CDBBatch (const CDBWrapper &parent ) : parent(parent ) { };
4256
4357 template <typename K, typename V>
4458 void Write (const K& key, const V& value)
@@ -51,7 +65,7 @@ class CDBBatch
5165 CDataStream ssValue (SER_DISK, CLIENT_VERSION);
5266 ssValue.reserve (ssValue.GetSerializeSize (value));
5367 ssValue << value;
54- ssValue.Xor (*obfuscate_key );
68+ ssValue.Xor (dbwrapper_private::GetObfuscateKey (parent) );
5569 leveldb::Slice slValue (&ssValue[0 ], ssValue.size ());
5670
5771 batch.Put (slKey, slValue);
@@ -72,17 +86,17 @@ class CDBBatch
7286class CDBIterator
7387{
7488private:
89+ const CDBWrapper &parent;
7590 leveldb::Iterator *piter;
76- const std::vector<unsigned char > *obfuscate_key;
7791
7892public:
7993
8094 /* *
95+ * @param[in] parent Parent CDBWrapper instance.
8196 * @param[in] piterIn The original leveldb iterator.
82- * @param[in] obfuscate_key If passed, XOR data with this key.
8397 */
84- CDBIterator (leveldb::Iterator *piterIn, const std::vector< unsigned char >* obfuscate_key ) :
85- piter (piterIn ), obfuscate_key(obfuscate_key ) { };
98+ CDBIterator (const CDBWrapper &parent, leveldb::Iterator *piterIn ) :
99+ parent (parent ), piter(piterIn ) { };
86100 ~CDBIterator ();
87101
88102 bool Valid ();
@@ -118,7 +132,7 @@ class CDBIterator
118132 leveldb::Slice slValue = piter->value ();
119133 try {
120134 CDataStream ssValue (slValue.data (), slValue.data () + slValue.size (), SER_DISK, CLIENT_VERSION);
121- ssValue.Xor (*obfuscate_key );
135+ ssValue.Xor (dbwrapper_private::GetObfuscateKey (parent) );
122136 ssValue >> value;
123137 } catch (const std::exception&) {
124138 return false ;
@@ -134,6 +148,7 @@ class CDBIterator
134148
135149class CDBWrapper
136150{
151+ friend const std::vector<unsigned char >& dbwrapper_private::GetObfuscateKey (const CDBWrapper &w);
137152private:
138153 // ! custom environment this database is using (may be NULL in case of default environment)
139154 leveldb::Env* penv;
@@ -208,7 +223,7 @@ class CDBWrapper
208223 template <typename K, typename V>
209224 bool Write (const K& key, const V& value, bool fSync = false )
210225 {
211- CDBBatch batch (&obfuscate_key );
226+ CDBBatch batch (* this );
212227 batch.Write (key, value);
213228 return WriteBatch (batch, fSync );
214229 }
@@ -235,7 +250,7 @@ class CDBWrapper
235250 template <typename K>
236251 bool Erase (const K& key, bool fSync = false )
237252 {
238- CDBBatch batch (&obfuscate_key );
253+ CDBBatch batch (* this );
239254 batch.Erase (key);
240255 return WriteBatch (batch, fSync );
241256 }
@@ -250,24 +265,19 @@ class CDBWrapper
250265
251266 bool Sync ()
252267 {
253- CDBBatch batch (&obfuscate_key );
268+ CDBBatch batch (* this );
254269 return WriteBatch (batch, true );
255270 }
256271
257272 CDBIterator *NewIterator ()
258273 {
259- return new CDBIterator (pdb->NewIterator (iteroptions), &obfuscate_key );
274+ return new CDBIterator (* this , pdb->NewIterator (iteroptions));
260275 }
261276
262277 /* *
263278 * Return true if the database managed by this class contains no entries.
264279 */
265280 bool IsEmpty ();
266-
267- /* *
268- * Accessor for obfuscate_key.
269- */
270- const std::vector<unsigned char >& GetObfuscateKey () const ;
271281};
272282
273283#endif // BITCOIN_DBWRAPPER_H
0 commit comments