|
3 | 3 | // Distributed under the MIT software license, see the accompanying
|
4 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 |
|
6 |
| -#ifndef H_BITCOIN_SCRIPT_COMPRESSOR |
7 |
| -#define H_BITCOIN_SCRIPT_COMPRESSOR |
| 6 | +#ifndef H_BITCOIN_COMPRESSOR |
| 7 | +#define H_BITCOIN_COMPRESSOR |
8 | 8 |
|
| 9 | +#include "core/transaction.h" |
9 | 10 | #include "script/script.h"
|
10 | 11 | #include "serialize.h"
|
11 | 12 |
|
@@ -86,4 +87,33 @@ class CScriptCompressor
|
86 | 87 | }
|
87 | 88 | };
|
88 | 89 |
|
89 |
| -#endif // H_BITCOIN_SCRIPT_COMPRESSOR |
| 90 | +/** wrapper for CTxOut that provides a more compact serialization */ |
| 91 | +class CTxOutCompressor |
| 92 | +{ |
| 93 | +private: |
| 94 | + CTxOut &txout; |
| 95 | + |
| 96 | +public: |
| 97 | + static uint64_t CompressAmount(uint64_t nAmount); |
| 98 | + static uint64_t DecompressAmount(uint64_t nAmount); |
| 99 | + |
| 100 | + CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { } |
| 101 | + |
| 102 | + ADD_SERIALIZE_METHODS; |
| 103 | + |
| 104 | + template <typename Stream, typename Operation> |
| 105 | + inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) { |
| 106 | + if (!ser_action.ForRead()) { |
| 107 | + uint64_t nVal = CompressAmount(txout.nValue); |
| 108 | + READWRITE(VARINT(nVal)); |
| 109 | + } else { |
| 110 | + uint64_t nVal = 0; |
| 111 | + READWRITE(VARINT(nVal)); |
| 112 | + txout.nValue = DecompressAmount(nVal); |
| 113 | + } |
| 114 | + CScriptCompressor cscript(REF(txout.scriptPubKey)); |
| 115 | + READWRITE(cscript); |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +#endif // H_BITCOIN_COMPRESSOR |
0 commit comments