Skip to content

Commit 723c752

Browse files
committed
Merge pull request bitcoin#5100
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)
2 parents cd9114e + 99f41b9 commit 723c752

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+992
-939
lines changed

src/Makefile.am

+8-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ BITCOIN_CORE_H = \
8080
coincontrol.h \
8181
coins.h \
8282
compat.h \
83-
core.h \
83+
compressor.h \
84+
core/block.h \
85+
core/transaction.h \
8486
core_io.h \
8587
crypter.h \
8688
db.h \
@@ -103,7 +105,6 @@ BITCOIN_CORE_H = \
103105
rpcclient.h \
104106
rpcprotocol.h \
105107
rpcserver.h \
106-
script/compressor.h \
107108
script/interpreter.h \
108109
script/script.h \
109110
script/sigcache.h \
@@ -119,6 +120,7 @@ BITCOIN_CORE_H = \
119120
txmempool.h \
120121
ui_interface.h \
121122
uint256.h \
123+
undo.h \
122124
util.h \
123125
utilstrencodings.h \
124126
utilmoneystr.h \
@@ -209,10 +211,13 @@ univalue_libbitcoin_univalue_a_SOURCES = \
209211
libbitcoin_common_a_CPPFLAGS = $(BITCOIN_INCLUDES)
210212
libbitcoin_common_a_SOURCES = \
211213
allocators.cpp \
214+
amount.cpp \
212215
base58.cpp \
213216
chainparams.cpp \
214217
coins.cpp \
215-
core.cpp \
218+
compressor.cpp \
219+
core/block.cpp \
220+
core/transaction.cpp \
216221
core_read.cpp \
217222
core_write.cpp \
218223
ecwrapper.cpp \
@@ -221,7 +226,6 @@ libbitcoin_common_a_SOURCES = \
221226
keystore.cpp \
222227
netbase.cpp \
223228
protocol.cpp \
224-
script/compressor.cpp \
225229
script/interpreter.cpp \
226230
script/script.cpp \
227231
script/sigcache.cpp \

src/amount.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2009-2010 Satoshi Nakamoto
2+
// Copyright (c) 2009-2014 The Bitcoin developers
3+
// Distributed under the MIT software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include "amount.h"
7+
8+
#include "tinyformat.h"
9+
10+
CFeeRate::CFeeRate(const CAmount& nFeePaid, size_t nSize)
11+
{
12+
if (nSize > 0)
13+
nSatoshisPerK = nFeePaid*1000/nSize;
14+
else
15+
nSatoshisPerK = 0;
16+
}
17+
18+
CAmount CFeeRate::GetFee(size_t nSize) const
19+
{
20+
CAmount nFee = nSatoshisPerK*nSize / 1000;
21+
22+
if (nFee == 0 && nSatoshisPerK > 0)
23+
nFee = nSatoshisPerK;
24+
25+
return nFee;
26+
}
27+
28+
std::string CFeeRate::ToString() const
29+
{
30+
return strprintf("%d.%08d BTC/kB", nSatoshisPerK / COIN, nSatoshisPerK % COIN);
31+
}

src/amount.h

+42-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,49 @@
66
#ifndef BITCOIN_AMOUNT_H
77
#define BITCOIN_AMOUNT_H
88

9-
#include <stdint.h>
9+
#include "serialize.h"
10+
11+
#include <stdlib.h>
12+
#include <string>
1013

1114
typedef int64_t CAmount;
1215

16+
static const CAmount COIN = 100000000;
17+
static const CAmount CENT = 1000000;
18+
19+
/** No amount larger than this (in satoshi) is valid */
20+
static const CAmount MAX_MONEY = 21000000 * COIN;
21+
inline bool MoneyRange(const CAmount& nValue) { return (nValue >= 0 && nValue <= MAX_MONEY); }
22+
23+
/** Type-safe wrapper class to for fee rates
24+
* (how much to pay based on transaction size)
25+
*/
26+
class CFeeRate
27+
{
28+
private:
29+
CAmount nSatoshisPerK; // unit is satoshis-per-1,000-bytes
30+
public:
31+
CFeeRate() : nSatoshisPerK(0) { }
32+
explicit CFeeRate(const CAmount& _nSatoshisPerK): nSatoshisPerK(_nSatoshisPerK) { }
33+
CFeeRate(const CAmount& nFeePaid, size_t nSize);
34+
CFeeRate(const CFeeRate& other) { nSatoshisPerK = other.nSatoshisPerK; }
35+
36+
CAmount GetFee(size_t size) const; // unit returned is satoshis
37+
CAmount GetFeePerK() const { return GetFee(1000); } // satoshis-per-1000-bytes
38+
39+
friend bool operator<(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK < b.nSatoshisPerK; }
40+
friend bool operator>(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK > b.nSatoshisPerK; }
41+
friend bool operator==(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK == b.nSatoshisPerK; }
42+
friend bool operator<=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK <= b.nSatoshisPerK; }
43+
friend bool operator>=(const CFeeRate& a, const CFeeRate& b) { return a.nSatoshisPerK >= b.nSatoshisPerK; }
44+
std::string ToString() const;
45+
46+
ADD_SERIALIZE_METHODS;
47+
48+
template <typename Stream, typename Operation>
49+
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
50+
READWRITE(nSatoshisPerK);
51+
}
52+
};
53+
1354
#endif // BITCOIN_AMOUNT_H

src/bitcoin-tx.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include "base58.h"
6-
#include "core.h"
6+
#include "core/transaction.h"
77
#include "core_io.h"
88
#include "keystore.h"
99
#include "main.h" // for MAX_BLOCK_SIZE

src/bloom.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "bloom.h"
66

7-
#include "core.h"
7+
#include "core/transaction.h"
88
#include "script/script.h"
99
#include "script/standard.h"
1010
#include "streams.h"

src/chain.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef H_BITCOIN_CHAIN
77
#define H_BITCOIN_CHAIN
88

9-
#include "core.h"
9+
#include "core/block.h"
1010
#include "pow.h"
1111
#include "tinyformat.h"
1212
#include "uint256.h"

src/chainparams.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
#ifndef BITCOIN_CHAIN_PARAMS_H
77
#define BITCOIN_CHAIN_PARAMS_H
88

9-
#include "core.h"
109
#include "chainparamsbase.h"
1110
#include "checkpoints.h"
11+
#include "core/block.h"
1212
#include "protocol.h"
1313
#include "uint256.h"
1414

src/coincontrol.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#ifndef COINCONTROL_H
66
#define COINCONTROL_H
77

8-
#include "core.h"
8+
#include "core/transaction.h"
99

1010
/** Coin Control Features. */
1111
class CCoinControl

src/coins.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
#ifndef BITCOIN_COINS_H
77
#define BITCOIN_COINS_H
88

9-
#include "core.h"
9+
#include "compressor.h"
1010
#include "serialize.h"
1111
#include "uint256.h"
12+
#include "undo.h"
1213

1314
#include <assert.h>
1415
#include <stdint.h>

src/script/compressor.cpp src/compressor.cpp

+55
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 src/compressor.h

+33-3
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

0 commit comments

Comments
 (0)