Bitcoin ABC 0.30.5
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
14bool CompressScript(const CScript &script, std::vector<uint8_t> &out);
15unsigned int GetSpecialScriptSize(unsigned int nSize);
16bool DecompressScript(CScript &script, unsigned int nSize,
17 const std::vector<uint8_t> &out);
18
24uint64_t CompressAmount(Amount nAmount);
25
26Amount 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 << Span{compr};
54 return;
55 }
56 unsigned int nSize = script.size() + nSpecialScripts;
57 s << VARINT(nSize);
58 s << Span{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 >> Span{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 >> Span{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
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:431
An output of a transaction.
Definition: transaction.h:128
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
size_type size() const
Definition: prevector.h:394
void resize(size_type new_size)
Definition: prevector.h:424
uint64_t CompressAmount(Amount nAmount)
Compress amount.
Definition: compressor.cpp:150
Amount DecompressAmount(uint64_t nAmount)
Definition: compressor.cpp:170
bool DecompressScript(CScript &script, unsigned int nSize, const std::vector< uint8_t > &out)
Definition: compressor.cpp:93
unsigned int GetSpecialScriptSize(unsigned int nSize)
Definition: compressor.cpp:83
bool CompressScript(const CScript &script, std::vector< uint8_t > &out)
Definition: compressor.cpp:53
static const int MAX_SCRIPT_SIZE
Definition: script.h:33
#define VARINT(obj)
Definition: serialize.h:579
#define READWRITE(...)
Definition: serialize.h:166
void Unser(Stream &s, I &val)
Definition: compressor.h:86
void Ser(Stream &s, I val)
Definition: compressor.h:83
Definition: amount.h:19
Compact serializer for scripts.
Definition: compressor.h:40
void Unser(Stream &s, CScript &script)
Definition: compressor.h:61
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
void Ser(Stream &s, const CScript &script) const
Definition: compressor.h:50
wrapper for CTxOut that provides a more compact serialization
Definition: compressor.h:96
FORMATTER_METHODS(CTxOut, obj)
Definition: compressor.h:97