forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedgerEntry.cpp
More file actions
377 lines (312 loc) · 11.2 KB
/
LedgerEntry.cpp
File metadata and controls
377 lines (312 loc) · 11.2 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
////////////////////////////////////////////////////////////////////////////////
// //
// 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 //
// //
////////////////////////////////////////////////////////////////////////////////
#include "LedgerEntry.h"
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// LedgerEntry
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
LedgerEntry LedgerEntry::EmptyLedger_;
map<BinaryData, LedgerEntry> LedgerEntry::EmptyLedgerMap_;
BinaryData LedgerEntry::EmptyID_ = BinaryData(0);
////////////////////////////////////////////////////////////////////////////////
BinaryData const & LedgerEntry::getScrAddr(void) const
{
if (ID_.getSize() == 21) return ID_;
return EmptyID_;
}
////////////////////////////////////////////////////////////////////////////////
string LedgerEntry::getWalletID(void) const
{
if (ID_.getSize() != 21) return ID_.toBinStr();
return string();
}
////////////////////////////////////////////////////////////////////////////////
void LedgerEntry::setScrAddr(BinaryData const & bd)
{
if(bd.getSize() == 21)
ID_ = bd;
}
////////////////////////////////////////////////////////////////////////////////
void LedgerEntry::setWalletID(BinaryData const & bd)
{
if (bd.getSize() != 21)
ID_ = bd;
}
////////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::operator<(LedgerEntry const & le2) const
{
if( blockNum_ != le2.blockNum_)
return blockNum_ < le2.blockNum_;
else if( index_ != le2.index_)
return index_ < le2.index_;
else
return false;
}
//////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::operator==(LedgerEntry const & le2) const
{
//TODO
//return (blockNum_ == le2.blockNum_ &&
//index_ == le2.index_ &&
//txTime_ == le2.txTime_);
return (blockNum_ == le2.blockNum_ && index_ == le2.index_);
}
//////////////////////////////////////////////////////////////////////////////
void LedgerEntry::pprint(void)
{
cout << "LedgerEntry: " << endl;
cout << " ScrAddr : " << getScrAddr().copySwapEndian().toHexStr() << endl;
cout << " Value : " << getValue()/1e8 << endl;
cout << " BlkNum : " << getBlockNum() << endl;
cout << " TxHash : " << getTxHash().copySwapEndian().toHexStr() << endl;
cout << " TxIndex : " << getIndex() << endl;
cout << " Coinbase: " << (isCoinbase() ? 1 : 0) << endl;
cout << " sentSelf: " << (isSentToSelf() ? 1 : 0) << endl;
cout << " isChange: " << (isChangeBack() ? 1 : 0) << endl;
cout << " isOptInRBF: " << (isOptInRBF() ? 1 : 0) << endl;
cout << endl;
}
//////////////////////////////////////////////////////////////////////////////
void LedgerEntry::pprintOneLine(void) const
{
printf(" Addr:%s Tx:%s:%02d BTC:%0.3f Blk:%06d\n",
" ",
getTxHash().getSliceCopy(0,8).toHexStr().c_str(),
getIndex(),
getValue()/1e8,
getBlockNum());
}
//////////////////////////////////////////////////////////////////////////////
bool LedgerEntry::operator>(LedgerEntry const & le2) const
{
if (blockNum_ != le2.blockNum_)
return blockNum_ > le2.blockNum_;
else if (index_ != le2.index_)
return index_ > le2.index_;
else
return false;
}
//////////////////////////////////////////////////////////////////////////////
void LedgerEntry::purgeLedgerMapFromHeight(
map<BinaryData, LedgerEntry>& leMap,
uint32_t purgeFrom)
{
//Remove all entries starting this height, included.
BinaryData cutOffHeight(6);
auto heightPtr = cutOffHeight.getPtr();
uint8_t* purgeFromPtr = reinterpret_cast<uint8_t*>(&purgeFrom);
memset(heightPtr, 0, 6);
heightPtr[0] = purgeFromPtr[2];
heightPtr[1] = purgeFromPtr[1];
heightPtr[2] = purgeFromPtr[0];
auto cutOffIterPair = leMap.equal_range(cutOffHeight);
leMap.erase(cutOffIterPair.first, leMap.end());
}
//////////////////////////////////////////////////////////////////////////////
void LedgerEntry::purgeLedgerVectorFromHeight(
vector<LedgerEntry>& leVec,
uint32_t purgeFrom)
{
//Remove all entries starting this height, included.
uint32_t i = 0;
sort(leVec.begin(), leVec.end());
for (const auto& le : leVec)
{
if (le.getBlockNum() >= purgeFrom)
break;
i++;
}
leVec.erase(leVec.begin() +i, leVec.end());
}
//////////////////////////////////////////////////////////////////////////////
map<BinaryData, LedgerEntry> LedgerEntry::computeLedgerMap(
const map<BinaryData, TxIOPair>& txioMap,
uint32_t startBlock, uint32_t endBlock, const BinaryDataRef ID,
const LMDBBlockDatabase* db, const Blockchain* bc,
const ZeroConfContainer* zc)
{
map<BinaryData, LedgerEntry> leMap;
//arrange txios by transaction
map<BinaryData, deque<const TxIOPair*>> TxnTxIOMap;
for (const auto& txio : txioMap)
{
auto&& txOutDBKey = txio.second.getDBKeyOfOutput().getSliceCopy(0, 6);
auto& txioVec = TxnTxIOMap[txOutDBKey];
txioVec.push_back(&txio.second);
if (txio.second.hasTxIn())
{
auto txInDBKey = txio.second.getDBKeyOfInput().getSliceCopy(0, 6);
auto& _txioVec = TxnTxIOMap[txInDBKey];
_txioVec.push_back(&txio.second);
}
}
//convert TxIO to ledgers
for (const auto& txioVec : TxnTxIOMap)
{
//reset ledger variables
BinaryData txHash;
uint32_t blockNum;
uint32_t txTime;
uint16_t txIndex;
set<BinaryData> scrAddrSet;
bool isRBF = false;
bool usesWitness = false;
bool isChained = false;
//grab iterator
auto txioIter = txioVec.second.cbegin();
//get txhash, block, txIndex and txtime
if (!txioVec.first.startsWith(DBUtils::ZeroConfHeader_))
{
blockNum = DBUtils::hgtxToHeight(txioVec.first.getSliceRef(0, 4));
txIndex = READ_UINT16_BE(txioVec.first.getSliceRef(4, 2));
txTime = bc->getHeaderByHeight(blockNum)->getTimestamp();
txHash = db->getTxHashForLdbKey(txioVec.first);
}
else
{
blockNum = UINT32_MAX;
txIndex = READ_UINT16_BE(txioVec.first.getSliceRef(4, 2));
txTime = (*txioIter)->getTxTime();
txHash = zc->getHashForKey(txioVec.first);
}
if (blockNum < startBlock || blockNum > endBlock)
continue;
bool isCoinbase=false;
int64_t value=0;
int64_t valIn=0, valOut=0;
uint32_t nTxInAreOurs = 0, nTxOutAreOurs = 0;
while (txioIter != txioVec.second.cend())
{
if (blockNum == UINT32_MAX)
{
if ((*txioIter)->isRBF())
isRBF = true;
if ((*txioIter)->getTxTime() > txTime)
txTime = (*txioIter)->getTxTime();
}
if ((*txioIter)->getDBKeyOfOutput().startsWith(txioVec.first))
{
isCoinbase |= (*txioIter)->isFromCoinbase();
valIn += (*txioIter)->getValue();
value += (*txioIter)->getValue();
nTxOutAreOurs++;
}
if ((*txioIter)->getDBKeyOfInput().startsWith(txioVec.first))
{
valOut -= (*txioIter)->getValue();
value -= (*txioIter)->getValue();
nTxInAreOurs++;
if ((*txioIter)->isChainedZC())
isChained = true;
}
scrAddrSet.insert((*txioIter)->getScrAddr());
++txioIter;
}
bool isSentToSelf = false;
bool isChangeBack = false;
if (nTxInAreOurs * nTxOutAreOurs > 0)
{
//if some of the txins AND some of the txouts are ours, this could be an STS
//pull the txn and compare the txin and txout counts
uint32_t nTxOutInTx = UINT32_MAX;
if (!txioVec.first.startsWith(DBUtils::ZeroConfHeader_))
{
nTxOutInTx = db->getStxoCountForTx(txioVec.first.getSliceRef(0, 6));
}
else
{
auto ptx = zc->getTxByKey(txioVec.first);
nTxOutInTx = ptx->outputs_.size();
}
if (nTxOutInTx == nTxOutAreOurs)
{
value = valIn;
isSentToSelf = true;
}
}
else if (nTxInAreOurs != 0 && (valIn + valOut) < 0)
isChangeBack = true;
LedgerEntry le(ID,
value,
blockNum,
txHash,
txIndex,
txTime,
isCoinbase,
isSentToSelf,
isChangeBack,
isRBF,
usesWitness,
isChained);
/*
When signing a tx online, the wallet knows the txhash, therefor it can register all
comments on outgoing addresses under the txhash.
When the tx is signed offline, there is no guarantee that the txhash will be known
when the offline tx is crafted. Therefor the comments for each outgoing address are
registered under that address only.
In order for the GUI to be able to resolve outgoing address comments, the ledger entry
needs to carry those for pay out transactions
*/
if (value < 0)
{
try
{
//grab tx by hash
auto&& payout_tx = db->getFullTxCopy(txioVec.first);
//get scrAddr for each txout
for (unsigned i=0; i < payout_tx.getNumTxOut(); i++)
{
auto&& txout = payout_tx.getTxOutCopy(i);
scrAddrSet.insert(txout.getScrAddressStr());
}
}
catch (exception&)
{
auto ptx = zc->getTxByKey(txioVec.first);
if (ptx == nullptr)
{
LOGWARN << "failed to get tx for ledger parsing";
}
else
{
for (auto& txout : ptx->outputs_)
scrAddrSet.insert(txout.scrAddr_);
}
}
}
le.scrAddrSet_ = move(scrAddrSet);
leMap[txioVec.first] = le;
}
return leMap;
}
////////////////////////////////////////////////////////////////////////////////
void LedgerEntry::fillMessage(::Codec_LedgerEntry::LedgerEntry* msg)
{
if (msg == nullptr)
{
LOGERR << "empty ledger msg";
return;
}
msg->set_id(ID_.getPtr(), ID_.getSize());
msg->set_balance(value_);
msg->set_txheight(blockNum_);
msg->set_txhash(txHash_.getPtr(), txHash_.getSize());
msg->set_index(index_);
msg->set_txtime(txTime_);
msg->set_iscoinbase(isCoinbase_);
msg->set_issts(isSentToSelf_);
msg->set_ischangeback(isChangeBack_);
msg->set_optinrbf(isOptInRBF_);
msg->set_ischainedzc(isChainedZC_);
msg->set_iswitness(usesWitness_);
for (auto& scrAddr : scrAddrSet_)
msg->add_scraddr(scrAddr.getPtr(), scrAddr.getSize());
}