@@ -98,31 +98,19 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
98
98
/* It seems that there are no "const iterators" for LevelDB. Since we
99
99
only need read operations on it, use a const-cast to get around
100
100
that restriction. */
101
- boost::scoped_ptr<leveldb::Iterator > pcursor (const_cast <CLevelDBWrapper*>(&db)->NewIterator ());
102
- pcursor->SeekToFirst ( );
101
+ boost::scoped_ptr<CLevelDBWrapper > pcursor (const_cast <CLevelDBWrapper*>(&db)->NewIterator ());
102
+ pcursor->Seek ( ' c ' );
103
103
104
104
CHashWriter ss (SER_GETHASH, PROTOCOL_VERSION);
105
105
stats.hashBlock = GetBestBlock ();
106
106
ss << stats.hashBlock ;
107
107
CAmount nTotalAmount = 0 ;
108
108
while (pcursor->Valid ()) {
109
109
boost::this_thread::interruption_point ();
110
- try {
111
- leveldb::Slice slKey = pcursor->key ();
112
- CDataStream ssKey (slKey.data (), slKey.data ()+slKey.size (), SER_DISK, CLIENT_VERSION);
113
- char chType;
114
- ssKey >> chType;
115
- if (chType == DB_COINS) {
116
- leveldb::Slice slValue = pcursor->value ();
117
- CDataStream ssValue (slValue.data (), slValue.data ()+slValue.size (), SER_DISK, CLIENT_VERSION);
118
- CCoins coins;
119
- ssValue >> coins;
120
- uint256 txhash;
121
- ssKey >> txhash;
122
- ss << txhash;
123
- ss << VARINT (coins.nVersion );
124
- ss << (coins.fCoinBase ? ' c' : ' n' );
125
- ss << VARINT (coins.nHeight );
110
+ std::pair<char , uint256> key;
111
+ CCoins coins;
112
+ if (pcursor->GetKey (key) && key.first == ' c' ) {
113
+ if (pcursor->GetValue (coins)) {
126
114
stats.nTransactions ++;
127
115
for (unsigned int i=0 ; i<coins.vout .size (); i++) {
128
116
const CTxOut &out = coins.vout [i];
@@ -133,13 +121,15 @@ bool CCoinsViewDB::GetStats(CCoinsStats &stats) const {
133
121
nTotalAmount += out.nValue ;
134
122
}
135
123
}
136
- stats.nSerializedSize += 32 + slValue. size ();
124
+ stats.nSerializedSize += 32 + pcursor-> GetKeySize ();
137
125
ss << VARINT (0 );
126
+ } else {
127
+ return error (" CCoinsViewDB::GetStats() : unable to read value" );
138
128
}
139
- pcursor->Next ();
140
- } catch (const std::exception& e) {
141
- return error (" %s: Deserialize or I/O error - %s" , __func__, e.what ());
129
+ } else {
130
+ break ;
142
131
}
132
+ pcursor->Next ();
143
133
}
144
134
{
145
135
LOCK (cs_main);
@@ -189,24 +179,15 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
189
179
{
190
180
boost::scoped_ptr<leveldb::Iterator> pcursor (NewIterator ());
191
181
192
- CDataStream ssKeySet (SER_DISK, CLIENT_VERSION);
193
- ssKeySet << make_pair (DB_BLOCK_INDEX, uint256 ());
194
- pcursor->Seek (ssKeySet.str ());
182
+ pcursor->Seek (make_pair (' b' , uint256 (0 )));
195
183
196
184
// Load mapBlockIndex
197
185
while (pcursor->Valid ()) {
198
186
boost::this_thread::interruption_point ();
199
- try {
200
- leveldb::Slice slKey = pcursor->key ();
201
- CDataStream ssKey (slKey.data (), slKey.data ()+slKey.size (), SER_DISK, CLIENT_VERSION);
202
- char chType;
203
- ssKey >> chType;
204
- if (chType == DB_BLOCK_INDEX) {
205
- leveldb::Slice slValue = pcursor->value ();
206
- CDataStream ssValue (slValue.data (), slValue.data ()+slValue.size (), SER_DISK, CLIENT_VERSION);
207
- CDiskBlockIndex diskindex;
208
- ssValue >> diskindex;
209
-
187
+ std::pair<char , uint256> key;
188
+ if (pcursor->GetKey (key) && key.first == ' b' ) {
189
+ CDiskBlockIndex diskindex;
190
+ if (pcursor->GetValue (diskindex)) {
210
191
// Construct block index object
211
192
CBlockIndex* pindexNew = InsertBlockIndex (diskindex.GetBlockHash ());
212
193
pindexNew->pprev = InsertBlockIndex (diskindex.hashPrev );
@@ -227,10 +208,10 @@ bool CBlockTreeDB::LoadBlockIndexGuts()
227
208
228
209
pcursor->Next ();
229
210
} else {
230
- break ; // if shutdown requested or finished loading block index
211
+ return error ( " LoadBlockIndex() : failed to read value " );
231
212
}
232
- } catch ( const std::exception& e) {
233
- return error ( " %s: Deserialize or I/O error - %s " , __func__, e. what ()) ;
213
+ } else {
214
+ break ;
234
215
}
235
216
}
236
217
0 commit comments