aboutsummaryrefslogtreecommitdiff
path: root/src/primitives
diff options
context:
space:
mode:
Diffstat (limited to 'src/primitives')
-rw-r--r--src/primitives/block.cpp2
-rw-r--r--src/primitives/transaction.cpp8
-rw-r--r--src/primitives/transaction.h43
3 files changed, 30 insertions, 23 deletions
diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp
index 3d21708820..27da161e1e 100644
--- a/src/primitives/block.cpp
+++ b/src/primitives/block.cpp
@@ -10,7 +10,7 @@
uint256 CBlockHeader::GetHash() const
{
- return (CHashWriter{PROTOCOL_VERSION} << *this).GetHash();
+ return (HashWriter{} << *this).GetHash();
}
std::string CBlock::ToString() const
diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp
index 1ad8345fcb..6650277c2b 100644
--- a/src/primitives/transaction.cpp
+++ b/src/primitives/transaction.cpp
@@ -68,12 +68,12 @@ CMutableTransaction::CMutableTransaction(const CTransaction& tx) : vin(tx.vin),
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
{
- return Txid::FromUint256((CHashWriter{SERIALIZE_TRANSACTION_NO_WITNESS} << *this).GetHash());
+ return Txid::FromUint256((HashWriter{} << TX_NO_WITNESS(*this)).GetHash());
}
Wtxid CTransaction::ComputeWitnessHash() const
@@ -82,7 +82,7 @@ Wtxid CTransaction::ComputeWitnessHash() const
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()} {}
@@ -102,7 +102,7 @@ CAmount CTransaction::GetValueOut() const
unsigned int CTransaction::GetTotalSize() const
{
- return ::GetSerializeSize(*this, PROTOCOL_VERSION);
+ return ::GetSerializeSize(TX_WITH_WITNESS(*this));
}
std::string CTransaction::ToString() const
diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h
index 89deb9de4d..ad190f7d7f 100644
--- a/src/primitives/transaction.h
+++ b/src/primitives/transaction.h
@@ -24,14 +24,6 @@
#include <utility>
#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 */
class COutPoint
{
@@ -197,6 +189,13 @@ public:
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:
* - int32_t nVersion
@@ -215,8 +214,9 @@ struct CMutableTransaction;
* - uint32_t nLockTime
*/
template<typename Stream, typename TxType>
-inline void UnserializeTransaction(TxType& tx, Stream& s) {
- const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
+void UnserializeTransaction(TxType& tx, Stream& s, const TransactionSerParams& params)
+{
+ const bool fAllowWitness = params.allow_witness;
s >> tx.nVersion;
unsigned char flags = 0;
@@ -254,8 +254,9 @@ inline void UnserializeTransaction(TxType& tx, Stream& s) {
}
template<typename Stream, typename TxType>
-inline void SerializeTransaction(const TxType& tx, Stream& s) {
- const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
+void SerializeTransaction(const TxType& tx, Stream& s, const TransactionSerParams& params)
+{
+ const bool fAllowWitness = params.allow_witness;
s << tx.nVersion;
unsigned char flags = 0;
@@ -323,13 +324,15 @@ public:
template <typename Stream>
inline void Serialize(Stream& s) const {
- SerializeTransaction(*this, s);
+ SerializeTransaction(*this, s, s.GetParams());
}
/** This deserializing constructor is provided instead of an Unserialize method.
* Unserialize is not possible, since it would require overwriting const fields. */
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 {
return vin.empty() && vout.empty();
@@ -389,17 +392,21 @@ struct CMutableTransaction
template <typename Stream>
inline void Serialize(Stream& s) const {
- SerializeTransaction(*this, s);
+ SerializeTransaction(*this, s, s.GetParams());
}
-
template <typename Stream>
inline void Unserialize(Stream& s) {
- UnserializeTransaction(*this, s);
+ UnserializeTransaction(*this, s, s.GetParams());
+ }
+
+ template <typename Stream>
+ CMutableTransaction(deserialize_type, const TransactionSerParams& params, Stream& s) {
+ UnserializeTransaction(*this, s, params);
}
template <typename Stream>
- CMutableTransaction(deserialize_type, Stream& s) {
+ CMutableTransaction(deserialize_type, ParamsStream<TransactionSerParams,Stream>& s) {
Unserialize(s);
}