diff --git a/src/Makefile.am b/src/Makefile.am index 36da0796745..654b322d338 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -200,12 +200,12 @@ BITCOIN_CORE_H = \ rpc/blockchain.h \ rpc/client.h \ rpc/mining.h \ - rpc/net.h \ rpc/protocol.h \ rpc/rawtransaction_util.h \ rpc/register.h \ rpc/request.h \ rpc/server.h \ + rpc/server_util.h \ rpc/util.h \ scheduler.h \ script/descriptor.h \ @@ -361,6 +361,7 @@ libbitcoin_server_a_SOURCES = \ rpc/net.cpp \ rpc/rawtransaction.cpp \ rpc/server.cpp \ + rpc/server_util.cpp \ script/sigcache.cpp \ shutdown.cpp \ signet.cpp \ diff --git a/src/net_processing.cpp b/src/net_processing.cpp index f352246b0d6..e4b8e9b6a93 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -312,6 +312,7 @@ public: /** Implement PeerManager */ void StartScheduledTasks(CScheduler& scheduler) override; void CheckForStaleTipAndEvictPeers() override; + bool FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& index) override; bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override; bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; } void SendPings() override; @@ -1427,6 +1428,41 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex) (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT); } +bool PeerManagerImpl::FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& index) +{ + if (fImporting || fReindex) return false; + + LOCK(cs_main); + // Ensure this peer exists and hasn't been disconnected + CNodeState* state = State(id); + if (state == nullptr) return false; + // Ignore pre-segwit peers + if (!state->fHaveWitness) return false; + + // Mark block as in-flight unless it already is + if (!BlockRequested(id, index)) return false; + + // Construct message to request the block + std::vector invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, hash)}; + + // Send block request message to the peer + bool success = m_connman.ForNode(id, [this, &invs](CNode* node) { + const CNetMsgMaker msgMaker(node->GetCommonVersion()); + this->m_connman.PushMessage(node, msgMaker.Make(NetMsgType::GETDATA, invs)); + return true; + }); + + if (success) { + LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n", + hash.ToString(), id); + } else { + RemoveBlockRequest(hash); + LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n", + hash.ToString(), id); + } + return success; +} + std::unique_ptr PeerManager::make(const CChainParams& chainparams, CConnman& connman, AddrMan& addrman, BanMan* banman, ChainstateManager& chainman, CTxMemPool& pool, bool ignore_incoming_txs) diff --git a/src/net_processing.h b/src/net_processing.h index 27bc40687a0..6c18e8ddfa2 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -42,6 +42,16 @@ public: CTxMemPool& pool, bool ignore_incoming_txs); virtual ~PeerManager() { } + /** + * Attempt to manually fetch block from a given peer. We must already have the header. + * + * @param[in] id The peer id + * @param[in] hash The block hash + * @param[in] pindex The blockindex + * @returns Whether a request was successfully made + */ + virtual bool FetchBlock(NodeId id, const uint256& hash, const CBlockIndex& pindex) = 0; + /** Begin running background tasks, should only be called once */ virtual void StartScheduledTasks(CScheduler& scheduler) = 0; diff --git a/src/rest.cpp b/src/rest.cpp index 3746fd752a5..2c0ead26c74 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index df580520164..9427a9fc596 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include #include @@ -30,6 +32,7 @@ #include #include #include +#include #include #include