2014-08-20 18:33:30 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-17 02:47:57 +01:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-09-09 10:00:42 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-08-20 18:33:30 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#ifndef BITCOIN_COMPRESSOR_H
|
|
|
|
#define BITCOIN_COMPRESSOR_H
|
2014-08-20 18:33:30 +02:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <serialize.h>
|
2018-04-08 17:57:22 +02:00
|
|
|
#include <span.h>
|
2014-08-20 18:33:30 +02:00
|
|
|
|
2017-07-09 01:43:34 +02:00
|
|
|
bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
|
|
|
|
unsigned int GetSpecialScriptSize(unsigned int nSize);
|
|
|
|
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
|
|
|
|
|
2020-03-05 21:25:32 +01:00
|
|
|
/**
|
|
|
|
* Compress amount.
|
|
|
|
*
|
|
|
|
* nAmount is of type uint64_t and thus cannot be negative. If you're passing in
|
|
|
|
* a CAmount (int64_t), make sure to properly handle the case where the amount
|
|
|
|
* is negative before calling CompressAmount(...).
|
|
|
|
*
|
|
|
|
* @pre Function defined only for 0 <= nAmount <= MAX_MONEY.
|
|
|
|
*/
|
2017-07-09 01:43:34 +02:00
|
|
|
uint64_t CompressAmount(uint64_t nAmount);
|
2020-03-05 21:25:32 +01:00
|
|
|
|
2017-07-09 01:43:34 +02:00
|
|
|
uint64_t DecompressAmount(uint64_t nAmount);
|
|
|
|
|
2014-08-20 18:33:30 +02:00
|
|
|
/** Compact serializer for scripts.
|
|
|
|
*
|
|
|
|
* It detects common cases and encodes them much more efficiently.
|
|
|
|
* 3 special cases are defined:
|
|
|
|
* * Pay to pubkey hash (encoded as 21 bytes)
|
|
|
|
* * Pay to script hash (encoded as 21 bytes)
|
|
|
|
* * Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
|
|
|
|
*
|
|
|
|
* Other scripts up to 121 bytes require 1 byte + script length. Above
|
|
|
|
* that, scripts up to 16505 bytes require 2 bytes + script length.
|
|
|
|
*/
|
2020-01-18 16:32:31 +01:00
|
|
|
struct ScriptCompression
|
2014-08-20 18:33:30 +02:00
|
|
|
{
|
2014-10-31 01:43:19 +01:00
|
|
|
/**
|
|
|
|
* make this static for now (there are only 6 special scripts defined)
|
|
|
|
* this can potentially be extended together with a new nVersion for
|
|
|
|
* transactions, in which case this value becomes dependent on nVersion
|
|
|
|
* and nHeight of the enclosing transaction.
|
|
|
|
*/
|
2014-08-20 18:33:30 +02:00
|
|
|
static const unsigned int nSpecialScripts = 6;
|
|
|
|
|
|
|
|
template<typename Stream>
|
2020-01-18 16:32:31 +01:00
|
|
|
void Ser(Stream &s, const CScript& script) {
|
2014-08-20 18:33:30 +02:00
|
|
|
std::vector<unsigned char> compr;
|
2017-07-09 01:43:34 +02:00
|
|
|
if (CompressScript(script, compr)) {
|
2018-04-08 17:57:22 +02:00
|
|
|
s << MakeSpan(compr);
|
2014-08-20 18:33:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
unsigned int nSize = script.size() + nSpecialScripts;
|
|
|
|
s << VARINT(nSize);
|
2018-04-08 17:57:22 +02:00
|
|
|
s << MakeSpan(script);
|
2014-08-20 18:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Stream>
|
2020-01-18 16:32:31 +01:00
|
|
|
void Unser(Stream &s, CScript& script) {
|
2014-08-20 18:33:30 +02:00
|
|
|
unsigned int nSize = 0;
|
|
|
|
s >> VARINT(nSize);
|
|
|
|
if (nSize < nSpecialScripts) {
|
2017-07-09 01:43:34 +02:00
|
|
|
std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
|
2018-04-08 17:57:22 +02:00
|
|
|
s >> MakeSpan(vch);
|
2017-07-09 01:43:34 +02:00
|
|
|
DecompressScript(script, nSize, vch);
|
2014-08-20 18:33:30 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
nSize -= nSpecialScripts;
|
2016-04-26 13:23:02 +02:00
|
|
|
if (nSize > MAX_SCRIPT_SIZE) {
|
|
|
|
// Overly long script, replace with a short invalid one
|
|
|
|
script << OP_RETURN;
|
|
|
|
s.ignore(nSize);
|
|
|
|
} else {
|
|
|
|
script.resize(nSize);
|
2018-04-08 17:57:22 +02:00
|
|
|
s >> MakeSpan(script);
|
2016-04-26 13:23:02 +02:00
|
|
|
}
|
2014-08-20 18:33:30 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-18 16:32:31 +01:00
|
|
|
struct AmountCompression
|
2014-10-19 04:28:43 +02:00
|
|
|
{
|
2020-01-18 16:32:31 +01:00
|
|
|
template<typename Stream, typename I> void Ser(Stream& s, I val)
|
|
|
|
{
|
|
|
|
s << VARINT(CompressAmount(val));
|
|
|
|
}
|
|
|
|
template<typename Stream, typename I> void Unser(Stream& s, I& val)
|
|
|
|
{
|
|
|
|
uint64_t v;
|
|
|
|
s >> VARINT(v);
|
|
|
|
val = DecompressAmount(v);
|
2014-10-19 04:28:43 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-18 16:32:31 +01:00
|
|
|
/** wrapper for CTxOut that provides a more compact serialization */
|
|
|
|
struct TxOutCompression
|
|
|
|
{
|
|
|
|
FORMATTER_METHODS(CTxOut, obj) { READWRITE(Using<AmountCompression>(obj.nValue), Using<ScriptCompression>(obj.scriptPubKey)); }
|
|
|
|
};
|
|
|
|
|
2014-11-03 16:16:40 +01:00
|
|
|
#endif // BITCOIN_COMPRESSOR_H
|