Skip to content

Commit b69836d

Browse files
committed
dbwrapper: Pass parent CDBWrapper into CDBBatch and CDBIterator
Pass parent wrapper directly instead of obfuscation key. This makes it possible for other databases which re-use this code to use other properties from the database. Add a namespace dbwrapper_private for private functions to be used only in dbwrapper.h/cpp and dbwrapper_tests.
1 parent 878bf48 commit b69836d

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

src/dbwrapper.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ bool CDBWrapper::IsEmpty()
136136
return !(it->Valid());
137137
}
138138

139-
const std::vector<unsigned char>& CDBWrapper::GetObfuscateKey() const
140-
{
141-
return obfuscate_key;
142-
}
143-
144139
CDBIterator::~CDBIterator() { delete piter; }
145140
bool CDBIterator::Valid() { return piter->Valid(); }
146141
void CDBIterator::SeekToFirst() { piter->SeekToFirst(); }
147142
void CDBIterator::Next() { piter->Next(); }
143+
144+
namespace dbwrapper_private {
145+
146+
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
147+
{
148+
return w.obfuscate_key;
149+
}
150+
151+
};

src/dbwrapper.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,34 @@ class dbwrapper_error : public std::runtime_error
2525

2626
void 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 */
2943
class CDBBatch
3044
{
3145
friend class CDBWrapper;
3246

3347
private:
48+
const CDBWrapper &parent;
3449
leveldb::WriteBatch batch;
35-
const std::vector<unsigned char> *obfuscate_key;
3650

3751
public:
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
7286
class CDBIterator
7387
{
7488
private:
89+
const CDBWrapper &parent;
7590
leveldb::Iterator *piter;
76-
const std::vector<unsigned char> *obfuscate_key;
7791

7892
public:
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

135149
class CDBWrapper
136150
{
151+
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
137152
private:
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

src/test/dbwrapper_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
3939
uint256 res;
4040

4141
// Ensure that we're doing real obfuscation when obfuscate=true
42-
BOOST_CHECK(obfuscate != is_null_key(dbw.GetObfuscateKey()));
42+
BOOST_CHECK(obfuscate != is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
4343

4444
BOOST_CHECK(dbw.Write(key, in));
4545
BOOST_CHECK(dbw.Read(key, res));
@@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_batch)
6464
uint256 in3 = GetRandHash();
6565

6666
uint256 res;
67-
CDBBatch batch(&dbw.GetObfuscateKey());
67+
CDBBatch batch(dbw);
6868

6969
batch.Write(key, in);
7070
batch.Write(key2, in2);
@@ -156,7 +156,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
156156
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
157157

158158
BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
159-
BOOST_CHECK(is_null_key(odbw.GetObfuscateKey())); // The key should be an empty string
159+
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
160160

161161
uint256 in2 = GetRandHash();
162162
uint256 res3;
@@ -193,7 +193,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
193193
// Check that the key/val we wrote with unobfuscated wrapper doesn't exist
194194
uint256 res2;
195195
BOOST_CHECK(!odbw.Read(key, res2));
196-
BOOST_CHECK(!is_null_key(odbw.GetObfuscateKey()));
196+
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
197197

198198
uint256 in2 = GetRandHash();
199199
uint256 res3;

src/txdb.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ uint256 CCoinsViewDB::GetBestBlock() const {
4949
}
5050

5151
bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) {
52-
CDBBatch batch(&db.GetObfuscateKey());
52+
CDBBatch batch(db);
5353
size_t count = 0;
5454
size_t changed = 0;
5555
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end();) {
@@ -139,7 +139,7 @@ void CCoinsViewDBCursor::Next()
139139
}
140140

141141
bool CBlockTreeDB::WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo) {
142-
CDBBatch batch(&GetObfuscateKey());
142+
CDBBatch batch(*this);
143143
for (std::vector<std::pair<int, const CBlockFileInfo*> >::const_iterator it=fileInfo.begin(); it != fileInfo.end(); it++) {
144144
batch.Write(make_pair(DB_BLOCK_FILES, it->first), *it->second);
145145
}
@@ -155,7 +155,7 @@ bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
155155
}
156156

157157
bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >&vect) {
158-
CDBBatch batch(&GetObfuscateKey());
158+
CDBBatch batch(*this);
159159
for (std::vector<std::pair<uint256,CDiskTxPos> >::const_iterator it=vect.begin(); it!=vect.end(); it++)
160160
batch.Write(make_pair(DB_TXINDEX, it->first), it->second);
161161
return WriteBatch(batch);

0 commit comments

Comments
 (0)