Skip to content

Commit

Permalink
Merge pull request #5100
Browse files Browse the repository at this point in the history
99f41b9 MOVEONLY: core.o -> core/block.o (jtimon)
561e9e9 MOVEONLY: Move script/compressor out of script and put CTxOutCompressor (from core) with it (jtimon)
999a2ab MOVEONLY: separate CTxUndo out of core (jtimon)
4a3587d MOVEONLY: Separate CTransaction and dependencies from core (jtimon)
eda3733 MOVEONLY: Move CFeeRate and Amount constants to amount.o (jtimon)
  • Loading branch information
sipa committed Oct 28, 2014
2 parents cd9114e + 99f41b9 commit 723c752
Show file tree
Hide file tree
Showing 41 changed files with 992 additions and 939 deletions.
12 changes: 8 additions & 4 deletions src/Makefile.am
Expand Up @@ -80,7 +80,9 @@ BITCOIN_CORE_H = \
coincontrol.h \
coins.h \
compat.h \
core.h \
compressor.h \
core/block.h \
core/transaction.h \
core_io.h \
crypter.h \
db.h \
Expand All @@ -103,7 +105,6 @@ BITCOIN_CORE_H = \
rpcclient.h \
rpcprotocol.h \
rpcserver.h \
script/compressor.h \
script/interpreter.h \
script/script.h \
script/sigcache.h \
Expand All @@ -119,6 +120,7 @@ BITCOIN_CORE_H = \
txmempool.h \
ui_interface.h \
uint256.h \
undo.h \
util.h \
utilstrencodings.h \
utilmoneystr.h \
Expand Down Expand Up @@ -209,10 +211,13 @@ univalue_libbitcoin_univalue_a_SOURCES = \
libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES)
libbitcoin_common_a_SOURCES = \
allocators.cpp \
amount.cpp \
base58.cpp \
chainparams.cpp \
coins.cpp \
core.cpp \
compressor.cpp \
core/block.cpp \
core/transaction.cpp \
core_read.cpp \
core_write.cpp \
ecwrapper.cpp \
Expand All @@ -221,7 +226,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
31 changes: 31 additions & 0 deletions src/amount.cpp
@@ -0,0 +1,31 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "amount.h"

#include "tinyformat.h"

CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize)
{
if (nSize > 0)
nSatoshisPerK = nFeePaid*1000/nSize;
else
nSatoshisPerK = 0;
}

CAmount CFeeRate::GetFee(size_t nSize) const
{
CAmount nFee = nSatoshisPerK*nSize / 1000;

if (nFee == 0 && nSatoshisPerK > 0)
nFee = nSatoshisPerK;

return nFee;
}

std::string CFeeRate::ToString() const
{
return strprintf("%d.%08d BTC/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN);
}
43 changes: 42 additions & 1 deletion src/amount.h
Expand Up @@ -6,8 +6,49 @@
#ifndef BITCOIN_AMOUNT_H
#define BITCOIN_AMOUNT_H

#include <stdint.h>
#include "serialize.h"

#include <stdlib.h>
#include <string>

typedef int64_t CAmount;

static const CAmount COIN = 100000000;
static const CAmount CENT = 1000000;

/** No amount larger than this (in satoshi) is valid */
static const CAmount MAX_MONEY = 21000000 * COIN;
inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }

/** Type-safe wrapper class to for fee rates
* (how much to pay based on transaction size)
*/
class CFeeRate
{
private:
CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
public:
CFeeRate() : nSatoshisPerK(0) { }
explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
CFeeRate(const CAmount& nFeePaid, size_t nSize);
CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }

CAmount GetFee(size_t size) const; // unit returned is satoshis
CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes

friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
std::string ToString() const;

ADD_SERIALIZE_METHODS;

template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
READWRITE(nSatoshisPerK);
}
};

#endif // BITCOIN_AMOUNT_H
2 changes: 1 addition & 1 deletion src/bitcoin-tx.cpp
Expand Up @@ -3,7 +3,7 @@
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "base58.h"
#include "core.h"
#include "core/transaction.h"
#include "core_io.h"
#include "keystore.h"
#include "main.h" // for MAX_BLOCK_SIZE
Expand Down
2 changes: 1 addition & 1 deletion src/bloom.cpp
Expand Up @@ -4,7 +4,7 @@

#include "bloom.h"

#include "core.h"
#include "core/transaction.h"
#include "script/script.h"
#include "script/standard.h"
#include "streams.h"
Expand Down
2 changes: 1 addition & 1 deletion src/chain.h
Expand Up @@ -6,7 +6,7 @@
#ifndef H_BITCOIN_CHAIN
#define H_BITCOIN_CHAIN

#include "core.h"
#include "core/block.h"
#include "pow.h"
#include "tinyformat.h"
#include "uint256.h"
Expand Down
2 changes: 1 addition & 1 deletion src/chainparams.h
Expand Up @@ -6,9 +6,9 @@
#ifndef BITCOIN_CHAIN_PARAMS_H
#define BITCOIN_CHAIN_PARAMS_H

#include "core.h"
#include "chainparamsbase.h"
#include "checkpoints.h"
#include "core/block.h"
#include "protocol.h"
#include "uint256.h"

Expand Down
2 changes: 1 addition & 1 deletion src/coincontrol.h
Expand Up @@ -5,7 +5,7 @@
#ifndef COINCONTROL_H
#define COINCONTROL_H

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

/** Coin Control Features. */
class CCoinControl
Expand Down
3 changes: 2 additions & 1 deletion src/coins.h
Expand Up @@ -6,9 +6,10 @@
#ifndef BITCOIN_COINS_H
#define BITCOIN_COINS_H

#include "core.h"
#include "compressor.h"
#include "serialize.h"
#include "uint256.h"
#include "undo.h"

#include <assert.h>
#include <stdint.h>
Expand Down
55 changes: 55 additions & 0 deletions src/script/compressor.cpp → src/compressor.cpp
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
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

0 comments on commit 723c752

Please sign in to comment.