aboutsummaryrefslogtreecommitdiff
path: root/src/primitives
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2016-11-21 10:51:32 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2016-11-21 10:51:37 +0100
commit7490ae8b699d2955b665cf849d86ff5bb5245c28 (patch)
tree31f4d8edd5134e92bf133ee3dfd52651c14ed21e /src/primitives
parent44adf683ad232db8ce0cb89b3e236a1f5944cfb0 (diff)
parentb4e4ba475a5679e09f279aaf2a83dcf93c632bdb (diff)
Merge #9125: Make CBlock a vector of shared_ptr of CTransactions
b4e4ba4 Introduce convenience type CTransactionRef (Pieter Wuille) 1662b43 Make CBlock::vtx a vector of shared_ptr<CTransaction> (Pieter Wuille) da60506 Add deserializing constructors to CTransaction and CMutableTransaction (Pieter Wuille) 0e85204 Add serialization for unique_ptr and shared_ptr (Pieter Wuille)
Diffstat (limited to 'src/primitives')
-rw-r--r--src/primitives/block.cpp2
-rw-r--r--src/primitives/block.h2
-rw-r--r--src/primitives/transaction.cpp4
-rw-r--r--src/primitives/transaction.h15
4 files changed, 21 insertions, 2 deletions
diff --git a/src/primitives/block.cpp b/src/primitives/block.cpp
index 0e6ab4dd71..95bd2211f9 100644
--- a/src/primitives/block.cpp
+++ b/src/primitives/block.cpp
@@ -27,7 +27,7 @@ std::string CBlock::ToString() const
vtx.size());
for (unsigned int i = 0; i < vtx.size(); i++)
{
- s << " " << vtx[i].ToString() << "\n";
+ s << " " << vtx[i]->ToString() << "\n";
}
return s.str();
}
diff --git a/src/primitives/block.h b/src/primitives/block.h
index d148aec1e0..b037fc839c 100644
--- a/src/primitives/block.h
+++ b/src/primitives/block.h
@@ -73,7 +73,7 @@ class CBlock : public CBlockHeader
{
public:
// network and disk
- std::vector<CTransaction> vtx;
+ std::vector<CTransactionRef> vtx;
// memory only
mutable bool fChecked;
diff --git a/src/primitives/transaction.cpp b/src/primitives/transaction.cpp
index 7acdac17f2..91f4d29488 100644
--- a/src/primitives/transaction.cpp
+++ b/src/primitives/transaction.cpp
@@ -78,6 +78,10 @@ CTransaction::CTransaction(const CMutableTransaction &tx) : nVersion(tx.nVersion
UpdateHash();
}
+CTransaction::CTransaction(CMutableTransaction &&tx) : nVersion(tx.nVersion), vin(std::move(tx.vin)), vout(std::move(tx.vout)), wit(std::move(tx.wit)), nLockTime(tx.nLockTime) {
+ UpdateHash();
+}
+
CTransaction& CTransaction::operator=(const CTransaction &tx) {
*const_cast<int*>(&nVersion) = tx.nVersion;
*const_cast<std::vector<CTxIn>*>(&vin) = tx.vin;
diff --git a/src/primitives/transaction.h b/src/primitives/transaction.h
index 1d176e5d8c..0fa85a1519 100644
--- a/src/primitives/transaction.h
+++ b/src/primitives/transaction.h
@@ -379,6 +379,7 @@ public:
/** Convert a CMutableTransaction into a CTransaction. */
CTransaction(const CMutableTransaction &tx);
+ CTransaction(CMutableTransaction &&tx);
CTransaction& operator=(const CTransaction& tx);
@@ -392,6 +393,9 @@ public:
}
}
+ template <typename Stream>
+ CTransaction(deserialize_type, Stream& s) : CTransaction(CMutableTransaction(deserialize, s)) {}
+
bool IsNull() const {
return vin.empty() && vout.empty();
}
@@ -460,12 +464,23 @@ struct CMutableTransaction
SerializeTransaction(*this, s, ser_action);
}
+ template <typename Stream>
+ CMutableTransaction(deserialize_type, Stream& s) {
+ Unserialize(s);
+ }
+
/** Compute the hash of this CMutableTransaction. This is computed on the
* fly, as opposed to GetHash() in CTransaction, which uses a cached result.
*/
uint256 GetHash() const;
};
+typedef std::shared_ptr<const CTransaction> CTransactionRef;
+static inline CTransactionRef MakeTransactionRef() { return std::make_shared<const CTransaction>(); }
+template <typename Tx> static inline CTransactionRef MakeTransactionRef(Tx&& txIn) { return std::make_shared<const CTransaction>(std::forward<Tx>(txIn)); }
+static inline CTransactionRef MakeTransactionRef(const CTransactionRef& txIn) { return txIn; }
+static inline CTransactionRef MakeTransactionRef(CTransactionRef&& txIn) { return std::move(txIn); }
+
/** Compute the weight of a transaction, as defined by BIP 141 */
int64_t GetTransactionWeight(const CTransaction &tx);