Bitcoin Core  21.99.0
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
ScriptCompression::Unser
void Unser(Stream &s, CScript &script)
Definition: compressor.h:65
transaction.h
TxOutCompression::FORMATTER_METHODS
FORMATTER_METHODS(CTxOut, obj)
Definition: compressor.h:103
TxOutCompression
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:101
ScriptCompression::Ser
void Ser(Stream &s, const CScript &script)
Definition: compressor.h:53
CompressAmount
uint64_t CompressAmount(uint64_t nAmount)
Compress amount.
Definition: compressor.cpp:149
AmountCompression::Ser
void Ser(Stream &s, I val)
Definition: compressor.h:88
MAX_SCRIPT_SIZE
static const int MAX_SCRIPT_SIZE
Definition: script.h:32
OP_RETURN
@ OP_RETURN
Definition: script.h:103
CTxOut
An output of a transaction.
Definition: transaction.h:128
CompressScript
bool CompressScript(const CScript &script, std::vector< unsigned char > &out)
Definition: compressor.cpp:55
DecompressScript
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< unsigned char > &out)
Definition: compressor.cpp:95
VARINT
#define VARINT(obj)
Definition: serialize.h:479
span.h
ScriptCompression::nSpecialScripts
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
DecompressAmount
uint64_t DecompressAmount(uint64_t nAmount)
Definition: compressor.cpp:168
READWRITE
#define READWRITE(...)
Definition: serialize.h:175
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
prevector::resize
void resize(size_type new_size)
Definition: prevector.h:316
AmountCompression::Unser
void Unser(Stream &s, I &val)
Definition: compressor.h:92
prevector::size
size_type size() const
Definition: prevector.h:282
GetSpecialScriptSize
unsigned int GetSpecialScriptSize(unsigned int nSize)
Definition: compressor.cpp:86
AmountCompression
Definition: compressor.h:86
serialize.h
script.h
ScriptCompression
Compact serializer for scripts.
Definition: compressor.h:42
MakeSpan
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:222