44
55#include < index/addressindex.h>
66
7- #include < index/addressindex_util.h>
87#include < chainparams.h>
98#include < clientversion.h>
109#include < hash.h>
10+ #include < index/addressindex_util.h>
1111#include < logging.h>
1212#include < node/blockstorage.h>
1313#include < undo.h>
@@ -47,8 +47,7 @@ bool AddressIndex::DB::WriteBatch(const std::vector<CAddressIndexEntry>& address
4747}
4848
4949bool AddressIndex::DB::ReadAddressIndex (const uint160& address_hash, const AddressType type,
50- std::vector<CAddressIndexEntry>& entries,
51- const int32_t start, const int32_t end)
50+ std::vector<CAddressIndexEntry>& entries, const int32_t start, const int32_t end)
5251{
5352 std::unique_ptr<CDBIterator> pcursor (NewIterator ());
5453
@@ -60,8 +59,8 @@ bool AddressIndex::DB::ReadAddressIndex(const uint160& address_hash, const Addre
6059
6160 while (pcursor->Valid ()) {
6261 std::pair<uint8_t , CAddressIndexKey> key;
63- if (pcursor->GetKey (key) && key.first == DB_ADDRESSINDEX &&
64- key.second .m_address_type == type && key. second . m_address_bytes == address_hash) {
62+ if (pcursor->GetKey (key) && key.first == DB_ADDRESSINDEX && key. second . m_address_type == type &&
63+ key.second .m_address_bytes == address_hash) {
6564 if (end > 0 && key.second .m_block_height > end) {
6665 break ;
6766 }
@@ -81,17 +80,16 @@ bool AddressIndex::DB::ReadAddressIndex(const uint160& address_hash, const Addre
8180}
8281
8382bool AddressIndex::DB::ReadAddressUnspentIndex (const uint160& address_hash, const AddressType type,
84- std::vector<CAddressUnspentIndexEntry>& entries,
85- const bool height_sort)
83+ std::vector<CAddressUnspentIndexEntry>& entries, const bool height_sort)
8684{
8785 std::unique_ptr<CDBIterator> pcursor (NewIterator ());
8886
8987 pcursor->Seek (std::make_pair (DB_ADDRESSUNSPENTINDEX, CAddressIndexIteratorKey (type, address_hash)));
9088
9189 while (pcursor->Valid ()) {
9290 std::pair<uint8_t , CAddressUnspentKey> key;
93- if (pcursor->GetKey (key) && key.first == DB_ADDRESSUNSPENTINDEX &&
94- key.second .m_address_type == type && key. second . m_address_bytes == address_hash) {
91+ if (pcursor->GetKey (key) && key.first == DB_ADDRESSUNSPENTINDEX && key. second . m_address_type == type &&
92+ key.second .m_address_bytes == address_hash) {
9593 CAddressUnspentValue value;
9694 if (pcursor->GetValue (value)) {
9795 entries.emplace_back (key.second , value);
@@ -106,9 +104,9 @@ bool AddressIndex::DB::ReadAddressUnspentIndex(const uint160& address_hash, cons
106104
107105 if (height_sort) {
108106 std::sort (entries.begin (), entries.end (),
109- [](const CAddressUnspentIndexEntry& a, const CAddressUnspentIndexEntry& b) {
110- return a.second .m_block_height < b.second .m_block_height ;
111- });
107+ [](const CAddressUnspentIndexEntry& a, const CAddressUnspentIndexEntry& b) {
108+ return a.second .m_block_height < b.second .m_block_height ;
109+ });
112110 }
113111
114112 return true ;
@@ -140,8 +138,8 @@ bool AddressIndex::DB::UpdateAddressUnspentIndex(const std::vector<CAddressUnspe
140138 return CDBWrapper::WriteBatch (batch);
141139}
142140
143- AddressIndex::AddressIndex (size_t n_cache_size, bool f_memory, bool f_wipe)
144- : m_db(std::make_unique<AddressIndex::DB>(n_cache_size, f_memory, f_wipe))
141+ AddressIndex::AddressIndex (size_t n_cache_size, bool f_memory, bool f_wipe) :
142+ m_db(std::make_unique<AddressIndex::DB>(n_cache_size, f_memory, f_wipe))
145143{
146144}
147145
@@ -157,8 +155,8 @@ bool AddressIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
157155 // Read undo data for this block to get information about spent outputs
158156 CBlockUndo blockundo;
159157 if (!node::UndoReadFromDisk (blockundo, pindex)) {
160- return error (" %s: Failed to read undo data for block %s at height %d" ,
161- __func__, pindex->GetBlockHash ().ToString (), pindex->nHeight );
158+ return error (" %s: Failed to read undo data for block %s at height %d" , __func__,
159+ pindex->GetBlockHash ().ToString (), pindex->nHeight );
162160 }
163161
164162 std::vector<CAddressIndexEntry> addressIndex;
@@ -189,15 +187,13 @@ bool AddressIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
189187 }
190188
191189 // Record spending activity
192- addressIndex.emplace_back (
193- CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, j, true ),
194- prevout.nValue * -1
195- );
190+ addressIndex.emplace_back (CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, j, true ),
191+ prevout.nValue * -1 );
196192
197193 // Remove from unspent index
198- addressUnspentIndex.emplace_back (
199- CAddressUnspentKey (address_type, address_bytes, input. prevout . hash , input.prevout .n ),
200- CAddressUnspentValue () // Null value means delete
194+ addressUnspentIndex.emplace_back (CAddressUnspentKey (address_type, address_bytes, input. prevout . hash ,
195+ input.prevout .n ),
196+ CAddressUnspentValue () // Null value means delete
201197 );
202198 }
203199
@@ -212,16 +208,12 @@ bool AddressIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
212208 }
213209
214210 // Record receiving activity
215- addressIndex.emplace_back (
216- CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, k, false ),
217- out.nValue
218- );
211+ addressIndex.emplace_back (CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, k, false ),
212+ out.nValue );
219213
220214 // Add to unspent index
221- addressUnspentIndex.emplace_back (
222- CAddressUnspentKey (address_type, address_bytes, txhash, k),
223- CAddressUnspentValue (out.nValue , out.scriptPubKey , pindex->nHeight )
224- );
215+ addressUnspentIndex.emplace_back (CAddressUnspentKey (address_type, address_bytes, txhash, k),
216+ CAddressUnspentValue (out.nValue , out.scriptPubKey , pindex->nHeight ));
225217 }
226218 }
227219
@@ -238,16 +230,12 @@ bool AddressIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
238230 }
239231
240232 // Record receiving activity for coinbase
241- addressIndex.emplace_back (
242- CAddressIndexKey (address_type, address_bytes, pindex->nHeight , 0 , coinbase_hash, k, false ),
243- out.nValue
244- );
233+ addressIndex.emplace_back (CAddressIndexKey (address_type, address_bytes, pindex->nHeight , 0 , coinbase_hash, k, false ),
234+ out.nValue );
245235
246236 // Add coinbase outputs to unspent index
247- addressUnspentIndex.emplace_back (
248- CAddressUnspentKey (address_type, address_bytes, coinbase_hash, k),
249- CAddressUnspentValue (out.nValue , out.scriptPubKey , pindex->nHeight )
250- );
237+ addressUnspentIndex.emplace_back (CAddressUnspentKey (address_type, address_bytes, coinbase_hash, k),
238+ CAddressUnspentValue (out.nValue , out.scriptPubKey , pindex->nHeight ));
251239 }
252240
253241 return m_db->WriteBatch (addressIndex, addressUnspentIndex);
@@ -262,14 +250,14 @@ bool AddressIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new
262250 for (const CBlockIndex* pindex = current_tip; pindex != new_tip; pindex = pindex->pprev ) {
263251 CBlock block;
264252 if (!node::ReadBlockFromDisk (block, pindex, Params ().GetConsensus ())) {
265- return error (" %s: Failed to read block %s from disk during rewind" ,
266- __func__, pindex->GetBlockHash ().ToString ());
253+ return error (" %s: Failed to read block %s from disk during rewind" , __func__,
254+ pindex->GetBlockHash ().ToString ());
267255 }
268256
269257 CBlockUndo blockundo;
270258 if (pindex->nHeight > 0 && !node::UndoReadFromDisk (blockundo, pindex)) {
271- return error (" %s: Failed to read undo data for block %s during rewind" ,
272- __func__, pindex->GetBlockHash ().ToString ());
259+ return error (" %s: Failed to read undo data for block %s during rewind" , __func__,
260+ pindex->GetBlockHash ().ToString ());
273261 }
274262
275263 std::vector<CAddressIndexEntry> addressIndex;
@@ -294,16 +282,14 @@ bool AddressIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new
294282 }
295283
296284 // Remove receiving activity from history
297- addressIndex.push_back (std::make_pair (
298- CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, k, false ),
299- out.nValue
300- ));
285+ addressIndex.push_back (std::make_pair (CAddressIndexKey (address_type, address_bytes, pindex->nHeight ,
286+ i + 1 , txhash, k, false ),
287+ out.nValue ));
301288
302289 // Remove from unspent index (mark for deletion)
303- addressUnspentIndex.push_back (std::make_pair (
304- CAddressUnspentKey (address_type, address_bytes, txhash, k),
305- CAddressUnspentValue () // null value signals deletion
306- ));
290+ addressUnspentIndex.push_back (std::make_pair (CAddressUnspentKey (address_type, address_bytes, txhash, k),
291+ CAddressUnspentValue () // null value signals deletion
292+ ));
307293 }
308294
309295 // Undo inputs (restore to unspent index, remove spending from history)
@@ -320,16 +306,14 @@ bool AddressIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new
320306 }
321307
322308 // Remove spending activity from history
323- addressIndex.push_back (std::make_pair (
324- CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, j, true ),
325- prevout.nValue * -1
326- ));
309+ addressIndex.push_back (
310+ std::make_pair (CAddressIndexKey (address_type, address_bytes, pindex->nHeight , i + 1 , txhash, j, true ),
311+ prevout.nValue * -1 ));
327312
328313 // Restore to unspent index
329- addressUnspentIndex.push_back (std::make_pair (
330- CAddressUnspentKey (address_type, address_bytes, input.prevout .hash , input.prevout .n ),
331- CAddressUnspentValue (prevout.nValue , prevout.scriptPubKey , coin.nHeight )
332- ));
314+ addressUnspentIndex.push_back (
315+ std::make_pair (CAddressUnspentKey (address_type, address_bytes, input.prevout .hash , input.prevout .n ),
316+ CAddressUnspentValue (prevout.nValue , prevout.scriptPubKey , coin.nHeight )));
333317 }
334318 }
335319
@@ -349,16 +333,14 @@ bool AddressIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new
349333 }
350334
351335 // Remove coinbase receiving activity
352- addressIndex.push_back (std::make_pair (
353- CAddressIndexKey (address_type, address_bytes, pindex->nHeight , 0 , cb_hash, k, false ),
354- out.nValue
355- ));
336+ addressIndex.push_back (
337+ std::make_pair (CAddressIndexKey (address_type, address_bytes, pindex->nHeight , 0 , cb_hash, k, false ),
338+ out.nValue ));
356339
357340 // Remove from unspent index
358- addressUnspentIndex.push_back (std::make_pair (
359- CAddressUnspentKey (address_type, address_bytes, cb_hash, k),
360- CAddressUnspentValue () // null value signals deletion
361- ));
341+ addressUnspentIndex.push_back (std::make_pair (CAddressUnspentKey (address_type, address_bytes, cb_hash, k),
342+ CAddressUnspentValue () // null value signals deletion
343+ ));
362344 }
363345 }
364346
@@ -385,17 +367,15 @@ void AddressIndex::BlockDisconnected(const std::shared_ptr<const CBlock>& block,
385367 // Only rewind if we have this block indexed
386368 if (best_block_index && best_block_index->nHeight >= pindex->nHeight && pindex->pprev ) {
387369 if (!Rewind (best_block_index, pindex->pprev )) {
388- error (" %s: Failed to rewind %s to previous block after disconnect" ,
389- __func__, GetName ());
370+ error (" %s: Failed to rewind %s to previous block after disconnect" , __func__, GetName ());
390371 }
391372 }
392373}
393374
394375BaseIndex::DB& AddressIndex::GetDB () const { return *m_db; }
395376
396377bool AddressIndex::GetAddressIndex (const uint160& address_hash, const AddressType type,
397- std::vector<CAddressIndexEntry>& entries,
398- const int32_t start, const int32_t end) const
378+ std::vector<CAddressIndexEntry>& entries, const int32_t start, const int32_t end) const
399379{
400380 if (!BlockUntilSyncedToCurrentChain ()) {
401381 return false ;
@@ -405,8 +385,7 @@ bool AddressIndex::GetAddressIndex(const uint160& address_hash, const AddressTyp
405385}
406386
407387bool AddressIndex::GetAddressUnspentIndex (const uint160& address_hash, const AddressType type,
408- std::vector<CAddressUnspentIndexEntry>& entries,
409- const bool height_sort) const
388+ std::vector<CAddressUnspentIndexEntry>& entries, const bool height_sort) const
410389{
411390 if (!BlockUntilSyncedToCurrentChain ()) {
412391 return false ;
0 commit comments