Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
Lint: Code style violation: '/home/jason/projects/bitcoin-abc/src/validation.cpp' has code style errors. | |||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Copyright (c) 2017 The Bitcoin developers | // Copyright (c) 2017 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include "validation.h" | #include "validation.h" | ||||
#include "arith_uint256.h" | #include "arith_uint256.h" | ||||
▲ Show 20 Lines • Show All 3,725 Lines • ▼ Show 20 Lines | static bool AcceptBlock(const Config &config, | ||||
if (!AcceptBlockHeader(config, block, state, &pindex)) { | if (!AcceptBlockHeader(config, block, state, &pindex)) { | ||||
return false; | return false; | ||||
} | } | ||||
// Try to process all requested blocks that we don't have, but only | // Try to process all requested blocks that we don't have, but only | ||||
// process an unrequested block if it's new and has enough work to | // process an unrequested block if it's new and has enough work to | ||||
// advance our tip, and isn't too many blocks ahead. | // advance our tip, and isn't too many blocks ahead. | ||||
bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA; | bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA; | ||||
// Compare block header timestamps and received times of the block and the chaintip. | |||||
// If they have the same chain height, use these diffs as a tie-breaker, attempting | |||||
// to pick the more honestly-mined block. | |||||
int64_t newBlockTimeDiff = pindex->GetHeaderTimeReceived() - pindex->GetBlockTime(); | |||||
int64_t chainTipTimeDiff = chainActive.Tip()->GetHeaderTimeReceived() - chainActive.Tip()->GetBlockTime(); | |||||
bool isSameHeightAndMoreHonestlyMined = (pindex->nChainWork == chainActive.Tip()->nChainWork) && (newBlockTimeDiff + SELFISH_MINER_HEADER_RECEIVE_TIME_THRESHOLD < chainTipTimeDiff); | |||||
if (isSameHeightAndMoreHonestlyMined) { | |||||
LogPrint(BCLog::HEADERTIMESTAMPS, "Chain tip timestamp-to-received-time difference: hash=%s, diff=%d\n", chainActive.Tip()->GetBlockHash().ToString(), chainTipTimeDiff); | |||||
LogPrint(BCLog::HEADERTIMESTAMPS, "New block timestamp-to-received-time difference: hash=%s, diff=%d\n", pindex->GetBlockHash().ToString(), newBlockTimeDiff); | |||||
} | |||||
bool fHasMoreWork = | bool fHasMoreWork = | ||||
(chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork | (chainActive.Tip() ? (pindex->nChainWork > chainActive.Tip()->nChainWork) || isSameHeightAndMoreHonestlyMined | ||||
: true); | : true); | ||||
// Blocks that are too out-of-order needlessly limit the effectiveness of | // Blocks that are too out-of-order needlessly limit the effectiveness of | ||||
// pruning, because pruning will not delete block files that contain any | // pruning, because pruning will not delete block files that contain any | ||||
// blocks which are too close in height to the tip. Apply this test | // blocks which are too close in height to the tip. Apply this test | ||||
// regardless of whether pruning is enabled; it should generally be safe to | // regardless of whether pruning is enabled; it should generally be safe to | ||||
// not process unrequested blocks. | // not process unrequested blocks. | ||||
bool fTooFarAhead = | bool fTooFarAhead = | ||||
(pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP)); | (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP)); | ||||
Show All 39 Lines | if (!CheckBlock(config, block, state) || | ||||
} | } | ||||
return error("%s: %s (block %s)", __func__, FormatStateMessage(state), | return error("%s: %s (block %s)", __func__, FormatStateMessage(state), | ||||
block.GetHash().ToString()); | block.GetHash().ToString()); | ||||
} | } | ||||
// Header is valid/has work and the merkle tree is good. | // Header is valid/has work and the merkle tree is good. | ||||
// Relay now, but if it does not build on our best tip, let the | // Relay now, but if it does not build on our best tip, let the | ||||
// SendMessages loop relay it. | // SendMessages loop relay it. | ||||
if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev) { | if (!IsInitialBlockDownload() && fHasMoreWork) { | ||||
GetMainSignals().NewPoWValidBlock(pindex, pblock); | GetMainSignals().NewPoWValidBlock(pindex, pblock); | ||||
} | } | ||||
int nHeight = pindex->nHeight; | int nHeight = pindex->nHeight; | ||||
const CChainParams &chainparams = config.GetChainParams(); | const CChainParams &chainparams = config.GetChainParams(); | ||||
// Write block to history file | // Write block to history file | ||||
try { | try { | ||||
Show All 14 Lines | try { | ||||
} | } | ||||
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | ||||
return error("AcceptBlock(): ReceivedBlockTransactions failed"); | return error("AcceptBlock(): ReceivedBlockTransactions failed"); | ||||
} | } | ||||
} catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
return AbortNode(state, std::string("System error: ") + e.what()); | return AbortNode(state, std::string("System error: ") + e.what()); | ||||
} | } | ||||
if (fHasMoreWork && isSameHeightAndMoreHonestlyMined) { | |||||
if (PreciousBlock(config, state, pindex)) { | |||||
LogPrint(BCLog::HEADERTIMESTAMPS, "More honestly broadcasted block has replaced chain tip (set as precious block): %s\n", pindex->GetBlockHash().ToString()); | |||||
} | |||||
} | |||||
if (fCheckForPruning) { | if (fCheckForPruning) { | ||||
// we just allocated more disk space for block files. | // we just allocated more disk space for block files. | ||||
FlushStateToDisk(config.GetChainParams(), state, FLUSH_STATE_NONE); | FlushStateToDisk(config.GetChainParams(), state, FLUSH_STATE_NONE); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 1,507 Lines • Show Last 20 Lines |
'/home/jason/projects/bitcoin-abc/src/validation.cpp' has code style errors.