get_emission() simplified

basicauth
moneroexamples 7 years ago
parent 3fcf87bdfc
commit 24cd9d83f0

@ -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;
}

Loading…
Cancel
Save