From 9d2d90cf836a33a8f5c5266576788c9d8d3cba2b Mon Sep 17 00:00:00 2001 From: "crypro.zoidberg" Date: Sun, 24 Feb 2019 03:35:38 +0100 Subject: [PATCH] light hash integrated into core --- src/currency_core/blockchain_storage.cpp | 7 ++-- src/currency_core/blockchain_storage.h | 3 +- src/currency_core/currency_config.h | 3 +- src/currency_core/currency_format_utils.cpp | 4 +-- src/currency_core/miner.cpp | 2 +- src/currency_core/miner.h | 2 +- src/currency_core/scratchpad_helper.cpp | 37 +++++++++++++++++---- src/currency_core/scratchpad_helper.h | 32 ++++++++++++------ 8 files changed, 63 insertions(+), 27 deletions(-) diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index dfe9c6a4..407a2f42 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -100,7 +100,8 @@ blockchain_storage::blockchain_storage(tx_memory_pool& tx_pool) :m_db(std::share m_current_fee_median_effective_index(0), m_is_reorganize_in_process(false), m_deinit_is_done(false), - m_current_scratchpad_seed(currency::null_hash) + m_current_scratchpad_seed(currency::null_hash), + m_current_scratchpad_seed_height(0) { @@ -291,6 +292,7 @@ bool blockchain_storage::init(const std::string& config_folder, const boost::pro initialize_db_solo_options_values(); get_seed_for_scratchpad(m_db_blocks.size(), m_current_scratchpad_seed); + m_current_scratchpad_seed_height = m_db_blocks.size(); m_services_mgr.init(config_folder, vm); @@ -4582,9 +4584,10 @@ void blockchain_storage::on_block_added(const block_extended_info& bei, const cr //------------------------------------------------------------------ bool blockchain_storage::check_scratchpad() { - if (get_scratchpad_size_for_height(m_db_blocks.size()) != m_scratchpad.size()) + if(get_scratchpad_last_update_rebuild_height(m_db_blocks.size()) != m_current_scratchpad_seed_height) { get_seed_for_scratchpad(m_db_blocks.size(), m_current_scratchpad_seed); + m_current_scratchpad_seed_height = get_scratchpad_last_update_rebuild_height(m_db_blocks.size()); } return true; } diff --git a/src/currency_core/blockchain_storage.h b/src/currency_core/blockchain_storage.h index 31ce5b67..5312a4c2 100644 --- a/src/currency_core/blockchain_storage.h +++ b/src/currency_core/blockchain_storage.h @@ -511,8 +511,9 @@ namespace currency mutable uint64_t m_current_fee_median; mutable uint64_t m_current_fee_median_effective_index; bool m_is_reorganize_in_process; - mutable scratchpad_keeper m_scratchpad; + mutable scratchpad_light_pool m_scratchpad; //TODO: optimization for using full scratchpad in mainchain crypto::hash m_current_scratchpad_seed; + uint64_t m_current_scratchpad_seed_height; mutable std::atomic m_deinit_is_done; diff --git a/src/currency_core/currency_config.h b/src/currency_core/currency_config.h index ccae4dae..5442de93 100644 --- a/src/currency_core/currency_config.h +++ b/src/currency_core/currency_config.h @@ -47,8 +47,7 @@ #define BASE_REWARD_DUST_THRESHOLD ((uint64_t)1000000) // pow(10, 6) - change this will cause hard-fork! #define DEFAULT_DUST_THRESHOLD ((uint64_t)0)//((uint64_t)100000) // pow(10, 5) -//#define CURRENCY_SCRATCHPAD_BASE_SIZE 16777216 //count in crypto::hash, to get size in bytes x32 -#define CURRENCY_SCRATCHPAD_BASE_SIZE 167 //count in crypto::hash, to get size in bytes x32 +#define CURRENCY_SCRATCHPAD_BASE_SIZE 16777210 //count in crypto::hash, to get size in bytes x32 #define CURRENCY_SCRATCHPAD_REBUILD_INTERVAL 720 //once a day if block goes once in 2 minute #define CURRENCY_SCRATCHPAD_BASE_INDEX_ID_OFFSET 20 //offset down from last rebuild height to block id, that used for indexing seed blocks in CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW #define CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW 700 //window for addressing seed block ids diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index 673092ca..87a9ecc1 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -2412,8 +2412,8 @@ namespace currency //----------------------------------------------------------------------------------------------- uint64_t get_scratchpad_size_for_height(uint64_t h) { - //let's have ~256MB/year if block interval is 2 minutes - return CURRENCY_SCRATCHPAD_BASE_SIZE + get_scratchpad_last_update_rebuild_height(h)*32; + //let's have ~250MB/year if block interval is 2 minutes + return CURRENCY_SCRATCHPAD_BASE_SIZE + get_scratchpad_last_update_rebuild_height(h)*30; } //----------------------------------------------------------------------------------------------- bool get_block_reward(bool is_pos, size_t median_size, size_t current_block_size, uint64_t already_generated_coins, uint64_t &reward, uint64_t height) diff --git a/src/currency_core/miner.cpp b/src/currency_core/miner.cpp index 56e4b53f..1a451d83 100644 --- a/src/currency_core/miner.cpp +++ b/src/currency_core/miner.cpp @@ -345,7 +345,7 @@ namespace currency } b.nonce = nonce; access_nonce_in_block_blob(local_blob_data) = b.nonce; - crypto::hash h = m_scratchpad.get_pow_hash(local_blob_data, local_height, local_seed); + crypto::hash h = m_scratchpad.get_pow_hash_from_blob(local_blob_data, local_height, local_seed); if(check_hash(h, local_diff)) { diff --git a/src/currency_core/miner.h b/src/currency_core/miner.h index 5e8727e9..21799172 100644 --- a/src/currency_core/miner.h +++ b/src/currency_core/miner.h @@ -73,7 +73,7 @@ namespace currency { nonce_ref = bl.nonce; - crypto::hash h = sk.get_pow_hash(bd, height, seed); + crypto::hash h = sk.get_pow_hash_from_blob(bd, height, seed); if(check_hash(h, diffic)) { LOG_PRINT_L0("Found nonce for block: " << get_block_hash(bl) << "[" << height << "]: PoW:" << h << "(diff:" << diffic << "), ts: " << bl.timestamp); diff --git a/src/currency_core/scratchpad_helper.cpp b/src/currency_core/scratchpad_helper.cpp index b220c8e3..d93c5e3f 100644 --- a/src/currency_core/scratchpad_helper.cpp +++ b/src/currency_core/scratchpad_helper.cpp @@ -15,6 +15,7 @@ namespace currency { } + //------------------------------------------------------------------------------------ bool scratchpad_keeper::generate(const crypto::hash& scr_seed, uint64_t height) { bool r = false; @@ -25,7 +26,8 @@ namespace currency CRITICAL_REGION_END(); return r; } - crypto::hash scratchpad_keeper::get_pow_hash(const blobdata& bd, uint64_t height, const crypto::hash& scr_seed) + //------------------------------------------------------------------------------------ + crypto::hash scratchpad_keeper::get_pow_hash_from_blob(const blobdata& bd, uint64_t height, const crypto::hash& scr_seed) { CRITICAL_REGION_LOCAL(m_lock); crypto::hash res_hash = null_hash; @@ -41,13 +43,34 @@ namespace currency CHECK_AND_ASSERT_THROW_MES(res, "Fatal error on hash calculation: scratchpad_size=" << m_scratchpad.size()); return res_hash; } - crypto::hash scratchpad_keeper::get_pow_hash(const block& b, const crypto::hash& scr_seed) - { - blobdata bl = get_block_hashing_blob(b); - return get_pow_hash(bl, get_block_height(b), scr_seed); - } + //------------------------------------------------------------------------------------ + + //------------------------------------------------------------------------------------ uint64_t scratchpad_keeper::size() { return m_scratchpad.size(); } -} \ No newline at end of file + //------------------------------------------------------------------------------------ + crypto::hash scratchpad_light_pool::get_pow_hash_from_blob(const blobdata& bd, uint64_t height, const crypto::hash& seed) + { + CRITICAL_REGION_LOCAL(m_lock); + std::shared_ptr> pscr_light; + if (!m_scratchpad_pools.get(seed, pscr_light)) + { + LOG_PRINT_MAGENTA("Generating scratchpad light for " << seed << "["<< height <<"]", LOG_LEVEL_0); + pscr_light.reset(new std::vector()); + bool r = crypto::generate_scratchpad_light(seed, *pscr_light, currency::get_scratchpad_size_for_height(height)); + CHECK_AND_ASSERT_THROW_MES(r, "Failed to generate_scratchpad_light"); + m_scratchpad_pools.set(seed, pscr_light); + LOG_PRINT_MAGENTA("Generated ok", LOG_LEVEL_0); + } + CHECK_AND_ASSERT_THROW_MES(pscr_light->size() == currency::get_scratchpad_size_for_height(height), + "Wrong size of cached scratchpad = " << pscr_light->size() << ", expected " << currency::get_scratchpad_size_for_height(height) << " for height " << height); + crypto::hash res = currency::null_hash; + bool r = crypto::get_wild_keccak_light(bd, res, *pscr_light); + CHECK_AND_ASSERT_THROW_MES(r, "Failed to get_wild_keccak_light"); + return res; + } + //------------------------------------------------------------------------------------ +} + diff --git a/src/currency_core/scratchpad_helper.h b/src/currency_core/scratchpad_helper.h index 222ebc28..dce6b4e9 100644 --- a/src/currency_core/scratchpad_helper.h +++ b/src/currency_core/scratchpad_helper.h @@ -6,16 +6,28 @@ #include "crypto/wild_keccak.h" #include "currency_protocol/blobdatatype.h" #include "currency_core/currency_basic.h" +#include "cache_helper.h" namespace currency { - class scratchpad_keeper + template + class scratchpad_keeper_base + { + public: + crypto::hash get_pow_hash(const block& b, const crypto::hash& scr_seed) + { + blobdata bl = get_block_hashing_blob(b); + return static_cast(this)->get_pow_hash_from_blob(bl, get_block_height(b), scr_seed); + } + }; + + + class scratchpad_keeper: public scratchpad_keeper_base { public: scratchpad_keeper(); bool generate(const crypto::hash& seed, uint64_t height); - crypto::hash get_pow_hash(const blobdata& bd, uint64_t height, const crypto::hash& seed); - crypto::hash get_pow_hash(const block& b, const crypto::hash& seed); + crypto::hash get_pow_hash_from_blob(const blobdata& bd, uint64_t height, const crypto::hash& seed); uint64_t size(); private: scratchpad_keeper(const scratchpad_keeper&) {} @@ -25,17 +37,15 @@ namespace currency }; - class scratchpad_light + class scratchpad_light_pool : public scratchpad_keeper_base { - scratchpad_light(); - bool generate(const crypto::hash& seed, uint64_t height); - crypto::hash get_pow_hash(const blobdata& bd, uint64_t height, const crypto::hash& seed); - crypto::hash get_pow_hash(const block& b, const crypto::hash& seed); - uint64_t size(); + public: + scratchpad_light_pool() {} + crypto::hash get_pow_hash_from_blob(const blobdata& bd, uint64_t height, const crypto::hash& seed); private: - std::vector m_scratchpad_light; + //map of seed to + epee::misc_utils::cache_base>, 4> m_scratchpad_pools; std::recursive_mutex m_lock; - }; } \ No newline at end of file