Skip to content
3 changes: 2 additions & 1 deletion src/bitcoin-tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal)

static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
{
const Consensus::Params& consensusParams = Params().GetConsensus();
// separate TXID:VOUT in string
size_t pos = strInput.find(':');
if ((pos == string::npos) ||
Expand All @@ -191,7 +192,7 @@ static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
uint256 txid(uint256S(strTxid));

static const unsigned int minTxOutSz = 9;
static const unsigned int maxVout = MAX_BLOCK_SIZE / minTxOutSz;
static const unsigned int maxVout = consensusParams.nMaxTxSize / minTxOutSz;

// extract and validate vout
string strVout = strInput.substr(pos + 1, string::npos);
Expand Down
17 changes: 17 additions & 0 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ class CMainParams : public CChainParams {
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nMaxBlockSize = 1000000;
consensus.nMaxTxSize = 1000000;
consensus.nMinTxSize = ::GetSerializeSize(CTransaction(), SER_NETWORK, PROTOCOL_VERSION);
consensus.nMaxBlockSigops = consensus.nMaxBlockSize / 50;
consensus.nCoinbaseMaturity = 100;

/**
* The message start string is designed to be unlikely to occur in normal data.
* The characters are rarely used upper ASCII, not valid as UTF-8, and produce
Expand Down Expand Up @@ -162,6 +168,12 @@ class CTestNetParams : public CChainParams {
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = false;
consensus.nMaxBlockSize = 1000000;
consensus.nMaxTxSize = 1000000;
consensus.nMinTxSize = ::GetSerializeSize(CTransaction(), SER_NETWORK, PROTOCOL_VERSION);
consensus.nMaxBlockSigops = consensus.nMaxBlockSize / 50;
consensus.nCoinbaseMaturity = 100;

pchMessageStart[0] = 0x0b;
pchMessageStart[1] = 0x11;
pchMessageStart[2] = 0x09;
Expand Down Expand Up @@ -227,6 +239,11 @@ class CRegTestParams : public CChainParams {
consensus.nPowTargetSpacing = 10 * 60;
consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = true;
consensus.nMaxBlockSize = 1000000;
consensus.nMaxTxSize = 1000000;
consensus.nMinTxSize = ::GetSerializeSize(CTransaction(), SER_NETWORK, PROTOCOL_VERSION);
consensus.nMaxBlockSigops = consensus.nMaxBlockSize / 50;
consensus.nCoinbaseMaturity = 100;

pchMessageStart[0] = 0xfa;
pchMessageStart[1] = 0xbf;
Expand Down
69 changes: 65 additions & 4 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,73 @@
#ifndef BITCOIN_CONSENSUS_CONSENSUS_H
#define BITCOIN_CONSENSUS_CONSENSUS_H

#include "consensus/params.h"

#include <stdint.h>

class CBlock;
class CBlockHeader;
class CBlockIndex;
class CCoinsViewCache;
class CTransaction;
class CValidationState;

/**
* Consensus validations:
* Check_ means checking everything possible with the data provided.
* Verify_ means all data provided was enough for this level and its "consensus-verified".
*/
namespace Consensus {

/** Transaction validation functions */

/**
* Context-independent CTransaction validity checks
*/
bool CheckTx(const CTransaction& tx, CValidationState& state, const Params& consensusParams);
/**
* Check whether all inputs of this transaction are valid (no double spends and amounts)
* This does not modify the UTXO set. This does not check scripts and sigs.
* Preconditions: tx.IsCoinBase() is false.
*/
bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const Params& consensusParams, const CCoinsViewCache& inputs, int nSpendHeight);

/** Block header validation functions */

/**
* Context-independent CBlockHeader validity checks
*/
bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Params& consensusParams, int64_t nTime, bool fCheckPOW = true);
/**
* Context-dependent CBlockHeader validity checks
*/
bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Params& consensusParams, const CBlockIndex* pindexPrev);

/** Block validation functions */

/**
* Context-independent CBlock validity checks
*/
bool CheckBlock(const CBlock& block, CValidationState& state, const Params& consensusParams, int64_t nTime, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
/**
* Context-dependent CBlock validity checks
*/
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Params& consensusParams, const CBlockIndex* pindexPrev);

} // namespace Consensus

/** Block validation utility functions */

/** The maximum allowed size for a serialized block, in bytes (network rule) */
static const unsigned int MAX_BLOCK_SIZE = 1000000;
inline uint64_t MaxBlockSize(const Consensus::Params& consensusParams)
{
return consensusParams.nMaxBlockSize;
}
/** The maximum allowed number of signature check operations in a block (network rule) */
static const unsigned int MAX_BLOCK_SIGOPS = MAX_BLOCK_SIZE/50;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 100;
inline uint64_t MaxBlockSigops(const Consensus::Params& consensusParams)
{
return consensusParams.nMaxBlockSigops;
}

/** Flags for LockTime() */
enum {
Expand Down
6 changes: 6 additions & 0 deletions src/consensus/params.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace Consensus {
*/
struct Params {
uint256 hashGenesisBlock;
uint32_t nMinTxSize;
uint64_t nMaxBlockSize;
uint32_t nMaxTxSize;
uint64_t nMaxBlockSigops;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
int32_t nCoinbaseMaturity;
int nSubsidyHalvingInterval;
/** Used to check majorities for block version upgrade */
int nMajorityEnforceBlockUpgrade;
Expand Down
15 changes: 8 additions & 7 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ void CleanupBlockRevFiles()

void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
{
const CChainParams& chainparams = Params();
RenameThread("bitcoin-loadblk");
// -reindex
if (fReindex) {
Expand All @@ -601,14 +602,14 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
if (!file)
break; // This error is logged in OpenBlockFile
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
LoadExternalBlockFile(file, &pos);
LoadExternalBlockFile(chainparams, file, &pos);
nFile++;
}
pblocktree->WriteReindexing(false);
fReindex = false;
LogPrintf("Reindexing finished\n");
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
InitBlockIndex();
InitBlockIndex(chainparams);
}

// hardcoded $DATADIR/bootstrap.dat
Expand All @@ -619,7 +620,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
CImportingNow imp;
boost::filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
LogPrintf("Importing bootstrap.dat...\n");
LoadExternalBlockFile(file);
LoadExternalBlockFile(chainparams, file);
RenameOver(pathBootstrap, pathBootstrapOld);
} else {
LogPrintf("Warning: Could not open bootstrap file %s\n", pathBootstrap.string());
Expand All @@ -632,7 +633,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
if (file) {
CImportingNow imp;
LogPrintf("Importing blocks file %s...\n", path.string());
LoadExternalBlockFile(file);
LoadExternalBlockFile(chainparams, file);
} else {
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
}
Expand Down Expand Up @@ -1297,7 +1298,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));

// Initialize the block index (no-op if non-empty database was already loaded)
if (!InitBlockIndex()) {
if (!InitBlockIndex(chainparams)) {
strLoadError = _("Error initializing block database");
break;
}
Expand Down Expand Up @@ -1332,7 +1333,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
}
}

if (!CVerifyDB().VerifyDB(pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
strLoadError = _("Corrupted block database detected");
break;
Expand Down Expand Up @@ -1556,7 +1557,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
uiInterface.InitMessage(_("Activating best chain..."));
// scan for better chains in the block chain database, that are not yet connected in the active best chain
CValidationState state;
if (!ActivateBestChain(state))
if (!ActivateBestChain(state, chainparams))
strErrors << "Failed to connect best block";

std::vector<boost::filesystem::path> vImportFiles;
Expand Down
Loading