Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show All 20 Lines | |||||
#include <consensus/tx_verify.h> | #include <consensus/tx_verify.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <hash.h> | #include <hash.h> | ||||
#include <index/blockfilterindex.h> | #include <index/blockfilterindex.h> | ||||
#include <index/txindex.h> | #include <index/txindex.h> | ||||
#include <logging.h> | #include <logging.h> | ||||
#include <logging/timer.h> | #include <logging/timer.h> | ||||
#include <minerfund.h> | #include <minerfund.h> | ||||
#include <node/blockstorage.h> | |||||
#include <node/coinstats.h> | #include <node/coinstats.h> | ||||
#include <node/ui_interface.h> | #include <node/ui_interface.h> | ||||
#include <policy/fees.h> | #include <policy/fees.h> | ||||
#include <policy/mempool.h> | #include <policy/mempool.h> | ||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <policy/settings.h> | #include <policy/settings.h> | ||||
#include <pow/pow.h> | #include <pow/pow.h> | ||||
#include <primitives/block.h> | #include <primitives/block.h> | ||||
▲ Show 20 Lines • Show All 812 Lines • ▼ Show 20 Lines | if (g_txindex) { | ||||
CTransactionRef tx; | CTransactionRef tx; | ||||
if (g_txindex->FindTx(txid, hashBlock, tx)) { | if (g_txindex->FindTx(txid, hashBlock, tx)) { | ||||
return tx; | return tx; | ||||
} | } | ||||
} | } | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
////////////////////////////////////////////////////////////////////////////// | |||||
// | |||||
// CBlock and CBlockIndex | |||||
// | |||||
static bool WriteBlockToDisk(const CBlock &block, FlatFilePos &pos, | |||||
const CMessageHeader::MessageMagic &messageStart) { | |||||
// Open history file to append | |||||
CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION); | |||||
if (fileout.IsNull()) { | |||||
return error("WriteBlockToDisk: OpenBlockFile failed"); | |||||
} | |||||
// Write index header | |||||
unsigned int nSize = GetSerializeSize(block, fileout.GetVersion()); | |||||
fileout << messageStart << nSize; | |||||
// Write block | |||||
long fileOutPos = ftell(fileout.Get()); | |||||
if (fileOutPos < 0) { | |||||
return error("WriteBlockToDisk: ftell failed"); | |||||
} | |||||
pos.nPos = (unsigned int)fileOutPos; | |||||
fileout << block; | |||||
return true; | |||||
} | |||||
Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams) { | Amount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams) { | ||||
int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; | int halvings = nHeight / consensusParams.nSubsidyHalvingInterval; | ||||
// Force block reward to zero when right shift is undefined. | // Force block reward to zero when right shift is undefined. | ||||
if (halvings >= 64) { | if (halvings >= 64) { | ||||
return Amount::zero(); | return Amount::zero(); | ||||
} | } | ||||
Amount nSubsidy = 50 * COIN; | Amount nSubsidy = 50 * COIN; | ||||
▲ Show 20 Lines • Show All 2,776 Lines • ▼ Show 20 Lines | if (pindexNew->UpdateChainStats()) { | ||||
} | } | ||||
} else if (pindexNew->pprev && | } else if (pindexNew->pprev && | ||||
pindexNew->pprev->IsValid(BlockValidity::TREE)) { | pindexNew->pprev->IsValid(BlockValidity::TREE)) { | ||||
m_blockman.m_blocks_unlinked.insert( | m_blockman.m_blocks_unlinked.insert( | ||||
std::make_pair(pindexNew->pprev, pindexNew)); | std::make_pair(pindexNew->pprev, pindexNew)); | ||||
} | } | ||||
} | } | ||||
static bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, | // TODO move to blockstorage | ||||
unsigned int nHeight, CChain &active_chain, | bool FindBlockPos(FlatFilePos &pos, unsigned int nAddSize, unsigned int nHeight, | ||||
uint64_t nTime, bool fKnown = false) { | CChain &active_chain, uint64_t nTime, bool fKnown = false) { | ||||
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); | ||||
} | } | ||||
bool finalize_undo = false; | bool finalize_undo = false; | ||||
▲ Show 20 Lines • Show All 566 Lines • ▼ Show 20 Lines | if (NotifyHeaderTip(ActiveChainstate())) { | ||||
Params().GetConsensus().nPowTargetSpacing) * | Params().GetConsensus().nPowTargetSpacing) * | ||||
(*ppindex)->nHeight); | (*ppindex)->nHeight); | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
/** | /** | ||||
* Store block on disk. If dbp is non-nullptr, the file is known to already | |||||
* reside on disk. | |||||
*/ | |||||
static FlatFilePos SaveBlockToDisk(const CBlock &block, int nHeight, | |||||
CChain &active_chain, | |||||
const CChainParams &chainparams, | |||||
const FlatFilePos *dbp) { | |||||
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); | |||||
FlatFilePos blockPos; | |||||
if (dbp != nullptr) { | |||||
blockPos = *dbp; | |||||
} | |||||
if (!FindBlockPos(blockPos, nBlockSize + 8, nHeight, active_chain, | |||||
block.GetBlockTime(), dbp != nullptr)) { | |||||
error("%s: FindBlockPos failed", __func__); | |||||
return FlatFilePos(); | |||||
} | |||||
if (dbp == nullptr) { | |||||
if (!WriteBlockToDisk(block, blockPos, chainparams.DiskMagic())) { | |||||
AbortNode("Failed to write block"); | |||||
return FlatFilePos(); | |||||
} | |||||
} | |||||
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. | ||||
* @param[in-out] fNewBlock True if block was first received via this call. | * @param[in-out] fNewBlock True if block was first received via this call. | ||||
▲ Show 20 Lines • Show All 2,176 Lines • Show Last 20 Lines |