Skip to content
Closed
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
27 changes: 25 additions & 2 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

static size_t vExtraTxnForCompactIt = 0;
static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompact GUARDED_BY(cs_main);
// Storage of transactions last validated as good by testproposedblock
static std::vector<std::pair<uint256, CTransactionRef>> vExtraTxnForCompactProp GUARDED_BY(cs_main);

static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]

Expand Down Expand Up @@ -603,6 +605,26 @@ void AddToCompactExtraTransactions(const CTransactionRef& tx)
vExtraTxnForCompactIt = (vExtraTxnForCompactIt + 1) % max_extra_txn;
}

void AddToCompactBlockProposal(const CTransactionRef& tx)
{
vExtraTxnForCompactProp.emplace_back(std::make_pair(tx->GetHashWithWitness(), tx));
}

void ClearCompactBlockProposal()
{
LogPrint("cmpctblock", "Wiping block proposal of cache size: %u", vExtraTxnForCompactProp.size());
vExtraTxnForCompactProp.clear();
}

std::vector<std::pair<uint256, CTransactionRef>>& GetCompactBlockExtras()
{
if (!vExtraTxnForCompactProp.empty()) {
return vExtraTxnForCompactProp;
} else {
return vExtraTxnForCompact;
}
}

bool AddOrphanTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
const uint256& hash = tx->GetHash();
Expand Down Expand Up @@ -2052,7 +2074,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
}

PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
ReadStatus status = partialBlock.InitData(cmpctblock, GetCompactBlockExtras());
if (status == READ_STATUS_INVALID) {
MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
Misbehaving(pfrom->GetId(), 100);
Expand Down Expand Up @@ -2082,13 +2104,14 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
}
} else {

// This block is either already in flight from a different
// peer, or this peer has too many blocks outstanding to
// download from.
// Optimistically try to reconstruct anyway since we might be
// able to without any round trips.
PartiallyDownloadedBlock tempBlock(&mempool);
ReadStatus status = tempBlock.InitData(cmpctblock, vExtraTxnForCompact);
ReadStatus status = tempBlock.InitData(cmpctblock, GetCompactBlockExtras());
if (status != READ_STATUS_OK) {
// TODO: don't ignore failures
return true;
Expand Down
9 changes: 9 additions & 0 deletions src/rpc/mining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ std::string gbt_vb_name(const Consensus::DeploymentPos pos) {
return s;
}

extern void AddToCompactBlockProposal(const CTransactionRef& tx);
extern void ClearCompactBlockProposal();

UniValue testproposedblock(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
Expand Down Expand Up @@ -380,6 +383,12 @@ UniValue testproposedblock(const JSONRPCRequest& request)
throw JSONRPCError(RPC_VERIFY_ERROR, strRejectReason);
}

// Cache the block; we're going to almost certainly use the contents
ClearCompactBlockProposal();
for (auto& transaction : block.vtx) {
AddToCompactBlockProposal(transaction);
}

const CChainParams& chainparams = Params();
if ((!request.params[1].isNull() && !request.params[1].get_bool()) ||
(request.params[1].isNull() && !GetBoolArg("-acceptnonstdtxn", !chainparams.RequireStandard()))) {
Expand Down
4 changes: 4 additions & 0 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3074,6 +3074,8 @@ struct ConnectTrace {
std::vector<std::pair<CBlockIndex*, std::shared_ptr<const CBlock> > > blocksConnected;
};

extern void ClearCompactBlockProposal();

/**
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
* corresponding to pindexNew, to bypass loading it again from disk.
Expand Down Expand Up @@ -3132,6 +3134,8 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,

return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
}
// We connected a block, clear out the proposal cache as it's likely useless
ClearCompactBlockProposal();
nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
bool flushed = view.Flush();
Expand Down