Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 3,167 Lines • ▼ Show 20 Lines | if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) { | ||||
mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew)); | mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew)); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, | static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, | ||||
unsigned int nAddSize, unsigned int nHeight, | unsigned int nAddSize, unsigned int nHeight, | ||||
uint64_t nTime, bool fKnown = false) { | uint64_t nTime, bool fKnown = false) { | ||||
jasonbcox: Missing lines from e104f0f | |||||
LOCK(cs_LastBlockFile); | LOCK(cs_LastBlockFile); | ||||
unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile; | unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile; | ||||
if (vinfoBlockFile.size() <= nFile) { | if (vinfoBlockFile.size() <= nFile) { | ||||
vinfoBlockFile.resize(nFile + 1); | vinfoBlockFile.resize(nFile + 1); | ||||
} | } | ||||
if (!fKnown) { | if (!fKnown) { | ||||
▲ Show 20 Lines • Show All 515 Lines • ▼ Show 20 Lines | if (first_invalid != nullptr) { | ||||
} | } | ||||
} | } | ||||
} | } | ||||
NotifyHeaderTip(); | NotifyHeaderTip(); | ||||
return true; | return true; | ||||
} | } | ||||
/** Store block on disk. If dbp is non-nullptr, the file is known to already | |||||
* reside on disk */ | |||||
static CDiskBlockPos SaveBlockToDisk(CValidationState &state, | |||||
const CBlock &block, int nHeight, | |||||
const CChainParams &chainparams, | |||||
const CDiskBlockPos *dbp) { | |||||
unsigned int nBlockSize = | |||||
::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); | |||||
CDiskBlockPos blockPos; | |||||
if (dbp != nullptr) blockPos = *dbp; | |||||
if (!FindBlockPos(state, blockPos, nBlockSize + 8, nHeight, | |||||
block.GetBlockTime(), dbp != nullptr)) { | |||||
error("%s: FindBlockPos failed", __func__); | |||||
return CDiskBlockPos(); | |||||
} | |||||
if (dbp == nullptr) { | |||||
if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { | |||||
AbortNode("Failed to write block"); | |||||
return CDiskBlockPos(); | |||||
} | |||||
} | |||||
return blockPos; | |||||
} | |||||
/** | /** | ||||
* Store a block on disk. | * Store a block on disk. | ||||
* | * | ||||
* @param[in] config The global config. | * @param[in] config The global config. | ||||
* @param[in-out] pblock The block we want to accept. | * @param[in-out] pblock The block we want to accept. | ||||
* @param[in] fRequested A boolean to indicate if this block was requested | * @param[in] fRequested A boolean to indicate if this block was requested | ||||
* from our peers. | * from our peers. | ||||
* @param[in] dbp If non-null, the disk position of the block. | * @param[in] dbp If non-null, the disk position of the block. | ||||
▲ Show 20 Lines • Show All 122 Lines • ▼ Show 20 Lines | static bool AcceptBlock(const Config &config, | ||||
// 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() && chainActive.Tip() == pindex->pprev) { | ||||
GetMainSignals().NewPoWValidBlock(pindex, pblock); | GetMainSignals().NewPoWValidBlock(pindex, pblock); | ||||
} | } | ||||
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 { | ||||
unsigned int nBlockSize = | CDiskBlockPos blockPos = | ||||
::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); | SaveBlockToDisk(state, block, pindex->nHeight, chainparams, dbp); | ||||
CDiskBlockPos blockPos; | if (blockPos.IsNull()) { | ||||
if (dbp != nullptr) { | return state.Error(strprintf( | ||||
blockPos = *dbp; | "%s: Failed to find position to write new block to disk", | ||||
__func__)); | |||||
jasonbcoxAuthorUnsubmitted Not Done Inline Actionsis this equivalent to return false; as in the original PR? jasonbcox: is this equivalent to `return false;` as in the original PR? | |||||
} | } | ||||
if (!FindBlockPos(state, blockPos, nBlockSize + 8, nHeight, | |||||
block.GetBlockTime(), dbp != nullptr)) { | |||||
return error("AcceptBlock(): FindBlockPos failed"); | |||||
} | |||||
if (dbp == nullptr) { | |||||
if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { | |||||
AbortNode(state, "Failed to write block"); | |||||
} | |||||
} | |||||
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 (fCheckForPruning) { | if (fCheckForPruning) { | ||||
▲ Show 20 Lines • Show All 918 Lines • ▼ Show 20 Lines | bool LoadGenesisBlock(const CChainParams &chainparams) { | ||||
if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash())) { | if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash())) { | ||||
return true; | return true; | ||||
} | } | ||||
// Only add the genesis block if not reindexing (in which case we reuse the | // Only add the genesis block if not reindexing (in which case we reuse the | ||||
// one already on disk) | // one already on disk) | ||||
try { | try { | ||||
CBlock &block = const_cast<CBlock &>(chainparams.GenesisBlock()); | CBlock &block = const_cast<CBlock &>(chainparams.GenesisBlock()); | ||||
// Start new block file | |||||
unsigned int nBlockSize = | |||||
::GetSerializeSize(block, SER_DISK, CLIENT_VERSION); | |||||
CDiskBlockPos blockPos; | |||||
CValidationState state; | CValidationState state; | ||||
jasonbcoxAuthorUnsubmitted Not Done Inline ActionsThis line was moved in the original PR jasonbcox: This line was moved in the original PR | |||||
if (!FindBlockPos(state, blockPos, nBlockSize + 8, 0, | CDiskBlockPos blockPos = | ||||
block.GetBlockTime())) { | SaveBlockToDisk(state, block, 0, chainparams, nullptr); | ||||
return error("%s: FindBlockPos failed", __func__); | if (blockPos.IsNull()) { | ||||
} | |||||
if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { | |||||
return error("%s: writing genesis block to disk failed", __func__); | return error("%s: writing genesis block to disk failed", __func__); | ||||
} | } | ||||
CBlockIndex *pindex = AddToBlockIndex(block); | CBlockIndex *pindex = AddToBlockIndex(block); | ||||
if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | if (!ReceivedBlockTransactions(block, state, pindex, blockPos)) { | ||||
return error("%s: genesis block not accepted", __func__); | return error("%s: genesis block not accepted", __func__); | ||||
} | } | ||||
} catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
return error("%s: failed to write genesis block: %s", __func__, | return error("%s: failed to write genesis block: %s", __func__, | ||||
▲ Show 20 Lines • Show All 656 Lines • Show Last 20 Lines |
Missing lines from e104f0f