1
0
Fork 0
forked from lthn/blockchain

inital wk2 integration code

This commit is contained in:
crypro.zoidberg 2019-01-11 06:15:34 +03:00
parent 23b64c1a66
commit 98a8608046
8 changed files with 85 additions and 2 deletions

View file

@ -220,7 +220,7 @@ namespace crypto
//------------------------------------------------------------------
inline
bool get_wild_keccak2(const std::string& bd, crypto::hash& res, uint64_t height, const std::vector<crypto::hash>& scratchpad, uint64_t sz)
bool get_wild_keccak2(const std::string& bd, crypto::hash& res, const std::vector<crypto::hash>& scratchpad, uint64_t sz)
{
crypto::wild_keccak2_dbl<crypto::regular_f>(reinterpret_cast<const uint8_t*>(bd.data()), bd.size(), reinterpret_cast<uint8_t*>(&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<crypto::hash>& 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<crypto::hash>& scratchpad, uint64_t sz)
{

View file

@ -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<crypto::hash> 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<crypto::hash>& 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<transaction>& tx_ptr, uint64_t min_allowed_block_height /* = 0 */) const
{
tx_ptr.reset(new transaction());

View file

@ -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<crypto::key_image>& 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<transaction>& tx_ptr, uint64_t min_allowed_block_height = 0) const;
bool get_seed_for_scratchpad(uint64_t height, std::vector<crypto::hash>& 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;

View file

@ -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)

View file

@ -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)

View file

@ -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);

View file

@ -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<crypto::hash>& 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();
}
}

View file

@ -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<crypto::hash>& seed, uint64_t height);
crypto::hash get_pow_hash(const blobdata& bd, uint64_t height);
uint64_t size();
private:
std::vector<crypto::hash> m_scratchpad;
};
}