2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
|
|
|
//
|
|
|
|
// This file is part of Bytecoin.
|
|
|
|
//
|
|
|
|
// Bytecoin is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Bytecoin is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "TestWalletLegacy.h"
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
namespace Tests {
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
using namespace CryptoNote;
|
2015-07-30 15:22:07 +00:00
|
|
|
using namespace Crypto;
|
2015-05-27 12:08:46 +00:00
|
|
|
|
|
|
|
const std::string TEST_PASSWORD = "password";
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TestWalletLegacy::TestWalletLegacy(System::Dispatcher& dispatcher, const Currency& currency, INode& node) :
|
2015-05-27 12:08:46 +00:00
|
|
|
m_dispatcher(dispatcher),
|
|
|
|
m_synchronizationCompleted(dispatcher),
|
|
|
|
m_someTransactionUpdated(dispatcher),
|
|
|
|
m_currency(currency),
|
|
|
|
m_node(node),
|
2015-07-30 15:22:07 +00:00
|
|
|
m_wallet(new CryptoNote::WalletLegacy(currency, node)),
|
2015-05-27 12:08:46 +00:00
|
|
|
m_currentHeight(0) {
|
|
|
|
m_wallet->addObserver(this);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TestWalletLegacy::~TestWalletLegacy() {
|
2015-05-27 12:08:46 +00:00
|
|
|
m_wallet->removeObserver(this);
|
|
|
|
// Make sure all remote spawns are executed
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
m_dispatcher.yield();
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::error_code TestWalletLegacy::init() {
|
|
|
|
CryptoNote::AccountBase walletAccount;
|
2015-05-27 12:08:46 +00:00
|
|
|
walletAccount.generate();
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_wallet->initWithKeys(walletAccount.getAccountKeys(), TEST_PASSWORD);
|
2015-05-27 12:08:46 +00:00
|
|
|
m_synchronizationCompleted.wait();
|
|
|
|
return m_lastSynchronizationResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2015-07-30 15:22:07 +00:00
|
|
|
struct TransactionSendingWaiter : public IWalletLegacyObserver {
|
2015-05-27 12:08:46 +00:00
|
|
|
System::Dispatcher& m_dispatcher;
|
|
|
|
System::Event m_event;
|
|
|
|
bool m_waiting = false;
|
|
|
|
TransactionId m_expectedTxId;
|
|
|
|
std::error_code m_result;
|
|
|
|
|
|
|
|
TransactionSendingWaiter(System::Dispatcher& dispatcher) : m_dispatcher(dispatcher), m_event(dispatcher) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void wait(TransactionId expectedTxId) {
|
|
|
|
m_waiting = true;
|
|
|
|
m_expectedTxId = expectedTxId;
|
|
|
|
m_event.wait();
|
|
|
|
m_waiting = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void sendTransactionCompleted(TransactionId transactionId, std::error_code result) {
|
|
|
|
m_dispatcher.remoteSpawn([this, transactionId, result]() {
|
|
|
|
if (m_waiting && m_expectedTxId == transactionId) {
|
|
|
|
m_result = result;
|
|
|
|
m_event.set();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::error_code TestWalletLegacy::sendTransaction(const std::string& address, uint64_t amount, Hash& txHash) {
|
2015-05-27 12:08:46 +00:00
|
|
|
TransactionSendingWaiter transactionSendingWaiter(m_dispatcher);
|
|
|
|
m_wallet->addObserver(&transactionSendingWaiter);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
WalletLegacyTransfer transfer{ address, static_cast<int64_t>(amount) };
|
2015-05-27 12:08:46 +00:00
|
|
|
auto txId = m_wallet->sendTransaction(transfer, m_currency.minimumFee());
|
|
|
|
transactionSendingWaiter.wait(txId);
|
|
|
|
m_wallet->removeObserver(&transactionSendingWaiter);
|
|
|
|
// TODO workaround: make sure ObserverManager doesn't have local pointers to transactionSendingWaiter, so it can be destroyed
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
// Run all spawned handlers from TransactionSendingWaiter::sendTransactionCompleted
|
|
|
|
m_dispatcher.yield();
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
WalletLegacyTransaction txInfo;
|
2015-05-27 12:08:46 +00:00
|
|
|
if (!m_wallet->getTransaction(txId, txInfo)) {
|
|
|
|
return std::make_error_code(std::errc::identifier_removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
txHash = txInfo.hash;
|
|
|
|
return transactionSendingWaiter.m_result;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
void TestWalletLegacy::waitForSynchronizationToHeight(uint32_t height) {
|
2015-05-27 12:08:46 +00:00
|
|
|
while (m_synchronizedHeight < height) {
|
|
|
|
m_synchronizationCompleted.wait();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
IWalletLegacy* TestWalletLegacy::wallet() {
|
2015-05-27 12:08:46 +00:00
|
|
|
return m_wallet.get();
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
AccountPublicAddress TestWalletLegacy::address() const {
|
2015-05-27 12:08:46 +00:00
|
|
|
std::string addressString = m_wallet->getAddress();
|
|
|
|
AccountPublicAddress address;
|
|
|
|
bool ok = m_currency.parseAccountAddressString(addressString, address);
|
|
|
|
assert(ok);
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
void TestWalletLegacy::synchronizationCompleted(std::error_code result) {
|
2015-05-27 12:08:46 +00:00
|
|
|
m_dispatcher.remoteSpawn([this, result]() {
|
|
|
|
m_lastSynchronizationResult = result;
|
|
|
|
m_synchronizedHeight = m_currentHeight;
|
|
|
|
m_synchronizationCompleted.set();
|
|
|
|
m_synchronizationCompleted.clear();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
void TestWalletLegacy::synchronizationProgressUpdated(uint32_t current, uint32_t total) {
|
2015-05-27 12:08:46 +00:00
|
|
|
m_dispatcher.remoteSpawn([this, current]() {
|
2015-07-30 15:22:07 +00:00
|
|
|
m_currentHeight = current;
|
2015-05-27 12:08:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|