Merge bitmonero@monero-project

This commit is contained in:
Neozaru 2014-06-01 18:21:30 +02:00
commit 8530629e22
50 changed files with 1202 additions and 433 deletions

View file

@ -372,7 +372,7 @@ bool blockchain_storage::rollback_blockchain_switching(std::list<block>& origina
return true;
}
//------------------------------------------------------------------
bool blockchain_storage::switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain)
bool blockchain_storage::switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain)
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);
CHECK_AND_ASSERT_MES(alt_chain.size(), false, "switch_to_alternative_blockchain: empty chain passed");
@ -414,16 +414,19 @@ bool blockchain_storage::switch_to_alternative_blockchain(std::list<blocks_ext_b
}
}
//pushing old chain as alternative chain
BOOST_FOREACH(auto& old_ch_ent, disconnected_chain)
if(!discard_disconnected_chain)
{
block_verification_context bvc = boost::value_initialized<block_verification_context>();
bool r = handle_alternative_block(old_ch_ent, get_block_hash(old_ch_ent), bvc);
if(!r)
//pushing old chain as alternative chain
BOOST_FOREACH(auto& old_ch_ent, disconnected_chain)
{
LOG_ERROR("Failed to push ex-main chain blocks to alternative chain ");
rollback_blockchain_switching(disconnected_chain, split_height);
return false;
block_verification_context bvc = boost::value_initialized<block_verification_context>();
bool r = handle_alternative_block(old_ch_ent, get_block_hash(old_ch_ent), bvc);
if(!r)
{
LOG_ERROR("Failed to push ex-main chain blocks to alternative chain ");
rollback_blockchain_switching(disconnected_chain, split_height);
return false;
}
}
}
@ -701,6 +704,22 @@ bool blockchain_storage::handle_alternative_block(const block& b, const crypto::
{
CRITICAL_REGION_LOCAL(m_blockchain_lock);
uint64_t block_height = get_block_height(b);
if(0 == block_height)
{
LOG_ERROR("Block with id: " << string_tools::pod_to_hex(id) << " (as alternative) have wrong miner transaction");
bvc.m_verifivation_failed = true;
return false;
}
if (!m_checkpoints.is_alternative_block_allowed(get_current_blockchain_height(), block_height))
{
LOG_PRINT_RED_L0("Block with id: " << id
<< ENDL << " can't be accepted for alternative chain, block height: " << block_height
<< ENDL << " blockchain height: " << get_current_blockchain_height());
bvc.m_verifivation_failed = true;
return false;
}
//block is not related with head of main chain
//first of all - look in alternative chains container
auto it_main_prev = m_blocks_index.find(b.prev_id);
@ -746,31 +765,28 @@ bool blockchain_storage::handle_alternative_block(const block& b, const crypto::
block_extended_info bei = boost::value_initialized<block_extended_info>();
bei.bl = b;
bei.height = alt_chain.size() ? it_prev->second.height + 1 : it_main_prev->second + 1;
bool is_a_checkpoint;
if(!m_checkpoints.check_block(bei.height, id, is_a_checkpoint))
{
LOG_ERROR("CHECKPOINT VALIDATION FAILED");
bvc.m_verifivation_failed = true;
return false;
}
// Always check PoW for alternative blocks
m_is_in_checkpoint_zone = false;
difficulty_type current_diff = get_next_difficulty_for_alternative_chain(alt_chain, bei);
CHECK_AND_ASSERT_MES(current_diff, false, "!!!!!!! DIFFICULTY OVERHEAD !!!!!!!");
crypto::hash proof_of_work = null_hash;
if(!m_checkpoints.is_in_checkpoint_zone(bei.height))
get_block_longhash(bei.bl, proof_of_work, bei.height);
if(!check_hash(proof_of_work, current_diff))
{
m_is_in_checkpoint_zone = false;
get_block_longhash(bei.bl, proof_of_work, bei.height);
if(!check_hash(proof_of_work, current_diff))
{
LOG_PRINT_RED_L0("Block with id: " << id
<< ENDL << " for alternative chain, have not enough proof of work: " << proof_of_work
<< ENDL << " expected difficulty: " << current_diff);
bvc.m_verifivation_failed = true;
return false;
}
}else
{
m_is_in_checkpoint_zone = true;
if(!m_checkpoints.check_block(bei.height, id))
{
LOG_ERROR("CHECKPOINT VALIDATION FAILED");
bvc.m_verifivation_failed = true;
return false;
}
LOG_PRINT_RED_L0("Block with id: " << id
<< ENDL << " for alternative chain, have not enough proof of work: " << proof_of_work
<< ENDL << " expected difficulty: " << current_diff);
bvc.m_verifivation_failed = true;
return false;
}
if(!prevalidate_miner_transaction(b, bei.height))
@ -792,22 +808,33 @@ bool blockchain_storage::handle_alternative_block(const block& b, const crypto::
auto i_res = m_alternative_chains.insert(blocks_ext_by_hash::value_type(id, bei));
CHECK_AND_ASSERT_MES(i_res.second, false, "insertion of new alternative block returned as it already exist");
alt_chain.push_back(i_res.first);
//check if difficulty bigger then in main chain
if(m_blocks.back().cumulative_difficulty < bei.cumulative_difficulty)
if(is_a_checkpoint)
{
//do reorganize!
LOG_PRINT_GREEN("###### REORGANIZE on height: " << alt_chain.front()->second.height << " of " << m_blocks.size() -1 << " with cum_difficulty " << m_blocks.back().cumulative_difficulty
<< ENDL << " alternative blockchain size: " << alt_chain.size() << " with cum_difficulty " << bei.cumulative_difficulty, LOG_LEVEL_0);
bool r = switch_to_alternative_blockchain(alt_chain);
LOG_PRINT_GREEN("###### REORGANIZE on height: " << alt_chain.front()->second.height << " of " << m_blocks.size() - 1 <<
", checkpoint is found in alternative chain on height " << bei.height, LOG_LEVEL_0);
bool r = switch_to_alternative_blockchain(alt_chain, true);
if(r) bvc.m_added_to_main_chain = true;
else bvc.m_verifivation_failed = true;
return r;
}else if(m_blocks.back().cumulative_difficulty < bei.cumulative_difficulty) //check if difficulty bigger then in main chain
{
//do reorganize!
LOG_PRINT_GREEN("###### REORGANIZE on height: " << alt_chain.front()->second.height << " of " << m_blocks.size() - 1 << " with cum_difficulty " << m_blocks.back().cumulative_difficulty
<< ENDL << " alternative blockchain size: " << alt_chain.size() << " with cum_difficulty " << bei.cumulative_difficulty, LOG_LEVEL_0);
bool r = switch_to_alternative_blockchain(alt_chain, false);
if(r) bvc.m_added_to_main_chain = true;
else bvc.m_verifivation_failed = true;
return r;
}else
{
LOG_PRINT_BLUE("----- BLOCK ADDED AS ALTERNATIVE ON HEIGHT " << bei.height
<< ENDL << "id:\t" << id
<< ENDL << "PoW:\t" << proof_of_work
<< ENDL << "difficulty:\t" << current_diff, LOG_LEVEL_0);
return true;
}
LOG_PRINT_BLUE("----- BLOCK ADDED AS ALTERNATIVE ON HEIGHT " << bei.height
<< ENDL << "id:\t" << id
<< ENDL << "PoW:\t" << proof_of_work
<< ENDL << "difficulty:\t" << current_diff, LOG_LEVEL_0);
return true;
}else
{
//block orphaned
@ -815,7 +842,6 @@ bool blockchain_storage::handle_alternative_block(const block& b, const crypto::
LOG_PRINT_RED_L0("Block recognized as orphaned and rejected, id = " << id);
}
return true;
}
//------------------------------------------------------------------
@ -1480,19 +1506,27 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
TIME_MEASURE_FINISH(target_calculating_time);
TIME_MEASURE_START(longhash_calculating_time);
crypto::hash proof_of_work = null_hash;
if(!m_checkpoints.is_in_checkpoint_zone(get_current_blockchain_height()))
{
proof_of_work = get_block_longhash(bl, m_blocks.size());
if(!check_hash(proof_of_work, current_diffic))
{
LOG_PRINT_L0("Block with id: " << id << ENDL
<< "have not enough proof of work: " << proof_of_work << ENDL
<< "nexpected difficulty: " << current_diffic );
bvc.m_verifivation_failed = true;
return false;
}
}else
// Formerly the code below contained an if loop with the following condition
// !m_checkpoints.is_in_checkpoint_zone(get_current_blockchain_height())
// however, this caused the daemon to not bother checking PoW for blocks
// before checkpoints, which is very dangerous behaviour. We moved the PoW
// validation out of the next chunk of code to make sure that we correctly
// check PoW now.
proof_of_work = get_block_longhash(bl, m_blocks.size());
if(!check_hash(proof_of_work, current_diffic))
{
LOG_PRINT_L0("Block with id: " << id << ENDL
<< "have not enough proof of work: " << proof_of_work << ENDL
<< "nexpected difficulty: " << current_diffic );
bvc.m_verifivation_failed = true;
return false;
}
// If we're at a checkpoint, ensure that our hardcoded checkpoint hash
// is correct.
if(m_checkpoints.is_in_checkpoint_zone(get_current_blockchain_height()))
{
if(!m_checkpoints.check_block(get_current_blockchain_height(), id))
{
@ -1501,6 +1535,7 @@ bool blockchain_storage::handle_block_to_main_chain(const block& bl, const crypt
return false;
}
}
TIME_MEASURE_FINISH(longhash_calculating_time);
if(!prevalidate_miner_transaction(bl, m_blocks.size()))
@ -1648,4 +1683,4 @@ bool blockchain_storage::add_new_block(const block& bl_, block_verification_cont
}
return handle_block_to_main_chain(bl, id, bvc);
}
}

View file

@ -13,6 +13,8 @@
#include <boost/foreach.hpp>
#include <atomic>
#include "syncobj.h"
#include "string_tools.h"
#include "tx_pool.h"
#include "cryptonote_basic.h"
#include "common/util.h"
@ -50,7 +52,7 @@ namespace cryptonote
uint64_t already_generated_coins;
};
blockchain_storage(tx_memory_pool& tx_pool):m_tx_pool(tx_pool), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false)
blockchain_storage(tx_memory_pool& tx_pool):m_tx_pool(tx_pool), m_current_block_cumul_sz_limit(0), m_is_in_checkpoint_zone(false), m_is_blockchain_storing(false)
{};
bool init() { return init(tools::get_default_data_dir()); }
@ -119,7 +121,7 @@ namespace cryptonote
missed_bs.push_back(bl_id);
else
{
CHECK_AND_ASSERT_MES(it->second < m_blocks.size(), false, "Internal error: bl_id=" << string_tools::pod_to_hex(bl_id)
CHECK_AND_ASSERT_MES(it->second < m_blocks.size(), false, "Internal error: bl_id=" << epee::string_tools::pod_to_hex(bl_id)
<< " have index record with offset="<<it->second<< ", bigger then m_blocks.size()=" << m_blocks.size());
blocks.push_back(m_blocks[it->second].bl);
}
@ -163,7 +165,7 @@ namespace cryptonote
typedef std::map<uint64_t, std::vector<std::pair<crypto::hash, size_t>>> outputs_container; //crypto::hash - tx hash, size_t - index of out in transaction
tx_memory_pool& m_tx_pool;
critical_section m_blockchain_lock; // TODO: add here reader/writer lock
epee::critical_section m_blockchain_lock; // TODO: add here reader/writer lock
// main chain
blocks_container m_blocks; // height -> block_extended_info
@ -186,7 +188,7 @@ namespace cryptonote
std::atomic<bool> m_is_in_checkpoint_zone;
std::atomic<bool> m_is_blockchain_storing;
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain);
bool switch_to_alternative_blockchain(std::list<blocks_ext_by_hash::iterator>& alt_chain, bool discard_disconnected_chain);
bool pop_block_from_blockchain();
bool purge_block_data_from_blockchain(const block& b, size_t processed_tx_count);
bool purge_transaction_from_blockchain(const crypto::hash& tx_id);
@ -301,7 +303,7 @@ namespace cryptonote
return false;
}
transactions_container::iterator tx_it = m_transactions.find(amount_outs_vec[i].first);
CHECK_AND_ASSERT_MES(tx_it != m_transactions.end(), false, "Wrong transaction id in output indexes: " <<string_tools::pod_to_hex(amount_outs_vec[i].first));
CHECK_AND_ASSERT_MES(tx_it != m_transactions.end(), false, "Wrong transaction id in output indexes: " << epee::string_tools::pod_to_hex(amount_outs_vec[i].first));
CHECK_AND_ASSERT_MES(amount_outs_vec[i].second < tx_it->second.tx.vout.size(), false,
"Wrong index in transaction outputs: " << amount_outs_vec[i].second << ", expected less then " << tx_it->second.tx.vout.size());
if(!vis.handle_output(tx_it->second.tx, tx_it->second.tx.vout[amount_outs_vec[i].second]))

View file

@ -29,10 +29,11 @@ namespace cryptonote
return !m_points.empty() && (height <= (--m_points.end())->first);
}
//---------------------------------------------------------------------------
bool checkpoints::check_block(uint64_t height, const crypto::hash& h) const
bool checkpoints::check_block(uint64_t height, const crypto::hash& h, bool& is_a_checkpoint) const
{
auto it = m_points.find(height);
if(it == m_points.end())
is_a_checkpoint = it != m_points.end();
if(!is_a_checkpoint)
return true;
if(it->second == h)
@ -45,4 +46,25 @@ namespace cryptonote
return false;
}
}
//---------------------------------------------------------------------------
bool checkpoints::check_block(uint64_t height, const crypto::hash& h) const
{
bool ignored;
return check_block(height, h, ignored);
}
//---------------------------------------------------------------------------
bool checkpoints::is_alternative_block_allowed(uint64_t blockchain_height, uint64_t block_height) const
{
if (0 == block_height)
return false;
auto it = m_points.upper_bound(blockchain_height);
// Is blockchain_height before the first checkpoint?
if (it == m_points.begin())
return true;
--it;
uint64_t checkpoint_height = it->first;
return checkpoint_height < block_height;
}
}

View file

@ -16,6 +16,9 @@ namespace cryptonote
bool add_checkpoint(uint64_t height, const std::string& hash_str);
bool is_in_checkpoint_zone(uint64_t height) const;
bool check_block(uint64_t height, const crypto::hash& h) const;
bool check_block(uint64_t height, const crypto::hash& h, bool& is_a_checkpoint) const;
bool is_alternative_block_allowed(uint64_t blockchain_height, uint64_t block_height) const;
private:
std::map<uint64_t, crypto::hash> m_points;
};

View file

@ -12,12 +12,9 @@
namespace cryptonote {
inline bool create_checkpoints(cryptonote::checkpoints& checkpoints)
{
// Checkpointing disabled until we can make the client not fast-sync
// without checking PoW at some point. Otherwise we may be exposed
// to blockchain corruption attacks. Need to investigate this further.
// 8-5-14
// ADD_CHECKPOINT(22231, "7cb10e29d67e1c069e6e11b17d30b809724255fee2f6868dc14cfc6ed44dfb25");
// ADD_CHECKPOINT(29556, "53c484a8ed91e4da621bb2fa88106dbde426fe90d7ef07b9c1e5127fb6f3a7f6");
ADD_CHECKPOINT(22231, "7cb10e29d67e1c069e6e11b17d30b809724255fee2f6868dc14cfc6ed44dfb25");
ADD_CHECKPOINT(29556, "53c484a8ed91e4da621bb2fa88106dbde426fe90d7ef07b9c1e5127fb6f3a7f6");
ADD_CHECKPOINT(50000, "0fe8758ab06a8b9cb35b7328fd4f757af530a5d37759f9d3e421023231f7b31c");
return true;
}
}

View file

@ -502,7 +502,7 @@ namespace cryptonote
LOG_PRINT_L0(ENDL << "**********************************************************************" << ENDL
<< "The daemon will start synchronizing with the network. It may take up to several hours." << ENDL
<< ENDL
<< "You can set the level of process detailization by using command \"set_log <level>\", where <level> is either 0 (no details), 1 (current block height synchronized), or 2 (all details)." << ENDL
<< "You can set the level of process detailization* through \"set_log <level>\" command*, where <level> is between 0 (no details) and 4 (very verbose)." << ENDL
<< ENDL
<< "Use \"help\" command to see the list of available commands." << ENDL
<< ENDL

View file

@ -115,13 +115,13 @@ namespace cryptonote
tx_memory_pool m_mempool;
blockchain_storage m_blockchain_storage;
i_cryptonote_protocol* m_pprotocol;
critical_section m_incoming_tx_lock;
epee::critical_section m_incoming_tx_lock;
//m_miner and m_miner_addres are probably temporary here
miner m_miner;
account_public_address m_miner_address;
std::string m_config_folder;
cryptonote_protocol_stub m_protocol_stub;
math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
epee::math_helper::once_a_time_seconds<60*60*12, false> m_store_blockchain_interval;
friend class tx_validate_inputs;
std::atomic<bool> m_starter_message_showed;
};

View file

@ -239,8 +239,7 @@ namespace cryptonote
crypto::public_key get_tx_pub_key_from_extra(const std::vector<uint8_t>& tx_extra)
{
std::vector<tx_extra_field> tx_extra_fields;
if (!parse_tx_extra(tx_extra, tx_extra_fields))
return null_pkey;
parse_tx_extra(tx_extra, tx_extra_fields);
tx_extra_pub_key pub_key_field;
if(!find_tx_extra_field_by_type(tx_extra_fields, pub_key_field))

View file

@ -4,7 +4,6 @@
#pragma once
#include <boost/atomic.hpp>
#include <boost/program_options.hpp>
#include <atomic>
#include "cryptonote_basic.h"
@ -66,7 +65,7 @@ namespace cryptonote
volatile uint32_t m_stop;
::critical_section m_template_lock;
epee::critical_section m_template_lock;
block m_template;
std::atomic<uint32_t> m_template_no;
std::atomic<uint32_t> m_starter_nonce;
@ -75,21 +74,21 @@ namespace cryptonote
volatile uint32_t m_thread_index;
volatile uint32_t m_threads_total;
std::atomic<int32_t> m_pausers_count;
::critical_section m_miners_count_lock;
epee::critical_section m_miners_count_lock;
std::list<boost::thread> m_threads;
::critical_section m_threads_lock;
epee::critical_section m_threads_lock;
i_miner_handler* m_phandler;
account_public_address m_mine_address;
math_helper::once_a_time_seconds<5> m_update_block_template_interval;
math_helper::once_a_time_seconds<2> m_update_merge_hr_interval;
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;
std::vector<blobdata> m_extra_messages;
miner_config m_config;
std::string m_config_folder_path;
std::atomic<uint64_t> m_last_hr_merge_time;
std::atomic<uint64_t> m_hashes;
std::atomic<uint64_t> m_current_hash_rate;
critical_section m_last_hash_rates_lock;
epee::critical_section m_last_hash_rates_lock;
std::list<uint64_t> m_last_hash_rates;
bool m_do_print_hashrate;
bool m_do_mining;

View file

@ -135,7 +135,6 @@ namespace cryptonote
// varint tag;
// varint size;
// varint data[];
//typedef boost::variant<tx_extra_padding, tx_extra_pub_key, tx_extra_nonce> tx_extra_field;
typedef boost::variant<tx_extra_padding, tx_extra_pub_key, tx_extra_nonce, tx_extra_merge_mining_tag> tx_extra_field;
}

View file

@ -350,18 +350,69 @@ namespace cryptonote
//---------------------------------------------------------------------------------
bool tx_memory_pool::fill_block_template(block &bl, size_t median_size, uint64_t already_generated_coins, size_t &total_size, uint64_t &fee)
{
// Warning: This function takes already_generated_
// coins as an argument and appears to do nothing
// with it.
CRITICAL_REGION_LOCAL(m_transactions_lock);
total_size = 0;
fee = 0;
size_t max_total_size = 2 * median_size - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
size_t max_total_size = 2 * median_size - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE; // Max block size
std::unordered_set<crypto::key_image> k_images;
// Tx size limit as in wallet2.h
// tx_pool.cpp uses size_t for tx sizes, whereas
// wallet2.h uses uint64_t; just use size_t here
// for now
size_t upper_transaction_size_limit = ((CRYPTONOTE_BLOCK_GRANTED_FULL_REWARD_ZONE * 125) / 100) - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
// Calculate size limit based on median too; useful
// for when we actually fix wallet2.h's maximum
// allowable tx size
//
// Can be removed when wallet2.h calculates max
// tx size based on the median too; just use
// upper_transaction_size_limit_median in all cases
size_t upper_transaction_size_limit_median = ((median_size * 125) / 100) - CRYPTONOTE_COINBASE_BLOB_RESERVED_SIZE;
if (upper_transaction_size_limit_median > upper_transaction_size_limit)
upper_transaction_size_limit = upper_transaction_size_limit_median;
BOOST_FOREACH(transactions_container::value_type& tx, m_transactions)
{
// Can not exceed maximum block size
if (max_total_size < total_size + tx.second.blob_size)
continue;
// Check to see if the minimum fee is included;
// exclude tx missing minimum fee
if (tx.second.fee < DEFAULT_FEE)
continue;
// Skip transactions that are too large
// TODO: Correct upper_transactions_size_limit
// such that it is based on median block size;
// We need to make a similar patch for
// wallet2.h
if (tx.second.blob_size > upper_transaction_size_limit)
continue;
// If adding this tx will make the block size
// greater than 130% of the median, reject the
// tx; this will keep down largely punitive tx
// from being included
if ( (total_size + tx.second.blob_size) > ((130 * median_size) / 100) )
continue;
// If we've exceeded the penalty free size,
// stop including more tx
if (total_size > median_size)
break;
// Skip transactions that are not ready to be
// included into the blockchain or that are
// missing key images
if (!is_transaction_ready_to_go(tx.second) || have_key_images(k_images, tx.second.tx))
continue;

View file

@ -4,8 +4,6 @@
#pragma once
#include "include_base_utils.h"
using namespace epee;
#include <set>
#include <unordered_map>