diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cryptonote_core/blockchain.cpp | 74 | ||||
-rw-r--r-- | src/cryptonote_core/hardfork.cpp | 70 | ||||
-rw-r--r-- | src/cryptonote_core/hardfork.h | 17 |
3 files changed, 130 insertions, 31 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 65d07dcf7..3d96f6f5d 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -73,13 +73,24 @@ static const struct { uint8_t version; uint64_t height; time_t time; -} hard_forks[] = { +} mainnet_hard_forks[] = { // version 1 from the start of the blockchain { 1, 1, 1341378000 }, // version 2 can start from block 1009827, setup on the 20th of september { 2, 1009827, 1442763710 }, }; +static const uint64_t mainnet_hard_fork_version_1_till = 750000; + +static const struct { + uint8_t version; + uint64_t height; + time_t time; +} testnet_hard_forks[] = { + // version 1 from the start of the blockchain + { 1, 1, 1341378000 }, +}; +static const uint64_t testnet_hard_fork_version_1_till = 540000; //------------------------------------------------------------------ Blockchain::Blockchain(tx_memory_pool& tx_pool) : @@ -287,9 +298,17 @@ bool Blockchain::init(BlockchainDB* db, const bool testnet) m_db = db; - m_hardfork = new HardFork(*db); - for (size_t n = 0; n < sizeof(hard_forks) / sizeof(hard_forks[0]); ++n) - m_hardfork->add(hard_forks[n].version, hard_forks[n].height, hard_forks[n].time); + if (testnet) { + m_hardfork = new HardFork(*db, 1, testnet_hard_fork_version_1_till); + for (size_t n = 0; n < sizeof(testnet_hard_forks) / sizeof(testnet_hard_forks[0]); ++n) + m_hardfork->add(testnet_hard_forks[n].version, testnet_hard_forks[n].height, testnet_hard_forks[n].time); + } + else + { + m_hardfork = new HardFork(*db, 1, mainnet_hard_fork_version_1_till); + for (size_t n = 0; n < sizeof(mainnet_hard_forks) / sizeof(mainnet_hard_forks[0]); ++n) + m_hardfork->add(mainnet_hard_forks[n].version, mainnet_hard_forks[n].height, mainnet_hard_forks[n].time); + } m_hardfork->init(); // if the blockchain is new, add the genesis block @@ -947,10 +966,14 @@ bool Blockchain::validate_miner_transaction(const block& b, size_t cumulative_bl LOG_PRINT_L1("coinbase transaction spend too much money (" << print_money(money_in_use) << "). Block reward is " << print_money(base_reward + fee) << "(" << print_money(base_reward) << "+" << print_money(fee) << ")"); return false; } - if(base_reward + fee != money_in_use) + // From hard fork 2, we allow a miner to claim less block reward than is allowed, in case a miner wants less dust + if (m_hardfork->get_current_version() < 2) { - LOG_PRINT_L1("coinbase transaction doesn't use full amount of block reward: spent: " << money_in_use << ", block reward " << base_reward + fee << "(" << base_reward << "+" << fee << ")"); - return false; + if(base_reward + fee != money_in_use) + { + LOG_PRINT_L1("coinbase transaction doesn't use full amount of block reward: spent: " << money_in_use << ", block reward " << base_reward + fee << "(" << base_reward << "+" << fee << ")"); + return false; + } } return true; } @@ -1975,6 +1998,43 @@ bool Blockchain::check_tx_inputs(const transaction& tx, uint64_t* pmax_used_bloc return true; } + // from hard fork 2, we require mixin at least 2 unless one output cannot mix with 2 others + // if one output cannot mix with 2 others, we accept at most 1 output that can mix + if (m_hardfork->get_current_version() >= 2) + { + size_t n_unmixable = 0, n_mixable = 0; + size_t mixin = std::numeric_limits<size_t>::max(); + for (const auto& txin : tx.vin) + { + // non txin_to_key inputs will be rejected below + if (txin.type() == typeid(txin_to_key)) + { + const txin_to_key& in_to_key = boost::get<txin_to_key>(txin); + uint64_t n_outputs = m_db->get_num_outputs(in_to_key.amount); + // n_outputs includes the output we're considering + if (n_outputs <= 2) + ++n_unmixable; + else + ++n_mixable; + if (in_to_key.key_offsets.size() - 1 < mixin) + mixin = in_to_key.key_offsets.size() - 1; + } + } + if (mixin < 2) + { + if (n_unmixable == 0) + { + LOG_PRINT_L1("Tx " << get_transaction_hash(tx) << " has too low mixin (" << mixin << "), and no unmixable inputs"); + return false; + } + if (n_mixable > 1) + { + LOG_PRINT_L1("Tx " << get_transaction_hash(tx) << " has too low mixin (" << mixin << "), and more than one mixable input with unmixable inputs"); + return false; + } + } + } + auto it = m_check_txin_table.find(tx_prefix_hash); if(it == m_check_txin_table.end()) { diff --git a/src/cryptonote_core/hardfork.cpp b/src/cryptonote_core/hardfork.cpp index d2e95b58e..4b40a4cf3 100644 --- a/src/cryptonote_core/hardfork.cpp +++ b/src/cryptonote_core/hardfork.cpp @@ -35,14 +35,19 @@ using namespace cryptonote; -HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent): +HardFork::HardFork(cryptonote::BlockchainDB &db, uint8_t original_version, uint64_t original_version_till_height, time_t forked_time, time_t update_time, uint64_t window_size, int threshold_percent): db(db), original_version(original_version), + original_version_till_height(original_version_till_height), forked_time(forked_time), update_time(update_time), window_size(window_size), threshold_percent(threshold_percent) { + if (window_size == 0) + throw "window_size needs to be strictly positive"; + if (threshold_percent > 100) + throw "threshold_percent needs to be between 0 and 100"; } bool HardFork::add(uint8_t version, uint64_t height, time_t time) @@ -60,13 +65,12 @@ bool HardFork::add(uint8_t version, uint64_t height, time_t time) if (time <= heights.back().time) return false; } - heights.push_back({version: version, height: height, time: time}); + heights.push_back(Params(version, height, time)); return true; } -uint8_t HardFork::get_effective_version(const cryptonote::block &block) const +uint8_t HardFork::get_effective_version(uint8_t version) const { - uint8_t version = block.major_version; if (!heights.empty()) { uint8_t max_version = heights.back().version; if (version > max_version) @@ -75,25 +79,27 @@ uint8_t HardFork::get_effective_version(const cryptonote::block &block) const return version; } -bool HardFork::do_check(const cryptonote::block &block) const +bool HardFork::do_check(uint8_t version) const { - return block.major_version >= heights[current_fork_index].version; + return version >= heights[current_fork_index].version; } bool HardFork::check(const cryptonote::block &block) const { CRITICAL_REGION_LOCAL(lock); - return do_check(block); + return do_check(block.major_version); } -bool HardFork::add(const cryptonote::block &block, uint64_t height) +bool HardFork::add(uint8_t block_version, uint64_t height) { CRITICAL_REGION_LOCAL(lock); - if (!do_check(block)) + if (!do_check(block_version)) return false; - const uint8_t version = get_effective_version(block); + db.set_hard_fork_version(height, heights[current_fork_index].version); + + const uint8_t version = get_effective_version(block_version); while (versions.size() >= window_size) { const uint8_t old_version = versions.front(); @@ -105,19 +111,23 @@ bool HardFork::add(const cryptonote::block &block, uint64_t height) last_versions[version]++; versions.push_back(version); - uint8_t voted = get_voted_fork_index(height); + uint8_t voted = get_voted_fork_index(height + 1); if (voted > current_fork_index) { for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) { - db.set_hard_fork_starting_height(v, height); + // we reached the vote threshold with this block, next one will be forked + db.set_hard_fork_starting_height(v, height + 1); } current_fork_index = voted; } - db.set_hard_fork_version(height, heights[current_fork_index].version); - return true; } +bool HardFork::add(const cryptonote::block &block, uint64_t height) +{ + return add(block.major_version, height); +} + void HardFork::init() { CRITICAL_REGION_LOCAL(lock); @@ -149,17 +159,29 @@ void HardFork::init() LOG_PRINT_L1("reorganization done"); } +uint8_t HardFork::get_block_version(uint64_t height) const +{ + if (height <= original_version_till_height) + return original_version; + + const cryptonote::block &block = db.get_block_from_height(height); + return block.major_version; +} + bool HardFork::reorganize_from_block_height(uint64_t height) { CRITICAL_REGION_LOCAL(lock); if (height >= db.height()) return false; + //db.set_batch_transactions(true); + //db.batch_start(); + versions.clear(); for (size_t n = 0; n < 256; ++n) last_versions[n] = 0; - const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size - 1) : 0; + const uint64_t rescan_height = height >= (window_size - 1) ? height - (window_size -1) : 0; const uint8_t start_version = height == 0 ? original_version : db.get_hard_fork_version(height); while (heights[current_fork_index].version > start_version) { db.set_hard_fork_starting_height(heights[current_fork_index].version, std::numeric_limits<uint64_t>::max()); @@ -167,15 +189,27 @@ bool HardFork::reorganize_from_block_height(uint64_t height) } for (uint64_t h = rescan_height; h <= height; ++h) { cryptonote::block b = db.get_block_from_height(h); - const uint8_t v = get_effective_version(b); + const uint8_t v = get_effective_version(b.major_version); last_versions[v]++; versions.push_back(v); } + + uint8_t voted = get_voted_fork_index(height + 1); + if (voted > current_fork_index) { + for (int v = heights[current_fork_index].version + 1; v <= heights[voted].version; ++v) { + // we reached the vote threshold with this block, next one will be forked + db.set_hard_fork_starting_height(v, height + 1); + } + current_fork_index = voted; + } + const uint64_t bc_height = db.height(); for (uint64_t h = height + 1; h < bc_height; ++h) { - add(db.get_block_from_height(h), h); + add(get_block_version(h), h); } + //db.batch_stop(); + return true; } @@ -224,7 +258,7 @@ HardFork::State HardFork::get_state() const uint8_t HardFork::get(uint64_t height) const { CRITICAL_REGION_LOCAL(lock); - if (height > db.height()) { + if (height >= db.height()) { assert(false); return 255; } diff --git a/src/cryptonote_core/hardfork.h b/src/cryptonote_core/hardfork.h index bdac87f2c..6b98b9fa1 100644 --- a/src/cryptonote_core/hardfork.h +++ b/src/cryptonote_core/hardfork.h @@ -44,9 +44,10 @@ namespace cryptonote Ready, } State; + static const uint64_t DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT = 0; // <= actual height static const time_t DEFAULT_FORKED_TIME = 31557600; // a year in seconds static const time_t DEFAULT_UPDATE_TIME = 31557600 / 2; - static const uint64_t DEFAULT_WINDOW_SIZE = 50; // supermajority window check length + static const uint64_t DEFAULT_WINDOW_SIZE = 10080; // supermajority window check length - a week static const int DEFAULT_THRESHOLD_PERCENT = 80; /** @@ -58,7 +59,7 @@ namespace cryptonote * @param window_size the size of the window in blocks to consider for version voting * @param threshold_percent the size of the majority in percents */ - HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, int threshold_percent = DEFAULT_THRESHOLD_PERCENT); + HardFork(cryptonote::BlockchainDB &db, uint8_t original_version = 1, uint64_t original_version_till_height = DEFAULT_ORIGINAL_VERSION_TILL_HEIGHT, time_t forked_time = DEFAULT_FORKED_TIME, time_t update_time = DEFAULT_UPDATE_TIME, uint64_t window_size = DEFAULT_WINDOW_SIZE, int threshold_percent = DEFAULT_THRESHOLD_PERCENT); /** * @brief add a new hardfork height @@ -182,9 +183,11 @@ namespace cryptonote private: - bool do_check(const cryptonote::block &block) const; + uint8_t get_block_version(uint64_t height) const; + bool do_check(uint8_t version) const; int get_voted_fork_index(uint64_t height) const; - uint8_t get_effective_version(const cryptonote::block &block) const; + uint8_t get_effective_version(uint8_t version) const; + bool add(uint8_t block_version, uint64_t height); private: @@ -196,12 +199,14 @@ namespace cryptonote int threshold_percent; uint8_t original_version; + uint64_t original_version_till_height; - typedef struct { + struct Params { uint8_t version; uint64_t height; time_t time; - } Params; + Params(uint8_t version, uint64_t height, time_t time): version(version), height(height), time(time) {} + }; std::vector<Params> heights; std::deque<uint8_t> versions; /* rolling window of the last N blocks' versions */ |