Skip to content

Commit

Permalink
MOVEONLY: Move script/compressor out of script and put CTxOutCompress…
Browse files Browse the repository at this point in the history
…or (from

core) with it
  • Loading branch information
jtimon committed Oct 27, 2014
1 parent 999a2ab commit 561e9e9
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 93 deletions.
4 changes: 2 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ BITCOIN_CORE_H = \
coincontrol.h \
coins.h \
compat.h \
compressor.h \
core.h \
core/transaction.h \
core_io.h \
Expand All @@ -103,7 +104,6 @@ BITCOIN_CORE_H = \
rpcclient.h \
rpcprotocol.h \
rpcserver.h \
script/compressor.h \
script/interpreter.h \
script/script.h \
script/sigcache.h \
Expand Down Expand Up @@ -214,6 +214,7 @@ libbitcoin_common_a_SOURCES = \
base58.cpp \
chainparams.cpp \
coins.cpp \
compressor.cpp \
core.cpp \
core/transaction.cpp \
core_read.cpp \
Expand All @@ -223,7 +224,6 @@ libbitcoin_common_a_SOURCES = \
keystore.cpp \
netbase.cpp \
protocol.cpp \
script/compressor.cpp \
script/interpreter.cpp \
script/script.cpp \
script/sigcache.cpp \
Expand Down
3 changes: 1 addition & 2 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
#ifndef BITCOIN_COINS_H
#define BITCOIN_COINS_H

#include "core.h" // Only for CTxOutCompressor
#include "core/transaction.h"
#include "compressor.h"
#include "serialize.h"
#include "uint256.h"
#include "undo.h"
Expand Down
55 changes: 55 additions & 0 deletions src/script/compressor.cpp → src/compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "compressor.h"

#include "hash.h"
#include "key.h"
#include "script/standard.h"

Expand Down Expand Up @@ -128,3 +129,57 @@ bool CScriptCompressor::Decompress(unsigned int nSize, const std::vector<unsigne
}
return false;
}

// Amount compression:
// * If the amount is 0, output 0
// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
// * call the result n
// * output 1 + 10*(9*n + d - 1) + e
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
// (this is decodable, as d is in [1-9] and e is in [0-9])

uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
{
if (n == 0)
return 0;
int e = 0;
while (((n % 10) == 0) && e < 9) {
n /= 10;
e++;
}
if (e < 9) {
int d = (n % 10);
assert(d >= 1 && d <= 9);
n /= 10;
return 1 + (n*9 + d - 1)*10 + e;
} else {
return 1 + (n - 1)*10 + 9;
}
}

uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
{
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
if (x == 0)
return 0;
x--;
// x = 10*(9*n + d - 1) + e
int e = x % 10;
x /= 10;
uint64_t n = 0;
if (e < 9) {
// x = 9*n + d - 1
int d = (x % 9) + 1;
x /= 9;
// x = n
n = x*10 + d;
} else {
n = x+1;
}
while (e) {
n *= 10;
e--;
}
return n;
}
36 changes: 33 additions & 3 deletions src/script/compressor.h → src/compressor.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef H_BITCOIN_SCRIPT_COMPRESSOR
#define H_BITCOIN_SCRIPT_COMPRESSOR
#ifndef H_BITCOIN_COMPRESSOR
#define H_BITCOIN_COMPRESSOR

#include "core/transaction.h"
#include "script/script.h"
#include "serialize.h"

Expand Down Expand Up @@ -86,4 +87,33 @@ class CScriptCompressor
}
};

#endif // H_BITCOIN_SCRIPT_COMPRESSOR
/** wrapper for CTxOut that provides a more compact serialization */
class CTxOutCompressor
{
private:
CTxOut &txout;

public:
static uint64_t CompressAmount(uint64_t nAmount);
static uint64_t DecompressAmount(uint64_t nAmount);

CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!ser_action.ForRead()) {
uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal));
} else {
uint64_t nVal = 0;
READWRITE(VARINT(nVal));
txout.nValue = DecompressAmount(nVal);
}
CScriptCompressor cscript(REF(txout.scriptPubKey));
READWRITE(cscript);
}
};

#endif // H_BITCOIN_COMPRESSOR
54 changes: 0 additions & 54 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,6 @@
#include "tinyformat.h"
#include "utilstrencodings.h"

// Amount compression:
// * If the amount is 0, output 0
// * first, divide the amount (in base units) by the largest power of 10 possible; call the exponent e (e is max 9)
// * if e<9, the last digit of the resulting number cannot be 0; store it as d, and drop it (divide by 10)
// * call the result n
// * output 1 + 10*(9*n + d - 1) + e
// * if e==9, we only know the resulting number is not zero, so output 1 + 10*(n - 1) + 9
// (this is decodable, as d is in [1-9] and e is in [0-9])

uint64_t CTxOutCompressor::CompressAmount(uint64_t n)
{
if (n == 0)
return 0;
int e = 0;
while (((n % 10) == 0) && e < 9) {
n /= 10;
e++;
}
if (e < 9) {
int d = (n % 10);
assert(d >= 1 && d <= 9);
n /= 10;
return 1 + (n*9 + d - 1)*10 + e;
} else {
return 1 + (n - 1)*10 + 9;
}
}

uint64_t CTxOutCompressor::DecompressAmount(uint64_t x)
{
// x = 0 OR x = 1+10*(9*n + d - 1) + e OR x = 1+10*(n - 1) + 9
if (x == 0)
return 0;
x--;
// x = 10*(9*n + d - 1) + e
int e = x % 10;
x /= 10;
uint64_t n = 0;
if (e < 9) {
// x = 9*n + d - 1
int d = (x % 9) + 1;
x /= 9;
// x = n
n = x*10 + d;
} else {
n = x+1;
}
while (e) {
n *= 10;
e--;
}
return n;
}

uint256 CBlockHeader::GetHash() const
{
return Hash(BEGIN(nVersion), END(nNonce));
Expand Down
30 changes: 0 additions & 30 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,9 @@
#define BITCOIN_CORE_H

#include "core/transaction.h"
#include "script/compressor.h"
#include "serialize.h"
#include "uint256.h"

/** wrapper for CTxOut that provides a more compact serialization */
class CTxOutCompressor
{
private:
CTxOut &txout;

public:
static uint64_t CompressAmount(uint64_t nAmount);
static uint64_t DecompressAmount(uint64_t nAmount);

CTxOutCompressor(CTxOut &txoutIn) : txout(txoutIn) { }

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
if (!ser_action.ForRead()) {
uint64_t nVal = CompressAmount(txout.nValue);
READWRITE(VARINT(nVal));
} else {
uint64_t nVal = 0;
READWRITE(VARINT(nVal));
txout.nValue = DecompressAmount(nVal);
}
CScriptCompressor cscript(REF(txout.scriptPubKey));
READWRITE(cscript);
}
};

/** Nodes collect new transactions into a block, hash them into a hash tree,
* and scan through nonce values to make the block's hash satisfy proof-of-work
* requirements. When they solve the proof-of-work, they broadcast the block
Expand Down
2 changes: 1 addition & 1 deletion src/test/compress_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "main.h"
#include "compressor.h"
#include "util.h"

#include <stdint.h>
Expand Down
2 changes: 1 addition & 1 deletion src/undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#ifndef H_BITCOIN_TXUNDO
#define H_BITCOIN_TXUNDO

#include "core.h" // Only for CTxOutCompressor
#include "compressor.h"
#include "core/transaction.h"
#include "serialize.h"

Expand Down

0 comments on commit 561e9e9

Please sign in to comment.