Skip to content

Instantly share code, notes, and snippets.

@sebseb7
Created February 10, 2019 20:38
Show Gist options
  • Save sebseb7/a156f4086c5ece7ad2b8889d637954ad to your computer and use it in GitHub Desktop.
Save sebseb7/a156f4086c5ece7ad2b8889d637954ad to your computer and use it in GitHub Desktop.
diff --git a/src/cryptonote_basic/difficulty.cpp b/src/cryptonote_basic/difficulty.cpp
index cb2a00a1..e89d1fa0 100644
--- a/src/cryptonote_basic/difficulty.cpp
+++ b/src/cryptonote_basic/difficulty.cpp
@@ -119,7 +119,16 @@ namespace cryptonote {
return !carry;
}
- difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) {
+ difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds, uint64_t height, uint64_t last_diff_reset_height, difficulty_type last_diff_reset_value) {
+
+ bool is_diff_reset = false;
+ if (last_diff_reset_height != 0 && height >= last_diff_reset_height && height - last_diff_reset_height < timestamps.size())
+ {
+ is_diff_reset = true;
+ const uint64_t num_ignored_blocks = timestamps.size() - (height - last_diff_reset_height);
+ timestamps.erase(timestamps.begin(), timestamps.begin() + num_ignored_blocks);
+ cumulative_difficulties.erase(cumulative_difficulties.begin(), cumulative_difficulties.begin() + num_ignored_blocks);
+ }
if(timestamps.size() > DIFFICULTY_WINDOW)
{
@@ -131,6 +140,9 @@ namespace cryptonote {
size_t length = timestamps.size();
assert(length == cumulative_difficulties.size());
if (length <= 1) {
+ if (is_diff_reset) {
+ return last_diff_reset_value;
+ }
return 1;
}
static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small");
diff --git a/src/cryptonote_basic/difficulty.h b/src/cryptonote_basic/difficulty.h
index b0653846..38cb2f95 100644
--- a/src/cryptonote_basic/difficulty.h
+++ b/src/cryptonote_basic/difficulty.h
@@ -52,5 +52,5 @@ namespace cryptonote
* @return true if valid, else false
*/
bool check_hash(const crypto::hash &hash, difficulty_type difficulty);
- difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds);
+ difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds, uint64_t height, uint64_t last_diff_reset_height = 0, difficulty_type last_diff_reset_value = 0);
}
diff --git a/src/cryptonote_basic/hardfork.cpp b/src/cryptonote_basic/hardfork.cpp
index 5440acbb..34a1c07c 100644
--- a/src/cryptonote_basic/hardfork.cpp
+++ b/src/cryptonote_basic/hardfork.cpp
@@ -63,7 +63,7 @@ HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint6
throw "default_threshold_percent needs to be between 0 and 100";
}
-bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time)
+bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time, difficulty_type diff_reset_value)
{
CRITICAL_REGION_LOCAL(lock);
@@ -80,7 +80,7 @@ bool HardFork::add_fork(uint8_t version, uint64_t height, uint8_t threshold, tim
}
if (threshold > 100)
return false;
- heights.push_back(Params(version, height, threshold, time));
+ heights.push_back(Params(version, height, threshold, time, diff_reset_value));
return true;
}
@@ -164,7 +164,7 @@ void HardFork::init()
// add a placeholder for the default version, to avoid special cases
if (heights.empty())
- heights.push_back(Params(original_version, 0, 0, 0));
+ heights.push_back(Params(original_version, 0, 0, 0, 0));
versions.clear();
for (size_t n = 0; n < 256; ++n)
@@ -396,6 +396,28 @@ uint8_t HardFork::get_next_version() const
return original_version;
}
+uint64_t HardFork::get_last_diff_reset_height(uint64_t height) const
+{
+ for (auto i = heights.rbegin(); i != heights.rend(); ++i) {
+ if (height < i->height)
+ continue;
+ if (i->diff_reset_value > 0)
+ return i->height;
+ }
+ return 0;
+}
+
+difficulty_type HardFork::get_last_diff_reset_value(uint64_t height) const
+{
+ for (auto i = heights.rbegin(); i != heights.rend(); ++i) {
+ if (height < i->height)
+ continue;
+ if (i->diff_reset_value > 0)
+ return i->diff_reset_value;
+ }
+ return 0;
+}
+
bool HardFork::get_voting_info(uint8_t version, uint32_t &window, uint32_t &votes, uint32_t &threshold, uint64_t &earliest_height, uint8_t &voting) const
{
CRITICAL_REGION_LOCAL(lock);
diff --git a/src/cryptonote_basic/hardfork.h b/src/cryptonote_basic/hardfork.h
index a63a6697..a82100f1 100644
--- a/src/cryptonote_basic/hardfork.h
+++ b/src/cryptonote_basic/hardfork.h
@@ -71,7 +71,7 @@ namespace cryptonote
* @param threshold The threshold of votes needed for this fork (0-100)
* @param time Approximate time of the hardfork (seconds since epoch)
*/
- bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time);
+ bool add_fork(uint8_t version, uint64_t height, uint8_t threshold, time_t time, difficulty_type diff_reset_value = 0);
/**
* @brief add a new hardfork height
@@ -202,6 +202,18 @@ namespace cryptonote
uint64_t get_earliest_ideal_height_for_version(uint8_t version) const;
/**
+ * @brief returns the last height up to the given height when the difficult was reset
+ * 0 means there was no reset
+ */
+ uint64_t get_last_diff_reset_height(uint64_t height) const;
+
+ /**
+ * @brief returns the predefined difficulty for the last difficult reset up to the given height
+ * 0 means there was no reset
+ */
+ difficulty_type get_last_diff_reset_value(uint64_t height) const;
+
+ /**
* @brief returns information about current voting state
*
* returns true if the given version is enabled (ie, the current version
@@ -225,7 +237,8 @@ namespace cryptonote
uint8_t threshold;
uint64_t height;
time_t time;
- Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time): version(version), threshold(threshold), height(height), time(time) {}
+ difficulty_type diff_reset_value;
+ Params(uint8_t version, uint64_t height, uint8_t threshold, time_t time, difficulty_type diff_reset_value): version(version), threshold(threshold), height(height), time(time), diff_reset_value(diff_reset_value) {}
};
private:
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index f3122738..b668c4ae 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -89,13 +89,14 @@ static const struct {
uint64_t height;
uint8_t threshold;
time_t time;
+ difficulty_type diff_reset_value;
} mainnet_hard_forks[] = {
// version 1 from the start of the blockchain
- { 1, 1 , 0, 1542228716 },
- { 2, 100 , 0, 1542238717 },
- { 3, 150 , 0, 1542248718 },
- { 8, 150000 , 0, 1545317008 },
- { 9, 151000 , 0, 1545332008 }
+ { 1, 1 , 0, 1542228716, 0 },
+ { 2, 100 , 0, 1542238717, 0 },
+ { 3, 150 , 0, 1542248718, 0 },
+ { 8, 150000 , 0, 1545317008, 0 },
+ { 9, 151000 , 0, 1545332008, 0 }
};
static const struct {
@@ -103,12 +104,13 @@ static const struct {
uint64_t height;
uint8_t threshold;
time_t time;
+ difficulty_type diff_reset_value;
} testnet_hard_forks[] = {
// version 1 from the start of the blockchain
- { 1, 1, 0, 1542228716 },
- { 2, 100, 0, 1542438717 },
- { 3, 150, 0, 1542548718 },
- { 9, 250, 0, 1542648718 }
+ { 1, 1, 0, 1542228716, 0 },
+ { 2, 100, 0, 1542438717, 0 },
+ { 3, 150, 0, 1542548718, 0 },
+ { 9, 250, 0, 1542648718, 0 }
};
static const struct {
@@ -116,10 +118,11 @@ static const struct {
uint64_t height;
uint8_t threshold;
time_t time;
+ difficulty_type diff_reset_value;
} stagenet_hard_forks[] = {
// version 10 from the start of the blockchain
- { 1 , 1, 0, 1341378000 },
- { 10, 2, 0, 1341478000 },
+ { 1 , 1, 0, 1341378000, 0 },
+ { 10, 2, 0, 1341478000, 0 },
};
//------------------------------------------------------------------
@@ -335,17 +338,17 @@ bool Blockchain::init(BlockchainDB* db, const network_type nettype, bool offline
else if (m_nettype == TESTNET)
{
for (size_t n = 0; n < sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]); ++n)
- m_hardfork->add_fork(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].threshold, testnet_hard_forks[n].time);
+ m_hardfork->add_fork(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].threshold, testnet_hard_forks[n].time, testnet_hard_forks[n].diff_reset_value);
}
else if (m_nettype == STAGENET)
{
for (size_t n = 0; n < sizeof(stagenet_hard_forks) / sizeof(stagenet_hard_forks[0]); ++n)
- m_hardfork->add_fork(stagenet_hard_forks[n].version, stagenet_hard_forks[n].height, stagenet_hard_forks[n].threshold, stagenet_hard_forks[n].time);
+ m_hardfork->add_fork(stagenet_hard_forks[n].version, stagenet_hard_forks[n].height, stagenet_hard_forks[n].threshold, stagenet_hard_forks[n].time, stagenet_hard_forks[n].diff_reset_value);
}
else
{
for (size_t n = 0; n < sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]); ++n)
- m_hardfork->add_fork(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].threshold, mainnet_hard_forks[n].time);
+ m_hardfork->add_fork(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].threshold, mainnet_hard_forks[n].time, mainnet_hard_forks[n].diff_reset_value);
}
m_hardfork->init();
@@ -829,7 +832,9 @@ difficulty_type Blockchain::get_difficulty_for_next_block()
m_difficulties = difficulties;
}
size_t target = get_difficulty_target();
- difficulty_type diff = next_difficulty(timestamps, difficulties, target);
+ uint64_t last_diff_reset_height = m_hardfork->get_last_diff_reset_height(height);
+ difficulty_type last_diff_reset_value = m_hardfork->get_last_diff_reset_value(height);
+ difficulty_type diff = next_difficulty(timestamps, difficulties, target, height, last_diff_reset_height, last_diff_reset_value);
CRITICAL_REGION_LOCAL1(m_difficulty_lock);
m_difficulty_for_next_block_top_hash = top_hash;
@@ -1039,7 +1044,9 @@ difficulty_type Blockchain::get_next_difficulty_for_alternative_chain(const std:
}
// calculate the difficulty target for the block and return it
- return next_difficulty(timestamps, cumulative_difficulties, DIFFICULTY_TARGET);
+ uint64_t last_diff_reset_height = m_hardfork->get_last_diff_reset_height(bei.height);
+ difficulty_type last_diff_reset_value = m_hardfork->get_last_diff_reset_value(bei.height);
+ return next_difficulty(timestamps, cumulative_difficulties, DIFFICULTY_TARGET, bei.height, last_diff_reset_height, last_diff_reset_value);
}
//------------------------------------------------------------------
// This function does a sanity check on basic things that all miner
@@ -4213,21 +4220,21 @@ const std::vector<HardFork::Params>& Blockchain::get_hard_fork_heights(network_t
{
std::vector<HardFork::Params> heights;
for (const auto& i : mainnet_hard_forks)
- heights.emplace_back(i.version, i.height, i.threshold, i.time);
+ heights.emplace_back(i.version, i.height, i.threshold, i.time, i.diff_reset_value);
return heights;
}();
static const std::vector<HardFork::Params> testnet_heights = []()
{
std::vector<HardFork::Params> heights;
for (const auto& i : testnet_hard_forks)
- heights.emplace_back(i.version, i.height, i.threshold, i.time);
+ heights.emplace_back(i.version, i.height, i.threshold, i.time, i.diff_reset_value);
return heights;
}();
static const std::vector<HardFork::Params> stagenet_heights = []()
{
std::vector<HardFork::Params> heights;
for (const auto& i : stagenet_hard_forks)
- heights.emplace_back(i.version, i.height, i.threshold, i.time);
+ heights.emplace_back(i.version, i.height, i.threshold, i.time, i.diff_reset_value);
return heights;
}();
static const std::vector<HardFork::Params> dummy;
diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl
index d1db5e14..cab61d0e 100644
--- a/src/p2p/net_node.inl
+++ b/src/p2p/net_node.inl
@@ -395,6 +395,7 @@ namespace nodetool
}
else if (nettype == cryptonote::STAGENET)
{
+ full_addrs.insert("78.46.85.142:39949");
}
else if (nettype == cryptonote::FAKECHAIN)
{
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment