forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtxio.cpp
More file actions
379 lines (323 loc) · 11.1 KB
/
txio.cpp
File metadata and controls
379 lines (323 loc) · 11.1 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2015, Armory Technologies, Inc. //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE-ATI or http://www.gnu.org/licenses/agpl.html //
// //
// //
// Copyright (C) 2016, goatpig //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#include "txio.h"
//////////////////////////////////////////////////////////////////////////////
TxIOPair::TxIOPair(void) :
amount_(0),
indexOfOutput_(0),
indexOfInput_(0),
isTxOutFromSelf_(false),
isFromCoinbase_(false),
isMultisig_(false),
txtime_(0),
isUTXO_(false)
{}
//////////////////////////////////////////////////////////////////////////////
TxIOPair::TxIOPair(uint64_t amount) :
amount_(amount),
indexOfOutput_(0),
indexOfInput_(0),
isTxOutFromSelf_(false),
isFromCoinbase_(false),
isMultisig_(false),
txtime_(0),
isUTXO_(false)
{}
//////////////////////////////////////////////////////////////////////////////
TxIOPair::TxIOPair(TxRef txPtrO, uint32_t txoutIndex) :
amount_(0),
indexOfInput_(0),
isTxOutFromSelf_(false),
isFromCoinbase_(false),
isMultisig_(false),
txtime_(0),
isUTXO_(false)
{
setTxOut(txPtrO, txoutIndex);
}
//////////////////////////////////////////////////////////////////////////////
TxIOPair::TxIOPair(TxRef txPtrO,
uint32_t txoutIndex,
TxRef txPtrI,
uint32_t txinIndex) :
amount_(0),
isTxOutFromSelf_(false),
isFromCoinbase_(false),
isMultisig_(false),
txtime_(0),
isUTXO_(false)
{
setTxOut(txPtrO, txoutIndex);
setTxIn(txPtrI, txinIndex);
}
//////////////////////////////////////////////////////////////////////////////
TxIOPair::TxIOPair(const BinaryData& txOutKey8B, uint64_t val) :
amount_(val),
indexOfOutput_(0),
indexOfInput_(0),
isTxOutFromSelf_(false),
isFromCoinbase_(false),
isMultisig_(false),
txtime_(0),
isUTXO_(false)
{
setTxOut(txOutKey8B);
}
//////////////////////////////////////////////////////////////////////////////
HashString TxIOPair::getTxHashOfOutput(const LMDBBlockDatabase *db) const
{
if (!hasTxOut())
return BtcUtils::EmptyHash();
else if (txHashOfOutput_.getSize() == 32)
return txHashOfOutput_;
else if (txRefOfOutput_.isInitialized() && db != nullptr)
{
DBTxRef dbTxRef(txRefOfOutput_, db);
txHashOfOutput_ = dbTxRef.getThisHash();
return txHashOfOutput_;
}
return BinaryData(0);
}
//////////////////////////////////////////////////////////////////////////////
HashString TxIOPair::getTxHashOfInput(const LMDBBlockDatabase *db) const
{
if (!hasTxIn())
return BtcUtils::EmptyHash();
else if (txHashOfInput_.getSize() == 32)
return txHashOfInput_;
else if (txRefOfInput_.isInitialized() && db != nullptr)
{
DBTxRef dbTxRef(txRefOfInput_, db);
txHashOfInput_ = dbTxRef.getThisHash();
return txHashOfInput_;
}
return BinaryData(0);
}
//////////////////////////////////////////////////////////////////////////////
TxOut TxIOPair::getTxOutCopy(LMDBBlockDatabase *db) const
{
// I actually want this to segfault when there is no TxOut...
// we should't ever be trying to access it without checking it
// first in the calling code (hasTxOut/hasTxOutZC)
if (hasTxOut())
{
DBTxRef dbTxRef(txRefOfOutput_, db);
return dbTxRef.getTxOutCopy(indexOfOutput_);
}
throw runtime_error("Has not TxOutCopy");
}
//////////////////////////////////////////////////////////////////////////////
TxIn TxIOPair::getTxInCopy(LMDBBlockDatabase *db) const
{
// I actually want this to segfault when there is no TxIn...
// we should't ever be trying to access it without checking it
// first in the calling code (hasTxIn/hasTxInZC)
if (hasTxIn())
{
DBTxRef dbTxRef(txRefOfInput_, db);
return dbTxRef.getTxInCopy(indexOfInput_);
}
throw runtime_error("Has not TxInCopy");
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::setTxIn(TxRef txref, uint32_t index)
{
txRefOfInput_ = txref;
indexOfInput_ = index;
return true;
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::setTxIn(const BinaryData& dbKey8B)
{
if (dbKey8B.getSize() == 8)
{
BinaryRefReader brr(dbKey8B);
BinaryDataRef txKey6B = brr.get_BinaryDataRef(6);
uint16_t txInIdx = brr.get_uint16_t(BE);
return setTxIn(TxRef(txKey6B), (uint32_t)txInIdx);
}
else
{
//pass a 0 byte dbkey to reset the txin
setTxIn(TxRef(), 0);
return false;
}
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::setTxOut(const BinaryData& dbKey8B)
{
if (dbKey8B.getSize() == 8)
{
BinaryRefReader brr(dbKey8B);
BinaryDataRef txKey6B = brr.get_BinaryDataRef(6);
uint16_t txOutIdx = brr.get_uint16_t(BE);
return setTxOut(TxRef(txKey6B), (uint32_t)txOutIdx);
}
else
{
//pass 0 byte dbkey to reset the txout
setTxOut(TxRef(), 0);
return false;
}
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::setTxOut(TxRef txref, uint32_t index)
{
txRefOfOutput_ = txref;
indexOfOutput_ = index;
return true;
}
//////////////////////////////////////////////////////////////////////////////
pair<bool, bool> TxIOPair::reassessValidity(LMDBBlockDatabase *db)
{
pair<bool, bool> result;
result.first = hasTxOutInMain(db);
result.second = hasTxInInMain(db);
return result;
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::isSpent(LMDBBlockDatabase *db) const
{
// Not sure whether we should verify hasTxOut. It wouldn't make much
// sense to have TxIn but not TxOut, but there might be a preferred
// behavior in such awkward circumstances
return (hasTxInZC() || hasTxInInMain(db));
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::isUnspent(LMDBBlockDatabase *db) const
{
return ((hasTxOutZC() || hasTxOutInMain(db)) && !isSpent(db));
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::isSpendable(LMDBBlockDatabase *db, uint32_t currBlk, bool ignoreAllZeroConf) const
{
// Spendable TxOuts are ones with at least 1 confirmation, or zero-conf
// TxOuts that were sent-to-self. Obviously, they should be unspent, too
if (hasTxInZC() || hasTxInInMain(db))
return false;
if (hasTxOutInMain(db))
{
uint32_t nConf = currBlk - txRefOfOutput_.getBlockHeight() + 1;
if (isFromCoinbase_ && nConf <= COINBASE_MATURITY)
return false;
else
return true;
}
if (hasTxOutZC() && isTxOutFromSelf())
return !ignoreAllZeroConf;
return false;
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::isMineButUnconfirmed(
LMDBBlockDatabase *db,
uint32_t currBlk, bool inclAllZC
) const
{
// All TxOuts that were from our own transactions are always confirmed
if (isTxOutFromSelf())
return false;
DBTxRef dbTxRef(txRefOfInput_, db);
if (hasTxInZC() || (hasTxIn() && dbTxRef.isMainBranch()))
return false;
if (hasTxOutInMain(db))
{
uint32_t nConf = currBlk - txRefOfOutput_.getBlockHeight() + 1;
if (isFromCoinbase_)
return (nConf<COINBASE_MATURITY);
else
return (nConf<MIN_CONFIRMATIONS);
}
else if (hasTxOutZC() && inclAllZC)
return true;
return false;
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::hasTxOutInMain(LMDBBlockDatabase *db) const
{
DBTxRef dbTxRef(txRefOfOutput_, db);
return (!hasTxOutZC() && hasTxOut() && dbTxRef.isMainBranch());
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::hasTxInInMain(LMDBBlockDatabase *db) const
{
DBTxRef dbTxRef(txRefOfInput_, db);
return (!hasTxInZC() && hasTxIn() && dbTxRef.isMainBranch());
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::hasTxOutZC(void) const
{
return txRefOfOutput_.getDBKey().startsWith(READHEX("ffff"));
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::hasTxInZC(void) const
{
return txRefOfInput_.getDBKey().startsWith(READHEX("ffff"));
}
//////////////////////////////////////////////////////////////////////////////
void TxIOPair::pprintOneLine(LMDBBlockDatabase *db) const
{
printf(" Val:(%0.3f)\t (STS, O,I, Omb,Imb, Oz,Iz) %d %d%d %d%d %d%d\n",
(double)getValue() / 1e8,
(isTxOutFromSelf() ? 1 : 0),
(hasTxOut() ? 1 : 0),
(hasTxIn() ? 1 : 0),
(hasTxOutInMain(db) ? 1 : 0),
(hasTxInInMain(db) ? 1 : 0),
(hasTxOutZC() ? 1 : 0),
(hasTxInZC() ? 1 : 0));
}
//////////////////////////////////////////////////////////////////////////////
bool TxIOPair::operator>=(const BinaryData &dbKey) const
{
if (txRefOfOutput_ >= dbKey)
return true;
if (txRefOfInput_ >= dbKey)
return true;
return false;
}
//////////////////////////////////////////////////////////////////////////////
TxIOPair& TxIOPair::operator=(const TxIOPair &rhs)
{
this->amount_ = rhs.amount_;
this->txRefOfOutput_ = rhs.txRefOfOutput_;
this->indexOfOutput_ = rhs.indexOfOutput_;
this->txRefOfInput_ = rhs.txRefOfInput_;
this->indexOfInput_ = rhs.indexOfInput_;
this->txHashOfOutput_ = rhs.txHashOfOutput_;
this->txHashOfInput_ = rhs.txHashOfInput_;
this->isTxOutFromSelf_ = rhs.isTxOutFromSelf_;
this->isFromCoinbase_ = rhs.isFromCoinbase_;
this->isMultisig_ = rhs.isMultisig_;
this->txtime_ = rhs.txtime_;
this->isUTXO_ = rhs.isUTXO_;
this->isRBF_ = rhs.isRBF_;
return *this;
}
//////////////////////////////////////////////////////////////////////////////
TxIOPair& TxIOPair::operator=(TxIOPair&& toMove)
{
this->amount_ = toMove.amount_;
this->txRefOfOutput_ = move(toMove.txRefOfOutput_);
this->indexOfOutput_ = move(toMove.indexOfOutput_);
this->txRefOfInput_ = move(toMove.txRefOfInput_);
this->indexOfInput_ = move(toMove.indexOfInput_);
this->txHashOfOutput_ = move(toMove.txHashOfOutput_);
this->txHashOfInput_ = move(toMove.txHashOfInput_);
this->isTxOutFromSelf_ = toMove.isTxOutFromSelf_;
this->isFromCoinbase_ = toMove.isFromCoinbase_;
this->isMultisig_ = toMove.isMultisig_;
this->txtime_ = toMove.txtime_;
this->isUTXO_ = toMove.isUTXO_;
this->isRBF_ = toMove.isRBF_;
return *this;
}