diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index ef4bf9faf9..0f7a117b8c 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -193,15 +193,9 @@ std::shared_ptr Irohad::createSimulator( std::shared_ptr block_query, std::shared_ptr temporary_factory, std::shared_ptr hash_provider) { - auto simulator = std::make_shared( - stateful_validator, temporary_factory, block_query, hash_provider); - - ordering_gate->on_proposal().subscribe( - [simulator](auto proposal) { - simulator->process_proposal(proposal); - }); - - return simulator; + return std::make_shared(ordering_gate, stateful_validator, + temporary_factory, block_query, + hash_provider); } std::shared_ptr @@ -217,14 +211,8 @@ std::shared_ptr Irohad::createSynchronizer( std::shared_ptr validator, std::shared_ptr mutableFactory, std::shared_ptr blockLoader) { - auto synchronizer = std::make_shared( - std::move(validator), mutableFactory, blockLoader); - - consensus_gate->on_commit().subscribe([synchronizer](auto block) { - synchronizer->process_commit(block); - }); - - return synchronizer; + return std::make_shared(consensus_gate, validator, + mutableFactory, blockLoader); } std::unique_ptr<::torii::CommandService> Irohad::createCommandService( diff --git a/irohad/network/consensus_gate.hpp b/irohad/network/consensus_gate.hpp index edf24a651e..8a576306e9 100644 --- a/irohad/network/consensus_gate.hpp +++ b/irohad/network/consensus_gate.hpp @@ -40,7 +40,7 @@ namespace iroha { * Note: committed block may be not satisfy for top block in ledger * because synchronization reasons */ - virtual rxcpp::observable on_commit() = 0; + virtual rxcpp::observable on_commit() = 0; virtual ~ConsensusGate() = default; }; diff --git a/irohad/simulator/impl/simulator.cpp b/irohad/simulator/impl/simulator.cpp index 6778c68fbc..701cd3fbd1 100644 --- a/irohad/simulator/impl/simulator.cpp +++ b/irohad/simulator/impl/simulator.cpp @@ -21,6 +21,7 @@ namespace iroha { namespace simulator { Simulator::Simulator( + std::shared_ptr ordering_gate, std::shared_ptr statefulValidator, std::shared_ptr factory, std::shared_ptr blockQuery, @@ -30,6 +31,9 @@ namespace iroha { block_queries_(std::move(blockQuery)), hash_provider_(std::move(hash_provider)) { log_ = logger::log("Simulator"); + ordering_gate->on_proposal().subscribe( + [this](auto proposal) { this->process_proposal(proposal); }); + notifier_.get_observable().subscribe([this](auto verified_proposal) { this->process_verified_proposal(verified_proposal); }); diff --git a/irohad/simulator/impl/simulator.hpp b/irohad/simulator/impl/simulator.hpp index eebc8d8e34..ffeacc3f5f 100644 --- a/irohad/simulator/impl/simulator.hpp +++ b/irohad/simulator/impl/simulator.hpp @@ -21,6 +21,7 @@ #include "ametsuchi/block_query.hpp" #include "ametsuchi/temporary_factory.hpp" #include "model/model_hash_provider_impl.hpp" +#include "network/ordering_gate.hpp" #include "simulator/block_creator.hpp" #include "simulator/verified_proposal_creator.hpp" #include "validation/stateful_validator.hpp" @@ -33,6 +34,7 @@ namespace iroha { class Simulator : public VerifiedProposalCreator, public BlockCreator { public: Simulator( + std::shared_ptr ordering_gate, std::shared_ptr statefulValidator, std::shared_ptr factory, std::shared_ptr blockQuery, diff --git a/irohad/synchronizer/impl/synchronizer_impl.cpp b/irohad/synchronizer/impl/synchronizer_impl.cpp index faef00ad5c..8981d55f51 100644 --- a/irohad/synchronizer/impl/synchronizer_impl.cpp +++ b/irohad/synchronizer/impl/synchronizer_impl.cpp @@ -23,6 +23,7 @@ namespace iroha { namespace synchronizer { SynchronizerImpl::SynchronizerImpl( + std::shared_ptr consensus_gate, std::shared_ptr validator, std::shared_ptr mutableFactory, std::shared_ptr blockLoader) @@ -30,6 +31,9 @@ namespace iroha { mutableFactory_(std::move(mutableFactory)), blockLoader_(std::move(blockLoader)) { log_ = logger::log("synchronizer"); + consensus_gate->on_commit().subscribe([this](auto block) { + this->process_commit(block); + }); } void SynchronizerImpl::process_commit(iroha::model::Block commit_message) { diff --git a/irohad/synchronizer/impl/synchronizer_impl.hpp b/irohad/synchronizer/impl/synchronizer_impl.hpp index 10b3b5fb81..d75a2a1bdd 100644 --- a/irohad/synchronizer/impl/synchronizer_impl.hpp +++ b/irohad/synchronizer/impl/synchronizer_impl.hpp @@ -19,6 +19,7 @@ #include "ametsuchi/mutable_factory.hpp" #include "network/block_loader.hpp" +#include "network/consensus_gate.hpp" #include "synchronizer/synchronizer.hpp" #include "validation/chain_validator.hpp" @@ -28,9 +29,11 @@ namespace iroha { namespace synchronizer { class SynchronizerImpl : public Synchronizer { public: - SynchronizerImpl(std::shared_ptr validator, - std::shared_ptr mutableFactory, - std::shared_ptr blockLoader); + SynchronizerImpl( + std::shared_ptr consensus_gate, + std::shared_ptr validator, + std::shared_ptr mutableFactory, + std::shared_ptr blockLoader); void process_commit(iroha::model::Block commit_message) override; diff --git a/test/module/irohad/network/network_mocks.hpp b/test/module/irohad/network/network_mocks.hpp index 0cb034d061..f9f06d0da6 100644 --- a/test/module/irohad/network/network_mocks.hpp +++ b/test/module/irohad/network/network_mocks.hpp @@ -20,8 +20,9 @@ #include #include "network/block_loader.hpp" -#include "network/peer_communication_service.hpp" +#include "network/consensus_gate.hpp" #include "network/ordering_gate.hpp" +#include "network/peer_communication_service.hpp" namespace iroha { namespace network { @@ -49,6 +50,13 @@ namespace iroha { MOCK_METHOD0(on_proposal, rxcpp::observable()); }; + + class MockConsensusGate : public ConsensusGate { + public: + MOCK_METHOD1(vote, void(model::Block)); + + MOCK_METHOD0(on_commit, rxcpp::observable()); + }; } // namespace network } // namespace iroha diff --git a/test/module/irohad/simulator/simulator_test.cpp b/test/module/irohad/simulator/simulator_test.cpp index da68cdcd12..49cc221ac5 100644 --- a/test/module/irohad/simulator/simulator_test.cpp +++ b/test/module/irohad/simulator/simulator_test.cpp @@ -16,6 +16,7 @@ */ #include "module/irohad/ametsuchi/ametsuchi_mocks.hpp" +#include "module/irohad/network/network_mocks.hpp" #include "module/irohad/validation/validation_mocks.hpp" #include "simulator/impl/simulator.hpp" @@ -25,6 +26,8 @@ using namespace iroha; using namespace iroha::validation; using namespace iroha::ametsuchi; using namespace iroha::model; +using namespace iroha::simulator; +using namespace iroha::network; using namespace framework::test_subscriber; using ::testing::Return; @@ -37,17 +40,31 @@ class SimulatorTest : public ::testing::Test { factory = std::make_shared(); query = std::make_shared(); provider = std::make_shared(); + ordering_gate = std::make_shared(); + } + + void init() { + simulator = std::make_shared(ordering_gate, validator, factory, + query, provider); } std::shared_ptr validator; std::shared_ptr factory; std::shared_ptr query; std::shared_ptr provider; + std::shared_ptr ordering_gate; + + std::shared_ptr simulator; }; -TEST_F(SimulatorTest, ValidWhenPreviousBlock) { - simulator::Simulator simulator(validator, factory, query, provider); +TEST_F(SimulatorTest, ValidWhenInitialized) { + EXPECT_CALL(*ordering_gate, on_proposal()) + .WillOnce(Return(rxcpp::observable<>::empty())); + init(); +} + +TEST_F(SimulatorTest, ValidWhenPreviousBlock) { auto txs = std::vector(2); auto proposal = model::Proposal(txs); proposal.height = 2; @@ -62,28 +79,31 @@ TEST_F(SimulatorTest, ValidWhenPreviousBlock) { EXPECT_CALL(*validator, validate(_, _)).WillOnce(Return(proposal)); + EXPECT_CALL(*ordering_gate, on_proposal()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); + auto proposal_wrapper = - make_test_subscriber(simulator.on_verified_proposal(), 1); + make_test_subscriber(simulator->on_verified_proposal(), 1); proposal_wrapper.subscribe([&proposal](auto verified_proposal) { ASSERT_EQ(verified_proposal.height, proposal.height); ASSERT_EQ(verified_proposal.transactions, proposal.transactions); }); - auto block_wrapper = make_test_subscriber(simulator.on_block(), 1); + auto block_wrapper = make_test_subscriber(simulator->on_block(), 1); block_wrapper.subscribe([&proposal](auto block) { ASSERT_EQ(block.height, proposal.height); ASSERT_EQ(block.transactions, proposal.transactions); }); - simulator.process_proposal(proposal); + simulator->process_proposal(proposal); ASSERT_TRUE(proposal_wrapper.validate()); ASSERT_TRUE(block_wrapper.validate()); } TEST_F(SimulatorTest, FailWhenNoBlock) { - simulator::Simulator simulator(validator, factory, query, provider); - auto txs = std::vector(2); auto proposal = model::Proposal(txs); proposal.height = 2; @@ -95,22 +115,25 @@ TEST_F(SimulatorTest, FailWhenNoBlock) { EXPECT_CALL(*validator, validate(_, _)).Times(0); + EXPECT_CALL(*ordering_gate, on_proposal()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); + auto proposal_wrapper = - make_test_subscriber(simulator.on_verified_proposal(), 0); + make_test_subscriber(simulator->on_verified_proposal(), 0); proposal_wrapper.subscribe(); - auto block_wrapper = make_test_subscriber(simulator.on_block(), 0); + auto block_wrapper = make_test_subscriber(simulator->on_block(), 0); block_wrapper.subscribe(); - simulator.process_proposal(proposal); + simulator->process_proposal(proposal); ASSERT_TRUE(proposal_wrapper.validate()); ASSERT_TRUE(block_wrapper.validate()); } TEST_F(SimulatorTest, FailWhenSameAsProposalHeight) { - simulator::Simulator simulator(validator, factory, query, provider); - auto txs = std::vector(2); auto proposal = model::Proposal(txs); proposal.height = 2; @@ -125,14 +148,19 @@ TEST_F(SimulatorTest, FailWhenSameAsProposalHeight) { EXPECT_CALL(*validator, validate(_, _)).Times(0); + EXPECT_CALL(*ordering_gate, on_proposal()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); + auto proposal_wrapper = - make_test_subscriber(simulator.on_verified_proposal(), 0); + make_test_subscriber(simulator->on_verified_proposal(), 0); proposal_wrapper.subscribe(); - auto block_wrapper = make_test_subscriber(simulator.on_block(), 0); + auto block_wrapper = make_test_subscriber(simulator->on_block(), 0); block_wrapper.subscribe(); - simulator.process_proposal(proposal); + simulator->process_proposal(proposal); ASSERT_TRUE(proposal_wrapper.validate()); ASSERT_TRUE(block_wrapper.validate()); diff --git a/test/module/irohad/synchronizer/synchronizer_test.cpp b/test/module/irohad/synchronizer/synchronizer_test.cpp index b45ddf09e5..3ba2dc9e13 100644 --- a/test/module/irohad/synchronizer/synchronizer_test.cpp +++ b/test/module/irohad/synchronizer/synchronizer_test.cpp @@ -35,13 +35,36 @@ using ::testing::Return; using ::testing::_; using ::testing::DefaultValue; -TEST(SynchronizerTest, ValidWhenSingleCommitSynchronized) { - auto chain_validator = std::make_shared(); - auto mutable_factory = std::make_shared(); - auto block_loader = std::make_shared(); +class SynchronizerTest : public ::testing::Test { + public: + void SetUp() override { + chain_validator = std::make_shared(); + mutable_factory = std::make_shared(); + block_loader = std::make_shared(); + consensus_gate = std::make_shared(); + } + + void init() { + synchronizer = std::make_shared( + consensus_gate, chain_validator, mutable_factory, block_loader); + } + + std::shared_ptr chain_validator; + std::shared_ptr mutable_factory; + std::shared_ptr block_loader; + std::shared_ptr consensus_gate; + + std::shared_ptr synchronizer; +}; + +TEST_F(SynchronizerTest, ValidWhenInitialized) { + EXPECT_CALL(*consensus_gate, on_commit()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); +} - auto synchronizer = iroha::synchronizer::SynchronizerImpl( - chain_validator, mutable_factory, block_loader); +TEST_F(SynchronizerTest, ValidWhenSingleCommitSynchronized) { Block test_block; test_block.height = 5; @@ -56,8 +79,13 @@ TEST(SynchronizerTest, ValidWhenSingleCommitSynchronized) { EXPECT_CALL(*block_loader, requestBlocks(_, _)).Times(0); + EXPECT_CALL(*consensus_gate, on_commit()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); + auto wrapper = - make_test_subscriber(synchronizer.on_commit_chain(), 1); + make_test_subscriber(synchronizer->on_commit_chain(), 1); wrapper.subscribe([&test_block](auto commit) { auto block_wrapper = make_test_subscriber(commit, 1); block_wrapper.subscribe([&test_block](auto block) { @@ -67,16 +95,12 @@ TEST(SynchronizerTest, ValidWhenSingleCommitSynchronized) { ASSERT_TRUE(block_wrapper.validate()); }); - synchronizer.process_commit(test_block); + synchronizer->process_commit(test_block); ASSERT_TRUE(wrapper.validate()); } -TEST(SynchronizerTest, ValidWhenBadStorage) { - auto chain_validator = std::make_shared(); - auto mutable_factory = std::make_shared(); - auto block_loader = std::make_shared(); - +TEST_F(SynchronizerTest, ValidWhenBadStorage) { Block test_block; DefaultValue>::Clear(); @@ -88,23 +112,21 @@ TEST(SynchronizerTest, ValidWhenBadStorage) { EXPECT_CALL(*block_loader, requestBlocks(_, _)).Times(0); - auto synchronizer = iroha::synchronizer::SynchronizerImpl( - chain_validator, mutable_factory, block_loader); + EXPECT_CALL(*consensus_gate, on_commit()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); auto wrapper = - make_test_subscriber(synchronizer.on_commit_chain(), 0); + make_test_subscriber(synchronizer->on_commit_chain(), 0); wrapper.subscribe(); - synchronizer.process_commit(test_block); + synchronizer->process_commit(test_block); ASSERT_TRUE(wrapper.validate()); } -TEST(SynchronizerTest, ValidWhenBlockValidationFailure) { - auto chain_validator = std::make_shared(); - auto mutable_factory = std::make_shared(); - auto block_loader = std::make_shared(); - +TEST_F(SynchronizerTest, ValidWhenBlockValidationFailure) { Block test_block; test_block.height = 5; test_block.sigs.emplace_back(); @@ -122,11 +144,13 @@ TEST(SynchronizerTest, ValidWhenBlockValidationFailure) { EXPECT_CALL(*block_loader, requestBlocks(_, _)) .WillOnce(Return(rxcpp::observable<>::just(test_block))); - auto synchronizer = iroha::synchronizer::SynchronizerImpl( - chain_validator, mutable_factory, block_loader); + EXPECT_CALL(*consensus_gate, on_commit()) + .WillOnce(Return(rxcpp::observable<>::empty())); + + init(); auto wrapper = - make_test_subscriber(synchronizer.on_commit_chain(), 1); + make_test_subscriber(synchronizer->on_commit_chain(), 1); wrapper.subscribe([&test_block](auto commit) { auto block_wrapper = make_test_subscriber(commit, 1); block_wrapper.subscribe([&test_block](auto block) { @@ -136,7 +160,7 @@ TEST(SynchronizerTest, ValidWhenBlockValidationFailure) { ASSERT_TRUE(block_wrapper.validate()); }); - synchronizer.process_commit(test_block); + synchronizer->process_commit(test_block); ASSERT_TRUE(wrapper.validate()); } \ No newline at end of file