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