Skip to content

Commit

Permalink
Merge pull request #232 from VanGrx/develop_home
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
VanGrx authored Jul 27, 2020
2 parents 9fc5d4a + 8184371 commit 0fa79b5
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 120 deletions.
10 changes: 5 additions & 5 deletions src/cryptonote_basic/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ namespace cryptonote
}


miner::miner(i_miner_handler* phandler, Blockchain* pbc):m_stop(1),
miner::miner(i_miner_handler* phandler, const get_block_hash_t &gbh):m_stop(1),
m_template(boost::value_initialized<block>()),
m_template_no(0),
m_diffic(0),
m_thread_index(0),
m_phandler(phandler),
m_pbc(pbc),
m_gbh(gbh),
m_height(0),
m_pausers_count(0),
m_threads_total(0),
Expand Down Expand Up @@ -382,12 +382,12 @@ namespace cryptonote
return true;
}
//-----------------------------------------------------------------------------------------------------
bool miner::find_nonce_for_given_block(const Blockchain *pbc, block& bl, const difficulty_type& diffic, uint64_t height)
bool miner::find_nonce_for_given_block(const get_block_hash_t &gbh, block& bl, const difficulty_type& diffic, uint64_t height)
{
for(; bl.nonce != std::numeric_limits<uint32_t>::max(); bl.nonce++)
{
crypto::hash h;
get_block_longhash(pbc, bl, h, height, tools::get_max_concurrency());
gbh(bl, height, tools::get_max_concurrency(), h);

if(check_hash(h, diffic))
{
Expand Down Expand Up @@ -485,7 +485,7 @@ namespace cryptonote

b.nonce = nonce;
crypto::hash h;
get_block_longhash(m_pbc, b, h, height, tools::get_max_concurrency());
m_gbh(b, height, tools::get_max_concurrency(), h);

if(check_hash(h, local_diff))
{
Expand Down
8 changes: 4 additions & 4 deletions src/cryptonote_basic/miner.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ namespace cryptonote
~i_miner_handler(){};
};

class Blockchain;
typedef std::function<bool(const cryptonote::block&, uint64_t, unsigned int, crypto::hash&)> get_block_hash_t;

/************************************************************************/
/* */
/************************************************************************/
class miner
{
public:
miner(i_miner_handler* phandler, Blockchain* pbc);
miner(i_miner_handler* phandler, const get_block_hash_t& gbh);
~miner();
bool init(const boost::program_options::variables_map& vm, network_type nettype);
static void init_options(boost::program_options::options_description& desc);
Expand All @@ -81,7 +81,7 @@ namespace cryptonote
bool on_idle();
void on_synchronized();
//synchronous analog (for fast calls)
static bool find_nonce_for_given_block(const Blockchain *pbc, block& bl, const difficulty_type& diffic, uint64_t height);
static bool find_nonce_for_given_block(const get_block_hash_t &gbh, block& bl, const difficulty_type& diffic, uint64_t height);
void pause();
void resume();
void do_print_hashrate(bool do_hr);
Expand Down Expand Up @@ -137,7 +137,7 @@ namespace cryptonote
std::list<boost::thread> m_threads;
epee::critical_section m_threads_lock;
i_miner_handler* m_phandler;
Blockchain* m_pbc;
get_block_hash_t m_gbh;
account_public_address m_mine_address;
epee::math_helper::once_a_time_seconds<5> m_update_block_template_interval;
epee::math_helper::once_a_time_seconds<2> m_update_merge_hr_interval;
Expand Down
88 changes: 0 additions & 88 deletions src/cryptonote_core/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2550,94 +2550,6 @@ uint64_t Blockchain::block_difficulty(uint64_t i) const
return 0;
}
//------------------------------------------------------------------
//TODO: return type should be void, throw on exception
// alternatively, return true only if no blocks missed
template<class t_ids_container, class t_blocks_container, class t_missed_container>
bool Blockchain::get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);

for (const auto& block_hash : block_ids)
{
try
{
blocks.push_back(std::make_pair(m_db->get_block_blob(block_hash), block()));
if (!parse_and_validate_block_from_blob(blocks.back().first, blocks.back().second))
{
LOG_ERROR("Invalid block");
return false;
}
}
catch (const BLOCK_DNE& e)
{
missed_bs.push_back(block_hash);
}
catch (const std::exception& e)
{
return false;
}
}
return true;
}
//------------------------------------------------------------------
//TODO: return type should be void, throw on exception
// alternatively, return true only if no transactions missed
template<class t_ids_container, class t_tx_container, class t_missed_container>
bool Blockchain::get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);

for (const auto& tx_hash : txs_ids)
{
try
{
cryptonote::blobdata tx = AUTO_VAL_INIT(tx);
if (m_db->get_tx_blob(tx_hash, tx))
txs.push_back(std::move(tx));
else
missed_txs.push_back(tx_hash);
}
catch (const std::exception& e)
{
return false;
}
}
return true;
}
//------------------------------------------------------------------
template<class t_ids_container, class t_tx_container, class t_missed_container>
bool Blockchain::get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);

for (const auto& tx_hash : txs_ids)
{
try
{
cryptonote::blobdata tx = AUTO_VAL_INIT(tx);
if (m_db->get_tx_blob(tx_hash, tx))
{
txs.push_back(transaction());
if (!parse_and_validate_tx_from_blob(tx, txs.back()))
{
LOG_ERROR("Invalid transaction");
return false;
}
}
else
missed_txs.push_back(tx_hash);
}
catch (const std::exception& e)
{
return false;
}
}
return true;
}
//------------------------------------------------------------------
// Find the split point between us and foreign blockchain and return
// (by reference) the most recent common block hash along with up to
// BLOCKS_IDS_SYNCHRONIZING_DEFAULT_COUNT additional (more recent) hashes.
Expand Down
87 changes: 84 additions & 3 deletions src/cryptonote_core/blockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,37 @@ namespace cryptonote
*
* @return false if an unexpected exception occurs, else true
*/
//------------------------------------------------------------------
//TODO: return type should be void, throw on exception
// alternatively, return true only if no blocks missed
template<class t_ids_container, class t_blocks_container, class t_missed_container>
bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const;
bool get_blocks(const t_ids_container& block_ids, t_blocks_container& blocks, t_missed_container& missed_bs) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);

for (const auto& block_hash : block_ids)
{
try
{
blocks.push_back(std::make_pair(m_db->get_block_blob(block_hash), block()));
if (!parse_and_validate_block_from_blob(blocks.back().first, blocks.back().second))
{
LOG_ERROR("Invalid block");
return false;
}
}
catch (const BLOCK_DNE& e)
{
missed_bs.push_back(block_hash);
}
catch (const std::exception& e)
{
return false;
}
}
return true;
}

/**
* @brief gets transactions based on a list of transaction hashes
Expand All @@ -749,10 +778,62 @@ namespace cryptonote
*
* @return false if an unexpected exception occurs, else true
*/
//TODO: return type should be void, throw on exception
// alternatively, return true only if no transactions missed
template<class t_ids_container, class t_tx_container, class t_missed_container>
bool get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
bool get_transactions_blobs(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);

for (const auto& tx_hash : txs_ids)
{
try
{
cryptonote::blobdata tx = AUTO_VAL_INIT(tx);
if (m_db->get_tx_blob(tx_hash, tx))
txs.push_back(std::move(tx));
else
missed_txs.push_back(tx_hash);
}
catch (const std::exception& e)
{
return false;
}
}
return true;
}
//------------------------------------------------------------------
template<class t_ids_container, class t_tx_container, class t_missed_container>
bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const;
bool get_transactions(const t_ids_container& txs_ids, t_tx_container& txs, t_missed_container& missed_txs) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);

for (const auto& tx_hash : txs_ids)
{
try
{
cryptonote::blobdata tx = AUTO_VAL_INIT(tx);
if (m_db->get_tx_blob(tx_hash, tx))
{
txs.push_back(transaction());
if (!parse_and_validate_tx_from_blob(tx, txs.back()))
{
LOG_ERROR("Invalid transaction");
return false;
}
}
else
missed_txs.push_back(tx_hash);
}
catch (const std::exception& e)
{
return false;
}
}
return true;
}

//debug functions

Expand Down
4 changes: 3 additions & 1 deletion src/cryptonote_core/cryptonote_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ namespace cryptonote
core::core(i_cryptonote_protocol* pprotocol):
m_mempool(m_blockchain_storage),
m_blockchain_storage(m_mempool),
m_miner(this, &m_blockchain_storage),
m_miner(this, [this](const cryptonote::block &b, uint64_t height, unsigned int threads, crypto::hash &hash) {
return cryptonote::get_block_longhash(&m_blockchain_storage, b, hash, height, threads);
}),
m_miner_address(boost::value_initialized<account_public_address>()),
m_starter_message_showed(false),
m_target_blockchain_height(0),
Expand Down
4 changes: 3 additions & 1 deletion src/cryptonote_core/cryptonote_tx_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,9 @@ namespace cryptonote
bl.invalidate_hashes();
bl.timestamp = 0;
bl.nonce = nonce;
miner::find_nonce_for_given_block(NULL, bl, 1, 0);
miner::find_nonce_for_given_block([](const cryptonote::block &b, uint64_t height, unsigned int threads, crypto::hash &hash){
return cryptonote::get_block_longhash(NULL, b, hash, height, threads);
}, bl, 1, 0);
return true;
}
//---------------------------------------------------------------
Expand Down
21 changes: 18 additions & 3 deletions src/cryptonote_protocol/cryptonote_protocol_handler.inl
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,13 @@ namespace cryptonote
m_core.pause_mine();
std::list<block_complete_entry> blocks;
blocks.push_back(arg.b);
m_core.prepare_handle_incoming_blocks(blocks);
if (!m_core.prepare_handle_incoming_blocks(blocks))
{
LOG_PRINT_CCONTEXT_L1("Block verification failed: prepare_handle_incoming_blocks failed, dropping connection");
drop_connection(context, false, false);
m_core.resume_mine();
return 1;
}
for(auto tx_blob_it = arg.b.txs.begin(); tx_blob_it!=arg.b.txs.end();tx_blob_it++)
{
cryptonote::tx_verification_context tvc = AUTO_VAL_INIT(tvc);
Expand Down Expand Up @@ -629,7 +635,12 @@ namespace cryptonote

std::list<block_complete_entry> blocks;
blocks.push_back(b);
m_core.prepare_handle_incoming_blocks(blocks);
if (!m_core.prepare_handle_incoming_blocks(blocks))
{
LOG_PRINT_CCONTEXT_L0("Failure in prepare_handle_incoming_blocks");
m_core.resume_mine();
return 1;
}

block_verification_context bvc = boost::value_initialized<block_verification_context>();
m_core.handle_incoming_block(arg.b.block, bvc); // got block from handle_notify_new_block
Expand Down Expand Up @@ -1075,7 +1086,11 @@ skip:
const boost::posix_time::ptime start = boost::posix_time::microsec_clock::universal_time();
context.m_last_request_time = start;

m_core.prepare_handle_incoming_blocks(blocks);
if (!m_core.prepare_handle_incoming_blocks(blocks))
{
LOG_ERROR_CCONTEXT("Failure in prepare_handle_incoming_blocks");
return 1;
}

uint64_t block_process_time_full = 0, transactions_process_time_full = 0;
size_t num_txs = 0;
Expand Down
3 changes: 0 additions & 3 deletions src/safex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ safex_add_library(safex_core

target_link_libraries(safex_core
PUBLIC
blockchain_db
cryptonote_core
cncrypto
common
cryptonote_protocol
PRIVATE
${EXTRA_LIBRARIES})
2 changes: 1 addition & 1 deletion src/simplewallet/simplewallet_safex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ namespace cryptonote
tr("Height ") << height << ", " <<
tr("txid ") << txid << ", " <<
tr("Feedback sent received for offer: ") << feedback.offer_id << " received, " <<
tr("Stars given: ") << feedback.stars_given << ", " <<
tr("Stars given: ") << (uint64_t)feedback.stars_given << ", " <<
tr("Comment given: ") << comment << ", " <<
tr("idx ") << subaddr_index;

Expand Down
Loading

0 comments on commit 0fa79b5

Please sign in to comment.