Merge bitcoin/bitcoin#28438: Use serialization parameters for CTransaction

a0c254c13a Drop CHashWriter (Anthony Towns)
c94f7e5b1c Drop OverrideStream (Anthony Towns)
6e9e4e6130 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 #25284 and #19477 for previous context.

ACKs for top commit:
  maflcko:
    re-ACK a0c254c13a 🐜
  theuni:
    ACK a0c254c13a

Tree-SHA512: 8fd5cadfd84c5128e36c34a51fb94fdccd956280e7f65b7d73c512d6a9cdb53cdd3649de99ffab5322bd34be26cb95ab4eb05932b3b9de9c11d85743f50dcb13
This commit is contained in:
fanquake 2023-11-15 14:59:54 +00:00
commit 108462139b
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
62 changed files with 228 additions and 277 deletions

View file

@ -24,7 +24,7 @@ static void DeserializeBlockTest(benchmark::Bench& bench)
bench.unit("block").run([&] { bench.unit("block").run([&] {
CBlock block; CBlock block;
stream >> block; stream >> TX_WITH_WITNESS(block);
bool rewound = stream.Rewind(benchmark::data::block413567.size()); bool rewound = stream.Rewind(benchmark::data::block413567.size());
assert(rewound); assert(rewound);
}); });
@ -41,7 +41,7 @@ static void DeserializeAndCheckBlockTest(benchmark::Bench& bench)
bench.unit("block").run([&] { bench.unit("block").run([&] {
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
stream >> block; stream >> TX_WITH_WITNESS(block);
bool rewound = stream.Rewind(benchmark::data::block413567.size()); bool rewound = stream.Rewind(benchmark::data::block413567.size());
assert(rewound); assert(rewound);

View file

@ -27,7 +27,7 @@ struct TestBlockAndIndex {
std::byte a{0}; std::byte a{0};
stream.write({&a, 1}); // Prevent compaction stream.write({&a, 1}); // Prevent compaction
stream >> block; stream >> TX_WITH_WITNESS(block);
blockHash = block.GetHash(); blockHash = block.GetHash();
blockindex.phashBlock = &blockHash; blockindex.phashBlock = &blockHash;

View file

@ -62,7 +62,7 @@ static void VerifyScriptBench(benchmark::Bench& bench)
#if defined(HAVE_CONSENSUS_LIB) #if defined(HAVE_CONSENSUS_LIB)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << txSpend; stream << TX_WITH_WITNESS(txSpend);
int csuccess = bitcoinconsensus_verify_script_with_amount( int csuccess = bitcoinconsensus_verify_script_with_amount(
txCredit.vout[0].scriptPubKey.data(), txCredit.vout[0].scriptPubKey.data(),
txCredit.vout[0].scriptPubKey.size(), txCredit.vout[0].scriptPubKey.size(),

View file

@ -65,7 +65,7 @@ public:
SERIALIZE_METHODS(BlockTransactions, obj) SERIALIZE_METHODS(BlockTransactions, obj)
{ {
READWRITE(obj.blockhash, Using<VectorFormatter<TransactionCompression>>(obj.txn)); READWRITE(obj.blockhash, TX_WITH_WITNESS(Using<VectorFormatter<TransactionCompression>>(obj.txn)));
} }
}; };
@ -76,7 +76,7 @@ struct PrefilledTransaction {
uint16_t index; uint16_t index;
CTransactionRef tx; CTransactionRef tx;
SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), Using<TransactionCompression>(obj.tx)); } SERIALIZE_METHODS(PrefilledTransaction, obj) { READWRITE(COMPACTSIZE(obj.index), TX_WITH_WITNESS(Using<TransactionCompression>(obj.tx))); }
}; };
typedef enum ReadStatus_t typedef enum ReadStatus_t

View file

@ -16,8 +16,9 @@ bool CheckTransaction(const CTransaction& tx, TxValidationState& state)
if (tx.vout.empty()) if (tx.vout.empty())
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty"); return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-vout-empty");
// Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability) // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) if (::GetSerializeSize(TX_NO_WITNESS(tx)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) {
return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize"); return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-oversize");
}
// Check for negative or overflow output values (see CVE-2010-5139) // Check for negative or overflow output values (see CVE-2010-5139)
CAmount nValueOut = 0; CAmount nValueOut = 0;

View file

@ -149,16 +149,16 @@ class BlockValidationState : public ValidationState<BlockValidationResult> {};
// weight = (stripped_size * 3) + total_size. // weight = (stripped_size * 3) + total_size.
static inline int32_t GetTransactionWeight(const CTransaction& tx) static inline int32_t GetTransactionWeight(const CTransaction& tx)
{ {
return ::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(tx, PROTOCOL_VERSION); return ::GetSerializeSize(TX_NO_WITNESS(tx)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(tx));
} }
static inline int64_t GetBlockWeight(const CBlock& block) static inline int64_t GetBlockWeight(const CBlock& block)
{ {
return ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(block, PROTOCOL_VERSION); return ::GetSerializeSize(TX_NO_WITNESS(block)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(block));
} }
static inline int64_t GetTransactionInputWeight(const CTxIn& txin) static inline int64_t GetTransactionInputWeight(const CTxIn& txin)
{ {
// scriptWitness size is added here because witnesses and txins are split up in segwit serialization. // scriptWitness size is added here because witnesses and txins are split up in segwit serialization.
return ::GetSerializeSize(txin, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(txin, PROTOCOL_VERSION) + ::GetSerializeSize(txin.scriptWitness.stack, PROTOCOL_VERSION); return ::GetSerializeSize(TX_NO_WITNESS(txin)) * (WITNESS_SCALE_FACTOR - 1) + ::GetSerializeSize(TX_WITH_WITNESS(txin)) + ::GetSerializeSize(txin.scriptWitness.stack);
} }
/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */ /** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */

View file

@ -51,9 +51,9 @@ bool ParseHashStr(const std::string& strHex, uint256& result);
// core_write.cpp // core_write.cpp
UniValue ValueFromAmount(const CAmount amount); UniValue ValueFromAmount(const CAmount amount);
std::string FormatScript(const CScript& script); std::string FormatScript(const CScript& script);
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags = 0); std::string EncodeHexTx(const CTransaction& tx, const bool without_witness = false);
std::string SighashToStr(unsigned char sighash_type); std::string SighashToStr(unsigned char sighash_type);
void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false, const SigningProvider* provider = nullptr); void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex = true, bool include_address = false, const SigningProvider* provider = nullptr);
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); 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);
#endif // BITCOIN_CORE_IO_H #endif // BITCOIN_CORE_IO_H

View file

@ -142,9 +142,9 @@ static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>&
// Try decoding with extended serialization support, and remember if the result successfully // Try decoding with extended serialization support, and remember if the result successfully
// consumes the entire input. // consumes the entire input.
if (try_witness) { if (try_witness) {
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION); DataStream ssData(tx_data);
try { try {
ssData >> tx_extended; ssData >> TX_WITH_WITNESS(tx_extended);
if (ssData.empty()) ok_extended = true; if (ssData.empty()) ok_extended = true;
} catch (const std::exception&) { } catch (const std::exception&) {
// Fall through. // Fall through.
@ -160,9 +160,9 @@ static bool DecodeTx(CMutableTransaction& tx, const std::vector<unsigned char>&
// Try decoding with legacy serialization, and remember if the result successfully consumes the entire input. // Try decoding with legacy serialization, and remember if the result successfully consumes the entire input.
if (try_no_witness) { if (try_no_witness) {
CDataStream ssData(tx_data, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS); DataStream ssData(tx_data);
try { try {
ssData >> tx_legacy; ssData >> TX_NO_WITNESS(tx_legacy);
if (ssData.empty()) ok_legacy = true; if (ssData.empty()) ok_legacy = true;
} catch (const std::exception&) { } catch (const std::exception&) {
// Fall through. // Fall through.
@ -222,9 +222,9 @@ bool DecodeHexBlk(CBlock& block, const std::string& strHexBlk)
return false; return false;
std::vector<unsigned char> blockData(ParseHex(strHexBlk)); std::vector<unsigned char> blockData(ParseHex(strHexBlk));
CDataStream ssBlock(blockData, SER_NETWORK, PROTOCOL_VERSION); DataStream ssBlock(blockData);
try { try {
ssBlock >> block; ssBlock >> TX_WITH_WITNESS(block);
} }
catch (const std::exception&) { catch (const std::exception&) {
return false; return false;

View file

@ -140,10 +140,14 @@ std::string ScriptToAsmStr(const CScript& script, const bool fAttemptSighashDeco
return str; return str;
} }
std::string EncodeHexTx(const CTransaction& tx, const int serializeFlags) std::string EncodeHexTx(const CTransaction& tx, const bool without_witness)
{ {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | serializeFlags); DataStream ssTx;
ssTx << tx; if (without_witness) {
ssTx << TX_NO_WITNESS(tx);
} else {
ssTx << TX_WITH_WITNESS(tx);
}
return HexStr(ssTx); return HexStr(ssTx);
} }
@ -168,7 +172,7 @@ void ScriptToUniv(const CScript& script, UniValue& out, bool include_hex, bool i
out.pushKV("type", GetTxnOutputType(type)); out.pushKV("type", GetTxnOutputType(type));
} }
void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, int serialize_flags, const CTxUndo* txundo, TxVerbosity verbosity) void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry, bool include_hex, bool without_witness, const CTxUndo* txundo, TxVerbosity verbosity)
{ {
CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS); CHECK_NONFATAL(verbosity >= TxVerbosity::SHOW_DETAILS);
@ -177,7 +181,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
// Transaction version is actually unsigned in consensus checks, just signed in memory, // Transaction version is actually unsigned in consensus checks, just signed in memory,
// so cast to unsigned before giving it to the user. // so cast to unsigned before giving it to the user.
entry.pushKV("version", static_cast<int64_t>(static_cast<uint32_t>(tx.nVersion))); entry.pushKV("version", static_cast<int64_t>(static_cast<uint32_t>(tx.nVersion)));
entry.pushKV("size", (int)::GetSerializeSize(tx, PROTOCOL_VERSION)); entry.pushKV("size", tx.GetTotalSize());
entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR); entry.pushKV("vsize", (GetTransactionWeight(tx) + WITNESS_SCALE_FACTOR - 1) / WITNESS_SCALE_FACTOR);
entry.pushKV("weight", GetTransactionWeight(tx)); entry.pushKV("weight", GetTransactionWeight(tx));
entry.pushKV("locktime", (int64_t)tx.nLockTime); entry.pushKV("locktime", (int64_t)tx.nLockTime);
@ -264,6 +268,6 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
} }
if (include_hex) { if (include_hex) {
entry.pushKV("hex", EncodeHexTx(tx, serialize_flags)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction". entry.pushKV("hex", EncodeHexTx(tx, without_witness)); // The hex-encoded transaction. Used the name "hex" to be consistent with the verbose output of "getrawtransaction".
} }
} }

View file

@ -146,23 +146,6 @@ public:
} }
}; };
class CHashWriter : public HashWriter
{
private:
const int nVersion;
public:
CHashWriter(int nVersionIn) : nVersion{nVersionIn} {}
int GetVersion() const { return nVersion; }
template<typename T>
CHashWriter& operator<<(const T& obj) {
::Serialize(*this, obj);
return (*this);
}
};
/** Reads data from an underlying stream, while hashing the read data. */ /** Reads data from an underlying stream, while hashing the read data. */
template <typename Source> template <typename Source>
class HashVerifier : public HashWriter class HashVerifier : public HashWriter

View file

@ -65,7 +65,7 @@ bool TxIndex::CustomAppend(const interfaces::BlockInfo& block)
vPos.reserve(block.data->vtx.size()); vPos.reserve(block.data->vtx.size());
for (const auto& tx : block.data->vtx) { for (const auto& tx : block.data->vtx) {
vPos.emplace_back(tx->GetHash(), pos); vPos.emplace_back(tx->GetHash(), pos);
pos.nTxOffset += ::GetSerializeSize(*tx, CLIENT_VERSION); pos.nTxOffset += ::GetSerializeSize(TX_WITH_WITNESS(*tx));
} }
return m_db->WriteTxs(vPos); return m_db->WriteTxs(vPos);
} }
@ -89,7 +89,7 @@ bool TxIndex::FindTx(const uint256& tx_hash, uint256& block_hash, CTransactionRe
if (fseek(file.Get(), postx.nTxOffset, SEEK_CUR)) { if (fseek(file.Get(), postx.nTxOffset, SEEK_CUR)) {
return error("%s: fseek(...) failed", __func__); return error("%s: fseek(...) failed", __func__);
} }
file >> tx; file >> TX_WITH_WITNESS(tx);
} catch (const std::exception& e) { } catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s", __func__, e.what()); return error("%s: Deserialize or I/O error - %s", __func__, e.what());
} }

View file

@ -335,7 +335,7 @@ public:
virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0; virtual void rpcRunLater(const std::string& name, std::function<void()> fn, int64_t seconds) = 0;
//! Current RPC serialization flags. //! Current RPC serialization flags.
virtual int rpcSerializationFlags() = 0; virtual bool rpcSerializationWithoutWitness() = 0;
//! Get settings value. //! Get settings value.
virtual common::SettingsValue getSetting(const std::string& arg) = 0; virtual common::SettingsValue getSetting(const std::string& arg) = 0;

View file

@ -42,7 +42,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
{ {
if (load_path.empty()) return false; if (load_path.empty()) return false;
CAutoFile file{opts.mockable_fopen_function(load_path, "rb"), CLIENT_VERSION}; AutoFile file{opts.mockable_fopen_function(load_path, "rb")};
if (file.IsNull()) { if (file.IsNull()) {
LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n"); LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
return false; return false;
@ -74,7 +74,7 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
CTransactionRef tx; CTransactionRef tx;
int64_t nTime; int64_t nTime;
int64_t nFeeDelta; int64_t nFeeDelta;
file >> tx; file >> TX_WITH_WITNESS(tx);
file >> nTime; file >> nTime;
file >> nFeeDelta; file >> nFeeDelta;
@ -158,7 +158,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
auto mid = SteadyClock::now(); auto mid = SteadyClock::now();
CAutoFile file{mockable_fopen_function(dump_path + ".new", "wb"), CLIENT_VERSION}; AutoFile file{mockable_fopen_function(dump_path + ".new", "wb")};
if (file.IsNull()) { if (file.IsNull()) {
return false; return false;
} }
@ -176,7 +176,7 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
file << (uint64_t)vinfo.size(); file << (uint64_t)vinfo.size();
for (const auto& i : vinfo) { for (const auto& i : vinfo) {
file << *(i.tx); file << TX_WITH_WITNESS(*(i.tx));
file << int64_t{count_seconds(i.m_time)}; file << int64_t{count_seconds(i.m_time)};
file << int64_t{i.nFeeDelta}; file << int64_t{i.nFeeDelta};
mapDeltas.erase(i.tx->GetHash()); mapDeltas.erase(i.tx->GetHash());

View file

@ -2308,9 +2308,9 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
} }
if (pblock) { if (pblock) {
if (inv.IsMsgBlk()) { if (inv.IsMsgBlk()) {
m_connman.PushMessage(&pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, *pblock)); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, TX_NO_WITNESS(*pblock)));
} else if (inv.IsMsgWitnessBlk()) { } else if (inv.IsMsgWitnessBlk()) {
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock)); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, TX_WITH_WITNESS(*pblock)));
} else if (inv.IsMsgFilteredBlk()) { } else if (inv.IsMsgFilteredBlk()) {
bool sendMerkleBlock = false; bool sendMerkleBlock = false;
CMerkleBlock merkleBlock; CMerkleBlock merkleBlock;
@ -2331,7 +2331,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
// however we MUST always provide at least what the remote peer needs // however we MUST always provide at least what the remote peer needs
typedef std::pair<unsigned int, uint256> PairType; typedef std::pair<unsigned int, uint256> PairType;
for (PairType& pair : merkleBlock.vMatchedTxn) for (PairType& pair : merkleBlock.vMatchedTxn)
m_connman.PushMessage(&pfrom, msgMaker.Make(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, *pblock->vtx[pair.first])); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::TX, TX_NO_WITNESS(*pblock->vtx[pair.first])));
} }
// else // else
// no response // no response
@ -2348,7 +2348,7 @@ void PeerManagerImpl::ProcessGetBlockData(CNode& pfrom, Peer& peer, const CInv&
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock)); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::CMPCTBLOCK, cmpctblock));
} }
} else { } else {
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, *pblock)); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::BLOCK, TX_WITH_WITNESS(*pblock)));
} }
} }
} }
@ -2418,8 +2418,8 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic
CTransactionRef tx = FindTxForGetData(*tx_relay, ToGenTxid(inv)); CTransactionRef tx = FindTxForGetData(*tx_relay, ToGenTxid(inv));
if (tx) { if (tx) {
// WTX and WITNESS_TX imply we serialize with witness // WTX and WITNESS_TX imply we serialize with witness
int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0); const auto maybe_with_witness = (inv.IsMsgTx() ? TX_NO_WITNESS : TX_WITH_WITNESS);
m_connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx)); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::TX, maybe_with_witness(*tx)));
m_mempool.RemoveUnbroadcastTx(tx->GetHash()); m_mempool.RemoveUnbroadcastTx(tx->GetHash());
} else { } else {
vNotFound.push_back(inv); vNotFound.push_back(inv);
@ -4119,7 +4119,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because active chain has too little work; sending empty response\n", pfrom.GetId()); LogPrint(BCLog::NET, "Ignoring getheaders from peer=%d because active chain has too little work; sending empty response\n", pfrom.GetId());
// Just respond with an empty headers message, to tell the peer to // Just respond with an empty headers message, to tell the peer to
// go away but not treat us as unresponsive. // go away but not treat us as unresponsive.
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, std::vector<CBlock>())); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, std::vector<CBlockHeader>()));
return; return;
} }
@ -4169,7 +4169,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
// will re-announce the new block via headers (or compact blocks again) // will re-announce the new block via headers (or compact blocks again)
// in the SendMessages logic. // in the SendMessages logic.
nodestate->pindexBestHeaderSent = pindex ? pindex : m_chainman.ActiveChain().Tip(); nodestate->pindexBestHeaderSent = pindex ? pindex : m_chainman.ActiveChain().Tip();
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, vHeaders)); m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::HEADERS, TX_WITH_WITNESS(vHeaders)));
return; return;
} }
@ -4186,7 +4186,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
if (m_chainman.IsInitialBlockDownload()) return; if (m_chainman.IsInitialBlockDownload()) return;
CTransactionRef ptx; CTransactionRef ptx;
vRecv >> ptx; vRecv >> TX_WITH_WITNESS(ptx);
const CTransaction& tx = *ptx; const CTransaction& tx = *ptx;
const uint256& txid = ptx->GetHash(); const uint256& txid = ptx->GetHash();
@ -4687,7 +4687,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
} }
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>(); std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
vRecv >> *pblock; vRecv >> TX_WITH_WITNESS(*pblock);
LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom.GetId()); LogPrint(BCLog::NET, "received block %s peer=%d\n", pblock->GetHash().ToString(), pfrom.GetId());
@ -5698,7 +5698,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__, LogPrint(BCLog::NET, "%s: sending header %s to peer=%d\n", __func__,
vHeaders.front().GetHash().ToString(), pto->GetId()); vHeaders.front().GetHash().ToString(), pto->GetId());
} }
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, vHeaders)); m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::HEADERS, TX_WITH_WITNESS(vHeaders)));
state.pindexBestHeaderSent = pBestIndex; state.pindexBestHeaderSent = pBestIndex;
} else } else
fRevertToInv = true; fRevertToInv = true;

View file

@ -956,7 +956,7 @@ bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
} }
// Write index header // Write index header
unsigned int nSize = GetSerializeSize(block, fileout.GetVersion()); unsigned int nSize = GetSerializeSize(TX_WITH_WITNESS(block));
fileout << GetParams().MessageStart() << nSize; fileout << GetParams().MessageStart() << nSize;
// Write block // Write block
@ -965,7 +965,7 @@ bool BlockManager::WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const
return error("WriteBlockToDisk: ftell failed"); return error("WriteBlockToDisk: ftell failed");
} }
pos.nPos = (unsigned int)fileOutPos; pos.nPos = (unsigned int)fileOutPos;
fileout << block; fileout << TX_WITH_WITNESS(block);
return true; return true;
} }
@ -1023,7 +1023,7 @@ bool BlockManager::ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos) cons
// Read block // Read block
try { try {
filein >> block; filein >> TX_WITH_WITNESS(block);
} catch (const std::exception& e) { } catch (const std::exception& e) {
return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString()); return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
} }
@ -1092,7 +1092,7 @@ bool BlockManager::ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatF
FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp) FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, const FlatFilePos* dbp)
{ {
unsigned int nBlockSize = ::GetSerializeSize(block, CLIENT_VERSION); unsigned int nBlockSize = ::GetSerializeSize(TX_WITH_WITNESS(block));
FlatFilePos blockPos; FlatFilePos blockPos;
const auto position_known {dbp != nullptr}; const auto position_known {dbp != nullptr};
if (position_known) { if (position_known) {

View file

@ -775,7 +775,7 @@ public:
{ {
RPCRunLater(name, std::move(fn), seconds); RPCRunLater(name, std::move(fn), seconds);
} }
int rpcSerializationFlags() override { return RPCSerializationFlags(); } bool rpcSerializationWithoutWitness() override { return RPCSerializationWithoutWitness(); }
common::SettingsValue getSetting(const std::string& name) override common::SettingsValue getSetting(const std::string& name) override
{ {
return args().GetSetting(name); return args().GetSetting(name);

View file

@ -10,7 +10,7 @@
uint256 CBlockHeader::GetHash() const uint256 CBlockHeader::GetHash() const
{ {
return (CHashWriter{PROTOCOL_VERSION} << *this).GetHash(); return (HashWriter{} << *this).GetHash();
} }
std::string CBlock::ToString() const std::string CBlock::ToString() const

View file

@ -68,12 +68,12 @@ CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin),
Txid CMutableTransaction::GetHash() const Txid CMutableTransaction::GetHash() const
{ {
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash()); return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
} }
Txid CTransaction::ComputeHash() const Txid CTransaction::ComputeHash() const
{ {
return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash()); return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
} }
Wtxid CTransaction::ComputeWitnessHash() const Wtxid CTransaction::ComputeWitnessHash() const
@ -82,7 +82,7 @@ Wtxid CTransaction::ComputeWitnessHash() const
return Wtxid::FromUint256(hash.ToUint256()); return Wtxid::FromUint256(hash.ToUint256());
} }
return Wtxid::FromUint256((CHashWriter{0} << *this).GetHash()); return Wtxid::FromUint256((HashWriter{} << TX_WITH_WITNESS(*this)).GetHash());
} }
CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {} CTransaction::CTransaction(const CMutableTransaction& tx) : vin(tx.vin), vout(tx.vout), nVersion(tx.nVersion), nLockTime(tx.nLockTime), hash{ComputeHash()}, m_witness_hash{ComputeWitnessHash()} {}
@ -102,7 +102,7 @@ CAmount CTransaction::GetValueOut() const
unsigned int CTransaction::GetTotalSize() const unsigned int CTransaction::GetTotalSize() const
{ {
return ::GetSerializeSize(*this, PROTOCOL_VERSION); return ::GetSerializeSize(TX_WITH_WITNESS(*this));
} }
std::string CTransaction::ToString() const std::string CTransaction::ToString() const

View file

@ -24,14 +24,6 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
/**
* A flag that is ORed into the protocol version to designate that a transaction
* should be (un)serialized without witness data.
* Make sure that this does not collide with any of the values in `version.h`
* or with `ADDRV2_FORMAT`.
*/
static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
/** An outpoint - a combination of a transaction hash and an index n into its vout */ /** An outpoint - a combination of a transaction hash and an index n into its vout */
class COutPoint class COutPoint
{ {
@ -197,6 +189,13 @@ public:
struct CMutableTransaction; struct CMutableTransaction;
struct TransactionSerParams {
const bool allow_witness;
SER_PARAMS_OPFUNC
};
static constexpr TransactionSerParams TX_WITH_WITNESS{.allow_witness = true};
static constexpr TransactionSerParams TX_NO_WITNESS{.allow_witness = false};
/** /**
* Basic transaction serialization format: * Basic transaction serialization format:
* - int32_t nVersion * - int32_t nVersion
@ -215,8 +214,9 @@ struct CMutableTransaction;
* - uint32_t nLockTime * - uint32_t nLockTime
*/ */
template<typename Stream, typename TxType> template<typename Stream, typename TxType>
inline void UnserializeTransaction(TxType& tx, Stream& s) { void UnserializeTransaction(TxType& tx, Stream& s, const TransactionSerParams& params)
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS); {
const bool fAllowWitness = params.allow_witness;
s >> tx.nVersion; s >> tx.nVersion;
unsigned char flags = 0; unsigned char flags = 0;
@ -254,8 +254,9 @@ inline void UnserializeTransaction(TxType& tx, Stream& s) {
} }
template<typename Stream, typename TxType> template<typename Stream, typename TxType>
inline void SerializeTransaction(const TxType& tx, Stream& s) { void SerializeTransaction(const TxType& tx, Stream& s, const TransactionSerParams& params)
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS); {
const bool fAllowWitness = params.allow_witness;
s << tx.nVersion; s << tx.nVersion;
unsigned char flags = 0; unsigned char flags = 0;
@ -323,13 +324,15 @@ public:
template <typename Stream> template <typename Stream>
inline void Serialize(Stream& s) const { inline void Serialize(Stream& s) const {
SerializeTransaction(*this, s); SerializeTransaction(*this, s, s.GetParams());
} }
/** This deserializing constructor is provided instead of an Unserialize method. /** This deserializing constructor is provided instead of an Unserialize method.
* Unserialize is not possible, since it would require overwriting const fields. */ * Unserialize is not possible, since it would require overwriting const fields. */
template <typename Stream> template <typename Stream>
CTransaction(deserialize_type, Stream& s) : CTransaction(CMutableTransaction(deserialize, s)) {} CTransaction(deserialize_type, const TransactionSerParams& params, Stream& s) : CTransaction(CMutableTransaction(deserialize, params, s)) {}
template <typename Stream>
CTransaction(deserialize_type, ParamsStream<TransactionSerParams,Stream>& s) : CTransaction(CMutableTransaction(deserialize, s)) {}
bool IsNull() const { bool IsNull() const {
return vin.empty() && vout.empty(); return vin.empty() && vout.empty();
@ -389,17 +392,21 @@ struct CMutableTransaction
template <typename Stream> template <typename Stream>
inline void Serialize(Stream& s) const { inline void Serialize(Stream& s) const {
SerializeTransaction(*this, s); SerializeTransaction(*this, s, s.GetParams());
} }
template <typename Stream> template <typename Stream>
inline void Unserialize(Stream& s) { inline void Unserialize(Stream& s) {
UnserializeTransaction(*this, s); UnserializeTransaction(*this, s, s.GetParams());
} }
template <typename Stream> template <typename Stream>
CMutableTransaction(deserialize_type, Stream& s) { CMutableTransaction(deserialize_type, const TransactionSerParams& params, Stream& s) {
UnserializeTransaction(*this, s, params);
}
template <typename Stream>
CMutableTransaction(deserialize_type, ParamsStream<TransactionSerParams,Stream>& s) {
Unserialize(s); Unserialize(s);
} }

View file

@ -95,7 +95,7 @@ void SerializeToVector(Stream& s, const X&... args)
// Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
template<typename Stream, typename... X> template<typename Stream, typename... X>
void UnserializeFromVector(Stream& s, X&... args) void UnserializeFromVector(Stream& s, X&&... args)
{ {
size_t expected_size = ReadCompactSize(s); size_t expected_size = ReadCompactSize(s);
size_t remaining_before = s.size(); size_t remaining_before = s.size();
@ -226,8 +226,7 @@ struct PSBTInput
// Write the utxo // Write the utxo
if (non_witness_utxo) { if (non_witness_utxo) {
SerializeToVector(s, CompactSizeWriter(PSBT_IN_NON_WITNESS_UTXO)); SerializeToVector(s, CompactSizeWriter(PSBT_IN_NON_WITNESS_UTXO));
OverrideStream<Stream> os{&s, s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS}; SerializeToVector(s, TX_NO_WITNESS(non_witness_utxo));
SerializeToVector(os, non_witness_utxo);
} }
if (!witness_utxo.IsNull()) { if (!witness_utxo.IsNull()) {
SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESS_UTXO)); SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESS_UTXO));
@ -394,8 +393,7 @@ struct PSBTInput
throw std::ios_base::failure("Non-witness utxo key is more than one byte type"); throw std::ios_base::failure("Non-witness utxo key is more than one byte type");
} }
// Set the stream to unserialize with witness since this is always a valid network transaction // Set the stream to unserialize with witness since this is always a valid network transaction
OverrideStream<Stream> os{&s, s.GetVersion() & ~SERIALIZE_TRANSACTION_NO_WITNESS}; UnserializeFromVector(s, TX_WITH_WITNESS(non_witness_utxo));
UnserializeFromVector(os, non_witness_utxo);
break; break;
} }
case PSBT_IN_WITNESS_UTXO: case PSBT_IN_WITNESS_UTXO:
@ -984,8 +982,7 @@ struct PartiallySignedTransaction
SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_UNSIGNED_TX)); SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_UNSIGNED_TX));
// Write serialized tx to a stream // Write serialized tx to a stream
OverrideStream<Stream> os{&s, s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS}; SerializeToVector(s, TX_NO_WITNESS(*tx));
SerializeToVector(os, *tx);
// Write xpubs // Write xpubs
for (const auto& xpub_pair : m_xpubs) { for (const auto& xpub_pair : m_xpubs) {
@ -1075,8 +1072,7 @@ struct PartiallySignedTransaction
} }
CMutableTransaction mtx; CMutableTransaction mtx;
// Set the stream to serialize with non-witness since this should always be non-witness // Set the stream to serialize with non-witness since this should always be non-witness
OverrideStream<Stream> os{&s, s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS}; UnserializeFromVector(s, TX_NO_WITNESS(mtx));
UnserializeFromVector(os, mtx);
tx = std::move(mtx); tx = std::move(mtx);
// Make sure that all scriptSigs and scriptWitnesses are empty // Make sure that all scriptSigs and scriptWitnesses are empty
for (const CTxIn& txin : tx->vin) { for (const CTxIn& txin : tx->vin) {

View file

@ -260,8 +260,8 @@ void WalletModel::sendCoins(WalletModelTransaction& transaction)
auto& newTx = transaction.getWtx(); auto& newTx = transaction.getWtx();
wallet().commitTransaction(newTx, /*value_map=*/{}, std::move(vOrderForm)); wallet().commitTransaction(newTx, /*value_map=*/{}, std::move(vOrderForm));
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION); DataStream ssTx;
ssTx << *newTx; ssTx << TX_WITH_WITNESS(*newTx);
transaction_array.append((const char*)ssTx.data(), ssTx.size()); transaction_array.append((const char*)ssTx.data(), ssTx.size());
} }

View file

@ -317,8 +317,8 @@ static bool rest_block(const std::any& context,
switch (rf) { switch (rf) {
case RESTResponseFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ssBlock;
ssBlock << block; ssBlock << RPCTxSerParams(block);
std::string binaryBlock = ssBlock.str(); std::string binaryBlock = ssBlock.str();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, binaryBlock); req->WriteReply(HTTP_OK, binaryBlock);
@ -326,8 +326,8 @@ static bool rest_block(const std::any& context,
} }
case RESTResponseFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ssBlock;
ssBlock << block; ssBlock << RPCTxSerParams(block);
std::string strHex = HexStr(ssBlock) + "\n"; std::string strHex = HexStr(ssBlock) + "\n";
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, strHex); req->WriteReply(HTTP_OK, strHex);
@ -723,8 +723,8 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
switch (rf) { switch (rf) {
case RESTResponseFormat::BINARY: { case RESTResponseFormat::BINARY: {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ssTx;
ssTx << tx; ssTx << RPCTxSerParams(tx);
std::string binaryTx = ssTx.str(); std::string binaryTx = ssTx.str();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
@ -733,8 +733,8 @@ static bool rest_tx(const std::any& context, HTTPRequest* req, const std::string
} }
case RESTResponseFormat::HEX: { case RESTResponseFormat::HEX: {
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ssTx;
ssTx << tx; ssTx << RPCTxSerParams(tx);
std::string strHex = HexStr(ssTx) + "\n"; std::string strHex = HexStr(ssTx) + "\n";
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");

View file

@ -166,8 +166,8 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
{ {
UniValue result = blockheaderToJSON(tip, blockindex); UniValue result = blockheaderToJSON(tip, blockindex);
result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); result.pushKV("strippedsize", (int)::GetSerializeSize(TX_NO_WITNESS(block)));
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION)); result.pushKV("size", (int)::GetSerializeSize(TX_WITH_WITNESS(block)));
result.pushKV("weight", (int)::GetBlockWeight(block)); result.pushKV("weight", (int)::GetBlockWeight(block));
UniValue txs(UniValue::VARR); UniValue txs(UniValue::VARR);
@ -189,7 +189,7 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
// coinbase transaction (i.e. i == 0) doesn't have undo data // coinbase transaction (i.e. i == 0) doesn't have undo data
const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr; const CTxUndo* txundo = (have_undo && i > 0) ? &blockUndo.vtxundo.at(i - 1) : nullptr;
UniValue objTx(UniValue::VOBJ); UniValue objTx(UniValue::VOBJ);
TxToUniv(*tx, /*block_hash=*/uint256(), /*entry=*/objTx, /*include_hex=*/true, RPCSerializationFlags(), txundo, verbosity); TxToUniv(*tx, /*block_hash=*/uint256(), /*entry=*/objTx, /*include_hex=*/true, /*without_witness=*/RPCSerializationWithoutWitness(), txundo, verbosity);
txs.push_back(objTx); txs.push_back(objTx);
} }
break; break;
@ -740,8 +740,8 @@ static RPCHelpMan getblock()
if (verbosity <= 0) if (verbosity <= 0)
{ {
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ssBlock;
ssBlock << block; ssBlock << RPCTxSerParams(block);
std::string strHex = HexStr(ssBlock); std::string strHex = HexStr(ssBlock);
return strHex; return strHex;
} }

View file

@ -390,8 +390,8 @@ static RPCHelpMan generateblock()
UniValue obj(UniValue::VOBJ); UniValue obj(UniValue::VOBJ);
obj.pushKV("hash", block_out->GetHash().GetHex()); obj.pushKV("hash", block_out->GetHash().GetHex());
if (!process_new_block) { if (!process_new_block) {
CDataStream block_ser{SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()}; DataStream block_ser;
block_ser << *block_out; block_ser << RPCTxSerParams(*block_out);
obj.pushKV("hex", HexStr(block_ser)); obj.pushKV("hex", HexStr(block_ser));
} }
return obj; return obj;

View file

@ -62,7 +62,7 @@ static void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue&
// Blockchain contextual information (confirmations and blocktime) is not // Blockchain contextual information (confirmations and blocktime) is not
// available to code in bitcoin-common, so we query them here and push the // available to code in bitcoin-common, so we query them here and push the
// data into the returned UniValue. // data into the returned UniValue.
TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationFlags(), txundo, verbosity); TxToUniv(tx, /*block_hash=*/uint256(), entry, /*include_hex=*/true, RPCSerializationWithoutWitness(), txundo, verbosity);
if (!hashBlock.IsNull()) { if (!hashBlock.IsNull()) {
LOCK(cs_main); LOCK(cs_main);
@ -383,7 +383,7 @@ static RPCHelpMan getrawtransaction()
} }
if (verbosity <= 0) { if (verbosity <= 0) {
return EncodeHexTx(*tx, RPCSerializationFlags()); return EncodeHexTx(*tx, /*without_witness=*/RPCSerializationWithoutWitness());
} }
UniValue result(UniValue::VOBJ); UniValue result(UniValue::VOBJ);
@ -1541,7 +1541,7 @@ static RPCHelpMan finalizepsbt()
std::string result_str; std::string result_str;
if (complete && extract) { if (complete && extract) {
ssTx << mtx; ssTx << TX_WITH_WITNESS(mtx);
result_str = HexStr(ssTx); result_str = HexStr(ssTx);
result.pushKV("hex", result_str); result.pushKV("hex", result_str);
} else { } else {
@ -1994,8 +1994,8 @@ RPCHelpMan descriptorprocesspsbt()
CMutableTransaction mtx; CMutableTransaction mtx;
PartiallySignedTransaction psbtx_copy = psbtx; PartiallySignedTransaction psbtx_copy = psbtx;
CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx_copy, mtx)); CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx_copy, mtx));
CDataStream ssTx_final(SER_NETWORK, PROTOCOL_VERSION); DataStream ssTx_final;
ssTx_final << mtx; ssTx_final << TX_WITH_WITNESS(mtx);
result.pushKV("hex", HexStr(ssTx_final)); result.pushKV("hex", HexStr(ssTx_final));
} }
return result; return result;

View file

@ -595,12 +595,9 @@ void RPCRunLater(const std::string& name, std::function<void()> func, int64_t nS
deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000))); deadlineTimers.emplace(name, std::unique_ptr<RPCTimerBase>(timerInterface->NewTimer(func, nSeconds*1000)));
} }
int RPCSerializationFlags() bool RPCSerializationWithoutWitness()
{ {
int flag = 0; return (gArgs.GetIntArg("-rpcserialversion", DEFAULT_RPC_SERIALIZE_VERSION) == 0);
if (gArgs.GetIntArg("-rpcserialversion", DEFAULT_RPC_SERIALIZE_VERSION) == 0)
flag |= SERIALIZE_TRANSACTION_NO_WITNESS;
return flag;
} }
CRPCTable tableRPC; CRPCTable tableRPC;

View file

@ -183,7 +183,14 @@ void InterruptRPC();
void StopRPC(); void StopRPC();
std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq); std::string JSONRPCExecBatch(const JSONRPCRequest& jreq, const UniValue& vReq);
// Retrieves any serialization flags requested in command line argument // Drop witness when serializing for RPC?
int RPCSerializationFlags(); bool RPCSerializationWithoutWitness();
template<typename T>
auto RPCTxSerParams(T&& t)
{
if (RPCSerializationWithoutWitness()) return TX_NO_WITNESS(t);
return TX_WITH_WITNESS(t);
}
#endif // BITCOIN_RPC_SERVER_H #endif // BITCOIN_RPC_SERVER_H

View file

@ -16,8 +16,7 @@ namespace {
class TxInputStream class TxInputStream
{ {
public: public:
TxInputStream(int nVersionIn, const unsigned char *txTo, size_t txToLen) : TxInputStream(const unsigned char *txTo, size_t txToLen) :
m_version(nVersionIn),
m_data(txTo), m_data(txTo),
m_remaining(txToLen) m_remaining(txToLen)
{} {}
@ -48,9 +47,7 @@ public:
return *this; return *this;
} }
int GetVersion() const { return m_version; }
private: private:
const int m_version;
const unsigned char* m_data; const unsigned char* m_data;
size_t m_remaining; size_t m_remaining;
}; };
@ -84,8 +81,8 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
} }
try { try {
TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen); TxInputStream stream(txTo, txToLen);
CTransaction tx(deserialize, stream); CTransaction tx(deserialize, TX_WITH_WITNESS, stream);
std::vector<CTxOut> spent_outputs; std::vector<CTxOut> spent_outputs;
if (spentOutputs != nullptr) { if (spentOutputs != nullptr) {
@ -102,7 +99,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
if (nIn >= tx.vin.size()) if (nIn >= tx.vin.size())
return set_error(err, bitcoinconsensus_ERR_TX_INDEX); return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen) if (GetSerializeSize(TX_WITH_WITNESS(tx)) != txToLen)
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH); return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
// Regardless of the verification result, the tx did not error. // Regardless of the verification result, the tx did not error.

View file

@ -45,45 +45,6 @@ inline void Xor(Span<std::byte> write, Span<const std::byte> key, size_t key_off
} }
} // namespace util } // namespace util
template<typename Stream>
class OverrideStream
{
Stream* stream;
const int nVersion;
public:
OverrideStream(Stream* stream_, int nVersion_) : stream{stream_}, nVersion{nVersion_} {}
template<typename T>
OverrideStream<Stream>& operator<<(const T& obj)
{
::Serialize(*this, obj);
return (*this);
}
template<typename T>
OverrideStream<Stream>& operator>>(T&& obj)
{
::Unserialize(*this, obj);
return (*this);
}
void write(Span<const std::byte> src)
{
stream->write(src);
}
void read(Span<std::byte> dst)
{
stream->read(dst);
}
int GetVersion() const { return nVersion; }
size_t size() const { return stream->size(); }
void ignore(size_t size) { return stream->ignore(size); }
};
/* Minimal stream for overwriting and/or appending to an existing byte vector /* Minimal stream for overwriting and/or appending to an existing byte vector
* *
* The referenced vector will grow as necessary * The referenced vector will grow as necessary

View file

@ -49,7 +49,7 @@ BOOST_AUTO_TEST_CASE(blockmanager_find_block_pos)
// 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293 // 8 bytes (for serialization header) + 285 (for serialized genesis block) = 293
// add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301 // add another 8 bytes for the second block's serialization header and we get 293 + 8 = 301
FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, nullptr)}; FlatFilePos actual{blockman.SaveBlockToDisk(params->GenesisBlock(), 1, nullptr)};
BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(params->GenesisBlock(), CLIENT_VERSION) + BLOCK_SERIALIZATION_HEADER_SIZE); BOOST_CHECK_EQUAL(actual.nPos, BLOCK_SERIALIZATION_HEADER_SIZE + ::GetSerializeSize(TX_WITH_WITNESS(params->GenesisBlock())) + BLOCK_SERIALIZATION_HEADER_SIZE);
} }
BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup) BOOST_FIXTURE_TEST_CASE(blockmanager_scan_unlink_already_pruned_files, TestChain100Setup)

File diff suppressed because one or more lines are too long

View file

@ -31,7 +31,7 @@ FUZZ_TARGET(block, .init = initialize_block)
int nVersion; int nVersion;
ds >> nVersion; ds >> nVersion;
ds.SetVersion(nVersion); ds.SetVersion(nVersion);
ds >> block; ds >> TX_WITH_WITNESS(block);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
return; return;
} }

View file

@ -58,7 +58,7 @@ FUZZ_TARGET(bloom_filter)
assert(present); assert(present);
}, },
[&] { [&] {
const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mut_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!mut_tx) { if (!mut_tx) {
good_data = false; good_data = false;
return; return;

View file

@ -114,7 +114,7 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
random_coin = *opt_coin; random_coin = *opt_coin;
}, },
[&] { [&] {
const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!opt_mutable_transaction) { if (!opt_mutable_transaction) {
good_data = false; good_data = false;
return; return;

View file

@ -217,7 +217,7 @@ FUZZ_TARGET_DESERIALIZE(psbt_output_deserialize, {
}) })
FUZZ_TARGET_DESERIALIZE(block_deserialize, { FUZZ_TARGET_DESERIALIZE(block_deserialize, {
CBlock block; CBlock block;
DeserializeFromFuzzingInput(buffer, block); DeserializeFromFuzzingInput(buffer, TX_WITH_WITNESS(block));
}) })
FUZZ_TARGET_DESERIALIZE(blocklocator_deserialize, { FUZZ_TARGET_DESERIALIZE(blocklocator_deserialize, {
CBlockLocator bl; CBlockLocator bl;
@ -225,7 +225,7 @@ FUZZ_TARGET_DESERIALIZE(blocklocator_deserialize, {
}) })
FUZZ_TARGET_DESERIALIZE(blockmerkleroot, { FUZZ_TARGET_DESERIALIZE(blockmerkleroot, {
CBlock block; CBlock block;
DeserializeFromFuzzingInput(buffer, block); DeserializeFromFuzzingInput(buffer, TX_WITH_WITNESS(block));
bool mutated; bool mutated;
BlockMerkleRoot(block, &mutated); BlockMerkleRoot(block, &mutated);
}) })

View file

@ -27,7 +27,7 @@ FUZZ_TARGET(merkleblock)
}, },
[&] { [&] {
CMerkleBlock merkle_block; CMerkleBlock merkle_block;
const std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider); const std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS);
CBloomFilter bloom_filter; CBloomFilter bloom_filter;
std::set<uint256> txids; std::set<uint256> txids;
if (opt_block && !opt_block->vtx.empty()) { if (opt_block && !opt_block->vtx.empty()) {

View file

@ -44,7 +44,7 @@ FUZZ_TARGET(partially_downloaded_block, .init = initialize_pdb)
{ {
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider)}; auto block{ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS)};
if (!block || block->vtx.size() == 0 || if (!block || block->vtx.size() == 0 ||
block->vtx.size() >= std::numeric_limits<uint16_t>::max()) { block->vtx.size() >= std::numeric_limits<uint16_t>::max()) {
return; return;

View file

@ -37,7 +37,7 @@ FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
CallOneOf( CallOneOf(
fuzzed_data_provider, fuzzed_data_provider,
[&] { [&] {
const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!mtx) { if (!mtx) {
good_data = false; good_data = false;
return; return;
@ -52,7 +52,7 @@ FUZZ_TARGET(policy_estimator, .init = initialize_policy_estimator)
std::vector<CTxMemPoolEntry> mempool_entries; std::vector<CTxMemPoolEntry> mempool_entries;
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
{ {
const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!mtx) { if (!mtx) {
good_data = false; good_data = false;
break; break;

View file

@ -24,8 +24,8 @@ FUZZ_TARGET(primitives_transaction)
const CTxOut tx_out_1{ConsumeMoney(fuzzed_data_provider), script}; const CTxOut tx_out_1{ConsumeMoney(fuzzed_data_provider), script};
const CTxOut tx_out_2{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)}; const CTxOut tx_out_2{ConsumeMoney(fuzzed_data_provider), ConsumeScript(fuzzed_data_provider)};
assert((tx_out_1 == tx_out_2) != (tx_out_1 != tx_out_2)); assert((tx_out_1 == tx_out_2) != (tx_out_1 != tx_out_2));
const std::optional<CMutableTransaction> mutable_tx_1 = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mutable_tx_1 = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
const std::optional<CMutableTransaction> mutable_tx_2 = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mutable_tx_2 = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (mutable_tx_1 && mutable_tx_2) { if (mutable_tx_1 && mutable_tx_2) {
const CTransaction tx_1{*mutable_tx_1}; const CTransaction tx_1{*mutable_tx_1};
const CTransaction tx_2{*mutable_tx_2}; const CTransaction tx_2{*mutable_tx_2};

View file

@ -33,7 +33,7 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
{ {
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
SetMockTime(ConsumeTime(fuzzed_data_provider)); SetMockTime(ConsumeTime(fuzzed_data_provider));
std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!mtx) { if (!mtx) {
return; return;
} }
@ -42,7 +42,7 @@ FUZZ_TARGET(rbf, .init = initialize_rbf)
LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000)
{ {
const std::optional<CMutableTransaction> another_mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> another_mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!another_mtx) { if (!another_mtx) {
break; break;
} }

View file

@ -249,13 +249,13 @@ std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider, b
}, },
[&] { [&] {
// hex encoded block // hex encoded block
std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider); std::optional<CBlock> opt_block = ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!opt_block) { if (!opt_block) {
good_data = false; good_data = false;
return; return;
} }
CDataStream data_stream{SER_NETWORK, PROTOCOL_VERSION}; CDataStream data_stream{SER_NETWORK, PROTOCOL_VERSION};
data_stream << *opt_block; data_stream << TX_WITH_WITNESS(*opt_block);
r = HexStr(data_stream); r = HexStr(data_stream);
}, },
[&] { [&] {
@ -271,13 +271,14 @@ std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider, b
}, },
[&] { [&] {
// hex encoded tx // hex encoded tx
std::optional<CMutableTransaction> opt_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); std::optional<CMutableTransaction> opt_tx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!opt_tx) { if (!opt_tx) {
good_data = false; good_data = false;
return; return;
} }
CDataStream data_stream{SER_NETWORK, fuzzed_data_provider.ConsumeBool() ? PROTOCOL_VERSION : (PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)}; DataStream data_stream;
data_stream << *opt_tx; auto allow_witness = (fuzzed_data_provider.ConsumeBool() ? TX_WITH_WITNESS : TX_NO_WITNESS);
data_stream << allow_witness(*opt_tx);
r = HexStr(data_stream); r = HexStr(data_stream);
}, },
[&] { [&] {

View file

@ -54,7 +54,7 @@ CMutableTransaction TxFromHex(const std::string& str)
{ {
CMutableTransaction tx; CMutableTransaction tx;
try { try {
SpanReader{SERIALIZE_TRANSACTION_NO_WITNESS, CheckedParseHex(str)} >> tx; SpanReader{0, CheckedParseHex(str)} >> TX_NO_WITNESS(tx);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
throw std::runtime_error("Tx deserialization failure"); throw std::runtime_error("Tx deserialization failure");
} }

View file

@ -23,7 +23,7 @@ FUZZ_TARGET(script_flags)
} }
try { try {
const CTransaction tx(deserialize, ds); const CTransaction tx(deserialize, TX_WITH_WITNESS, ds);
unsigned int verify_flags; unsigned int verify_flags;
ds >> verify_flags; ds >> verify_flags;

View file

@ -20,13 +20,13 @@ FUZZ_TARGET(script_interpreter)
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
{ {
const CScript script_code = ConsumeScript(fuzzed_data_provider); const CScript script_code = ConsumeScript(fuzzed_data_provider);
const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mtx = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (mtx) { if (mtx) {
const CTransaction tx_to{*mtx}; const CTransaction tx_to{*mtx};
const unsigned int in = fuzzed_data_provider.ConsumeIntegral<unsigned int>(); const unsigned int in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
if (in < tx_to.vin.size()) { if (in < tx_to.vin.size()) {
(void)SignatureHash(script_code, tx_to, in, fuzzed_data_provider.ConsumeIntegral<int>(), ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0}), nullptr); (void)SignatureHash(script_code, tx_to, in, fuzzed_data_provider.ConsumeIntegral<int>(), ConsumeMoney(fuzzed_data_provider), fuzzed_data_provider.PickValueInArray({SigVersion::BASE, SigVersion::WITNESS_V0}), nullptr);
const std::optional<CMutableTransaction> mtx_precomputed = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mtx_precomputed = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (mtx_precomputed) { if (mtx_precomputed) {
const CTransaction tx_precomputed{*mtx_precomputed}; const CTransaction tx_precomputed{*mtx_precomputed};
const PrecomputedTransactionData precomputed_transaction_data{tx_precomputed}; const PrecomputedTransactionData precomputed_transaction_data{tx_precomputed};

View file

@ -30,7 +30,7 @@ FUZZ_TARGET(script_sigcache, .init = initialize_script_sigcache)
{ {
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size()); FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
const CTransaction tx{mutable_transaction ? *mutable_transaction : CMutableTransaction{}}; const CTransaction tx{mutable_transaction ? *mutable_transaction : CMutableTransaction{}};
const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>(); const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
const CAmount amount = ConsumeMoney(fuzzed_data_provider); const CAmount amount = ConsumeMoney(fuzzed_data_provider);

View file

@ -86,7 +86,7 @@ FUZZ_TARGET(script_sign, .init = initialize_script_sign)
} }
{ {
const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
const std::optional<CTxOut> tx_out = ConsumeDeserializable<CTxOut>(fuzzed_data_provider); const std::optional<CTxOut> tx_out = ConsumeDeserializable<CTxOut>(fuzzed_data_provider);
const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>(); const unsigned int n_in = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
if (mutable_transaction && tx_out && mutable_transaction->vin.size() > n_in) { if (mutable_transaction && tx_out && mutable_transaction->vin.size() > n_in) {
@ -100,7 +100,7 @@ FUZZ_TARGET(script_sign, .init = initialize_script_sign)
if (mutable_transaction) { if (mutable_transaction) {
CTransaction tx_from{*mutable_transaction}; CTransaction tx_from{*mutable_transaction};
CMutableTransaction tx_to; CMutableTransaction tx_to;
const std::optional<CMutableTransaction> opt_tx_to = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider); const std::optional<CMutableTransaction> opt_tx_to = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
if (opt_tx_to) { if (opt_tx_to) {
tx_to = *opt_tx_to; tx_to = *opt_tx_to;
} }

View file

@ -25,7 +25,7 @@ void initialize_signet()
FUZZ_TARGET(signet, .init = initialize_signet) FUZZ_TARGET(signet, .init = initialize_signet)
{ {
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()}; FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
const std::optional<CBlock> block = ConsumeDeserializable<CBlock>(fuzzed_data_provider); const std::optional<CBlock> block = ConsumeDeserializable<CBlock>(fuzzed_data_provider, TX_WITH_WITNESS);
if (!block) { if (!block) {
return; return;
} }

View file

@ -40,7 +40,7 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
bool valid_tx = true; bool valid_tx = true;
const CTransaction tx = [&] { const CTransaction tx = [&] {
try { try {
return CTransaction(deserialize, ds); return CTransaction(deserialize, TX_WITH_WITNESS, ds);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
valid_tx = false; valid_tx = false;
return CTransaction{CMutableTransaction{}}; return CTransaction{CMutableTransaction{}};
@ -53,7 +53,7 @@ FUZZ_TARGET(transaction, .init = initialize_transaction)
int nVersion; int nVersion;
ds_mtx >> nVersion; ds_mtx >> nVersion;
ds_mtx.SetVersion(nVersion); ds_mtx.SetVersion(nVersion);
ds_mtx >> mutable_tx; ds_mtx >> TX_WITH_WITNESS(mutable_tx);
} catch (const std::ios_base::failure&) { } catch (const std::ios_base::failure&) {
valid_mutable_tx = false; valid_mutable_tx = false;
} }

View file

@ -122,12 +122,12 @@ BOOST_AUTO_TEST_CASE(siphash)
(uint64_t(x+4)<<32)|(uint64_t(x+5)<<40)|(uint64_t(x+6)<<48)|(uint64_t(x+7)<<56)); (uint64_t(x+4)<<32)|(uint64_t(x+5)<<40)|(uint64_t(x+6)<<48)|(uint64_t(x+7)<<56));
} }
CHashWriter ss{CLIENT_VERSION}; HashWriter ss{};
CMutableTransaction tx; CMutableTransaction tx;
// Note these tests were originally written with tx.nVersion=1 // Note these tests were originally written with tx.nVersion=1
// and the test would be affected by default tx version bumps if not fixed. // and the test would be affected by default tx version bumps if not fixed.
tx.nVersion = 1; tx.nVersion = 1;
ss << tx; ss << TX_WITH_WITNESS(tx);
BOOST_CHECK_EQUAL(SipHashUint256(1, 2, ss.GetHash()), 0x79751e980c2a0a35ULL); BOOST_CHECK_EQUAL(SipHashUint256(1, 2, ss.GetHash()), 0x79751e980c2a0a35ULL);
// Check consistency between CSipHasher and SipHashUint256[Extra]. // Check consistency between CSipHasher and SipHashUint256[Extra].

View file

@ -144,7 +144,7 @@ void MinerTestingSetup::TestPackageSelection(const CScript& scriptPubKey, const
tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee tx.vout[0].nValue = 5000000000LL - 1000 - 50000; // 0 fee
uint256 hashFreeTx = tx.GetHash(); uint256 hashFreeTx = tx.GetHash();
tx_mempool.addUnchecked(entry.Fee(0).FromTx(tx)); tx_mempool.addUnchecked(entry.Fee(0).FromTx(tx));
size_t freeTxSize = ::GetSerializeSize(tx, PROTOCOL_VERSION); size_t freeTxSize = ::GetSerializeSize(TX_WITH_WITNESS(tx));
// Calculate a fee on child transaction that will put the package just // Calculate a fee on child transaction that will put the package just
// below the block min tx fee (assuming 1 child tx of the same size). // below the block min tx fee (assuming 1 child tx of the same size).

View file

@ -141,8 +141,8 @@ void DoTest(const CScript& scriptPubKey, const CScript& scriptSig, const CScript
} }
#if defined(HAVE_CONSENSUS_LIB) #if defined(HAVE_CONSENSUS_LIB)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << tx2; stream << TX_WITH_WITNESS(tx2);
uint32_t libconsensus_flags{flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL}; uint32_t libconsensus_flags{flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL};
if (libconsensus_flags == flags) { if (libconsensus_flags == flags) {
int expectedSuccessCode = expect ? 1 : 0; int expectedSuccessCode = expect ? 1 : 0;
@ -1470,7 +1470,7 @@ BOOST_AUTO_TEST_CASE(script_HasValidOps)
static CMutableTransaction TxFromHex(const std::string& str) static CMutableTransaction TxFromHex(const std::string& str)
{ {
CMutableTransaction tx; CMutableTransaction tx;
SpanReader{SERIALIZE_TRANSACTION_NO_WITNESS, ParseHex(str)} >> tx; SpanReader{0, ParseHex(str)} >> TX_NO_WITNESS(tx);
return tx; return tx;
} }
@ -1513,8 +1513,8 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_returns_true)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << spendTx; stream << TX_WITH_WITNESS(spendTx);
bitcoinconsensus_error err; bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err); int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
@ -1536,8 +1536,8 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_index_err)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << spendTx; stream << TX_WITH_WITNESS(spendTx);
bitcoinconsensus_error err; bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err); int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
@ -1559,8 +1559,8 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_size)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << spendTx; stream << TX_WITH_WITNESS(spendTx);
bitcoinconsensus_error err; bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size() * 2, nIn, libconsensus_flags, &err); int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size() * 2, nIn, libconsensus_flags, &err);
@ -1582,7 +1582,7 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_serialization)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << 0xffffffff; stream << 0xffffffff;
bitcoinconsensus_error err; bitcoinconsensus_error err;
@ -1605,8 +1605,8 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_amount_required_err)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << spendTx; stream << TX_WITH_WITNESS(spendTx);
bitcoinconsensus_error err; bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err); int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
@ -1628,8 +1628,8 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_invalid_flags)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << spendTx; stream << TX_WITH_WITNESS(spendTx);
bitcoinconsensus_error err; bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err); int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), UCharCast(stream.data()), stream.size(), nIn, libconsensus_flags, &err);
@ -1651,8 +1651,8 @@ BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_spent_outputs_required_err)
CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)}; CTransaction creditTx{BuildCreditingTransaction(scriptPubKey, 1)};
CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)}; CTransaction spendTx{BuildSpendingTransaction(scriptSig, wit, creditTx)};
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << spendTx; stream << TX_WITH_WITNESS(spendTx);
bitcoinconsensus_error err; bitcoinconsensus_error err;
int result{bitcoinconsensus_verify_script_with_spent_outputs(scriptPubKey.data(), scriptPubKey.size(), creditTx.vout[0].nValue, UCharCast(stream.data()), stream.size(), nullptr, 0, nIn, libconsensus_flags, &err)}; int result{bitcoinconsensus_verify_script_with_spent_outputs(scriptPubKey.data(), scriptPubKey.size(), creditTx.vout[0].nValue, UCharCast(stream.data()), stream.size(), nullptr, 0, nIn, libconsensus_flags, &err)};
@ -1718,8 +1718,8 @@ static void AssetTest(const UniValue& test)
CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata); CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
#if defined(HAVE_CONSENSUS_LIB) #if defined(HAVE_CONSENSUS_LIB)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << tx; stream << TX_WITH_WITNESS(tx);
std::vector<UTXO> utxos; std::vector<UTXO> utxos;
utxos.resize(prevouts.size()); utxos.resize(prevouts.size());
for (size_t i = 0; i < prevouts.size(); i++) { for (size_t i = 0; i < prevouts.size(); i++) {
@ -1752,8 +1752,8 @@ static void AssetTest(const UniValue& test)
CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata); CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
#if defined(HAVE_CONSENSUS_LIB) #if defined(HAVE_CONSENSUS_LIB)
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION); DataStream stream;
stream << tx; stream << TX_WITH_WITNESS(tx);
std::vector<UTXO> utxos; std::vector<UTXO> utxos;
utxos.resize(prevouts.size()); utxos.resize(prevouts.size());
for (size_t i = 0; i < prevouts.size(); i++) { for (size_t i = 0; i < prevouts.size(); i++) {
@ -1816,7 +1816,7 @@ BOOST_AUTO_TEST_CASE(bip341_keypath_test_vectors)
for (const auto& vec : vectors.getValues()) { for (const auto& vec : vectors.getValues()) {
auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str()); auto txhex = ParseHex(vec["given"]["rawUnsignedTx"].get_str());
CMutableTransaction tx; CMutableTransaction tx;
SpanReader{PROTOCOL_VERSION, txhex} >> tx; SpanReader{PROTOCOL_VERSION, txhex} >> TX_WITH_WITNESS(tx);
std::vector<CTxOut> utxos; std::vector<CTxOut> utxos;
for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) { for (const auto& utxo_spent : vec["given"]["utxosSpent"].getValues()) {
auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str()); auto script_bytes = ParseHex(utxo_spent["scriptPubKey"].get_str());

View file

@ -36,7 +36,7 @@ public:
READWRITE(obj.boolval); READWRITE(obj.boolval);
READWRITE(obj.stringval); READWRITE(obj.stringval);
READWRITE(obj.charstrval); READWRITE(obj.charstrval);
READWRITE(obj.txval); READWRITE(TX_WITH_WITNESS(obj.txval));
} }
bool operator==(const CSerializeMethodsTestSingle& rhs) const bool operator==(const CSerializeMethodsTestSingle& rhs) const
@ -56,7 +56,7 @@ public:
SERIALIZE_METHODS(CSerializeMethodsTestMany, obj) SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
{ {
READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, obj.txval); READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval));
} }
}; };
@ -240,7 +240,7 @@ BOOST_AUTO_TEST_CASE(class_methods)
CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref); CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
CSerializeMethodsTestSingle methodtest3; CSerializeMethodsTestSingle methodtest3;
CSerializeMethodsTestMany methodtest4; CSerializeMethodsTestMany methodtest4;
CDataStream ss(SER_DISK, PROTOCOL_VERSION); DataStream ss;
BOOST_CHECK(methodtest1 == methodtest2); BOOST_CHECK(methodtest1 == methodtest2);
ss << methodtest1; ss << methodtest1;
ss >> methodtest4; ss >> methodtest4;
@ -250,8 +250,8 @@ BOOST_AUTO_TEST_CASE(class_methods)
BOOST_CHECK(methodtest2 == methodtest3); BOOST_CHECK(methodtest2 == methodtest3);
BOOST_CHECK(methodtest3 == methodtest4); BOOST_CHECK(methodtest3 == methodtest4);
CDataStream ss2{SER_DISK, PROTOCOL_VERSION}; DataStream ss2;
ss2 << intval << boolval << stringval << charstrval << txval; ss2 << intval << boolval << stringval << charstrval << TX_WITH_WITNESS(txval);
ss2 >> methodtest3; ss2 >> methodtest3;
BOOST_CHECK(methodtest3 == methodtest4); BOOST_CHECK(methodtest3 == methodtest4);
{ {

View file

@ -78,8 +78,8 @@ uint256 static SignatureHashOld(CScript scriptCode, const CTransaction& txTo, un
} }
// Serialize and hash // Serialize and hash
CHashWriter ss{SERIALIZE_TRANSACTION_NO_WITNESS}; HashWriter ss{};
ss << txTmp << nHashType; ss << TX_NO_WITNESS(txTmp) << nHashType;
return ss.GetHash(); return ss.GetHash();
} }
@ -138,8 +138,8 @@ BOOST_AUTO_TEST_CASE(sighash_test)
sho = SignatureHashOld(scriptCode, CTransaction(txTo), nIn, nHashType); sho = SignatureHashOld(scriptCode, CTransaction(txTo), nIn, nHashType);
sh = SignatureHash(scriptCode, txTo, nIn, nHashType, 0, SigVersion::BASE); sh = SignatureHash(scriptCode, txTo, nIn, nHashType, 0, SigVersion::BASE);
#if defined(PRINT_SIGHASH_JSON) #if defined(PRINT_SIGHASH_JSON)
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); DataStream ss;
ss << txTo; ss << TX_WITH_WITNESS(txTo);
std::cout << "\t[\"" ; std::cout << "\t[\"" ;
std::cout << HexStr(ss) << "\", \""; std::cout << HexStr(ss) << "\", \"";
@ -188,8 +188,8 @@ BOOST_AUTO_TEST_CASE(sighash_from_data)
nHashType = test[3].getInt<int>(); nHashType = test[3].getInt<int>();
sigHashHex = test[4].get_str(); sigHashHex = test[4].get_str();
CDataStream stream(ParseHex(raw_tx), SER_NETWORK, PROTOCOL_VERSION); DataStream stream(ParseHex(raw_tx));
stream >> tx; stream >> TX_WITH_WITNESS(tx);
TxValidationState state; TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest); BOOST_CHECK_MESSAGE(CheckTransaction(*tx, state), strTest);

View file

@ -233,8 +233,8 @@ BOOST_AUTO_TEST_CASE(tx_valid)
} }
std::string transaction = test[1].get_str(); std::string transaction = test[1].get_str();
CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION); DataStream stream(ParseHex(transaction));
CTransaction tx(deserialize, stream); CTransaction tx(deserialize, TX_WITH_WITNESS, stream);
TxValidationState state; TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest); BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
@ -321,8 +321,8 @@ BOOST_AUTO_TEST_CASE(tx_invalid)
} }
std::string transaction = test[1].get_str(); std::string transaction = test[1].get_str();
CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION ); DataStream stream(ParseHex(transaction));
CTransaction tx(deserialize, stream); CTransaction tx(deserialize, TX_WITH_WITNESS, stream);
TxValidationState state; TxValidationState state;
if (!CheckTransaction(tx, state) || state.IsInvalid()) { if (!CheckTransaction(tx, state) || state.IsInvalid()) {
@ -371,9 +371,9 @@ BOOST_AUTO_TEST_CASE(basic_transaction_tests)
// Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436) // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00}; unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1); std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
CDataStream stream(vch, SER_DISK, CLIENT_VERSION); DataStream stream(vch);
CMutableTransaction tx; CMutableTransaction tx;
stream >> tx; stream >> TX_WITH_WITNESS(tx);
TxValidationState state; TxValidationState state;
BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid."); BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");
@ -418,9 +418,9 @@ static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const
outputm.vout.resize(1); outputm.vout.resize(1);
outputm.vout[0].nValue = 1; outputm.vout[0].nValue = 1;
outputm.vout[0].scriptPubKey = outscript; outputm.vout[0].scriptPubKey = outscript;
CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION); DataStream ssout;
ssout << outputm; ssout << TX_WITH_WITNESS(outputm);
ssout >> output; ssout >> TX_WITH_WITNESS(output);
assert(output->vin.size() == 1); assert(output->vin.size() == 1);
assert(output->vin[0] == outputm.vin[0]); assert(output->vin[0] == outputm.vin[0]);
assert(output->vout.size() == 1); assert(output->vout.size() == 1);
@ -437,9 +437,9 @@ static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const
SignatureData empty; SignatureData empty;
bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL, empty); bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL, empty);
assert(ret == success); assert(ret == success);
CDataStream ssin(SER_NETWORK, PROTOCOL_VERSION); DataStream ssin;
ssin << inputm; ssin << TX_WITH_WITNESS(inputm);
ssin >> input; ssin >> TX_WITH_WITNESS(input);
assert(input.vin.size() == 1); assert(input.vin.size() == 1);
assert(input.vin[0] == inputm.vin[0]); assert(input.vin[0] == inputm.vin[0]);
assert(input.vout.size() == 1); assert(input.vout.size() == 1);
@ -524,9 +524,9 @@ BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
assert(hashSigned); assert(hashSigned);
} }
CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION); DataStream ssout;
ssout << mtx; ssout << TX_WITH_WITNESS(mtx);
CTransaction tx(deserialize, ssout); CTransaction tx(deserialize, TX_WITH_WITNESS, ssout);
// check all inputs concurrently, with the cache // check all inputs concurrently, with the cache
PrecomputedTransactionData txdata(tx); PrecomputedTransactionData txdata(tx);

File diff suppressed because one or more lines are too long

View file

@ -722,7 +722,7 @@ bool MemPoolAccept::PreChecks(ATMPArgs& args, Workspace& ws)
} }
// Transactions smaller than 65 non-witness bytes are not relayed to mitigate CVE-2017-12842. // Transactions smaller than 65 non-witness bytes are not relayed to mitigate CVE-2017-12842.
if (::GetSerializeSize(tx, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) < MIN_STANDARD_TX_NONWITNESS_SIZE) if (::GetSerializeSize(TX_NO_WITNESS(tx)) < MIN_STANDARD_TX_NONWITNESS_SIZE)
return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "tx-size-small"); return state.Invalid(TxValidationResult::TX_NOT_STANDARD, "tx-size-small");
// Only accept nLockTime-using transactions that can be mined in the next // Only accept nLockTime-using transactions that can be mined in the next
@ -3685,7 +3685,7 @@ bool CheckBlock(const CBlock& block, BlockValidationState& state, const Consensu
// checks that use witness data may be performed here. // checks that use witness data may be performed here.
// Size limits // Size limits
if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT) if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(TX_NO_WITNESS(block)) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed"); return state.Invalid(BlockValidationResult::BLOCK_CONSENSUS, "bad-blk-length", "size limits failed");
// First transaction must be coinbase, the rest must not be // First transaction must be coinbase, the rest must not be
@ -4724,7 +4724,7 @@ void ChainstateManager::LoadExternalBlockFile(
// This block can be processed immediately; rewind to its start, read and deserialize it. // This block can be processed immediately; rewind to its start, read and deserialize it.
blkdat.SetPos(nBlockPos); blkdat.SetPos(nBlockPos);
pblock = std::make_shared<CBlock>(); pblock = std::make_shared<CBlock>();
blkdat >> *pblock; blkdat >> TX_WITH_WITNESS(*pblock);
nRewind = blkdat.GetPos(); nRewind = blkdat.GetPos();
BlockValidationState state; BlockValidationState state;

View file

@ -35,7 +35,4 @@ static const int INVALID_CB_NO_BAN_VERSION = 70015;
//! "wtxidrelay" command for wtxid-based relay starts with this version //! "wtxidrelay" command for wtxid-based relay starts with this version
static const int WTXID_RELAY_VERSION = 70016; static const int WTXID_RELAY_VERSION = 70016;
// Make sure that none of the values above collide with
// `SERIALIZE_TRANSACTION_NO_WITNESS`.
#endif // BITCOIN_VERSION_H #endif // BITCOIN_VERSION_H

View file

@ -1617,8 +1617,8 @@ RPCHelpMan walletprocesspsbt()
CMutableTransaction mtx; CMutableTransaction mtx;
// Returns true if complete, which we already think it is. // Returns true if complete, which we already think it is.
CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx, mtx)); CHECK_NONFATAL(FinalizeAndExtractPSBT(psbtx, mtx));
CDataStream ssTx_final(SER_NETWORK, PROTOCOL_VERSION); DataStream ssTx_final;
ssTx_final << mtx; ssTx_final << TX_WITH_WITNESS(mtx);
result.pushKV("hex", HexStr(ssTx_final)); result.pushKV("hex", HexStr(ssTx_final));
} }

View file

@ -783,7 +783,7 @@ RPCHelpMan gettransaction()
ListTransactions(*pwallet, wtx, 0, false, details, filter, /*filter_label=*/std::nullopt); ListTransactions(*pwallet, wtx, 0, false, details, filter, /*filter_label=*/std::nullopt);
entry.pushKV("details", details); entry.pushKV("details", details);
std::string strHex = EncodeHexTx(*wtx.tx, pwallet->chain().rpcSerializationFlags()); std::string strHex = EncodeHexTx(*wtx.tx, pwallet->chain().rpcSerializationWithoutWitness());
entry.pushKV("hex", strHex); entry.pushKV("hex", strHex);
if (verbose) { if (verbose) {

View file

@ -34,12 +34,12 @@ BOOST_AUTO_TEST_CASE(psbt_updater_test)
// Create prevtxs and add to wallet // Create prevtxs and add to wallet
CDataStream s_prev_tx1(ParseHex("0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"), SER_NETWORK, PROTOCOL_VERSION); CDataStream s_prev_tx1(ParseHex("0200000000010158e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd7501000000171600145f275f436b09a8cc9a2eb2a2f528485c68a56323feffffff02d8231f1b0100000017a914aed962d6654f9a2b36608eb9d64d2b260db4f1118700c2eb0b0000000017a914b7f5faf40e3d40a5a459b1db3535f2b72fa921e88702483045022100a22edcc6e5bc511af4cc4ae0de0fcd75c7e04d8c1c3a8aa9d820ed4b967384ec02200642963597b9b1bc22c75e9f3e117284a962188bf5e8a74c895089046a20ad770121035509a48eb623e10aace8bfd0212fdb8a8e5af3c94b0b133b95e114cab89e4f7965000000"), SER_NETWORK, PROTOCOL_VERSION);
CTransactionRef prev_tx1; CTransactionRef prev_tx1;
s_prev_tx1 >> prev_tx1; s_prev_tx1 >> TX_WITH_WITNESS(prev_tx1);
m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx1->GetHash()), std::forward_as_tuple(prev_tx1, TxStateInactive{})); m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx1->GetHash()), std::forward_as_tuple(prev_tx1, TxStateInactive{}));
CDataStream s_prev_tx2(ParseHex("0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000"), SER_NETWORK, PROTOCOL_VERSION); CDataStream s_prev_tx2(ParseHex("0200000001aad73931018bd25f84ae400b68848be09db706eac2ac18298babee71ab656f8b0000000048473044022058f6fc7c6a33e1b31548d481c826c015bd30135aad42cd67790dab66d2ad243b02204a1ced2604c6735b6393e5b41691dd78b00f0c5942fb9f751856faa938157dba01feffffff0280f0fa020000000017a9140fb9463421696b82c833af241c78c17ddbde493487d0f20a270100000017a91429ca74f8a08f81999428185c97b5d852e4063f618765000000"), SER_NETWORK, PROTOCOL_VERSION);
CTransactionRef prev_tx2; CTransactionRef prev_tx2;
s_prev_tx2 >> prev_tx2; s_prev_tx2 >> TX_WITH_WITNESS(prev_tx2);
m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx2->GetHash()), std::forward_as_tuple(prev_tx2, TxStateInactive{})); m_wallet.mapWallet.emplace(std::piecewise_construct, std::forward_as_tuple(prev_tx2->GetHash()), std::forward_as_tuple(prev_tx2, TxStateInactive{}));
// Import descriptors for keys and scripts // Import descriptors for keys and scripts

View file

@ -165,7 +165,7 @@ public:
std::vector<uint256> vMerkleBranch; std::vector<uint256> vMerkleBranch;
int nIndex; int nIndex;
s >> tx >> hashBlock >> vMerkleBranch >> nIndex; s >> TX_WITH_WITNESS(tx) >> hashBlock >> vMerkleBranch >> nIndex;
} }
}; };
@ -276,7 +276,7 @@ public:
bool dummy_bool = false; //!< Used to be fSpent bool dummy_bool = false; //!< Used to be fSpent
uint256 serializedHash = TxStateSerializedBlockHash(m_state); uint256 serializedHash = TxStateSerializedBlockHash(m_state);
int serializedIndex = TxStateSerializedIndex(m_state); int serializedIndex = TxStateSerializedIndex(m_state);
s << tx << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_bool; s << TX_WITH_WITNESS(tx) << serializedHash << dummy_vector1 << serializedIndex << dummy_vector2 << mapValueCopy << vOrderForm << fTimeReceivedIsTxTime << nTimeReceived << fFromMe << dummy_bool;
} }
template<typename Stream> template<typename Stream>
@ -289,7 +289,7 @@ public:
bool dummy_bool; //! Used to be fSpent bool dummy_bool; //! Used to be fSpent
uint256 serialized_block_hash; uint256 serialized_block_hash;
int serializedIndex; int serializedIndex;
s >> tx >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> dummy_bool; s >> TX_WITH_WITNESS(tx) >> serialized_block_hash >> dummy_vector1 >> serializedIndex >> dummy_vector2 >> mapValue >> vOrderForm >> fTimeReceivedIsTxTime >> nTimeReceived >> fFromMe >> dummy_bool;
m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex}); m_state = TxStateInterpretSerialized({serialized_block_hash, serializedIndex});

View file

@ -244,14 +244,14 @@ bool CZMQPublishRawBlockNotifier::NotifyBlock(const CBlockIndex *pindex)
{ {
LogPrint(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address); LogPrint(BCLog::ZMQ, "Publish rawblock %s to %s\n", pindex->GetBlockHash().GetHex(), this->address);
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ss;
CBlock block; CBlock block;
if (!m_get_block_by_index(block, *pindex)) { if (!m_get_block_by_index(block, *pindex)) {
zmqError("Can't read block from disk"); zmqError("Can't read block from disk");
return false; return false;
} }
ss << block; ss << RPCTxSerParams(block);
return SendZmqMessage(MSG_RAWBLOCK, &(*ss.begin()), ss.size()); return SendZmqMessage(MSG_RAWBLOCK, &(*ss.begin()), ss.size());
} }
@ -260,8 +260,8 @@ bool CZMQPublishRawTransactionNotifier::NotifyTransaction(const CTransaction &tr
{ {
uint256 hash = transaction.GetHash(); uint256 hash = transaction.GetHash();
LogPrint(BCLog::ZMQ, "Publish rawtx %s to %s\n", hash.GetHex(), this->address); LogPrint(BCLog::ZMQ, "Publish rawtx %s to %s\n", hash.GetHex(), this->address);
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION | RPCSerializationFlags()); DataStream ss;
ss << transaction; ss << RPCTxSerParams(transaction);
return SendZmqMessage(MSG_RAWTX, &(*ss.begin()), ss.size()); return SendZmqMessage(MSG_RAWTX, &(*ss.begin()), ss.size());
} }