From 61848790b9ff6c983f544f0411d5b86c196126f8 Mon Sep 17 00:00:00 2001 From: Mikhail Boldyrev Date: Fri, 12 Apr 2019 15:58:51 +0300 Subject: [PATCH] refactor iroha::expected::Result Signed-off-by: Mikhail Boldyrev --- iroha-cli/main.cpp | 5 +- .../impl/flat_file_block_storage.cpp | 7 +- .../ametsuchi/impl/mutable_storage_impl.cpp | 11 +-- .../ametsuchi/impl/postgres_block_query.cpp | 19 ++-- .../impl/postgres_query_executor.cpp | 3 +- irohad/ametsuchi/impl/postgres_wsv_query.cpp | 7 +- irohad/ametsuchi/impl/storage_impl.cpp | 94 +++++++++---------- irohad/ametsuchi/impl/temporary_wsv_impl.cpp | 4 +- .../yac/impl/yac_crypto_provider_impl.cpp | 9 +- .../yac/transport/yac_pb_converters.hpp | 11 +-- irohad/main/application.cpp | 14 ++- irohad/main/irohad.cpp | 2 +- .../transport/impl/mst_transport_grpc.cpp | 13 +-- irohad/network/impl/block_loader_impl.cpp | 41 ++++---- .../impl/on_demand_os_client_grpc.cpp | 5 +- .../impl/on_demand_os_server_grpc.cpp | 14 +-- .../impl/command_service_transport_grpc.cpp | 11 +-- irohad/torii/impl/query_service.cpp | 12 +-- .../impl/stateful_validator_impl.cpp | 5 +- libs/common/result.hpp | 29 ++++-- .../transaction_sequence_factory.cpp | 4 +- test/framework/batch_helper.hpp | 6 +- .../fake_peer/fake_peer.cpp | 29 +++--- .../integration_framework/iroha_instance.cpp | 4 +- test/framework/sql_query.cpp | 7 +- .../irohad/ametsuchi/ametsuchi_fixture.hpp | 5 +- .../irohad/ametsuchi/ametsuchi_test.cpp | 17 +--- .../irohad/ametsuchi/block_query_test.cpp | 2 +- .../postgres_query_executor_test.cpp | 16 ++-- .../irohad/ametsuchi/storage_init_test.cpp | 11 +-- .../ametsuchi/tx_presence_cache_test.cpp | 7 +- .../irohad/torii/torii_queries_test.cpp | 9 +- .../irohad/torii/torii_service_query_test.cpp | 9 +- .../proto_common_objects_factory_test.cpp | 64 +++++-------- .../proto_proposal_factory_test.cpp | 11 +-- 35 files changed, 214 insertions(+), 303 deletions(-) diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp index d75afade1c..ed4ebb8e45 100644 --- a/iroha-cli/main.cpp +++ b/iroha-cli/main.cpp @@ -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 &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) { diff --git a/irohad/ametsuchi/impl/flat_file_block_storage.cpp b/irohad/ametsuchi/impl/flat_file_block_storage.cpp index 43970474d1..4dde8561ba 100644 --- a/irohad/ametsuchi/impl/flat_file_block_storage.cpp +++ b/irohad/ametsuchi/impl/flat_file_block_storage.cpp @@ -29,7 +29,7 @@ FlatFileBlockStorage::~FlatFileBlockStorage() { bool FlatFileBlockStorage::insert( std::shared_ptr block) { return json_converter_->serialize(*block).match( - [&](const expected::Value &block_json) { + [&](const auto &block_json) { return flat_file_storage_->add(block->height(), stringToBytes(block_json.value)); }, @@ -49,13 +49,12 @@ FlatFileBlockStorage::fetch( return json_converter_->deserialize(bytesToString(*storage_block)) .match( - [&](expected::Value> - &block) { + [&](auto &&block) { return boost::make_optional< std::shared_ptr>( std::move(block.value)); }, - [&](expected::Error &error) + [&](const auto &error) -> boost::optional< std::shared_ptr> { log_->warn("Error while block deserialization: {}", error.error); diff --git a/irohad/ametsuchi/impl/mutable_storage_impl.cpp b/irohad/ametsuchi/impl/mutable_storage_impl.cpp index ac4407ba46..138f47308f 100644 --- a/irohad/ametsuchi/impl/mutable_storage_impl.cpp +++ b/irohad/ametsuchi/impl/mutable_storage_impl.cpp @@ -55,12 +55,11 @@ namespace iroha { auto command_applied = boost::apply_visitor(*command_executor_, command.get()); - return command_applied.match( - [](expected::Value &) { return true; }, - [&](expected::Error &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(), diff --git a/irohad/ametsuchi/impl/postgres_block_query.cpp b/irohad/ametsuchi/impl/postgres_block_query.cpp index 13387f0a35..8f5578ad05 100644 --- a/irohad/ametsuchi/impl/postgres_block_query.cpp +++ b/irohad/ametsuchi/impl/postgres_block_query.cpp @@ -48,14 +48,9 @@ namespace iroha { return result; } for (auto i = height; i <= to; i++) { - auto block = getBlock(i); - block.match( - [&result]( - expected::Value> - &v) { result.emplace_back(std::move(v.value)); }, - [this](const expected::Error &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; } @@ -107,13 +102,13 @@ namespace iroha { PostgresBlockQuery::getTopBlock() { return getBlock(block_store_.last_id()) .match( - [](expected::Value< - std::unique_ptr> &v) + [](auto &&v) -> expected::Result { - return expected::makeValue( + return expected::makeValue< + std::shared_ptr>( std::move(v.value)); }, - [](expected::Error &e) + [](auto &&e) -> expected::Result { return expected::makeError(std::move(e.error)); }); diff --git a/irohad/ametsuchi/impl/postgres_query_executor.cpp b/irohad/ametsuchi/impl/postgres_query_executor.cpp index e7571b63e1..98e65297bb 100644 --- a/irohad/ametsuchi/impl/postgres_query_executor.cpp +++ b/irohad/ametsuchi/impl/postgres_query_executor.cpp @@ -627,8 +627,7 @@ namespace iroha { return converter_->deserialize(bytesToString(*serialized_block)) .match( - [this](iroha::expected::Value< - std::unique_ptr> &block) { + [this](auto &&block) { return this->query_response_factory_->createBlockResponse( std::move(block.value), query_hash_); }, diff --git a/irohad/ametsuchi/impl/postgres_wsv_query.cpp b/irohad/ametsuchi/impl/postgres_wsv_query.cpp index 13c64f5f6a..ab8223fb3c 100644 --- a/irohad/ametsuchi/impl/postgres_wsv_query.cpp +++ b/irohad/ametsuchi/impl/postgres_wsv_query.cpp @@ -37,12 +37,11 @@ namespace iroha { boost::optional> PostgresWsvQuery::fromResult( shared_model::interface::CommonObjectsFactory::FactoryResult< std::unique_ptr> &&result) { - return result.match( - [](iroha::expected::Value> &v) { + return std::move(result).match( + [](auto &&v) { return boost::make_optional(std::shared_ptr(std::move(v.value))); }, - [&](iroha::expected::Error &e) - -> boost::optional> { + [&](const auto &e) -> boost::optional> { log_->error(e.error); return boost::none; }); diff --git a/irohad/ametsuchi/impl/storage_impl.cpp b/irohad/ametsuchi/impl/storage_impl.cpp index 5a386dc245..e4193f4e93 100644 --- a/irohad/ametsuchi/impl/storage_impl.cpp +++ b/irohad/ametsuchi/impl/storage_impl.cpp @@ -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> - &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 &) { - log_->error("Could not get top block!"); + [this](const auto &e) { + log_->error("Could not get top block: {}", e.error); }); return expected::makeValue>( std::make_unique( @@ -225,18 +223,14 @@ namespace iroha { bool StorageImpl::insertBlock( std::shared_ptr block) { log_->info("create mutable storage"); - auto storageResult = createMutableStorage(); bool inserted = false; - storageResult.match( - [&](expected::Value> - &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 &error) { - log_->error(error.error); - }); + [&](const auto &error) { log_->error(error.error); }); return inserted; } @@ -246,16 +240,14 @@ namespace iroha { &blocks) { log_->info("create mutable storage"); bool inserted = true; - auto storageResult = createMutableStorage(); - storageResult.match( - [&](iroha::expected::Value> - &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 &error) { + [&](const auto &error) { log_->error(error.error); inserted = false; }); @@ -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 &val) {}, - [&string_res](expected::Error &error) { + .match([](auto &&val) {}, + [&string_res](auto &&error) { string_res = error.error; }); }; @@ -425,30 +417,31 @@ namespace iroha { initConnections(block_store_dir, log_manager->getLogger()); auto db_result = initPostgresConnection(postgres_options, pool_size); expected::Result, std::string> storage; - ctx_result.match( - [&](expected::Value &ctx) { - db_result.match( - [&](expected::Value> - &connection) { - soci::session sql(*connection.value); - bool enable_prepared_transactions = - preparedTransactionsAvailable(sql); - storage = expected::makeValue(std::shared_ptr( - 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 &error) { storage = error; }); - }, - [&](expected::Error &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( + 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; } @@ -602,9 +595,8 @@ namespace iroha { bool StorageImpl::storeBlock( std::shared_ptr block) { - auto json_result = converter_->serialize(*block); - return json_result.match( - [this, &block](const expected::Value &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; @@ -613,7 +605,7 @@ namespace iroha { return false; } }, - [this, &block](const expected::Error &e) { + [this, &block](const auto &e) { log_->error("Block serialization failed: {}: {}", *block, e.error); return false; }); diff --git a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp index 09b23879f4..53865b3b37 100644 --- a/irohad/ametsuchi/impl/temporary_wsv_impl.cpp +++ b/irohad/ametsuchi/impl/temporary_wsv_impl.cpp @@ -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 &) { return true; }, - [i, &cmd_error](expected::Error &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, diff --git a/irohad/consensus/yac/impl/yac_crypto_provider_impl.cpp b/irohad/consensus/yac/impl/yac_crypto_provider_impl.cpp index 1dfae5b74a..31a0a87b3c 100644 --- a/irohad/consensus/yac/impl/yac_crypto_provider_impl.cpp +++ b/irohad/consensus/yac/impl/yac_crypto_provider_impl.cpp @@ -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> &sig) { - vote.signature = std::move(sig.value); - }, - [](iroha::expected::Error &reason) { - }); + .match([&](auto &&sig) { vote.signature = std::move(sig.value); }, + [](const auto &) {}); return vote; } diff --git a/irohad/consensus/yac/transport/yac_pb_converters.hpp b/irohad/consensus/yac/transport/yac_pb_converters.hpp index 950fef62bb..0b1d7c989f 100644 --- a/irohad/consensus/yac/transport/yac_pb_converters.hpp +++ b/irohad/consensus/yac/transport/yac_pb_converters.hpp @@ -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> - &sig) { val = std::move(sig.value); }, - [&](iroha::expected::Error &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()) { diff --git a/irohad/main/application.cpp b/irohad/main/application.cpp index 9d06253e04..c18cca9a62 100644 --- a/irohad/main/application.cpp +++ b/irohad/main/application.cpp @@ -185,19 +185,17 @@ void Irohad::initStorage() { perm_converter, std::move(block_storage_factory), log_manager_->getChild("Storage")); - storageResult.match( - [&](expected::Value> &_storage) { - storage = _storage.value; - }, - [&](expected::Error &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 v) { return true; }, - [&](iroha::expected::Error &error) { + [](const auto &) { return true; }, + [this](const auto &error) { log_->error(error.error); return false; }); @@ -735,7 +733,7 @@ Irohad::RunResult Irohad::run() { initial_ledger_state}); return {}; }, - [&](const expected::Error &e) -> RunResult { + [&](const auto &e) -> RunResult { log_->error(e.error); return e; }); diff --git a/irohad/main/irohad.cpp b/irohad/main/irohad.cpp index fd71352c21..735e7449e0 100644 --- a/irohad/main/irohad.cpp +++ b/irohad/main/irohad.cpp @@ -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 &) { return false; }); + [](const auto &) { return false; }); if (not blocks_exist) { log->error( diff --git a/irohad/multi_sig_transactions/transport/impl/mst_transport_grpc.cpp b/irohad/multi_sig_transactions/transport/impl/mst_transport_grpc.cpp index f76b57c5e2..0b87d5a0fa 100644 --- a/irohad/multi_sig_transactions/transport/impl/mst_transport_grpc.cpp +++ b/irohad/multi_sig_transactions/transport/impl/mst_transport_grpc.cpp @@ -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> &) { - return true; - }, - [&](const iroha::expected::Error - &error) { + [](const auto &) { return true; }, + [&](const auto &error) { log_->info("Transaction deserialization failed: hash {}, {}", error.error.hash, error.error.error); @@ -91,8 +87,7 @@ grpc::Status MstTransportGrpc::SendState( for (auto &batch : batches) { batch_factory_->createTransactionBatch(batch).match( - [&](iroha::expected::Value> &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 @@ -115,7 +110,7 @@ grpc::Status MstTransportGrpc::SendState( new_state += std::move(value).value; } }, - [&](iroha::expected::Error &error) { + [&](const auto &error) { log_->warn("Batch deserialization failed: {}", error.error); }); } diff --git a/irohad/network/impl/block_loader_impl.cpp b/irohad/network/impl/block_loader_impl.cpp index d641e2c529..e53f310dd0 100644 --- a/irohad/network/impl/block_loader_impl.cpp +++ b/irohad/network/impl/block_loader_impl.cpp @@ -61,17 +61,15 @@ rxcpp::observable> BlockLoaderImpl::retrieveBlocks( auto reader = this->getPeerStub(**peer).retrieveBlocks(&context, request); while (subscriber.is_subscribed() and reader->Read(&block)) { - auto proto_block = block_factory_.createBlock(std::move(block)); - proto_block.match( - [&subscriber]( - iroha::expected::Value> &result) { - subscriber.on_next(std::move(result.value)); - }, - [this, - &context](const iroha::expected::Error &error) { - log_->error(error.error); - context.TryCancel(); - }); + block_factory_.createBlock(std::move(block)) + .match( + [&subscriber](auto &&result) { + subscriber.on_next(std::move(result.value)); + }, + [this, &context](const auto &error) { + log_->error(error.error); + context.TryCancel(); + }); } reader->Finish(); subscriber.on_completed(); @@ -99,17 +97,16 @@ boost::optional> BlockLoaderImpl::retrieveBlock( return boost::none; } - auto result = block_factory_.createBlock(std::move(block)); - - return result.match( - [](iroha::expected::Value> &v) { - return boost::make_optional(std::shared_ptr(std::move(v.value))); - }, - [this](const iroha::expected::Error &e) - -> boost::optional> { - log_->error(e.error); - return boost::none; - }); + return block_factory_.createBlock(std::move(block)) + .match( + [](auto &&v) { + return boost::make_optional( + std::shared_ptr(std::move(v.value))); + }, + [this](const auto &e) -> boost::optional> { + log_->error(e.error); + return boost::none; + }); } boost::optional> diff --git a/irohad/ordering/impl/on_demand_os_client_grpc.cpp b/irohad/ordering/impl/on_demand_os_client_grpc.cpp index dc2a31f15a..fbed8b4036 100644 --- a/irohad/ordering/impl/on_demand_os_client_grpc.cpp +++ b/irohad/ordering/impl/on_demand_os_client_grpc.cpp @@ -66,13 +66,12 @@ OnDemandOsClientGrpc::onRequestProposal(consensus::Round round) { } return proposal_factory_->build(response.proposal()) .match( - [&](iroha::expected::Value< - std::unique_ptr> &v) { + [&](auto &&v) { return boost::make_optional( std::shared_ptr( std::move(v).value)); }, - [this](iroha::expected::Error &error) { + [this](const auto &error) { log_->info(error.error.error); // error return boost::optional< std::shared_ptr>(); diff --git a/irohad/ordering/impl/on_demand_os_server_grpc.cpp b/irohad/ordering/impl/on_demand_os_server_grpc.cpp index bedbef9328..4d0c4d14b3 100644 --- a/irohad/ordering/impl/on_demand_os_server_grpc.cpp +++ b/irohad/ordering/impl/on_demand_os_server_grpc.cpp @@ -41,12 +41,8 @@ OnDemandOsServerGrpc::deserializeTransactions( [&](const auto &tx) { return transaction_factory_->build(tx); }) | boost::adaptors::filtered([&](const auto &result) { return result.match( - [](const iroha::expected::Value< - std::unique_ptr> &) { - return true; - }, - [&](const iroha::expected::Error - &error) { + [](const auto &) { return true; }, + [&](const auto &error) { log_->info("Transaction deserialization failed: hash {}, {}", error.error.hash, error.error.error); @@ -75,10 +71,8 @@ grpc::Status OnDemandOsServerGrpc::SendBatches( OdOsNotification::CollectionType{}, [this](auto &acc, const auto &cand) { batch_factory_->createTransactionBatch(cand).match( - [&](iroha::expected::Value< - std::unique_ptr> - &value) { acc.push_back(std::move(value).value); }, - [&](iroha::expected::Error &error) { + [&](auto &&value) { acc.push_back(std::move(value).value); }, + [&](const auto &error) { log_->warn("Batch deserialization failed: {}", error.error); }); return acc; diff --git a/irohad/torii/impl/command_service_transport_grpc.cpp b/irohad/torii/impl/command_service_transport_grpc.cpp index f24d1e41b1..7d835dceb5 100644 --- a/irohad/torii/impl/command_service_transport_grpc.cpp +++ b/irohad/torii/impl/command_service_transport_grpc.cpp @@ -100,12 +100,10 @@ namespace iroha { shared_model::interface::types::SharedTxsCollectionType tx_collection; for (const auto &tx : request->transactions()) { transaction_factory_->build(tx).match( - [&tx_collection]( - iroha::expected::Value< - std::unique_ptr> &v) { + [&tx_collection](auto &&v) { tx_collection.emplace_back(std::move(v).value); }, - [this](iroha::expected::Error &error) { + [this](const auto &error) { status_bus_->publish(status_factory_->makeStatelessFail( error.error.hash, shared_model::interface::TxStatusFactory::TransactionError{ @@ -125,12 +123,11 @@ namespace iroha { for (auto &batch : batches) { batch_factory_->createTransactionBatch(batch).match( - [&](iroha::expected::Value> &value) { + [&](auto &&value) { this->command_service_->handleTransactionBatch( std::move(value).value); }, - [&](iroha::expected::Error &error) { + [&](const auto &error) { std::vector hashes; std::transform(batch.begin(), diff --git a/irohad/torii/impl/query_service.cpp b/irohad/torii/impl/query_service.cpp index cfb5c15ef3..73410a002f 100644 --- a/irohad/torii/impl/query_service.cpp +++ b/irohad/torii/impl/query_service.cpp @@ -40,9 +40,7 @@ namespace iroha { } query_factory_->build(request).match( - [this, &hash, &response]( - const iroha::expected::Value< - std::unique_ptr> &query) { + [this, &hash, &response](const auto &query) { // Send query to iroha response = static_cast( *query_processor_->queryHandle(*query.value)) @@ -51,8 +49,7 @@ namespace iroha { // 0 is used as a dummy value cache_.addItem(hash, 0); }, - [&hash, &response]( - const iroha::expected::Error &error) { + [&hash, &response](auto &&error) { response.set_query_hash(hash.hex()); response.mutable_error_response()->set_reason( iroha::protocol::ErrorResponse::STATELESS_INVALID); @@ -80,8 +77,7 @@ namespace iroha { blocks_query_factory_->build(*request).match( [this, context, request, writer, ¤t_thread, &run_loop]( - const iroha::expected::Value> &query) { + const auto &query) { rxcpp::composite_subscription subscription; std::string client_id = (boost::format("Peer: '%s'") % context->peer()).str(); @@ -143,7 +139,7 @@ namespace iroha { iroha::schedulers::handleEvents(subscription, run_loop); }, - [this, writer](const auto &error) { + [this, writer](auto &&error) { log_->debug("Stateless invalid: {}", error.error.error); iroha::protocol::BlockQueryResponse response; response.mutable_block_error_response()->set_message( diff --git a/irohad/validation/impl/stateful_validator_impl.cpp b/irohad/validation/impl/stateful_validator_impl.cpp index a80ed18f24..2e6130ab1b 100644 --- a/irohad/validation/impl/stateful_validator_impl.cpp +++ b/irohad/validation/impl/stateful_validator_impl.cpp @@ -32,9 +32,8 @@ namespace iroha { validation::TransactionsErrors &transactions_errors_log, const shared_model::interface::Transaction &tx) { return temporary_wsv.apply(tx).match( - [](expected::Value &) { return true; }, - [&tx, &transactions_errors_log]( - expected::Error &error) { + [](const auto &) { return true; }, + [&tx, &transactions_errors_log](auto &&error) { transactions_errors_log.emplace_back(validation::TransactionError{ tx.hash(), std::move(error.error)}); return false; diff --git a/libs/common/result.hpp b/libs/common/result.hpp index 99d3103000..68b0d197c3 100644 --- a/libs/common/result.hpp +++ b/libs/common/result.hpp @@ -81,10 +81,25 @@ namespace iroha { * @nocode */ template - constexpr auto match(ValueMatch &&value_func, ErrorMatch &&error_func) { + constexpr auto match(ValueMatch &&value_func, ErrorMatch &&error_func) & { return visit_in_place(*this, - std::forward(value_func), - std::forward(error_func)); + [f = std::forward(value_func)]( + ValueType &v) { return f(v); }, + [f = std::forward(error_func)]( + ErrorType &e) { return f(e); }); + } + + /** + * Move alternative for match function + */ + template + constexpr auto match(ValueMatch &&value_func, + ErrorMatch &&error_func) && { + return visit_in_place(*this, + [f = std::forward(value_func)]( + ValueType &v) { return f(std::move(v)); }, + [f = std::forward(error_func)]( + ErrorType &e) { return f(std::move(e)); }); } /** @@ -92,10 +107,12 @@ namespace iroha { */ template constexpr auto match(ValueMatch &&value_func, - ErrorMatch &&error_func) const { + ErrorMatch &&error_func) const & { return visit_in_place(*this, - std::forward(value_func), - std::forward(error_func)); + [f = std::forward(value_func)]( + const ValueType &v) { return f(v); }, + [f = std::forward(error_func)]( + const ErrorType &e) { return f(e); }); } /** diff --git a/shared_model/interfaces/iroha_internal/transaction_sequence_factory.cpp b/shared_model/interfaces/iroha_internal/transaction_sequence_factory.cpp index d4fd7baac0..4c7d0f27cf 100644 --- a/shared_model/interfaces/iroha_internal/transaction_sequence_factory.cpp +++ b/shared_model/interfaces/iroha_internal/transaction_sequence_factory.cpp @@ -46,7 +46,9 @@ namespace shared_model { types::BatchesCollectionType batches; auto insert_batch = [&batches](iroha::expected::Value> - &value) { batches.push_back(std::move(value.value)); }; + &&value) { + batches.push_back(std::move(value.value)); + }; validation::Answer result; if (transactions.empty()) { diff --git a/test/framework/batch_helper.hpp b/test/framework/batch_helper.hpp index e3c241686a..fc23558122 100644 --- a/test/framework/batch_helper.hpp +++ b/test/framework/batch_helper.hpp @@ -218,10 +218,8 @@ namespace framework { batch_validator); return batch_factory->createTransactionBatch(std::move(tx)) .match( - [](iroha::expected::Value> &value) - -> std::shared_ptr< - shared_model::interface::TransactionBatch> { + [](auto &&value) -> std::shared_ptr< + shared_model::interface::TransactionBatch> { return std::move(value.value); }, [](const auto &err) diff --git a/test/framework/integration_framework/fake_peer/fake_peer.cpp b/test/framework/integration_framework/fake_peer/fake_peer.cpp index 9513172aa2..17e330d480 100644 --- a/test/framework/integration_framework/fake_peer/fake_peer.cpp +++ b/test/framework/integration_framework/fake_peer/fake_peer.cpp @@ -45,14 +45,8 @@ static std::shared_ptr createPeer( std::shared_ptr peer; common_objects_factory->createPeer(address, key) .match( - [&peer](iroha::expected::Result< - std::unique_ptr, - std::string>::ValueType &result) { - peer = std::move(result.value); - }, - [&address](const iroha::expected::Result< - std::unique_ptr, - std::string>::ErrorType &error) { + [&peer](auto &&result) { peer = std::move(result.value); }, + [&address](const auto &error) { BOOST_THROW_EXCEPTION( std::runtime_error("Failed to create peer object for peer " + address + ". " + error.error)); @@ -206,8 +200,7 @@ namespace integration_framework { .append(synchronizer_transport_) .run() .match( - [this](const iroha::expected::Result::ValueType - &val) { + [this](const auto &val) { const size_t bound_port = val.value; BOOST_VERIFY_MSG( bound_port == internal_port_, @@ -283,14 +276,14 @@ namespace integration_framework { std::shared_ptr signature_with_pubkey; common_objects_factory_ ->createSignature(keypair_->publicKey(), bare_signature) - .match([&signature_with_pubkey]( - iroha::expected::Value< - std::unique_ptr> & - sig) { signature_with_pubkey = std::move(sig.value); }, - [](iroha::expected::Error &reason) { - BOOST_THROW_EXCEPTION(std::runtime_error( - "Cannot build signature: " + reason.error)); - }); + .match( + [&signature_with_pubkey](auto &&sig) { + signature_with_pubkey = std::move(sig.value); + }, + [](const auto &reason) { + BOOST_THROW_EXCEPTION(std::runtime_error( + "Cannot build signature: " + reason.error)); + }); return signature_with_pubkey; } diff --git a/test/framework/integration_framework/iroha_instance.cpp b/test/framework/integration_framework/iroha_instance.cpp index 702f2d69b7..938f220bb3 100644 --- a/test/framework/integration_framework/iroha_instance.cpp +++ b/test/framework/integration_framework/iroha_instance.cpp @@ -97,8 +97,8 @@ namespace integration_framework { void IrohaInstance::run() { instance_->run().match( - [](const Irohad::RunResult::ValueType &) {}, - [](const Irohad::RunResult::ErrorType &error) { + [](const auto &) {}, + [](const auto &error) { BOOST_THROW_EXCEPTION(std::runtime_error(error.error)); }); } diff --git a/test/framework/sql_query.cpp b/test/framework/sql_query.cpp index 4e3b37f498..db088320ae 100644 --- a/test/framework/sql_query.cpp +++ b/test/framework/sql_query.cpp @@ -41,12 +41,11 @@ namespace framework { boost::optional> SqlQuery::fromResult( shared_model::interface::CommonObjectsFactory::FactoryResult< std::unique_ptr> &&result) { - return result.match( - [](iroha::expected::Value> &v) { + return std::move(result).match( + [](auto &&v) { return boost::make_optional(std::shared_ptr(std::move(v.value))); }, - [&](iroha::expected::Error &e) - -> boost::optional> { + [&](const auto &e) -> boost::optional> { log_->error(e.error); return boost::none; }); diff --git a/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp b/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp index 9f88cf33df..411f0fdade 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp +++ b/test/module/irohad/ametsuchi/ametsuchi_fixture.hpp @@ -54,9 +54,8 @@ namespace iroha { perm_converter_, std::move(block_storage_factory), getTestLoggerManager()->getChild("Storage")) - .match([&](iroha::expected::Value> - &_storage) { storage = _storage.value; }, - [](iroha::expected::Error &error) { + .match([&](const auto &_storage) { storage = _storage.value; }, + [](const auto &error) { FAIL() << "StorageImpl: " << error.error; }); sql = std::make_shared(*soci::factory_postgresql(), diff --git a/test/module/irohad/ametsuchi/ametsuchi_test.cpp b/test/module/irohad/ametsuchi/ametsuchi_test.cpp index 85a36e8e25..77d496aa86 100644 --- a/test/module/irohad/ametsuchi/ametsuchi_test.cpp +++ b/test/module/irohad/ametsuchi/ametsuchi_test.cpp @@ -85,12 +85,8 @@ void apply(S &&storage, std::unique_ptr ms; auto storageResult = storage->createMutableStorage(); storageResult.match( - [&](iroha::expected::Value> &_storage) { - ms = std::move(_storage.value); - }, - [](iroha::expected::Error &error) { - FAIL() << "MutableStorage: " << error.error; - }); + [&](auto &&_storage) { ms = std::move(_storage.value); }, + [](const auto &error) { FAIL() << "MutableStorage: " << error.error; }); ms->apply(block); storage->commit(std::move(ms)); } @@ -400,8 +396,7 @@ TEST_F(AmetsuchiTest, TestingStorageWhenCommitBlock) { std::unique_ptr mutable_storage; storage->createMutableStorage().match( - [&mutable_storage](iroha::expected::Value> - &mut_storage) { + [&mutable_storage](auto &&mut_storage) { mutable_storage = std::move(mut_storage.value); }, [](const auto &) { FAIL() << "Mutable storage cannot be created"; }); @@ -461,10 +456,8 @@ TEST_F(AmetsuchiTest, TestRestoreWSV) { // recover storage and check it is recovered WsvRestorerImpl wsvRestorer; wsvRestorer.restoreWsv(*storage).match( - [](iroha::expected::Value) {}, - [&](iroha::expected::Error &error) { - FAIL() << "Failed to recover WSV"; - }); + [](const auto &) {}, + [&](const auto &error) { FAIL() << "Failed to recover WSV"; }); res = sql_query->getDomain("test"); EXPECT_TRUE(res); diff --git a/test/module/irohad/ametsuchi/block_query_test.cpp b/test/module/irohad/ametsuchi/block_query_test.cpp index 10a423e69a..50f5d11511 100644 --- a/test/module/irohad/ametsuchi/block_query_test.cpp +++ b/test/module/irohad/ametsuchi/block_query_test.cpp @@ -85,7 +85,7 @@ class BlockQueryTest : public AmetsuchiTest { for (const auto &b : {std::move(block1), std::move(block2)}) { converter->serialize(b).match( - [this, &b](const iroha::expected::Value &json) { + [this, &b](const auto &json) { file->add(b.height(), iroha::stringToBytes(json.value)); index->index(b); blocks_total++; diff --git a/test/module/irohad/ametsuchi/postgres_query_executor_test.cpp b/test/module/irohad/ametsuchi/postgres_query_executor_test.cpp index 16fd9ece31..b72024cd27 100644 --- a/test/module/irohad/ametsuchi/postgres_query_executor_test.cpp +++ b/test/module/irohad/ametsuchi/postgres_query_executor_test.cpp @@ -791,11 +791,9 @@ namespace iroha { void commitBlocks(shared_model::interface::types::HeightType number_of_blocks = kLedgerHeight) { std::unique_ptr ms; - auto storageResult = storage->createMutableStorage(); - storageResult.match( - [&ms](iroha::expected::Value> - &storage) { ms = std::move(storage.value); }, - [](iroha::expected::Error &error) { + storage->createMutableStorage().match( + [&ms](auto &&storage) { ms = std::move(storage.value); }, + [](const auto &error) { FAIL() << "MutableStorage: " << error.error; }); @@ -1069,11 +1067,9 @@ namespace iroha { void apply(S &&storage, std::shared_ptr block) { std::unique_ptr ms; - auto storageResult = storage->createMutableStorage(); - storageResult.match( - [&](iroha::expected::Value> - &_storage) { ms = std::move(_storage.value); }, - [](iroha::expected::Error &error) { + storage->createMutableStorage().match( + [&](auto &&_storage) { ms = std::move(_storage.value); }, + [](const auto &error) { FAIL() << "MutableStorage: " << error.error; }); ms->apply(block); diff --git a/test/module/irohad/ametsuchi/storage_init_test.cpp b/test/module/irohad/ametsuchi/storage_init_test.cpp index 4f3ca547ed..b55622ae04 100644 --- a/test/module/irohad/ametsuchi/storage_init_test.cpp +++ b/test/module/irohad/ametsuchi/storage_init_test.cpp @@ -91,11 +91,11 @@ TEST_F(StorageInitTest, CreateStorageWithDatabase) { std::move(block_storage_factory_), storage_log_manager_) .match( - [&storage](const Value> &value) { + [&storage](const auto &value) { storage = value.value; SUCCEED(); }, - [](const Error &error) { FAIL() << error.error; }); + [](const auto &error) { FAIL() << error.error; }); soci::session sql(*soci::factory_postgresql(), pg_opt_without_dbname_); int size; sql << "SELECT COUNT(datname) FROM pg_catalog.pg_database WHERE datname = " @@ -120,9 +120,6 @@ TEST_F(StorageInitTest, CreateStorageWithInvalidPgOpt) { perm_converter_, std::move(block_storage_factory_), storage_log_manager_) - .match( - [](const Value> &) { - FAIL() << "storage created, but should not"; - }, - [](const Error &) { SUCCEED(); }); + .match([](const auto &) { FAIL() << "storage created, but should not"; }, + [](const auto &) { SUCCEED(); }); } diff --git a/test/module/irohad/ametsuchi/tx_presence_cache_test.cpp b/test/module/irohad/ametsuchi/tx_presence_cache_test.cpp index a41f1d0dc3..ffb526c70b 100644 --- a/test/module/irohad/ametsuchi/tx_presence_cache_test.cpp +++ b/test/module/irohad/ametsuchi/tx_presence_cache_test.cpp @@ -154,8 +154,7 @@ TEST_F(TxPresenceCacheTest, BatchHashTest) { })); batch_factory->createTransactionBatch(txs).match( - [&](iroha::expected::Value< - std::unique_ptr> &batch) { + [&](const auto &batch) { auto batch_statuses = *cache.check(*batch.value); ASSERT_EQ(3, batch_statuses.size()); tx_cache_status_responses::Rejected ts1; @@ -171,7 +170,5 @@ TEST_F(TxPresenceCacheTest, BatchHashTest) { ASSERT_EQ(hash2, ts2.hash); ASSERT_EQ(hash3, ts3.hash); }, - [&](iroha::expected::Error &error) { - FAIL() << error.error; - }); + [&](const auto &error) { FAIL() << error.error; }); } diff --git a/test/module/irohad/torii/torii_queries_test.cpp b/test/module/irohad/torii/torii_queries_test.cpp index 6b22aea202..ec3d34a18b 100644 --- a/test/module/irohad/torii/torii_queries_test.cpp +++ b/test/module/irohad/torii/torii_queries_test.cpp @@ -90,13 +90,8 @@ class ToriiQueriesTest : public testing::Test { blocks_query_factory, getTestLogger("QueryService"))) .run() - .match( - [this](iroha::expected::Value port) { - this->port = port.value; - }, - [](iroha::expected::Error err) { - FAIL() << err.error; - }); + .match([this](auto port) { this->port = port.value; }, + [](const auto &err) { FAIL() << err.error; }); runner->waitForServersReady(); } diff --git a/test/module/irohad/torii/torii_service_query_test.cpp b/test/module/irohad/torii/torii_service_query_test.cpp index 62d1ae114c..d493e239f4 100644 --- a/test/module/irohad/torii/torii_service_query_test.cpp +++ b/test/module/irohad/torii/torii_service_query_test.cpp @@ -48,13 +48,8 @@ class ToriiQueryServiceTest : public ::testing::Test { blocks_query_factory, getTestLogger("QueryService"))) .run() - .match( - [this](iroha::expected::Value port) { - this->port = port.value; - }, - [](iroha::expected::Error err) { - FAIL() << err.error; - }); + .match([this](auto port) { this->port = port.value; }, + [](const auto &err) { FAIL() << err.error; }); runner->waitForServersReady(); } diff --git a/test/module/shared_model/backend_proto/common_objects/proto_common_objects_factory_test.cpp b/test/module/shared_model/backend_proto/common_objects/proto_common_objects_factory_test.cpp index e8eeb766ee..3a8fd9fb40 100644 --- a/test/module/shared_model/backend_proto/common_objects/proto_common_objects_factory_test.cpp +++ b/test/module/shared_model/backend_proto/common_objects/proto_common_objects_factory_test.cpp @@ -38,11 +38,11 @@ TEST_F(PeerTest, ValidPeerInitialization) { auto peer = factory.createPeer(valid_address, valid_pubkey); peer.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(v.value->address(), valid_address); ASSERT_EQ(v.value->pubkey().hex(), valid_pubkey.hex()); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -53,9 +53,8 @@ TEST_F(PeerTest, ValidPeerInitialization) { TEST_F(PeerTest, InvalidPeerInitialization) { auto peer = factory.createPeer(invalid_address, valid_pubkey); - peer.match( - [](const ValueOf &v) { FAIL() << "Expected error case"; }, - [](const ErrorOf &e) { SUCCEED(); }); + peer.match([](const auto &v) { FAIL() << "Expected error case"; }, + [](const auto &e) { SUCCEED(); }); } class AccountTest : public ProtoFixture { @@ -78,13 +77,13 @@ TEST_F(AccountTest, ValidAccountInitialization) { valid_account_id, valid_domain_id, valid_quorum, valid_json); account.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(v.value->accountId(), valid_account_id); ASSERT_EQ(v.value->domainId(), valid_domain_id); ASSERT_EQ(v.value->quorum(), valid_quorum); ASSERT_EQ(v.value->jsonData(), valid_json); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -96,11 +95,8 @@ TEST_F(AccountTest, InvalidAccountInitialization) { auto account = factory.createAccount( invalid_account_id, valid_domain_id, valid_quorum, valid_json); - account.match( - [](const ValueOf &v) { - FAIL() << "Expected error case"; - }, - [](const ErrorOf &e) { SUCCEED(); }); + account.match([](const auto &v) { FAIL() << "Expected error case"; }, + [](const auto &e) { SUCCEED(); }); } class AccountAssetTest : public ProtoFixture { @@ -122,12 +118,12 @@ TEST_F(AccountAssetTest, ValidAccountAssetInitialization) { valid_account_id, valid_asset_id, valid_amount); account_asset.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(v.value->accountId(), valid_account_id); ASSERT_EQ(v.value->assetId(), valid_asset_id); ASSERT_EQ(v.value->balance(), valid_amount); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -139,11 +135,8 @@ TEST_F(AccountAssetTest, InvalidAccountAssetInitialization) { auto account_asset = factory.createAccountAsset( invalid_account_id, valid_asset_id, valid_amount); - account_asset.match( - [](const ValueOf &v) { - FAIL() << "Expected error case"; - }, - [](const ErrorOf &e) { SUCCEED(); }); + account_asset.match([](const auto &v) { FAIL() << "Expected error case"; }, + [](const auto &e) { SUCCEED(); }); } class AssetTest : public ProtoFixture { @@ -165,12 +158,12 @@ TEST_F(AssetTest, ValidAssetInitialization) { factory.createAsset(valid_asset_id, valid_domain_id, valid_precision); asset.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(v.value->assetId(), valid_asset_id); ASSERT_EQ(v.value->domainId(), valid_domain_id); ASSERT_EQ(v.value->precision(), valid_precision); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -182,11 +175,8 @@ TEST_F(AssetTest, InvalidAssetInitialization) { auto asset = factory.createAsset(invalid_asset_id, valid_domain_id, valid_precision); - asset.match( - [](const ValueOf &v) { - FAIL() << "Expected error case"; - }, - [](const ErrorOf &e) { SUCCEED(); }); + asset.match([](const auto &v) { FAIL() << "Expected error case"; }, + [](const auto &e) { SUCCEED(); }); } class DomainTest : public ProtoFixture { @@ -206,11 +196,11 @@ TEST_F(DomainTest, ValidDomainInitialization) { auto domain = factory.createDomain(valid_domain_id, valid_role_id); domain.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(v.value->domainId(), valid_domain_id); ASSERT_EQ(v.value->defaultRole(), valid_role_id); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -221,11 +211,8 @@ TEST_F(DomainTest, ValidDomainInitialization) { TEST_F(DomainTest, InvalidDomainInitialization) { auto domain = factory.createDomain(invalid_domain_id, valid_role_id); - domain.match( - [](const ValueOf &v) { - FAIL() << "Expected error case"; - }, - [](const ErrorOf &e) { SUCCEED(); }); + domain.match([](const auto &v) { FAIL() << "Expected error case"; }, + [](const auto &e) { SUCCEED(); }); } class SignatureTest : public ProtoFixture { @@ -245,11 +232,11 @@ TEST_F(SignatureTest, ValidSignatureInitialization) { auto signature = factory.createSignature(valid_pubkey, valid_data); signature.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(v.value->publicKey().hex(), valid_pubkey.hex()); ASSERT_EQ(v.value->signedData().hex(), valid_data.hex()); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -260,9 +247,6 @@ TEST_F(SignatureTest, ValidSignatureInitialization) { TEST_F(SignatureTest, InvalidSignatureInitialization) { auto signature = factory.createSignature(invalid_pubkey, valid_data); - signature.match( - [](const ValueOf &v) { - FAIL() << "Expected error case"; - }, - [](const ErrorOf &e) { SUCCEED(); }); + signature.match([](const auto &v) { FAIL() << "Expected error case"; }, + [](const auto &e) { SUCCEED(); }); } diff --git a/test/module/shared_model/backend_proto/proto_proposal_factory_test.cpp b/test/module/shared_model/backend_proto/proto_proposal_factory_test.cpp index 05e2908dad..1abf8e7a54 100644 --- a/test/module/shared_model/backend_proto/proto_proposal_factory_test.cpp +++ b/test/module/shared_model/backend_proto/proto_proposal_factory_test.cpp @@ -54,12 +54,12 @@ TEST_F(ProposalFactoryTest, ValidProposalTest) { auto proposal = valid_factory.createProposal(height, time, txs); proposal.match( - [&](const ValueOf &v) { + [&](const auto &v) { ASSERT_EQ(txs, v.value->transactions()); ASSERT_EQ(height, v.value->height()); ASSERT_EQ(time, v.value->createdTime()); }, - [](const ErrorOf &e) { FAIL() << e.error; }); + [](const auto &e) { FAIL() << e.error; }); } /** @@ -70,9 +70,6 @@ TEST_F(ProposalFactoryTest, ValidProposalTest) { TEST_F(ProposalFactoryTest, InvalidProposalTest) { auto proposal = factory.createProposal(height, time, txs); - proposal.match( - [&](const ValueOf &) { - FAIL() << "unexpected value case"; - }, - [](const ErrorOf &) { SUCCEED(); }); + proposal.match([&](const auto &) { FAIL() << "unexpected value case"; }, + [](const auto &) { SUCCEED(); }); }