-
Notifications
You must be signed in to change notification settings - Fork 16
/
UBICalculator.cpp
executable file
·82 lines (67 loc) · 3.33 KB
/
UBICalculator.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
#include "UBICalculator.h"
#include "CertStore/CertStore.h"
#include "Chain.h"
#include "PathSum/PathSum.h"
#include "Tools/Log.h"
bool UBICalculator::isAddressConnectedToADSC(AddressForStore* address) {
return !address->getDscToAddressLinks().empty();
}
UAmount UBICalculator::totalReceivedUBI(AddressForStore* addressForStore) {
Chain& chain = Chain::Instance();
return UBICalculator::totalReceivedUBI(addressForStore, chain.getCurrentBlockchainHeight());
}
UAmount UBICalculator::totalReceivedUBI(AddressForStore* addressForStore, uint32_t blockHeight) {
CertStore& certStore = CertStore::Instance();
PathSum &pathSum = PathSum::Instance();
UAmount totalAmount;
auto dscToAddressLinks = addressForStore->getDscToAddressLinks();
auto it = dscToAddressLinks.begin();
while (it != dscToAddressLinks.end()) {
uint32_t startHeight = ((DscToAddressLink)*it).getDSCLinkedAtHeight();
Cert *cert = certStore.getDscCertWithCertId(((DscToAddressLink)*it).getDscCertificate());
std::vector<std::pair<uint32_t, bool> > statusList = cert->getStatusList();
std::vector<std::pair<uint32_t, uint32_t> > newStatusList;
std::vector<std::pair<uint32_t, bool> >::const_iterator statusIterator = statusList.begin();
// only one element in statusList
if (statusIterator == statusList.end()) {
std::pair<uint32_t, uint32_t> currentPair(0, 0);
currentPair = std::pair<uint32_t, uint32_t>(statusIterator->first, blockHeight);
if (statusIterator->second) { // only if is active status
newStatusList.emplace_back(currentPair);
}
} else {
// several elements in statusList
while (statusIterator != statusList.end()) {
std::pair<uint32_t, uint32_t> currentPair(0, 0);
currentPair.first = statusIterator->first;
bool isActive = statusIterator->second;
statusIterator++;
if (statusIterator == statusList.end()) {
currentPair.second = blockHeight;
} else {
currentPair.second = statusIterator->first;
}
if (isActive) { // only if is active status
newStatusList.emplace_back(currentPair);
}
}
}
for (std::pair<uint32_t, uint32_t> pair : newStatusList) {
Log(LOG_LEVEL_INFO) << "PAIR: " << pair.first << ", " << pair.second << " START: " << startHeight;
if (startHeight > pair.second) {
//do nothing
} else if (startHeight > pair.first) {
// take pair between startHeight and pair.second
totalAmount.map[cert->getCurrencyId()] += pathSum.getSum(startHeight,
pair.second).map[cert->getCurrencyId()]; // @TODO can be performance optimized
} else if (startHeight < pair.first) {
//take entire pair
totalAmount.map[cert->getCurrencyId()] += pathSum.getSum(pair.first,
pair.second).map[cert->getCurrencyId()];
}
}
it++;
}
Log(LOG_LEVEL_INFO) << "UBI sum: " << totalAmount;
return totalAmount;
}