cryptonote: block parsing + hash calculation speedup

This saves a duplicate serialization step
release-v0.6.1.2
moneromooo-monero 6 years ago
parent 11604b6da6
commit 547a9708de
No known key found for this signature in database
GPG Key ID: 686F07454D6CEFC3

@ -1143,12 +1143,19 @@ namespace cryptonote
return blob; return blob;
} }
//--------------------------------------------------------------- //---------------------------------------------------------------
bool calculate_block_hash(const block& b, crypto::hash& res) bool calculate_block_hash(const block& b, crypto::hash& res, const blobdata *blob)
{ {
blobdata bd;
if (!blob)
{
bd = block_to_blob(b);
blob = &bd;
}
// EXCEPTION FOR BLOCK 202612 // EXCEPTION FOR BLOCK 202612
const std::string correct_blob_hash_202612 = "3a8a2b3a29b50fc86ff73dd087ea43c6f0d6b8f936c849194d5c84c737903966"; const std::string correct_blob_hash_202612 = "3a8a2b3a29b50fc86ff73dd087ea43c6f0d6b8f936c849194d5c84c737903966";
const std::string existing_block_id_202612 = "bbd604d2ba11ba27935e006ed39c9bfdd99b76bf4a50654bc1e1e61217962698"; const std::string existing_block_id_202612 = "bbd604d2ba11ba27935e006ed39c9bfdd99b76bf4a50654bc1e1e61217962698";
crypto::hash block_blob_hash = get_blob_hash(block_to_blob(b)); crypto::hash block_blob_hash = get_blob_hash(*blob);
if (string_tools::pod_to_hex(block_blob_hash) == correct_blob_hash_202612) if (string_tools::pod_to_hex(block_blob_hash) == correct_blob_hash_202612)
{ {
@ -1239,7 +1246,7 @@ namespace cryptonote
return p; return p;
} }
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b) bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash *block_hash)
{ {
std::stringstream ss; std::stringstream ss;
ss << b_blob; ss << b_blob;
@ -1248,9 +1255,26 @@ namespace cryptonote
CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob"); CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob");
b.invalidate_hashes(); b.invalidate_hashes();
b.miner_tx.invalidate_hashes(); b.miner_tx.invalidate_hashes();
if (block_hash)
{
calculate_block_hash(b, *block_hash, &b_blob);
++block_hashes_calculated_count;
b.hash = *block_hash;
b.set_hash_valid(true);
}
return true; return true;
} }
//--------------------------------------------------------------- //---------------------------------------------------------------
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b)
{
return parse_and_validate_block_from_blob(b_blob, b, NULL);
}
//---------------------------------------------------------------
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash &block_hash)
{
return parse_and_validate_block_from_blob(b_blob, b, &block_hash);
}
//---------------------------------------------------------------
blobdata block_to_blob(const block& b) blobdata block_to_blob(const block& b)
{ {
return t_serializable_object_to_blob(b); return t_serializable_object_to_blob(b);

@ -114,12 +114,14 @@ namespace cryptonote
crypto::hash get_pruned_transaction_hash(const transaction& t, const crypto::hash &pruned_data_hash); crypto::hash get_pruned_transaction_hash(const transaction& t, const crypto::hash &pruned_data_hash);
blobdata get_block_hashing_blob(const block& b); blobdata get_block_hashing_blob(const block& b);
bool calculate_block_hash(const block& b, crypto::hash& res); bool calculate_block_hash(const block& b, crypto::hash& res, const blobdata *blob = NULL);
bool get_block_hash(const block& b, crypto::hash& res); bool get_block_hash(const block& b, crypto::hash& res);
crypto::hash get_block_hash(const block& b); crypto::hash get_block_hash(const block& b);
bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height); bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height);
crypto::hash get_block_longhash(const block& b, uint64_t height); crypto::hash get_block_longhash(const block& b, uint64_t height);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash *block_hash);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b); bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash &block_hash);
bool get_inputs_money_amount(const transaction& tx, uint64_t& money); bool get_inputs_money_amount(const transaction& tx, uint64_t& money);
uint64_t get_outs_money_amount(const transaction& tx); uint64_t get_outs_money_amount(const transaction& tx);
bool check_inputs_types_supported(const transaction& tx); bool check_inputs_types_supported(const transaction& tx);

@ -4283,8 +4283,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
for (unsigned int j = 0; j < batches; j++, ++blockidx) for (unsigned int j = 0; j < batches; j++, ++blockidx)
{ {
block &block = blocks[blockidx]; block &block = blocks[blockidx];
crypto::hash block_hash;
if (!parse_and_validate_block_from_blob(it->block, block)) if (!parse_and_validate_block_from_blob(it->block, block, block_hash))
return false; return false;
// check first block and skip all blocks if its not chained properly // check first block and skip all blocks if its not chained properly
@ -4297,7 +4298,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
return true; return true;
} }
} }
if (have_block(get_block_hash(block))) if (have_block(block_hash))
blocks_exist = true; blocks_exist = true;
std::advance(it, 1); std::advance(it, 1);
@ -4307,11 +4308,12 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
for (unsigned i = 0; i < extra && !blocks_exist; i++, blockidx++) for (unsigned i = 0; i < extra && !blocks_exist; i++, blockidx++)
{ {
block &block = blocks[blockidx]; block &block = blocks[blockidx];
crypto::hash block_hash;
if (!parse_and_validate_block_from_blob(it->block, block)) if (!parse_and_validate_block_from_blob(it->block, block, block_hash))
return false; return false;
if (have_block(get_block_hash(block))) if (have_block(block_hash))
blocks_exist = true; blocks_exist = true;
std::advance(it, 1); std::advance(it, 1);

@ -1432,7 +1432,8 @@ namespace cryptonote
block lb; block lb;
if (!b) if (!b)
{ {
if(!parse_and_validate_block_from_blob(block_blob, lb)) crypto::hash block_hash;
if(!parse_and_validate_block_from_blob(block_blob, lb, block_hash))
{ {
LOG_PRINT_L1("Failed to parse and validate new block"); LOG_PRINT_L1("Failed to parse and validate new block");
bvc.m_verifivation_failed = true; bvc.m_verifivation_failed = true;

@ -993,7 +993,8 @@ namespace cryptonote
return 1; return 1;
} }
if(!parse_and_validate_block_from_blob(block_entry.block, b)) crypto::hash block_hash;
if(!parse_and_validate_block_from_blob(block_entry.block, b, block_hash))
{ {
LOG_ERROR_CCONTEXT("sent wrong block: failed to parse and validate block: " LOG_ERROR_CCONTEXT("sent wrong block: failed to parse and validate block: "
<< epee::string_tools::buff_to_hex_nodelimer(block_entry.block) << ", dropping connection"); << epee::string_tools::buff_to_hex_nodelimer(block_entry.block) << ", dropping connection");
@ -1012,7 +1013,6 @@ namespace cryptonote
if (start_height == std::numeric_limits<uint64_t>::max()) if (start_height == std::numeric_limits<uint64_t>::max())
start_height = boost::get<txin_gen>(b.miner_tx.vin[0]).height; start_height = boost::get<txin_gen>(b.miner_tx.vin[0]).height;
const crypto::hash block_hash = get_block_hash(b);
auto req_it = context.m_requested_objects.find(block_hash); auto req_it = context.m_requested_objects.find(block_hash);
if(req_it == context.m_requested_objects.end()) if(req_it == context.m_requested_objects.end())
{ {
@ -1119,13 +1119,13 @@ namespace cryptonote
<< ", we need " << previous_height); << ", we need " << previous_height);
block new_block; block new_block;
if (!parse_and_validate_block_from_blob(blocks.back().block, new_block)) crypto::hash last_block_hash;
if (!parse_and_validate_block_from_blob(blocks.back().block, new_block, last_block_hash))
{ {
MERROR(context << "Failed to parse block, but it should already have been parsed"); MERROR(context << "Failed to parse block, but it should already have been parsed");
m_block_queue.remove_spans(span_connection_id, start_height); m_block_queue.remove_spans(span_connection_id, start_height);
continue; continue;
} }
const crypto::hash last_block_hash = cryptonote::get_block_hash(new_block);
if (m_core.have_block(last_block_hash)) if (m_core.have_block(last_block_hash))
{ {
const uint64_t subchain_height = start_height + blocks.size(); const uint64_t subchain_height = start_height + blocks.size();

@ -2238,9 +2238,7 @@ void wallet2::get_short_chain_history(std::list<crypto::hash>& ids, uint64_t gra
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void wallet2::parse_block_round(const cryptonote::blobdata &blob, cryptonote::block &bl, crypto::hash &bl_id, bool &error) const void wallet2::parse_block_round(const cryptonote::blobdata &blob, cryptonote::block &bl, crypto::hash &bl_id, bool &error) const
{ {
error = !cryptonote::parse_and_validate_block_from_blob(blob, bl); error = !cryptonote::parse_and_validate_block_from_blob(blob, bl, bl_id);
if (!error)
bl_id = get_block_hash(bl);
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void wallet2::pull_blocks(uint64_t start_height, uint64_t &blocks_start_height, const std::list<crypto::hash> &short_chain_history, std::vector<cryptonote::block_complete_entry> &blocks, std::vector<cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::block_output_indices> &o_indices) void wallet2::pull_blocks(uint64_t start_height, uint64_t &blocks_start_height, const std::list<crypto::hash> &short_chain_history, std::vector<cryptonote::block_complete_entry> &blocks, std::vector<cryptonote::COMMAND_RPC_GET_BLOCKS_FAST::block_output_indices> &o_indices)

Loading…
Cancel
Save