Skip to content

Commit b0f745f

Browse files
UI: Use struct instead of string for overview recent cache (thanks @dexX7)
1 parent 3735675 commit b0f745f

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

src/qt/overviewpage.cpp

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,28 @@ using std::string;
4949

5050
using namespace mastercore;
5151

52-
std::map<uint256, std::string> recentCache;
53-
5452
#define DECORATION_SIZE 64
5553
#define NUM_ITEMS 6 // 3 - number of recent transactions to display
5654

55+
struct OverviewCacheEntry
56+
{
57+
OverviewCacheEntry()
58+
: address("unknown"), amount("0.0000000"), valid(false), sendToSelf(false), outbound(false)
59+
{}
60+
61+
OverviewCacheEntry(const QString& addressIn, const QString& amountIn, bool validIn, bool sendToSelfIn, bool outboundIn)
62+
: address(addressIn), amount(amountIn), valid(validIn), sendToSelf(sendToSelfIn), outbound(outboundIn)
63+
{}
64+
65+
QString address;
66+
QString amount;
67+
bool valid;
68+
bool sendToSelf;
69+
bool outbound;
70+
};
71+
72+
std::map<uint256, OverviewCacheEntry> recentCache;
73+
5774
class TxViewDelegate : public QAbstractItemDelegate
5875
{
5976
Q_OBJECT
@@ -89,7 +106,7 @@ class TxViewDelegate : public QAbstractItemDelegate
89106
uint256 hash = 0;
90107
hash.SetHex(index.data(TransactionTableModel::TxIDRole).toString().toStdString());
91108
bool omniOverride = false, omniSendToSelf = false, valid = false, omniOutbound = true;
92-
std::string omniAmountStr;
109+
QString omniAmountStr;
93110

94111
// check pending
95112
PendingMap::iterator it = my_pending.find(hash);
@@ -99,29 +116,23 @@ class TxViewDelegate : public QAbstractItemDelegate
99116
CMPPending *p_pending = &(it->second);
100117
address = QString::fromStdString(p_pending->src);
101118
if (isPropertyDivisible(p_pending->prop)) {
102-
omniAmountStr = FormatDivisibleShortMP(p_pending->amount) + getTokenLabel(p_pending->prop);
119+
omniAmountStr = QString::fromStdString(FormatDivisibleShortMP(p_pending->amount) + getTokenLabel(p_pending->prop));
103120
} else {
104-
omniAmountStr = FormatIndivisibleMP(p_pending->amount) + getTokenLabel(p_pending->prop);
121+
omniAmountStr = QString::fromStdString(FormatIndivisibleMP(p_pending->amount) + getTokenLabel(p_pending->prop));
105122
}
106123
}
107124

108125
// check cache (avoid reparsing the same transactions repeatedly over and over on repaint)
109-
std::map<uint256, std::string>::iterator cacheIt = recentCache.find(hash);
126+
std::map<uint256, OverviewCacheEntry>::iterator cacheIt = recentCache.find(hash);
110127
if (cacheIt != recentCache.end()) {
111-
std::string txData = cacheIt->second;
112-
std::vector<std::string> vecData;
113-
boost::split(vecData, txData, boost::is_any_of(":"), boost::token_compress_on);
114-
if (vecData.size() == 5) {
115-
address = QString::fromStdString(vecData[0]);
116-
valid = boost::lexical_cast<bool>(vecData[1]);
117-
omniSendToSelf = boost::lexical_cast<bool>(vecData[2]);
118-
omniOutbound = boost::lexical_cast<bool>(vecData[3]);
119-
omniAmountStr = vecData[4];
120-
omniOverride = true;
121-
amount = 0;
122-
} else {
123-
PrintToLog("ERROR: Recent transactions cache has an invalid number of tokens for entry %s\n", hash.GetHex());
124-
}
128+
OverviewCacheEntry txEntry = cacheIt->second;
129+
address = txEntry.address;
130+
valid = txEntry.valid;
131+
omniSendToSelf = txEntry.sendToSelf;
132+
omniOutbound = txEntry.outbound;
133+
omniAmountStr = txEntry.amount;
134+
omniOverride = true;
135+
amount = 0;
125136
} else { // cache miss, check database
126137
if (p_txlistdb->exists(hash)) {
127138
omniOverride = true;
@@ -152,17 +163,17 @@ class TxViewDelegate : public QAbstractItemDelegate
152163
address = QString::fromStdString(tmpBuyer);
153164
omniOutbound = false;
154165
}
155-
omniAmountStr = FormatDivisibleMP(total);
166+
omniAmountStr = QString::fromStdString(FormatDivisibleMP(total));
156167
}
157168
} else if (0 == parseRC) {
158169
if (mp_obj.interpret_Transaction()) {
159170
valid = getValidMPTX(hash);
160171
uint32_t omniPropertyId = mp_obj.getProperty();
161172
int64_t omniAmount = mp_obj.getAmount();
162173
if (isPropertyDivisible(omniPropertyId)) {
163-
omniAmountStr = FormatDivisibleShortMP(omniAmount) + getTokenLabel(omniPropertyId);
174+
omniAmountStr = QString::fromStdString(FormatDivisibleShortMP(omniAmount) + getTokenLabel(omniPropertyId));
164175
} else {
165-
omniAmountStr = FormatIndivisibleMP(omniAmount) + getTokenLabel(omniPropertyId);
176+
omniAmountStr = QString::fromStdString(FormatIndivisibleMP(omniAmount) + getTokenLabel(omniPropertyId));
166177
}
167178
if (!mp_obj.getReceiver().empty()) {
168179
if (IsMyAddress(mp_obj.getReceiver())) {
@@ -175,11 +186,14 @@ class TxViewDelegate : public QAbstractItemDelegate
175186
}
176187
}
177188
}
178-
std::string validStr = valid ? "1":"0";
179-
std::string omniSendToSelfStr = omniSendToSelf ? "1":"0";
180-
std::string omniOutboundStr = omniOutbound ? "1":"0";
181-
std::string entry = address.toStdString() + ":" + validStr + ":" + omniSendToSelfStr + ":" + omniOutboundStr + ":" + omniAmountStr;
182-
recentCache.insert(std::make_pair(hash, entry));
189+
// insert into cache
190+
OverviewCacheEntry newEntry;
191+
newEntry.valid = valid;
192+
newEntry.sendToSelf = omniSendToSelf;
193+
newEntry.outbound = omniOutbound;
194+
newEntry.address = address;
195+
newEntry.amount = omniAmountStr;
196+
recentCache.insert(std::make_pair(hash, newEntry));
183197
}
184198
}
185199
}
@@ -232,7 +246,7 @@ class TxViewDelegate : public QAbstractItemDelegate
232246
if (!omniOverride) {
233247
amountText = BitcoinUnits::formatWithUnit(unit, amount, true, BitcoinUnits::separatorAlways);
234248
} else {
235-
amountText = QString::fromStdString(omniAmountStr);
249+
amountText = omniAmountStr;
236250
}
237251
if(!confirmed)
238252
{

0 commit comments

Comments
 (0)