Raven Core  3.0.0
P2P Digital Currency
compressor.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_COMPRESSOR_H
8 #define RAVEN_COMPRESSOR_H
9 
10 #include "primitives/transaction.h"
11 #include "script/script.h"
12 #include "serialize.h"
13 
14 class CKeyID;
15 class CPubKey;
16 class CScriptID;
17 
30 {
31 private:
38  static const unsigned int nSpecialScripts = 6;
39 
41 protected:
49  bool IsToKeyID(CKeyID &hash) const;
50  bool IsToScriptID(CScriptID &hash) const;
51  bool IsToPubKey(CPubKey &pubkey) const;
52 
53  bool Compress(std::vector<unsigned char> &out) const;
54  unsigned int GetSpecialSize(unsigned int nSize) const;
55  bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out);
56 public:
57  explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
58 
59  template<typename Stream>
60  void Serialize(Stream &s) const {
61  std::vector<unsigned char> compr;
62  if (Compress(compr)) {
63  s << CFlatData(compr);
64  return;
65  }
66  unsigned int nSize = script.size() + nSpecialScripts;
67  s << VARINT(nSize);
68  s << CFlatData(script);
69  }
70 
71  template<typename Stream>
72  void Unserialize(Stream &s) {
73  unsigned int nSize = 0;
74  s >> VARINT(nSize);
75  if (nSize < nSpecialScripts) {
76  std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
77  s >> REF(CFlatData(vch));
78  Decompress(nSize, vch);
79  return;
80  }
81  nSize -= nSpecialScripts;
82  if (nSize > MAX_SCRIPT_SIZE) {
83  // Overly long script, replace with a short invalid one
84  script << OP_RETURN;
85  s.ignore(nSize);
86  } else {
87  script.resize(nSize);
88  s >> REF(CFlatData(script));
89  }
90  }
91 };
92 
95 {
96 private:
98 
99 public:
100  static uint64_t CompressAmount(uint64_t nAmount);
101  static uint64_t DecompressAmount(uint64_t nAmount);
102 
103  explicit CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
104 
106 
107  template <typename Stream, typename Operation>
108  inline void SerializationOp(Stream& s, Operation ser_action) {
109  if (!ser_action.ForRead()) {
110  uint64_t nVal = CompressAmount(txout.nValue);
111  READWRITE(VARINT(nVal));
112  } else {
113  uint64_t nVal = 0;
114  READWRITE(VARINT(nVal));
115  txout.nValue = DecompressAmount(nVal);
116  }
117  CScriptCompressor cscript(REF(txout.scriptPubKey));
118  READWRITE(cscript);
119  }
120 };
121 
122 #endif // RAVEN_COMPRESSOR_H
CAmount nValue
Definition: transaction.h:140
#define VARINT(obj)
Definition: serialize.h:367
void resize(size_type new_size)
Definition: prevector.h:317
CScript scriptPubKey
Definition: transaction.h:141
#define READWRITE(obj)
Definition: serialize.h:163
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:94
bool IsToPubKey(CPubKey &pubkey) const
Definition: compressor.cpp:34
Compact serializer for scripts.
Definition: compressor.h:29
CTxOutCompressor(CTxOut &txoutIn)
Definition: compressor.h:103
bool IsToKeyID(CKeyID &hash) const
These check for scripts for which a special case with a shorter encoding is defined.
Definition: compressor.cpp:13
CScriptCompressor(CScript &scriptIn)
Definition: compressor.h:57
An encapsulated public key.
Definition: pubkey.h:40
unsigned int GetSpecialSize(unsigned int nSize) const
Definition: compressor.cpp:80
CScript & script
Definition: compressor.h:40
An output of a transaction.
Definition: transaction.h:137
static const unsigned int nSpecialScripts
make this static for now (there are only 6 special scripts defined) this can potentially be extended ...
Definition: compressor.h:38
CTxOut & txout
Definition: compressor.h:97
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
bool Decompress(unsigned int nSize, const std::vector< unsigned char > &out)
Definition: compressor.cpp:89
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:23
size_type size() const
Definition: prevector.h:283
void SerializationOp(Stream &s, Operation ser_action)
Definition: compressor.h:108
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers s...
Definition: serialize.h:48
void Unserialize(Stream &s)
Definition: compressor.h:72
bool IsToScriptID(CScriptID &hash) const
Definition: compressor.cpp:24
bool Compress(std::vector< unsigned char > &out) const
Definition: compressor.cpp:49
Wrapper for serializing arrays and POD.
Definition: serialize.h:374
void Serialize(Stream &s) const
Definition: compressor.h:60