|
5 | 5 | #include <nano/secure/common.hpp> |
6 | 6 | #include <nano/secure/ext_ledger.hpp> |
7 | 7 | #include <nano/secure/ledger.hpp> |
| 8 | +#include <nano/store/ext_ledger/receive_block_by_send_block.hpp> |
8 | 9 | #include <nano/store/ext_ledger_store.hpp> |
| 10 | +#include <nano/store/ledger/block.hpp> |
9 | 11 | #include <nano/store/ledger_store.hpp> |
10 | 12 | #include <nano/store/meta.hpp> |
11 | 13 |
|
@@ -36,11 +38,81 @@ void nano::ext_ledger::initialize () |
36 | 38 |
|
37 | 39 | if (store.get_mode () != nano::store::open_mode::read_only) |
38 | 40 | { |
| 41 | + auto has_flags = [&store] (nano::store::ext_ledger_flags flags) -> bool { |
| 42 | + return store.ext.has_flags (store.tx_begin_read (), flags); |
| 43 | + }; |
| 44 | + |
| 45 | + if (!has_flags (nano::store::ext_ledger_flags::receive_block_by_send_block_initialized)) |
| 46 | + { |
| 47 | + store.ext.receive_block_by_send_block.clear (); |
| 48 | + initialize_receive_block_by_send_block_index (); |
| 49 | + } |
39 | 50 | } |
40 | 51 |
|
41 | 52 | initialized = true; |
42 | 53 | } |
43 | 54 |
|
| 55 | +void nano::ext_ledger::initialize_receive_block_by_send_block_index () |
| 56 | +{ |
| 57 | + nano::store::ledger_store & store = ledger.store; |
| 58 | + nano::store::write_transaction txn = store.tx_begin_write (); |
| 59 | + |
| 60 | + release_assert (store.ext.is_initialized (), "Extended ledger store must be initialized"); |
| 61 | + release_assert (store.ext.receive_block_by_send_block.empty (txn), "The index must be cleared before rebuilding to avoid inconsistent or duplicate entries."); |
| 62 | + release_assert (store.get_mode () != nano::store::open_mode::read_only, "The index cannot be built while the backend is opened in read-only mode."); |
| 63 | + |
| 64 | + logger.info (nano::log::type::ext_ledger, "Building receive block by send block index from existing ledger data, this may take a while..."); |
| 65 | + |
| 66 | + size_t processed = 0; |
| 67 | + size_t indexed = 0; |
| 68 | + size_t const batch_size = 100000; |
| 69 | + |
| 70 | + for (auto itr = store.block.begin (txn), end = store.block.end (txn); itr != end; ++itr) |
| 71 | + { |
| 72 | + auto const & sideband = itr->second; |
| 73 | + auto block = sideband.block; |
| 74 | + if (block->is_receive ()) |
| 75 | + { |
| 76 | + nano::block_hash receive_block_hash = itr->first; |
| 77 | + nano::block_hash send_block_hash = block->source (); |
| 78 | + store.ext.receive_block_by_send_block.put (txn, send_block_hash, receive_block_hash); |
| 79 | + ++indexed; |
| 80 | + } |
| 81 | + ++processed; |
| 82 | + if (processed % batch_size == 0) |
| 83 | + { |
| 84 | + logger.info (nano::log::type::ext_ledger, "Build progress: processed {} blocks, indexed {} entries", processed, indexed); |
| 85 | + |
| 86 | + txn.refresh (); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + logger.info (nano::log::type::ext_ledger, "Build completed: processed {} blocks, indexed {} entries", processed, indexed); |
| 91 | + |
| 92 | + // Mark index as fully built and consistent with the current ledger state |
| 93 | + store.ext.add_flags (txn, nano::store::ext_ledger_flags::receive_block_by_send_block_initialized); |
| 94 | +} |
| 95 | + |
| 96 | +void nano::ext_ledger::on_put_block (nano::store::write_transaction const & txn, nano::block_hash const & hash, nano::block const & block) |
| 97 | +{ |
| 98 | + nano::store::ledger_store & store = ledger.store; |
| 99 | + release_assert (store.ext.is_initialized (), "Extended ledger store must be initialized"); |
| 100 | + if (block.is_receive ()) |
| 101 | + { |
| 102 | + store.ext.receive_block_by_send_block.put (txn, block.source (), hash); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +void nano::ext_ledger::on_del_block (nano::store::write_transaction const & txn, nano::block_hash const & hash, nano::block const & block) |
| 107 | +{ |
| 108 | + nano::store::ledger_store & store = ledger.store; |
| 109 | + release_assert (store.ext.is_initialized (), "Extended ledger store must be initialized"); |
| 110 | + if (block.is_receive ()) |
| 111 | + { |
| 112 | + store.ext.receive_block_by_send_block.del (txn, block.source ()); |
| 113 | + } |
| 114 | +} |
| 115 | + |
44 | 116 | void nano::ext_ledger::clear (nano::store::write_transaction const & txn) |
45 | 117 | { |
46 | 118 | nano::store::ledger_store & store = ledger.store; |
|
0 commit comments