Skip to content

Commit 561e9e9

Browse files
author
jtimon
committed
MOVEONLY: Move script/compressor out of script and put CTxOutCompressor (from
core) with it
1 parent 999a2ab commit 561e9e9

File tree

8 files changed

+93
-93
lines changed

8 files changed

+93
-93
lines changed

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ BITCOIN_CORE_H = \
8080
coincontrol.h \
8181
coins.h \
8282
compat.h \
83+
compressor.h \
8384
core.h \
8485
core/transaction.h \
8586
core_io.h \
@@ -103,7 +104,6 @@ BITCOIN_CORE_H = \
103104
rpcclient.h \
104105
rpcprotocol.h \
105106
rpcserver.h \
106-
script/compressor.h \
107107
script/interpreter.h \
108108
script/script.h \
109109
script/sigcache.h \
@@ -214,6 +214,7 @@ libbitcoin_common_a_SOURCES = \
214214
base58.cpp \
215215
chainparams.cpp \
216216
coins.cpp \
217+
compressor.cpp \
217218
core.cpp \
218219
core/transaction.cpp \
219220
core_read.cpp \
@@ -223,7 +224,6 @@ libbitcoin_common_a_SOURCES = \
223224
keystore.cpp \
224225
netbase.cpp \
225226
protocol.cpp \
226-
script/compressor.cpp \
227227
script/interpreter.cpp \
228228
script/script.cpp \
229229
script/sigcache.cpp \

src/coins.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#ifndef BITCOIN_COINS_H
77
#define BITCOIN_COINS_H
88

9-
#include "core.h" // Only for CTxOutCompressor
10-
#include "core/transaction.h"
9+
#include "compressor.h"
1110
#include "serialize.h"
1211
#include "uint256.h"
1312
#include "undo.h"

src/script/compressor.cpp renamed to src/compressor.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
#include "compressor.h"
77

8+
#include "hash.h"
89
#include "key.h"
910
#include "script/standard.h"
1011

@@ -128,3 +129,57 @@ bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigne
128129
}
129130
return false;
130131
}
132+
133+
// Amount compression:
134+
// * If the amount is 0, output 0
135+
// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
136+
// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
137+
// * call the result n
138+
// * output 1 + 10*(9*n + d - 1) + e
139+
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
140+
// (this is decodable, as d is in [1-9] and e is in [0-9])
141+
142+
uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
143+
{
144+
if (n == 0)
145+
return 0;
146+
int e = 0;
147+
while (((n % 10) == 0) && e < 9) {
148+
n /= 10;
149+
e++;
150+
}
151+
if (e < 9) {
152+
int d = (n % 10);
153+
assert(d >= 1 && d <= 9);
154+
n /= 10;
155+
return 1 + (n*9 + d - 1)*10 + e;
156+
} else {
157+
return 1 + (n - 1)*10 + 9;
158+
}
159+
}
160+
161+
uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
162+
{
163+
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
164+
if (x == 0)
165+
return 0;
166+
x--;
167+
// x = 10*(9*n + d - 1) + e
168+
int e = x % 10;
169+
x /= 10;
170+
uint64_t n = 0;
171+
if (e < 9) {
172+
// x = 9*n + d - 1
173+
int d = (x % 9) + 1;
174+
x /= 9;
175+
// x = n
176+
n = x*10 + d;
177+
} else {
178+
n = x+1;
179+
}
180+
while (e) {
181+
n *= 10;
182+
e--;
183+
}
184+
return n;
185+
}

src/script/compressor.h renamed to src/compressor.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#ifndef H_BITCOIN_SCRIPT_COMPRESSOR
7-
#define H_BITCOIN_SCRIPT_COMPRESSOR
6+
#ifndef H_BITCOIN_COMPRESSOR
7+
#define H_BITCOIN_COMPRESSOR
88

9+
#include "core/transaction.h"
910
#include "script/script.h"
1011
#include "serialize.h"
1112

@@ -86,4 +87,33 @@ class CScriptCompressor
8687
}
8788
};
8889

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

src/core.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,6 @@
99
#include "tinyformat.h"
1010
#include "utilstrencodings.h"
1111

12-
// Amount compression:
13-
// * If the amount is 0, output 0
14-
// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
15-
// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
16-
// * call the result n
17-
// * output 1 + 10*(9*n + d - 1) + e
18-
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
19-
// (this is decodable, as d is in [1-9] and e is in [0-9])
20-
21-
uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
22-
{
23-
if (n == 0)
24-
return 0;
25-
int e = 0;
26-
while (((n % 10) == 0) && e < 9) {
27-
n /= 10;
28-
e++;
29-
}
30-
if (e < 9) {
31-
int d = (n % 10);
32-
assert(d >= 1 && d <= 9);
33-
n /= 10;
34-
return 1 + (n*9 + d - 1)*10 + e;
35-
} else {
36-
return 1 + (n - 1)*10 + 9;
37-
}
38-
}
39-
40-
uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
41-
{
42-
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
43-
if (x == 0)
44-
return 0;
45-
x--;
46-
// x = 10*(9*n + d - 1) + e
47-
int e = x % 10;
48-
x /= 10;
49-
uint64_t n = 0;
50-
if (e < 9) {
51-
// x = 9*n + d - 1
52-
int d = (x % 9) + 1;
53-
x /= 9;
54-
// x = n
55-
n = x*10 + d;
56-
} else {
57-
n = x+1;
58-
}
59-
while (e) {
60-
n *= 10;
61-
e--;
62-
}
63-
return n;
64-
}
65-
6612
uint256 CBlockHeader::GetHash() const
6713
{
6814
return Hash(BEGIN(nVersion), END(nNonce));

src/core.h

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,9 @@
77
#define BITCOIN_CORE_H
88

99
#include "core/transaction.h"
10-
#include "script/compressor.h"
1110
#include "serialize.h"
1211
#include "uint256.h"
1312

14-
/** wrapper for CTxOut that provides a more compact serialization */
15-
class CTxOutCompressor
16-
{
17-
private:
18-
CTxOut &txout;
19-
20-
public:
21-
static uint64_t CompressAmount(uint64_t nAmount);
22-
static uint64_t DecompressAmount(uint64_t nAmount);
23-
24-
CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }
25-
26-
ADD_SERIALIZE_METHODS;
27-
28-
template <typename Stream, typename Operation>
29-
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
30-
if (!ser_action.ForRead()) {
31-
uint64_t nVal = CompressAmount(txout.nValue);
32-
READWRITE(VARINT(nVal));
33-
} else {
34-
uint64_t nVal = 0;
35-
READWRITE(VARINT(nVal));
36-
txout.nValue = DecompressAmount(nVal);
37-
}
38-
CScriptCompressor cscript(REF(txout.scriptPubKey));
39-
READWRITE(cscript);
40-
}
41-
};
42-
4313
/** Nodes collect new transactions into a block, hash them into a hash tree,
4414
* and scan through nonce values to make the block's hash satisfy proof-of-work
4515
* requirements. When they solve the proof-of-work, they broadcast the block

src/test/compress_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Distributed under the MIT/X11 software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5-
#include "main.h"
5+
#include "compressor.h"
66
#include "util.h"
77

88
#include <stdint.h>

src/undo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef H_BITCOIN_TXUNDO
77
#define H_BITCOIN_TXUNDO
88

9-
#include "core.h" // Only for CTxOutCompressor
9+
#include "compressor.h"
1010
#include "core/transaction.h"
1111
#include "serialize.h"
1212

0 commit comments

Comments
 (0)