Skip to content

Commit 17da8b4

Browse files
committed
Swap offers monitor class added
1 parent 3cbd8d9 commit 17da8b4

File tree

11 files changed

+2699
-48
lines changed

11 files changed

+2699
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ debug/
5656
release/
5757
cmake-build-*/
5858

59+
docs
5960
wallet/unittests/wallet_test
6061
beam/beam
6162
beam/unittests/node_test

Doxyfile

Lines changed: 2565 additions & 0 deletions
Large diffs are not rendered by default.

android/wallet_model.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ void WalletModel::onAddresses(bool own, const std::vector<WalletAddress>& addres
227227
env->DeleteLocalRef(addrArray);
228228
}
229229

230-
void WalletModel::onSwapOffers(const std::vector<beam::wallet::TxDescription>& offers)
230+
void WalletModel::onSwapOffersChanged(ChangeAction action, const std::vector<beam::wallet::TxDescription>& offers)
231231
{
232-
LOG_DEBUG() << "onSwapOffers()";
232+
LOG_DEBUG() << "onSwapOffersChanged()";
233233

234234
// TODO
235235
}

android/wallet_model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class WalletModel
3333
void onChangeCalculated(beam::Amount change) override;
3434
void onAllUtxoChanged(const std::vector<beam::wallet::Coin>& utxos) override;
3535
void onAddresses(bool own, const std::vector<beam::wallet::WalletAddress>& addrs) override;
36-
void onSwapOffers(const std::vector<beam::wallet::TxDescription>& offers) override;
36+
void onSwapOffersChanged(ChangeAction action, const std::vector<beam::wallet::TxDescription>& offers) override;
3737
void onGeneratedNewAddress(const beam::wallet::WalletAddress& walletAddr) override;
3838
void onNewAddressFailed() override;
3939
void onChangeCurrentWalletIDs(beam::wallet::WalletID senderID, beam::wallet::WalletID receiverID) override;

ui/model/wallet_model.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void WalletModel::onAddresses(bool own, const std::vector<beam::wallet::WalletAd
134134
emit addressesChanged(own, addrs);
135135
}
136136

137-
void WalletModel::onSwapOffers(const std::vector<beam::wallet::TxDescription>& offers)
137+
void WalletModel::onSwapOffersChanged(ChangeAction action, const std::vector<beam::wallet::TxDescription>& offers)
138138
{
139139
emit swapOffersChanged(offers);
140140
}

ui/model/wallet_model.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class WalletModel
5858
void onChangeCalculated(beam::Amount change) override;
5959
void onAllUtxoChanged(const std::vector<beam::wallet::Coin>& utxos) override;
6060
void onAddresses(bool own, const std::vector<beam::wallet::WalletAddress>& addrs) override;
61-
void onSwapOffers(const std::vector<beam::wallet::TxDescription>& offers) override;
61+
void onSwapOffersChanged(ChangeAction action, const std::vector<beam::wallet::TxDescription>& offers) override;
6262
void onGeneratedNewAddress(const beam::wallet::WalletAddress& walletAddr) override;
6363
void onNewAddressFailed() override;
6464
void onChangeCurrentWalletIDs(beam::wallet::WalletID senderID, beam::wallet::WalletID receiverID) override;

ui/view/offers.qml

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -116,43 +116,15 @@ ColumnLayout {
116116
title: qsTrId("general-status")
117117
width: 200 * parent.width / 800
118118
movable: false
119-
resizable: false
120-
delegate: Item {
121-
id: delegate_id
122-
width: parent.width
123-
height: tableView.rowHeight
124-
readonly property var lineSeparator: "\n"
125-
property var texts: styleData.value
126-
property color secondLineColor: Style.content_secondary
127-
128-
ColumnLayout {
129-
anchors.right: parent.right
130-
anchors.left: parent.left
131-
anchors.verticalCenter: parent.verticalCenter
132-
133-
SFLabel {
134-
Layout.fillWidth: true
135-
Layout.fillHeight: true
136-
Layout.topMargin: 20
137-
elide: Text.ElideRight
138-
text: delegate_id.texts[0] // add check for NULL
139-
textFormat: Text.StyledText
140-
font.italic: true
141-
font.pixelSize: 14
142-
}
143-
}
144-
}
145119
}
146120

147121
rowDelegate: Item {
148122
height: tableView.rowHeight
149-
150123
anchors.left: parent.left
151124
anchors.right: parent.right
152125

153126
Rectangle {
154127
anchors.fill: parent
155-
156128
color: Style.background_row_even
157129
visible: styleData.alternate
158130
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright 2019 The Beam Team
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "wallet/common.h"
18+
#include "core/fly_client.h"
19+
#include "wallet/wallet.h"
20+
#include "utility/logger.h"
21+
22+
#include <unordered_map>
23+
24+
namespace beam::wallet
25+
{
26+
using namespace beam::proto;
27+
28+
class SwapOffersMonitor : public FlyClient::IBbsReceiver
29+
{
30+
31+
public:
32+
SwapOffersMonitor(FlyClient::INetwork& network, IWalletObserver& observer)
33+
: m_observer(observer)
34+
{
35+
network.BbsSubscribe(OffersBbsChannel, m_lastTimestamp, this);
36+
37+
// test on dummy data
38+
for (int i = 0; i < 10; ++i) {
39+
40+
TxID id {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,i};
41+
TxDescription txDesc;
42+
43+
txDesc.m_txId = id;
44+
txDesc.m_txType = wallet::TxType::Simple;
45+
txDesc.m_amount = i*11;
46+
txDesc.m_fee=0;
47+
txDesc.m_change=0;
48+
txDesc.m_minHeight=0;
49+
txDesc.m_peerId = Zero;
50+
txDesc.m_myId = Zero;
51+
txDesc.m_message = {'m','e','s','s','a','g','e'};
52+
txDesc.m_createTime=0;
53+
txDesc.m_modifyTime=0;
54+
txDesc.m_sender=false;
55+
txDesc.m_selfTx = false;
56+
txDesc.m_status=TxStatus::Pending;
57+
txDesc.m_kernelID = Zero;
58+
txDesc.m_failureReason = TxFailureReason::Unknown;
59+
60+
m_offersCache.emplace(std::make_pair(id,txDesc));
61+
};
62+
63+
};
64+
65+
virtual void OnMsg(proto::BbsMsg&& msg) override
66+
{
67+
if (msg.m_Message.empty())
68+
return;
69+
70+
Blob blob;
71+
72+
uint8_t* pMsg = &msg.m_Message.front();
73+
uint32_t nSize = static_cast<uint32_t>(msg.m_Message.size());
74+
75+
// SetTxParameter deserializedObj;
76+
TxDescription deserializedObj;
77+
bool bValid = false;
78+
79+
try
80+
{
81+
Deserializer der;
82+
der.reset(pMsg, nSize);
83+
der& deserializedObj;
84+
bValid = true;
85+
}
86+
catch (const std::exception&)
87+
{
88+
LOG_WARNING() << "not crypted BBS deserialization failed";
89+
}
90+
91+
m_offersCache[deserializedObj.m_txId] = deserializedObj;
92+
93+
m_observer.onSwapOffersChanged(ChangeAction::Added, std::vector<TxDescription>{deserializedObj});
94+
}
95+
96+
auto getOffersList() const -> std::vector<TxDescription>
97+
{
98+
std::vector<TxDescription> offers;
99+
100+
for (auto offer : m_offersCache)
101+
{
102+
offers.push_back(offer.second);
103+
}
104+
105+
return offers;
106+
}
107+
108+
// implement smart cache to prevent load of all mesages each time
109+
110+
private:
111+
static constexpr BbsChannel OffersBbsChannel = proto::Bbs::s_MaxChannels - 1;
112+
Timestamp m_lastTimestamp = 0; /// for test
113+
114+
IWalletObserver& m_observer;
115+
116+
std::unordered_map<TxID, TxDescription> m_offersCache;
117+
118+
};
119+
120+
} // namespace beam::wallet

wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ namespace beam::wallet
5252
// @param done - number of done tasks
5353
// @param total - number of total tasks
5454
virtual void onSyncProgress(int done, int total) = 0;
55+
virtual void onSwapOffersChanged(ChangeAction action, std::vector<TxDescription>&& offers) = 0;
5556
};
5657

5758
// Wallet base class.

wallet/wallet_client.cpp

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,6 @@ namespace beam::wallet
239239
, m_isConnected(false)
240240
, m_nodeAddrStr(nodeAddr)
241241
{
242-
// test on dummy data
243-
TxDescription tr1, tr2, tr3, tr4;
244-
tr1.m_amount = 1;
245-
tr2.m_amount = 2;
246-
tr3.m_amount = 3;
247-
tr4.m_amount = 4;
248-
dummySwapOffers.push_back(tr1);
249-
dummySwapOffers.push_back(tr2);
250-
dummySwapOffers.push_back(tr3);
251-
dummySwapOffers.push_back(tr4);
252242
}
253243

254244
WalletClient::~WalletClient()
@@ -296,7 +286,6 @@ namespace beam::wallet
296286

297287
onStatus(getStatus());
298288
onTxStatus(ChangeAction::Reset, m_walletDB->getTxHistory());
299-
onSwapOffers(dummySwapOffers);
300289

301290
static const unsigned LOG_ROTATION_PERIOD_SEC = 3 * 3600; // 3 hours
302291
static const unsigned LOG_CLEANUP_PERIOD_SEC = 120 * 3600; // 5 days
@@ -384,6 +373,9 @@ namespace beam::wallet
384373

385374
wallet_subscriber = make_unique<WalletSubscriber>(static_cast<IWalletObserver*>(this), wallet);
386375

376+
auto offersMonitor = make_shared<SwapOffersMonitor>(nodeNetwork,static_cast<IWalletObserver&>(*this));
377+
m_offersMonitor = offersMonitor;
378+
387379
nodeNetwork->tryToConnect();
388380
m_reactor->run_ex([&wallet, &nodeNetwork](){
389381
wallet->CleanupNetwork();
@@ -576,7 +568,6 @@ namespace beam::wallet
576568
onStatus(getStatus());
577569
onTxStatus(ChangeAction::Reset, m_walletDB->getTxHistory());
578570
onAddresses(false, m_walletDB->getAddresses(false));
579-
onSwapOffers(dummySwapOffers);
580571
}
581572

582573
void WalletClient::getUtxosStatus()
@@ -592,7 +583,7 @@ namespace beam::wallet
592583

593584
void WalletClient::getSwapOffers()
594585
{
595-
onSwapOffers(dummySwapOffers);
586+
onSwapOffersChanged(ChangeAction::Reset, ...);
596587
}
597588

598589
void WalletClient::cancelTx(const TxID& id)

0 commit comments

Comments
 (0)