forked from lthn/blockchain
light hash integrated into core
This commit is contained in:
parent
93831b9ab4
commit
9d2d90cf83
8 changed files with 63 additions and 27 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<bool> m_deinit_is_done;
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
//------------------------------------------------------------------------------------
|
||||
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<std::vector<crypto::hash>> 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<crypto::hash>());
|
||||
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;
|
||||
}
|
||||
//------------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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 t_parent>
|
||||
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<t_parent*>(this)->get_pow_hash_from_blob(bl, get_block_height(b), scr_seed);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class scratchpad_keeper: public scratchpad_keeper_base<scratchpad_keeper>
|
||||
{
|
||||
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_pool>
|
||||
{
|
||||
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<crypto::hash> m_scratchpad_light;
|
||||
//map of seed to
|
||||
epee::misc_utils::cache_base<false, crypto::hash, std::shared_ptr<std::vector<crypto::hash>>, 4> m_scratchpad_pools;
|
||||
std::recursive_mutex m_lock;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue