From 5b46a0df6dc95aeec800a7a9aa462bd7615987e2 Mon Sep 17 00:00:00 2001 From: wowario Date: Sat, 1 Aug 2020 09:40:24 +0300 Subject: [PATCH] add generated coins and height to unlock calculation --- src/cryptonote_core/blockchain.cpp | 15 +++++++++------ src/cryptonote_core/cryptonote_tx_utils.cpp | 5 ++++- src/wallet/wallet2.cpp | 8 ++++---- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 3cbaa251d..bfb80c850 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -1386,22 +1386,25 @@ bool Blockchain::prevalidate_miner_transaction(const block& b, uint64_t height, MDEBUG("Miner tx hash: " << get_transaction_hash(b.miner_tx)); // Dynamic unlock time from HF 16 - // To calculate unlock window, use first 3 characters of tx public key (convert to decimal), multiply by 2, plus 288 - // Unlock minimum 1 day, maximum ~1 month + // To calculate unlock window, use first 3 characters of tx public key (convert hex to decimal), + // multiply by 2, plus last 3 numbers of already_generated_coins, plus last numbers of block height, plus 288 + // Unlock minimum 1 day (288 blocks), maximum is ~33 days ((4095*2)+999+99+288 = 9576 blocks) // unlock time = unlock_window + height if (hf_version >= HF_VERSION_DYNAMIC_UNLOCK) { crypto::public_key tx_pub_key = cryptonote::get_tx_pub_key_from_extra(b.miner_tx); std::string hex_str = epee::string_tools::pod_to_hex(tx_pub_key).substr(0, 3); - uint64_t unlock_window = (std::stol(hex_str,nullptr,16) * 2) + 288; + uint64_t pub_key_num = std::stol(hex_str,nullptr,16) * 2; + uint64_t coin_gen_num = m_db->get_block_already_generated_coins(height - 1) % 1000; + uint64_t h_num = height % 100; + uint64_t unlock_window = pub_key_num + coin_gen_num + h_num + 288; if (b.miner_tx.unlock_time != height + unlock_window) { - MWARNING("Coinbase transaction has the wrong unlock time=" << b.miner_tx.unlock_time << ", expected " << height + unlock_window << - ", unlock window based on first 3 characters of transaction public key, multiply by 2, plus 288."); + MWARNING("Coinbase transaction has the wrong unlock time=" << b.miner_tx.unlock_time << ", expected " << height + unlock_window); return false; } LOG_PRINT_L1("+++++ MINER TX UNLOCK TIME INFO\n" << "Height: " << height << ", Unlock window: " << unlock_window << ", Unlock time: " << b.miner_tx.unlock_time << "\nMiner TX:\t" << get_transaction_hash(b.miner_tx) << "\nTX Pub Key:\t" << tx_pub_key << - "\nHex: " << hex_str << ", Decimal: " << std::stol(hex_str,nullptr,16)); + "\nPub Key Num: " << pub_key_num << ", Coin Gen Num: " << coin_gen_num << ", Height Num: " << h_num); } else { CHECK_AND_ASSERT_MES(b.miner_tx.unlock_time == height + 60, false, "coinbase transaction transaction has the wrong unlock time=" << b.miner_tx.unlock_time << ", expected " << height + 60); diff --git a/src/cryptonote_core/cryptonote_tx_utils.cpp b/src/cryptonote_core/cryptonote_tx_utils.cpp index b0a5c2968..0020df89d 100644 --- a/src/cryptonote_core/cryptonote_tx_utils.cpp +++ b/src/cryptonote_core/cryptonote_tx_utils.cpp @@ -171,7 +171,10 @@ namespace cryptonote { crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx); std::string hex_str = epee::string_tools::pod_to_hex(tx_pub_key).substr(0, 3); - uint64_t unlock_window = (std::stol(hex_str,nullptr,16) * 2) + 288; + uint64_t pub_key_num = std::stol(hex_str,nullptr,16) * 2; + uint64_t coin_gen_num = already_generated_coins % 1000; + uint64_t h_num = height % 100; + uint64_t unlock_window = pub_key_num + coin_gen_num + h_num + 288; tx.unlock_time = height + unlock_window; } else { tx.unlock_time = height + 60; diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 6c8e01f1d..c084ea123 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -8097,8 +8097,8 @@ void wallet2::get_outs(std::vector> const uint64_t amount = td.is_rct() ? 0 : td.amount(); std::unordered_set seen_indices; // request more for rct in base recent (locked) coinbases are picked, since they're locked for longer - // unlock window max is (4095*2)+288 = 8478 (~29 days) - size_t requested_outputs_count = base_requested_outputs_count + (td.is_rct() ? 8478 - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE : 0); + // maximum unlock is ~33 days ((4095*2)+999+99+288 = 9576 blocks + size_t requested_outputs_count = base_requested_outputs_count + (td.is_rct() ? 9576 - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE : 0); size_t start = req.outputs.size(); bool use_histogram = amount != 0 || !has_rct_distribution; @@ -8414,8 +8414,8 @@ void wallet2::get_outs(std::vector> for(size_t idx: selected_transfers) { const transfer_details &td = m_transfers[idx]; - // unlock window max is (4095*2)+288 = 8478 (~29 days) - size_t requested_outputs_count = base_requested_outputs_count + (td.is_rct() ? 8478 - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE : 0); + // maximum unlock is ~33 days ((4095*2)+999+99+288 = 9576 blocks + size_t requested_outputs_count = base_requested_outputs_count + (td.is_rct() ? 9576 - CRYPTONOTE_DEFAULT_TX_SPENDABLE_AGE : 0); outs.push_back(std::vector()); outs.back().reserve(fake_outputs_count + 1); const rct::key mask = td.is_rct() ? rct::commit(td.amount(), td.m_mask) : rct::zeroCommit(td.amount());