Bitcoin Core  0.18.99
P2P Digital Currency
compressor.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 BITCOIN_COMPRESSOR_H
7 #define BITCOIN_COMPRESSOR_H
8 
10 #include <script/script.h>
11 #include <serialize.h>
12 #include <span.h>
13 
14 class CKeyID;
15 class CPubKey;
16 class CScriptID;
17 
18 bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
19 unsigned int GetSpecialScriptSize(unsigned int nSize);
20 bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
21 
22 uint64_t CompressAmount(uint64_t nAmount);
23 uint64_t DecompressAmount(uint64_t nAmount);
24 
37 {
38 private:
45  static const unsigned int nSpecialScripts = 6;
46 
48 public:
49  explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
50 
51  template<typename Stream>
52  void Serialize(Stream &s) const {
53  std::vector<unsigned char> compr;
54  if (CompressScript(script, compr)) {
55  s << MakeSpan(compr);
56  return;
57  }
58  unsigned int nSize = script.size() + nSpecialScripts;
59  s << VARINT(nSize);
60  s << MakeSpan(script);
61  }
62 
63  template<typename Stream>
64  void Unserialize(Stream &s) {
65  unsigned int nSize = 0;
66  s >> VARINT(nSize);
67  if (nSize < nSpecialScripts) {
68  std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
69  s >> MakeSpan(vch);
70  DecompressScript(script, nSize, vch);
71  return;
72  }
73  nSize -= nSpecialScripts;
74  if (nSize > MAX_SCRIPT_SIZE) {
75  // Overly long script, replace with a short invalid one
76  script << OP_RETURN;
77  s.ignore(nSize);
78  } else {
79  script.resize(nSize);
80  s >> MakeSpan(script);
81  }
82  }
83 };
84 
87 {
88 private:
90 
91 public:
92  explicit CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
93 
95 
96  template <typename Stream, typename Operation>
97  inline void SerializationOp(Stream& s, Operation ser_action) {
98  if (!ser_action.ForRead()) {
99  uint64_t nVal = CompressAmount(txout.nValue);
100  READWRITE(VARINT(nVal));
101  } else {
102  uint64_t nVal = 0;
103  READWRITE(VARINT(nVal));
104  txout.nValue = DecompressAmount(nVal);
105  }
106  CScriptCompressor cscript(REF(txout.scriptPubKey));
107  READWRITE(cscript);
108  }
109 };
110 
111 #endif // BITCOIN_COMPRESSOR_H
CAmount nValue
Definition: transaction.h:136
void resize(size_type new_size)
Definition: prevector.h:311
bool CompressScript(const CScript &script, std::vector< unsigned char > &out)
Definition: compressor.cpp:56
uint64_t DecompressAmount(uint64_t nAmount)
Definition: compressor.cpp:169
CScript scriptPubKey
Definition: transaction.h:137
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:86
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< unsigned char > &out)
Definition: compressor.cpp:96
Compact serializer for scripts.
Definition: compressor.h:36
CTxOutCompressor(CTxOut &txoutIn)
Definition: compressor.h:92
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
CScriptCompressor(CScript &scriptIn)
Definition: compressor.h:49
An encapsulated public key.
Definition: pubkey.h:30
CScript & script
Definition: compressor.h:47
An output of a transaction.
Definition: transaction.h:133
static const int MAX_SCRIPT_SIZE
Definition: script.h:32
unsigned int GetSpecialScriptSize(unsigned int nSize)
Definition: compressor.cpp:87
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:45
CTxOut & txout
Definition: compressor.h:89
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: standard.h:22
size_type size() const
Definition: prevector.h:277
#define READWRITE(...)
Definition: serialize.h:184
void SerializationOp(Stream &s, Operation ser_action)
Definition: compressor.h:97
T & REF(const T &val)
Used to bypass the rule against non-const reference to temporary where it makes sense with wrappers...
Definition: serialize.h:48
void Unserialize(Stream &s)
Definition: compressor.h:64
#define VARINT(obj,...)
Definition: serialize.h:422
uint64_t CompressAmount(uint64_t nAmount)
Definition: compressor.cpp:150
void Serialize(Stream &s) const
Definition: compressor.h:52