Bitcoin Core  0.20.99
P2P Digital Currency
compressor.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 
27 uint64_t CompressAmount(uint64_t nAmount);
28 
29 uint64_t DecompressAmount(uint64_t nAmount);
30 
43 {
50  static const unsigned int nSpecialScripts = 6;
51 
52  template<typename Stream>
53  void Ser(Stream &s, const CScript& script) {
54  std::vector<unsigned char> compr;
55  if (CompressScript(script, compr)) {
56  s << MakeSpan(compr);
57  return;
58  }
59  unsigned int nSize = script.size() + nSpecialScripts;
60  s << VARINT(nSize);
61  s << MakeSpan(script);
62  }
63 
64  template<typename Stream>
65  void Unser(Stream &s, CScript& script) {
66  unsigned int nSize = 0;
67  s >> VARINT(nSize);
68  if (nSize < nSpecialScripts) {
69  std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
70  s >> MakeSpan(vch);
71  DecompressScript(script, nSize, vch);
72  return;
73  }
74  nSize -= nSpecialScripts;
75  if (nSize > MAX_SCRIPT_SIZE) {
76  // Overly long script, replace with a short invalid one
77  script << OP_RETURN;
78  s.ignore(nSize);
79  } else {
80  script.resize(nSize);
81  s >> MakeSpan(script);
82  }
83  }
84 };
85 
87 {
88  template<typename Stream, typename I> void Ser(Stream& s, I val)
89  {
90  s << VARINT(CompressAmount(val));
91  }
92  template<typename Stream, typename I> void Unser(Stream& s, I& val)
93  {
94  uint64_t v;
95  s >> VARINT(v);
96  val = DecompressAmount(v);
97  }
98 };
99 
102 {
103  FORMATTER_METHODS(CTxOut, obj) { READWRITE(Using<AmountCompression>(obj.nValue), Using<ScriptCompression>(obj.scriptPubKey)); }
104 };
105 
106 #endif // BITCOIN_COMPRESSOR_H
#define VARINT(obj)
Definition: serialize.h:468
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:53
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:101
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:88
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:77
void Unser(Stream &s, CScript &script)
Definition: compressor.h:65
Compact serializer for scripts.
Definition: compressor.h:42
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:50
An output of a transaction.
Definition: transaction.h:120
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:92
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
FORMATTER_METHODS(CTxOut, obj)
Definition: compressor.h:103
size_type size() const
Definition: prevector.h:282
#define READWRITE(...)
Definition: serialize.h:171
uint64_t CompressAmount(uint64_t nAmount)
Compress amount.
Definition: compressor.cpp:149