Skip to content

Commit

Permalink
storage_impl nullptr to Result substitution
Browse files Browse the repository at this point in the history
Signed-off-by: Moonraker <[email protected]>
  • Loading branch information
Solonets committed Feb 7, 2018
1 parent dbc14d7 commit efcc252
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 31 deletions.
50 changes: 31 additions & 19 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "model/converters/json_common.hpp"
#include "model/execution/command_executor_factory.hpp" // for CommandExecutorFactory

#include <boost/format.hpp>

using iroha::expected::Error;
using iroha::expected::Result;
using iroha::expected::Value;
Expand Down Expand Up @@ -88,7 +90,7 @@ namespace iroha {
postgres_connection->activate();
} catch (const pqxx::broken_connection &e) {
log_->error(kPsqlBroken, e.what());
return makeError(kPsqlBroken);
return makeError((boost::format(kPsqlBroken) % e.what()).str());
}
auto wsv_transaction =
std::make_unique<pqxx::nontransaction>(*postgres_connection, kTmpWsv);
Expand All @@ -101,11 +103,11 @@ namespace iroha {
);
}

std::unique_ptr<MutableStorage> StorageImpl::createMutableStorage() {
expected::Result<std::unique_ptr<MutableStorage>, std::string> StorageImpl::createMutableStorage() {
auto command_executors = model::CommandExecutorFactory::create();
if (not command_executors.has_value()) {
log_->error(kCommandExecutorError);
return nullptr;
return makeError(kCommandExecutorError);
}

auto postgres_connection =
Expand All @@ -114,7 +116,7 @@ namespace iroha {
postgres_connection->activate();
} catch (const pqxx::broken_connection &e) {
log_->error(kPsqlBroken, e.what());
return nullptr;
return makeError((boost::format(kPsqlBroken) % e.what()).str());
}
auto wsv_transaction =
std::make_unique<pqxx::nontransaction>(*postgres_connection, kTmpWsv);
Expand All @@ -124,7 +126,7 @@ namespace iroha {
index->connect(redis_host_, redis_port_);
} catch (const cpp_redis::redis_error &e) {
log_->error(kRedisBroken, redis_host_, redis_port_, e.what());
return nullptr;
return makeError((boost::format(kRedisBroken) % redis_host_ % redis_port_ % e.what()).str());
}

nonstd::optional<hash256_t> top_hash;
Expand All @@ -134,24 +136,34 @@ namespace iroha {
.as_blocking()
.subscribe([&top_hash](auto block) { top_hash = block.hash; });

return std::make_unique<MutableStorageImpl>(
return makeValue<std::unique_ptr<MutableStorage>>(
std::make_unique<MutableStorageImpl>(
top_hash.value_or(hash256_t{}),
std::move(index),
std::move(postgres_connection),
std::move(wsv_transaction),
std::move(command_executors.value()));
std::move(command_executors.value()))
);
}

bool StorageImpl::insertBlock(model::Block block) {
log_->info("create mutable storage");
auto storage = createMutableStorage();
auto inserted = storage->apply(
block,
[](const auto &current_block, auto &query, const auto &top_hash) {
return true;
});
log_->info("block inserted: {}", inserted);
commit(std::move(storage));
auto storageResult = createMutableStorage();
bool inserted = false;
storageResult.match(
[&](expected::Value<std::unique_ptr<ametsuchi::MutableStorage>> &storage) {
inserted = storage.value->apply(
block,
[](const auto &current_block, auto &query, const auto &top_hash) {
return true;
});
log_->info("block inserted: {}", inserted);
commit(std::move(storage.value));
}, [&](expected::Error<std::string> &error) {
log_->error("Cannot create mutable storage");
}
);

return inserted;
}

Expand Down Expand Up @@ -239,26 +251,26 @@ DROP TABLE IF EXISTS role;
std::move(wsv_transaction));
}

std::shared_ptr<StorageImpl> StorageImpl::create(
expected::Result<std::shared_ptr<StorageImpl>, std::string> StorageImpl::create(
std::string block_store_dir,
std::string redis_host,
std::size_t redis_port,
std::string postgres_options) {
auto ctx = initConnections(
block_store_dir, redis_host, redis_port, postgres_options);
if (not ctx.has_value()) {
return nullptr;
return makeError("Failed to connect to PostgreSql");
}

return std::shared_ptr<StorageImpl>(
return makeValue(std::shared_ptr<StorageImpl>(
new StorageImpl(block_store_dir,
redis_host,
redis_port,
postgres_options,
std::move(ctx->block_store),
std::move(ctx->index),
std::move(ctx->pg_lazy),
std::move(ctx->pg_nontx)));
std::move(ctx->pg_nontx))));
}

void StorageImpl::commit(std::unique_ptr<MutableStorage> mutableStorage) {
Expand Down
4 changes: 2 additions & 2 deletions irohad/ametsuchi/impl/storage_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ namespace iroha {
std::string postgres_options);

public:
static std::shared_ptr<StorageImpl> create(
static expected::Result<std::shared_ptr<StorageImpl>, std::string> create(
std::string block_store_dir,
std::string redis_host,
std::size_t redis_port,
std::string postgres_connection);

expected::Result<std::unique_ptr<TemporaryWsv>, std::string> createTemporaryWsv() override;

std::unique_ptr<MutableStorage> createMutableStorage() override;
expected::Result<std::unique_ptr<MutableStorage>, std::string> createMutableStorage() override;

virtual bool insertBlock(model::Block block) override;

Expand Down
3 changes: 2 additions & 1 deletion irohad/ametsuchi/mutable_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define IROHA_MUTABLE_FACTORY_HPP

#include <memory>
#include "common/result.hpp"

namespace iroha {
namespace ametsuchi {
Expand All @@ -32,7 +33,7 @@ namespace iroha {
* Mutable storage is the only way to commit the block to the ledger.
* @return Created mutable storage
*/
virtual std::unique_ptr<MutableStorage> createMutableStorage() = 0;
virtual expected::Result<std::unique_ptr<MutableStorage>, std::string> createMutableStorage() = 0;

/**
* Commit mutable storage to Ametsuchi.
Expand Down
10 changes: 8 additions & 2 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ void Irohad::dropStorage() {
* Initializing iroha daemon storage
*/
void Irohad::initStorage() {
storage =
StorageImpl::create(block_store_dir_, redis_host_, redis_port_, pg_conn_);
auto storageResult = StorageImpl::create(block_store_dir_, redis_host_, redis_port_, pg_conn_);;
storageResult.match(
[&](expected::Value<std::shared_ptr<ametsuchi::StorageImpl>> &_storage) {
storage = _storage.value;
}, [](expected::Error<std::string> &error) {
throw std::runtime_error(error.error);
}
);

log_->info("[Init] => storage", logger::logBool(storage));
}
Expand Down
12 changes: 7 additions & 5 deletions irohad/simulator/impl/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ namespace iroha {
proposal.height);
return;
}
auto temporaryStorage = ametsuchi_factory_->createTemporaryWsv();
temporaryStorage.match(
[&](expected::Value<std::unique_ptr<ametsuchi::TemporaryWsv>> &v) {
auto temporaryStorageResult = ametsuchi_factory_->createTemporaryWsv();
temporaryStorageResult.match(
[&](expected::Value<std::unique_ptr<ametsuchi::TemporaryWsv>> &temporaryStorage) {
notifier_.get_subscriber().on_next(
validator_->validate(proposal, *(v.value))
validator_->validate(proposal, *(temporaryStorage.value))
);
}, [](expected::Error<std::string> &v) {}
}, [](expected::Error<std::string> &error) {
throw std::runtime_error(error.error);
}
);
}

Expand Down
16 changes: 14 additions & 2 deletions irohad/synchronizer/impl/synchronizer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ namespace iroha {

void SynchronizerImpl::process_commit(iroha::model::Block commit_message) {
log_->info("processing commit");
auto storage = mutableFactory_->createMutableStorage();
auto storageResult = mutableFactory_->createMutableStorage();
std::unique_ptr<ametsuchi::MutableStorage> storage;
storageResult.match(
[&](expected::Value<std::unique_ptr<ametsuchi::MutableStorage>> &_storage) {
storage = std::move(_storage.value);
}, [](expected::Error<std::string> &error) {}
);
if (not storage) {
log_->error("Cannot create mutable storage");
return;
Expand All @@ -54,7 +60,13 @@ namespace iroha {
// Block can't be applied to current storage
// Download all missing blocks
for (auto signature : commit_message.sigs) {
storage = mutableFactory_->createMutableStorage();
auto storageResult = mutableFactory_->createMutableStorage();
std::unique_ptr<ametsuchi::MutableStorage> storage;
storageResult.match(
[&](expected::Value<std::unique_ptr<ametsuchi::MutableStorage>> &_storage) {
storage = std::move(_storage.value);
}, [](expected::Error<std::string> &error) {}
);
if (not storage) {
log_->error("cannot create storage");
return;
Expand Down

0 comments on commit efcc252

Please sign in to comment.