From 98a8608046abe7c98baab5ba8597a1b48bf6be5e Mon Sep 17 00:00:00 2001 From: "crypro.zoidberg" Date: Fri, 11 Jan 2019 06:15:34 +0300 Subject: [PATCH] inital wk2 integration code --- src/crypto/wild_keccak.h | 8 +++++- src/currency_core/blockchain_storage.cpp | 14 +++++++++ src/currency_core/blockchain_storage.h | 3 ++ src/currency_core/currency_config.h | 1 + src/currency_core/currency_format_utils.cpp | 7 ++++- src/currency_core/currency_format_utils.h | 1 + src/currency_core/scratchpad_helper.cpp | 32 +++++++++++++++++++++ src/currency_core/scratchpad_helper.h | 21 ++++++++++++++ 8 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/currency_core/scratchpad_helper.cpp create mode 100644 src/currency_core/scratchpad_helper.h diff --git a/src/crypto/wild_keccak.h b/src/crypto/wild_keccak.h index d1fb5acf..9ec0ecb1 100644 --- a/src/crypto/wild_keccak.h +++ b/src/crypto/wild_keccak.h @@ -220,7 +220,7 @@ namespace crypto //------------------------------------------------------------------ inline - bool get_wild_keccak2(const std::string& bd, crypto::hash& res, uint64_t height, const std::vector& scratchpad, uint64_t sz) + bool get_wild_keccak2(const std::string& bd, crypto::hash& res, const std::vector& scratchpad, uint64_t sz) { crypto::wild_keccak2_dbl(reinterpret_cast(bd.data()), bd.size(), reinterpret_cast(&res), sizeof(res), [&](crypto::state_t_m& st) { @@ -249,6 +249,12 @@ namespace crypto return true; } //------------------------------------------------------------------ + inline + bool get_wild_keccak2(const std::string& bd, crypto::hash& res, const std::vector& scratchpad) + { + return get_wild_keccak2(bd, res, scratchpad, scratchpad.size()); + } + //------------------------------------------------------------------ inline bool get_wild_keccak(const std::string& bd, crypto::hash& res, uint64_t height, const std::vector& scratchpad, uint64_t sz) { diff --git a/src/currency_core/blockchain_storage.cpp b/src/currency_core/blockchain_storage.cpp index 40032deb..42840ab4 100644 --- a/src/currency_core/blockchain_storage.cpp +++ b/src/currency_core/blockchain_storage.cpp @@ -4541,6 +4541,12 @@ void blockchain_storage::on_block_added(const block_extended_info& bei, const cr { update_next_comulative_size_limit(); m_timestamps_median_cache.clear(); + if (get_scratchpad_size_by_height(bei.height) != m_scratchpad.size()) + { + std::vector seed; + + m_scratchpad.update(bei.height); + } m_tx_pool.on_blockchain_inc(bei.height, id); TIME_MEASURE_START_PD(raise_block_core_event); @@ -5285,6 +5291,14 @@ bool blockchain_storage::validate_alt_block_ms_input(const transaction& input_tx return false; } //------------------------------------------------------------------ +bool blockchain_storage::get_seed_for_scratchpad(uint64_t height, std::vector& seed) +{ + CRITICAL_REGION_LOCAL(m_read_lock); + CHECK_AND_ASSERT_THROW_MES(m_db_blocks.size() > height, "Internal error: m_db_blocks.size()=" << m_db_blocks.size() << " > height=" << height); + uint64_t last_upd_h = get_scratchpad_last_update_rebuild_height(height); + +} +//------------------------------------------------------------------ bool blockchain_storage::get_transaction_from_pool_or_db(const crypto::hash& tx_id, std::shared_ptr& tx_ptr, uint64_t min_allowed_block_height /* = 0 */) const { tx_ptr.reset(new transaction()); diff --git a/src/currency_core/blockchain_storage.h b/src/currency_core/blockchain_storage.h index 93b32816..aa0e39e9 100644 --- a/src/currency_core/blockchain_storage.h +++ b/src/currency_core/blockchain_storage.h @@ -39,6 +39,7 @@ #include "dispatch_core_events.h" #include "bc_attachments_service_manager.h" #include "common/median_db_cache.h" +#include "scratchpad_helper.h" MARK_AS_POD_C11(crypto::key_image); @@ -508,6 +509,7 @@ namespace currency mutable uint64_t m_current_fee_median; mutable uint64_t m_current_fee_median_effective_index; bool m_is_reorganize_in_process; + scratchpad_keeper m_scratchpad; bool init_tx_fee_median(); @@ -535,6 +537,7 @@ namespace currency bool validate_alt_block_txs(const block& b, const crypto::hash& id, std::set& collected_keyimages, alt_block_extended_info& abei, const alt_chain_type& alt_chain, uint64_t split_height, uint64_t& ki_lookup_time_total) const; bool update_alt_out_indexes_for_tx_in_block(const transaction& tx, alt_block_extended_info& abei)const; bool get_transaction_from_pool_or_db(const crypto::hash& tx_id, std::shared_ptr& tx_ptr, uint64_t min_allowed_block_height = 0) const; + bool get_seed_for_scratchpad(uint64_t height, std::vector& seed); bool prevalidate_miner_transaction(const block& b, uint64_t height, bool pos)const; bool validate_transaction(const block& b, uint64_t height, const transaction& tx)const; diff --git a/src/currency_core/currency_config.h b/src/currency_core/currency_config.h index b1d927ae..1acf67e4 100644 --- a/src/currency_core/currency_config.h +++ b/src/currency_core/currency_config.h @@ -48,6 +48,7 @@ #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_REBUILD_INTERVAL 720 //once a day if block goes once in 2 minute #define TX_DEFAULT_FEE ((uint64_t)100000) // pow(10, 5) #define TX_MINIMUM_FEE ((uint64_t)100000) // pow(10, 5) diff --git a/src/currency_core/currency_format_utils.cpp b/src/currency_core/currency_format_utils.cpp index b47d91f7..8958c55a 100644 --- a/src/currency_core/currency_format_utils.cpp +++ b/src/currency_core/currency_format_utils.cpp @@ -2580,10 +2580,15 @@ namespace currency return reward; } //----------------------------------------------------------------------------------------------- + uint64_t get_scratchpad_last_update_rebuild_height(uint64_t h) + { + return h - (h%CURRENCY_SCRATCHPAD_REBUILD_INTERVAL); + } + //----------------------------------------------------------------------------------------------- uint64_t get_scratchpad_size_by_height(uint64_t h) { //let's have ~256MB/year if block interval is 2 minutes - return CURRENCY_SCRATCHPAD_BASE_SIZE + h*32; + return CURRENCY_SCRATCHPAD_BASE_SIZE + get_scratchpad_last_update_rebuild_height(h)*32; } //----------------------------------------------------------------------------------------------- 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/currency_format_utils.h b/src/currency_core/currency_format_utils.h index 2abc8cf6..89802c58 100644 --- a/src/currency_core/currency_format_utils.h +++ b/src/currency_core/currency_format_utils.h @@ -414,6 +414,7 @@ namespace currency size_t get_max_tx_size(); 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); uint64_t get_base_block_reward(bool is_pos, uint64_t already_generated_coins, uint64_t height); + uint64_t get_scratchpad_last_update_rebuild_height(uint64_t h); uint64_t get_scratchpad_size_by_height(uint64_t h); bool is_payment_id_size_ok(const std::string& payment_id); std::string get_account_address_as_str(const account_public_address& addr); diff --git a/src/currency_core/scratchpad_helper.cpp b/src/currency_core/scratchpad_helper.cpp new file mode 100644 index 00000000..a74400ad --- /dev/null +++ b/src/currency_core/scratchpad_helper.cpp @@ -0,0 +1,32 @@ +// Copyright (c) 2018-2019 Zano Project + +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + + +#include "scratchpad_helper.h" +#include "currency_format_utils.h" + + + +namespace currency +{ + + bool scratchpad_keeper::update(const std::vector& seed, uint64_t height) + { + return crypto::generate_scratchpad(seed, m_scratchpad, get_scratchpad_size_by_height(height)); + } + crypto::hash scratchpad_keeper::get_pow_hash(const blobdata& bd, uint64_t height) + { + CHECK_AND_ASSERT_THROW_MES(get_scratchpad_size_by_height(height) == this->size(), "Fatal error on hash calculation: scratchpad_size=" << m_scratchpad.size() << " at height=" << height); + crypto::hash res_hash = null_hash; + bool res = get_wild_keccak2(bd, res_hash, m_scratchpad); + CHECK_AND_ASSERT_THROW_MES(res, "Fatal error on hash calculation: scratchpad_size=" << m_scratchpad.size()); + return res_hash; + } + uint64_t scratchpad_keeper::size() + { + return m_scratchpad.size(); + } + +} \ No newline at end of file diff --git a/src/currency_core/scratchpad_helper.h b/src/currency_core/scratchpad_helper.h new file mode 100644 index 00000000..949f10d1 --- /dev/null +++ b/src/currency_core/scratchpad_helper.h @@ -0,0 +1,21 @@ +// Copyright (c) 2014-2018 Zano Project +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + + +#include "crypto/wild_keccak.h" + + +namespace currency +{ + class scratchpad_keeper + { + public: + bool update(const std::vector& seed, uint64_t height); + crypto::hash get_pow_hash(const blobdata& bd, uint64_t height); + uint64_t size(); + private: + std::vector m_scratchpad; + }; + +} \ No newline at end of file