diff --git a/src/block_template.cpp b/src/block_template.cpp index 708359c..4ac086e 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -207,7 +207,7 @@ void BlockTemplate::shuffle_tx_order() } } -void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet* miner_wallet) +void BlockTemplate::update(const MinerData& data, const Mempool& mempool, const Wallet* miner_wallet) { if (data.major_version > HARDFORK_SUPPORTED_VERSION) { LOGERR(1, "got hardfork version " << data.major_version << ", expected <= " << HARDFORK_SUPPORTED_VERSION); @@ -272,14 +272,6 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet m_blockHeaderSize = m_blockHeader.size(); - const int sidechain_version = m_poolBlockTemplate->get_sidechain_version(); - - if (sidechain_version <= 1) { - get_tx_keys(m_poolBlockTemplate->m_txkeyPub, m_poolBlockTemplate->m_txkeySec, miner_wallet->spend_public_key(), data.prev_id); - // Both values are the same before v2 - m_poolBlockTemplate->m_txkeySecSeed = m_poolBlockTemplate->m_txkeySec; - } - m_poolBlockTemplate->m_minerWallet = *miner_wallet; m_sidechain->fill_sidechain_data(*m_poolBlockTemplate, m_shares); @@ -669,7 +661,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet { memset(m_sidechainHashKeccakState, 0, sizeof(m_sidechainHashKeccakState)); - const size_t extra_nonce_offset = m_sidechainHashBlob.size() - HASH_SIZE - ((sidechain_version > 1) ? EXTRA_NONCE_SIZE : 0); + const size_t extra_nonce_offset = m_sidechainHashBlob.size() - HASH_SIZE - EXTRA_NONCE_SIZE; if (extra_nonce_offset >= KeccakParams::HASH_DATA_AREA) { // Sidechain data is big enough to cache keccak state up to extra_nonce m_sidechainHashInputLength = (extra_nonce_offset / KeccakParams::HASH_DATA_AREA) * KeccakParams::HASH_DATA_AREA; @@ -954,11 +946,10 @@ int BlockTemplate::create_miner_tx(const MinerData& data, const std::vectorget_sidechain_version(); const size_t size = m_sidechainHashBlob.size(); const size_t N = m_sidechainHashInputLength; - const size_t sidechain_extra_nonce_offset = size - HASH_SIZE - ((v > 1) ? EXTRA_NONCE_SIZE : 0); + const size_t sidechain_extra_nonce_offset = size - HASH_SIZE - EXTRA_NONCE_SIZE; const uint8_t sidechain_extra_nonce_buf[EXTRA_NONCE_SIZE] = { static_cast(sidechain_extra_nonce >> 0), static_cast(sidechain_extra_nonce >> 8), @@ -973,12 +964,10 @@ hash BlockTemplate::calc_sidechain_hash(uint32_t sidechain_extra_nonce) const // Slow path: O(N) if (!b || pool_block_debug()) { - keccak_custom([this, v, sidechain_extra_nonce_offset, &sidechain_extra_nonce_buf](int offset) -> uint8_t { - if (v > 1) { - const uint32_t k = static_cast(offset - sidechain_extra_nonce_offset); - if (k < EXTRA_NONCE_SIZE) { - return sidechain_extra_nonce_buf[k]; - } + keccak_custom([this, sidechain_extra_nonce_offset, &sidechain_extra_nonce_buf](int offset) -> uint8_t { + const uint32_t k = static_cast(offset - sidechain_extra_nonce_offset); + if (k < EXTRA_NONCE_SIZE) { + return sidechain_extra_nonce_buf[k]; } return m_sidechainHashBlob[offset]; }, static_cast(size), result.h, HASH_SIZE); @@ -989,9 +978,7 @@ hash BlockTemplate::calc_sidechain_hash(uint32_t sidechain_extra_nonce) const const int inlen = static_cast(size - N); memcpy(buf, m_sidechainHashBlob.data() + N, size - N); - if (v > 1) { - memcpy(buf + sidechain_extra_nonce_offset - N, sidechain_extra_nonce_buf, EXTRA_NONCE_SIZE); - } + memcpy(buf + sidechain_extra_nonce_offset - N, sidechain_extra_nonce_buf, EXTRA_NONCE_SIZE); uint64_t st[25]; memcpy(st, m_sidechainHashKeccakState, sizeof(st)); diff --git a/src/block_template.h b/src/block_template.h index bb90336..bffef0e 100644 --- a/src/block_template.h +++ b/src/block_template.h @@ -39,7 +39,7 @@ public: BlockTemplate(const BlockTemplate& b); BlockTemplate& operator=(const BlockTemplate& b); - void update(const MinerData& data, const Mempool& mempool, Wallet* miner_wallet); + void update(const MinerData& data, const Mempool& mempool, const Wallet* miner_wallet); uint64_t last_updated() const { return m_lastUpdated.load(); } bool get_difficulties(const uint32_t template_id, uint64_t& height, uint64_t& sidechain_height, difficulty_type& mainchain_difficulty, difficulty_type& sidechain_difficulty) const; diff --git a/src/miner.cpp b/src/miner.cpp index 6f92376..00a8baa 100644 --- a/src/miner.cpp +++ b/src/miner.cpp @@ -106,7 +106,7 @@ void Miner::on_block(const BlockTemplate& block) Job& j = m_job[next_index]; hash seed; - const uint32_t extra_nonce = PoolBlock::signal_v2_readiness(static_cast(m_rng() >> 32)); + const uint32_t extra_nonce = static_cast(m_rng() >> 32); j.m_blobSize = block.get_hashing_blob(extra_nonce, j.m_blob, j.m_height, j.m_sidechainHeight, j.m_diff, j.m_sidechainDiff, seed, j.m_nonceOffset, j.m_templateId); const uint64_t next_full_nonce = (static_cast(extra_nonce) << 32) | std::numeric_limits::max(); diff --git a/src/pool_block.cpp b/src/pool_block.cpp index 4e04a42..8d62611 100644 --- a/src/pool_block.cpp +++ b/src/pool_block.cpp @@ -220,18 +220,9 @@ std::vector PoolBlock::serialize_sidechain_data() const const hash& spend = m_minerWallet.spend_public_key(); const hash& view = m_minerWallet.view_public_key(); - const int sidechain_version = get_sidechain_version(); - data.insert(data.end(), spend.h, spend.h + HASH_SIZE); data.insert(data.end(), view.h, view.h + HASH_SIZE); - - if (sidechain_version > 1) { - data.insert(data.end(), m_txkeySecSeed.h, m_txkeySecSeed.h + HASH_SIZE); - } - else { - data.insert(data.end(), m_txkeySec.h, m_txkeySec.h + HASH_SIZE); - } - + data.insert(data.end(), m_txkeySecSeed.h, m_txkeySecSeed.h + HASH_SIZE); data.insert(data.end(), m_parent.h, m_parent.h + HASH_SIZE); writeVarint(m_uncles.size(), data); @@ -248,10 +239,8 @@ std::vector PoolBlock::serialize_sidechain_data() const writeVarint(m_cumulativeDifficulty.lo, data); writeVarint(m_cumulativeDifficulty.hi, data); - if (sidechain_version > 1) { - const uint8_t* p = reinterpret_cast(m_sidechainExtraBuf); - data.insert(data.end(), p, p + sizeof(m_sidechainExtraBuf)); - } + const uint8_t* p = reinterpret_cast(m_sidechainExtraBuf); + data.insert(data.end(), p, p + sizeof(m_sidechainExtraBuf)); #if POOL_BLOCK_DEBUG if (!m_sideChainDataDebug.empty() && (data != m_sideChainDataDebug)) { @@ -386,24 +375,6 @@ uint64_t PoolBlock::get_payout(const Wallet& w) const return 0; } -static constexpr uint64_t VERSION2_MAINNET_TIMESTAMP = 1679173200U; // 2023-03-18 21:00 UTC -static constexpr uint64_t VERSION2_TESTNET_TIMESTAMP = 1674507600U; // 2023-01-23 21:00 UTC - -uint32_t PoolBlock::signal_v2_readiness(uint32_t extra_nonce) -{ - const uint64_t ts = (SideChain::network_type() == NetworkType::Mainnet) ? VERSION2_MAINNET_TIMESTAMP : VERSION2_TESTNET_TIMESTAMP; - if (time(nullptr) < static_cast(ts)) { - return (extra_nonce & 0x007FFFFFUL) | 0xFF000000UL; - } - return extra_nonce; -} - -int PoolBlock::get_sidechain_version() const -{ - const uint64_t ts = (SideChain::network_type() == NetworkType::Mainnet) ? VERSION2_MAINNET_TIMESTAMP : VERSION2_TESTNET_TIMESTAMP; - return (m_timestamp >= ts) ? 2 : 1; -} - hash PoolBlock::calculate_tx_key_seed() const { const char domain[] = "tx_key_seed"; diff --git a/src/pool_block.h b/src/pool_block.h index b5c1327..f8dd65f 100644 --- a/src/pool_block.h +++ b/src/pool_block.h @@ -148,12 +148,6 @@ struct PoolBlock // but P2Pool can switch to using only TXOUT_TO_TAGGED_KEY for miner payouts starting from v15 FORCEINLINE uint8_t get_tx_type() const { return (m_majorVersion < HARDFORK_VIEW_TAGS_VERSION) ? TXOUT_TO_KEY : TXOUT_TO_TAGGED_KEY; } - // Signal hardfork readiness (only before the v2 hardfork) - // TODO: remove this code after hardfork - static uint32_t signal_v2_readiness(uint32_t extra_nonce); - - int get_sidechain_version() const; - typedef std::array full_id; FORCEINLINE full_id get_full_id() const diff --git a/src/pool_block_parser.inl b/src/pool_block_parser.inl index aea6e4a..f91d39a 100644 --- a/src/pool_block_parser.inl +++ b/src/pool_block_parser.inl @@ -260,27 +260,10 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si READ_BUF(m_txkeySecSeed.h, HASH_SIZE); - const int sidechain_version = get_sidechain_version(); - - if (sidechain_version > 1) { - hash pub; - get_tx_keys(pub, m_txkeySec, m_txkeySecSeed, m_prevId); - if (pub != m_txkeyPub) { - return __LINE__; - } - } - else { - // Both values are the same before v2 - m_txkeySec = m_txkeySecSeed; - - // Enforce deterministic tx keys starting from v15 - if (m_majorVersion >= HARDFORK_VIEW_TAGS_VERSION) { - hash pub, sec; - get_tx_keys(pub, sec, spend_pub_key, m_prevId); - if ((pub != m_txkeyPub) || (sec != m_txkeySec)) { - return __LINE__; - } - } + hash pub; + get_tx_keys(pub, m_txkeySec, m_txkeySecSeed, m_prevId); + if (pub != m_txkeyPub) { + return __LINE__; } if (!check_keys(m_txkeyPub, m_txkeySec)) { @@ -329,9 +312,7 @@ int PoolBlock::deserialize(const uint8_t* data, size_t size, const SideChain& si READ_VARINT(m_cumulativeDifficulty.lo); READ_VARINT(m_cumulativeDifficulty.hi); - if (sidechain_version > 1) { - READ_BUF(m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf)); - } + READ_BUF(m_sidechainExtraBuf, sizeof(m_sidechainExtraBuf)); #undef READ_BYTE #undef EXPECT_BYTE diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 6ec6f6e..06f6e35 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -217,7 +217,6 @@ SideChain::~SideChain() void SideChain::fill_sidechain_data(PoolBlock& block, std::vector& shares) const { - const int sidechain_version = block.get_sidechain_version(); block.m_uncles.clear(); ReadLock lock(m_sidechainLock); @@ -229,20 +228,15 @@ void SideChain::fill_sidechain_data(PoolBlock& block, std::vector& s block.m_sidechainHeight = 0; block.m_difficulty = m_minDifficulty; block.m_cumulativeDifficulty = m_minDifficulty; - - if (sidechain_version > 1) { - block.m_txkeySecSeed = m_consensusHash; - get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId); - } + block.m_txkeySecSeed = m_consensusHash; + get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId); get_shares(&block, shares); return; } - if (sidechain_version > 1) { - block.m_txkeySecSeed = (block.m_prevId == tip->m_prevId) ? tip->m_txkeySecSeed : tip->calculate_tx_key_seed(); - get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId); - } + block.m_txkeySecSeed = (block.m_prevId == tip->m_prevId) ? tip->m_txkeySecSeed : tip->calculate_tx_key_seed(); + get_tx_keys(block.m_txkeyPub, block.m_txkeySec, block.m_txkeySecSeed, block.m_prevId); block.m_parent = tip->m_sidechainId; block.m_sidechainHeight = tip->m_sidechainHeight + 1; @@ -368,8 +362,7 @@ bool SideChain::get_shares(const PoolBlock* tip, std::vector& shares // Dynamic PPLNS window starting from v2 // Limit PPLNS weight to 2x of the Monero difficulty (max 2 blocks per PPLNS window on average) - const int sidechain_version = tip->get_sidechain_version(); - const difficulty_type max_pplns_weight = (sidechain_version > 1) ? (mainchain_diff * 2) : diff_max; + const difficulty_type max_pplns_weight = mainchain_diff * 2; difficulty_type pplns_weight; unordered_set shares_set; @@ -454,7 +447,7 @@ bool SideChain::get_shares(const PoolBlock* tip, std::vector& shares const uint64_t n = shares.size(); // Shuffle shares - if ((sidechain_version > 1) && (n > 1)) { + if (n > 1) { hash h; keccak(tip->m_txkeySecSeed.h, HASH_SIZE, h.h); @@ -1323,15 +1316,13 @@ void SideChain::verify_loop(PoolBlock* block) void SideChain::verify(PoolBlock* block) { - const int sidechain_version = block->get_sidechain_version(); - // Genesis block if (block->m_sidechainHeight == 0) { if (!block->m_parent.empty() || !block->m_uncles.empty() || (block->m_difficulty != m_minDifficulty) || (block->m_cumulativeDifficulty != m_minDifficulty) || - ((sidechain_version > 1) && (block->m_txkeySecSeed != m_consensusHash))) + (block->m_txkeySecSeed != m_consensusHash)) { block->m_invalid = true; } @@ -1377,15 +1368,13 @@ void SideChain::verify(PoolBlock* block) return; } - if (sidechain_version > 1) { - // Check m_txkeySecSeed - const hash h = (block->m_prevId == parent->m_prevId) ? parent->m_txkeySecSeed : parent->calculate_tx_key_seed(); - if (block->m_txkeySecSeed != h) { - LOGWARN(3, "block " << block->m_sidechainId << " has invalid tx key seed: expected " << h << ", got " << block->m_txkeySecSeed); - block->m_verified = true; - block->m_invalid = true; - return; - } + // Check m_txkeySecSeed + const hash h = (block->m_prevId == parent->m_prevId) ? parent->m_txkeySecSeed : parent->calculate_tx_key_seed(); + if (block->m_txkeySecSeed != h) { + LOGWARN(3, "block " << block->m_sidechainId << " has invalid tx key seed: expected " << h << ", got " << block->m_txkeySecSeed); + block->m_verified = true; + block->m_invalid = true; + return; } const uint64_t expectedHeight = parent->m_sidechainHeight + 1; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index 9650cc3..9fe8f90 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -66,7 +66,7 @@ StratumServer::StratumServer(p2pool* pool) uv_mutex_init_checked(&m_rngLock); uv_rwlock_init_checked(&m_hashrateDataLock); - m_extraNonce = PoolBlock::signal_v2_readiness(get_random32()); + m_extraNonce = get_random32(); m_submittedSharesPool.resize(10); for (size_t i = 0; i < m_submittedSharesPool.size(); ++i) { @@ -109,7 +109,7 @@ void StratumServer::on_block(const BlockTemplate& block) return; } - const uint32_t extra_nonce_start = PoolBlock::signal_v2_readiness(get_random32()); + const uint32_t extra_nonce_start = get_random32(); m_extraNonce.exchange(extra_nonce_start + num_connections); BlobsData* blobs_data = new BlobsData{};