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.
6 #ifndef BITCOIN_COMPRESSOR_H
7 #define BITCOIN_COMPRESSOR_H
9 #include "primitives/transaction.h"
10 #include "script/script.h"
11 #include "serialize.h"
17 /** Compact serializer for scripts.
19 * It detects common cases and encodes them much more efficiently.
20 * 3 special cases are defined:
21 * * Pay to pubkey hash (encoded as 21 bytes)
22 * * Pay to script hash (encoded as 21 bytes)
23 * * Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
25 * Other scripts up to 121 bytes require 1 byte + script length. Above
26 * that, scripts up to 16505 bytes require 2 bytes + script length.
28 class CScriptCompressor
32 * make this static for now (there are only 6 special scripts defined)
33 * this can potentially be extended together with a new nVersion for
34 * transactions, in which case this value becomes dependent on nVersion
35 * and nHeight of the enclosing transaction.
37 static const unsigned int nSpecialScripts
= 6;
42 * These check for scripts for which a special case with a shorter encoding is defined.
43 * They are implemented separately from the CScript test, as these test for exact byte
44 * sequence correspondences, and are more strict. For example, IsToPubKey also verifies
45 * whether the public key is valid (as invalid ones cannot be represented in compressed
48 bool IsToKeyID(CKeyID
&hash
) const;
49 bool IsToScriptID(CScriptID
&hash
) const;
50 bool IsToPubKey(CPubKey
&pubkey
) const;
52 bool Compress(std::vector
<unsigned char> &out
) const;
53 unsigned int GetSpecialSize(unsigned int nSize
) const;
54 bool Decompress(unsigned int nSize
, const std::vector
<unsigned char> &out
);
56 explicit CScriptCompressor(CScript
&scriptIn
) : script(scriptIn
) { }
58 template<typename Stream
>
59 void Serialize(Stream
&s
) const {
60 std::vector
<unsigned char> compr
;
61 if (Compress(compr
)) {
62 s
<< CFlatData(compr
);
65 unsigned int nSize
= script
.size() + nSpecialScripts
;
67 s
<< CFlatData(script
);
70 template<typename Stream
>
71 void Unserialize(Stream
&s
) {
72 unsigned int nSize
= 0;
74 if (nSize
< nSpecialScripts
) {
75 std::vector
<unsigned char> vch(GetSpecialSize(nSize
), 0x00);
76 s
>> REF(CFlatData(vch
));
77 Decompress(nSize
, vch
);
80 nSize
-= nSpecialScripts
;
81 if (nSize
> MAX_SCRIPT_SIZE
) {
82 // Overly long script, replace with a short invalid one
87 s
>> REF(CFlatData(script
));
92 /** wrapper for CTxOut that provides a more compact serialization */
93 class CTxOutCompressor
99 static uint64_t CompressAmount(uint64_t nAmount
);
100 static uint64_t DecompressAmount(uint64_t nAmount
);
102 explicit CTxOutCompressor(CTxOut
&txoutIn
) : txout(txoutIn
) { }
104 ADD_SERIALIZE_METHODS
;
106 template <typename Stream
, typename Operation
>
107 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
108 if (!ser_action
.ForRead()) {
109 uint64_t nVal
= CompressAmount(txout
.nValue
);
110 READWRITE(VARINT(nVal
));
113 READWRITE(VARINT(nVal
));
114 txout
.nValue
= DecompressAmount(nVal
);
116 CScriptCompressor
cscript(REF(txout
.scriptPubKey
));
121 #endif // BITCOIN_COMPRESSOR_H