diff --git a/src/block_template.cpp b/src/block_template.cpp index e1096a0..188c2ee 100644 --- a/src/block_template.cpp +++ b/src/block_template.cpp @@ -39,6 +39,7 @@ namespace p2pool { BlockTemplate::BlockTemplate(p2pool* pool) : m_pool(pool) , m_templateId(0) + , m_lastUpdated(seconds_since_epoch()) , m_blockHeaderSize(0) , m_minerTxOffsetInTemplate(0) , m_minerTxSize(0) @@ -110,6 +111,7 @@ BlockTemplate& BlockTemplate::operator=(const BlockTemplate& b) m_pool = b.m_pool; m_templateId = b.m_templateId; + m_lastUpdated = b.m_lastUpdated.load(); m_blockTemplateBlob = b.m_blockTemplateBlob; m_merkleTreeMainBranch = b.m_merkleTreeMainBranch; m_blockHeaderSize = b.m_blockHeaderSize; @@ -203,6 +205,7 @@ void BlockTemplate::update(const MinerData& data, const Mempool& mempool, Wallet } ++m_templateId; + m_lastUpdated = seconds_since_epoch(); // When block template generation fails for any reason auto use_old_template = [this]() { diff --git a/src/block_template.h b/src/block_template.h index 4a13189..f673a30 100644 --- a/src/block_template.h +++ b/src/block_template.h @@ -39,6 +39,7 @@ public: BlockTemplate& operator=(const BlockTemplate& b); void update(const MinerData& data, const Mempool& mempool, Wallet* miner_wallet); + uint64_t last_updated() const { return m_lastUpdated.load(); } bool get_difficulties(const uint32_t template_id, uint64_t& height, difficulty_type& mainchain_difficulty, difficulty_type& sidechain_difficulty) const; uint32_t get_hashing_blob(const uint32_t template_id, uint32_t extra_nonce, uint8_t (&blob)[128], uint64_t& height, difficulty_type& difficulty, difficulty_type& sidechain_difficulty, hash& seed_hash, size_t& nonce_offset) const; @@ -69,6 +70,7 @@ private: mutable uv_rwlock_t m_lock; uint32_t m_templateId; + std::atomic m_lastUpdated; std::vector m_blockTemplateBlob; std::vector m_merkleTreeMainBranch; diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 142b031..2022bc7 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -25,6 +25,7 @@ #include "block_cache.h" #include "json_rpc_request.h" #include "json_parsers.h" +#include "block_template.h" #include #include #include @@ -972,6 +973,7 @@ void P2PServer::on_timer() save_peer_list_async(); update_peer_connections(); check_zmq(); + check_block_template(); } void P2PServer::flush_cache() @@ -1106,6 +1108,19 @@ void P2PServer::check_zmq() } } +void P2PServer::check_block_template() +{ + if (!m_pool->side_chain().precalcFinished()) { + return; + } + + // Force update block template every 30 seconds after the initial sync is done + if (seconds_since_epoch() >= m_pool->block_template().last_updated() + 30) { + LOGINFO(4, "block template is 30 seconds old, updating it"); + m_pool->update_block_template_async(); + } +} + P2PServer::P2PClient::P2PClient() : m_peerId(0) , m_expectedMessage(MessageId::HANDSHAKE_CHALLENGE) diff --git a/src/p2p_server.h b/src/p2p_server.h index 90972a2..da278c0 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -167,6 +167,7 @@ private: void flush_cache(); void download_missing_blocks(); void check_zmq(); + void check_block_template(); void update_peer_connections(); void update_peer_list(); void send_peer_list_request(P2PClient* client, uint64_t cur_time);