Skip to content

Commit

Permalink
Update alt chain difficulty
Browse files Browse the repository at this point in the history
  • Loading branch information
atanmarko committed Jan 11, 2019
1 parent de02c2d commit c849928
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,15 +993,27 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
std::vector<uint64_t> timestamps;
std::vector<difficulty_type> cumulative_difficulties;

size_t difficulty_blocks_count;

if (m_hardfork->get_current_version() < HF_VERSION_DIFFICULTY_V2)
{
difficulty_blocks_count = DIFFICULTY_BLOCKS_COUNT;
}
else
{
difficulty_blocks_count = DIFFICULTY_BLOCKS_COUNT_V2;
}


// if the alt chain isn't long enough to calculate the difficulty target
// based on its blocks alone, need to get more blocks from the main chain
if(alt_chain.size()< DIFFICULTY_BLOCKS_COUNT)
if(alt_chain.size()< difficulty_blocks_count)
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);

// Figure out start and stop offsets for main chain blocks
size_t main_chain_stop_offset = alt_chain.size() ? alt_chain.front()->second.height : bei.height;
size_t main_chain_count = DIFFICULTY_BLOCKS_COUNT - std::min(static_cast<size_t>(DIFFICULTY_BLOCKS_COUNT), alt_chain.size());
size_t main_chain_count = difficulty_blocks_count - std::min(static_cast<size_t>(difficulty_blocks_count), alt_chain.size());
main_chain_count = std::min(main_chain_count, main_chain_stop_offset);
size_t main_chain_start_offset = main_chain_stop_offset - main_chain_count;

Expand All @@ -1016,7 +1028,7 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
}

// make sure we haven't accidentally grabbed too many blocks...maybe don't need this check?
CHECK_AND_ASSERT_MES((alt_chain.size() + timestamps.size()) <= DIFFICULTY_BLOCKS_COUNT, false, "Internal error, alt_chain.size()[" << alt_chain.size() << "] + vtimestampsec.size()[" << timestamps.size() << "] NOT <= DIFFICULTY_WINDOW[]" << DIFFICULTY_BLOCKS_COUNT);
CHECK_AND_ASSERT_MES((alt_chain.size() + timestamps.size()) <= difficulty_blocks_count, false, "Internal error, alt_chain.size()[" << alt_chain.size() << "] + vtimestampsec.size()[" << timestamps.size() << "] NOT <= DIFFICULTY_WINDOW[]" << difficulty_blocks_count);

for (auto it : alt_chain)
{
Expand All @@ -1028,8 +1040,8 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
// and timestamps from it alone
else
{
timestamps.resize(static_cast<size_t>(DIFFICULTY_BLOCKS_COUNT));
cumulative_difficulties.resize(static_cast<size_t>(DIFFICULTY_BLOCKS_COUNT));
timestamps.resize(static_cast<size_t>(difficulty_blocks_count));
cumulative_difficulties.resize(static_cast<size_t>(difficulty_blocks_count));
size_t count = 0;
size_t max_i = timestamps.size()-1;
// get difficulties and timestamps from most recent blocks in alt chain
Expand All @@ -1038,16 +1050,21 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
timestamps[max_i - count] = it->second.bl.timestamp;
cumulative_difficulties[max_i - count] = it->second.cumulative_difficulty;
count++;
if(count >= DIFFICULTY_BLOCKS_COUNT)
if(count >= difficulty_blocks_count)
break;
}
}

// FIXME: This will fail if fork activation heights are subject to voting
size_t target = DIFFICULTY_TARGET;
size_t target = get_difficulty_target();

// calculate the difficulty target for the block and return it
return next_difficulty(timestamps, cumulative_difficulties, target);
if (m_hardfork->get_current_version() < HF_VERSION_DIFFICULTY_V2)
{
return next_difficulty(timestamps, cumulative_difficulties, target);
}
else
{
return next_difficulty_v2(timestamps, cumulative_difficulties, target);
}
}
//------------------------------------------------------------------
// This function does a sanity check on basic things that all miner
Expand Down

0 comments on commit c849928

Please sign in to comment.