1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 CScriptCompressor(CScript
&scriptIn
) : script(scriptIn
) { }
58 unsigned int GetSerializeSize(int nType
, int nVersion
) const {
59 std::vector
<unsigned char> compr
;
62 unsigned int nSize
= script
.size() + nSpecialScripts
;
63 return script
.size() + VARINT(nSize
).GetSerializeSize(nType
, nVersion
);
66 template<typename Stream
>
67 void Serialize(Stream
&s
, int nType
, int nVersion
) const {
68 std::vector
<unsigned char> compr
;
69 if (Compress(compr
)) {
70 s
<< CFlatData(compr
);
73 unsigned int nSize
= script
.size() + nSpecialScripts
;
75 s
<< CFlatData(script
);
78 template<typename Stream
>
79 void Unserialize(Stream
&s
, int nType
, int nVersion
) {
80 unsigned int nSize
= 0;
82 if (nSize
< nSpecialScripts
) {
83 std::vector
<unsigned char> vch(GetSpecialSize(nSize
), 0x00);
84 s
>> REF(CFlatData(vch
));
85 Decompress(nSize
, vch
);
88 nSize
-= nSpecialScripts
;
90 s
>> REF(CFlatData(script
));
94 /** wrapper for CTxOut that provides a more compact serialization */
95 class CTxOutCompressor
101 static uint64_t CompressAmount(uint64_t nAmount
);
102 static uint64_t DecompressAmount(uint64_t nAmount
);
104 CTxOutCompressor(CTxOut
&txoutIn
) : txout(txoutIn
) { }
106 ADD_SERIALIZE_METHODS
;
108 template <typename Stream
, typename Operation
>
109 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
110 if (!ser_action
.ForRead()) {
111 uint64_t nVal
= CompressAmount(txout
.nValue
);
112 READWRITE(VARINT(nVal
));
115 READWRITE(VARINT(nVal
));
116 txout
.nValue
= DecompressAmount(nVal
);
118 CScriptCompressor
cscript(REF(txout
.scriptPubKey
));
123 #endif // BITCOIN_COMPRESSOR_H