Bitcoin Core  0.19.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 bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
15 unsigned int GetSpecialScriptSize(unsigned int nSize);
16 bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
17 
18 uint64_t CompressAmount(uint64_t nAmount);
19 uint64_t DecompressAmount(uint64_t nAmount);
20 
33 {
40  static const unsigned int nSpecialScripts = 6;
41 
42  template<typename Stream>
43  void Ser(Stream &s, const CScript& script) {
44  std::vector<unsigned char> compr;
45  if (CompressScript(script, compr)) {
46  s << MakeSpan(compr);
47  return;
48  }
49  unsigned int nSize = script.size() + nSpecialScripts;
50  s << VARINT(nSize);
51  s << MakeSpan(script);
52  }
53 
54  template<typename Stream>
55  void Unser(Stream &s, CScript& script) {
56  unsigned int nSize = 0;
57  s >> VARINT(nSize);
58  if (nSize < nSpecialScripts) {
59  std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
60  s >> MakeSpan(vch);
61  DecompressScript(script, nSize, vch);
62  return;
63  }
64  nSize -= nSpecialScripts;
65  if (nSize > MAX_SCRIPT_SIZE) {
66  // Overly long script, replace with a short invalid one
67  script << OP_RETURN;
68  s.ignore(nSize);
69  } else {
70  script.resize(nSize);
71  s >> MakeSpan(script);
72  }
73  }
74 };
75 
77 {
78  template<typename Stream, typename I> void Ser(Stream& s, I val)
79  {
80  s << VARINT(CompressAmount(val));
81  }
82  template<typename Stream, typename I> void Unser(Stream& s, I& val)
83  {
84  uint64_t v;
85  s >> VARINT(v);
86  val = DecompressAmount(v);
87  }
88 };
89 
92 {
93  FORMATTER_METHODS(CTxOut, obj) { READWRITE(Using<AmountCompression>(obj.nValue), Using<ScriptCompression>(obj.scriptPubKey)); }
94 };
95 
96 #endif // BITCOIN_COMPRESSOR_H
#define VARINT(obj)
Definition: serialize.h:502
void resize(size_type new_size)
Definition: prevector.h:316
bool CompressScript(const CScript &script, std::vector< unsigned char > &out)
Definition: compressor.cpp:55
uint64_t DecompressAmount(uint64_t nAmount)
Definition: compressor.cpp:168
void Ser(Stream &s, const CScript &script)
Definition: compressor.h:43
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:91
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< unsigned char > &out)
Definition: compressor.cpp:95
void Ser(Stream &s, I val)
Definition: compressor.h:78
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:55
void Unser(Stream &s, CScript &script)
Definition: compressor.h:55
Compact serializer for scripts.
Definition: compressor.h:32
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:40
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:86
void Unser(Stream &s, I &val)
Definition: compressor.h:82
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
FORMATTER_METHODS(CTxOut, obj)
Definition: compressor.h:93
size_type size() const
Definition: prevector.h:282
#define READWRITE(...)
Definition: serialize.h:191
uint64_t CompressAmount(uint64_t nAmount)
Definition: compressor.cpp:149