Skip to content

Commit fdf8888

Browse files
committed
[build] Move CheckTransaction from lib_server to lib_consensus
CheckTransaction is a context-free function that does not require access to the blockchain or mempool. Move it from src/consensus/tx_verify in lib_server to a new unit src/consensus/tx_check in lib_consensus so that it can be called by non-server libraries.
1 parent 93de9ab commit fdf8888

File tree

10 files changed

+84
-55
lines changed

10 files changed

+84
-55
lines changed

build_msvc/libbitcoinconsensus/libbitcoinconsensus.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<ItemGroup>
2323
<ClCompile Include="..\..\src\arith_uint256.cpp" />
2424
<ClCompile Include="..\..\src\consensus\merkle.cpp" />
25+
<ClCompile Include="..\..\src\consensus\tx_check.cpp" />
2526
<ClCompile Include="..\..\src\crypto\aes.cpp" />
2627
<ClCompile Include="..\..\src\crypto\chacha20.cpp" />
2728
<ClCompile Include="..\..\src\crypto\hmac_sha256.cpp" />

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ BITCOIN_CORE_H = \
124124
compat/sanity.h \
125125
compressor.h \
126126
consensus/consensus.h \
127+
consensus/tx_check.h \
127128
consensus/tx_verify.h \
128129
core_io.h \
129130
core_memusage.h \
@@ -391,6 +392,7 @@ libbitcoin_consensus_a_SOURCES = \
391392
consensus/merkle.cpp \
392393
consensus/merkle.h \
393394
consensus/params.h \
395+
consensus/tx_check.cpp \
394396
consensus/validation.h \
395397
hash.cpp \
396398
hash.h \

src/consensus/tx_check.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) 2017-2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <consensus/tx_check.h>
6+
7+
#include <primitives/transaction.h>
8+
#include <consensus/validation.h>
9+
10+
bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fCheckDuplicateInputs)
11+
{
12+
// Basic checks that don't depend on any context
13+
if (tx.vin.empty())
14+
return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");
15+
if (tx.vout.empty())
16+
return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");
17+
// Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
18+
if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
19+
return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");
20+
21+
// Check for negative or overflow output values
22+
CAmount nValueOut = 0;
23+
for (const auto& txout : tx.vout)
24+
{
25+
if (txout.nValue < 0)
26+
return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
27+
if (txout.nValue > MAX_MONEY)
28+
return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");
29+
nValueOut += txout.nValue;
30+
if (!MoneyRange(nValueOut))
31+
return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");
32+
}
33+
34+
// Check for duplicate inputs - note that this check is slow so we skip it in CheckBlock
35+
if (fCheckDuplicateInputs) {
36+
std::set<COutPoint> vInOutPoints;
37+
for (const auto& txin : tx.vin)
38+
{
39+
if (!vInOutPoints.insert(txin.prevout).second)
40+
return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");
41+
}
42+
}
43+
44+
if (tx.IsCoinBase())
45+
{
46+
if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
47+
return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
48+
}
49+
else
50+
{
51+
for (const auto& txin : tx.vin)
52+
if (txin.prevout.IsNull())
53+
return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");
54+
}
55+
56+
return true;
57+
}

src/consensus/tx_check.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2017-2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_CONSENSUS_TX_CHECK_H
6+
#define BITCOIN_CONSENSUS_TX_CHECK_H
7+
8+
/**
9+
* Context-independent transaction checking code that can be called outside the
10+
* bitcoin server and doesn't depend on chain or mempool state. Transaction
11+
* verification code that does call server functions or depend on server state
12+
* belongs in tx_verify.h/cpp instead.
13+
*/
14+
15+
class CTransaction;
16+
class CValidationState;
17+
18+
bool CheckTransaction(const CTransaction& tx, CValidationState& state, bool fCheckDuplicateInputs=true);
19+
20+
#endif // BITCOIN_CONSENSUS_TX_CHECK_H

src/consensus/tx_verify.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -156,55 +156,6 @@ int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& i
156156
return nSigOps;
157157
}
158158

159-
bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fCheckDuplicateInputs)
160-
{
161-
// Basic checks that don't depend on any context
162-
if (tx.vin.empty())
163-
return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");
164-
if (tx.vout.empty())
165-
return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");
166-
// Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
167-
if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
168-
return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");
169-
170-
// Check for negative or overflow output values
171-
CAmount nValueOut = 0;
172-
for (const auto& txout : tx.vout)
173-
{
174-
if (txout.nValue < 0)
175-
return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
176-
if (txout.nValue > MAX_MONEY)
177-
return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");
178-
nValueOut += txout.nValue;
179-
if (!MoneyRange(nValueOut))
180-
return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");
181-
}
182-
183-
// Check for duplicate inputs - note that this check is slow so we skip it in CheckBlock
184-
if (fCheckDuplicateInputs) {
185-
std::set<COutPoint> vInOutPoints;
186-
for (const auto& txin : tx.vin)
187-
{
188-
if (!vInOutPoints.insert(txin.prevout).second)
189-
return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");
190-
}
191-
}
192-
193-
if (tx.IsCoinBase())
194-
{
195-
if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
196-
return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
197-
}
198-
else
199-
{
200-
for (const auto& txin : tx.vin)
201-
if (txin.prevout.IsNull())
202-
return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");
203-
}
204-
205-
return true;
206-
}
207-
208159
bool Consensus::CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight, CAmount& txfee)
209160
{
210161
// are the actual inputs available?

src/consensus/tx_verify.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ class CValidationState;
1717

1818
/** Transaction validation functions */
1919

20-
/** Context-independent validity checks */
21-
bool CheckTransaction(const CTransaction& tx, CValidationState& state, bool fCheckDuplicateInputs=true);
22-
2320
namespace Consensus {
2421
/**
2522
* Check whether all inputs of this transaction are valid (no double spends and amounts)

src/test/sighash_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include <consensus/tx_verify.h>
5+
#include <consensus/tx_check.h>
66
#include <consensus/validation.h>
77
#include <test/data/sighash.json.h>
88
#include <hash.h>

src/test/transaction_tests.cpp

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

99
#include <clientversion.h>
1010
#include <checkqueue.h>
11-
#include <consensus/tx_verify.h>
11+
#include <consensus/tx_check.h>
1212
#include <consensus/validation.h>
1313
#include <core_io.h>
1414
#include <key.h>

src/validation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <checkqueue.h>
1313
#include <consensus/consensus.h>
1414
#include <consensus/merkle.h>
15+
#include <consensus/tx_check.h>
1516
#include <consensus/tx_verify.h>
1617
#include <consensus/validation.h>
1718
#include <cuckoocache.h>

src/wallet/walletdb.cpp

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

66
#include <wallet/walletdb.h>
77

8-
#include <consensus/tx_verify.h>
8+
#include <consensus/tx_check.h>
99
#include <consensus/validation.h>
1010
#include <fs.h>
1111
#include <key_io.h>

0 commit comments

Comments
 (0)