Skip to content

Commit fd19dd4

Browse files
committed
temp
1 parent 9fe53a8 commit fd19dd4

File tree

6 files changed

+22
-141
lines changed

6 files changed

+22
-141
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ libbitcoin_common_a_SOURCES = \
239239
chainparams.cpp \
240240
coins.cpp \
241241
compressor.cpp \
242+
consensus/consensus.cpp \
242243
primitives/block.cpp \
243244
primitives/transaction.cpp \
244245
core_read.cpp \

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "base58.h"
66
#include "clientversion.h"
7-
#include "primitives/block.h" // for MAX_BLOCK_SIZE
7+
#include "consensus/consensus.h" // for MAX_BLOCK_SIZE
88
#include "primitives/transaction.h"
99
#include "core_io.h"
1010
#include "coins.h"

src/main.cpp

Lines changed: 17 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -818,58 +818,8 @@ unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& in
818818

819819
bool CheckTransaction(const CTransaction& tx, CValidationState &state)
820820
{
821-
// Basic checks that don't depend on any context
822-
if (tx.vin.empty())
823-
return state.DoS(10, error("CheckTransaction() : vin empty"),
824-
REJECT_INVALID, "bad-txns-vin-empty");
825-
if (tx.vout.empty())
826-
return state.DoS(10, error("CheckTransaction() : vout empty"),
827-
REJECT_INVALID, "bad-txns-vout-empty");
828-
// Size limits
829-
if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
830-
return state.DoS(100, error("CheckTransaction() : size limits failed"),
831-
REJECT_INVALID, "bad-txns-oversize");
832-
833-
// Check for negative or overflow output values
834-
CAmount nValueOut = 0;
835-
BOOST_FOREACH(const CTxOut& txout, tx.vout)
836-
{
837-
if (txout.nValue < 0)
838-
return state.DoS(100, error("CheckTransaction() : txout.nValue negative"),
839-
REJECT_INVALID, "bad-txns-vout-negative");
840-
if (txout.nValue > MAX_MONEY)
841-
return state.DoS(100, error("CheckTransaction() : txout.nValue too high"),
842-
REJECT_INVALID, "bad-txns-vout-toolarge");
843-
nValueOut += txout.nValue;
844-
if (!MoneyRange(nValueOut))
845-
return state.DoS(100, error("CheckTransaction() : txout total out of range"),
846-
REJECT_INVALID, "bad-txns-txouttotal-toolarge");
847-
}
848-
849-
// Check for duplicate inputs
850-
set<COutPoint> vInOutPoints;
851-
BOOST_FOREACH(const CTxIn& txin, tx.vin)
852-
{
853-
if (vInOutPoints.count(txin.prevout))
854-
return state.DoS(100, error("CheckTransaction() : duplicate inputs"),
855-
REJECT_INVALID, "bad-txns-inputs-duplicate");
856-
vInOutPoints.insert(txin.prevout);
857-
}
858-
859-
if (tx.IsCoinBase())
860-
{
861-
if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
862-
return state.DoS(100, error("CheckTransaction() : coinbase script size"),
863-
REJECT_INVALID, "bad-cb-length");
864-
}
865-
else
866-
{
867-
BOOST_FOREACH(const CTxIn& txin, tx.vin)
868-
if (txin.prevout.IsNull())
869-
return state.DoS(10, error("CheckTransaction() : prevout is null"),
870-
REJECT_INVALID, "bad-txns-prevout-null");
871-
}
872-
821+
if (!Consensus::CheckTransaction(tx,state))
822+
return error(std::string(std::string(__func__) + ": " + state.GetRejectReason()).c_str());
873823
return true;
874824
}
875825

@@ -1845,7 +1795,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
18451795
if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
18461796
return error("ConnectBlock() : FindUndoPos failed");
18471797
if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash()))
1848-
return state.Abort("Failed to write undo data");
1798+
return AbortNode(state, "Failed to write undo data");
18491799

18501800
// update nUndoPos in block index
18511801
pindex->nUndoPos = pos.nPos;
@@ -1858,7 +1808,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
18581808

18591809
if (fTxIndex)
18601810
if (!pblocktree->WriteTxIndex(vPos))
1861-
return state.Abort("Failed to write transaction index");
1811+
return AbortNode(state, "Failed to write transaction index");
18621812

18631813
// add this block to the view's block chain
18641814
view.SetBestBlock(pindex->GetBlockHash());
@@ -1919,20 +1869,20 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
19191869
setDirtyBlockIndex.erase(it++);
19201870
}
19211871
if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
1922-
return state.Abort("Files to write to block index database");
1872+
return AbortNode(state, "Files to write to block index database");
19231873
}
19241874
}
19251875
// Finally flush the chainstate (which may refer to block index entries).
19261876
if (!pcoinsTip->Flush())
1927-
return state.Abort("Failed to write to coin database");
1877+
return AbortNode(state, "Failed to write to coin database");
19281878
// Update best block in wallet (so we can detect restored wallets).
19291879
if (mode != FLUSH_STATE_IF_NEEDED) {
19301880
g_signals.SetBestChain(chainActive.GetLocator());
19311881
}
19321882
nLastWrite = GetTimeMicros();
19331883
}
19341884
} catch (const std::runtime_error& e) {
1935-
return state.Abort(std::string("System error while flushing: ") + e.what());
1885+
return AbortNode(state, std::string("System error while flushing: ") + e.what());
19361886
}
19371887
return true;
19381888
}
@@ -1989,7 +1939,7 @@ bool static DisconnectTip(CValidationState &state) {
19891939
// Read block from disk.
19901940
CBlock block;
19911941
if (!ReadBlockFromDisk(block, pindexDelete))
1992-
return state.Abort("Failed to read block");
1942+
return AbortNode(state, "Failed to read block");
19931943
// Apply the block atomically to the chain state.
19941944
int64_t nStart = GetTimeMicros();
19951945
{
@@ -2040,7 +1990,7 @@ bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *
20401990
CBlock block;
20411991
if (!pblock) {
20421992
if (!ReadBlockFromDisk(block, pindexNew))
2043-
return state.Abort("Failed to read block");
1993+
return AbortNode(state, "Failed to read block");
20441994
pblock = &block;
20451995
}
20461996
// Apply the block atomically to the chain state.
@@ -2706,11 +2656,11 @@ bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex,
27062656
return error("AcceptBlock() : FindBlockPos failed");
27072657
if (dbp == NULL)
27082658
if (!WriteBlockToDisk(block, blockPos))
2709-
return state.Abort("Failed to write block");
2659+
AbortNode(state, "Failed to write block");
27102660
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
27112661
return error("AcceptBlock() : ReceivedBlockTransactions failed");
27122662
} catch (const std::runtime_error& e) {
2713-
return state.Abort(std::string("System error: ") + e.what());
2663+
return AbortNode(state, std::string("System error: ") + e.what());
27142664
}
27152665

27162666
return true;
@@ -2799,6 +2749,12 @@ bool AbortNode(const std::string &strMessage, const std::string &userMessage) {
27992749
return false;
28002750
}
28012751

2752+
bool AbortNode(CValidationState& state, const std::string &strMessage, const std::string &userMessage)
2753+
{
2754+
AbortNode(strMessage, userMessage);
2755+
return state.Error(strMessage);
2756+
}
2757+
28022758
bool CheckDiskSpace(uint64_t nAdditionalBytes)
28032759
{
28042760
uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;

src/main.h

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "chain.h"
1515
#include "chainparams.h"
1616
#include "coins.h"
17+
#include "consensus/consensus.h"
1718
#include "primitives/block.h"
1819
#include "primitives/transaction.h"
1920
#include "net.h"
@@ -91,18 +92,6 @@ static const unsigned int MAX_HEADERS_RESULTS = 2000;
9192
static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
9293
/** Time to wait (in seconds) between writing blockchain state to disk. */
9394
static const unsigned int DATABASE_WRITE_INTERVAL = 3600;
94-
/** Maximum length of reject messages. */
95-
static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
96-
97-
/** "reject" message codes */
98-
static const unsigned char REJECT_MALFORMED = 0x01;
99-
static const unsigned char REJECT_INVALID = 0x10;
100-
static const unsigned char REJECT_OBSOLETE = 0x11;
101-
static const unsigned char REJECT_DUPLICATE = 0x12;
102-
static const unsigned char REJECT_NONSTANDARD = 0x40;
103-
static const unsigned char REJECT_DUST = 0x41;
104-
static const unsigned char REJECT_INSUFFICIENTFEE = 0x42;
105-
static const unsigned char REJECT_CHECKPOINT = 0x43;
10695

10796
struct BlockHasher
10897
{
@@ -196,6 +185,7 @@ CAmount GetBlockValue(int nHeight, const CAmount& nFees);
196185
CBlockIndex * InsertBlockIndex(uint256 hash);
197186
/** Abort with a message */
198187
bool AbortNode(const std::string &msg, const std::string &userMessage="");
188+
bool AbortNode(CValidationState& state, const std::string &msg, const std::string &userMessage="");
199189
/** Get statistics from node state */
200190
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
201191
/** Increase a node's misbehavior score. */
@@ -424,69 +414,6 @@ class CBlockFileInfo
424414
}
425415
};
426416

427-
/** Capture information about block/transaction validation */
428-
class CValidationState {
429-
private:
430-
enum mode_state {
431-
MODE_VALID, //! everything ok
432-
MODE_INVALID, //! network rule violation (DoS value may be set)
433-
MODE_ERROR, //! run-time error
434-
} mode;
435-
int nDoS;
436-
std::string strRejectReason;
437-
unsigned char chRejectCode;
438-
bool corruptionPossible;
439-
public:
440-
CValidationState() : mode(MODE_VALID), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
441-
bool DoS(int level, bool ret = false,
442-
unsigned char chRejectCodeIn=0, std::string strRejectReasonIn="",
443-
bool corruptionIn=false) {
444-
chRejectCode = chRejectCodeIn;
445-
strRejectReason = strRejectReasonIn;
446-
corruptionPossible = corruptionIn;
447-
if (mode == MODE_ERROR)
448-
return ret;
449-
nDoS += level;
450-
mode = MODE_INVALID;
451-
return ret;
452-
}
453-
bool Invalid(bool ret = false,
454-
unsigned char _chRejectCode=0, std::string _strRejectReason="") {
455-
return DoS(0, ret, _chRejectCode, _strRejectReason);
456-
}
457-
bool Error(std::string strRejectReasonIn="") {
458-
if (mode == MODE_VALID)
459-
strRejectReason = strRejectReasonIn;
460-
mode = MODE_ERROR;
461-
return false;
462-
}
463-
bool Abort(const std::string &msg) {
464-
AbortNode(msg);
465-
return Error(msg);
466-
}
467-
bool IsValid() const {
468-
return mode == MODE_VALID;
469-
}
470-
bool IsInvalid() const {
471-
return mode == MODE_INVALID;
472-
}
473-
bool IsError() const {
474-
return mode == MODE_ERROR;
475-
}
476-
bool IsInvalid(int &nDoSOut) const {
477-
if (IsInvalid()) {
478-
nDoSOut = nDoS;
479-
return true;
480-
}
481-
return false;
482-
}
483-
bool CorruptionPossible() const {
484-
return corruptionPossible;
485-
}
486-
unsigned char GetRejectCode() const { return chRejectCode; }
487-
std::string GetRejectReason() const { return strRejectReason; }
488-
};
489-
490417
/** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
491418
class CVerifyDB {
492419
public:

src/merkleblock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "merkleblock.h"
77

88
#include "hash.h"
9-
#include "primitives/block.h" // for MAX_BLOCK_SIZE
9+
#include "consensus/consensus.h" // for MAX_BLOCK_SIZE
1010
#include "utilstrencodings.h"
1111

1212
using namespace std;

src/primitives/block.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
#include "serialize.h"
1111
#include "uint256.h"
1212

13-
/** The maximum allowed size for a serialized block, in bytes (network rule) */
14-
static const unsigned int MAX_BLOCK_SIZE = 1000000;
15-
1613
/** Nodes collect new transactions into a block, hash them into a hash tree,
1714
* and scan through nonce values to make the block's hash satisfy proof-of-work
1815
* requirements. When they solve the proof-of-work, they broadcast the block

0 commit comments

Comments
 (0)