@@ -1034,15 +1034,30 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
10341034 std::vector<uint64_t > timestamps;
10351035 std::vector<difficulty_type> cumulative_difficulties;
10361036
1037+ // set the right difficulty algorithm for the correct hard fork
1038+ size_t difficult_block_count;
1039+ difficulty_type diff;
1040+ uint64_t block_height = m_db->height ();
1041+ uint8_t version = get_current_hard_fork_version ();
1042+
1043+ if (version < HF_VERSION_LWMA_DIFFICULTY)
1044+ {
1045+ difficult_block_count = DIFFICULTY_BLOCKS_COUNT;
1046+ }
1047+ else
1048+ {
1049+ difficult_block_count = DIFFICULTY_WINDOW_V8;
1050+ }
1051+
10371052 // if the alt chain isn't long enough to calculate the difficulty target
10381053 // based on its blocks alone, need to get more blocks from the main chain
1039- if (alt_chain.size ()< DIFFICULTY_BLOCKS_COUNT )
1054+ if (alt_chain.size ()< difficult_block_count )
10401055 {
10411056 CRITICAL_REGION_LOCAL (m_blockchain_lock);
10421057
10431058 // Figure out start and stop offsets for main chain blocks
10441059 size_t main_chain_stop_offset = alt_chain.size () ? alt_chain.front ()->second .height : bei.height ;
1045- size_t main_chain_count = DIFFICULTY_BLOCKS_COUNT - std::min (static_cast <size_t >(DIFFICULTY_BLOCKS_COUNT ), alt_chain.size ());
1060+ size_t main_chain_count = difficult_block_count - std::min (static_cast <size_t >(difficult_block_count ), alt_chain.size ());
10461061 main_chain_count = std::min (main_chain_count, main_chain_stop_offset);
10471062 size_t main_chain_start_offset = main_chain_stop_offset - main_chain_count;
10481063
@@ -1057,7 +1072,7 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
10571072 }
10581073
10591074 // make sure we haven't accidentally grabbed too many blocks...maybe don't need this check?
1060- 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 );
1075+ 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 );
10611076
10621077 for (auto it : alt_chain)
10631078 {
@@ -1069,8 +1084,8 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
10691084 // and timestamps from it alone
10701085 else
10711086 {
1072- timestamps.resize (static_cast <size_t >(DIFFICULTY_BLOCKS_COUNT ));
1073- cumulative_difficulties.resize (static_cast <size_t >(DIFFICULTY_BLOCKS_COUNT ));
1087+ timestamps.resize (static_cast <size_t >(difficult_block_count ));
1088+ cumulative_difficulties.resize (static_cast <size_t >(difficult_block_count ));
10741089 size_t count = 0 ;
10751090 size_t max_i = timestamps.size ()-1 ;
10761091 // get difficulties and timestamps from most recent blocks in alt chain
@@ -1079,16 +1094,24 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
10791094 timestamps[max_i - count] = it->second .bl .timestamp ;
10801095 cumulative_difficulties[max_i - count] = it->second .cumulative_difficulty ;
10811096 count++;
1082- if (count >= DIFFICULTY_BLOCKS_COUNT )
1097+ if (count >= difficult_block_count )
10831098 break ;
10841099 }
10851100 }
10861101
10871102 // FIXME: This will fail if fork activation heights are subject to voting
10881103 size_t target = get_ideal_hard_fork_version (bei.height ) < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;
10891104
1090- // calculate the difficulty target for the block and return it
1091- return next_difficulty (timestamps, cumulative_difficulties, target);
1105+ // run the correct difficulty algorithm based on the hardfork
1106+ if (version < HF_VERSION_LWMA_DIFFICULTY)
1107+ {
1108+ diff = next_difficulty (timestamps, cumulative_difficulties, target);
1109+ }
1110+ else
1111+ {
1112+ diff = next_difficulty_V8 (timestamps, cumulative_difficulties, block_height);
1113+ }
1114+ return diff;
10921115}
10931116// ------------------------------------------------------------------
10941117// This function does a sanity check on basic things that all miner
0 commit comments