Better wording for transaction fee notification messages
[novacoin.git] / serialize.h
index 0970758..383c987 100644 (file)
@@ -2,9 +2,14 @@
 // Distributed under the MIT/X11 software license, see the accompanying
 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
+#include <string>
 #include <vector>
 #include <map>
+#include <set>
 #include <boost/type_traits/is_fundamental.hpp>
+#include <boost/tuple/tuple.hpp>
+#include <boost/tuple/tuple_comparison.hpp>
+#include <boost/tuple/tuple_io.hpp>
 #if defined(_MSC_VER) || defined(__BORLANDC__)
 typedef __int64  int64;
 typedef unsigned __int64  uint64;
@@ -18,9 +23,12 @@ typedef unsigned long long  uint64;
 class CScript;
 class CDataStream;
 class CAutoFile;
+static const unsigned int MAX_SIZE = 0x02000000;
+
+static const int VERSION = 32100;
+static const char* pszSubVer = "";
+static const bool VERSION_IS_BETA = true;
 
-static const int VERSION = 308;
-static const char* pszSubVer = ".3";
 
 
 
@@ -152,7 +160,7 @@ template<typename Stream> inline void Unserialize(Stream& s, bool& a, int, int=0
 //
 inline unsigned int GetSizeOfCompactSize(uint64 nSize)
 {
-    if (nSize < UCHAR_MAX-2)     return sizeof(unsigned char);
+    if (nSize < 253)             return sizeof(unsigned char);
     else if (nSize <= USHRT_MAX) return sizeof(unsigned char) + sizeof(unsigned short);
     else if (nSize <= UINT_MAX)  return sizeof(unsigned char) + sizeof(unsigned int);
     else                         return sizeof(unsigned char) + sizeof(uint64);
@@ -161,30 +169,31 @@ inline unsigned int GetSizeOfCompactSize(uint64 nSize)
 template<typename Stream>
 void WriteCompactSize(Stream& os, uint64 nSize)
 {
-    if (nSize < UCHAR_MAX-2)
+    if (nSize < 253)
     {
         unsigned char chSize = nSize;
         WRITEDATA(os, chSize);
     }
     else if (nSize <= USHRT_MAX)
     {
-        unsigned char chSize = UCHAR_MAX-2;
+        unsigned char chSize = 253;
         unsigned short xSize = nSize;
         WRITEDATA(os, chSize);
         WRITEDATA(os, xSize);
     }
     else if (nSize <= UINT_MAX)
     {
-        unsigned char chSize = UCHAR_MAX-1;
+        unsigned char chSize = 254;
         unsigned int xSize = nSize;
         WRITEDATA(os, chSize);
         WRITEDATA(os, xSize);
     }
     else
     {
-        unsigned char chSize = UCHAR_MAX;
+        unsigned char chSize = 255;
+        uint64 xSize = nSize;
         WRITEDATA(os, chSize);
-        WRITEDATA(os, nSize);
+        WRITEDATA(os, xSize);
     }
     return;
 }
@@ -195,29 +204,29 @@ uint64 ReadCompactSize(Stream& is)
     unsigned char chSize;
     READDATA(is, chSize);
     uint64 nSizeRet = 0;
-    if (chSize < UCHAR_MAX-2)
+    if (chSize < 253)
     {
         nSizeRet = chSize;
     }
-    else if (chSize == UCHAR_MAX-2)
+    else if (chSize == 253)
     {
-        unsigned short nSize;
-        READDATA(is, nSize);
-        nSizeRet = nSize;
+        unsigned short xSize;
+        READDATA(is, xSize);
+        nSizeRet = xSize;
     }
-    else if (chSize == UCHAR_MAX-1)
+    else if (chSize == 254)
     {
-        unsigned int nSize;
-        READDATA(is, nSize);
-        nSizeRet = nSize;
+        unsigned int xSize;
+        READDATA(is, xSize);
+        nSizeRet = xSize;
     }
     else
     {
-        uint64 nSize;
-        READDATA(is, nSize);
-        nSizeRet = nSize;
+        uint64 xSize;
+        READDATA(is, xSize);
+        nSizeRet = xSize;
     }
-    if (nSizeRet > (uint64)INT_MAX)
+    if (nSizeRet > (uint64)MAX_SIZE)
         throw std::ios_base::failure("ReadCompactSize() : size too large");
     return nSizeRet;
 }
@@ -333,6 +342,16 @@ template<typename K, typename T> unsigned int GetSerializeSize(const std::pair<K
 template<typename Stream, typename K, typename T> void Serialize(Stream& os, const std::pair<K, T>& item, int nType, int nVersion=VERSION);
 template<typename Stream, typename K, typename T> void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion=VERSION);
 
+// 3 tuple
+template<typename T0, typename T1, typename T2> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
+template<typename Stream, typename T0, typename T1, typename T2> void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
+template<typename Stream, typename T0, typename T1, typename T2> void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion=VERSION);
+
+// 4 tuple
+template<typename T0, typename T1, typename T2, typename T3> unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
+template<typename Stream, typename T0, typename T1, typename T2, typename T3> void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
+template<typename Stream, typename T0, typename T1, typename T2, typename T3> void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion=VERSION);
+
 // map
 template<typename K, typename T, typename Pred, typename A> unsigned int GetSerializeSize(const std::map<K, T, Pred, A>& m, int nType, int nVersion=VERSION);
 template<typename Stream, typename K, typename T, typename Pred, typename A> void Serialize(Stream& os, const std::map<K, T, Pred, A>& m, int nType, int nVersion=VERSION);
@@ -550,6 +569,71 @@ void Unserialize(Stream& is, std::pair<K, T>& item, int nType, int nVersion)
 
 
 //
+// 3 tuple
+//
+template<typename T0, typename T1, typename T2>
+unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
+{
+    unsigned int nSize = 0;
+    nSize += GetSerializeSize(get<0>(item), nType, nVersion);
+    nSize += GetSerializeSize(get<1>(item), nType, nVersion);
+    nSize += GetSerializeSize(get<2>(item), nType, nVersion);
+    return nSize;
+}
+
+template<typename Stream, typename T0, typename T1, typename T2>
+void Serialize(Stream& os, const boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
+{
+    Serialize(os, get<0>(item), nType, nVersion);
+    Serialize(os, get<1>(item), nType, nVersion);
+    Serialize(os, get<2>(item), nType, nVersion);
+}
+
+template<typename Stream, typename T0, typename T1, typename T2>
+void Unserialize(Stream& is, boost::tuple<T0, T1, T2>& item, int nType, int nVersion)
+{
+    Unserialize(is, get<0>(item), nType, nVersion);
+    Unserialize(is, get<1>(item), nType, nVersion);
+    Unserialize(is, get<2>(item), nType, nVersion);
+}
+
+
+
+//
+// 4 tuple
+//
+template<typename T0, typename T1, typename T2, typename T3>
+unsigned int GetSerializeSize(const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
+{
+    unsigned int nSize = 0;
+    nSize += GetSerializeSize(get<0>(item), nType, nVersion);
+    nSize += GetSerializeSize(get<1>(item), nType, nVersion);
+    nSize += GetSerializeSize(get<2>(item), nType, nVersion);
+    nSize += GetSerializeSize(get<3>(item), nType, nVersion);
+    return nSize;
+}
+
+template<typename Stream, typename T0, typename T1, typename T2, typename T3>
+void Serialize(Stream& os, const boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
+{
+    Serialize(os, get<0>(item), nType, nVersion);
+    Serialize(os, get<1>(item), nType, nVersion);
+    Serialize(os, get<2>(item), nType, nVersion);
+    Serialize(os, get<3>(item), nType, nVersion);
+}
+
+template<typename Stream, typename T0, typename T1, typename T2, typename T3>
+void Unserialize(Stream& is, boost::tuple<T0, T1, T2, T3>& item, int nType, int nVersion)
+{
+    Unserialize(is, get<0>(item), nType, nVersion);
+    Unserialize(is, get<1>(item), nType, nVersion);
+    Unserialize(is, get<2>(item), nType, nVersion);
+    Unserialize(is, get<3>(item), nType, nVersion);
+}
+
+
+
+//
 // map
 //
 template<typename K, typename T, typename Pred, typename A>
@@ -679,6 +763,8 @@ struct secure_allocator : public std::allocator<T>
     typedef typename base::value_type value_type;
     secure_allocator() throw() {}
     secure_allocator(const secure_allocator& a) throw() : base(a) {}
+    template <typename U>
+    secure_allocator(const secure_allocator<U>& a) throw() : base(a) {}
     ~secure_allocator() throw() {}
     template<typename _Other> struct rebind
     { typedef secure_allocator<_Other> other; };
@@ -720,39 +806,39 @@ public:
     typedef vector_type::const_iterator   const_iterator;
     typedef vector_type::reverse_iterator reverse_iterator;
 
-    explicit CDataStream(int nTypeIn=0, int nVersionIn=VERSION)
+    explicit CDataStream(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
     {
         Init(nTypeIn, nVersionIn);
     }
 
-    CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=0, int nVersionIn=VERSION) : vch(pbegin, pend)
+    CDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
     {
         Init(nTypeIn, nVersionIn);
     }
 
 #if !defined(_MSC_VER) || _MSC_VER >= 1300
-    CDataStream(const char* pbegin, const char* pend, int nTypeIn=0, int nVersionIn=VERSION) : vch(pbegin, pend)
+    CDataStream(const char* pbegin, const char* pend, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(pbegin, pend)
     {
         Init(nTypeIn, nVersionIn);
     }
 #endif
 
-    CDataStream(const vector_type& vchIn, int nTypeIn=0, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
+    CDataStream(const vector_type& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
     {
         Init(nTypeIn, nVersionIn);
     }
 
-    CDataStream(const vector<char>& vchIn, int nTypeIn=0, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
+    CDataStream(const vector<char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end())
     {
         Init(nTypeIn, nVersionIn);
     }
 
-    CDataStream(const vector<unsigned char>& vchIn, int nTypeIn=0, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
+    CDataStream(const vector<unsigned char>& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0])
     {
         Init(nTypeIn, nVersionIn);
     }
 
-    void Init(int nTypeIn=0, int nVersionIn=VERSION)
+    void Init(int nTypeIn=SER_NETWORK, int nVersionIn=VERSION)
     {
         nReadPos = 0;
         nType = nTypeIn;