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

refactored iroha::expected::Result #2226

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
5 changes: 2 additions & 3 deletions iroha-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,8 @@ int main(int argc, char *argv[]) {
iroha::model::converters::PbBlockFactory().serialize(block).block_v1());

shared_model::proto::ProtoBlockJsonConverter().serialize(bl).match(
[&logger,
&output_file](const iroha::expected::Value<std::string> &json) {
output_file << json.value;
[&logger, &output_file](auto &&json) {
output_file << std::move(json.value);
logger->info("File saved to genesis.block");
},
[&logger](const auto &error) {
Expand Down
7 changes: 3 additions & 4 deletions irohad/ametsuchi/impl/flat_file_block_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ FlatFileBlockStorage::~FlatFileBlockStorage() {
bool FlatFileBlockStorage::insert(
std::shared_ptr<const shared_model::interface::Block> block) {
return json_converter_->serialize(*block).match(
[&](const expected::Value<std::string> &block_json) {
[&](const auto &block_json) {
return flat_file_storage_->add(block->height(),
stringToBytes(block_json.value));
},
Expand All @@ -49,13 +49,12 @@ FlatFileBlockStorage::fetch(

return json_converter_->deserialize(bytesToString(*storage_block))
.match(
[&](expected::Value<std::unique_ptr<shared_model::interface::Block>>
&block) {
[&](auto &&block) {
return boost::make_optional<
std::shared_ptr<const shared_model::interface::Block>>(
std::move(block.value));
},
[&](expected::Error<std::string> &error)
[&](const auto &error)
-> boost::optional<
std::shared_ptr<const shared_model::interface::Block>> {
log_->warn("Error while block deserialization: {}", error.error);
Expand Down
11 changes: 5 additions & 6 deletions irohad/ametsuchi/impl/mutable_storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ namespace iroha {
auto command_applied =
boost::apply_visitor(*command_executor_, command.get());

return command_applied.match(
[](expected::Value<void> &) { return true; },
[&](expected::Error<CommandError> &e) {
log_->error(e.error.toString());
return false;
});
return command_applied.match([](const auto &) { return true; },
[&](const auto &e) {
log_->error(e.error.toString());
return false;
});
};

return std::all_of(transaction.commands().begin(),
Expand Down
19 changes: 7 additions & 12 deletions irohad/ametsuchi/impl/postgres_block_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,9 @@ namespace iroha {
return result;
}
for (auto i = height; i <= to; i++) {
auto block = getBlock(i);
block.match(
[&result](
expected::Value<std::unique_ptr<shared_model::interface::Block>>
&v) { result.emplace_back(std::move(v.value)); },
[this](const expected::Error<std::string> &e) {
log_->error(e.error);
});
getBlock(i).match(
[&result](auto &&v) { result.emplace_back(std::move(v.value)); },
[this](const auto &e) { log_->error(e.error); });
}
return result;
}
Expand Down Expand Up @@ -107,13 +102,13 @@ namespace iroha {
PostgresBlockQuery::getTopBlock() {
return getBlock(block_store_.last_id())
.match(
[](expected::Value<
std::unique_ptr<shared_model::interface::Block>> &v)
[](auto &&v)
-> expected::Result<BlockQuery::wBlock, std::string> {
return expected::makeValue<BlockQuery::wBlock>(
return expected::makeValue<
std::shared_ptr<shared_model::interface::Block>>(
std::move(v.value));
},
[](expected::Error<std::string> &e)
[](auto &&e)
-> expected::Result<BlockQuery::wBlock, std::string> {
return expected::makeError(std::move(e.error));
});
Expand Down
3 changes: 1 addition & 2 deletions irohad/ametsuchi/impl/postgres_query_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,7 @@ namespace iroha {

return converter_->deserialize(bytesToString(*serialized_block))
.match(
[this](iroha::expected::Value<
std::unique_ptr<shared_model::interface::Block>> &block) {
[this](auto &&block) {
return this->query_response_factory_->createBlockResponse(
std::move(block.value), query_hash_);
},
Expand Down
7 changes: 3 additions & 4 deletions irohad/ametsuchi/impl/postgres_wsv_query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ namespace iroha {
boost::optional<std::shared_ptr<T>> PostgresWsvQuery::fromResult(
shared_model::interface::CommonObjectsFactory::FactoryResult<
std::unique_ptr<T>> &&result) {
return result.match(
[](iroha::expected::Value<std::unique_ptr<T>> &v) {
return std::move(result).match(
[](auto &&v) {
return boost::make_optional(std::shared_ptr<T>(std::move(v.value)));
},
[&](iroha::expected::Error<std::string> &e)
-> boost::optional<std::shared_ptr<T>> {
[&](const auto &e) -> boost::optional<std::shared_ptr<T>> {
log_->error(e.error);
return boost::none;
});
Expand Down
94 changes: 43 additions & 51 deletions irohad/ametsuchi/impl/storage_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,12 @@ namespace iroha {
shared_model::interface::types::HashType hash{""};
shared_model::interface::types::HeightType height{0};
getBlockQuery()->getTopBlock().match(
[&hash, &height](
expected::Value<std::shared_ptr<shared_model::interface::Block>>
&block) {
hash = block.value->hash();
height = block.value->height();
[&hash, &height](const auto &v) {
hash = v.value->hash();
height = v.value->height();
},
[this](expected::Error<std::string> &) {
log_->error("Could not get top block!");
[this](const auto &e) {
log_->error("Could not get top block: {}", e.error);
});
return expected::makeValue<std::unique_ptr<MutableStorage>>(
std::make_unique<MutableStorageImpl>(
Expand Down Expand Up @@ -225,18 +223,14 @@ namespace iroha {
bool StorageImpl::insertBlock(
std::shared_ptr<const shared_model::interface::Block> block) {
log_->info("create mutable storage");
auto storageResult = createMutableStorage();
bool inserted = false;
storageResult.match(
[&](expected::Value<std::unique_ptr<ametsuchi::MutableStorage>>
&storage) {
createMutableStorage().match(
[&, this](auto &&storage) {
inserted = storage.value->apply(block);
log_->info("block inserted: {}", inserted);
commit(std::move(storage.value));
this->commit(std::move(storage.value));
},
[&](expected::Error<std::string> &error) {
log_->error(error.error);
});
[&](const auto &error) { log_->error(error.error); });

return inserted;
}
Expand All @@ -246,16 +240,14 @@ namespace iroha {
&blocks) {
log_->info("create mutable storage");
bool inserted = true;
auto storageResult = createMutableStorage();
storageResult.match(
[&](iroha::expected::Value<std::unique_ptr<MutableStorage>>
&mutableStorage) {
createMutableStorage().match(
[&, this](auto &&mutableStorage) {
std::for_each(blocks.begin(), blocks.end(), [&](auto block) {
inserted &= mutableStorage.value->apply(block);
});
commit(std::move(mutableStorage.value));
this->commit(std::move(mutableStorage.value));
},
[&](iroha::expected::Error<std::string> &error) {
[&](const auto &error) {
log_->error(error.error);
inserted = false;
});
Expand Down Expand Up @@ -411,8 +403,8 @@ namespace iroha {
// create database if
options.dbname() | [&options, &string_res](const std::string &dbname) {
createDatabaseIfNotExist(dbname, options.optionsStringWithoutDbName())
.match([](expected::Value<bool> &val) {},
[&string_res](expected::Error<std::string> &error) {
.match([](auto &&val) {},
[&string_res](auto &&error) {
string_res = error.error;
});
};
Expand All @@ -425,30 +417,31 @@ namespace iroha {
initConnections(block_store_dir, log_manager->getLogger());
auto db_result = initPostgresConnection(postgres_options, pool_size);
expected::Result<std::shared_ptr<StorageImpl>, std::string> storage;
ctx_result.match(
[&](expected::Value<ConnectionContext> &ctx) {
db_result.match(
[&](expected::Value<std::shared_ptr<soci::connection_pool>>
&connection) {
soci::session sql(*connection.value);
bool enable_prepared_transactions =
preparedTransactionsAvailable(sql);
storage = expected::makeValue(std::shared_ptr<StorageImpl>(
new StorageImpl(block_store_dir,
options,
std::move(ctx.value.block_store),
connection.value,
factory,
converter,
perm_converter,
std::move(block_storage_factory),
pool_size,
enable_prepared_transactions,
std::move(log_manager))));
},
[&](expected::Error<std::string> &error) { storage = error; });
},
[&](expected::Error<std::string> &error) { storage = error; });
std::move(ctx_result)
.match(
[&](auto &&ctx) {
std::move(db_result).match(
[&](auto &&connection) {
soci::session sql(*connection.value);
bool enable_prepared_transactions =
preparedTransactionsAvailable(sql);
storage =
expected::makeValue(std::shared_ptr<StorageImpl>(
new StorageImpl(block_store_dir,
options,
std::move(ctx.value.block_store),
std::move(connection.value),
factory,
converter,
perm_converter,
std::move(block_storage_factory),
pool_size,
enable_prepared_transactions,
std::move(log_manager))));
},
[&](const auto &error) { storage = error; });
},
[&](const auto &error) { storage = error; });
return storage;
}

Expand Down Expand Up @@ -602,9 +595,8 @@ namespace iroha {

bool StorageImpl::storeBlock(
std::shared_ptr<const shared_model::interface::Block> block) {
auto json_result = converter_->serialize(*block);
return json_result.match(
[this, &block](const expected::Value<std::string> &v) {
return converter_->serialize(*block).match(
[this, &block](const auto &v) {
if (block_store_->add(block->height(), stringToBytes(v.value))) {
notifier_.get_subscriber().on_next(block);
return true;
Expand All @@ -613,7 +605,7 @@ namespace iroha {
return false;
}
},
[this, &block](const expected::Error<std::string> &e) {
[this, &block](const auto &e) {
log_->error("Block serialization failed: {}: {}", *block, e.error);
return false;
});
Expand Down
4 changes: 2 additions & 2 deletions irohad/ametsuchi/impl/temporary_wsv_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ namespace iroha {
// in case of failed command, rollback and return
auto cmd_is_valid =
execute_command(commands[i])
.match([](expected::Value<void> &) { return true; },
[i, &cmd_error](expected::Error<CommandError> &error) {
.match([](const auto &) { return true; },
[i, &cmd_error](const auto &error) {
cmd_error = {error.error.command_name,
error.error.error_code,
error.error.error_extra,
Expand Down
9 changes: 2 additions & 7 deletions irohad/consensus/yac/impl/yac_crypto_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,8 @@ namespace iroha {
// TODO 30.08.2018 andrei: IR-1670 Remove optional from YAC
// CryptoProviderImpl::getVote
factory_->createSignature(pubkey, signature)
.match(
[&](iroha::expected::Value<
std::unique_ptr<shared_model::interface::Signature>> &sig) {
vote.signature = std::move(sig.value);
},
[](iroha::expected::Error<std::string> &reason) {
});
.match([&](auto &&sig) { vote.signature = std::move(sig.value); },
[](const auto &) {});

return vote;
}
Expand Down
11 changes: 4 additions & 7 deletions irohad/consensus/yac/transport/yac_pb_converters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,10 @@ namespace iroha {
factory
.createSignature(shared_model::crypto::PublicKey(pubkey),
shared_model::crypto::Signed(signature))
.match(
[&](iroha::expected::Value<
std::unique_ptr<shared_model::interface::Signature>>
&sig) { val = std::move(sig.value); },
[&](iroha::expected::Error<std::string> &reason) {
log->error(msg, reason.error);
});
.match([&](auto &&sig) { val = std::move(sig.value); },
[&](const auto &reason) {
log->error(msg, reason.error);
});
};

if (pb_vote.hash().has_block_signature()) {
Expand Down
14 changes: 6 additions & 8 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,17 @@ void Irohad::initStorage() {
perm_converter,
std::move(block_storage_factory),
log_manager_->getChild("Storage"));
storageResult.match(
[&](expected::Value<std::shared_ptr<ametsuchi::StorageImpl>> &_storage) {
storage = _storage.value;
},
[&](expected::Error<std::string> &error) { log_->error(error.error); });
std::move(storageResult)
.match([&](auto &&v) { storage = std::move(v.value); },
[&](const auto &error) { log_->error(error.error); });

log_->info("[Init] => storage ({})", logger::logBool(storage));
}

bool Irohad::restoreWsv() {
return wsv_restorer_->restoreWsv(*storage).match(
[](iroha::expected::Value<void> v) { return true; },
[&](iroha::expected::Error<std::string> &error) {
[](const auto &) { return true; },
[this](const auto &error) {
log_->error(error.error);
return false;
});
Expand Down Expand Up @@ -735,7 +733,7 @@ Irohad::RunResult Irohad::run() {
initial_ledger_state});
return {};
},
[&](const expected::Error<std::string> &e) -> RunResult {
[&](const auto &e) -> RunResult {
log_->error(e.error);
return e;
});
Expand Down
2 changes: 1 addition & 1 deletion irohad/main/irohad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ int main(int argc, char *argv[]) {
// check if at least one block is available in the ledger
auto blocks_exist = irohad.storage->getBlockQuery()->getTopBlock().match(
[](const auto &) { return true; },
[](iroha::expected::Error<std::string> &) { return false; });
[](const auto &) { return false; });

if (not blocks_exist) {
log->error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,8 @@ MstTransportGrpc::deserializeTransactions(const transport::MstState *request) {
[&](const auto &tx) { return transaction_factory_->build(tx); })
| boost::adaptors::filtered([&](const auto &result) {
return result.match(
[](const iroha::expected::Value<
std::unique_ptr<shared_model::interface::Transaction>> &) {
return true;
},
[&](const iroha::expected::Error<TransportFactoryType::Error>
&error) {
[](const auto &) { return true; },
[&](const auto &error) {
log_->info("Transaction deserialization failed: hash {}, {}",
error.error.hash,
error.error.error);
Expand Down Expand Up @@ -91,8 +87,7 @@ grpc::Status MstTransportGrpc::SendState(

for (auto &batch : batches) {
batch_factory_->createTransactionBatch(batch).match(
[&](iroha::expected::Value<std::unique_ptr<
shared_model::interface::TransactionBatch>> &value) {
[&](auto &&value) {
auto cache_presence = tx_presence_cache_->check(*value.value);
if (not cache_presence) {
// TODO andrei 30.11.18 IR-51 Handle database error
Expand All @@ -115,7 +110,7 @@ grpc::Status MstTransportGrpc::SendState(
new_state += std::move(value).value;
}
},
[&](iroha::expected::Error<std::string> &error) {
[&](const auto &error) {
log_->warn("Batch deserialization failed: {}", error.error);
});
}
Expand Down
Loading