forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedgerEntry.cpp
More file actions
297 lines (246 loc) · 9.15 KB
/
LedgerEntry.cpp
File metadata and controls
297 lines (246 loc) · 9.15 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
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2011-2015, Armory Technologies, Inc. //
// Distributed under the GNU Affero General Public License (AGPL v3) //
// See LICENSE 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 LedgerEntry::ZCheader_ = WRITE_UINT16_BE(0xFFFF);
////////////////////////////////////////////////////////////////////////////////
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
{
// TODO: I wanted to update this with txTime_, but I didn't want to c
// complicate the mess of changes going in, yet. Do this later
// once everything is stable again.
//if( blockNum_ != le2.blockNum_)
//return blockNum_ < le2.blockNum_;
//else if( index_ != le2.index_)
//return index_ < le2.index_;
//else if( txTime_ != le2.txTime_)
//return txTime_ < le2.txTime_;
//else
//return false;
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 << 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());
}
//////////////////////////////////////////////////////////////////////////////
void LedgerEntry::computeLedgerMap(map<BinaryData, LedgerEntry> &leMap,
const map<BinaryData, TxIOPair>& txioMap,
uint32_t startBlock, uint32_t endBlock,
const BinaryData& ID,
const LMDBBlockDatabase* db,
const Blockchain* bc,
bool purge)
{
if (purge)
LedgerEntry::purgeLedgerMapFromHeight(leMap, startBlock);
//arrange txios by transaction
map<BinaryData, vector<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;
//grab iterator
auto txioIter = txioVec.second.cbegin();
//get txhash, block, txIndex and txtime
if (!txioVec.first.startsWith(ZCheader_))
{
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();
if ((*txioIter)->getDBKeyOfOutput().startsWith(txioVec.first))
txHash = (*txioIter)->getTxHashOfOutput(db);
else if ((*txioIter)->getDBKeyOfInput().startsWith(txioVec.first))
txHash = (*txioIter)->getTxHashOfInput(db);
}
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 ((*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++;
}
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 = db->getStxoCountForTx(txioVec.first.getSliceRef(0, 6));
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);
le.scrAddrSet_ = move(scrAddrSet);
leMap[txioVec.first] = le;
}
}
// kate: indent-width 3; replace-tabs on;