Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Add option to load peers from a file on startup #2232

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
#include "ametsuchi/impl/postgres_block_query.hpp"
#include "ametsuchi/impl/postgres_command_executor.hpp"
#include "ametsuchi/impl/postgres_query_executor.hpp"
#include "ametsuchi/impl/postgres_wsv_command.hpp"
#include "ametsuchi/impl/postgres_wsv_query.hpp"
#include "ametsuchi/impl/temporary_wsv_impl.hpp"
#include "backend/protobuf/permissions.hpp"
#include "common/bind.hpp"
#include "common/byteutils.hpp"
#include "converters/protobuf/json_proto_converter.hpp"
#include "cryptography/public_key.hpp"
#include "logger/logger.hpp"
#include "logger/logger_manager.hpp"

Expand Down Expand Up @@ -264,6 +266,18 @@ namespace iroha {
return inserted;
}

bool StorageImpl::insertPeer(const shared_model::interface::Peer &peer) {
log_->info("insert peer {}", peer.pubkey().hex());
soci::session sql(*connection_);
PostgresWsvCommand wsv_command(sql);
auto status = wsv_command.insertPeer(peer);
if (auto e = boost::get<expected::Error<std::string>>(&status)) {
log_->error("Failed to insert peer: {}", e->error);
return false;
}
return true;
}

void StorageImpl::reset() {
log_->info("drop wsv records from db tables");
try {
Expand All @@ -280,6 +294,16 @@ namespace iroha {
}
}

void StorageImpl::resetPeers() {
log_->info("remove everything from peers table");
try {
soci::session sql(*connection_);
sql << reset_peers_;
} catch (std::exception &e) {
log_->warn("peers reset failed, reason: {}", e.what());
}
}

void StorageImpl::dropStorage() {
log_->info("drop storage");
if (connection_ == nullptr) {
Expand Down Expand Up @@ -657,6 +681,10 @@ TRUNCATE TABLE tx_status_by_hash RESTART IDENTITY CASCADE;
TRUNCATE TABLE height_by_account_set RESTART IDENTITY CASCADE;
TRUNCATE TABLE index_by_creator_height RESTART IDENTITY CASCADE;
TRUNCATE TABLE position_by_account_asset RESTART IDENTITY CASCADE;
)";

const std::string &StorageImpl::reset_peers_ = R"(
TRUNCATE TABLE peer RESTART IDENTITY CASCADE;
)";

const std::string &StorageImpl::init_ =
Expand Down
5 changes: 5 additions & 0 deletions irohad/ametsuchi/impl/storage_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,12 @@ namespace iroha {
const std::vector<std::shared_ptr<shared_model::interface::Block>>
&blocks) override;

bool insertPeer(const shared_model::interface::Peer &peer) override;

void reset() override;

void resetPeers() override;

void dropStorage() override;

void freeConnections() override;
Expand Down Expand Up @@ -188,6 +192,7 @@ namespace iroha {
protected:
static const std::string &drop_;
static const std::string &reset_;
static const std::string &reset_peers_;
static const std::string &init_;
};
} // namespace ametsuchi
Expand Down
12 changes: 12 additions & 0 deletions irohad/ametsuchi/storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ namespace iroha {
const std::vector<std::shared_ptr<shared_model::interface::Block>>
&blocks) = 0;

/**
* Inserts peer into WSV
* @param peer - peer to insert
* @return true if inserted
*/
virtual bool insertPeer(const shared_model::interface::Peer &peer) = 0;

/**
* method called when block is written to the storage
* @return observable with the Block committed
Expand All @@ -73,6 +80,11 @@ namespace iroha {
*/
virtual void reset() = 0;

/**
* Removes all saved peers
*/
virtual void resetPeers() = 0;

/**
* Remove all information from ledger
* Tables and the database will be removed too
Expand Down
7 changes: 7 additions & 0 deletions irohad/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ target_link_libraries(raw_block_loader
logger
)

add_library(peers_file_reader impl/peers_file_reader_impl.cpp)
target_link_libraries(peers_file_reader
shared_model_interfaces
parser
)

add_library(application
application.cpp
# TODO andrei 08.11.2018 IR-1851 Create separate targets for initialization
Expand Down Expand Up @@ -61,6 +67,7 @@ add_executable(irohad irohad.cpp)
target_link_libraries(irohad
application
raw_block_loader
peers_file_reader
gflags
rapidjson
keys_manager
Expand Down
49 changes: 34 additions & 15 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <boost/filesystem.hpp>

#include "ametsuchi/impl/flat_file_block_storage_factory.hpp"
#include "ametsuchi/impl/postgres_wsv_command.hpp"
#include "ametsuchi/impl/storage_impl.hpp"
#include "ametsuchi/impl/tx_presence_cache_impl.hpp"
#include "ametsuchi/impl/wsv_restorer_impl.hpp"
Expand Down Expand Up @@ -74,21 +75,23 @@ static constexpr iroha::consensus::yac::ConsistencyModel
/**
* Configuring iroha daemon
*/
Irohad::Irohad(const std::string &block_store_dir,
const std::string &pg_conn,
const std::string &listen_ip,
size_t torii_port,
size_t internal_port,
size_t max_proposal_size,
std::chrono::milliseconds proposal_delay,
std::chrono::milliseconds vote_delay,
std::chrono::minutes mst_expiration_time,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds max_rounds_delay,
size_t stale_stream_max_rounds,
logger::LoggerManagerTreePtr logger_manager,
const boost::optional<GossipPropagationStrategyParams>
&opt_mst_gossip_params)
Irohad::Irohad(
const std::string &block_store_dir,
const std::string &pg_conn,
const std::string &listen_ip,
size_t torii_port,
size_t internal_port,
size_t max_proposal_size,
std::chrono::milliseconds proposal_delay,
std::chrono::milliseconds vote_delay,
std::chrono::minutes mst_expiration_time,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds max_rounds_delay,
size_t stale_stream_max_rounds,
std::vector<std::unique_ptr<shared_model::interface::Peer>> initial_peers,
logger::LoggerManagerTreePtr logger_manager,
const boost::optional<GossipPropagationStrategyParams>
&opt_mst_gossip_params)
: block_store_dir_(block_store_dir),
pg_conn_(pg_conn),
listen_ip_(listen_ip),
Expand All @@ -101,6 +104,7 @@ Irohad::Irohad(const std::string &block_store_dir,
mst_expiration_time_(mst_expiration_time),
max_rounds_delay_(max_rounds_delay),
stale_stream_max_rounds_(stale_stream_max_rounds),
initial_peers_(std::move(initial_peers)),
opt_mst_gossip_params_(opt_mst_gossip_params),
keypair(keypair),
ordering_init(logger_manager->getLogger()),
Expand Down Expand Up @@ -129,6 +133,7 @@ void Irohad::init() {
// Recover WSV from the existing ledger to be sure it is consistent
initWsvRestorer();
restoreWsv();
updatePeers();

initCryptoProvider();
initBatchParser();
Expand Down Expand Up @@ -203,6 +208,20 @@ bool Irohad::restoreWsv() {
});
}

bool Irohad::updatePeers() {
// drop old peers and insert new ones if --peers flag is set
if (not initial_peers_.empty()) {
storage->resetPeers();
for (const auto &peer : initial_peers_) {
if (not storage->insertPeer(*peer)) {
log_->error("Peer insertion failed");
return false;
}
}
}
return true;
}

/**
* Initializing crypto provider
*/
Expand Down
36 changes: 21 additions & 15 deletions irohad/main/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,26 +93,29 @@ class Irohad {
* transactions
* @param stale_stream_max_rounds - maximum number of rounds between
* consecutive status emissions
*
* @param logger_manager - the logger manager to use
* @param opt_mst_gossip_params - parameters for Gossip MST propagation
* (optional). If not provided, disables mst processing support
* TODO mboldyrev 03.11.2018 IR-1844 Refactor the constructor.
*/
Irohad(const std::string &block_store_dir,
const std::string &pg_conn,
const std::string &listen_ip,
size_t torii_port,
size_t internal_port,
size_t max_proposal_size,
std::chrono::milliseconds proposal_delay,
std::chrono::milliseconds vote_delay,
std::chrono::minutes mst_expiration_time,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds max_rounds_delay,
size_t stale_stream_max_rounds,
logger::LoggerManagerTreePtr logger_manager,
const boost::optional<iroha::GossipPropagationStrategyParams>
&opt_mst_gossip_params = boost::none);
Irohad(
const std::string &block_store_dir,
const std::string &pg_conn,
const std::string &listen_ip,
size_t torii_port,
size_t internal_port,
size_t max_proposal_size,
std::chrono::milliseconds proposal_delay,
std::chrono::milliseconds vote_delay,
std::chrono::minutes mst_expiration_time,
const shared_model::crypto::Keypair &keypair,
std::chrono::milliseconds max_rounds_delay,
size_t stale_stream_max_rounds,
std::vector<std::unique_ptr<shared_model::interface::Peer>> initial_peers,
logger::LoggerManagerTreePtr logger_manager,
const boost::optional<iroha::GossipPropagationStrategyParams>
&opt_mst_gossip_params = boost::none);

/**
* Initialization of whole objects in system
Expand All @@ -125,6 +128,8 @@ class Irohad {
*/
bool restoreWsv();

bool updatePeers();

/**
* Drop wsv and block store
*/
Expand Down Expand Up @@ -197,6 +202,7 @@ class Irohad {
std::chrono::minutes mst_expiration_time_;
std::chrono::milliseconds max_rounds_delay_;
size_t stale_stream_max_rounds_;
std::vector<std::unique_ptr<shared_model::interface::Peer>> initial_peers_;
boost::optional<iroha::GossipPropagationStrategyParams>
opt_mst_gossip_params_;

Expand Down
60 changes: 60 additions & 0 deletions irohad/main/impl/peers_file_reader_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "peers_file_reader_impl.hpp"

#include <fstream>

#include "cryptography/public_key.hpp"
#include "interfaces/common_objects/types.hpp"
#include "parser/parser.hpp"

using namespace iroha::main;

boost::optional<std::string> PeersFileReaderImpl::openFile(
const std::string &name) {
std::ifstream file(name);
if (not file) {
return boost::none;
}

std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
return str;
}

boost::optional<std::vector<std::unique_ptr<shared_model::interface::Peer>>>
PeersFileReaderImpl::readPeers(
const std::string &peers_data,
std::shared_ptr<shared_model::interface::CommonObjectsFactory>
common_objects_factory) {
auto strings = parser::split(peers_data);
if (strings.size() % 2 != 0) {
return boost::none;
}

std::vector<std::unique_ptr<shared_model::interface::Peer>> peers{};

for (uint32_t i = 0; i < strings.size(); i += 2) {
shared_model::interface::types::AddressType address = strings.at(i);
shared_model::interface::types::PubkeyType key(
shared_model::interface::types::PubkeyType::fromHexString(
strings.at(i + 1)));
auto peer = common_objects_factory->createPeer(address, key);

if (auto e = boost::get<expected::Error<std::string>>(&peer)) {
return boost::none;
}

peers.emplace_back(std::move(
boost::get<
expected::Value<std::unique_ptr<shared_model::interface::Peer>>>(
&peer)
->value));
}
return boost::make_optional<
std::vector<std::unique_ptr<shared_model::interface::Peer>>>(
std::move(peers));
}
26 changes: 26 additions & 0 deletions irohad/main/impl/peers_file_reader_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_PEERS_FILE_READER_IMPL_HPP
#define IROHA_PEERS_FILE_READER_IMPL_HPP

#include "main/peers_file_reader.hpp"

namespace iroha {
namespace main {
class PeersFileReaderImpl : public PeersFileReader {
public:
boost::optional<std::string> openFile(const std::string &name) override;

boost::optional<
std::vector<std::unique_ptr<shared_model::interface::Peer>>>
readPeers(const std::string &peers_data,
std::shared_ptr<shared_model::interface::CommonObjectsFactory>
common_objects_factory) override;
};
} // namespace main
} // namespace iroha

#endif // IROHA_PEERS_FILE_READER_IMPL_HPP
Loading