-
Notifications
You must be signed in to change notification settings - Fork 173
/
transactionrecord.cpp
executable file
·511 lines (453 loc) · 18.6 KB
/
transactionrecord.cpp
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "transactionrecord.h"
#include "wallet/wallet.h"
#include "base58.h"
#include <QObject>
/* Return positive answer if transaction should be shown in list. */
bool TransactionRecord::showTransaction(const CWalletTx &wtx, bool datetime_limit_flag, const int64_t &datetime_limit)
{
// Do not show transactions earlier than the datetime_limit if the flag is set.
if (datetime_limit_flag && (int64_t) wtx.nTime < datetime_limit)
{
return false;
}
if (wtx.IsCoinStake() && !wtx.IsInMainChain())
{
// Show stale (orphaned) staking transactions if requested:
return gArgs.GetBoolArg("-showorphans", false);
}
if (wtx.IsCoinBase())
{
// Ensures we show generated coins / mined transactions at depth 1
if (!wtx.IsInMainChain())
{
return false;
}
}
// Suppress OP_RETURN transactions if they did not originate from you.
// This is not "very" taxing but necessary since the transaction is in the wallet already.
// We only do this for older transactions, because this legacy error does not occur
// anymore, and we can't filter entire transactions that have OP_RETURNs, since
// some outputs are relevant with the new contract types, such as messages.
//
// The selected timestamp represents 2018-11-12, a date that shortly follows
// the next mandatory release (v4.0.0) hard-fork after the fix for OP_RETURN
// filtering merged. No wallet databases should contain transactions for any
// !IsFromMe() OP_RETURN outputs created after that time.
//
if (wtx.nTime < 1542000000 && !wtx.IsFromMe())
{
for (auto const& txout : wtx.vout)
{
if (txout.scriptPubKey == (CScript() << OP_RETURN))
return false;
}
}
return true;
}
/*
* Decompose CWallet transaction to model transaction records.
*/
QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *wallet, const CWalletTx &wtx)
{
QList<TransactionRecord> parts;
int64_t nTime = wtx.GetTxTime();
int64_t nCredit = wtx.GetCredit(true);
int64_t nDebit = wtx.GetDebit();
int64_t nNet = nCredit - nDebit;
size_t wtx_size = wtx.vout.size();
uint256 hash = wtx.GetHash();
std::map<std::string, std::string> mapValue = wtx.mapValue;
bool fContractPresent = false;
// Initialize to unknown to prevent a possible uninitialized warning.
GRC::ContractType ContractType = GRC::ContractType::UNKNOWN;
if (!wtx.GetContracts().empty())
{
const auto& contract = wtx.GetContracts().begin();
fContractPresent = true;
ContractType = contract->m_type.Value();
}
// This is legacy CoinBase for PoW, no longer used.
if (wtx.IsCoinBase())
{
for (const auto& txout :wtx.vout)
{
if (wallet->IsMine(txout) != ISMINE_NO)
{
TransactionRecord sub(hash, nTime);
CTxDestination address;
sub.idx = parts.size(); // sequence number
if (ExtractDestination(txout.scriptPubKey, address))
{
sub.address = CBitcoinAddress(address).ToString();
}
// Generated (proof-of-work)
sub.type = TransactionRecord::Generated;
sub.credit = txout.nValue;
parts.append(sub);
}
}
}
// Since we are now separating out the sent sidestake info
// into a separate subtransaction, we need to include the entire
// value of the coinstake transaction here, rather than the previous
// counting of only IsMine outputs.
else if (wtx.IsCoinStake())
{
// We check the first coinstake output (zero is empty) for IsMine to
// determine how to characterize the entire coinstake transaction. The
// sidestakes to other (not mine) addresses are accounted for as negatives
// in a separate subtransaction. The first output is ALWAYS guaranteed to be
// the stake return to the original owner, and so matches the input.
if (wallet->IsMine(wtx.vout[1]) != ISMINE_NO)
{
TransactionRecord sub(hash, nTime);
CTxDestination address;
sub.idx = parts.size();
sub.vout = 1;
sub.type = TransactionRecord::Generated;
// The coinstake HAS to be from an address.
if(ExtractDestination(wtx.vout[1].scriptPubKey, address))
{
sub.address = CBitcoinAddress(address).ToString();
}
// Here we add up all of the outputs, whether they are ours (the stake return with
// apportioned reward, or not (sidestake), because the part that is not ours
// will be accounted in the separated sidestake send transaction.
sub.credit = 0;
for (const auto& txout : wtx.vout)
{
sub.credit += txout.nValue;
}
sub.debit = -nDebit;
// Append the subtransaction to the parts QList (transaction record).
parts.append(sub);
}
// We only want outputs > 1 because the zeroth output is always empty,
// and the first output is always the staker's. Output 2 onwards may or
// may not be a sidestake, depending on whether stakesplitting is active,
// or whether sidestaking is even turned on.
// There is no coalescing here. A separate subtransaction is created for each
// sidestake.
for (unsigned int t = 2; t < wtx_size; t++)
{
// If not a stake split and a self sidestake, then we need to net the self-sidestake
// from the gross stake entry above. The first condition below guarantees that
// parts[0] will be "my" gross stake and exists because it matches the condition
// above, the second guarantees that the output t is a sidestake back to me. The
// actual self-sidestake entry itself is handled below.
if (wtx.vout[t].scriptPubKey != wtx.vout[1].scriptPubKey
&& wallet->IsMine(wtx.vout[1]) != ISMINE_NO
&& wallet->IsMine(wtx.vout[t]) != ISMINE_NO) {
parts[0].debit += -wtx.vout[t].nValue;
}
// If this is not a stake split AND either vout[1] is mine OR
// vout[t] is mine
if (wtx.vout[t].scriptPubKey != wtx.vout[1].scriptPubKey &&
(wallet->IsMine(wtx.vout[1]) != ISMINE_NO ||
wallet->IsMine(wtx.vout[t]) != ISMINE_NO))
{
TransactionRecord sub(hash, nTime);
CTxDestination address;
sub.idx = parts.size(); // sequence number
sub.vout = t;
sub.type = TransactionRecord::Generated;
if (ExtractDestination(wtx.vout[t].scriptPubKey, address))
{
sub.address = CBitcoinAddress(address).ToString();
}
int64_t nValue = wtx.vout[t].nValue;
if (wallet->IsMine(wtx.vout[t]) != ISMINE_NO)
{
sub.credit = nValue;
}
else
{
sub.debit = -nValue;
}
parts.append(sub);
}
}
}
else if (nNet > 0)
{
for (const auto& txout : wtx.vout)
{
if (wallet->IsMine(txout) != ISMINE_NO)
{
TransactionRecord sub(hash, nTime);
CTxDestination address;
sub.idx = parts.size(); // sequence number
if (ExtractDestination(txout.scriptPubKey, address))
{
// Received by Bitcoin Address
sub.type = TransactionRecord::RecvWithAddress;
sub.address = CBitcoinAddress(address).ToString();
}
else
{
// Received by IP connection (deprecated features), or a multisignature or other non-simple transaction
sub.type = TransactionRecord::RecvFromOther;
sub.address = mapValue["from"];
}
if (fContractPresent && ContractType == GRC::ContractType::MESSAGE)
{
sub.type = TransactionRecord::Message;
}
sub.credit = txout.nValue;
parts.append(sub);
}
}
}
else // Everything else
{
bool fAllFromMe = true;
for (auto const& txin : wtx.vin)
{
fAllFromMe = fAllFromMe && (wallet->IsMine(txin) != ISMINE_NO);
// Once false, no point in continuing.
if (!fAllFromMe) break;
}
bool fAllToMe = true;
for (auto const& txout : wtx.vout)
{
fAllToMe = fAllToMe && (wallet->IsMine(txout) != ISMINE_NO);
// Once false, no point in continuing.
if (!fAllToMe) break;
}
if (fAllFromMe && fAllToMe)
{
// Payment to self
int64_t nChange = wtx.GetChange();
parts.append(TransactionRecord(hash, nTime, TransactionRecord::SendToSelf, "",
-(nDebit - nChange), nCredit - nChange, 0));
}
else if (fAllFromMe)
{
//
// Debit
//
int64_t nTxFee = nDebit - wtx.GetValueOut();
// for tracking message type display
bool fMessageDisplayed = false;
for (unsigned int nOut = 0; nOut < wtx.vout.size(); nOut++)
{
const CTxOut& txout = wtx.vout[nOut];
TransactionRecord sub(hash, nTime);
sub.idx = parts.size();
if(wallet->IsMine(txout) != ISMINE_NO)
{
// Ignore parts sent to self, as this is usually the change
// from a transaction sent back to our own address.
continue;
}
CTxDestination address;
if (ExtractDestination(txout.scriptPubKey, address))
{
// Sent to Bitcoin Address
sub.type = TransactionRecord::SendToAddress;
sub.address = CBitcoinAddress(address).ToString();
}
else
{
// Sent to IP, or other non-address transaction like OP_EVAL
sub.type = TransactionRecord::SendToOther;
sub.address = mapValue["to"];
}
int64_t nValue = txout.nValue;
/* Add fee to first output */
if (nTxFee > 0)
{
nValue += nTxFee;
nTxFee = 0;
}
sub.debit = -nValue;
// Determine if the transaction is a beacon advertisement or a vote.
// For right now, there should only be one contract in a transaction.
// We will simply select the first and only one. Note that we are
// looping through the outputs one by one in the for loop above this,
// So if we get here, we are not a coinbase or coinstake, and we are on
// an output that isn't ours. The worst that can happen from this
// simple approach is to label more than one output with the
// first found contract type. For right now, this is sufficient, because
// the contracts that are sent right now only contain two outputs,
// the burn and the change. We will have to get more sophisticated
// when we allow more than one contract per transaction.
// Notice this doesn't mess with the value or debit, it simply
// overrides the TransactionRecord enum type.
if (fContractPresent)
{
switch (ContractType)
{
case GRC::ContractType::BEACON:
sub.type = TransactionRecord::BeaconAdvertisement;
break;
case GRC::ContractType::POLL:
sub.type = TransactionRecord::Poll;
break;
case GRC::ContractType::VOTE:
sub.type = TransactionRecord::Vote;
break;
case GRC::ContractType::MRC:
sub.type = TransactionRecord::MRC;
break;
case GRC::ContractType::MESSAGE:
// Only display the message type for the first not is mine output
if (!fMessageDisplayed && wallet->IsMine(txout) == ISMINE_NO)
{
sub.type = TransactionRecord::Message;
fMessageDisplayed = true;
}
// Do not display the op return output for a send message contract separately.
else if (txout.scriptPubKey[0] == OP_RETURN)
{
continue;
}
break;
default:
break; // Suppress warning
}
}
parts.append(sub);
}
}
else
{
//
// Mixed debit transaction, can't break down payees
//
parts.append(TransactionRecord(hash, nTime, TransactionRecord::Other, "", nNet, 0, 0));
}
}
return parts;
}
QString TransactionRecord::TypeToString() const
{
return TypeToString(type);
}
QString TransactionRecord::TypeToString(const Type& type, const bool& translated)
{
if (translated) {
switch(type) {
case Other: return QObject::tr("Other");
case Generated: return QObject::tr("Mined");
case SendToAddress: return QObject::tr("Sent to Address");
case SendToOther: return QObject::tr("Sent to Other");
case RecvWithAddress: return QObject::tr("Received with Address");
case RecvFromOther: return QObject::tr("Received from Other");
case SendToSelf: return QObject::tr("Self");
case BeaconAdvertisement: return QObject::tr("Beacon Advertisements");
case Poll: return QObject::tr("Polls");
case Vote: return QObject::tr("Votes");
case Message: return QObject::tr("Messages");
case MRC: return QObject::tr("MRCs");
}
assert(false); // Suppress warning
} else {
switch(type) {
case Other: return "Other";
case Generated: return "Mined";
case SendToAddress: return "Sent to Address";
case SendToOther: return "Sent to Other";
case RecvWithAddress: return "Received with Address";
case RecvFromOther: return "Received from Other";
case SendToSelf: return "Self";
case BeaconAdvertisement: return "Beacon Advertisements";
case Poll: return "Polls";
case Vote: return "Votes";
case Message: return "Messages";
case MRC: return "MRCs";
}
assert(false); // Suppress warning
}
// This will never be reached. Put it in anyway to prevent control reaches end of non-void function warning
// from some compiler versions.
return QString{};
}
void TransactionRecord::updateStatus(const CWalletTx &wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
// Determine transaction status
// Find the block the tx is in
CBlockIndex* pindex = nullptr;
BlockMap::iterator mi = mapBlockIndex.find(wtx.hashBlock);
if (mi != mapBlockIndex.end())
pindex = mi->second;
// Sort order, unrecorded transactions sort to the top
status.sortKey = strprintf("%010d-%01d-%010u-%03d",
(pindex ? pindex->nHeight : std::numeric_limits<int>::max()),
(wtx.IsCoinBase() ? 1 : 0),
wtx.nTimeReceived,
idx);
status.countsForBalance = wtx.IsTrusted() && !(wtx.GetBlocksToMaturity() > 0);
status.depth = wtx.GetDepthInMainChain();
status.cur_num_blocks = nBestHeight;
if (!IsFinalTx(wtx, nBestHeight + 1))
{
if (wtx.nLockTime < LOCKTIME_THRESHOLD)
{
status.status = TransactionStatus::OpenUntilBlock;
status.open_for = wtx.nLockTime - nBestHeight;
}
else
{
status.status = TransactionStatus::OpenUntilDate;
status.open_for = wtx.nLockTime;
}
}
// For generated transactions, determine maturity
else if(type == TransactionRecord::Generated)
{
if (wtx.GetBlocksToMaturity() > 0)
{
status.status = TransactionStatus::Immature;
if (wtx.IsInMainChain())
{
status.matures_in = wtx.GetBlocksToMaturity();
// Check if the block was requested by anyone
if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
status.status = TransactionStatus::MaturesWarning;
}
else
{
status.status = TransactionStatus::NotAccepted;
}
}
else
{
status.status = TransactionStatus::Confirmed;
}
status.generated_type = wtx.GetGeneratedType(vout);
}
else
{
if (status.depth < 0)
{
status.status = TransactionStatus::Conflicted;
}
else if (GetAdjustedTime() - wtx.nTimeReceived > 2 * 60 && wtx.GetRequestCount() == 0)
{
status.status = TransactionStatus::Offline;
}
else if (status.depth == 0)
{
status.status = TransactionStatus::Unconfirmed;
}
else if (status.depth < RecommendedNumConfirmations)
{
status.status = TransactionStatus::Confirming;
}
else
{
status.status = TransactionStatus::Confirmed;
}
}
}
bool TransactionRecord::statusUpdateNeeded() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
AssertLockHeld(cs_main);
return status.cur_num_blocks != nBestHeight;
}
std::string TransactionRecord::getTxID()
{
return hash.ToString();
}