2015-05-27 12:08:46 +00:00
|
|
|
// Copyright (c) 2012-2015, The CryptoNote developers, The Bytecoin developers
|
2015-04-06 16:13:07 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "Transfers/BlockchainSynchronizer.h"
|
|
|
|
#include "Transfers/TransfersConsumer.h"
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
#include "CryptoNoteCore/TransactionApi.h"
|
|
|
|
#include "CryptoNoteCore/CryptoNoteFormatUtils.h"
|
|
|
|
#include "CryptoNoteCore/CryptoNoteTools.h"
|
2015-05-27 12:08:46 +00:00
|
|
|
#include "Logging/ConsoleLogger.h"
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
#include "INodeStubs.h"
|
|
|
|
#include "TestBlockchainGenerator.h"
|
|
|
|
#include "EventWaiter.h"
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
using namespace Crypto;
|
2015-04-06 16:13:07 +00:00
|
|
|
using namespace CryptoNote;
|
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
namespace {
|
|
|
|
Transaction createTx(ITransactionReader& tx) {
|
|
|
|
Transaction outTx;
|
|
|
|
fromBinaryArray(outTx, tx.getTransactionData());
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
return outTx;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class INodeNonTrivialRefreshStub : public INodeTrivialRefreshStub {
|
|
|
|
public:
|
|
|
|
|
|
|
|
INodeNonTrivialRefreshStub(TestBlockchainGenerator& generator) : INodeTrivialRefreshStub(generator), blocksWasQueried(false), poolWasQueried(false) {}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void queryBlocks(std::vector<Hash>&& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks, uint32_t& startHeight, const Callback& callback) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
blocksWasQueried = true;
|
|
|
|
INodeTrivialRefreshStub::queryBlocks(std::move(knownBlockIds), timestamp, newBlocks, startHeight, callback);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void getPoolSymmetricDifference(std::vector<Hash>&& known_pool_tx_ids, Hash known_block_id, bool& is_bc_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted_tx_ids, const Callback& callback) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
poolWasQueried = true;
|
|
|
|
INodeTrivialRefreshStub::getPoolSymmetricDifference(std::move(known_pool_tx_ids), known_block_id, is_bc_actual, new_txs, deleted_tx_ids, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void notifyAboutPool() {
|
2015-07-30 15:22:07 +00:00
|
|
|
observerManager.notify(&INodeObserver::poolChanged);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool blocksWasQueried;
|
|
|
|
bool poolWasQueried;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class INodeFunctorialStub : public INodeNonTrivialRefreshStub {
|
|
|
|
public:
|
|
|
|
|
|
|
|
INodeFunctorialStub(TestBlockchainGenerator& generator)
|
|
|
|
: INodeNonTrivialRefreshStub(generator)
|
2015-07-30 15:22:07 +00:00
|
|
|
, queryBlocksFunctor([](const std::vector<Hash>&, uint64_t, std::vector<BlockShortEntry>&, uint32_t&, const Callback&) -> bool { return true; })
|
|
|
|
, getPoolSymmetricDifferenceFunctor([](const std::vector<Hash>&, Hash, bool&, std::vector<std::unique_ptr<ITransactionReader>>&, std::vector<Hash>&, const Callback&)->bool {return true; }) {
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void queryBlocks(std::vector<Hash>&& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks,
|
|
|
|
uint32_t& startHeight, const Callback& callback) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
if (queryBlocksFunctor(knownBlockIds, timestamp, newBlocks, startHeight, callback)) {
|
|
|
|
INodeNonTrivialRefreshStub::queryBlocks(std::move(knownBlockIds), timestamp, newBlocks, startHeight, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void getPoolSymmetricDifference(std::vector<Hash>&& known_pool_tx_ids, Hash known_block_id, bool& is_bc_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted_tx_ids, const Callback& callback) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
if (getPoolSymmetricDifferenceFunctor(known_pool_tx_ids, known_block_id, is_bc_actual, new_txs, deleted_tx_ids, callback)) {
|
|
|
|
INodeNonTrivialRefreshStub::getPoolSymmetricDifference(std::move(known_pool_tx_ids), known_block_id, is_bc_actual, new_txs, deleted_tx_ids, callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::function<bool(const std::vector<Hash>&, uint64_t, std::vector<BlockShortEntry>&, uint32_t&, const Callback&)> queryBlocksFunctor;
|
|
|
|
std::function<bool(const std::vector<Hash>&, Hash, bool&, std::vector<std::unique_ptr<ITransactionReader>>&, std::vector<Hash>&, const Callback&)> getPoolSymmetricDifferenceFunctor;
|
2015-04-06 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class IBlockchainSynchronizerTrivialObserver : public IBlockchainSynchronizerObserver {
|
|
|
|
public:
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void synchronizationProgressUpdated(uint32_t current, uint32_t total) override { m_current = current; m_total = total; }
|
2015-04-06 16:13:07 +00:00
|
|
|
virtual void synchronizationCompleted(std::error_code result) override { completionResult = result; }
|
|
|
|
|
|
|
|
std::error_code completionResult;
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t m_current;
|
|
|
|
uint32_t m_total;
|
2015-04-06 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class IBlockchainSynchronizerFunctorialObserver : public IBlockchainSynchronizerObserver {
|
|
|
|
public:
|
2015-07-30 15:22:07 +00:00
|
|
|
IBlockchainSynchronizerFunctorialObserver() : updFunc([](uint32_t, uint32_t) {}), syncFunc([](std::error_code) {}) {
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void synchronizationProgressUpdated(uint32_t current, uint32_t total) override { updFunc(current, total); }
|
2015-04-06 16:13:07 +00:00
|
|
|
virtual void synchronizationCompleted(std::error_code result) override { syncFunc(result); }
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::function<void(uint32_t, uint32_t)> updFunc;
|
2015-04-06 16:13:07 +00:00
|
|
|
std::function<void(std::error_code)> syncFunc;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ConsumerStub : public IBlockchainConsumer {
|
|
|
|
public:
|
2015-07-30 15:22:07 +00:00
|
|
|
ConsumerStub(const Hash& genesisBlockHash) {
|
2015-04-06 16:13:07 +00:00
|
|
|
m_blockchain.push_back(genesisBlockHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual SynchronizationStart getSyncStart() override {
|
|
|
|
SynchronizationStart start = { 0, 0 };
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void onBlockchainDetach(uint32_t height) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
assert(height < m_blockchain.size());
|
|
|
|
m_blockchain.resize(height);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual bool onNewBlocks(const CompleteBlock* blocks, uint32_t startHeight, uint32_t count) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
//assert(m_blockchain.size() == startHeight);
|
|
|
|
while (count--) {
|
|
|
|
m_blockchain.push_back(blocks->blockHash);
|
|
|
|
++blocks;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
const std::vector<Hash>& getBlockchain() const {
|
2015-04-06 16:13:07 +00:00
|
|
|
return m_blockchain;
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void getKnownPoolTxIds(std::vector<Hash>& ids) override {
|
|
|
|
ids.assign(m_pool.begin(), m_pool.end());
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual std::error_code onPoolUpdated(const std::vector<std::unique_ptr<ITransactionReader>>& addedTransactions, const std::vector<Hash>& deletedTransactions) override {
|
|
|
|
for (const auto& tx: addedTransactions) {
|
|
|
|
Hash hash = tx->getTransactionHash();
|
|
|
|
m_pool.push_back(reinterpret_cast<const Hash&>(hash));
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
for (auto& hash : deletedTransactions) {
|
2015-07-30 15:22:07 +00:00
|
|
|
auto pos = std::find(m_pool.begin(), m_pool.end(), hash);
|
2015-04-06 16:13:07 +00:00
|
|
|
if (pos != m_pool.end()) {
|
|
|
|
m_pool.erase(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> m_pool;
|
|
|
|
std::vector<Hash> m_blockchain;
|
2015-04-06 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BcSTest : public ::testing::Test, public IBlockchainSynchronizerObserver {
|
|
|
|
public:
|
|
|
|
BcSTest() :
|
2015-07-30 15:22:07 +00:00
|
|
|
m_currency(CurrencyBuilder(m_logger).currency()),
|
2015-04-06 16:13:07 +00:00
|
|
|
generator(m_currency),
|
|
|
|
m_node(generator),
|
|
|
|
m_sync(m_node, m_currency.genesisBlockHash()) {
|
|
|
|
m_node.setGetNewBlocksLimit(5); // sync max 5 blocks per request
|
|
|
|
}
|
|
|
|
|
|
|
|
void addConsumers(size_t count = 1) {
|
|
|
|
while (count--) {
|
|
|
|
std::shared_ptr<ConsumerStub> stub(new ConsumerStub(m_currency.genesisBlockHash()));
|
|
|
|
m_sync.addConsumer(stub.get());
|
|
|
|
m_consumers.push_back(stub);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkSyncedBlockchains() {
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> generatorBlockchain;
|
2015-04-06 16:13:07 +00:00
|
|
|
std::transform(
|
|
|
|
generator.getBlockchain().begin(),
|
|
|
|
generator.getBlockchain().end(),
|
|
|
|
std::back_inserter(generatorBlockchain),
|
2015-07-30 15:22:07 +00:00
|
|
|
[](const Block& b) { return get_block_hash(b); });
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
for (const auto& consumer : m_consumers) {
|
|
|
|
ASSERT_EQ(consumer->getBlockchain(), generatorBlockchain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void startSync() {
|
|
|
|
syncCompleted = std::promise<std::error_code>();
|
|
|
|
syncCompletedFuture = syncCompleted.get_future();
|
|
|
|
m_sync.addObserver(this);
|
|
|
|
m_sync.start();
|
|
|
|
syncCompletedFuture.get();
|
|
|
|
m_sync.removeObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void refreshSync() {
|
|
|
|
syncCompleted = std::promise<std::error_code>();
|
|
|
|
syncCompletedFuture = syncCompleted.get_future();
|
|
|
|
m_sync.addObserver(this);
|
|
|
|
m_node.updateObservers();
|
|
|
|
syncCompletedFuture.get();
|
|
|
|
m_sync.removeObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void synchronizationCompleted(std::error_code result) override {
|
|
|
|
decltype(syncCompleted) detachedPromise = std::move(syncCompleted);
|
|
|
|
detachedPromise.set_value(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2015-05-27 12:08:46 +00:00
|
|
|
Logging::ConsoleLogger m_logger;
|
2015-07-30 15:22:07 +00:00
|
|
|
Currency m_currency;
|
2015-04-06 16:13:07 +00:00
|
|
|
TestBlockchainGenerator generator;
|
|
|
|
|
|
|
|
INodeFunctorialStub m_node;
|
|
|
|
BlockchainSynchronizer m_sync;
|
|
|
|
std::vector<std::shared_ptr<ConsumerStub>> m_consumers;
|
|
|
|
|
|
|
|
std::promise<std::error_code> syncCompleted;
|
|
|
|
std::future<std::error_code> syncCompletedFuture;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BcSTest, addConsumerStopped) {
|
|
|
|
ASSERT_NO_THROW(addConsumers());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, addConsumerStartStop) {
|
|
|
|
addConsumers();
|
|
|
|
m_sync.start();
|
|
|
|
m_sync.stop();
|
|
|
|
ASSERT_NO_THROW(addConsumers());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, addConsumerStartThrow) {
|
|
|
|
addConsumers();
|
|
|
|
m_sync.start();
|
|
|
|
ASSERT_ANY_THROW(addConsumers());
|
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, removeConsumerWhichIsNotExist) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
ASSERT_FALSE(m_sync.removeConsumer(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, removeConsumerStartThrow) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
ASSERT_ANY_THROW(m_sync.removeConsumer(&c));
|
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, removeConsumerStopped) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
EXPECT_EQ(true, m_sync.removeConsumer(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, removeConsumerStartStop) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
m_sync.stop();
|
|
|
|
EXPECT_EQ(true, m_sync.removeConsumer(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, getConsumerStateWhichIsNotExist) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
EXPECT_EQ(nullptr, m_sync.getConsumerState(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, getConsumerStateStartThrow) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
ASSERT_ANY_THROW(m_sync.getConsumerState(&c));
|
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, getConsumerStateStopped) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
EXPECT_NE(nullptr, m_sync.getConsumerState(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, getConsumerStateStartStop) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
m_sync.stop();
|
|
|
|
EXPECT_NE(nullptr, m_sync.getConsumerState(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, startWithoutConsumersThrow) {
|
|
|
|
ASSERT_ANY_THROW(m_sync.start());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, doubleStart) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
ASSERT_ANY_THROW(m_sync.start());
|
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, startAfterStop) {
|
|
|
|
addConsumers();
|
|
|
|
m_sync.start();
|
|
|
|
m_sync.stop();
|
|
|
|
ASSERT_NO_THROW(m_sync.start());
|
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, startAndObserve) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
ASSERT_ANY_THROW(m_sync.start());
|
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, noObservationsBeforeStart) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_node.updateObservers();
|
|
|
|
ASSERT_FALSE(m_node.blocksWasQueried);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, noObservationsAfterStop) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
m_sync.stop();
|
|
|
|
m_node.blocksWasQueried = false;
|
|
|
|
m_node.updateObservers();
|
|
|
|
ASSERT_FALSE(m_node.blocksWasQueried);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, stopOnCreation) {
|
|
|
|
ASSERT_NO_THROW(m_sync.stop());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, doubleStopAfterStart) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
m_sync.stop();
|
|
|
|
ASSERT_NO_THROW(m_sync.stop());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, stopIsWaiting) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
|
|
|
|
bool flag = false;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
o1.updFunc = std::move([&e, &flag](uint32_t, uint32_t) {
|
2015-04-06 16:13:07 +00:00
|
|
|
e.notify(); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); flag = true;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(flag, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, syncCompletedError) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
IBlockchainSynchronizerTrivialObserver o;
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
o1.updFunc = std::move([&e](uint32_t curr, uint32_t total) {
|
2015-04-06 16:13:07 +00:00
|
|
|
e.notify(); std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o);
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(std::errc::interrupted, o.completionResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, onLastKnownBlockHeightUpdated) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
|
|
|
|
e.wait();
|
|
|
|
m_node.blocksWasQueried = false;
|
|
|
|
m_node.poolWasQueried = false;
|
|
|
|
m_node.updateObservers();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(true, m_node.blocksWasQueried);
|
|
|
|
EXPECT_EQ(true, m_node.poolWasQueried);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, onPoolChanged) {
|
|
|
|
ConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
|
|
|
|
e.wait();
|
|
|
|
m_node.poolWasQueried = false;
|
|
|
|
m_node.notifyAboutPool();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(true, m_node.poolWasQueried);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, serializationCheck) {
|
|
|
|
addConsumers(2);
|
|
|
|
|
|
|
|
std::stringstream memstream;
|
|
|
|
m_sync.save(memstream);
|
|
|
|
|
|
|
|
ASSERT_GT(memstream.str().size(), 0);
|
|
|
|
|
|
|
|
std::string first = memstream.str();
|
|
|
|
|
|
|
|
BlockchainSynchronizer sync2(m_node, m_currency.genesisBlockHash());
|
|
|
|
|
|
|
|
ASSERT_NO_THROW(sync2.load(memstream));
|
|
|
|
std::stringstream memstream2;
|
|
|
|
m_sync.save(memstream2);
|
|
|
|
EXPECT_EQ(memstream2.str(), first);
|
|
|
|
}
|
|
|
|
|
|
|
|
class FunctorialPoolConsumerStub : public ConsumerStub {
|
|
|
|
public:
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
FunctorialPoolConsumerStub(const Hash& genesisBlockHash) : ConsumerStub(genesisBlockHash) {}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void getKnownPoolTxIds(std::vector<Hash>& ids) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
getKnownPoolTxIdsFunctor(ids);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual std::error_code onPoolUpdated(const std::vector<std::unique_ptr<ITransactionReader>>& addedTransactions, const std::vector<Hash>& deletedTransactions) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
return onPoolUpdatedFunctor(addedTransactions, deletedTransactions);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::function<void(std::vector<Hash>&)> getKnownPoolTxIdsFunctor;
|
|
|
|
std::function<std::error_code(const std::vector<std::unique_ptr<ITransactionReader>>&, const std::vector<Hash>&)> onPoolUpdatedFunctor;
|
2015-04-06 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BcSTest, firstPoolSynchronizationCheck) {
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1ptr = createTransaction();
|
|
|
|
auto tx2ptr = createTransaction();
|
|
|
|
auto tx3ptr = createTransaction();
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
auto tx1 = ::createTx(*tx1ptr.get());
|
|
|
|
auto tx2 = ::createTx(*tx2ptr.get());
|
|
|
|
auto tx3 = ::createTx(*tx3ptr.get());
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1hash = getObjectHash(tx1);
|
|
|
|
auto tx2hash = getObjectHash(tx2);
|
|
|
|
auto tx3hash = getObjectHash(tx3);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> consumer1Pool = { tx1hash, tx2hash };
|
|
|
|
std::vector<Hash> consumer2Pool = { tx2hash, tx3hash };
|
|
|
|
std::unordered_set<Hash> firstExpectedPool = { tx1hash, tx2hash, tx3hash };
|
|
|
|
std::unordered_set<Hash> secondExpectedPool = { tx2hash };
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> expectedDeletedPoolAnswer = { tx3hash };
|
|
|
|
std::vector<Transaction> expectedNewPoolAnswer = { tx1 };
|
|
|
|
std::vector<Hash> expectedNewPoolAnswerHashes = { tx1hash };
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
FunctorialPoolConsumerStub c1(m_currency.genesisBlockHash());
|
|
|
|
FunctorialPoolConsumerStub c2(m_currency.genesisBlockHash());
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c1.getKnownPoolTxIdsFunctor = [&](std::vector<Hash>& ids) { ids.assign(consumer1Pool.begin(), consumer1Pool.end()); };
|
|
|
|
c2.getKnownPoolTxIdsFunctor = [&](std::vector<Hash>& ids) { ids.assign(consumer2Pool.begin(), consumer2Pool.end()); };
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> c1ResponseDeletedPool;
|
|
|
|
std::vector<Hash> c2ResponseDeletedPool;
|
|
|
|
std::vector<Hash> c1ResponseNewPool;
|
|
|
|
std::vector<Hash> c2ResponseNewPool;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c1.onPoolUpdatedFunctor = [&](const std::vector<std::unique_ptr<ITransactionReader>>& new_txs, const std::vector<Hash>& deleted)->std::error_code {
|
2015-04-06 16:13:07 +00:00
|
|
|
c1ResponseDeletedPool.assign(deleted.begin(), deleted.end());
|
2015-07-30 15:22:07 +00:00
|
|
|
for (const auto& tx: new_txs) {
|
|
|
|
Hash hash = tx->getTransactionHash();
|
|
|
|
c1ResponseNewPool.push_back(reinterpret_cast<const Hash&>(hash));
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
return std::error_code();
|
|
|
|
};
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c2.onPoolUpdatedFunctor = [&](const std::vector<std::unique_ptr<ITransactionReader>>& new_txs, const std::vector<Hash>& deleted)->std::error_code {
|
2015-04-06 16:13:07 +00:00
|
|
|
c2ResponseDeletedPool.assign(deleted.begin(), deleted.end());
|
2015-07-30 15:22:07 +00:00
|
|
|
for (const auto& tx: new_txs) {
|
|
|
|
Hash hash = tx->getTransactionHash();
|
|
|
|
c2ResponseNewPool.push_back(reinterpret_cast<const Hash&>(hash));
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
return std::error_code();
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addConsumer(&c1);
|
|
|
|
m_sync.addConsumer(&c2);
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
2015-07-30 15:22:07 +00:00
|
|
|
std::unordered_set<Hash> firstKnownPool;
|
|
|
|
std::unordered_set<Hash> secondKnownPool;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
new_txs.clear();
|
|
|
|
for (const auto& tx: expectedNewPoolAnswer) {
|
|
|
|
new_txs.push_back(createTransactionPrefix(tx));
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
deleted.assign(expectedDeletedPoolAnswer.begin(), expectedDeletedPoolAnswer.end());
|
|
|
|
|
|
|
|
if (requestsCount == 1) {
|
|
|
|
firstKnownPool.insert(known.begin(), known.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
secondKnownPool.insert(known.begin(), known.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(std::error_code());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(2, requestsCount);
|
|
|
|
EXPECT_EQ(firstExpectedPool, firstKnownPool);
|
|
|
|
EXPECT_EQ(secondExpectedPool, secondKnownPool);
|
|
|
|
EXPECT_EQ(expectedDeletedPoolAnswer, c1ResponseDeletedPool);
|
|
|
|
EXPECT_EQ(expectedDeletedPoolAnswer, c2ResponseDeletedPool);
|
2015-07-30 15:22:07 +00:00
|
|
|
EXPECT_EQ(expectedNewPoolAnswerHashes, c1ResponseNewPool);
|
|
|
|
EXPECT_EQ(expectedNewPoolAnswerHashes, c2ResponseNewPool);
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, firstPoolSynchronizationCheckNonActual) {
|
|
|
|
addConsumers(2);
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
is_actual = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(std::error_code());
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(4, requestsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, firstPoolSynchronizationCheckGetPoolErr) {
|
|
|
|
addConsumers(2);
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
callback(std::make_error_code(std::errc::invalid_argument));
|
|
|
|
} else {
|
|
|
|
callback(std::error_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_node.notifyAboutPool();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(4, requestsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckActual) {
|
|
|
|
addConsumers(1);
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
|
|
|
if (requestsCount == 1) {
|
|
|
|
is_actual = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(std::error_code());
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_node.notifyAboutPool();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(2, requestsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckError) {
|
|
|
|
addConsumers(1);
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
|
|
|
if (requestsCount == 1) {
|
|
|
|
callback(std::make_error_code(std::errc::invalid_argument));
|
|
|
|
} else {
|
|
|
|
callback(std::error_code());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_node.notifyAboutPool();
|
|
|
|
e.wait();
|
|
|
|
EXPECT_NE(0, errc.value());
|
|
|
|
m_node.notifyAboutPool(); //error, notify again
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(2, requestsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckTxAdded) {
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1ptr = createTransaction();
|
2015-04-06 16:13:07 +00:00
|
|
|
auto tx1 = ::createTx(*tx1ptr.get());
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1hash = getObjectHash(tx1);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Transaction> newPoolAnswer = { tx1 };
|
|
|
|
std::vector<Hash> expectedKnownPoolHashes = { tx1hash };
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
addConsumers(1);
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> knownPool;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
|
|
|
if (requestsCount == 1) {
|
2015-07-30 15:22:07 +00:00
|
|
|
new_txs.clear();
|
|
|
|
for (const auto& tx: newPoolAnswer) {
|
|
|
|
new_txs.push_back(createTransactionPrefix(tx));
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
knownPool.assign(known.begin(), known.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(std::error_code());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_node.notifyAboutPool();
|
|
|
|
e.wait();
|
|
|
|
m_node.notifyAboutPool();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(2, requestsCount);
|
|
|
|
EXPECT_EQ(expectedKnownPoolHashes, knownPool);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckTxDeleted) {
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1ptr = createTransaction();
|
2015-04-06 16:13:07 +00:00
|
|
|
auto tx1 = ::createTx(*tx1ptr.get());
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1hash = getObjectHash(tx1);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Transaction> newPoolAnswer = { tx1 };
|
|
|
|
std::vector<Hash> deletedPoolAnswer = { tx1hash };
|
|
|
|
std::vector<Hash> expectedKnownPoolHashes = {};
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
addConsumers(1);
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> knownPool;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.getPoolSymmetricDifferenceFunctor = [&](const std::vector<Hash>& known, Hash last, bool& is_actual,
|
|
|
|
std::vector<std::unique_ptr<ITransactionReader>>& new_txs, std::vector<Hash>& deleted, const INode::Callback& callback) {
|
2015-04-06 16:13:07 +00:00
|
|
|
is_actual = true;
|
|
|
|
requestsCount++;
|
|
|
|
|
|
|
|
|
|
|
|
if (requestsCount == 1) {
|
2015-07-30 15:22:07 +00:00
|
|
|
new_txs.clear();
|
|
|
|
for (const auto& tx: newPoolAnswer) {
|
|
|
|
new_txs.push_back(createTransactionPrefix(tx));
|
|
|
|
}
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
deleted.assign(deletedPoolAnswer.begin(), deletedPoolAnswer.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestsCount == 3) {
|
|
|
|
knownPool.assign(known.begin(), known.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(std::error_code());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_node.notifyAboutPool(); // add
|
|
|
|
e.wait();
|
|
|
|
m_node.notifyAboutPool(); // delete
|
|
|
|
e.wait();
|
|
|
|
m_node.notifyAboutPool(); //getknown
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(3, requestsCount);
|
|
|
|
EXPECT_EQ(expectedKnownPoolHashes, knownPool);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckNotification) {
|
2015-04-06 16:13:07 +00:00
|
|
|
addConsumers(1);
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
2015-07-30 15:22:07 +00:00
|
|
|
EXPECT_EQ(true, e.wait_for(std::chrono::milliseconds(1000)));
|
2015-04-06 16:13:07 +00:00
|
|
|
m_sync.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckConsumersNotififcation) {
|
|
|
|
FunctorialPoolConsumerStub c1(m_currency.genesisBlockHash());
|
|
|
|
FunctorialPoolConsumerStub c2(m_currency.genesisBlockHash());
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c1.getKnownPoolTxIdsFunctor = [&](std::vector<Hash>& ids) {};
|
|
|
|
c2.getKnownPoolTxIdsFunctor = [&](std::vector<Hash>& ids) {};
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
bool c1Notified = false;
|
|
|
|
bool c2Notified = false;
|
2015-07-30 15:22:07 +00:00
|
|
|
c1.onPoolUpdatedFunctor = [&](const std::vector<std::unique_ptr<ITransactionReader>>& new_txs, const std::vector<Hash>& deleted)->std::error_code {
|
2015-04-06 16:13:07 +00:00
|
|
|
c1Notified = true;
|
|
|
|
return std::error_code();
|
|
|
|
};
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c2.onPoolUpdatedFunctor = [&](const std::vector<std::unique_ptr<ITransactionReader>>& new_txs, const std::vector<Hash>& deleted)->std::error_code {
|
2015-04-06 16:13:07 +00:00
|
|
|
c2Notified = true;
|
|
|
|
return std::error_code();
|
|
|
|
};
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
o1.syncFunc = std::move([&e](std::error_code) {
|
|
|
|
e.notify();
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c1);
|
|
|
|
m_sync.addConsumer(&c2);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
ASSERT_TRUE(c1Notified);
|
|
|
|
ASSERT_TRUE(c2Notified);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, poolSynchronizationCheckConsumerReturnError) {
|
|
|
|
FunctorialPoolConsumerStub c1(m_currency.genesisBlockHash());
|
|
|
|
FunctorialPoolConsumerStub c2(m_currency.genesisBlockHash());
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c1.getKnownPoolTxIdsFunctor = [&](std::vector<Hash>& ids) {};
|
|
|
|
c2.getKnownPoolTxIdsFunctor = [&](std::vector<Hash>& ids) {};
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
bool c1Notified = false;
|
|
|
|
bool c2Notified = false;
|
2015-07-30 15:22:07 +00:00
|
|
|
c1.onPoolUpdatedFunctor = [&](const std::vector<std::unique_ptr<ITransactionReader>>& new_txs, const std::vector<Hash>& deleted)->std::error_code {
|
2015-04-06 16:13:07 +00:00
|
|
|
c1Notified = true;
|
|
|
|
return std::make_error_code(std::errc::invalid_argument);
|
|
|
|
};
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c2.onPoolUpdatedFunctor = [&](const std::vector<std::unique_ptr<ITransactionReader>>& new_txs, const std::vector<Hash>& deleted)->std::error_code {
|
2015-04-06 16:13:07 +00:00
|
|
|
c2Notified = true;
|
|
|
|
return std::make_error_code(std::errc::invalid_argument);
|
|
|
|
};
|
|
|
|
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c1);
|
|
|
|
m_sync.addConsumer(&c2);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
ASSERT_TRUE(c1Notified != c2Notified);
|
|
|
|
EXPECT_NE(0, errc.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
class FunctorialBlockhainConsumerStub : public ConsumerStub {
|
|
|
|
public:
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
FunctorialBlockhainConsumerStub(const Hash& genesisBlockHash) : ConsumerStub(genesisBlockHash), onBlockchainDetachFunctor([](uint32_t) {}) {}
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual bool onNewBlocks(const CompleteBlock* blocks, uint32_t startHeight, uint32_t count) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
return onNewBlocksFunctor(blocks, startHeight, count);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
virtual void onBlockchainDetach(uint32_t height) override {
|
2015-04-06 16:13:07 +00:00
|
|
|
onBlockchainDetachFunctor(height);
|
|
|
|
}
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::function<bool(const CompleteBlock*, uint32_t, size_t)> onNewBlocksFunctor;
|
|
|
|
std::function<void(uint32_t)> onBlockchainDetachFunctor;
|
2015-04-06 16:13:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkINodeError) {
|
|
|
|
addConsumers(1);
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.queryBlocksFunctor = [](const std::vector<Hash>& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks, uint32_t& startHeight, const INode::Callback& callback) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
callback(std::make_error_code(std::errc::invalid_argument));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(std::make_error_code(std::errc::invalid_argument), errc);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkConsumerError) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
generator.generateEmptyBlocks(10);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [](const CompleteBlock*, uint32_t, size_t) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(std::make_error_code(std::errc::invalid_argument), errc);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkBlocksRequesting) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t blocksExpected = 20;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
generator.generateEmptyBlocks(blocksExpected - 1); //-1 for genesis
|
|
|
|
m_node.setGetNewBlocksLimit(3);
|
|
|
|
|
|
|
|
size_t blocksRequested = 0;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint32_t, size_t count) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
blocksRequested += count;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(blocksExpected, blocksRequested);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkConsumerHeightReceived) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t firstlySnchronizedHeight = 20;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
generator.generateEmptyBlocks(firstlySnchronizedHeight - 1);//-1 for genesis
|
|
|
|
m_node.setGetNewBlocksLimit(50);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint32_t startHeight, size_t) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
|
|
|
|
ConsumerStub fake_c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&fake_c);
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t receivedStartHeight = 0;
|
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint32_t startHeight, size_t) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
receivedStartHeight = startHeight;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(firstlySnchronizedHeight + 1, receivedStartHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkConsumerOldBlocksNotIvoked) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
m_node.setGetNewBlocksLimit(50);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint32_t startHeight, size_t) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
|
|
|
|
ConsumerStub fake_c(m_currency.genesisBlockHash());
|
|
|
|
m_sync.addConsumer(&fake_c);
|
|
|
|
|
|
|
|
bool onNewBlocksInvoked = false;
|
|
|
|
|
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint64_t startHeight, size_t) -> bool {
|
|
|
|
onNewBlocksInvoked = true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
ASSERT_FALSE(onNewBlocksInvoked);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkConsumerHeightReceivedOnDetach) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
m_node.setGetNewBlocksLimit(50);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint32_t startHeight, size_t) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t alternativeHeight = 10;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
m_node.startAlternativeChain(alternativeHeight);
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t receivedStartHeight = 0;
|
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock*, uint32_t startHeight, size_t) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
receivedStartHeight = startHeight;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
uint32_t receivedetachHeight = 0;
|
|
|
|
c.onBlockchainDetachFunctor = [&](uint32_t detachHeight) {
|
2015-04-06 16:13:07 +00:00
|
|
|
receivedetachHeight = detachHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(alternativeHeight, receivedetachHeight);
|
|
|
|
EXPECT_EQ(alternativeHeight, receivedStartHeight);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkStatePreservingBetweenSynchronizations) {
|
|
|
|
addConsumers(1);
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
Hash lastBlockHash = get_block_hash(generator.getBlockchain().back());
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
Hash receivedLastBlockHash;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.queryBlocksFunctor = [&receivedLastBlockHash](const std::vector<Hash>& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks, uint32_t& startHeight, const INode::Callback& callback) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
receivedLastBlockHash = knownBlockIds.front();
|
|
|
|
startHeight = 1;
|
|
|
|
callback(std::make_error_code(std::errc::interrupted));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(lastBlockHash, receivedLastBlockHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkBlocksRerequestingOnError) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
|
|
|
generator.generateEmptyBlocks(20);
|
|
|
|
m_node.setGetNewBlocksLimit(10);
|
|
|
|
|
|
|
|
int requestsCount = 0;
|
2015-07-30 15:22:07 +00:00
|
|
|
std::list<Hash> firstlyKnownBlockIdsTaken;
|
|
|
|
std::list<Hash> secondlyKnownBlockIdsTaken;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> firstlyReceivedBlocks;
|
|
|
|
std::vector<Hash> secondlyReceivedBlocks;
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock* blocks, uint32_t, size_t count) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
firstlyReceivedBlocks.push_back(blocks[i].blockHash);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestsCount == 3) {
|
|
|
|
for (size_t i = 0; i < count; ++i) {
|
|
|
|
secondlyReceivedBlocks.push_back(blocks[i].blockHash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.queryBlocksFunctor = [&](const std::vector<Hash>& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks, uint32_t& startHeight, const INode::Callback& callback) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
if (requestsCount == 1) {
|
|
|
|
firstlyKnownBlockIdsTaken.assign(knownBlockIds.begin(), knownBlockIds.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (requestsCount == 2) {
|
|
|
|
secondlyKnownBlockIdsTaken.assign(knownBlockIds.begin(), knownBlockIds.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
++requestsCount;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(firstlyKnownBlockIdsTaken, secondlyKnownBlockIdsTaken);
|
|
|
|
EXPECT_EQ(firstlyReceivedBlocks, secondlyReceivedBlocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BcSTest, checkTxOrder) {
|
|
|
|
FunctorialBlockhainConsumerStub c(m_currency.genesisBlockHash());
|
|
|
|
IBlockchainSynchronizerFunctorialObserver o1;
|
|
|
|
EventWaiter e;
|
|
|
|
std::error_code errc;
|
|
|
|
o1.syncFunc = std::move([&](std::error_code ec) {
|
|
|
|
e.notify();
|
|
|
|
errc = ec;
|
|
|
|
});
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1ptr = createTransaction();
|
|
|
|
auto tx2ptr = createTransaction();
|
|
|
|
auto tx3ptr = createTransaction();
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
auto tx1 = ::createTx(*tx1ptr.get());
|
|
|
|
auto tx2 = ::createTx(*tx2ptr.get());
|
|
|
|
auto tx3 = ::createTx(*tx3ptr.get());
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
auto tx1hash = getObjectHash(tx1);
|
|
|
|
auto tx2hash = getObjectHash(tx2);
|
|
|
|
auto tx3hash = getObjectHash(tx3);
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
generator.generateEmptyBlocks(2);
|
|
|
|
|
|
|
|
auto last_block = generator.getBlockchain().back();
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
BlockShortEntry bse;
|
|
|
|
bse.hasBlock = true;
|
|
|
|
bse.blockHash = get_block_hash(last_block);;
|
|
|
|
bse.block = last_block;
|
|
|
|
bse.txsShortInfo.push_back({tx1hash, tx1});
|
|
|
|
bse.txsShortInfo.push_back({tx2hash, tx2});
|
|
|
|
bse.txsShortInfo.push_back({tx3hash, tx3});
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> expectedTxHashes = { getObjectHash(last_block.baseTransaction), tx1hash, tx2hash, tx3hash };
|
2015-04-06 16:13:07 +00:00
|
|
|
|
|
|
|
int requestNumber = 0;
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
m_node.queryBlocksFunctor = [&bse, &requestNumber](const std::vector<Hash>& knownBlockIds, uint64_t timestamp, std::vector<BlockShortEntry>& newBlocks, uint32_t& startHeight, const INode::Callback& callback) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
startHeight = 1;
|
2015-07-30 15:22:07 +00:00
|
|
|
newBlocks.push_back(bse);
|
2015-04-06 16:13:07 +00:00
|
|
|
if (requestNumber > 0) {
|
|
|
|
callback(std::make_error_code(std::errc::interrupted));
|
|
|
|
} else {
|
|
|
|
callback(std::error_code());
|
|
|
|
}
|
|
|
|
|
|
|
|
requestNumber++;
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
std::vector<Hash> receivedTxHashes = {};
|
2015-04-06 16:13:07 +00:00
|
|
|
|
2015-07-30 15:22:07 +00:00
|
|
|
c.onNewBlocksFunctor = [&](const CompleteBlock* blocks, uint32_t, size_t count) -> bool {
|
2015-04-06 16:13:07 +00:00
|
|
|
for (auto& tx : blocks[count - 1].transactions) {
|
|
|
|
auto hash = tx->getTransactionHash();
|
2015-07-30 15:22:07 +00:00
|
|
|
receivedTxHashes.push_back(*reinterpret_cast<Hash*>(&hash));
|
2015-04-06 16:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
m_sync.addObserver(&o1);
|
|
|
|
m_sync.addConsumer(&c);
|
|
|
|
m_sync.start();
|
|
|
|
e.wait();
|
|
|
|
m_sync.stop();
|
|
|
|
m_sync.removeObserver(&o1);
|
|
|
|
o1.syncFunc = [](std::error_code) {};
|
|
|
|
|
|
|
|
EXPECT_EQ(expectedTxHashes, receivedTxHashes);
|
|
|
|
}
|