aboutsummaryrefslogtreecommitdiff
path: root/src/cryptonote_core
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptonote_core')
-rw-r--r--src/cryptonote_core/blockchain.cpp77
-rw-r--r--src/cryptonote_core/blockchain.h7
-rw-r--r--src/cryptonote_core/cryptonote_core.cpp47
-rw-r--r--src/cryptonote_core/cryptonote_tx_utils.h7
4 files changed, 118 insertions, 20 deletions
diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp
index e4a63c77a..08f077f07 100644
--- a/src/cryptonote_core/blockchain.cpp
+++ b/src/cryptonote_core/blockchain.cpp
@@ -178,6 +178,7 @@ Blockchain::Blockchain(tx_memory_pool& tx_pool) :
m_enforce_dns_checkpoints(false), m_max_prepare_blocks_threads(4), m_db_sync_on_blocks(true), m_db_sync_threshold(1), m_db_sync_mode(db_async), m_db_default_sync(false), m_fast_sync(true), m_show_time_stats(false), m_sync_counter(0), m_bytes_to_sync(0), m_cancel(false),
m_long_term_block_weights_window(CRYPTONOTE_LONG_TERM_BLOCK_WEIGHT_WINDOW_SIZE),
m_long_term_effective_median_block_weight(0),
+ m_long_term_block_weights_cache_tip_hash(crypto::null_hash),
m_difficulty_for_next_block_top_hash(crypto::null_hash),
m_difficulty_for_next_block(1),
m_btc_valid(false)
@@ -645,6 +646,8 @@ block Blockchain::pop_block_from_blockchain()
block popped_block;
std::vector<transaction> popped_txs;
+ CHECK_AND_ASSERT_THROW_MES(m_db->height() > 1, "Cannot pop the genesis block");
+
try
{
m_db->pop_block(popped_block, popped_txs);
@@ -1279,7 +1282,50 @@ void Blockchain::get_long_term_block_weights(std::vector<uint64_t>& weights, uin
LOG_PRINT_L3("Blockchain::" << __func__);
CRITICAL_REGION_LOCAL(m_blockchain_lock);
+ PERF_TIMER(get_long_term_block_weights);
+
+ if (count == 0)
+ return;
+
+ bool cached = false;
+ uint64_t blockchain_height = m_db->height();
+ uint64_t tip_height = start_height + count - 1;
+ crypto::hash tip_hash = crypto::null_hash;
+ if (tip_height < blockchain_height && count == m_long_term_block_weights_cache.size())
+ {
+ tip_hash = m_db->get_block_hash_from_height(tip_height);
+ cached = tip_hash == m_long_term_block_weights_cache_tip_hash;
+ }
+
+ if (cached)
+ {
+ MTRACE("requesting " << count << " from " << start_height << ", cached");
+ weights = m_long_term_block_weights_cache;
+ return;
+ }
+
+ // in the vast majority of uncached cases, most is still cached,
+ // as we just move the window one block up:
+ if (tip_height > 0 && count == m_long_term_block_weights_cache.size() && tip_height < blockchain_height)
+ {
+ crypto::hash old_tip_hash = m_db->get_block_hash_from_height(tip_height - 1);
+ if (old_tip_hash == m_long_term_block_weights_cache_tip_hash)
+ {
+ weights = m_long_term_block_weights_cache;
+ for (size_t i = 1; i < weights.size(); ++i)
+ weights[i - 1] = weights[i];
+ MTRACE("requesting " << count << " from " << start_height << ", incremental");
+ weights.back() = m_db->get_block_long_term_weight(tip_height);
+ m_long_term_block_weights_cache = weights;
+ m_long_term_block_weights_cache_tip_hash = tip_hash;
+ return;
+ }
+ }
+
+ MTRACE("requesting " << count << " from " << start_height << ", uncached");
weights = m_db->get_long_term_block_weights(start_height, count);
+ m_long_term_block_weights_cache = weights;
+ m_long_term_block_weights_cache_tip_hash = tip_hash;
}
//------------------------------------------------------------------
uint64_t Blockchain::get_current_cumulative_block_weight_limit() const
@@ -1326,6 +1372,7 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m
m_btc.timestamp = time(NULL); // update timestamp unconditionally
b = m_btc;
diffic = m_btc_difficulty;
+ height = m_btc_height;
expected_reward = m_btc_expected_reward;
return true;
}
@@ -1470,7 +1517,7 @@ bool Blockchain::create_block_template(block& b, const account_public_address& m
", cumulative weight " << cumulative_weight << " is now good");
#endif
- cache_block_template(b, miner_address, ex_nonce, diffic, expected_reward, pool_cookie);
+ cache_block_template(b, miner_address, ex_nonce, diffic, height, expected_reward, pool_cookie);
return true;
}
LOG_ERROR("Failed to create_block_template with " << 10 << " tries");
@@ -1997,7 +2044,7 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc
return true;
}
//------------------------------------------------------------------
-uint64_t Blockchain::block_difficulty(uint64_t i) const
+difficulty_type Blockchain::block_difficulty(uint64_t i) const
{
LOG_PRINT_L3("Blockchain::" << __func__);
// WARNING: this function does not take m_blockchain_lock, and thus should only call read only
@@ -2196,7 +2243,11 @@ bool Blockchain::find_blockchain_supplement(const std::list<crypto::hash>& qbloc
bool result = find_blockchain_supplement(qblock_ids, resp.m_block_ids, resp.start_height, resp.total_height);
if (result)
- resp.cumulative_difficulty = m_db->get_block_cumulative_difficulty(resp.total_height - 1);
+ {
+ cryptonote::difficulty_type wide_cumulative_difficulty = m_db->get_block_cumulative_difficulty(resp.total_height - 1);
+ resp.cumulative_difficulty = (wide_cumulative_difficulty << 64 >> 64).convert_to<uint64_t>();
+ resp.cumulative_difficulty_top64 = (wide_cumulative_difficulty >> 64).convert_to<uint64_t>();
+ }
return result;
}
@@ -3126,6 +3177,7 @@ bool Blockchain::check_fee(size_t tx_weight, uint64_t fee) const
if (version >= HF_VERSION_DYNAMIC_FEE)
{
median = m_current_block_cumul_weight_limit / 2;
+ const uint64_t blockchain_height = m_db->height();
already_generated_coins = blockchain_height ? m_db->get_block_already_generated_coins(blockchain_height - 1) : 0;
if (!get_block_reward(median, 1, already_generated_coins, base_reward, version))
return false;
@@ -3816,6 +3868,8 @@ bool Blockchain::update_next_cumulative_weight_limit(uint64_t *long_term_effecti
LOG_PRINT_L3("Blockchain::" << __func__);
+ m_db->block_txn_start(false);
+
// when we reach this, the last hf version is not yet written to the db
const uint64_t db_height = m_db->height();
const uint8_t hf_version = get_current_hard_fork_version();
@@ -3878,6 +3932,10 @@ bool Blockchain::update_next_cumulative_weight_limit(uint64_t *long_term_effecti
if (long_term_effective_median_block_weight)
*long_term_effective_median_block_weight = m_long_term_effective_median_block_weight;
+ m_db->add_max_block_size(m_current_block_cumul_weight_limit);
+
+ m_db->block_txn_stop();
+
return true;
}
//------------------------------------------------------------------
@@ -4276,8 +4334,9 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
for (unsigned int j = 0; j < batches; j++, ++blockidx)
{
block &block = blocks[blockidx];
+ crypto::hash block_hash;
- if (!parse_and_validate_block_from_blob(it->block, block))
+ if (!parse_and_validate_block_from_blob(it->block, block, block_hash))
return false;
// check first block and skip all blocks if its not chained properly
@@ -4290,7 +4349,7 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
return true;
}
}
- if (have_block(get_block_hash(block)))
+ if (have_block(block_hash))
blocks_exist = true;
std::advance(it, 1);
@@ -4300,11 +4359,12 @@ bool Blockchain::prepare_handle_incoming_blocks(const std::vector<block_complete
for (unsigned i = 0; i < extra && !blocks_exist; i++, blockidx++)
{
block &block = blocks[blockidx];
+ crypto::hash block_hash;
- if (!parse_and_validate_block_from_blob(it->block, block))
+ if (!parse_and_validate_block_from_blob(it->block, block, block_hash))
return false;
- if (have_block(get_block_hash(block)))
+ if (have_block(block_hash))
blocks_exist = true;
std::advance(it, 1);
@@ -4832,13 +4892,14 @@ void Blockchain::invalidate_block_template_cache()
m_btc_valid = false;
}
-void Blockchain::cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t expected_reward, uint64_t pool_cookie)
+void Blockchain::cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t height, uint64_t expected_reward, uint64_t pool_cookie)
{
MDEBUG("Setting block template cache");
m_btc = b;
m_btc_address = address;
m_btc_nonce = nonce;
m_btc_difficulty = diff;
+ m_btc_height = height;
m_btc_expected_reward = expected_reward;
m_btc_pool_cookie = pool_cookie;
m_btc_valid = true;
diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h
index 3b8169764..c1677ed37 100644
--- a/src/cryptonote_core/blockchain.h
+++ b/src/cryptonote_core/blockchain.h
@@ -653,7 +653,7 @@ namespace cryptonote
*
* @return the difficulty
*/
- uint64_t block_difficulty(uint64_t i) const;
+ difficulty_type block_difficulty(uint64_t i) const;
/**
* @brief gets blocks based on a list of block hashes
@@ -1060,6 +1060,8 @@ namespace cryptonote
uint64_t m_timestamps_and_difficulties_height;
uint64_t m_long_term_block_weights_window;
uint64_t m_long_term_effective_median_block_weight;
+ mutable crypto::hash m_long_term_block_weights_cache_tip_hash;
+ mutable std::vector<uint64_t> m_long_term_block_weights_cache;
epee::critical_section m_difficulty_lock;
crypto::hash m_difficulty_for_next_block_top_hash;
@@ -1092,6 +1094,7 @@ namespace cryptonote
account_public_address m_btc_address;
blobdata m_btc_nonce;
difficulty_type m_btc_difficulty;
+ uint64_t m_btc_height;
uint64_t m_btc_pool_cookie;
uint64_t m_btc_expected_reward;
bool m_btc_valid;
@@ -1462,6 +1465,6 @@ namespace cryptonote
*
* At some point, may be used to push an update to miners
*/
- void cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t expected_reward, uint64_t pool_cookie);
+ void cache_block_template(const block &b, const cryptonote::account_public_address &address, const blobdata &nonce, const difficulty_type &diff, uint64_t height, uint64_t expected_reward, uint64_t pool_cookie);
};
} // namespace cryptonote
diff --git a/src/cryptonote_core/cryptonote_core.cpp b/src/cryptonote_core/cryptonote_core.cpp
index 387203cc0..6b0052dc0 100644
--- a/src/cryptonote_core/cryptonote_core.cpp
+++ b/src/cryptonote_core/cryptonote_core.cpp
@@ -62,6 +62,9 @@ DISABLE_VS_WARNINGS(4355)
#define BAD_SEMANTICS_TXES_MAX_SIZE 100
+// basically at least how many bytes the block itself serializes to without the miner tx
+#define BLOCK_SIZE_SANITY_LEEWAY 100
+
namespace cryptonote
{
const command_line::arg_descriptor<bool, false> arg_testnet_on = {
@@ -1417,22 +1420,26 @@ namespace cryptonote
{
TRY_ENTRY();
- // load json & DNS checkpoints every 10min/hour respectively,
- // and verify them with respect to what blocks we already have
- CHECK_AND_ASSERT_MES(update_checkpoints(), false, "One or more checkpoints loaded from json or dns conflicted with existing checkpoints.");
-
bvc = boost::value_initialized<block_verification_context>();
- if(block_blob.size() > get_max_block_size())
+
+ if (!check_incoming_block_size(block_blob))
{
- LOG_PRINT_L1("WRONG BLOCK BLOB, too big size " << block_blob.size() << ", rejected");
bvc.m_verifivation_failed = true;
return false;
}
+ if (((size_t)-1) <= 0xffffffff && block_blob.size() >= 0x3fffffff)
+ MWARNING("This block's size is " << block_blob.size() << ", closing on the 32 bit limit");
+
+ // load json & DNS checkpoints every 10min/hour respectively,
+ // and verify them with respect to what blocks we already have
+ CHECK_AND_ASSERT_MES(update_checkpoints(), false, "One or more checkpoints loaded from json or dns conflicted with existing checkpoints.");
+
block lb;
if (!b)
{
- if(!parse_and_validate_block_from_blob(block_blob, lb))
+ crypto::hash block_hash;
+ if(!parse_and_validate_block_from_blob(block_blob, lb, block_hash))
{
LOG_PRINT_L1("Failed to parse and validate new block");
bvc.m_verifivation_failed = true;
@@ -1452,9 +1459,13 @@ namespace cryptonote
// block_blob
bool core::check_incoming_block_size(const blobdata& block_blob) const
{
- if(block_blob.size() > get_max_block_size())
+ // note: we assume block weight is always >= block blob size, so we check incoming
+ // blob size against the block weight limit, which acts as a sanity check without
+ // having to parse/weigh first; in fact, since the block blob is the block header
+ // plus the tx hashes, the weight will typically be much larger than the blob size
+ if(block_blob.size() > m_blockchain_storage.get_current_cumulative_block_weight_limit() + BLOCK_SIZE_SANITY_LEEWAY)
{
- LOG_PRINT_L1("WRONG BLOCK BLOB, too big size " << block_blob.size() << ", rejected");
+ LOG_PRINT_L1("WRONG BLOCK BLOB, sanity check failed on size " << block_blob.size() << ", rejected");
return false;
}
return true;
@@ -1784,12 +1795,28 @@ namespace cryptonote
return f;
}
//-----------------------------------------------------------------------------------------------
- static double probability(unsigned int blocks, unsigned int expected)
+ static double probability1(unsigned int blocks, unsigned int expected)
{
// https://www.umass.edu/wsp/resources/poisson/#computing
return pow(expected, blocks) / (factorial(blocks) * exp(expected));
}
//-----------------------------------------------------------------------------------------------
+ static double probability(unsigned int blocks, unsigned int expected)
+ {
+ double p = 0.0;
+ if (blocks <= expected)
+ {
+ for (unsigned int b = 0; b <= blocks; ++b)
+ p += probability1(b, expected);
+ }
+ else if (blocks > expected)
+ {
+ for (unsigned int b = blocks; b <= expected * 3 /* close enough */; ++b)
+ p += probability1(b, expected);
+ }
+ return p;
+ }
+ //-----------------------------------------------------------------------------------------------
bool core::check_block_rate()
{
if (m_offline || m_target_blockchain_height > get_current_blockchain_height())
diff --git a/src/cryptonote_core/cryptonote_tx_utils.h b/src/cryptonote_core/cryptonote_tx_utils.h
index cb1561c2d..d38aa7474 100644
--- a/src/cryptonote_core/cryptonote_tx_utils.h
+++ b/src/cryptonote_core/cryptonote_tx_utils.h
@@ -104,6 +104,13 @@ namespace cryptonote
std::vector<rct::key> &amount_keys,
crypto::public_key &out_eph_public_key) ;
+ bool generate_output_ephemeral_keys(const size_t tx_version, const cryptonote::account_keys &sender_account_keys, const crypto::public_key &txkey_pub, const crypto::secret_key &tx_key,
+ const cryptonote::tx_destination_entry &dst_entr, const boost::optional<cryptonote::account_public_address> &change_addr, const size_t output_index,
+ const bool &need_additional_txkeys, const std::vector<crypto::secret_key> &additional_tx_keys,
+ std::vector<crypto::public_key> &additional_tx_public_keys,
+ std::vector<rct::key> &amount_keys,
+ crypto::public_key &out_eph_public_key) ;
+
bool generate_genesis_block(
block& bl
, std::string const & genesis_tx