Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ BITCOIN_TESTS =\
test/governance_budget_tests.cpp \
test/governance_validators_tests.cpp \
test/governance_vote_sync_tests.cpp \
test/governance_request_tests.cpp \
test/governance_vote_wire_tests.cpp \
test/coinjoin_inouts_tests.cpp \
test/hash_tests.cpp \
Expand Down Expand Up @@ -359,6 +360,7 @@ test_fuzz_fuzz_SOURCES = \
test/fuzz/parse_numbers.cpp \
test/fuzz/parse_script.cpp \
test/fuzz/parse_univalue.cpp \
test/fuzz/partially_downloaded_block.cpp \
test/fuzz/policy_estimator.cpp \
test/fuzz/policy_estimator_io.cpp \
test/fuzz/poolresource.cpp \
Expand Down
13 changes: 4 additions & 9 deletions src/blockencodings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,15 +203,10 @@ ReadStatus PartiallyDownloadedBlock::FillBlock(CBlock& block, const std::vector<
if (vtx_missing.size() != tx_missing_offset)
return READ_STATUS_INVALID;

BlockValidationState state;
if (!CheckBlock(block, state, Params().GetConsensus())) {
// TODO: We really want to just check merkle tree manually here,
// but that is expensive, and CheckBlock caches a block's
// "checked-status" (in the CBlock?). CBlock should be able to
// check its own merkle root and cache that check.
if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED)
return READ_STATUS_FAILED; // Possible Short ID collision
return READ_STATUS_CHECKBLOCK_FAILED;
// Check for possible mutations early now that we have a seemingly good block
IsBlockMutatedFn check_mutated{m_check_block_mutated_mock ? m_check_block_mutated_mock : IsBlockMutated};
if (check_mutated(/*block=*/block)) {
return READ_STATUS_FAILED; // Possible Short ID collision
}

LogPrint(BCLog::CMPCTBLOCK, "Successfully reconstructed block %s with %lu txn prefilled, %lu txn from mempool (incl at least %lu from extra pool) and %lu txn requested\n", hash.ToString(), prefilled_count, mempool_count, extra_count, vtx_missing.size());
Expand Down
12 changes: 10 additions & 2 deletions src/blockencodings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@

#include <primitives/block.h>

#include <functional>

class CTxMemPool;
class BlockValidationState;
namespace Consensus {
struct Params;
};

// Transaction compression schemes for compact block relay can be introduced by writing
// an actual formatter here.
Expand Down Expand Up @@ -79,8 +84,6 @@ typedef enum ReadStatus_t
READ_STATUS_OK,
READ_STATUS_INVALID, // Invalid object, peer is sending bogus crap
READ_STATUS_FAILED, // Failed to process object
READ_STATUS_CHECKBLOCK_FAILED, // Used only by FillBlock to indicate a
// failure in CheckBlock.
} ReadStatus;

class CBlockHeaderAndShortTxIDs {
Expand Down Expand Up @@ -131,6 +134,11 @@ class PartiallyDownloadedBlock {
public:
CBlockHeader header;
std::vector<unsigned char> vchBlockSig;

// Can be overridden for fuzz testing.
using IsBlockMutatedFn = std::function<bool(const CBlock&)>;
IsBlockMutatedFn m_check_block_mutated_mock{nullptr};

explicit PartiallyDownloadedBlock(CTxMemPool* poolIn) : pool(poolIn) {}

// extra_txn is a list of extra transactions to look at, in <hash, reference> form
Expand Down
50 changes: 10 additions & 40 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, Pe

uint256 nHash = govobj.GetHash();

WITH_LOCK(::cs_main, peerman.EraseObjectRequest(peer.GetId(), CInv(MSG_GOVERNANCE_OBJECT, nHash)));

if (!m_mn_sync.IsBlockchainSynced()) {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode list not synced\n");
Expand All @@ -277,7 +276,10 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, Pe

LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received object: %s\n", strHash);

if (!AcceptMessage(nHash)) {
// Consume after the sync gate, so a dropped response can be retransmitted after sync.
const bool announced_or_requested = WITH_LOCK(
::cs_main, return peerman.ConsumeObjectRequest(peer.GetId(), CInv{MSG_GOVERNANCE_OBJECT, nHash}));
if (!announced_or_requested) {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received unrequested object: %s\n", strHash);
return {};
}
Expand Down Expand Up @@ -333,7 +335,6 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, Pe
}

uint256 nHash = vote.GetHash();
WITH_LOCK(::cs_main, peerman.EraseObjectRequest(peer.GetId(), CInv(MSG_GOVERNANCE_OBJECT_VOTE, nHash)));

// Ignore such messages until masternode list is synced
if (!m_mn_sync.IsBlockchainSynced()) {
Expand All @@ -345,7 +346,10 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, Pe

std::string strHash = nHash.ToString();

if (!AcceptMessage(nHash)) {
// Consume after the sync gate, so a dropped response can be retransmitted after sync.
const bool announced_or_requested = WITH_LOCK(
::cs_main, return peerman.ConsumeObjectRequest(peer.GetId(), CInv{MSG_GOVERNANCE_OBJECT_VOTE, nHash}));
if (!announced_or_requested) {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Received unrequested vote object: %s, hash: %s, peer = %d\n",
vote.ToString(tip_mn_list), strHash, peer.GetId());
return {};
Expand Down Expand Up @@ -558,18 +562,7 @@ void CGovernanceManager::CheckAndRemove()
}
}

// forget about expired requests
auto r_it = m_requested_hash_time.begin();
while (r_it != m_requested_hash_time.end()) {
if (r_it->second < std::chrono::seconds(nNow)) {
m_requested_hash_time.erase(r_it++);
} else {
++r_it;
}
}

LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdateCachesAndClean -- %s, m_requested_hash_time size=%d\n",
ToString(), m_requested_hash_time.size());
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdateCachesAndClean -- %s\n", ToString());
}

const CGovernanceObject* CGovernanceManager::FindConstGovernanceObject(const uint256& nHash) const
Expand Down Expand Up @@ -991,16 +984,7 @@ bool CGovernanceManager::ConfirmInventoryRequest(const CInv& inv)
return false;
}

const auto valid_until = GetTime<std::chrono::seconds>() + std::chrono::seconds(RELIABLE_PROPAGATION_TIME);
const auto& [_itr, inserted] = m_requested_hash_time.emplace(inv.hash, valid_until);

if (inserted) {
LogPrint(BCLog::GOBJECT, /* Continued */
"CGovernanceManager::ConfirmInventoryRequest added %s inv hash to m_requested_hash_time, size=%d\n",
inv.type == MSG_GOVERNANCE_OBJECT ? "object" : "vote", m_requested_hash_time.size());
}

LogPrint(BCLog::GOBJECT, "CGovernanceManager::ConfirmInventoryRequest reached end, returning true\n");
// The bounded net-layer per-peer tracker records the request and authorizes the response.
return true;
}

Expand Down Expand Up @@ -1481,19 +1465,6 @@ int CGovernanceManager::RequestGovernanceObjectVotes(const std::vector<CNode*>&
return int(vTriggerObjHashes.size() + vOtherObjHashes.size());
}

bool CGovernanceManager::AcceptMessage(const uint256& nHash)
{
LOCK(cs);
auto it = m_requested_hash_time.find(nHash);
if (it == m_requested_hash_time.end()) {
// We never requested this
return false;
}
// Only accept one response
m_requested_hash_time.erase(it);
return true;
}

void CGovernanceManager::RebuildIndexes()
{
LOCK(cs);
Expand Down Expand Up @@ -1725,7 +1696,6 @@ void CGovernanceManager::RemoveInvalidVotes()
for (auto& voteHash : removed) {
cmapVoteToObject.Erase(voteHash);
cmmapOrphanVotes.Erase(voteHash);
m_requested_hash_time.erase(voteHash);
}
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/governance/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ class CGovernanceManager : public GovernanceStore
int nCachedBlockHeight;
std::map<uint256, CGovernanceObject> mapPostponedObjects;
std::set<uint256> setAdditionalRelayObjects;
std::map<uint256, std::chrono::seconds> m_requested_hash_time;
bool fRateChecksEnabled;
std::optional<uint256> votedFundingYesTriggerHash;
std::map<uint256, std::shared_ptr<CSuperblock>> mapTrigger;
Expand Down Expand Up @@ -401,8 +400,6 @@ class CGovernanceManager : public GovernanceStore

bool ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman);

/// Called to indicate a requested object or vote has been received
bool AcceptMessage(const uint256& nHash);

void CheckOrphanVotes(CGovernanceObject& govobj, PeerManager& peerman);

Expand Down
2 changes: 2 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,9 @@ class CNodeStats
int nVersion;
std::string cleanSubVer;
bool fInbound;
// We requested high bandwidth connection to peer
bool m_bip152_highbandwidth_to;
// Peer requested high bandwidth connection
bool m_bip152_highbandwidth_from;
int m_starting_height;
uint64_t nSendBytes;
Expand Down
Loading
Loading