Bitcoin ABC  0.22.13
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 // 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<uint8_t> &out);
15 unsigned int GetSpecialScriptSize(unsigned int nSize);
16 bool DecompressScript(CScript &script, unsigned int nSize,
17  const std::vector<uint8_t> &out);
18 
24 uint64_t CompressAmount(Amount nAmount);
25 
26 Amount DecompressAmount(uint64_t nAmount);
27 
47  static const unsigned int nSpecialScripts = 6;
48 
49  template <typename Stream>
50  void Ser(Stream &s, const CScript &script) const {
51  std::vector<uint8_t> compr;
52  if (CompressScript(script, compr)) {
53  s << MakeSpan(compr);
54  return;
55  }
56  unsigned int nSize = script.size() + nSpecialScripts;
57  s << VARINT(nSize);
58  s << MakeSpan(script);
59  }
60 
61  template <typename Stream> void Unser(Stream &s, CScript &script) {
62  unsigned int nSize = 0;
63  s >> VARINT(nSize);
64  if (nSize < nSpecialScripts) {
65  std::vector<uint8_t> vch(GetSpecialScriptSize(nSize), 0x00);
66  s >> MakeSpan(vch);
67  DecompressScript(script, nSize, vch);
68  return;
69  }
70  nSize -= nSpecialScripts;
71  if (nSize > MAX_SCRIPT_SIZE) {
72  // Overly long script, replace with a short invalid one
73  script << OP_RETURN;
74  s.ignore(nSize);
75  } else {
76  script.resize(nSize);
77  s >> MakeSpan(script);
78  }
79  }
80 };
81 
83  template <typename Stream, typename I> void Ser(Stream &s, I val) {
84  s << VARINT(CompressAmount(val));
85  }
86  template <typename Stream, typename I> void Unser(Stream &s, I &val) {
87  uint64_t v;
88  s >> VARINT(v);
89  val = DecompressAmount(v);
90  }
91 };
92 
98  READWRITE(Using<AmountCompression>(obj.nValue),
99  Using<ScriptCompression>(obj.scriptPubKey));
100  }
101 };
102 
103 #endif // BITCOIN_COMPRESSOR_H
#define VARINT(obj)
Definition: serialize.h:608
uint64_t CompressAmount(Amount nAmount)
Compress amount.
Definition: compressor.cpp:150
void resize(size_type new_size)
Definition: prevector.h:414
void Ser(Stream &s, const CScript &script) const
Definition: compressor.h:50
Amount DecompressAmount(uint64_t nAmount)
Definition: compressor.cpp:170
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:96
Definition: amount.h:17
void Ser(Stream &s, I val)
Definition: compressor.h:83
constexpr Span< A > MakeSpan(A(&a)[N])
Create a span to a container exposing data() and size().
Definition: span.h:108
void Unser(Stream &s, CScript &script)
Definition: compressor.h:61
Compact serializer for scripts.
Definition: compressor.h:40
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:47
bool CompressScript(const CScript &script, std::vector< uint8_t > &out)
Definition: compressor.cpp:53
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< uint8_t > &out)
Definition: compressor.cpp:93
An output of a transaction.
Definition: transaction.h:141
static const int MAX_SCRIPT_SIZE
Definition: script.h:32
unsigned int GetSpecialScriptSize(unsigned int nSize)
Definition: compressor.cpp:83
void Unser(Stream &s, I &val)
Definition: compressor.h:86
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
FORMATTER_METHODS(CTxOut, obj)
Definition: compressor.h:97
size_type size() const
Definition: prevector.h:384
#define READWRITE(...)
Definition: serialize.h:191