-
Notifications
You must be signed in to change notification settings - Fork 4
/
tinytx.h
327 lines (276 loc) · 8.76 KB
/
tinytx.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (c) 2018 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_TINYTX_H
#define BITCOIN_TINYTX_H
#include <inttypes.h>
#include <uint256.h>
#ifdef TINY_MINIMAL
# define TINY_NOSERIALIZE
# define TINY_NOUTILSTRENC
# define TINY_NOHASH
#endif
#ifndef TINY_NOSERIALIZE
# include <serialize.h>
#endif
#ifndef TINY_NOUTILSTRENC
# include <utilstrencodings.h>
#endif
#ifndef TINY_NOHASH
# include <hash.h>
# ifdef TINY_NOSERIALIZE
# error tiny hashes require serialization (disable TINY_NOSERIALIZE)
# endif
#endif
namespace tiny {
typedef int64_t amount;
static const amount COIN = 100000000;
#define PRIamt PRIi64
inline std::string coin_str(amount sat) {
char buf[128];
char* pbuf = buf + sprintf(buf, "%" PRIamt ".%08" PRIamt, sat / COIN, sat % COIN);
return std::string(buf, pbuf);
}
static const int SERIALIZE_TRANSACTION_NO_WITNESS = 0x40000000;
struct outpoint {
uint256 hash;
uint32_t n;
outpoint() : n((uint32_t)-1) {}
outpoint(const uint256& hash_in, uint32_t n_in) : hash(hash_in), n(n_in) {}
#ifndef TINY_NOSERIALIZE
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(hash);
READWRITE(n);
}
#endif
bool IsNull() const { return (hash.IsNull() && n == (uint32_t) -1); }
bool operator==(const outpoint& other) const { return hash == other.hash && n == other.n; }
bool operator<(const outpoint& other) const { return hash < other.hash || (hash == other.hash && n < other.n); }
std::string ToString() const { return "outpoint(" + hash.ToString()/*.substr(0,10)*/ + ", " + std::to_string(n) + ")"; }
};
typedef std::vector<uint8_t> script_data_t;
typedef std::vector<std::vector<uint8_t>> script_stack_t;
struct txin {
outpoint prevout;
script_data_t scriptSig;
uint32_t sequence;
script_stack_t scriptWit;
static const uint32_t SEQUENCE_FINAL = 0xffffffff;
txin() : sequence(SEQUENCE_FINAL) {}
txin(outpoint prevout_in, script_data_t scriptSig_in=script_data_t(), uint32_t sequence_in=SEQUENCE_FINAL) {
prevout = prevout_in;
scriptSig = scriptSig_in;
sequence = sequence_in;
}
friend bool operator==(const txin& a, const txin& b)
{
return a.prevout == b.prevout
&& a.scriptSig == b.scriptSig
&& a.sequence == b.sequence
&& a.scriptWit == b.scriptWit;
}
#ifndef TINY_NOSERIALIZE
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(prevout);
READWRITE(scriptSig);
READWRITE(sequence);
}
#endif
#ifndef TINY_NOUTILSTRENC
std::string scriptWitString() const {
std::string ret = "scriptWit(";
for (unsigned int i = 0; i < scriptWit.size(); i++) {
if (i) {
ret += ", ";
}
ret += HexStr(scriptWit[i]);
}
return ret + ")";
}
#endif
std::string ToString() const {
std::string str;
str += "txin(";
str += prevout.ToString();
#ifndef TINY_NOUTILSTRENC
if (prevout.IsNull()) {
str += ", coinbase " + HexStr(scriptSig);
} else {
str += ", scriptSig=" + HexStr(scriptSig).substr(0, 24);
}
#endif
if (sequence != SEQUENCE_FINAL) {
str += ", sequence=" + std::to_string(sequence);
}
str += ")";
return str;
}
};
struct txout {
amount value;
script_data_t scriptPubKey;
txout() : value(-1) {}
txout(const amount& value_in, script_data_t scriptPubKey_in) : value(value_in), scriptPubKey(scriptPubKey_in) {}
friend bool operator==(const txout& a, const txout& b)
{
return a.value == b.value
&& a.scriptPubKey == b.scriptPubKey;
}
#ifndef TINY_NOSERIALIZE
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream& s, Operation ser_action) {
READWRITE(value);
READWRITE(scriptPubKey);
}
#endif
std::string ToString() const {
#ifndef TINY_NOUTILSTRENC
return "txout(value=" + coin_str(value) + ", scriptPubKey=" + HexStr(scriptPubKey).substr(0, 30) + ")";
#else
return "txout(value=" + coin_str(value) + ")";
#endif
}
};
/**
* Basic transaction serialization format:
* - int32_t nVersion
* - std::vector<CTxIn> vin
* - std::vector<CTxOut> vout
* - uint32_t nLockTime
*
* Extended transaction serialization format:
* - int32_t nVersion
* - unsigned char dummy = 0x00
* - unsigned char flags (!= 0)
* - std::vector<CTxIn> vin
* - std::vector<CTxOut> vout
* - if (flags & 1):
* - CTxWitness wit;
* - uint32_t nLockTime
*/
struct tx {
std::vector<txin> vin;
std::vector<txout> vout;
int32_t version;
uint32_t locktime;
uint256 hash;
tx() : vin(), vout(), version(2), locktime(0), hash() {}
friend bool operator==(const tx& a, const tx& b)
{
return a.hash == b.hash;
}
bool verify(const tx& o) const {
return vin == o.vin
&& vout == o.vout
&& version == o.version
&& locktime == o.locktime
&& hash == o.hash;
}
#ifndef TINY_NOHASH
void UpdateHash() {
hash = SerializeHash(*this, SER_GETHASH, SERIALIZE_TRANSACTION_NO_WITNESS);
}
#endif
bool HasWitness() const
{
for (size_t i = 0; i < vin.size(); i++) {
if (vin[i].scriptWit.size()) {
return true;
}
}
return false;
}
bool IsCoinBase() const
{
return (vin.size() == 1 && vin[0].prevout.IsNull());
}
#ifndef TINY_NOSERIALIZE
template<typename Stream>
inline void Unserialize(Stream& s) {
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
s >> version;
unsigned char flags = 0;
vin.clear();
vout.clear();
/* Try to read the vin. In case the dummy is there, this will be read as an empty vector. */
s >> vin;
if (vin.size() == 0 && fAllowWitness) {
/* We read a dummy or an empty vin. */
s >> flags;
if (flags != 0) {
s >> vin;
s >> vout;
}
} else {
/* We read a non-empty vin. Assume a normal vout follows. */
s >> vout;
}
if ((flags & 1) && fAllowWitness) {
/* The witness flag is present, and we support witnesses. */
flags ^= 1;
for (size_t i = 0; i < vin.size(); i++) {
s >> vin[i].scriptWit;
}
}
if (flags) {
/* Unknown flag in the serialization */
throw std::ios_base::failure("Unknown transaction optional data");
}
s >> locktime;
#ifndef TINY_NOHASH
UpdateHash();
#endif
}
template<typename Stream>
inline void Serialize(Stream& s) const {
const bool fAllowWitness = !(s.GetVersion() & SERIALIZE_TRANSACTION_NO_WITNESS);
s << version;
unsigned char flags = 0;
// Consistency check
if (fAllowWitness) {
/* Check whether witnesses need to be serialized. */
if (HasWitness()) {
flags |= 1;
}
}
if (flags) {
/* Use extended format in case witnesses are to be serialized. */
std::vector<txin> vinDummy;
s << vinDummy;
s << flags;
}
s << vin;
s << vout;
if (flags & 1) {
for (size_t i = 0; i < vin.size(); i++) {
s << vin[i].scriptWit;
}
}
s << locktime;
}
std::string ToString() const {
std::string str;
str += "tx(hash=" + hash.ToString().substr(0,10) + ", ver=" + std::to_string(version) + ", vin.size=" + std::to_string(vin.size()) + ", vout.size=" + std::to_string(vout.size()) + ", locktime=" + std::to_string(locktime) + ")\n";
for (const auto& tx_in : vin)
str += " " + tx_in.ToString() + "\n";
#ifndef TINY_NOUTILSTRENC
for (const auto& tx_in : vin)
str += " " + tx_in.scriptWitString() + "\n";
#endif
for (const auto& tx_out : vout)
str += " " + tx_out.ToString() + "\n";
return str;
}
inline int64_t GetWeight() const {
#define WITNESS_SCALE_FACTOR 4
return GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * (WITNESS_SCALE_FACTOR - 1) + GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION);
}
#endif
};
}
#endif // BITCOIN_TINYTX_H