Pulse: Make create block template have pulse/miner variants

This commit is contained in:
Doyle 2020-07-16 14:08:29 +10:00
parent 80273cd7fe
commit 5c2c35c0d6
8 changed files with 60 additions and 23 deletions

View File

@ -123,7 +123,7 @@ namespace cryptonote
extra_nonce = m_extra_messages[m_config.current_extra_message_index];
}
if(!m_phandler->get_next_block_template(bl, m_mine_address, di, height, expected_reward, extra_nonce))
if(!m_phandler->create_next_miner_block_template(bl, m_mine_address, di, height, expected_reward, extra_nonce))
{
LOG_ERROR("Failed to get_block_template(), stopping mining");
return false;

View File

@ -50,7 +50,7 @@ namespace cryptonote
struct i_miner_handler
{
virtual bool handle_block_found(block& b, block_verification_context &bvc) = 0;
virtual bool get_next_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce) = 0;
virtual bool create_next_miner_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce) = 0;
protected:
~i_miner_handler(){};
};

View File

@ -1514,7 +1514,7 @@ uint64_t Blockchain::get_current_cumulative_block_weight_median() const
// unchanged for the time being.
//
// This function makes a new block for a miner to mine the hash for
bool Blockchain::create_block_template(block& b, const crypto::hash *from_block, const account_public_address& miner_address, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
bool Blockchain::create_block_template_internal(block& b, const crypto::hash *from_block, const block_template_info& info, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
{
LOG_PRINT_L3("Blockchain::" << __func__);
size_t median_weight;
@ -1522,12 +1522,13 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
uint64_t pool_cookie;
auto lock = tools::unique_locks(m_tx_pool, *this);
if (m_btc_valid && !from_block) {
if (0 && m_btc_valid && !from_block)
{
// The pool cookie is atomic. The lack of locking is OK, as if it changes
// just as we compare it, we'll just use a slightly old template, but
// this would be the case anyway if we'd lock, and the change happened
// just after the block template was created
if (!memcmp(&miner_address, &m_btc_address, sizeof(cryptonote::account_public_address)) && m_btc_nonce == ex_nonce
if (!memcmp(&info.miner_address, &m_btc_address, sizeof(cryptonote::account_public_address)) && m_btc_nonce == ex_nonce
&& m_btc_pool_cookie == m_tx_pool.cookie() && m_btc.prev_id == get_tail_id()) {
MDEBUG("Using cached template");
const uint64_t now = time(NULL);
@ -1539,7 +1540,7 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
expected_reward = m_btc_expected_reward;
return true;
}
MDEBUG("Not using cached template: address " << (!memcmp(&miner_address, &m_btc_address, sizeof(cryptonote::account_public_address))) << ", nonce " << (m_btc_nonce == ex_nonce) << ", cookie " << (m_btc_pool_cookie == m_tx_pool.cookie()) << ", from_block " << (!!from_block));
MDEBUG("Not using cached template: address " << (!memcmp(&info.miner_address, &m_btc_address, sizeof(cryptonote::account_public_address))) << ", nonce " << (m_btc_nonce == ex_nonce) << ", cookie " << (m_btc_pool_cookie == m_tx_pool.cookie()) << ", from_block " << (!!from_block));
invalidate_block_template_cache();
}
@ -1633,7 +1634,10 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
*/
//make blocks coin-base tx looks close to real coinbase tx to get truthful blob weight
uint8_t hf_version = b.major_version;
auto miner_tx_context = loki_miner_tx_context::miner_block(m_nettype, miner_address, m_service_node_list.get_block_leader());
auto miner_tx_context =
info.is_miner
? loki_miner_tx_context::miner_block(m_nettype, info.miner_address, m_service_node_list.get_block_leader())
: loki_miner_tx_context::pulse_block(m_nettype, info.service_node_payout, m_service_node_list.get_block_leader());
if (!calc_batched_governance_reward(height, miner_tx_context.batched_governance))
{
LOG_ERROR("Failed to calculate batched governance reward");
@ -1678,16 +1682,36 @@ bool Blockchain::create_block_template(block& b, const crypto::hash *from_block,
CHECK_AND_ASSERT_MES(cumulative_weight == txs_weight + get_transaction_weight(b.miner_tx), false, "unexpected case: cumulative_weight=" << cumulative_weight << " is not equal txs_cumulative_weight=" << txs_weight << " + get_transaction_weight(b.miner_tx)=" << get_transaction_weight(b.miner_tx));
if (!from_block)
cache_block_template(b, miner_address, ex_nonce, diffic, height, expected_reward, pool_cookie);
cache_block_template(b, info.miner_address, ex_nonce, diffic, height, expected_reward, pool_cookie);
return true;
}
LOG_ERROR("Failed to create_block_template with " << 10 << " tries");
return false;
}
//------------------------------------------------------------------
bool Blockchain::create_next_block_template(block& b, const account_public_address& miner_address, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
bool Blockchain::create_miner_block_template(block& b, const crypto::hash *from_block, const account_public_address& miner_address, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
{
return create_block_template(b, NULL, miner_address, diffic, height, expected_reward, ex_nonce);
block_template_info info = {};
info.is_miner = true;
info.miner_address = miner_address;
return create_block_template_internal(b, from_block, info, diffic, height, expected_reward, ex_nonce);
}
//------------------------------------------------------------------
bool Blockchain::create_next_miner_block_template(block& b, const account_public_address& miner_address, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
{
block_template_info info = {};
info.is_miner = true;
info.miner_address = miner_address;
return create_block_template_internal(b, NULL /*from_block*/, info, diffic, height, expected_reward, ex_nonce);
}
//------------------------------------------------------------------
bool Blockchain::create_next_pulse_block_template(block& b, const service_nodes::payout& block_producer, uint64_t& height, uint64_t& expected_reward)
{
block_template_info info = {};
info.service_node_payout = block_producer;
uint64_t diffic = 0;
blobdata nonce = {};
return create_block_template_internal(b, NULL /*from_block*/, info, diffic, height, expected_reward, nonce);
}
//------------------------------------------------------------------
// for an alternate chain, get the timestamps from the main chain to complete
@ -4307,7 +4331,7 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
}
abort_block.cancel();
if (bl.signatures.size())
if (bl.signatures.size() == service_nodes::PULSE_BLOCK_REQUIRED_SIGNATURES)
{
MINFO("+++++ PULSE BLOCK SUCCESSFULLY ADDED\n"
<< "id:\t" << id << "\n"
@ -4319,6 +4343,7 @@ bool Blockchain::handle_block_to_main_chain(const block& bl, const crypto::hash&
}
else
{
assert(bl.signatures.empty() && "Signatures were supposted to be checked in Service Node List already.");
MINFO("+++++ MINER BLOCK SUCCESSFULLY ADDED\n"
<< "id:\t" << id << "\n"
<< "PoW:\t" << proof_of_work << "\n"

View File

@ -355,8 +355,9 @@ namespace cryptonote
*
* @return true if block template filled in successfully, else false
*/
bool create_next_block_template(block& b, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
bool create_block_template(block& b, const crypto::hash *from_block, const account_public_address& miner_address, difficulty_type& di, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
bool create_miner_block_template (block& b, const crypto::hash *from_block, const account_public_address& miner_address, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
bool create_next_miner_block_template(block& b, const account_public_address& miner_address, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
bool create_next_pulse_block_template(block& b, const service_nodes::payout& block_producer, uint64_t& height, uint64_t& expected_reward);
/**
* @brief checks if a block is known about with a given hash
@ -1061,6 +1062,15 @@ namespace cryptonote
private:
#endif
struct block_template_info
{
bool is_miner;
account_public_address miner_address;
service_nodes::payout service_node_payout;
};
bool create_block_template_internal(block& b, const crypto::hash *from_block, block_template_info const &info, difficulty_type& di, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
bool load_missing_blocks_into_loki_subsystems();
// TODO: evaluate whether or not each of these typedefs are left over from blockchain_storage

View File

@ -1914,14 +1914,14 @@ namespace cryptonote
m_quorum_cop.set_votes_relayed(votes);
}
//-----------------------------------------------------------------------------------------------
bool core::get_next_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
bool core::create_next_miner_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
{
return m_blockchain_storage.create_next_block_template(b, adr, diffic, height, expected_reward, ex_nonce);
return m_blockchain_storage.create_next_miner_block_template(b, adr, diffic, height, expected_reward, ex_nonce);
}
//-----------------------------------------------------------------------------------------------
bool core::get_block_template(block& b, const crypto::hash *prev_block, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
bool core::create_miner_block_template(block& b, const crypto::hash *prev_block, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce)
{
return m_blockchain_storage.create_block_template(b, prev_block, adr, diffic, height, expected_reward, ex_nonce);
return m_blockchain_storage.create_miner_block_template(b, prev_block, adr, diffic, height, expected_reward, ex_nonce);
}
//-----------------------------------------------------------------------------------------------
bool core::find_blockchain_supplement(const std::list<crypto::hash>& qblock_ids, NOTIFY_RESPONSE_CHAIN_ENTRY::request& resp) const

View File

@ -359,8 +359,8 @@ namespace cryptonote
*
* @note see Blockchain::create_block_template
*/
virtual bool get_next_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
virtual bool get_block_template(block& b, const crypto::hash *prev_block, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
virtual bool create_next_miner_block_template(block& b, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
virtual bool create_miner_block_template(block& b, const crypto::hash *prev_block, const account_public_address& adr, difficulty_type& diffic, uint64_t& height, uint64_t& expected_reward, const blobdata& ex_nonce);
/**
* @brief called when a transaction is relayed; return the hash of the parsed tx, or null_hash

View File

@ -3,11 +3,13 @@
#include "crypto/crypto.h"
#include "cryptonote_config.h"
#include "service_node_voting.h"
#include <chrono>
namespace service_nodes {
constexpr size_t PULSE_QUORUM_ENTROPY_LAG = 21; // How many blocks back from the tip of the Blockchain to source entropy for the Pulse quorums.
constexpr size_t PULSE_QUORUM_NUM_VALIDATORS = 0;
constexpr size_t PULSE_QUORUM_SIZE = PULSE_QUORUM_NUM_VALIDATORS + 1 /*Leader*/;
constexpr auto PULSE_TIME_PER_BLOCK = std::chrono::minutes(2);
constexpr size_t PULSE_QUORUM_ENTROPY_LAG = 21; // How many blocks back from the tip of the Blockchain to source entropy for the Pulse quorums.
constexpr size_t PULSE_QUORUM_NUM_VALIDATORS = 0;
constexpr size_t PULSE_QUORUM_SIZE = PULSE_QUORUM_NUM_VALIDATORS + 1 /*Leader*/;
constexpr size_t pulse_min_service_nodes(cryptonote::network_type nettype)
{

View File

@ -1616,7 +1616,7 @@ namespace cryptonote { namespace rpc {
if (!epee::string_tools::hex_to_pod(req.prev_block, prev_block))
throw rpc_error{ERROR_INTERNAL, "Invalid prev_block"};
}
if(!m_core.get_block_template(b, req.prev_block.empty() ? NULL : &prev_block, info.address, diff, res.height, res.expected_reward, blob_reserve))
if(!m_core.create_miner_block_template(b, req.prev_block.empty() ? NULL : &prev_block, info.address, diff, res.height, res.expected_reward, blob_reserve))
{
LOG_ERROR("Failed to create block template");
throw rpc_error{ERROR_INTERNAL, "Internal error: failed to create block template"};