From 24cd9d83f0ba94e2997dc5f9f71b8c5c1a3af022 Mon Sep 17 00:00:00 2001 From: moneroexamples Date: Fri, 19 May 2017 12:43:43 +0800 Subject: [PATCH] get_emission() simplified --- src/CurrentBlockchainStatus.cpp | 75 +++++++++------------------------ 1 file changed, 20 insertions(+), 55 deletions(-) diff --git a/src/CurrentBlockchainStatus.cpp b/src/CurrentBlockchainStatus.cpp index 05957b2..4b13b57 100644 --- a/src/CurrentBlockchainStatus.cpp +++ b/src/CurrentBlockchainStatus.cpp @@ -249,15 +249,6 @@ CurrentBlockchainStatus::get_output_file_path() CurrentBlockchainStatus::Emission CurrentBlockchainStatus::get_emission() { - // we store gap emission from last few blocks - // we use it together with last_block_hash, so that - // we can reuse gap emission if hash is same between - // requests. no need to always keep recalucalting - // it, if top block hash is same as last time. - static Emission total_emission_gap {0, 0, 0}; - static crypto::hash last_block_hash {null_hash}; - - // get current emission Emission current_emission = total_emission_atomic; @@ -266,61 +257,35 @@ CurrentBlockchainStatus::get_emission() // the emission from the top missing blocks, to have complete // emission data. - Emission gap_emission_calculated {0, 0, 0}; - - crypto::hash top_block_id = core_storage->get_tail_id(); + uint64_t current_blockchain_height = current_height; - if (last_block_hash == top_block_id) - { - // if we already calclated gap for the same few blocks - // just reuse the emission calcualted for the gap. + uint64_t start_blk = current_emission.blk_no; - //cout << "reusing total_emission_gap" << endl; + // this should be at current hight or above + // as we calculate missing blocks only for top blockchain + // height + uint64_t end_block = start_blk + blockchain_chunk_gap; - gap_emission_calculated = total_emission_gap; - } - else + if (end_block >= current_blockchain_height + && start_blk < current_blockchain_height) { - // if top height has change for whatever reason, e.g, new block - // was added, blockchain reoraganization happened, then - // recaulate the gap emission - - //cout << "recalculate total_emission_gap" << endl; - - uint64_t current_blockchain_height = current_height; + // make sure we are not over the blockchain height + end_block = end_block > current_blockchain_height + ? current_blockchain_height : end_block; - uint64_t start_blk = current_emission.blk_no; + // calculated emission for missing blocks + Emission gap_emission_calculated + = calculate_emission_in_blocks(start_blk, end_block); - // this should be at current hight or above - // as we calculate missing blocks only for top blockchain - // height - uint64_t end_block = start_blk + blockchain_chunk_gap; + //cout << "gap_emission_calculated: " << std::string(gap_emission_calculated) << endl; - if (end_block >= current_blockchain_height && start_blk < current_blockchain_height) - { - // make sure we are not over the blockchain height - end_block = end_block > current_blockchain_height - ? current_blockchain_height : end_block; - - // calculated emission for missing blocks - gap_emission_calculated = calculate_emission_in_blocks(start_blk, end_block); - } - - // store calcualted gap emission for future use - total_emission_gap = gap_emission_calculated; - - // update stored top block hash - last_block_hash = top_block_id; + current_emission.coinbase += gap_emission_calculated.coinbase; + current_emission.fee += gap_emission_calculated.fee; + current_emission.blk_no = gap_emission_calculated.blk_no > 0 + ? gap_emission_calculated.blk_no + : current_emission.blk_no; } - //cout << "gap_emission_calculated: " << std::string(gap_emission_calculated) << endl; - - current_emission.coinbase += gap_emission_calculated.coinbase; - current_emission.fee += gap_emission_calculated.fee; - current_emission.blk_no = gap_emission_calculated.blk_no > 0 - ? gap_emission_calculated.blk_no - : current_emission.blk_no; - return current_emission; }