Bitcoin ABC  0.24.10
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
ScriptCompression::Unser
void Unser(Stream &s, CScript &script)
Definition: compressor.h:61
ScriptCompression::Ser
void Ser(Stream &s, const CScript &script) const
Definition: compressor.h:50
CompressAmount
uint64_t CompressAmount(Amount nAmount)
Compress amount.
Definition: compressor.cpp:150
transaction.h
TxOutCompression::FORMATTER_METHODS
FORMATTER_METHODS(CTxOut, obj)
Definition: compressor.h:97
TxOutCompression
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:96
AmountCompression::Ser
void Ser(Stream &s, I val)
Definition: compressor.h:83
MAX_SCRIPT_SIZE
static const int MAX_SCRIPT_SIZE
Definition: script.h:32
CTxOut
An output of a transaction.
Definition: transaction.h:130
VARINT
#define VARINT(obj)
Definition: serialize.h:596
DecompressScript
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< uint8_t > &out)
Definition: compressor.cpp:93
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:47
CompressScript
bool CompressScript(const CScript &script, std::vector< uint8_t > &out)
Definition: compressor.cpp:53
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
Amount
Definition: amount.h:19
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:430
ScriptError::OP_RETURN
@ OP_RETURN
prevector::resize
void resize(size_type new_size)
Definition: prevector.h:416
AmountCompression::Unser
void Unser(Stream &s, I &val)
Definition: compressor.h:86
prevector::size
size_type size() const
Definition: prevector.h:386
GetSpecialScriptSize
unsigned int GetSpecialScriptSize(unsigned int nSize)
Definition: compressor.cpp:83
AmountCompression
Definition: compressor.h:82
serialize.h
script.h
ScriptCompression
Compact serializer for scripts.
Definition: compressor.h:40
DecompressAmount
Amount DecompressAmount(uint64_t nAmount)
Definition: compressor.cpp:170
MakeSpan
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:259