danicoin/tests/UnitTests/TestWalletLegacy.cpp

1831 lines
56 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-06-25 17:21:42 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "gtest/gtest.h"
#include <future>
#include <chrono>
#include <array>
#include "EventWaiter.h"
2014-06-25 17:21:42 +00:00
#include "INode.h"
2015-07-30 15:22:07 +00:00
#include "WalletLegacy/WalletLegacy.h"
#include "WalletLegacy/WalletHelper.h"
#include "CryptoNoteCore/Account.h"
#include "CryptoNoteCore/Currency.h"
#include "CryptoNote.h"
2014-06-25 17:21:42 +00:00
#include "INodeStubs.h"
#include "TestBlockchainGenerator.h"
2015-05-27 12:08:46 +00:00
#include <Logging/ConsoleLogger.h>
2014-06-25 17:21:42 +00:00
2015-07-30 15:22:07 +00:00
class TrivialWalletObserver : public CryptoNote::IWalletLegacyObserver
2014-06-25 17:21:42 +00:00
{
public:
TrivialWalletObserver() : actualBalance(0), pendingBalance(0) {}
2014-06-25 17:21:42 +00:00
bool waitForSyncEnd() {
return synced.wait_for(std::chrono::milliseconds(3000));
2014-06-25 17:21:42 +00:00
}
bool waitForSendEnd(std::error_code& ec) {
if (!sent.wait_for(std::chrono::milliseconds(5000))) return false;
ec = sendResult;
2014-06-25 17:21:42 +00:00
return true;
}
bool waitForSaveEnd(std::error_code& ec) {
if (!saved.wait_for(std::chrono::milliseconds(5000))) return false;
ec = saveResult;
2014-06-25 17:21:42 +00:00
return true;
}
bool waitForLoadEnd(std::error_code& ec) {
if (!loaden.wait_for(std::chrono::milliseconds(5000))) return false;
ec = loadResult;
2014-06-25 17:21:42 +00:00
return true;
}
virtual void synchronizationCompleted(std::error_code result) override {
synced.notify();
2014-06-25 17:21:42 +00:00
}
virtual void sendTransactionCompleted(CryptoNote::TransactionId transactionId, std::error_code result) override {
sendResult = result;
sent.notify();
2014-06-25 17:21:42 +00:00
}
virtual void saveCompleted(std::error_code result) override {
saveResult = result;
saved.notify();
2014-06-25 17:21:42 +00:00
}
virtual void initCompleted(std::error_code result) override {
loadResult = result;
loaden.notify();
2014-06-25 17:21:42 +00:00
}
virtual void actualBalanceUpdated(uint64_t actualBalance) override {
// std::cout << "actual balance: " << actualBalance << std::endl;
this->actualBalance = actualBalance;
2014-06-25 17:21:42 +00:00
}
virtual void pendingBalanceUpdated(uint64_t pendingBalance) override {
2014-06-25 17:21:42 +00:00
// std::cout << "pending balance: " << pendingBalance << std::endl;
this->pendingBalance = pendingBalance;
2014-06-25 17:21:42 +00:00
}
std::error_code sendResult;
std::error_code saveResult;
std::error_code loadResult;
std::atomic<uint64_t> actualBalance;
std::atomic<uint64_t> pendingBalance;
EventWaiter synced;
EventWaiter saved;
EventWaiter loaden;
EventWaiter sent;
2014-06-25 17:21:42 +00:00
};
2015-07-30 15:22:07 +00:00
struct SaveOnInitWalletObserver: public CryptoNote::IWalletLegacyObserver {
SaveOnInitWalletObserver(CryptoNote::WalletLegacy* wallet) : wallet(wallet) {
};
2014-08-13 10:51:37 +00:00
virtual ~SaveOnInitWalletObserver() {}
virtual void initCompleted(std::error_code result) override {
2014-08-13 10:51:37 +00:00
wallet->save(stream, true, true);
}
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacy* wallet;
2014-08-13 10:51:37 +00:00
std::stringstream stream;
2014-06-25 17:21:42 +00:00
};
static const uint64_t TEST_BLOCK_REWARD = 70368744177663;
2015-07-30 15:22:07 +00:00
CryptoNote::TransactionId TransferMoney(CryptoNote::WalletLegacy& from, CryptoNote::WalletLegacy& to, int64_t amount, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "") {
CryptoNote::WalletLegacyTransfer transfer;
2014-06-25 17:21:42 +00:00
transfer.amount = amount;
transfer.address = to.getAddress();
return from.sendTransaction(transfer, fee, extra, mixIn);
}
void WaitWalletSync(TrivialWalletObserver* observer) {
ASSERT_TRUE(observer->waitForSyncEnd());
}
void WaitWalletSend(TrivialWalletObserver* observer) {
std::error_code ec;
ASSERT_TRUE(observer->waitForSendEnd(ec));
}
void WaitWalletSend(TrivialWalletObserver* observer, std::error_code& ec) {
ASSERT_TRUE(observer->waitForSendEnd(ec));
}
void WaitWalletSave(TrivialWalletObserver* observer) {
std::error_code ec;
ASSERT_TRUE(observer->waitForSaveEnd(ec));
EXPECT_FALSE(ec);
}
void WaitWalletLoad(TrivialWalletObserver* observer) {
std::error_code ec;
ASSERT_TRUE(observer->waitForLoadEnd(ec));
EXPECT_FALSE(ec);
}
2015-07-30 15:22:07 +00:00
class WalletLegacyApi : public ::testing::Test
2014-06-25 17:21:42 +00:00
{
public:
2015-07-30 15:22:07 +00:00
WalletLegacyApi() : m_currency(CryptoNote::CurrencyBuilder(m_logger).currency()), generator(m_currency) {
2014-08-13 10:51:37 +00:00
}
void SetUp() override;
2014-06-25 17:21:42 +00:00
protected:
void prepareAliceWallet();
void prepareBobWallet();
void prepareCarolWallet();
2015-07-30 15:22:07 +00:00
void GetOneBlockReward(CryptoNote::WalletLegacy& wallet);
2015-08-05 13:09:05 +00:00
void GetOneBlockReward(CryptoNote::WalletLegacy& wallet, const CryptoNote::Currency& currency, TestBlockchainGenerator& blockchainGenerator);
void GetOneBlockRewardAndUnlock(CryptoNote::WalletLegacy& wallet, TrivialWalletObserver& observer, INodeTrivialRefreshStub& node,
const CryptoNote::Currency& currency, TestBlockchainGenerator& blockchainGenerator);
2014-06-25 17:21:42 +00:00
void TestSendMoney(int64_t transferAmount, uint64_t fee, uint64_t mixIn = 0, const std::string& extra = "");
void performTransferWithErrorTx(const std::array<int64_t, 5>& amounts, uint64_t fee);
2015-05-27 12:08:46 +00:00
Logging::ConsoleLogger m_logger;
CryptoNote::Currency m_currency;
2014-08-13 10:51:37 +00:00
2014-06-25 17:21:42 +00:00
TestBlockchainGenerator generator;
std::shared_ptr<TrivialWalletObserver> aliceWalletObserver;
std::shared_ptr<INodeTrivialRefreshStub> aliceNode;
2015-07-30 15:22:07 +00:00
std::shared_ptr<CryptoNote::WalletLegacy> alice;
2014-06-25 17:21:42 +00:00
std::shared_ptr<TrivialWalletObserver> bobWalletObserver;
std::shared_ptr<INodeTrivialRefreshStub> bobNode;
2015-07-30 15:22:07 +00:00
std::shared_ptr<CryptoNote::WalletLegacy> bob;
2014-06-25 17:21:42 +00:00
std::shared_ptr<TrivialWalletObserver> carolWalletObserver;
std::shared_ptr<INodeTrivialRefreshStub> carolNode;
2015-07-30 15:22:07 +00:00
std::shared_ptr<CryptoNote::WalletLegacy> carol;
2014-06-25 17:21:42 +00:00
};
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::SetUp() {
2014-06-25 17:21:42 +00:00
prepareAliceWallet();
generator.generateEmptyBlocks(3);
}
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::prepareAliceWallet() {
decltype(aliceNode) newNode(new INodeTrivialRefreshStub(generator));
2014-06-25 17:21:42 +00:00
2015-07-30 15:22:07 +00:00
alice.reset(new CryptoNote::WalletLegacy(m_currency, *newNode));
aliceNode = newNode;
2014-06-25 17:21:42 +00:00
aliceWalletObserver.reset(new TrivialWalletObserver());
2014-06-25 17:21:42 +00:00
alice->addObserver(aliceWalletObserver.get());
}
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::prepareBobWallet() {
2014-06-25 17:21:42 +00:00
bobNode.reset(new INodeTrivialRefreshStub(generator));
bobWalletObserver.reset(new TrivialWalletObserver());
2015-07-30 15:22:07 +00:00
bob.reset(new CryptoNote::WalletLegacy(m_currency, *bobNode));
2014-06-25 17:21:42 +00:00
bob->addObserver(bobWalletObserver.get());
}
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::prepareCarolWallet() {
2014-06-25 17:21:42 +00:00
carolNode.reset(new INodeTrivialRefreshStub(generator));
carolWalletObserver.reset(new TrivialWalletObserver());
2015-07-30 15:22:07 +00:00
carol.reset(new CryptoNote::WalletLegacy(m_currency, *carolNode));
2014-06-25 17:21:42 +00:00
carol->addObserver(carolWalletObserver.get());
}
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::GetOneBlockReward(CryptoNote::WalletLegacy& wallet) {
2015-08-05 13:09:05 +00:00
GetOneBlockReward(wallet, m_currency, generator);
2014-06-25 17:21:42 +00:00
}
2015-08-05 13:09:05 +00:00
void WalletLegacyApi::GetOneBlockReward(CryptoNote::WalletLegacy& wallet, const CryptoNote::Currency& currency, TestBlockchainGenerator& blockchainGenerator) {
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
2015-08-05 13:09:05 +00:00
ASSERT_TRUE(currency.parseAccountAddressString(wallet.getAddress(), address));
blockchainGenerator.getBlockRewardForAddress(address);
}
void WalletLegacyApi::GetOneBlockRewardAndUnlock(CryptoNote::WalletLegacy& wallet, TrivialWalletObserver& observer, INodeTrivialRefreshStub& node,
const CryptoNote::Currency& currency, TestBlockchainGenerator& blockchainGenerator) {
GetOneBlockReward(wallet, currency, blockchainGenerator);
blockchainGenerator.generateEmptyBlocks(10);
node.updateObservers();
WaitWalletSync(&observer);
2014-06-25 17:21:42 +00:00
}
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::performTransferWithErrorTx(const std::array<int64_t, 5>& amounts, uint64_t fee) {
std::vector<CryptoNote::WalletLegacyTransfer> trs;
CryptoNote::WalletLegacyTransfer tr;
2014-06-25 17:21:42 +00:00
tr.address = bob->getAddress();
tr.amount = amounts[0];
trs.push_back(tr);
tr.address = bob->getAddress();
tr.amount = amounts[1];
trs.push_back(tr);
tr.address = carol->getAddress();
tr.amount = amounts[2];
trs.push_back(tr);
aliceNode->setNextTransactionError();
alice->sendTransaction(trs, fee);
std::error_code result;
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get(), result));
ASSERT_NE(result.value(), 0);
trs.clear();
tr.address = bob->getAddress();
tr.amount = amounts[3];
trs.push_back(tr);
tr.address = carol->getAddress();
tr.amount = amounts[4];
trs.push_back(tr);
alice->sendTransaction(trs, fee);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get(), result));
ASSERT_EQ(result.value(), 0);
}
2015-07-30 15:22:07 +00:00
void WalletLegacyApi::TestSendMoney(int64_t transferAmount, uint64_t fee, uint64_t mixIn, const std::string& extra) {
2014-06-25 17:21:42 +00:00
prepareBobWallet();
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
2015-05-27 12:08:46 +00:00
//unlock Alice's money
2014-06-25 17:21:42 +00:00
generator.generateEmptyBlocks(10);
uint64_t expectedBalance = TEST_BLOCK_REWARD;
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(0, alice->pendingBalance());
EXPECT_EQ(expectedBalance, alice->actualBalance());
EXPECT_EQ(expectedBalance, aliceWalletObserver->actualBalance);
EXPECT_EQ(0, aliceWalletObserver->pendingBalance);
2014-06-25 17:21:42 +00:00
bob->initAndGenerate("pass2");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
2015-07-09 14:52:47 +00:00
ASSERT_NO_FATAL_FAILURE(TransferMoney(*alice, *bob, transferAmount, fee, mixIn, ""));
2015-05-27 12:08:46 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
2014-06-25 17:21:42 +00:00
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
bobNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
EXPECT_EQ(0, bob->pendingBalance());
EXPECT_EQ(transferAmount, bob->actualBalance());
2014-06-25 17:21:42 +00:00
EXPECT_EQ(0, alice->pendingBalance());
EXPECT_EQ(expectedBalance - transferAmount - fee, alice->actualBalance());
2014-06-25 17:21:42 +00:00
alice->shutdown();
bob->shutdown();
}
void WaitWalletLoad(TrivialWalletObserver* observer, std::error_code& ec) {
ASSERT_TRUE(observer->waitForLoadEnd(ec));
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, initAndSave) {
2014-08-13 10:51:37 +00:00
SaveOnInitWalletObserver saveOnInit(alice.get());
alice->addObserver(&saveOnInit);
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
2014-08-13 10:51:37 +00:00
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, refreshWithMoney) {
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(alice->actualBalance(), 0);
ASSERT_EQ(alice->pendingBalance(), 0);
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
2014-08-13 10:51:37 +00:00
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
2014-06-25 17:21:42 +00:00
generator.getBlockRewardForAddress(address);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(alice->actualBalance(), 0);
EXPECT_EQ(alice->pendingBalance(), TEST_BLOCK_REWARD);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, initWithMoney) {
2014-08-13 10:51:37 +00:00
std::stringstream archive;
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->save(archive, true, true);
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
ASSERT_EQ(alice->actualBalance(), 0);
ASSERT_EQ(alice->pendingBalance(), 0);
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
2014-08-13 10:51:37 +00:00
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
alice->shutdown();
2014-06-25 17:21:42 +00:00
2014-08-13 10:51:37 +00:00
generator.getBlockRewardForAddress(address);
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(alice->actualBalance(), 0);
EXPECT_EQ(alice->pendingBalance(), TEST_BLOCK_REWARD);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, TransactionsAndTransfersAfterSend) {
2014-06-25 17:21:42 +00:00
prepareBobWallet();
prepareCarolWallet();
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(alice->getTransactionCount(), 0);
EXPECT_EQ(alice->getTransferCount(), 0);
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
//unblock Alice's money
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(alice->getTransactionCount(), 1);
2014-06-25 17:21:42 +00:00
bob->initAndGenerate("pass2");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
uint64_t fee = 100000;
int64_t amount1 = 1230000;
ASSERT_NO_FATAL_FAILURE(TransferMoney(*alice, *bob, amount1, fee, 0));
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
int64_t amount2 = 1234500;
ASSERT_NO_FATAL_FAILURE(TransferMoney(*alice, *bob, amount2, fee, 0));
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2014-06-25 17:21:42 +00:00
int64_t amount3 = 1234567;
ASSERT_NO_FATAL_FAILURE(TransferMoney(*alice, *bob, amount3, fee, 0));
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
carol->initAndGenerate("pass3");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(carolWalletObserver.get()));
int64_t amount4 = 1020304;
ASSERT_NO_FATAL_FAILURE(TransferMoney(*alice, *carol, amount4, fee, 0));
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
EXPECT_EQ(alice->getTransactionCount(), 5);
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransaction tx;
2014-06-25 17:21:42 +00:00
//Transaction with id = 0 is tested in getTransactionSuccess
ASSERT_TRUE(alice->getTransaction(1, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amount1 + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.fee, fee);
EXPECT_EQ(tx.isCoinbase, false);
EXPECT_EQ(tx.firstTransferId, 0);
EXPECT_EQ(tx.transferCount, 1);
ASSERT_TRUE(alice->getTransaction(2, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amount2 + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.fee, fee);
EXPECT_EQ(tx.isCoinbase, false);
EXPECT_EQ(tx.firstTransferId, 1);
EXPECT_EQ(tx.transferCount, 1);
ASSERT_TRUE(alice->getTransaction(3, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amount3 + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.fee, fee);
EXPECT_EQ(tx.isCoinbase, false);
EXPECT_EQ(tx.firstTransferId, 2);
EXPECT_EQ(tx.transferCount, 1);
ASSERT_TRUE(alice->getTransaction(4, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amount4 + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.fee, fee);
EXPECT_EQ(tx.isCoinbase, false);
EXPECT_EQ(tx.firstTransferId, 3);
EXPECT_EQ(tx.transferCount, 1);
//Now checking transfers
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
2014-06-25 17:21:42 +00:00
ASSERT_TRUE(alice->getTransfer(0, tr));
EXPECT_EQ(tr.amount, amount1);
EXPECT_EQ(tr.address, bob->getAddress());
ASSERT_TRUE(alice->getTransfer(1, tr));
EXPECT_EQ(tr.amount, amount2);
EXPECT_EQ(tr.address, bob->getAddress());
ASSERT_TRUE(alice->getTransfer(2, tr));
EXPECT_EQ(tr.amount, amount3);
EXPECT_EQ(tr.address, bob->getAddress());
ASSERT_TRUE(alice->getTransfer(3, tr));
EXPECT_EQ(tr.amount, amount4);
EXPECT_EQ(tr.address, carol->getAddress());
EXPECT_EQ(alice->findTransactionByTransferId(0), 1);
EXPECT_EQ(alice->findTransactionByTransferId(1), 2);
EXPECT_EQ(alice->findTransactionByTransferId(2), 3);
EXPECT_EQ(alice->findTransactionByTransferId(3), 4);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, saveAndLoadCacheDetails) {
2014-06-25 17:21:42 +00:00
prepareBobWallet();
prepareCarolWallet();
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
//unblock Alice's money
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
bob->initAndGenerate("pass2");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
carol->initAndGenerate("pass3");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(carolWalletObserver.get()));
uint64_t fee = 1000000;
int64_t amount1 = 1234567;
int64_t amount2 = 1020304;
int64_t amount3 = 2030405;
2015-07-30 15:22:07 +00:00
std::vector<CryptoNote::WalletLegacyTransfer> trs;
CryptoNote::WalletLegacyTransfer tr;
2014-06-25 17:21:42 +00:00
tr.address = bob->getAddress();
tr.amount = amount1;
trs.push_back(tr);
tr.address = bob->getAddress();
tr.amount = amount2;
trs.push_back(tr);
alice->sendTransaction(trs, fee, "", 0, 0);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
trs.clear();
tr.address = carol->getAddress();
tr.amount = amount3;
trs.push_back(tr);
alice->sendTransaction(trs, fee, "", 0, 0);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
std::stringstream archive;
alice->save(archive, true, true);
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
auto prevActualBalance = alice->actualBalance();
auto prevPendingBalance = alice->pendingBalance();
2014-06-25 17:21:42 +00:00
alice->shutdown();
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
std::error_code ec;
WaitWalletLoad(aliceWalletObserver.get(), ec);
ASSERT_FALSE(ec);
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(alice->getTransactionCount(), 3);
ASSERT_EQ(alice->getTransferCount(), 3);
EXPECT_EQ(prevActualBalance, alice->actualBalance());
EXPECT_EQ(prevPendingBalance, alice->pendingBalance());
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransaction tx;
2014-06-25 17:21:42 +00:00
ASSERT_TRUE(alice->getTransaction(1, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amount1 + amount2 + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.fee, fee);
EXPECT_EQ(tx.firstTransferId, 0);
EXPECT_EQ(tx.transferCount, 2);
ASSERT_TRUE(alice->getTransaction(2, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amount3 + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.fee, fee);
EXPECT_EQ(tx.firstTransferId, 2);
EXPECT_EQ(tx.transferCount, 1);
ASSERT_TRUE(alice->getTransfer(0, tr));
EXPECT_EQ(tr.address, bob->getAddress());
EXPECT_EQ(tr.amount, amount1);
ASSERT_TRUE(alice->getTransfer(1, tr));
EXPECT_EQ(tr.address, bob->getAddress());
EXPECT_EQ(tr.amount, amount2);
ASSERT_TRUE(alice->getTransfer(2, tr));
EXPECT_EQ(tr.address, carol->getAddress());
EXPECT_EQ(tr.amount, amount3);
EXPECT_EQ(alice->findTransactionByTransferId(0), 1);
EXPECT_EQ(alice->findTransactionByTransferId(1), 1);
EXPECT_EQ(alice->findTransactionByTransferId(2), 2);
alice->shutdown();
carol->shutdown();
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, sendMoneySuccessNoMixin) {
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(TestSendMoney(10000000, 1000000, 0));
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, sendMoneySuccessWithMixin) {
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(TestSendMoney(10000000, 1000000, 3));
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, getTransactionSuccess) {
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransaction tx;
2014-06-25 17:21:42 +00:00
ASSERT_EQ(alice->getTransactionCount(), 1);
ASSERT_TRUE(alice->getTransaction(0, tx));
2015-07-30 15:22:07 +00:00
EXPECT_EQ(tx.firstTransferId, CryptoNote::WALLET_LEGACY_INVALID_TRANSFER_ID);
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.transferCount, 0);
EXPECT_EQ(tx.totalAmount, TEST_BLOCK_REWARD);
EXPECT_EQ(tx.fee, 0);
EXPECT_EQ(tx.isCoinbase, false);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, getTransactionFailure) {
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransaction tx;
2014-06-25 17:21:42 +00:00
ASSERT_EQ(alice->getTransactionCount(), 0);
ASSERT_FALSE(alice->getTransaction(0, tx));
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, useNotInitializedObject) {
2014-06-25 17:21:42 +00:00
EXPECT_THROW(alice->pendingBalance(), std::system_error);
EXPECT_THROW(alice->actualBalance(), std::system_error);
EXPECT_THROW(alice->getTransactionCount(), std::system_error);
EXPECT_THROW(alice->getTransferCount(), std::system_error);
EXPECT_THROW(alice->getAddress(), std::system_error);
std::stringstream archive;
EXPECT_THROW(alice->save(archive, true, true), std::system_error);
EXPECT_THROW(alice->findTransactionByTransferId(1), std::system_error);
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransaction tx;
CryptoNote::WalletLegacyTransfer tr;
2014-06-25 17:21:42 +00:00
EXPECT_THROW(alice->getTransaction(1, tx), std::system_error);
EXPECT_THROW(alice->getTransfer(2, tr), std::system_error);
tr.address = "lslslslslslsls";
tr.amount = 1000000;
EXPECT_THROW(alice->sendTransaction(tr, 300201), std::system_error);
2015-07-30 15:22:07 +00:00
std::vector<CryptoNote::WalletLegacyTransfer> trs;
2014-06-25 17:21:42 +00:00
trs.push_back(tr);
EXPECT_THROW(alice->sendTransaction(trs, 329293), std::system_error);
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, sendWrongAmount) {
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
2014-06-25 17:21:42 +00:00
tr.address = "1234567890qwertasdfgzxcvbyuiophjklnm";
tr.amount = 1;
EXPECT_THROW(alice->sendTransaction(tr, 1), std::system_error);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, wrongPassword) {
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
std::stringstream archive;
alice->save(archive, true, false);
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
alice->shutdown();
prepareAliceWallet();
alice->initAndLoad(archive, "wrongpass");
std::error_code result;
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get(), result));
2015-05-27 12:08:46 +00:00
EXPECT_EQ(result.value(), CryptoNote::error::WRONG_PASSWORD);
2014-06-25 17:21:42 +00:00
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, detachBlockchain) {
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
aliceNode->startAlternativeChain(3);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(0, alice->actualBalance());
EXPECT_EQ(0, alice->pendingBalance());
2014-06-25 17:21:42 +00:00
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, saveAndLoad) {
alice->initAndGenerate("pass");
std::error_code result;
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get(), result));
ASSERT_EQ(result.value(), 0);
std::stringstream archive;
ASSERT_NO_FATAL_FAILURE(alice->save(archive));
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get(), result));
ASSERT_EQ(result.value(), 0);
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, saveAndLoadErroneousTxsCacheDetails) {
2014-06-25 17:21:42 +00:00
prepareBobWallet();
prepareCarolWallet();
std::error_code result;
2014-06-25 17:21:42 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
carol->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(carolWalletObserver.get()));
std::array<int64_t, 5> amounts;
amounts[0] = 1234567;
amounts[1] = 1345678;
amounts[2] = 1456789;
amounts[3] = 1567890;
amounts[4] = 1678901;
uint64_t fee = 10000;
ASSERT_NO_FATAL_FAILURE(performTransferWithErrorTx(amounts, fee));
std::stringstream archive;
alice->save(archive);
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get(), result));
ASSERT_EQ(result.value(), 0);
EXPECT_EQ(alice->getTransactionCount(), 2);
EXPECT_EQ(alice->getTransferCount(), 2);
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransaction tx;
2014-06-25 17:21:42 +00:00
ASSERT_TRUE(alice->getTransaction(1, tx));
EXPECT_EQ(tx.totalAmount, -static_cast<int64_t>(amounts[3] + amounts[4] + fee));
2014-06-25 17:21:42 +00:00
EXPECT_EQ(tx.firstTransferId, 0);
EXPECT_EQ(tx.transferCount, 2);
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
2014-06-25 17:21:42 +00:00
ASSERT_TRUE(alice->getTransfer(0, tr));
EXPECT_EQ(tr.amount, amounts[3]);
EXPECT_EQ(tr.address, bob->getAddress());
ASSERT_TRUE(alice->getTransfer(1, tr));
EXPECT_EQ(tr.amount, amounts[4]);
EXPECT_EQ(tr.address, carol->getAddress());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, saveAndLoadErroneousTxsCacheNoDetails) {
2014-06-25 17:21:42 +00:00
prepareBobWallet();
prepareCarolWallet();
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(GetOneBlockReward(*alice));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
2014-06-25 17:21:42 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
carol->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(carolWalletObserver.get()));
std::array<int64_t, 5> amounts;
amounts[0] = 1234567;
amounts[1] = 1345678;
amounts[2] = 1456789;
amounts[3] = 1567890;
amounts[4] = 1678901;
uint64_t fee = 10000;
ASSERT_NO_FATAL_FAILURE(performTransferWithErrorTx(amounts, fee));
std::stringstream archive;
alice->save(archive, false, true);
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
std::error_code result;
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get(), result));
ASSERT_EQ(result.value(), 0);
EXPECT_EQ(0, alice->getTransactionCount());
EXPECT_EQ(0, alice->getTransferCount());
2014-06-25 17:21:42 +00:00
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, mineSaveNoCacheNoDetailsRefresh) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
generator.getBlockRewardForAddress(address);
generator.getBlockRewardForAddress(address);
generator.getBlockRewardForAddress(address);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
std::stringstream archive;
alice->save(archive, false, false);
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
2014-06-25 17:21:42 +00:00
alice->shutdown();
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(TEST_BLOCK_REWARD * 3, alice->pendingBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, sendMoneyToMyself) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
generator.getBlockRewardForAddress(address);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
CryptoNote::TransactionId txId = TransferMoney(*alice, *alice, 100000000, 100);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
2015-05-27 12:08:46 +00:00
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(TEST_BLOCK_REWARD - 100, alice->actualBalance());
ASSERT_EQ(0, alice->pendingBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, sendSeveralTransactions) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
for (int i = 0; i < 5; ++i) {
GetOneBlockReward(*alice);
}
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
auto aliceBalance = alice->actualBalance();
uint64_t sendAmount = 100000;
uint64_t totalSentAmount = 0;
size_t transactionCount = 0;
for (int i = 0; i < 10 && alice->actualBalance() > sendAmount; ++i) {
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
tr.address = bob->getAddress();
tr.amount = sendAmount;
auto txId = alice->sendTransaction(tr, m_currency.minimumFee(), "", 1, 0);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
std::error_code sendResult;
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get(), sendResult));
ASSERT_EQ(std::error_code(), sendResult);
++transactionCount;
totalSentAmount += sendAmount;
}
generator.generateEmptyBlocks(10);
bobNode->updateObservers();
while (totalSentAmount != bob->actualBalance()) {
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
}
EXPECT_EQ(transactionCount, bob->getTransactionCount());
EXPECT_EQ(0, bob->pendingBalance());
EXPECT_EQ(totalSentAmount, bob->actualBalance());
uint64_t aliceTotalBalance = alice->actualBalance() + alice->pendingBalance();
EXPECT_EQ(aliceBalance - transactionCount * (sendAmount + m_currency.minimumFee()), aliceTotalBalance);
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, balanceAfterFailedTransaction) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
auto actualBalance = alice->actualBalance();
auto pendingBalance = alice->pendingBalance();
uint64_t send = 11000000;
uint64_t fee = m_currency.minimumFee();
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
tr.address = bob->getAddress();
tr.amount = send;
aliceNode->setNextTransactionError();
alice->sendTransaction(tr, fee, "", 1, 0);
generator.generateEmptyBlocks(1);
ASSERT_EQ(actualBalance, alice->actualBalance());
ASSERT_EQ(pendingBalance, alice->pendingBalance());
alice->shutdown();
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, checkPendingBalance) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
uint64_t startActualBalance = alice->actualBalance();
int64_t sendAmount = 304050;
uint64_t fee = m_currency.minimumFee();
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
tr.address = bob->getAddress();
tr.amount = sendAmount;
auto txId = alice->sendTransaction(tr, fee, "", 1, 0);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
std::error_code sendResult;
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get(), sendResult));
ASSERT_EQ(std::error_code(), sendResult);
uint64_t totalBalance = alice->actualBalance() + alice->pendingBalance();
ASSERT_EQ(startActualBalance - sendAmount - fee, totalBalance);
generator.generateEmptyBlocks(1);
bobNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
2015-04-08 15:08:54 +00:00
ASSERT_EQ(sendAmount, bob->actualBalance());
ASSERT_EQ(0, bob->pendingBalance());
alice->shutdown();
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, checkChange) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
uint64_t banknote = 1000000000;
uint64_t sendAmount = 50000;
uint64_t fee = m_currency.minimumFee();
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
generator.getSingleOutputTransaction(address, banknote);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
tr.address = bob->getAddress();
tr.amount = sendAmount;
auto txId = alice->sendTransaction(tr, fee, "", 1, 0);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
std::error_code sendResult;
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get(), sendResult));
ASSERT_EQ(std::error_code(), sendResult);
EXPECT_EQ(0, alice->actualBalance());
EXPECT_EQ(banknote - sendAmount - fee, alice->pendingBalance());
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, checkBalanceAfterSend) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
uint64_t banknote = 1000000000;
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
//Once wallet takes outputs in random fashion we don't know for sure which outputs will be taken.
//In this case we generate controllable set of outs.
generator.getSingleOutputTransaction(address, banknote);
generator.getSingleOutputTransaction(address, banknote);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
const uint64_t sendAmount = 10000000;
const uint64_t fee = 100;
CryptoNote::TransactionId txId = TransferMoney(*alice, *alice, sendAmount, fee);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
ASSERT_EQ(banknote, alice->actualBalance());
ASSERT_EQ(banknote - sendAmount - fee, alice->pendingBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, moneyInPoolDontAffectActualBalance) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
uint64_t banknote = 1000000000;
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
generator.getSingleOutputTransaction(address, banknote);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
const uint64_t sendAmount = 10000000;
const uint64_t fee = 100;
aliceNode->setNextTransactionToPool();
CryptoNote::TransactionId txId = TransferMoney(*alice, *bob, sendAmount, fee);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(0, alice->actualBalance());
EXPECT_EQ(banknote - sendAmount - fee, alice->pendingBalance());
alice->shutdown();
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, balanceAfterTransactionsPlacedInBlockchain) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
uint64_t banknote = 1000000000;
2015-05-27 12:08:46 +00:00
CryptoNote::AccountPublicAddress address;
ASSERT_TRUE(m_currency.parseAccountAddressString(alice->getAddress(), address));
generator.getSingleOutputTransaction(address, banknote);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
const uint64_t sendAmount = 10000000;
const uint64_t fee = 100;
aliceNode->setNextTransactionToPool();
CryptoNote::TransactionId txId = TransferMoney(*alice, *bob, sendAmount, fee);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
aliceNode->includeTransactionsFromPoolToBlock();
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
EXPECT_EQ(banknote - sendAmount - fee, alice->actualBalance());
EXPECT_EQ(0, alice->pendingBalance());
2014-06-25 17:21:42 +00:00
alice->shutdown();
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, checkMyMoneyInTxPool) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
uint64_t sendAmount = 8821902;
uint64_t fee = 10000;
aliceNode->setNextTransactionToPool();
CryptoNote::TransactionId txId = TransferMoney(*alice, *bob, sendAmount, fee);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
bobNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
EXPECT_EQ(0, bob->actualBalance());
EXPECT_EQ(sendAmount, bob->pendingBalance());
alice->shutdown();
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, initWithKeys) {
CryptoNote::AccountKeys accountKeys;
2015-07-30 15:22:07 +00:00
Crypto::generate_keys(accountKeys.address.spendPublicKey, accountKeys.spendSecretKey);
Crypto::generate_keys(accountKeys.address.viewPublicKey, accountKeys.viewSecretKey);
alice->initWithKeys(accountKeys, "pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::AccountKeys keys;
alice->getAccountKeys(keys);
2015-07-30 15:22:07 +00:00
EXPECT_EQ(accountKeys.address.spendPublicKey, keys.address.spendPublicKey);
EXPECT_EQ(accountKeys.spendSecretKey, keys.spendSecretKey);
EXPECT_EQ(accountKeys.address.viewPublicKey, keys.address.viewPublicKey);
EXPECT_EQ(accountKeys.viewSecretKey, keys.viewSecretKey);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, deleteTxFromPool) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
uint64_t sendAmount = 9748291;
uint64_t fee = 10000;
aliceNode->setNextTransactionToPool();
CryptoNote::TransactionId txId = TransferMoney(*alice, *bob, sendAmount, fee);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
alice->shutdown();
bobNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
generator.clearTxPool();
bobNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
EXPECT_EQ(0, bob->actualBalance());
EXPECT_EQ(0, bob->pendingBalance());
bob->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, sendAfterFailedTransaction) {
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::WalletLegacyTransfer tr;
tr.amount = 100000;
tr.address = "wrong_address";
EXPECT_THROW(alice->sendTransaction(tr, 1000, "", 2, 0), std::system_error);
CryptoNote::TransactionId txId = TransferMoney(*alice, *alice, 100000, 100);
2015-07-30 15:22:07 +00:00
ASSERT_NE(txId, CryptoNote::WALLET_LEGACY_INVALID_TRANSACTION_ID);
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, DISABLED_loadingBrokenCache) {
alice->initAndGenerate("pass");
std::error_code result;
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(result.value(), 0);
std::stringstream archive;
ASSERT_NO_FATAL_FAILURE(alice->save(archive, false, true));
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
size_t sizeWithEmptyCache = archive.str().size();
for (size_t i = 0; i < 3; ++i) {
GetOneBlockReward(*alice);
}
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
archive.str("");
archive.clear();
ASSERT_NO_FATAL_FAILURE(alice->save(archive, false, true));
ASSERT_NO_FATAL_FAILURE(WaitWalletSave(aliceWalletObserver.get()));
std::string state = archive.str();
for (size_t i = sizeWithEmptyCache; i < state.size(); ++i) {
state[i] = '\xff';
}
archive.str(state);
prepareAliceWallet();
alice->initAndLoad(archive, "pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletLoad(aliceWalletObserver.get(), result));
ASSERT_EQ(result.value(), 0);
2014-06-25 17:21:42 +00:00
}
2015-07-09 14:52:47 +00:00
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, outcommingExternalTransactionTotalAmount) {
class ExternalTxChecker : public CryptoNote::IWalletLegacyObserver {
public:
ExternalTxChecker(CryptoNote::WalletLegacy& wallet) : wallet(wallet), totalAmount(std::numeric_limits<int64_t>::max()) {
}
virtual void externalTransactionCreated(CryptoNote::TransactionId transactionId) override {
CryptoNote::WalletLegacyTransaction txInfo;
ASSERT_TRUE(wallet.getTransaction(transactionId, txInfo));
totalAmount = txInfo.totalAmount;
}
CryptoNote::WalletLegacy& wallet;
int64_t totalAmount;
};
std::stringstream walletData;
alice->initAndGenerate("pass");
WaitWalletSync(aliceWalletObserver.get());
alice->save(walletData, false, false);
WaitWalletSave(aliceWalletObserver.get());
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
WaitWalletSync(aliceWalletObserver.get());
prepareBobWallet();
bob->initAndGenerate("pass2");
WaitWalletSync(bobWalletObserver.get());
uint64_t sent = 10000000;
uint64_t fee = 1000;
CryptoNote::WalletLegacyTransfer tr;
tr.amount = sent;
tr.address = bob->getAddress();
alice->sendTransaction(tr, fee);
WaitWalletSend(aliceWalletObserver.get());
bob->shutdown();
alice->shutdown();
CryptoNote::WalletLegacy wallet(m_currency, *aliceNode);
ExternalTxChecker externalTransactionObserver(wallet);
TrivialWalletObserver walletObserver;
wallet.addObserver(&externalTransactionObserver);
wallet.addObserver(&walletObserver);
wallet.initAndLoad(walletData, "pass");
WaitWalletSync(&walletObserver);
2015-10-01 15:27:18 +00:00
ASSERT_EQ(-static_cast<int64_t>(sent + fee), externalTransactionObserver.totalAmount);
2015-07-30 15:22:07 +00:00
wallet.shutdown();
}
TEST_F(WalletLegacyApi, shutdownAllowsInitializeWalletWithTheSameKeys) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::AccountKeys accountKeys;
2015-07-09 14:52:47 +00:00
alice->getAccountKeys(accountKeys);
alice->shutdown();
alice->initWithKeys(accountKeys, "pass");
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(1, alice->getTransactionCount());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, shutdownAllowsInitializeWalletWithDifferentKeys) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->shutdown();
alice->initAndGenerate("pass");
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(0, alice->getTransactionCount());
alice->shutdown();
}
namespace {
2015-07-30 15:22:07 +00:00
class WalletSynchronizationProgressUpdatedObserver : public CryptoNote::IWalletLegacyObserver {
2015-07-09 14:52:47 +00:00
public:
2015-07-30 15:22:07 +00:00
virtual void synchronizationProgressUpdated(uint32_t current, uint32_t /*total*/) override {
2015-07-09 14:52:47 +00:00
m_current = current;
}
uint64_t m_current = 0;
};
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, shutdownDoesNotRemoveObservers) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
WalletSynchronizationProgressUpdatedObserver observer;
CryptoNote::WalletHelper::IWalletRemoveObserverGuard observerGuard(*alice, observer);
alice->shutdown();
observer.m_current = 0;
alice->initAndGenerate("pass");
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(5, observer.m_current);
observerGuard.removeObserver();
alice->shutdown();
}
namespace {
2015-07-30 15:22:07 +00:00
class WalletTransactionEventCounter : public CryptoNote::IWalletLegacyObserver {
2015-07-09 14:52:47 +00:00
public:
virtual void externalTransactionCreated(CryptoNote::TransactionId /*transactionId*/) override {
++m_count;
}
virtual void transactionUpdated(CryptoNote::TransactionId /*transactionId*/) override {
++m_count;
}
size_t m_count = 0;
};
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, afterShutdownAndInitWalletDoesNotSendNotificationsRelatedToOldAddress) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
std::string aliceAddress1 = alice->getAddress();
2015-07-30 15:22:07 +00:00
CryptoNote::AccountKeys accountKeys1;
2015-07-09 14:52:47 +00:00
alice->getAccountKeys(accountKeys1);
alice->shutdown();
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
std::string aliceAddress2 = alice->getAddress();
alice->shutdown();
alice->initWithKeys(accountKeys1, "pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
WalletTransactionEventCounter observer;
CryptoNote::WalletHelper::IWalletRemoveObserverGuard observerGuard(*alice, observer);
prepareBobWallet();
bob->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
GetOneBlockReward(*bob);
generator.generateEmptyBlocks(10);
bobNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
2015-07-30 15:22:07 +00:00
std::vector<CryptoNote::WalletLegacyTransfer> transfers;
2015-07-09 14:52:47 +00:00
transfers.push_back({ aliceAddress1, TEST_BLOCK_REWARD / 10 });
transfers.push_back({ aliceAddress2, TEST_BLOCK_REWARD / 5 });
bob->sendTransaction(transfers, m_currency.minimumFee());
std::error_code sendResult;
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(bobWalletObserver.get(), sendResult));
generator.generateEmptyBlocks(1);
bobNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(bobWalletObserver.get()));
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(1, observer.m_count);
observerGuard.removeObserver();
bob->shutdown();
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetDoesNotChangeAddress) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
auto expectedAddress = alice->getAddress();
alice->reset();
ASSERT_EQ(expectedAddress, alice->getAddress());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetDoesNotChangeAccountKeys) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
2015-07-30 15:22:07 +00:00
CryptoNote::AccountKeys expectedAccountKeys;
2015-07-09 14:52:47 +00:00
alice->getAccountKeys(expectedAccountKeys);
alice->reset();
2015-07-30 15:22:07 +00:00
CryptoNote::AccountKeys actualAccountKeys;
2015-07-09 14:52:47 +00:00
alice->getAccountKeys(actualAccountKeys);
2015-07-30 15:22:07 +00:00
ASSERT_EQ(expectedAccountKeys.address, actualAccountKeys.address);
2015-07-09 14:52:47 +00:00
ASSERT_EQ(expectedAccountKeys.spendSecretKey, actualAccountKeys.spendSecretKey);
ASSERT_EQ(expectedAccountKeys.viewSecretKey, actualAccountKeys.viewSecretKey);
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetDoesNotRemoveObservers) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
WalletSynchronizationProgressUpdatedObserver observer;
CryptoNote::WalletHelper::IWalletRemoveObserverGuard observerGuard(*alice, observer);
alice->reset();
observer.m_current = 0;
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(5, observer.m_current);
observerGuard.removeObserver();
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetDoesNotChangePassword) {
2015-07-09 14:52:47 +00:00
std::string password = "password";
std::string newPassword = "new_password";
alice->initAndGenerate(password);
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->reset();
ASSERT_TRUE(static_cast<bool>(alice->changePassword(newPassword, password)));
ASSERT_FALSE(static_cast<bool>(alice->changePassword(password, newPassword)));
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetClearsPendingBalance) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(TEST_BLOCK_REWARD, alice->pendingBalance());
alice->reset();
ASSERT_EQ(0, alice->pendingBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetClearsActualBalance) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(TEST_BLOCK_REWARD, alice->actualBalance());
alice->reset();
ASSERT_EQ(0, alice->actualBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetClearsTransactionHistory) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(1, alice->getTransactionCount());
alice->reset();
ASSERT_EQ(0, alice->getTransactionCount());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetClearsTransfersHistory) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->sendTransaction({ alice->getAddress(), 100 }, m_currency.minimumFee());
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
ASSERT_EQ(1, alice->getTransferCount());
alice->reset();
ASSERT_EQ(0, alice->getTransferCount());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetAndSyncRestorePendingBalance) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->reset();
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(TEST_BLOCK_REWARD, alice->pendingBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetAndSyncRestoreActualBalance) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->reset();
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(TEST_BLOCK_REWARD, alice->actualBalance());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetAndSyncRestoreTransactions) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->reset();
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(1, alice->getTransactionCount());
alice->shutdown();
}
2015-07-30 15:22:07 +00:00
TEST_F(WalletLegacyApi, resetAndSyncDoNotRestoreTransfers) {
2015-07-09 14:52:47 +00:00
alice->initAndGenerate("pass");
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
GetOneBlockReward(*alice);
generator.generateEmptyBlocks(10);
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
alice->sendTransaction({ alice->getAddress(), 100 }, m_currency.minimumFee());
ASSERT_NO_FATAL_FAILURE(WaitWalletSend(aliceWalletObserver.get()));
alice->reset();
aliceNode->updateObservers();
ASSERT_NO_FATAL_FAILURE(WaitWalletSync(aliceWalletObserver.get()));
ASSERT_EQ(0, alice->getTransferCount());
alice->shutdown();
}
2015-08-05 13:09:05 +00:00
void generateWallet(CryptoNote::IWalletLegacy& wallet, TrivialWalletObserver& observer, const std::string& pass) {
wallet.initAndGenerate(pass);
WaitWalletSync(&observer);
}
TEST_F(WalletLegacyApi, outdatedUnconfirmedTransactionDeletedOnNewBlock) {
const uint64_t TRANSACTION_MEMPOOL_TIME = 1;
CryptoNote::Currency currency(CryptoNote::CurrencyBuilder(m_logger).mempoolTxLiveTime(TRANSACTION_MEMPOOL_TIME).currency());
TestBlockchainGenerator blockchainGenerator(currency);
INodeTrivialRefreshStub node(blockchainGenerator);
CryptoNote::WalletLegacy wallet(currency, node);
TrivialWalletObserver walletObserver;
wallet.addObserver(&walletObserver);
wallet.initAndGenerate("pass");
WaitWalletSync(&walletObserver);
GetOneBlockRewardAndUnlock(wallet, walletObserver, node, currency, blockchainGenerator);
const std::string ADDRESS = "2634US2FAz86jZT73YmM8u5GPCknT2Wxj8bUCKivYKpThFhF2xsjygMGxbxZzM42zXhKUhym6Yy6qHHgkuWtruqiGkDpX6m";
node.setNextTransactionToPool();
auto id = wallet.sendTransaction({ADDRESS, static_cast<int64_t>(TEST_BLOCK_REWARD - m_currency.minimumFee())}, m_currency.minimumFee());
WaitWalletSend(&walletObserver);
node.cleanTransactionPool();
std::this_thread::sleep_for(std::chrono::seconds(TRANSACTION_MEMPOOL_TIME));
blockchainGenerator.generateEmptyBlocks(1);
node.updateObservers();
WaitWalletSync(&walletObserver);
ASSERT_EQ(TEST_BLOCK_REWARD, wallet.actualBalance());
CryptoNote::WalletLegacyTransaction transaction;
ASSERT_TRUE(wallet.getTransaction(id, transaction));
EXPECT_EQ(CryptoNote::WalletLegacyTransactionState::Deleted, transaction.state);
wallet.removeObserver(&walletObserver);
wallet.shutdown();
}
TEST_F(WalletLegacyApi, outdatedUnconfirmedTransactionDeletedOnLoad) {
const uint64_t TRANSACTION_MEMPOOL_TIME = 1;
CryptoNote::Currency currency(CryptoNote::CurrencyBuilder(m_logger).mempoolTxLiveTime(TRANSACTION_MEMPOOL_TIME).currency());
TestBlockchainGenerator blockchainGenerator(currency);
INodeTrivialRefreshStub node(blockchainGenerator);
CryptoNote::WalletLegacy wallet(currency, node);
TrivialWalletObserver walletObserver;
wallet.addObserver(&walletObserver);
wallet.initAndGenerate("pass");
WaitWalletSync(&walletObserver);
GetOneBlockRewardAndUnlock(wallet, walletObserver, node, currency, blockchainGenerator);
const std::string ADDRESS = "2634US2FAz86jZT73YmM8u5GPCknT2Wxj8bUCKivYKpThFhF2xsjygMGxbxZzM42zXhKUhym6Yy6qHHgkuWtruqiGkDpX6m";
node.setNextTransactionToPool();
auto id = wallet.sendTransaction({ADDRESS, static_cast<int64_t>(TEST_BLOCK_REWARD - m_currency.minimumFee())}, m_currency.minimumFee());
WaitWalletSend(&walletObserver);
node.cleanTransactionPool();
std::stringstream data;
wallet.save(data);
WaitWalletSave(&walletObserver);
wallet.shutdown();
std::this_thread::sleep_for(std::chrono::seconds(TRANSACTION_MEMPOOL_TIME));
wallet.initAndLoad(data, "pass");
WaitWalletSync(&walletObserver);
ASSERT_EQ(TEST_BLOCK_REWARD, wallet.actualBalance());
CryptoNote::WalletLegacyTransaction transaction;
ASSERT_TRUE(wallet.getTransaction(id, transaction));
EXPECT_EQ(CryptoNote::WalletLegacyTransactionState::Deleted, transaction.state);
wallet.removeObserver(&walletObserver);
wallet.shutdown();
}
2015-08-11 14:33:19 +00:00
TEST_F(WalletLegacyApi, walletLoadsNullSpendSecretKey) {
CryptoNote::AccountKeys accountKeys;
Crypto::generate_keys(accountKeys.address.spendPublicKey, accountKeys.spendSecretKey);
Crypto::generate_keys(accountKeys.address.viewPublicKey, accountKeys.viewSecretKey);
accountKeys.spendSecretKey = CryptoNote::NULL_SECRET_KEY;
alice->initWithKeys(accountKeys, "pass");
WaitWalletSync(aliceWalletObserver.get());
std::stringstream data;
alice->save(data);
WaitWalletSave(aliceWalletObserver.get());
alice->shutdown();
alice->initAndLoad(data, "pass");
WaitWalletSync(aliceWalletObserver.get());
ASSERT_EQ(std::error_code(), aliceWalletObserver->loadResult);
alice->shutdown();
}