You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compressor.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef STARWELS_COMPRESSOR_H
  6. #define STARWELS_COMPRESSOR_H
  7. #include "primitives/transaction.h"
  8. #include "script/script.h"
  9. #include "serialize.h"
  10. class CKeyID;
  11. class CPubKey;
  12. class CScriptID;
  13. /** Compact serializer for scripts.
  14. *
  15. * It detects common cases and encodes them much more efficiently.
  16. * 3 special cases are defined:
  17. * * Pay to pubkey hash (encoded as 21 bytes)
  18. * * Pay to script hash (encoded as 21 bytes)
  19. * * Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
  20. *
  21. * Other scripts up to 121 bytes require 1 byte + script length. Above
  22. * that, scripts up to 16505 bytes require 2 bytes + script length.
  23. */
  24. class CScriptCompressor
  25. {
  26. private:
  27. /**
  28. * make this static for now (there are only 6 special scripts defined)
  29. * this can potentially be extended together with a new nVersion for
  30. * transactions, in which case this value becomes dependent on nVersion
  31. * and nHeight of the enclosing transaction.
  32. */
  33. static const unsigned int nSpecialScripts = 6;
  34. CScript &script;
  35. protected:
  36. /**
  37. * These check for scripts for which a special case with a shorter encoding is defined.
  38. * They are implemented separately from the CScript test, as these test for exact byte
  39. * sequence correspondences, and are more strict. For example, IsToPubKey also verifies
  40. * whether the public key is valid (as invalid ones cannot be represented in compressed
  41. * form).
  42. */
  43. bool IsToKeyID(CKeyID &hash) const;
  44. bool IsToScriptID(CScriptID &hash) const;
  45. bool IsToPubKey(CPubKey &pubkey) const;
  46. bool Compress(std::vector<unsigned char> &out) const;
  47. unsigned int GetSpecialSize(unsigned int nSize) const;
  48. bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out);
  49. public:
  50. CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
  51. template<typename Stream>
  52. void Serialize(Stream &s) const {
  53. std::vector<unsigned char> compr;
  54. if (Compress(compr)) {
  55. s << CFlatData(compr);
  56. return;
  57. }
  58. unsigned int nSize = script.size() + nSpecialScripts;
  59. s << VARINT(nSize);
  60. s << CFlatData(script);
  61. }
  62. template<typename Stream>
  63. void Unserialize(Stream &s) {
  64. unsigned int nSize = 0;
  65. s >> VARINT(nSize);
  66. if (nSize < nSpecialScripts) {
  67. std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
  68. s >> REF(CFlatData(vch));
  69. Decompress(nSize, vch);
  70. return;
  71. }
  72. nSize -= nSpecialScripts;
  73. if (nSize > MAX_SCRIPT_SIZE) {
  74. // Overly long script, replace with a short invalid one
  75. script << OP_RETURN;
  76. s.ignore(nSize);
  77. } else {
  78. script.resize(nSize);
  79. s >> REF(CFlatData(script));
  80. }
  81. }
  82. };
  83. /** wrapper for CTxOut that provides a more compact serialization */
  84. class CTxOutCompressor
  85. {
  86. private:
  87. CTxOut &txout;
  88. public:
  89. static uint64_t CompressAmount(uint64_t nAmount);
  90. static uint64_t DecompressAmount(uint64_t nAmount);
  91. CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
  92. ADD_SERIALIZE_METHODS;
  93. template <typename Stream, typename Operation>
  94. inline void SerializationOp(Stream& s, Operation ser_action) {
  95. if (!ser_action.ForRead()) {
  96. uint64_t nVal = CompressAmount(txout.nValue);
  97. READWRITE(VARINT(nVal));
  98. } else {
  99. uint64_t nVal = 0;
  100. READWRITE(VARINT(nVal));
  101. txout.nValue = DecompressAmount(nVal);
  102. }
  103. CScriptCompressor cscript(REF(txout.scriptPubKey));
  104. READWRITE(cscript);
  105. }
  106. };
  107. #endif // STARWELS_COMPRESSOR_H