Don't re-check AuxPoW when sending data to peers

Checking scrypt PoW is expensive and needless in this case. All block
headers are already checked when they are accepted, and they will be
checked again on the receiving end.
This commit is contained in:
Shibe 2021-02-08 04:54:51 +04:00
parent d96b5daafa
commit ef9242b9ec
5 changed files with 22 additions and 22 deletions

View file

@ -10,7 +10,7 @@ using namespace std;
/* Moved here from the header, because we need auxpow and the logic /* Moved here from the header, because we need auxpow and the logic
becomes more involved. */ becomes more involved. */
CBlockHeader CBlockIndex::GetBlockHeader(const Consensus::Params& consensusParams) const CBlockHeader CBlockIndex::GetBlockHeader(const Consensus::Params& consensusParams, bool fCheckPOW) const
{ {
CBlockHeader block; CBlockHeader block;
@ -21,7 +21,7 @@ CBlockHeader CBlockIndex::GetBlockHeader(const Consensus::Params& consensusParam
have to read the actual *header*, not the full block. */ have to read the actual *header*, not the full block. */
if (block.IsAuxpow()) if (block.IsAuxpow())
{ {
ReadBlockHeaderFromDisk(block, this, consensusParams); ReadBlockHeaderFromDisk(block, this, consensusParams, fCheckPOW);
return block; return block;
} }

View file

@ -263,7 +263,7 @@ public:
return ret; return ret;
} }
CBlockHeader GetBlockHeader(const Consensus::Params& consensusParams) const; CBlockHeader GetBlockHeader(const Consensus::Params& consensusParams, bool fCheckPOW = true) const;
uint256 GetBlockHash() const uint256 GetBlockHash() const
{ {

View file

@ -1037,7 +1037,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
{ {
// Send block from disk // Send block from disk
CBlock block; CBlock block;
if (!ReadBlockFromDisk(block, (*mi).second, consensusParams)) if (!ReadBlockFromDisk(block, (*mi).second, consensusParams, false))
assert(!"cannot load block from disk"); assert(!"cannot load block from disk");
if (inv.type == MSG_BLOCK) if (inv.type == MSG_BLOCK)
connman.PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block)); connman.PushMessage(pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block));
@ -1705,7 +1705,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
} }
CBlock block; CBlock block;
bool ret = ReadBlockFromDisk(block, it->second, chainparams.GetConsensus(it->second->nHeight)); bool ret = ReadBlockFromDisk(block, it->second, chainparams.GetConsensus(it->second->nHeight), false);
assert(ret); assert(ret);
SendBlockTransactions(block, req, pfrom, connman); SendBlockTransactions(block, req, pfrom, connman);
@ -1748,7 +1748,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->id); LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->id);
for (; pindex; pindex = chainActive.Next(pindex)) for (; pindex; pindex = chainActive.Next(pindex))
{ {
vHeaders.push_back(pindex->GetBlockHeader(chainparams.GetConsensus(pindex->nHeight))); vHeaders.push_back(pindex->GetBlockHeader(chainparams.GetConsensus(pindex->nHeight), false));
if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop) if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
break; break;
} }
@ -2974,14 +2974,14 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
pBestIndex = pindex; pBestIndex = pindex;
if (fFoundStartingHeader) { if (fFoundStartingHeader) {
// add this to the headers message // add this to the headers message
vHeaders.push_back(pindex->GetBlockHeader(consensusParams)); vHeaders.push_back(pindex->GetBlockHeader(consensusParams, false));
} else if (PeerHasHeader(&state, pindex)) { } else if (PeerHasHeader(&state, pindex)) {
continue; // keep looking for the first new block continue; // keep looking for the first new block
} else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) { } else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) {
// Peer doesn't have this header but they do have the prior one. // Peer doesn't have this header but they do have the prior one.
// Start sending headers. // Start sending headers.
fFoundStartingHeader = true; fFoundStartingHeader = true;
vHeaders.push_back(pindex->GetBlockHeader(consensusParams)); vHeaders.push_back(pindex->GetBlockHeader(consensusParams, false));
} else { } else {
// Peer doesn't have this header or the prior one -- nothing will // Peer doesn't have this header or the prior one -- nothing will
// connect, so bail out. // connect, so bail out.
@ -3014,7 +3014,7 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
} }
if (!fGotBlockFromCache) { if (!fGotBlockFromCache) {
CBlock block; CBlock block;
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams); bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams, false);
assert(ret); assert(ret);
CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness); CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock)); connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));

View file

@ -1142,7 +1142,7 @@ bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHea
both a block and its header. */ both a block and its header. */
template<typename T> template<typename T>
static bool ReadBlockOrHeader(T& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams) static bool ReadBlockOrHeader(T& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams, bool fCheckPOW)
{ {
block.SetNull(); block.SetNull();
@ -1160,16 +1160,16 @@ static bool ReadBlockOrHeader(T& block, const CDiskBlockPos& pos, const Consensu
} }
// Check the header // Check the header
if (!CheckAuxPowProofOfWork(block, consensusParams)) if (fCheckPOW && !CheckAuxPowProofOfWork(block, consensusParams))
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString()); return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
return true; return true;
} }
template<typename T> template<typename T>
static bool ReadBlockOrHeader(T& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams) static bool ReadBlockOrHeader(T& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool fCheckPOW)
{ {
if (!ReadBlockOrHeader(block, pindex->GetBlockPos(), consensusParams)) if (!ReadBlockOrHeader(block, pindex->GetBlockPos(), consensusParams, fCheckPOW))
return false; return false;
if (block.GetHash() != pindex->GetBlockHash()) if (block.GetHash() != pindex->GetBlockHash())
return error("ReadBlockOrHeader(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s", return error("ReadBlockOrHeader(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
@ -1177,19 +1177,19 @@ static bool ReadBlockOrHeader(T& block, const CBlockIndex* pindex, const Consens
return true; return true;
} }
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams) bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams, bool fCheckPOW)
{ {
return ReadBlockOrHeader(block, pos, consensusParams); return ReadBlockOrHeader(block, pos, consensusParams, fCheckPOW);
} }
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams) bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool fCheckPOW)
{ {
return ReadBlockOrHeader(block, pindex, consensusParams); return ReadBlockOrHeader(block, pindex, consensusParams, fCheckPOW);
} }
bool ReadBlockHeaderFromDisk(CBlockHeader& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams) bool ReadBlockHeaderFromDisk(CBlockHeader& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool fCheckPOW)
{ {
return ReadBlockOrHeader(block, pindex, consensusParams); return ReadBlockOrHeader(block, pindex, consensusParams, fCheckPOW);
} }
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams) CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)

View file

@ -476,9 +476,9 @@ public:
/** Functions for disk access for blocks */ /** Functions for disk access for blocks */
bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart); bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart);
bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams); bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams, bool fCheckPOW = true);
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams); bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool fCheckPOW = true);
bool ReadBlockHeaderFromDisk(CBlockHeader& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams); bool ReadBlockHeaderFromDisk(CBlockHeader& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool fCheckPOW = true);
/** Functions for validating blocks and updating the block tree */ /** Functions for validating blocks and updating the block tree */