mirror of
https://github.com/monero-project/monero.git
synced 2025-12-14 21:08:51 -05:00
WalletListener functionality
This commit is contained in:
parent
d27b883b2d
commit
9d2cb4f36c
5 changed files with 196 additions and 53 deletions
|
|
@ -46,6 +46,7 @@ namespace Bitmonero {
|
|||
namespace {
|
||||
// copy-pasted from simplewallet
|
||||
static const size_t DEFAULT_MIXIN = 4;
|
||||
static const int DEFAULT_REFRESH_INTERVAL_SECONDS = 10;
|
||||
}
|
||||
|
||||
struct Wallet2CallbackImpl : public tools::i_wallet2_callback
|
||||
|
|
@ -75,7 +76,7 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
|
|||
virtual void on_new_block(uint64_t height, const cryptonote::block& block)
|
||||
{
|
||||
// TODO;
|
||||
LOG_PRINT_L3(__FUNCTION__ << ": new block. height: " << height);
|
||||
LOG_PRINT_L0(__FUNCTION__ << ": new block. height: " << height);
|
||||
}
|
||||
|
||||
virtual void on_money_received(uint64_t height, const cryptonote::transaction& tx, size_t out_index)
|
||||
|
|
@ -84,11 +85,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
|
|||
std::string tx_hash = epee::string_tools::pod_to_hex(get_transaction_hash(tx));
|
||||
uint64_t amount = tx.vout[out_index].amount;
|
||||
|
||||
LOG_PRINT_L3(__FUNCTION__ << ": money received. height: " << height
|
||||
LOG_PRINT_L0(__FUNCTION__ << ": money received. height: " << height
|
||||
<< ", tx: " << tx_hash
|
||||
<< ", amount: " << print_money(amount));
|
||||
if (m_listener) {
|
||||
m_listener->moneyReceived(tx_hash, amount);
|
||||
m_listener->updated();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -98,11 +100,12 @@ struct Wallet2CallbackImpl : public tools::i_wallet2_callback
|
|||
// TODO;
|
||||
std::string tx_hash = epee::string_tools::pod_to_hex(get_transaction_hash(spend_tx));
|
||||
uint64_t amount = in_tx.vout[out_index].amount;
|
||||
LOG_PRINT_L3(__FUNCTION__ << ": money spent. height: " << height
|
||||
LOG_PRINT_L0(__FUNCTION__ << ": money spent. height: " << height
|
||||
<< ", tx: " << tx_hash
|
||||
<< ", amount: " << print_money(amount));
|
||||
if (m_listener) {
|
||||
m_listener->moneySpent(tx_hash, amount);
|
||||
m_listener->updated();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -159,13 +162,22 @@ WalletImpl::WalletImpl(bool testnet)
|
|||
m_wallet = new tools::wallet2(testnet);
|
||||
m_history = new TransactionHistoryImpl(this);
|
||||
m_wallet2Callback = new Wallet2CallbackImpl;
|
||||
m_wallet->callback(m_wallet2Callback);
|
||||
m_refreshThreadDone = false;
|
||||
m_refreshEnabled = false;
|
||||
m_refreshIntervalSeconds = DEFAULT_REFRESH_INTERVAL_SECONDS;
|
||||
m_refreshThread = std::thread([this] () {
|
||||
this->refreshThreadFunc();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
WalletImpl::~WalletImpl()
|
||||
{
|
||||
delete m_wallet2Callback;
|
||||
stopRefresh();
|
||||
delete m_history;
|
||||
delete m_wallet;
|
||||
delete m_wallet2Callback;
|
||||
}
|
||||
|
||||
bool WalletImpl::create(const std::string &path, const std::string &password, const std::string &language)
|
||||
|
|
@ -359,6 +371,7 @@ bool WalletImpl::init(const std::string &daemon_address, uint64_t upper_transact
|
|||
if (Utils::isAddressLocal(daemon_address)) {
|
||||
this->setTrustedDaemon(true);
|
||||
}
|
||||
startRefresh();
|
||||
|
||||
} catch (const std::exception &e) {
|
||||
LOG_ERROR("Error initializing wallet: " << e.what());
|
||||
|
|
@ -383,15 +396,17 @@ uint64_t WalletImpl::unlockedBalance() const
|
|||
bool WalletImpl::refresh()
|
||||
{
|
||||
clearStatus();
|
||||
try {
|
||||
m_wallet->refresh();
|
||||
} catch (const std::exception &e) {
|
||||
m_status = Status_Error;
|
||||
m_errorString = e.what();
|
||||
}
|
||||
doRefresh();
|
||||
return m_status == Status_Ok;
|
||||
}
|
||||
|
||||
void WalletImpl::refreshAsync()
|
||||
{
|
||||
LOG_PRINT_L3(__FUNCTION__ << ": Refreshing asyncronously..");
|
||||
clearStatus();
|
||||
m_refreshCV.notify_one();
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// 1 - properly handle payment id (add another menthod with explicit 'payment_id' param)
|
||||
// 2 - check / design how "Transaction" can be single interface
|
||||
|
|
@ -574,6 +589,8 @@ bool WalletImpl::connectToDaemon()
|
|||
m_status = result ? Status_Ok : Status_Error;
|
||||
if (!result) {
|
||||
m_errorString = "Error connecting to daemon at " + m_wallet->get_daemon_address();
|
||||
} else {
|
||||
// start refreshing here
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
@ -594,5 +611,57 @@ void WalletImpl::clearStatus()
|
|||
m_errorString.clear();
|
||||
}
|
||||
|
||||
void WalletImpl::refreshThreadFunc()
|
||||
{
|
||||
LOG_PRINT_L3(__FUNCTION__ << ": starting refresh thread");
|
||||
|
||||
while (true) {
|
||||
std::unique_lock<std::mutex> lock(m_refreshMutex);
|
||||
if (m_refreshThreadDone) {
|
||||
break;
|
||||
}
|
||||
m_refreshCV.wait_for(lock, std::chrono::seconds(m_refreshIntervalSeconds));
|
||||
if (m_refreshEnabled && m_status == Status_Ok) {
|
||||
doRefresh();
|
||||
}
|
||||
}
|
||||
LOG_PRINT_L3(__FUNCTION__ << ": refresh thread stopped");
|
||||
}
|
||||
|
||||
void WalletImpl::doRefresh()
|
||||
{
|
||||
// synchronizing async and sync refresh calls
|
||||
std::lock_guard<std::mutex> guarg(m_refreshMutex2);
|
||||
try {
|
||||
m_wallet->refresh();
|
||||
if (m_walletListener) {
|
||||
m_walletListener->refreshed();
|
||||
}
|
||||
} catch (const std::exception &e) {
|
||||
m_status = Status_Error;
|
||||
m_errorString = e.what();
|
||||
}
|
||||
}
|
||||
|
||||
// supposed to be called from ctor only
|
||||
void WalletImpl::startRefresh()
|
||||
{
|
||||
if (!m_refreshEnabled) {
|
||||
m_refreshEnabled = true;
|
||||
m_refreshCV.notify_one();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// supposed to be called from dtor only
|
||||
void WalletImpl::stopRefresh()
|
||||
{
|
||||
if (!m_refreshThreadDone) {
|
||||
m_refreshEnabled = false;
|
||||
m_refreshThreadDone = true;
|
||||
m_refreshThread.join();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace
|
||||
|
|
|
|||
|
|
@ -35,6 +35,9 @@
|
|||
#include "wallet/wallet2.h"
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
|
||||
namespace Bitmonero {
|
||||
|
|
@ -71,7 +74,7 @@ public:
|
|||
uint64_t balance() const;
|
||||
uint64_t unlockedBalance() const;
|
||||
bool refresh();
|
||||
|
||||
void refreshAsync();
|
||||
PendingTransaction * createTransaction(const std::string &dst_addr, const std::string &payment_id,
|
||||
uint64_t amount, uint32_t mixin_count,
|
||||
PendingTransaction::Priority priority = PendingTransaction::Priority_Low);
|
||||
|
|
@ -84,19 +87,33 @@ public:
|
|||
|
||||
private:
|
||||
void clearStatus();
|
||||
void refreshThreadFunc();
|
||||
void doRefresh();
|
||||
void startRefresh();
|
||||
void stopRefresh();
|
||||
|
||||
private:
|
||||
friend class PendingTransactionImpl;
|
||||
friend class TransactionHistoryImpl;
|
||||
|
||||
tools::wallet2 * m_wallet;
|
||||
int m_status;
|
||||
std::atomic<int> m_status;
|
||||
std::string m_errorString;
|
||||
std::string m_password;
|
||||
TransactionHistoryImpl * m_history;
|
||||
bool m_trustedDaemon;
|
||||
WalletListener * m_walletListener;
|
||||
Wallet2CallbackImpl * m_wallet2Callback;
|
||||
|
||||
// multi-threaded refresh stuff
|
||||
std::atomic<bool> m_refreshEnabled;
|
||||
std::atomic<bool> m_refreshThreadDone;
|
||||
std::atomic<int> m_refreshIntervalSeconds;
|
||||
std::mutex m_refreshMutex;
|
||||
std::mutex m_refreshMutex2;
|
||||
std::condition_variable m_refreshCV;
|
||||
std::thread m_refreshThread;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue