danicoin/tests/IntegrationTests/WalletLegacyObserver.h

165 lines
4.6 KiB
C
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
2015-05-27 12:08:46 +00:00
#pragma once
2015-07-30 15:22:07 +00:00
#include "IWalletLegacy.h"
2015-05-27 12:08:46 +00:00
#include <atomic>
#include <condition_variable>
#include <deque>
#include <mutex>
#include <unordered_map>
2015-07-30 15:22:07 +00:00
#include "../IntegrationTestLib/ObservableValue.h"
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2015-07-30 15:22:07 +00:00
class WalletLegacyObserver: public IWalletLegacyObserver {
2015-05-27 12:08:46 +00:00
public:
2015-07-30 15:22:07 +00:00
WalletLegacyObserver() :
2015-05-27 12:08:46 +00:00
m_actualBalance(0),
m_actualBalancePrev(0),
m_pendingBalance(0),
m_pendingBalancePrev(0),
2015-07-30 15:22:07 +00:00
m_syncResult(m_mutex, m_cv) {}
2015-05-27 12:08:46 +00:00
2015-07-30 15:22:07 +00:00
virtual void actualBalanceUpdated(uint64_t actualBalance) override {
2015-05-27 12:08:46 +00:00
std::unique_lock<std::mutex> lk(m_mutex);
m_actualBalance = actualBalance;
lk.unlock();
m_cv.notify_all();
}
2015-07-30 15:22:07 +00:00
virtual void pendingBalanceUpdated(uint64_t pendingBalance) override {
2015-05-27 12:08:46 +00:00
std::unique_lock<std::mutex> lk(m_mutex);
m_pendingBalance = pendingBalance;
lk.unlock();
m_cv.notify_all();
}
2015-07-30 15:22:07 +00:00
virtual void sendTransactionCompleted(CryptoNote::TransactionId transactionId, std::error_code result) override {
2015-05-27 12:08:46 +00:00
std::unique_lock<std::mutex> lk(m_mutex);
m_sendResults[transactionId] = result;
m_cv.notify_all();
}
2015-07-30 15:22:07 +00:00
virtual void synchronizationCompleted(std::error_code result) override {
m_syncResult.set(result);
2015-05-27 12:08:46 +00:00
}
2015-07-30 15:22:07 +00:00
virtual void synchronizationProgressUpdated(uint32_t current, uint32_t total) override {
2015-05-27 12:08:46 +00:00
std::unique_lock<std::mutex> lk(m_mutex);
2015-07-30 15:22:07 +00:00
m_syncProgress.emplace_back(current, total);
2015-05-27 12:08:46 +00:00
m_currentHeight = current;
m_cv.notify_all();
}
virtual void externalTransactionCreated(TransactionId transactionId) override {
std::unique_lock<std::mutex> lk(m_mutex);
m_externalTransactions.push_back(transactionId);
m_cv.notify_all();
}
uint64_t getCurrentHeight() {
std::unique_lock<std::mutex> lk(m_mutex);
return m_currentHeight;
}
uint64_t waitPendingBalanceChange() {
std::unique_lock<std::mutex> lk(m_mutex);
while (m_pendingBalance == m_pendingBalancePrev) {
m_cv.wait(lk);
}
m_pendingBalancePrev = m_pendingBalance;
return m_pendingBalance;
}
uint64_t waitTotalBalanceChange() {
std::unique_lock<std::mutex> lk(m_mutex);
while (m_pendingBalance == m_pendingBalancePrev && m_actualBalance == m_actualBalancePrev) {
m_cv.wait(lk);
}
m_actualBalancePrev = m_actualBalance;
m_pendingBalancePrev = m_pendingBalance;
return m_actualBalance + m_pendingBalance;
}
CryptoNote::TransactionId waitExternalTransaction() {
std::unique_lock<std::mutex> lk(m_mutex);
while (m_externalTransactions.empty()) {
m_cv.wait(lk);
}
CryptoNote::TransactionId txId = m_externalTransactions.front();
m_externalTransactions.pop_front();
return txId;
}
template<class Rep, class Period>
std::pair<bool, uint64_t> waitPendingBalanceChangeFor(const std::chrono::duration<Rep, Period>& timePeriod) {
std::unique_lock<std::mutex> lk(m_mutex);
bool result = m_cv.wait_for(lk, timePeriod, [&] { return m_pendingBalance != m_pendingBalancePrev; });
m_pendingBalancePrev = m_pendingBalance;
return std::make_pair(result, m_pendingBalance);
}
uint64_t waitActualBalanceChange() {
std::unique_lock<std::mutex> lk(m_mutex);
while (m_actualBalance == m_actualBalancePrev) {
m_cv.wait(lk);
}
m_actualBalancePrev = m_actualBalance;
return m_actualBalance;
}
std::error_code waitSendResult(CryptoNote::TransactionId txid) {
std::unique_lock<std::mutex> lk(m_mutex);
std::unordered_map<CryptoNote::TransactionId, std::error_code>::iterator it;
while ((it = m_sendResults.find(txid)) == m_sendResults.end()) {
m_cv.wait(lk);
}
return it->second;
}
uint64_t totalBalance() {
std::unique_lock<std::mutex> lk(m_mutex);
m_pendingBalancePrev = m_pendingBalance;
m_actualBalancePrev = m_actualBalance;
return m_pendingBalance + m_actualBalance;
}
2015-07-30 15:22:07 +00:00
std::vector<std::pair<uint32_t, uint32_t>> getSyncProgress() {
std::unique_lock<std::mutex> lk(m_mutex);
return m_syncProgress;
}
ObservableValueBase<std::error_code> m_syncResult;
2015-05-27 12:08:46 +00:00
private:
std::mutex m_mutex;
std::condition_variable m_cv;
2015-07-30 15:22:07 +00:00
2015-05-27 12:08:46 +00:00
uint64_t m_actualBalance;
uint64_t m_actualBalancePrev;
uint64_t m_pendingBalance;
uint64_t m_pendingBalancePrev;
2015-07-30 15:22:07 +00:00
uint32_t m_currentHeight;
std::vector<std::pair<uint32_t, uint32_t>> m_syncProgress;
2015-05-27 12:08:46 +00:00
std::unordered_map<CryptoNote::TransactionId, std::error_code> m_sendResults;
std::deque<CryptoNote::TransactionId> m_externalTransactions;
};
}