Skip to content

Commit 98aad2d

Browse files
committed
fixed tx_list filtering issue
fixed linux build
1 parent 6319863 commit 98aad2d

File tree

4 files changed

+132
-37
lines changed

4 files changed

+132
-37
lines changed

wallet/api/api_handler.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ namespace beam::wallet
954954
res.resultList.reserve(data.count);
955955
int offset = 0;
956956
int counter = 0;
957-
walletDB->visitTx([&](TxType type, TxStatus status)
957+
walletDB->visitTx([&](TxType type, TxStatus status, Asset::ID assetID, Height height)
958958
{
959959
if (type != TxType::Simple
960960
&& type != TxType::AssetIssue
@@ -964,45 +964,45 @@ namespace beam::wallet
964964
return false;
965965
}
966966

967-
if (data.filter.status && status != *data.filter.status)
967+
if (!data.withAssets && (assetID != Asset::s_InvalidID || type != TxType::Simple))
968968
{
969969
return false;
970970
}
971971

972-
++offset;
973-
if (offset <= data.skip)
972+
if (data.filter.assetId && assetID != *data.filter.assetId)
974973
{
975974
return false;
976975
}
977976

978-
++counter;
979-
return data.count == 0 || counter <= data.count;
980-
},
981-
[&](const auto& tx)
982-
{
983-
if (!data.withAssets && (tx.m_assetId != Asset::s_InvalidID || tx.m_txType != TxType::Simple))
977+
if (data.filter.status && status != *data.filter.status)
984978
{
985-
return;
979+
return false;
986980
}
987981

988-
if (data.filter.assetId && tx.m_assetId != *data.filter.assetId)
982+
if (data.filter.height && height != *data.filter.height)
989983
{
990-
return;
984+
return false;
991985
}
992986

993-
const auto height = storage::DeduceTxProofHeight(*walletDB, tx);
994-
if (data.filter.height && height != *data.filter.height)
987+
return data.count == 0 || counter < data.count;
988+
},
989+
[&](const auto& tx)
990+
{
991+
++offset;
992+
if (offset <= data.skip)
995993
{
996994
return;
997995
}
998-
996+
const auto height = storage::DeduceTxProofHeight(*walletDB, tx);
999997
Status::Response& item = res.resultList.emplace_back();
1000998
item.tx = tx;
1001999
item.txHeight = height;
10021000
item.systemHeight = stateID.m_Height;
10031001
item.confirmations = 0;
1004-
});
10051002

1003+
++counter;
1004+
});
1005+
assert(data.count == 0 || res.resultList.size() <= data.count);
10061006
std::sort(res.resultList.begin(), res.resultList.end(), [](const auto& a, const auto& b)
10071007
{
10081008
return a.tx.m_minHeight > b.tx.m_minHeight;

wallet/core/wallet_db.cpp

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,22 @@ namespace beam::wallet
496496
}
497497
return coins;
498498
}
499+
500+
Height DeduceTxProofHeightImpl(const IWalletDB& walletDB, const TxID& txID, TxType type)
501+
{
502+
Height height = 0;
503+
504+
if (type == TxType::AssetInfo)
505+
{
506+
storage::getTxParameter(walletDB, txID, TxParameterID::AssetConfirmedHeight, height);
507+
}
508+
else
509+
{
510+
storage::getTxParameter(walletDB, txID, TxParameterID::KernelProofHeight, height);
511+
}
512+
513+
return height;
514+
}
499515
}
500516

501517
namespace sqlite
@@ -3312,7 +3328,7 @@ namespace beam::wallet
33123328
notifyShieldedCoinsChanged(ChangeAction::Updated, v);
33133329
}
33143330

3315-
void WalletDB::visitTx(std::function<bool(TxType, TxStatus)> filter, std::function<void(const TxDescription&)> func) const
3331+
void WalletDB::visitTx(std::function<bool(TxType, TxStatus, Asset::ID, Height)> filter, std::function<void(const TxDescription&)> func) const
33163332
{
33173333
using TxData = std::pair<TxID, Timestamp>;
33183334
auto pred = [](const TxData& left, const TxData& right) {return left.second < right.second; };
@@ -3343,11 +3359,19 @@ namespace beam::wallet
33433359
{
33443360
TxID txID = transactions.top().first;
33453361
transactions.pop();
3346-
TxType type;
3347-
TxStatus status;
3362+
TxType type = TxType::Simple;
3363+
TxStatus status = TxStatus::Pending;
3364+
33483365
if (!getTxParameterImpl(txID, kDefaultSubTxID, TxParameterID::TransactionType, type, stm3) ||
3349-
!getTxParameterImpl(txID, kDefaultSubTxID, TxParameterID::Status, status, stm3) ||
3350-
!filter(type, status))
3366+
!getTxParameterImpl(txID, kDefaultSubTxID, TxParameterID::Status, status, stm3))
3367+
{
3368+
continue;
3369+
}
3370+
3371+
Height h = DeduceTxProofHeightImpl(*this, txID, type);
3372+
Asset::ID assetID = Asset::s_InvalidID;
3373+
getTxParameterImpl(txID, kDefaultSubTxID, TxParameterID::AssetID, assetID, stm3);
3374+
if (!filter(type, status, assetID, h))
33513375
{
33523376
continue;
33533377
}
@@ -5175,18 +5199,7 @@ namespace beam::wallet
51755199

51765200
Height DeduceTxProofHeight(const IWalletDB& walletDB, const TxDescription &tx)
51775201
{
5178-
Height height = 0;
5179-
5180-
if (tx.m_txType == TxType::AssetInfo)
5181-
{
5182-
storage::getTxParameter(walletDB, tx.m_txId, TxParameterID::AssetConfirmedHeight, height);
5183-
}
5184-
else
5185-
{
5186-
storage::getTxParameter(walletDB, tx.m_txId, TxParameterID::KernelProofHeight, height);
5187-
}
5188-
5189-
return height;
5202+
return DeduceTxProofHeightImpl(walletDB, tx.m_txId, tx.m_txType);
51905203
}
51915204

51925205
Height DeduceTxDisplayHeight(const IWalletDB& walletDB, const TxDescription &tx)

wallet/core/wallet_db.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ namespace beam::wallet
470470

471471
// /////////////////////////////////////////////
472472
// Transaction management
473-
virtual void visitTx(std::function<bool(TxType, TxStatus)> filter, std::function<void(const TxDescription&)> func) const = 0;
473+
virtual void visitTx(std::function<bool(TxType, TxStatus, Asset::ID, Height)> filter, std::function<void(const TxDescription&)> func) const = 0;
474474
virtual std::vector<TxDescription> getTxHistory(wallet::TxType txType = wallet::TxType::Simple, uint64_t start = 0, int count = std::numeric_limits<int>::max()) const = 0;
475475
virtual boost::optional<TxDescription> getTx(const TxID& txId) const = 0;
476476
virtual void saveTx(const TxDescription& p) = 0;
@@ -636,7 +636,7 @@ namespace beam::wallet
636636
void DeleteShieldedCoin(const ShieldedTxo::BaseKey&) override;
637637
void rollbackConfirmedShieldedUtxo(Height minHeight) override;
638638

639-
void visitTx(std::function<bool(TxType, TxStatus)> filter, std::function<void(const TxDescription&)> func) const override;
639+
void visitTx(std::function<bool(TxType, TxStatus, Asset::ID, Height)> filter, std::function<void(const TxDescription&)> func) const override;
640640
std::vector<TxDescription> getTxHistory(wallet::TxType txType, uint64_t start, int count) const override;
641641
boost::optional<TxDescription> getTx(const TxID& txId) const override;
642642
void saveTx(const TxDescription& p) override;

wallet/unittests/wallet_test.cpp

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,29 @@ namespace
105105
struct ApiHandler : beam::wallet::WalletApiHandler
106106
{
107107
using beam::wallet::WalletApiHandler::WalletApiHandler;
108+
109+
std::vector<json> m_Messages;
108110
void serializeMsg(const json& msg) override
109111
{
112+
m_Messages.push_back(msg);
113+
}
110114

115+
void TestTxListResSize(size_t s)
116+
{
117+
WALLET_CHECK(m_Messages.size() == 1);
118+
WALLET_CHECK(m_Messages[0]["result"].size() == s);
111119
}
120+
121+
void TestTxListResHeight(Height h)
122+
{
123+
WALLET_CHECK(m_Messages.size() == 1);
124+
WALLET_CHECK(m_Messages[0]["result"][0]["height"] == h);
125+
}
126+
112127
};
113128

129+
130+
114131
void TestTxList()
115132
{
116133
cout << "\nTesting Tx list...\n";
@@ -209,24 +226,89 @@ namespace
209226
for (int i = 0; i < 100; ++i)
210227
{
211228
handler.onMessage(1, message);
229+
handler.TestTxListResSize(10);
230+
handler.m_Messages.clear();
212231
}
213232
sw.stop();
214233
cout << "TxList elapsed time: " << sw.milliseconds() << " ms\n";
215234

235+
message.count = 10;
236+
message.skip = 0;
237+
handler.onMessage(1, message);
238+
handler.TestTxListResSize(10);
239+
handler.m_Messages.clear();
240+
241+
message.count = 100;
242+
message.skip = 0;
243+
handler.onMessage(1, message);
244+
handler.TestTxListResSize(64);
245+
handler.m_Messages.clear();
246+
247+
message.count = 100;
248+
message.skip = 10;
249+
handler.onMessage(1, message);
250+
handler.TestTxListResSize(54);
251+
handler.m_Messages.clear();
252+
253+
message.count = 10;
254+
message.skip = 10;
255+
handler.onMessage(1, message);
256+
handler.TestTxListResSize(10);
257+
handler.m_Messages.clear();
258+
259+
message.count = 10;
260+
message.skip = 63;
261+
handler.onMessage(1, message);
262+
handler.TestTxListResSize(1);
263+
handler.m_Messages.clear();
264+
265+
message.count = 10;
266+
message.skip = 64;
267+
handler.onMessage(1, message);
268+
handler.TestTxListResSize(0);
269+
handler.m_Messages.clear();
270+
271+
message.count = 10;
272+
message.skip = 65;
273+
handler.onMessage(1, message);
274+
handler.TestTxListResSize(0);
275+
handler.m_Messages.clear();
276+
216277
Timestamp t = std::numeric_limits<Timestamp>::max();
217278
int count = 0;
218-
sender.m_WalletDB->visitTx([](auto t, auto s)
279+
280+
std::map<TxID, Height> storedTx;
281+
sender.m_WalletDB->visitTx([](auto t, auto s, auto assetID, auto h)
219282
{
220283
return true;
221284
}, [&](const auto& tx)
222285
{
286+
const auto height = storage::DeduceTxProofHeight(*sender.m_WalletDB, tx);
287+
storedTx.emplace(tx.m_txId, height);
223288
WALLET_CHECK(tx.m_createTime > 0);
224289
WALLET_CHECK(tx.m_createTime < t);
225290
t = tx.m_createTime;
226291
++count;
227292
return true;
228293
});
229294
WALLET_CHECK(count == Count);
295+
WALLET_CHECK(count == (int)storedTx.size());
296+
for (auto p : storedTx)
297+
{
298+
299+
TxList message2;
300+
message2.count = 0;
301+
message2.skip = 0;
302+
message2.filter.status = wallet::TxStatus::Completed;
303+
message2.withAssets = false;
304+
message2.filter.height = p.second;
305+
306+
handler.onMessage(1, message2);
307+
handler.TestTxListResSize(1);
308+
handler.TestTxListResHeight(p.second);
309+
310+
handler.m_Messages.clear();
311+
}
230312
}
231313

232314
void TestEventTypeSerialization()

0 commit comments

Comments
 (0)