get_emission() simplified

master
moneroexamples 8 years ago
parent 3fcf87bdfc
commit 24cd9d83f0

@ -249,15 +249,6 @@ CurrentBlockchainStatus::get_output_file_path()
CurrentBlockchainStatus::Emission CurrentBlockchainStatus::Emission
CurrentBlockchainStatus::get_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 // get current emission
Emission current_emission = total_emission_atomic; Emission current_emission = total_emission_atomic;
@ -266,61 +257,35 @@ CurrentBlockchainStatus::get_emission()
// the emission from the top missing blocks, to have complete // the emission from the top missing blocks, to have complete
// emission data. // emission data.
Emission gap_emission_calculated {0, 0, 0}; uint64_t current_blockchain_height = current_height;
crypto::hash top_block_id = core_storage->get_tail_id();
if (last_block_hash == top_block_id) uint64_t start_blk = current_emission.blk_no;
{
// if we already calclated gap for the same few blocks
// just reuse the emission calcualted for the gap.
//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; if (end_block >= current_blockchain_height
} && start_blk < current_blockchain_height)
else
{ {
// if top height has change for whatever reason, e.g, new block // make sure we are not over the blockchain height
// was added, blockchain reoraganization happened, then end_block = end_block > current_blockchain_height
// recaulate the gap emission ? current_blockchain_height : end_block;
//cout << "recalculate total_emission_gap" << endl;
uint64_t current_blockchain_height = current_height;
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 //cout << "gap_emission_calculated: " << std::string(gap_emission_calculated) << endl;
// as we calculate missing blocks only for top blockchain
// height
uint64_t end_block = start_blk + blockchain_chunk_gap;
if (end_block >= current_blockchain_height && start_blk < current_blockchain_height) current_emission.coinbase += gap_emission_calculated.coinbase;
{ current_emission.fee += gap_emission_calculated.fee;
// make sure we are not over the blockchain height current_emission.blk_no = gap_emission_calculated.blk_no > 0
end_block = end_block > current_blockchain_height ? gap_emission_calculated.blk_no
? current_blockchain_height : end_block; : current_emission.blk_no;
// 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;
} }
//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; return current_emission;
} }

Loading…
Cancel
Save