diff --git a/src/cryptonote_config.h b/src/cryptonote_config.h index 94ead558f..6053387ac 100644 --- a/src/cryptonote_config.h +++ b/src/cryptonote_config.h @@ -75,11 +75,12 @@ #define DIFFICULTY_BLOCKS_COUNT DIFFICULTY_WINDOW + DIFFICULTY_LAG +#define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1 DIFFICULTY_TARGET_V1 * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS #define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS DIFFICULTY_TARGET * CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS #define CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_BLOCKS 1 -#define DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN DIFFICULTY_TARGET //just alias +#define DIFFICULTY_BLOCKS_ESTIMATE_TIMESPAN DIFFICULTY_TARGET_V1 //just alias; used by tests #define BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT 10000 //by default, blocks ids count in synchronizing diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index a24ef8283..e6a233868 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -674,8 +674,8 @@ difficulty_type Blockchain::get_difficulty_for_next_block() m_timestamps = timestamps; m_difficulties = difficulties; } - - return next_difficulty(timestamps, difficulties); + size_t target = get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET; + return next_difficulty(timestamps, difficulties, target); } //------------------------------------------------------------------ // This function removes blocks from the blockchain until it gets to the @@ -865,8 +865,11 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std: } } + // 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; + // calculate the difficulty target for the block and return it - return next_difficulty(timestamps, cumulative_difficulties); + return next_difficulty(timestamps, cumulative_difficulties, target); } //------------------------------------------------------------------ // This function does a sanity check on basic things that all miner @@ -2194,7 +2197,7 @@ bool Blockchain::is_tx_spendtime_unlocked(uint64_t unlock_time) const { //interpret as time uint64_t current_time = static_cast(time(NULL)); - if(current_time + CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS >= unlock_time) + if(current_time + (get_current_hard_fork_version() < 2 ? CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1 : CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS) >= unlock_time) return true; else return false; diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 00670c6d3..c83314634 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -160,6 +160,8 @@ namespace cryptonote HardFork::State get_hard_fork_state() const; uint8_t get_current_hard_fork_version() const { return m_hardfork->get_current_version(); } uint8_t get_ideal_hard_fork_version() const { return m_hardfork->get_ideal_version(); } + uint8_t get_ideal_hard_fork_version(uint64_t height) const { return m_hardfork->get_ideal_version(height); } + bool get_hard_fork_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint8_t &voting) const; bool for_all_key_images(std::function) const; diff --git a/src/cryptonote_core/blockchain_storage.cpp b/src/cryptonote_core/blockchain_storage.cpp index fb06da48a..f7c9ffb78 100644 --- a/src/cryptonote_core/blockchain_storage.cpp +++ b/src/cryptonote_core/blockchain_storage.cpp @@ -430,7 +430,7 @@ difficulty_type blockchain_storage::get_difficulty_for_next_block() const timestamps.push_back(m_blocks[offset].bl.timestamp); commulative_difficulties.push_back(m_blocks[offset].cumulative_difficulty); } - return next_difficulty(timestamps, commulative_difficulties); + return next_difficulty(timestamps, commulative_difficulties,DIFFICULTY_TARGET_V1); } //------------------------------------------------------------------ bool blockchain_storage::rollback_blockchain_switching(std::list& original_chain, size_t rollback_height) @@ -571,7 +571,7 @@ difficulty_type blockchain_storage::get_next_difficulty_for_alternative_chain(co break; } } - return next_difficulty(timestamps, commulative_difficulties); + return next_difficulty(timestamps, commulative_difficulties,DIFFICULTY_TARGET_V1); } //------------------------------------------------------------------ bool blockchain_storage::prevalidate_miner_transaction(const block& b, uint64_t height) const @@ -1505,7 +1505,7 @@ bool blockchain_storage::is_tx_spendtime_unlocked(uint64_t unlock_time) const { //interpret as time uint64_t current_time = static_cast(time(NULL)); - if(current_time + CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS >= unlock_time) + if(current_time + CRYPTONOTE_LOCKED_TX_ALLOWED_DELTA_SECONDS_V1 >= unlock_time) return true; else return false; diff --git a/src/cryptonote_core/difficulty.cpp b/src/cryptonote_core/difficulty.cpp index 2b5466791..3c3f1dec1 100644 --- a/src/cryptonote_core/difficulty.cpp +++ b/src/cryptonote_core/difficulty.cpp @@ -157,8 +157,4 @@ namespace cryptonote { return (low + time_span - 1) / time_span; } - difficulty_type next_difficulty(vector timestamps, vector cumulative_difficulties) - { - return next_difficulty(std::move(timestamps), std::move(cumulative_difficulties), DIFFICULTY_TARGET); - } } diff --git a/src/cryptonote_core/difficulty.h b/src/cryptonote_core/difficulty.h index 42d2df500..6e06dd702 100644 --- a/src/cryptonote_core/difficulty.h +++ b/src/cryptonote_core/difficulty.h @@ -40,6 +40,5 @@ namespace cryptonote typedef std::uint64_t difficulty_type; bool check_hash(const crypto::hash &hash, difficulty_type difficulty); - difficulty_type next_difficulty(std::vector timestamps, std::vector cumulative_difficulties); difficulty_type next_difficulty(std::vector timestamps, std::vector cumulative_difficulties, size_t target_seconds); } diff --git a/tests/core_tests/block_validation.cpp b/tests/core_tests/block_validation.cpp index 97d6634c0..440e46647 100644 --- a/tests/core_tests/block_validation.cpp +++ b/tests/core_tests/block_validation.cpp @@ -45,7 +45,7 @@ namespace for (size_t i = 0; i < new_block_count; ++i) { block blk_next; - difficulty_type diffic = next_difficulty(timestamps, cummulative_difficulties); + difficulty_type diffic = next_difficulty(timestamps, cummulative_difficulties,DIFFICULTY_TARGET_V1); if (!generator.construct_block_manually(blk_next, blk_prev, miner_account, test_generator::bf_timestamp | test_generator::bf_diffic, 0, 0, blk_prev.timestamp, crypto::hash(), diffic)) return false; @@ -175,7 +175,7 @@ bool gen_block_invalid_nonce::generate(std::vector& events) co return false; // Create invalid nonce - difficulty_type diffic = next_difficulty(timestamps, commulative_difficulties); + difficulty_type diffic = next_difficulty(timestamps, commulative_difficulties,DIFFICULTY_TARGET_V1); assert(1 < diffic); const block& blk_last = boost::get(events.back()); uint64_t timestamp = blk_last.timestamp; @@ -570,7 +570,7 @@ bool gen_block_invalid_binary_format::generate(std::vector& ev do { blk_last = boost::get(events.back()); - diffic = next_difficulty(timestamps, cummulative_difficulties); + diffic = next_difficulty(timestamps, cummulative_difficulties,DIFFICULTY_TARGET_V1); if (!lift_up_difficulty(events, timestamps, cummulative_difficulties, generator, 1, blk_last, miner_account)) return false; std::cout << "Block #" << events.size() << ", difficulty: " << diffic << std::endl; @@ -585,7 +585,7 @@ bool gen_block_invalid_binary_format::generate(std::vector& ev std::vector tx_hashes; tx_hashes.push_back(get_transaction_hash(tx_0)); size_t txs_size = get_object_blobsize(tx_0); - diffic = next_difficulty(timestamps, cummulative_difficulties); + diffic = next_difficulty(timestamps, cummulative_difficulties,DIFFICULTY_TARGET_V1); if (!generator.construct_block_manually(blk_test, blk_last, miner_account, test_generator::bf_diffic | test_generator::bf_timestamp | test_generator::bf_tx_hashes, 0, 0, blk_last.timestamp, crypto::hash(), diffic, transaction(), tx_hashes, txs_size))