Skip to content

Commit d2d7267

Browse files
committed
Merge #12752: [MOVEONLY] Move compressor utility functions out of class
76a9aac Move compressor utility functions out of class (Pieter Wuille) Pull request description: This is a refactor from #10785 with no functionality change. Move the compresion utility functions out of CScriptCompressor, as a preparation for making the class templated. I'm submitting it as a separate PR as I think it's a general improvement to code readability, and to reduce the diff further on. Tree-SHA512: 3b3d17c2b96e43f752f512dd573296a6bb15cae165fbe3c79212a0970f5196a62a59a821d5100f29638af1e7461c9171f3dccb8112f005ee08da0ec7fe0073fd
2 parents ff48f62 + 76a9aac commit d2d7267

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

src/compressor.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
#include <pubkey.h>
1010
#include <script/standard.h>
1111

12-
bool CScriptCompressor::IsToKeyID(CKeyID &hash) const
12+
/*
13+
* These check for scripts for which a special case with a shorter encoding is defined.
14+
* They are implemented separately from the CScript test, as these test for exact byte
15+
* sequence correspondences, and are more strict. For example, IsToPubKey also verifies
16+
* whether the public key is valid (as invalid ones cannot be represented in compressed
17+
* form).
18+
*/
19+
20+
static bool IsToKeyID(const CScript& script, CKeyID &hash)
1321
{
1422
if (script.size() == 25 && script[0] == OP_DUP && script[1] == OP_HASH160
1523
&& script[2] == 20 && script[23] == OP_EQUALVERIFY
@@ -20,7 +28,7 @@ bool CScriptCompressor::IsToKeyID(CKeyID &hash) const
2028
return false;
2129
}
2230

23-
bool CScriptCompressor::IsToScriptID(CScriptID &hash) const
31+
static bool IsToScriptID(const CScript& script, CScriptID &hash)
2432
{
2533
if (script.size() == 23 && script[0] == OP_HASH160 && script[1] == 20
2634
&& script[22] == OP_EQUAL) {
@@ -30,7 +38,7 @@ bool CScriptCompressor::IsToScriptID(CScriptID &hash) const
3038
return false;
3139
}
3240

33-
bool CScriptCompressor::IsToPubKey(CPubKey &pubkey) const
41+
static bool IsToPubKey(const CScript& script, CPubKey &pubkey)
3442
{
3543
if (script.size() == 35 && script[0] == 33 && script[34] == OP_CHECKSIG
3644
&& (script[1] == 0x02 || script[1] == 0x03)) {
@@ -45,24 +53,24 @@ bool CScriptCompressor::IsToPubKey(CPubKey &pubkey) const
4553
return false;
4654
}
4755

48-
bool CScriptCompressor::Compress(std::vector<unsigned char> &out) const
56+
bool CompressScript(const CScript& script, std::vector<unsigned char> &out)
4957
{
5058
CKeyID keyID;
51-
if (IsToKeyID(keyID)) {
59+
if (IsToKeyID(script, keyID)) {
5260
out.resize(21);
5361
out[0] = 0x00;
5462
memcpy(&out[1], &keyID, 20);
5563
return true;
5664
}
5765
CScriptID scriptID;
58-
if (IsToScriptID(scriptID)) {
66+
if (IsToScriptID(script, scriptID)) {
5967
out.resize(21);
6068
out[0] = 0x01;
6169
memcpy(&out[1], &scriptID, 20);
6270
return true;
6371
}
6472
CPubKey pubkey;
65-
if (IsToPubKey(pubkey)) {
73+
if (IsToPubKey(script, pubkey)) {
6674
out.resize(33);
6775
memcpy(&out[1], &pubkey[1], 32);
6876
if (pubkey[0] == 0x02 || pubkey[0] == 0x03) {
@@ -76,7 +84,7 @@ bool CScriptCompressor::Compress(std::vector<unsigned char> &out) const
7684
return false;
7785
}
7886

79-
unsigned int CScriptCompressor::GetSpecialSize(unsigned int nSize) const
87+
unsigned int GetSpecialScriptSize(unsigned int nSize)
8088
{
8189
if (nSize == 0 || nSize == 1)
8290
return 20;
@@ -85,7 +93,7 @@ unsigned int CScriptCompressor::GetSpecialSize(unsigned int nSize) const
8593
return 0;
8694
}
8795

88-
bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigned char> &in)
96+
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &in)
8997
{
9098
switch(nSize) {
9199
case 0x00:
@@ -139,7 +147,7 @@ bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigne
139147
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
140148
// (this is decodable, as d is in [1-9] and e is in [0-9])
141149

142-
uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
150+
uint64_t CompressAmount(uint64_t n)
143151
{
144152
if (n == 0)
145153
return 0;
@@ -158,7 +166,7 @@ uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
158166
}
159167
}
160168

161-
uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
169+
uint64_t DecompressAmount(uint64_t x)
162170
{
163171
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
164172
if (x == 0)

src/compressor.h

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class CKeyID;
1414
class CPubKey;
1515
class CScriptID;
1616

17+
bool CompressScript(const CScript& script, std::vector<unsigned char> &out);
18+
unsigned int GetSpecialScriptSize(unsigned int nSize);
19+
bool DecompressScript(CScript& script, unsigned int nSize, const std::vector<unsigned char> &out);
20+
21+
uint64_t CompressAmount(uint64_t nAmount);
22+
uint64_t DecompressAmount(uint64_t nAmount);
23+
1724
/** Compact serializer for scripts.
1825
*
1926
* It detects common cases and encodes them much more efficiently.
@@ -37,28 +44,13 @@ class CScriptCompressor
3744
static const unsigned int nSpecialScripts = 6;
3845

3946
CScript &script;
40-
protected:
41-
/**
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
46-
* form).
47-
*/
48-
bool IsToKeyID(CKeyID &hash) const;
49-
bool IsToScriptID(CScriptID &hash) const;
50-
bool IsToPubKey(CPubKey &pubkey) const;
51-
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);
5547
public:
5648
explicit CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
5749

5850
template<typename Stream>
5951
void Serialize(Stream &s) const {
6052
std::vector<unsigned char> compr;
61-
if (Compress(compr)) {
53+
if (CompressScript(script, compr)) {
6254
s << CFlatData(compr);
6355
return;
6456
}
@@ -72,9 +64,9 @@ class CScriptCompressor
7264
unsigned int nSize = 0;
7365
s >> VARINT(nSize);
7466
if (nSize < nSpecialScripts) {
75-
std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
67+
std::vector<unsigned char> vch(GetSpecialScriptSize(nSize), 0x00);
7668
s >> CFlatData(vch);
77-
Decompress(nSize, vch);
69+
DecompressScript(script, nSize, vch);
7870
return;
7971
}
8072
nSize -= nSpecialScripts;
@@ -96,9 +88,6 @@ class CTxOutCompressor
9688
CTxOut &txout;
9789

9890
public:
99-
static uint64_t CompressAmount(uint64_t nAmount);
100-
static uint64_t DecompressAmount(uint64_t nAmount);
101-
10291
explicit CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
10392

10493
ADD_SERIALIZE_METHODS;

src/test/compress_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,16 @@
2525
BOOST_FIXTURE_TEST_SUITE(compress_tests, BasicTestingSetup)
2626

2727
bool static TestEncode(uint64_t in) {
28-
return in == CTxOutCompressor::DecompressAmount(CTxOutCompressor::CompressAmount(in));
28+
return in == DecompressAmount(CompressAmount(in));
2929
}
3030

3131
bool static TestDecode(uint64_t in) {
32-
return in == CTxOutCompressor::CompressAmount(CTxOutCompressor::DecompressAmount(in));
32+
return in == CompressAmount(DecompressAmount(in));
3333
}
3434

3535
bool static TestPair(uint64_t dec, uint64_t enc) {
36-
return CTxOutCompressor::CompressAmount(dec) == enc &&
37-
CTxOutCompressor::DecompressAmount(enc) == dec;
36+
return CompressAmount(dec) == enc &&
37+
DecompressAmount(enc) == dec;
3838
}
3939

4040
BOOST_AUTO_TEST_CASE(compress_amounts)

0 commit comments

Comments
 (0)