diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -60,11 +60,19 @@ /** update statistics (does not update nSize) */ void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) { - if (nBlocks == 0 || nHeightFirst > nHeightIn) nHeightFirst = nHeightIn; - if (nBlocks == 0 || nTimeFirst > nTimeIn) nTimeFirst = nTimeIn; + if (nBlocks == 0 || nHeightFirst > nHeightIn) { + nHeightFirst = nHeightIn; + } + if (nBlocks == 0 || nTimeFirst > nTimeIn) { + nTimeFirst = nTimeIn; + } nBlocks++; - if (nHeightIn > nHeightLast) nHeightLast = nHeightIn; - if (nTimeIn > nTimeLast) nTimeLast = nTimeIn; + if (nHeightIn > nHeightLast) { + nHeightLast = nHeightIn; + } + if (nTimeIn > nTimeLast) { + nTimeLast = nTimeIn; + } } }; @@ -271,7 +279,9 @@ CBlockHeader GetBlockHeader() const { CBlockHeader block; block.nVersion = nVersion; - if (pprev) block.hashPrevBlock = pprev->GetBlockHash(); + if (pprev) { + block.hashPrevBlock = pprev->GetBlockHash(); + } block.hashMerkleRoot = hashMerkleRoot; block.nTime = nTime; block.nBits = nBits; @@ -294,8 +304,9 @@ const CBlockIndex *pindex = this; for (int i = 0; i < nMedianTimeSpan && pindex; - i++, pindex = pindex->pprev) + i++, pindex = pindex->pprev) { *(--pbegin) = pindex->GetBlockTime(); + } std::sort(pbegin, pend); return pbegin[(pend - pbegin) / 2]; @@ -312,7 +323,9 @@ bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const { // Only validity flags allowed. assert(!(nUpTo & ~BLOCK_VALID_MASK)); - if (nStatus & BLOCK_FAILED_MASK) return false; + if (nStatus & BLOCK_FAILED_MASK) { + return false; + } return ((nStatus & BLOCK_VALID_MASK) >= nUpTo); } @@ -321,7 +334,9 @@ bool RaiseValidity(enum BlockStatus nUpTo) { // Only validity flags allowed. assert(!(nUpTo & ~BLOCK_VALID_MASK)); - if (nStatus & BLOCK_FAILED_MASK) return false; + if (nStatus & BLOCK_FAILED_MASK) { + return false; + } if ((nStatus & BLOCK_VALID_MASK) < nUpTo) { nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo; return true; @@ -362,15 +377,22 @@ template inline void SerializationOp(Stream &s, Operation ser_action) { int nVersion = s.GetVersion(); - if (!(s.GetType() & SER_GETHASH)) READWRITE(VARINT(nVersion)); + if (!(s.GetType() & SER_GETHASH)) { + READWRITE(VARINT(nVersion)); + } READWRITE(VARINT(nHeight)); READWRITE(VARINT(nStatus)); READWRITE(VARINT(nTx)); - if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO)) + if (nStatus & (BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO)) { READWRITE(VARINT(nFile)); - if (nStatus & BLOCK_HAVE_DATA) READWRITE(VARINT(nDataPos)); - if (nStatus & BLOCK_HAVE_UNDO) READWRITE(VARINT(nUndoPos)); + } + if (nStatus & BLOCK_HAVE_DATA) { + READWRITE(VARINT(nDataPos)); + } + if (nStatus & BLOCK_HAVE_UNDO) { + READWRITE(VARINT(nUndoPos)); + } // block header READWRITE(this->nVersion); @@ -427,7 +449,9 @@ * if no such height exists. */ CBlockIndex *operator[](int nHeight) const { - if (nHeight < 0 || nHeight >= (int)vChain.size()) return nullptr; + if (nHeight < 0 || nHeight >= (int)vChain.size()) { + return nullptr; + } return vChain[nHeight]; } @@ -447,10 +471,11 @@ * index is not found or is the tip. */ CBlockIndex *Next(const CBlockIndex *pindex) const { - if (Contains(pindex)) - return (*this)[pindex->nHeight + 1]; - else + if (!Contains(pindex)) { return nullptr; + } + + return (*this)[pindex->nHeight + 1]; } /** diff --git a/src/chain.cpp b/src/chain.cpp --- a/src/chain.cpp +++ b/src/chain.cpp @@ -13,6 +13,7 @@ vChain.clear(); return; } + vChain.resize(pindex->nHeight + 1); while (pindex && vChain[pindex->nHeight] != pindex) { vChain[pindex->nHeight] = pindex; @@ -25,11 +26,15 @@ std::vector vHave; vHave.reserve(32); - if (!pindex) pindex = Tip(); + if (!pindex) { + pindex = Tip(); + } while (pindex) { vHave.push_back(pindex->GetBlockHash()); // Stop when we have added the genesis block. - if (pindex->nHeight == 0) break; + if (pindex->nHeight == 0) { + break; + } // Exponentially larger steps back, plus the genesis block. int nHeight = std::max(pindex->nHeight - nStep, 0); if (Contains(pindex)) { @@ -39,7 +44,9 @@ // Otherwise, use O(log n) skiplist. pindex = pindex->GetAncestor(nHeight); } - if (vHave.size() > 10) nStep *= 2; + if (vHave.size() > 10) { + nStep *= 2; + } } return CBlockLocator(vHave); @@ -49,9 +56,12 @@ if (pindex == nullptr) { return nullptr; } - if (pindex->nHeight > Height()) pindex = pindex->GetAncestor(Height()); - while (pindex && !Contains(pindex)) + if (pindex->nHeight > Height()) { + pindex = pindex->GetAncestor(Height()); + } + while (pindex && !Contains(pindex)) { pindex = pindex->pprev; + } return pindex; } @@ -72,7 +82,9 @@ /** Compute what height to jump back to with the CBlockIndex::pskip pointer. */ static inline int GetSkipHeight(int height) { - if (height < 2) return 0; + if (height < 2) { + return 0; + } // Determine which height to jump back to. Any number strictly lower than // height is acceptable, but the following expression seems to perform well @@ -82,7 +94,9 @@ } CBlockIndex *CBlockIndex::GetAncestor(int height) { - if (height > nHeight || height < 0) return nullptr; + if (height > nHeight || height < 0) { + return nullptr; + } CBlockIndex *pindexWalk = this; int heightWalk = nHeight; @@ -110,7 +124,9 @@ } void CBlockIndex::BuildSkip() { - if (pprev) pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); + if (pprev) { + pskip = pprev->GetAncestor(GetSkipHeight(nHeight)); + } } arith_uint256 GetBlockProof(const CBlockIndex &block) { @@ -118,7 +134,9 @@ bool fNegative; bool fOverflow; bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow); - if (fNegative || fOverflow || bnTarget == 0) return 0; + if (fNegative || fOverflow || bnTarget == 0) { + return 0; + } // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256 // as it's too large for a arith_uint256. However, as 2**256 is at least as // large as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / diff --git a/src/chainparams.cpp b/src/chainparams.cpp --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -494,15 +494,20 @@ } CChainParams &Params(const std::string &chain) { - if (chain == CBaseChainParams::MAIN) + if (chain == CBaseChainParams::MAIN) { return mainParams; - else if (chain == CBaseChainParams::TESTNET) + } + + if (chain == CBaseChainParams::TESTNET) { return testNetParams; - else if (chain == CBaseChainParams::REGTEST) + } + + if (chain == CBaseChainParams::REGTEST) { return regTestParams; - else - throw std::runtime_error( - strprintf("%s: Unknown chain %s.", __func__, chain)); + } + + throw std::runtime_error( + strprintf("%s: Unknown chain %s.", __func__, chain)); } void SelectParams(const std::string &network) { diff --git a/src/netbase.cpp b/src/netbase.cpp --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -79,10 +79,11 @@ portOut = n; } } - if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') + if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') { hostOut = in.substr(1, in.size() - 2); - else + } else { hostOut = in; + } } static bool LookupIntern(const char *pszName, std::vector &vIP, diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -491,7 +491,7 @@ rawTx.nLockTime = nLockTime; } - for (unsigned int idx = 0; idx < inputs.size(); idx++) { + for (size_t idx = 0; idx < inputs.size(); idx++) { const UniValue &input = inputs[idx]; const UniValue &o = input.get_obj(); @@ -521,13 +521,12 @@ throw JSONRPCError( RPC_INVALID_PARAMETER, "Invalid parameter, sequence number is out of range"); - } else { - nSequence = (uint32_t)seqNr64; } + + nSequence = uint32_t(seqNr64); } CTxIn in(COutPoint(txid, nOutput), CScript(), nSequence); - rawTx.vin.push_back(in); } diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -449,10 +449,11 @@ } uint64_t nSigOps = 0; - for (unsigned int i = 0; i < tx.vin.size(); i++) { - const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]); - if (prevout.scriptPubKey.IsPayToScriptHash()) - nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig); + for (auto &i : tx.vin) { + const CTxOut &prevout = inputs.GetOutputFor(i); + if (prevout.scriptPubKey.IsPayToScriptHash()) { + nSigOps += prevout.scriptPubKey.GetSigOpCount(i.scriptSig); + } } return nSigOps; } @@ -848,9 +849,11 @@ return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee)); - } else if (GetBoolArg("-relaypriority", DEFAULT_RELAYPRIORITY) && - nModifiedFees < ::minRelayTxFee.GetFee(nSize) && - !AllowFree(entry.GetPriority(chainActive.Height() + 1))) { + } + + if (GetBoolArg("-relaypriority", DEFAULT_RELAYPRIORITY) && + nModifiedFees < ::minRelayTxFee.GetFee(nSize) && + !AllowFree(entry.GetPriority(chainActive.Height() + 1))) { // Require that free transactions have sufficient priority to be // mined in the next block. return state.DoS(0, false, REJECT_INSUFFICIENTFEE, @@ -870,7 +873,7 @@ LOCK(csFreeLimiter); // Use an exponentially decaying ~10-minute window: - dFreeCount *= pow(1.0 - 1.0 / 600.0, (double)(nNow - nLastTime)); + dFreeCount *= pow(1.0 - 1.0 / 600.0, double(nNow - nLastTime)); nLastTime = nNow; // -limitfreerelay unit is thousand-bytes-per-minute // At default rate it would take over a month to fill 1GB @@ -1271,10 +1274,9 @@ // should be detected by both). We define it this way because it allows us // to only store the highest fork tip (+ base) which meets the 7-block // condition and from this always have the most-likely-to-cause-warning fork - if (pfork && - (!pindexBestForkTip || - (pindexBestForkTip && - pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) && + if (pfork && (!pindexBestForkTip || + (pindexBestForkTip && + pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) && pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) && chainActive.Height() - pindexNewForkTip->nHeight < 72) { @@ -1389,10 +1391,10 @@ } if (nValueIn < tx.GetValueOut()) { - return state.DoS( - 100, false, REJECT_INVALID, "bad-txns-in-belowout", false, - strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), - FormatMoney(tx.GetValueOut()))); + return state.DoS(100, false, REJECT_INVALID, "bad-txns-in-belowout", + false, strprintf("value in (%s) < value out (%s)", + FormatMoney(nValueIn), + FormatMoney(tx.GetValueOut()))); } // Tally transaction fees @@ -2025,9 +2027,8 @@ if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) { return state.DoS( - 100, - error("%s: contains a non-BIP68-final transaction", - __func__), + 100, error("%s: contains a non-BIP68-final transaction", + __func__), REJECT_INVALID, "bad-txns-nonfinal"); } } @@ -2077,9 +2078,8 @@ int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2; - LogPrint("bench", - " - Connect %u transactions: %.2fms (%.3fms/tx, " - "%.3fms/txin) [%.2fs]\n", + LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, " + "%.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime3 - nTime2), 0.001 * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime3 - nTime2) / (nInputs - 1), @@ -2088,10 +2088,9 @@ CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus()); if (block.vtx[0]->GetValueOut() > blockReward) { - return state.DoS(100, - error("ConnectBlock(): coinbase pays too much " - "(actual=%d vs limit=%d)", - block.vtx[0]->GetValueOut(), blockReward), + return state.DoS(100, error("ConnectBlock(): coinbase pays too much " + "(actual=%d vs limit=%d)", + block.vtx[0]->GetValueOut(), blockReward), REJECT_INVALID, "bad-cb-amount"); } @@ -2805,8 +2804,9 @@ std::shared_ptr nullBlockPtr; if (!ActivateBestChainStep( config, state, pindexMostWork, - pblock && pblock->GetHash() == - pindexMostWork->GetBlockHash() + pblock && + pblock->GetHash() == + pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace)) @@ -3507,8 +3507,9 @@ } assert(pindexPrev); - if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint( - pindexPrev, state, chainparams, hash)) { + if (fCheckpointsEnabled && + !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, + hash)) { return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str()); } @@ -3909,9 +3910,8 @@ } } - LogPrint("prune", - "Prune: target=%dMiB actual=%dMiB diff=%dMiB " - "max_prune_height=%d removed %d blk/rev pairs\n", + LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB " + "max_prune_height=%d removed %d blk/rev pairs\n", nPruneTarget / 1024 / 1024, nCurrentUsage / 1024 / 1024, ((int64_t)nPruneTarget - (int64_t)nCurrentUsage) / 1024 / 1024, nLastBlockWeCanPrune, count);