1
0
Fork 0
forked from lthn/blockchain

wk2 integration for main chain finished

This commit is contained in:
crypro.zoidberg 2019-01-11 23:46:29 +03:00
parent d3a0ec0f92
commit 020006b273
14 changed files with 83 additions and 35 deletions

View file

@ -104,6 +104,7 @@ struct response_schema
"\"time_started\": " << ce.time_started << ", "
"\"last_recv\": " << ce.last_recv << ", "
"\"last_send\": " << ce.last_send << ", "
"\"version\": " << ce.version << ", "
"\"is_income\": " << ce.is_income << "}";
if(rs.ns_rsp.v.connections_list.size()-1 != i)
ss << ",";

View file

@ -116,20 +116,20 @@ namespace crypto
st[0] ^= keccakf_rndc[round];
}
}
bool generate_scratchpad(const std::vector<crypto::hash>& seed_data, std::vector<crypto::hash>& result_data, uint64_t target_size)
{
//this is very basic implementation, not considered for production (possible to reduce memory by keeping only every x10 item and calc it instead of read)
//TODO: research safe way for scratchpad generation
if (target_size == 0 || seed_data.size() == 0)
return false;
result_data.resize(target_size);
result_data[0] = crypto::cn_fast_hash(&seed_data[0], sizeof(seed_data[0]) * seed_data.size());
//crypto::hash = get_transaction_hash()
for (size_t i = 1; i < target_size; i++)
{
result_data[i] = crypto::cn_fast_hash(&result_data[i - 1], sizeof(result_data[i - 1]));
}
return true;
}
}
bool generate_scratchpad(const std::vector<crypto::hash>& seed_data, std::vector<crypto::hash>& result_data, uint64_t target_size)
{
//this is very basic implementation, not considered for production (possible to reduce memory by keeping only every x10 item and calc it instead of read)
//TODO: research safe way for scratchpad generation
if (target_size == 0 || seed_data.size() == 0)
return false;
result_data.resize(target_size);
result_data[0] = crypto::cn_fast_hash(&seed_data[0], sizeof(seed_data[0]) * seed_data.size());
//crypto::hash = get_transaction_hash()
for (size_t i = 1; i < target_size; i++)
{
result_data[i] = crypto::cn_fast_hash(&result_data[i-1], sizeof(result_data[i - 1]));
}
return true;
}

View file

@ -264,7 +264,6 @@ bool blockchain_storage::init(const std::string& config_folder, const boost::pro
if (m_db_storage_minor_compatibility_version < 1)
need_reinit_medians = true;
}
if (need_reinit)
{
clear();
@ -4226,9 +4225,8 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
<< "expected: " << get_top_block_id());
return false;
}
#ifdef _DEBUG
uint64_t h = get_block_height(bl);
#endif// _DEBUG
if(!check_block_timestamp_main(bl))
{
@ -4273,7 +4271,12 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
}
else
{
proof_hash = get_block_longhash(bl);
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
//precaution(do we really need this check?)
check_scratchpad();
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
proof_hash = m_scratchpad.get_pow_hash(bl);
if (!check_hash(proof_hash, current_diffic))
{
@ -4541,23 +4544,29 @@ 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);
}
check_scratchpad();
m_tx_pool.on_blockchain_inc(bei.height, id);
TIME_MEASURE_START_PD(raise_block_core_event);
rise_core_event(CORE_EVENT_BLOCK_ADDED, void_struct());
TIME_MEASURE_FINISH_PD(raise_block_core_event);
}
//------------------------------------------------------------------
bool blockchain_storage::check_scratchpad()
{
if (get_scratchpad_size_for_height(m_db_blocks.size()) != m_scratchpad.size())
{
std::vector<crypto::hash> seed;
get_seed_for_scratchpad(m_db_blocks.size(), seed);
m_scratchpad.update(seed, m_db_blocks.size());
}
return true;
}
//------------------------------------------------------------------
void blockchain_storage::on_block_removed(const block_extended_info& bei)
{
m_tx_pool.on_blockchain_dec(m_db_blocks.size() - 1, get_top_block_id());
m_timestamps_median_cache.clear();
check_scratchpad();
LOG_PRINT_L2("block at height " << bei.height << " was removed from the blockchain");
}
//------------------------------------------------------------------
@ -5296,7 +5305,31 @@ bool blockchain_storage::get_seed_for_scratchpad(uint64_t height, std::vector<cr
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);
if (last_upd_h == 0)
{
crypto::hash genesis_seed = null_hash;
bool r = epee::string_tools::hex_to_pod(CURRENCY_SCRATCHPAD_GENESIS_SEED, genesis_seed);
CHECK_AND_ASSERT_THROW_MES(r, "Unable to parse CURRENCY_SCRATCHPAD_GENESIS_SEED " << CURRENCY_SCRATCHPAD_GENESIS_SEED);
LOG_PRINT_MAGENTA("[SCRATCHPAD] GENESIS SEED SELECTED: " << genesis_seed, LOG_LEVEL_1);
seed.push_back(genesis_seed);
return true;
}
uint64_t low_bound_window = 0;
CHECK_AND_ASSERT_THROW_MES(last_upd_h >= CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW, "Internal error: last_upd_h(" << last_upd_h <<") < CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW(" << CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW << ")");
low_bound_window = last_upd_h - CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW;
crypto::hash selector_id = get_block_hash(m_db_blocks[last_upd_h - CURRENCY_SCRATCHPAD_BASE_INDEX_ID_OFFSET]->bl);
const uint64_t* pselectors = (const uint64_t*)&selector_id;
std::stringstream ss;
for (size_t i = 0; i != 4; i++)
{
seed.push_back(get_block_hash(m_db_blocks[low_bound_window + pselectors[i] % CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW]->bl));
ss << "[" << std::setw(8) << std::hex << pselectors[i] << "->" << low_bound_window + pselectors[i] % CURRENCY_SCRATCHPAD_SEED_BLOCKS_WINDOW << "]"<<seed.back() << ENDL;
}
LOG_PRINT_MAGENTA("[SCRATCHPAD] SEED SELECTED: h = " << last_upd_h << ", selector: " << selector_id << ENDL << ss.str(), LOG_LEVEL_1);
return true;
}
//------------------------------------------------------------------
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

View file

@ -538,6 +538,7 @@ namespace currency
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 check_scratchpad();
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

@ -32,7 +32,6 @@ namespace currency
//members that supposed to be accessed only from one thread
std::list<block_context_info> m_needed_objects;
std::unordered_set<crypto::hash> m_requested_objects;
std::string m_remote_version;
std::atomic<uint32_t> m_callback_request_count; //in debug purpose: problem with double callback rise
};
@ -51,6 +50,7 @@ namespace currency
uint64_t m_remote_blockchain_height;
uint64_t m_last_response_height;
int64_t m_time_delta;
std::string m_remote_version;
private:
template<class t_core> friend class t_currency_protocol_handler;
uncopybale_currency_context m_priv;

View file

@ -47,8 +47,12 @@
#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 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_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
#define CURRENCY_SCRATCHPAD_GENESIS_SEED "4c98962ddce32c7763bb9326933a4692975ca29a76349ae7a139faa3430cc5ab"
#define TX_DEFAULT_FEE ((uint64_t)100000) // pow(10, 5)
#define TX_MINIMUM_FEE ((uint64_t)100000) // pow(10, 5)

View file

@ -2585,7 +2585,7 @@ namespace currency
return h - (h%CURRENCY_SCRATCHPAD_REBUILD_INTERVAL);
}
//-----------------------------------------------------------------------------------------------
uint64_t get_scratchpad_size_by_height(uint64_t h)
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;

View file

@ -415,7 +415,7 @@ namespace currency
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);
uint64_t get_scratchpad_size_for_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);
std::string get_account_address_and_payment_id_as_str(const account_public_address& addr, const std::string& payment_id);

View file

@ -14,16 +14,21 @@ 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));
return crypto::generate_scratchpad(seed, m_scratchpad, get_scratchpad_size_for_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);
CHECK_AND_ASSERT_THROW_MES(get_scratchpad_size_for_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;
}
crypto::hash scratchpad_keeper::get_pow_hash(const block& b)
{
blobdata bl = get_block_hashing_blob(b);
return get_pow_hash(bl, get_block_height(b));
}
uint64_t scratchpad_keeper::size()
{
return m_scratchpad.size();

View file

@ -4,7 +4,8 @@
#include "crypto/wild_keccak.h"
#include "currency_protocol/blobdatatype.h"
#include "currency_core/currency_basic.h"
namespace currency
{
@ -13,6 +14,7 @@ namespace currency
public:
bool update(const std::vector<crypto::hash>& seed, uint64_t height);
crypto::hash get_pow_hash(const blobdata& bd, uint64_t height);
crypto::hash get_pow_hash(const block& b);
uint64_t size();
private:
std::vector<crypto::hash> m_scratchpad;

View file

@ -114,7 +114,7 @@ namespace currency
{
context.m_priv.m_remote_version = hshd.client_version;
context.m_remote_version = hshd.client_version;
if(context.m_state == currency_connection_context::state_befor_handshake && !is_inital)
return true;

View file

@ -1061,6 +1061,7 @@ namespace nodetool
ce.time_started = cntxt.m_started;
ce.last_recv = cntxt.m_last_recv;
ce.last_send = cntxt.m_last_send;
ce.version = cntxt.m_remote_version;
rsp.connections_list.push_back(ce);
return true;
});

View file

@ -41,6 +41,7 @@ namespace nodetool
uint64_t time_started;
uint64_t last_recv;
uint64_t last_send;
std::string version;
};
#pragma pack(pop)

View file

@ -2,6 +2,6 @@
#define BUILD_COMMIT_ID "@VERSION@"
#define PROJECT_VERSION "1.0"
#define PROJECT_VERSION_BUILD_NO 3
#define PROJECT_VERSION_BUILD_NO 4
#define PROJECT_VERSION_BUILD_NO_STR STRINGIFY_EXPAND(PROJECT_VERSION_BUILD_NO)
#define PROJECT_VERSION_LONG PROJECT_VERSION "." PROJECT_VERSION_BUILD_NO_STR "[" BUILD_COMMIT_ID "]"