Skip to content

Commit

Permalink
alt block fix
Browse files Browse the repository at this point in the history
alt block fix
  • Loading branch information
X-CASH-official committed Oct 10, 2018
1 parent e65db6b commit 99c69c1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
39 changes: 31 additions & 8 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,15 +1034,30 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
std::vector<uint64_t> timestamps;
std::vector<difficulty_type> cumulative_difficulties;

// set the right difficulty algorithm for the correct hard fork
size_t difficult_block_count;
difficulty_type diff;
uint64_t block_height = m_db->height();
uint8_t version = get_current_hard_fork_version();

if (version < HF_VERSION_LWMA_DIFFICULTY)
{
difficult_block_count = DIFFICULTY_BLOCKS_COUNT;
}
else
{
difficult_block_count = DIFFICULTY_WINDOW_V8;
}

// 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()< difficult_block_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 = difficult_block_count - std::min(static_cast<size_t>(difficult_block_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 @@ -1057,7 +1072,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()) <= difficult_block_count, false, "Internal error, alt_chain.size()[" << alt_chain.size() << "] + vtimestampsec.size()[" << timestamps.size() << "] NOT <= DIFFICULTY_WINDOW[]" << difficult_block_count);

for (auto it : alt_chain)
{
Expand All @@ -1069,8 +1084,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>(difficult_block_count));
cumulative_difficulties.resize(static_cast<size_t>(difficult_block_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 @@ -1079,16 +1094,24 @@ 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 >= difficult_block_count)
break;
}
}

// FIXME: This will fail if fork activation heights are subject to voting
size_t target = get_ideal_hard_fork_version(bei.height) < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;

// calculate the difficulty target for the block and return it
return next_difficulty(timestamps, cumulative_difficulties, target);
// run the correct difficulty algorithm based on the hardfork
if (version < HF_VERSION_LWMA_DIFFICULTY)
{
diff = next_difficulty(timestamps, cumulative_difficulties, target);
}
else
{
diff = next_difficulty_V8(timestamps, cumulative_difficulties, block_height);
}
return diff;
}
//------------------------------------------------------------------
// This function does a sanity check on basic things that all miner
Expand Down
2 changes: 1 addition & 1 deletion src/version.cpp.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define DEF_XCASH_VERSION_TAG "@VERSIONTAG@"
#define DEF_XCASH_VERSION "1.3.0"
#define DEF_XCASH_VERSION "1.3.1"
#define DEF_XCASH_RELEASE_NAME ""
#define DEF_XCASH_VERSION_FULL DEF_XCASH_VERSION "-" DEF_XCASH_VERSION_TAG

Expand Down

0 comments on commit 99c69c1

Please sign in to comment.