Skip to content

Commit

Permalink
Refactor simulator and synchronizer interface:
Browse files Browse the repository at this point in the history
- Subscription to ordering and consensus gates is in constructors
- Add fixture to synchronizer test
- Add mock consensus gate
  • Loading branch information
lebdron committed Aug 6, 2017
1 parent 552d28a commit 63e2a4d
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 63 deletions.
22 changes: 5 additions & 17 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,9 @@ std::shared_ptr<Simulator> Irohad::createSimulator(
std::shared_ptr<BlockQuery> block_query,
std::shared_ptr<TemporaryFactory> temporary_factory,
std::shared_ptr<HashProviderImpl> hash_provider) {
auto simulator = std::make_shared<Simulator>(
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<Simulator>(ordering_gate, stateful_validator,
temporary_factory, block_query,
hash_provider);
}

std::shared_ptr<PeerCommunicationService>
Expand All @@ -217,14 +211,8 @@ std::shared_ptr<Synchronizer> Irohad::createSynchronizer(
std::shared_ptr<ChainValidator> validator,
std::shared_ptr<MutableFactory> mutableFactory,
std::shared_ptr<BlockLoader> blockLoader) {
auto synchronizer = std::make_shared<SynchronizerImpl>(
std::move(validator), mutableFactory, blockLoader);

consensus_gate->on_commit().subscribe([synchronizer](auto block) {
synchronizer->process_commit(block);
});

return synchronizer;
return std::make_shared<SynchronizerImpl>(consensus_gate, validator,
mutableFactory, blockLoader);
}

std::unique_ptr<::torii::CommandService> Irohad::createCommandService(
Expand Down
2 changes: 1 addition & 1 deletion irohad/network/consensus_gate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace iroha {
* Note: committed block may be not satisfy for top block in ledger
* because synchronization reasons
*/
virtual rxcpp::observable <model::Block> on_commit() = 0;
virtual rxcpp::observable<model::Block> on_commit() = 0;

virtual ~ConsensusGate() = default;
};
Expand Down
4 changes: 4 additions & 0 deletions irohad/simulator/impl/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace iroha {
namespace simulator {

Simulator::Simulator(
std::shared_ptr<network::OrderingGate> ordering_gate,
std::shared_ptr<validation::StatefulValidator> statefulValidator,
std::shared_ptr<ametsuchi::TemporaryFactory> factory,
std::shared_ptr<ametsuchi::BlockQuery> blockQuery,
Expand All @@ -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);
});
Expand Down
2 changes: 2 additions & 0 deletions irohad/simulator/impl/simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -33,6 +34,7 @@ namespace iroha {
class Simulator : public VerifiedProposalCreator, public BlockCreator {
public:
Simulator(
std::shared_ptr<network::OrderingGate> ordering_gate,
std::shared_ptr<validation::StatefulValidator> statefulValidator,
std::shared_ptr<ametsuchi::TemporaryFactory> factory,
std::shared_ptr<ametsuchi::BlockQuery> blockQuery,
Expand Down
4 changes: 4 additions & 0 deletions irohad/synchronizer/impl/synchronizer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ namespace iroha {
namespace synchronizer {

SynchronizerImpl::SynchronizerImpl(
std::shared_ptr<network::ConsensusGate> consensus_gate,
std::shared_ptr<validation::ChainValidator> validator,
std::shared_ptr<ametsuchi::MutableFactory> mutableFactory,
std::shared_ptr<network::BlockLoader> blockLoader)
: validator_(std::move(validator)),
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) {
Expand Down
9 changes: 6 additions & 3 deletions irohad/synchronizer/impl/synchronizer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -28,9 +29,11 @@ namespace iroha {
namespace synchronizer {
class SynchronizerImpl : public Synchronizer {
public:
SynchronizerImpl(std::shared_ptr<validation::ChainValidator> validator,
std::shared_ptr<ametsuchi::MutableFactory> mutableFactory,
std::shared_ptr<network::BlockLoader> blockLoader);
SynchronizerImpl(
std::shared_ptr<network::ConsensusGate> consensus_gate,
std::shared_ptr<validation::ChainValidator> validator,
std::shared_ptr<ametsuchi::MutableFactory> mutableFactory,
std::shared_ptr<network::BlockLoader> blockLoader);

void process_commit(iroha::model::Block commit_message) override;

Expand Down
10 changes: 9 additions & 1 deletion test/module/irohad/network/network_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

#include <gmock/gmock.h>
#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 {
Expand Down Expand Up @@ -49,6 +50,13 @@ namespace iroha {

MOCK_METHOD0(on_proposal, rxcpp::observable<model::Proposal>());
};

class MockConsensusGate : public ConsensusGate {
public:
MOCK_METHOD1(vote, void(model::Block));

MOCK_METHOD0(on_commit, rxcpp::observable<model::Block>());
};
} // namespace network
} // namespace iroha

Expand Down
58 changes: 43 additions & 15 deletions test/module/irohad/simulator/simulator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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;
Expand All @@ -37,17 +40,31 @@ class SimulatorTest : public ::testing::Test {
factory = std::make_shared<MockTemporaryFactory>();
query = std::make_shared<MockBlockQuery>();
provider = std::make_shared<HashProviderImpl>();
ordering_gate = std::make_shared<MockOrderingGate>();
}

void init() {
simulator = std::make_shared<Simulator>(ordering_gate, validator, factory,
query, provider);
}

std::shared_ptr<MockStatefulValidator> validator;
std::shared_ptr<MockTemporaryFactory> factory;
std::shared_ptr<MockBlockQuery> query;
std::shared_ptr<HashProviderImpl> provider;
std::shared_ptr<MockOrderingGate> ordering_gate;

std::shared_ptr<Simulator> 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<Proposal>()));

init();
}

TEST_F(SimulatorTest, ValidWhenPreviousBlock) {
auto txs = std::vector<model::Transaction>(2);
auto proposal = model::Proposal(txs);
proposal.height = 2;
Expand All @@ -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<Proposal>()));

init();

auto proposal_wrapper =
make_test_subscriber<CallExact>(simulator.on_verified_proposal(), 1);
make_test_subscriber<CallExact>(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<CallExact>(simulator.on_block(), 1);
auto block_wrapper = make_test_subscriber<CallExact>(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<model::Transaction>(2);
auto proposal = model::Proposal(txs);
proposal.height = 2;
Expand All @@ -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<Proposal>()));

init();

auto proposal_wrapper =
make_test_subscriber<CallExact>(simulator.on_verified_proposal(), 0);
make_test_subscriber<CallExact>(simulator->on_verified_proposal(), 0);
proposal_wrapper.subscribe();

auto block_wrapper = make_test_subscriber<CallExact>(simulator.on_block(), 0);
auto block_wrapper = make_test_subscriber<CallExact>(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<model::Transaction>(2);
auto proposal = model::Proposal(txs);
proposal.height = 2;
Expand All @@ -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<Proposal>()));

init();

auto proposal_wrapper =
make_test_subscriber<CallExact>(simulator.on_verified_proposal(), 0);
make_test_subscriber<CallExact>(simulator->on_verified_proposal(), 0);
proposal_wrapper.subscribe();

auto block_wrapper = make_test_subscriber<CallExact>(simulator.on_block(), 0);
auto block_wrapper = make_test_subscriber<CallExact>(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());
Expand Down
Loading

0 comments on commit 63e2a4d

Please sign in to comment.