blockchain: some batch tx scanning speedup

This commit is contained in:
moneromooo-monero 2018-07-21 11:54:48 +01:00
parent 702a41034d
commit 8e24533a7f
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3
1 changed files with 19 additions and 15 deletions

View File

@ -4041,6 +4041,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
TIME_MEASURE_START(prepare); TIME_MEASURE_START(prepare);
bool stop_batch; bool stop_batch;
uint64_t bytes = 0; uint64_t bytes = 0;
size_t total_txs = 0;
// Order of locking must be: // Order of locking must be:
// m_incoming_tx_lock (optional) // m_incoming_tx_lock (optional)
@ -4069,6 +4070,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
{ {
bytes += tx_blob.size(); bytes += tx_blob.size();
} }
total_txs += entry.txs.size();
} }
while (!(stop_batch = m_db->batch_start(blocks_entry.size(), bytes))) { while (!(stop_batch = m_db->batch_start(blocks_entry.size(), bytes))) {
m_blockchain_lock.unlock(); m_blockchain_lock.unlock();
@ -4128,7 +4130,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
break; break;
} }
blocks[i].push_back(block); blocks[i].push_back(std::move(block));
std::advance(it, 1); std::advance(it, 1);
} }
} }
@ -4149,7 +4151,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
break; break;
} }
blocks[i].push_back(block); blocks[i].push_back(std::move(block));
std::advance(it, 1); std::advance(it, 1);
} }
@ -4205,6 +4207,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
std::map<uint64_t, std::vector<uint64_t>> offset_map; std::map<uint64_t, std::vector<uint64_t>> offset_map;
// [output] stores all output_data_t for each absolute_offset // [output] stores all output_data_t for each absolute_offset
std::map<uint64_t, std::vector<output_data_t>> tx_map; std::map<uint64_t, std::vector<output_data_t>> tx_map;
std::vector<std::pair<cryptonote::transaction, crypto::hash>> txes(total_txs);
#define SCAN_TABLE_QUIT(m) \ #define SCAN_TABLE_QUIT(m) \
do { \ do { \
@ -4214,6 +4217,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
} while(0); \ } while(0); \
// generate sorted tables for all amounts and absolute offsets // generate sorted tables for all amounts and absolute offsets
size_t tx_index = 0;
for (const auto &entry : blocks_entry) for (const auto &entry : blocks_entry)
{ {
if (m_cancel) if (m_cancel)
@ -4221,12 +4225,15 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
for (const auto &tx_blob : entry.txs) for (const auto &tx_blob : entry.txs)
{ {
crypto::hash tx_hash = null_hash; if (tx_index >= txes.size())
crypto::hash tx_prefix_hash = null_hash; SCAN_TABLE_QUIT("tx_index is out of sync");
transaction tx; transaction &tx = txes[tx_index].first;
crypto::hash &tx_prefix_hash = txes[tx_index].second;
++tx_index;
if (!parse_and_validate_tx_from_blob(tx_blob, tx, tx_hash, tx_prefix_hash)) if (!parse_and_validate_tx_base_from_blob(tx_blob, tx))
SCAN_TABLE_QUIT("Could not parse tx from incoming blocks."); SCAN_TABLE_QUIT("Could not parse tx from incoming blocks.");
cryptonote::get_transaction_prefix_hash(tx, tx_prefix_hash);
auto its = m_scan_table.find(tx_prefix_hash); auto its = m_scan_table.find(tx_prefix_hash);
if (its != m_scan_table.end()) if (its != m_scan_table.end())
@ -4312,9 +4319,8 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
} }
} }
int total_txs = 0;
// now generate a table for each tx_prefix and k_image hashes // now generate a table for each tx_prefix and k_image hashes
tx_index = 0;
for (const auto &entry : blocks_entry) for (const auto &entry : blocks_entry)
{ {
if (m_cancel) if (m_cancel)
@ -4322,14 +4328,12 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
for (const auto &tx_blob : entry.txs) for (const auto &tx_blob : entry.txs)
{ {
crypto::hash tx_hash = null_hash; if (tx_index >= txes.size())
crypto::hash tx_prefix_hash = null_hash; SCAN_TABLE_QUIT("tx_index is out of sync");
transaction tx; const transaction &tx = txes[tx_index].first;
const crypto::hash &tx_prefix_hash = txes[tx_index].second;
++tx_index;
if (!parse_and_validate_tx_from_blob(tx_blob, tx, tx_hash, tx_prefix_hash))
SCAN_TABLE_QUIT("Could not parse tx from incoming blocks.");
++total_txs;
auto its = m_scan_table.find(tx_prefix_hash); auto its = m_scan_table.find(tx_prefix_hash);
if (its == m_scan_table.end()) if (its == m_scan_table.end())
SCAN_TABLE_QUIT("Tx not found on scan table from incoming blocks."); SCAN_TABLE_QUIT("Tx not found on scan table from incoming blocks.");