Skip to content

Commit 1084621

Browse files
committed
Merge bitcoin#28438: Use serialization parameters for CTransaction
a0c254c Drop CHashWriter (Anthony Towns) c94f7e5 Drop OverrideStream (Anthony Towns) 6e9e4e6 Use ParamsWrapper for witness serialization (Anthony Towns) Pull request description: Choose whether witness is included in transaction serialization via serialization parameter rather than the stream version. See bitcoin#25284 and bitcoin#19477 for previous context. ACKs for top commit: maflcko: re-ACK a0c254c 🐜 theuni: ACK a0c254c Tree-SHA512: 8fd5cadfd84c5128e36c34a51fb94fdccd956280e7f65b7d73c512d6a9cdb53cdd3649de99ffab5322bd34be26cb95ab4eb05932b3b9de9c11d85743f50dcb13
2 parents a73715e + a0c254c commit 1084621

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+228
-277
lines changed

src/bench/checkblock.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static void DeserializeBlockTest(benchmark::Bench& bench)
2424

2525
bench.unit("block").run([&] {
2626
CBlock block;
27-
stream >> block;
27+
stream >> TX_WITH_WITNESS(block);
2828
bool rewound = stream.Rewind(benchmark::data::block413567.size());
2929
assert(rewound);
3030
});
@@ -41,7 +41,7 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
4141

4242
bench.unit("block").run([&] {
4343
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
44-
stream >> block;
44+
stream >> TX_WITH_WITNESS(block);
4545
bool rewound = stream.Rewind(benchmark::data::block413567.size());
4646
assert(rewound);
4747

src/bench/rpc_blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ struct TestBlockAndIndex {
2727
std::byte a{0};
2828
stream.write({&a, 1}); // Prevent compaction
2929

30-
stream >> block;
30+
stream >> TX_WITH_WITNESS(block);
3131

3232
blockHash = block.GetHash();
3333
blockindex.phashBlock = &blockHash;

src/bench/verify_script.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
6262

6363
#if defined(HAVE_CONSENSUS_LIB)
6464
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
65-
stream << txSpend;
65+
stream << TX_WITH_WITNESS(txSpend);
6666
int csuccess = bitcoinconsensus_verify_script_with_amount(
6767
txCredit.vout[0].scriptPubKey.data(),
6868
txCredit.vout[0].scriptPubKey.size(),

src/blockencodings.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class BlockTransactions {
6565

6666
SERIALIZE_METHODS(BlockTransactions, obj)
6767
{
68-
READWRITE(obj.blockhash, Using<VectorFormatter<TransactionCompression>>(obj.txn));
68+
READWRITE(obj.blockhash, TX_WITH_WITNESS(Using<VectorFormatter<TransactionCompression>>(obj.txn)));
6969
}
7070
};
7171

@@ -76,7 +76,7 @@ struct PrefilledTransaction {
7676
uint16_t index;
7777
CTransactionRef tx;
7878

79-
SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), Using<TransactionCompression>(obj.tx)); }
79+
SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
8080
};
8181

8282
typedef enum ReadStatus_t

src/consensus/tx_check.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
1616
if (tx.vout.empty())
1717
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
1818
// Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
19-
if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
19+
if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) {
2020
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
21+
}
2122

2223
// Check for negative or overflow output values (see CVE-2010-5139)
2324
CAmount nValueOut = 0;

src/consensus/validation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ class BlockValidationState : public ValidationState<BlockValidationResult> {};
149149
// weight = (stripped_size * 3) + total_size.
150150
static inline int32_t GetTransactionWeight(const CTransaction& tx)
151151
{
152-
return ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION);
152+
return ::GetSerializeSize(TX_NO_WITNESS(tx)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(tx));
153153
}
154154
static inline int64_t GetBlockWeight(const CBlock& block)
155155
{
156-
return ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, PROTOCOL_VERSION);
156+
return ::GetSerializeSize(TX_NO_WITNESS(block)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(block));
157157
}
158158
static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
159159
{
160160
// scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
161-
return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION);
161+
return ::GetSerializeSize(TX_NO_WITNESS(txin)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(txin)) + ::GetSerializeSize(txin.scriptWitness.stack);
162162
}
163163

164164
/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */

src/core_io.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ bool ParseHashStr(const std::string& strHex, uint256& result);
5151
// core_write.cpp
5252
UniValue ValueFromAmount(const CAmount amount);
5353
std::string FormatScript(const CScript& script);
54-
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0);
54+
std::string EncodeHexTx(const CTransaction& tx, const bool without_witness = false);
5555
std::string SighashToStr(unsigned char sighash_type);
5656
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false, const SigningProvider* provider = nullptr);
57-
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex = true, int serialize_flags = 0, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS);
57+
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex = true, bool without_witness = false, const CTxUndo* txundo = nullptr, TxVerbosity verbosity = TxVerbosity::SHOW_DETAILS);
5858

5959
#endif // BITCOIN_CORE_IO_H

src/core_read.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>&
142142
// Try decoding with extended serialization support, and remember if the result successfully
143143
// consumes the entire input.
144144
if (try_witness) {
145-
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION);
145+
DataStream ssData(tx_data);
146146
try {
147-
ssData >> tx_extended;
147+
ssData >> TX_WITH_WITNESS(tx_extended);
148148
if (ssData.empty()) ok_extended = true;
149149
} catch (const std::exception&) {
150150
// Fall through.
@@ -160,9 +160,9 @@ static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>&
160160

161161
// Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
162162
if (try_no_witness) {
163-
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS);
163+
DataStream ssData(tx_data);
164164
try {
165-
ssData >> tx_legacy;
165+
ssData >> TX_NO_WITNESS(tx_legacy);
166166
if (ssData.empty()) ok_legacy = true;
167167
} catch (const std::exception&) {
168168
// Fall through.
@@ -222,9 +222,9 @@ bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
222222
return false;
223223

224224
std::vector<unsigned char> blockData(ParseHex(strHexBlk));
225-
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION);
225+
DataStream ssBlock(blockData);
226226
try {
227-
ssBlock >> block;
227+
ssBlock >> TX_WITH_WITNESS(block);
228228
}
229229
catch (const std::exception&) {
230230
return false;

src/core_write.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,14 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco
140140
return str;
141141
}
142142

143-
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags)
143+
std::string EncodeHexTx(const CTransaction& tx, const bool without_witness)
144144
{
145-
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags);
146-
ssTx << tx;
145+
DataStream ssTx;
146+
if (without_witness) {
147+
ssTx << TX_NO_WITNESS(tx);
148+
} else {
149+
ssTx << TX_WITH_WITNESS(tx);
150+
}
147151
return HexStr(ssTx);
148152
}
149153

@@ -168,7 +172,7 @@ void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool i
168172
out.pushKV("type", GetTxnOutputType(type));
169173
}
170174

171-
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity)
175+
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, bool without_witness, const CTxUndo* txundo, TxVerbosity verbosity)
172176
{
173177
CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
174178

@@ -177,7 +181,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
177181
// Transaction version is actually unsigned in consensus checks, just signed in memory,
178182
// so cast to unsigned before giving it to the user.
179183
entry.pushKV("version", static_cast<int64_t>(static_cast<uint32_t>(tx.nVersion)));
180-
entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION));
184+
entry.pushKV("size", tx.GetTotalSize());
181185
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
182186
entry.pushKV("weight", GetTransactionWeight(tx));
183187
entry.pushKV("locktime", (int64_t)tx.nLockTime);
@@ -264,6 +268,6 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
264268
}
265269

266270
if (include_hex) {
267-
entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
271+
entry.pushKV("hex", EncodeHexTx(tx, without_witness)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
268272
}
269273
}

src/hash.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,23 +146,6 @@ class HashWriter
146146
}
147147
};
148148

149-
class CHashWriter : public HashWriter
150-
{
151-
private:
152-
const int nVersion;
153-
154-
public:
155-
CHashWriter(int nVersionIn) : nVersion{nVersionIn} {}
156-
157-
int GetVersion() const { return nVersion; }
158-
159-
template<typename T>
160-
CHashWriter& operator<<(const T& obj) {
161-
::Serialize(*this, obj);
162-
return (*this);
163-
}
164-
};
165-
166149
/** Reads data from an underlying stream, while hashing the read data. */
167150
template <typename Source>
168151
class HashVerifier : public HashWriter

0 commit comments

Comments
 (0)