Skip to content

Commit

Permalink
Unity build: prepare support in irohad
Browse files Browse the repository at this point in the history
Network transaction endpoints: remove code duplication and make more
strict validation

Signed-off-by: Andrei Lebedev <lebdron@gmail.com>
  • Loading branch information
lebdron committed Oct 21, 2019
1 parent ba7b1f1 commit fbe494d
Show file tree
Hide file tree
Showing 18 changed files with 285 additions and 228 deletions.
1 change: 1 addition & 0 deletions irohad/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ add_library(ametsuchi
impl/peer_query_wsv.cpp
impl/postgres_block_query.cpp
impl/postgres_setting_query.cpp
impl/executor_common.cpp
impl/postgres_command_executor.cpp
impl/postgres_indexer.cpp
impl/postgres_block_index.cpp
Expand Down
29 changes: 29 additions & 0 deletions irohad/ametsuchi/impl/executor_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "ametsuchi/impl/executor_common.hpp"

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include "interfaces/permissions.hpp"

namespace iroha {
namespace ametsuchi {

const std::string kRootRolePermStr{
shared_model::interface::RolePermissionSet(
{shared_model::interface::permissions::Role::kRoot})
.toBitstring()};

shared_model::interface::types::DomainIdType getDomainFromName(
const shared_model::interface::types::AccountIdType &account_id) {
// TODO 03.10.18 andrei: IR-1728 Move getDomainFromName to shared_model
std::vector<std::string> res;
boost::split(res, account_id, boost::is_any_of("@"));
return res.at(1);
}

} // namespace ametsuchi
} // namespace iroha
22 changes: 22 additions & 0 deletions irohad/ametsuchi/impl/executor_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_AMETSUCHI_EXECUTOR_COMMON_HPP
#define IROHA_AMETSUCHI_EXECUTOR_COMMON_HPP

#include "interfaces/common_objects/types.hpp"

namespace iroha {
namespace ametsuchi {

extern const std::string kRootRolePermStr;

shared_model::interface::types::DomainIdType getDomainFromName(
const shared_model::interface::types::AccountIdType &account_id);

} // namespace ametsuchi
} // namespace iroha

#endif // IROHA_AMETSUCHI_EXECUTOR_COMMON_HPP
14 changes: 2 additions & 12 deletions irohad/ametsuchi/impl/postgres_command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/format.hpp>
#include "ametsuchi/impl/executor_common.hpp"
#include "ametsuchi/impl/soci_utils.hpp"
#include "cryptography/public_key.hpp"
#include "interfaces/commands/add_asset_quantity.hpp"
Expand Down Expand Up @@ -51,9 +52,6 @@ namespace {
const std::string kPgTrue{"true"};
const std::string kPgFalse{"false"};

const auto kRootRolePermStr =
shared_model::interface::RolePermissionSet({Role::kRoot}).toBitstring();

std::string makeJsonString(std::string value) {
return std::string{"\""} + value + "\"";
}
Expand Down Expand Up @@ -188,7 +186,7 @@ namespace {
JOIN account_has_roles AS ar on ar.role_id = rp.role_id
WHERE ar.account_id = %4%)")
% kRolePermissionSetSize % permission_bitstring
% kRootRolePermStr % account_id)
% iroha::ametsuchi::kRootRolePermStr % account_id)
.str();
return query;
}
Expand Down Expand Up @@ -224,14 +222,6 @@ namespace {
return query;
}

shared_model::interface::types::DomainIdType getDomainFromName(
const shared_model::interface::types::AccountIdType &account_id) {
// TODO 03.10.18 andrei: IR-1728 Move getDomainFromName to shared_model
std::vector<std::string> res;
boost::split(res, account_id, boost::is_any_of("@"));
return res.at(1);
}

/**
* Generate an SQL subquery which checks if creator has corresponding
* permissions for target account
Expand Down
24 changes: 7 additions & 17 deletions irohad/ametsuchi/impl/postgres_specific_query_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "ametsuchi/impl/postgres_specific_query_executor.hpp"

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/format.hpp>
Expand All @@ -14,6 +13,7 @@
#include <boost/range/algorithm/transform.hpp>
#include <boost/range/irange.hpp>
#include "ametsuchi/block_storage.hpp"
#include "ametsuchi/impl/executor_common.hpp"
#include "ametsuchi/impl/soci_utils.hpp"
#include "backend/plain/account_detail_record_id.hpp"
#include "backend/plain/peer.hpp"
Expand Down Expand Up @@ -48,25 +48,15 @@ namespace {

using namespace iroha;

const auto kRootRolePermStr =
shared_model::interface::RolePermissionSet({Role::kRoot}).toBitstring();

shared_model::interface::types::DomainIdType getDomainFromName(
const shared_model::interface::types::AccountIdType &account_id) {
// TODO 03.10.18 andrei: IR-1728 Move getDomainFromName to shared_model
std::vector<std::string> res;
boost::split(res, account_id, boost::is_any_of("@"));
return res.at(1);
}

std::string getAccountRolePermissionCheckSql(
shared_model::interface::permissions::Role permission,
const std::string &account_alias = ":role_account_id") {
const auto perm_str =
shared_model::interface::RolePermissionSet({permission}).toBitstring();
const auto bits = shared_model::interface::RolePermissionSet::size();
// TODO 14.09.18 andrei: IR-1708 Load SQL from separate files
std::string query = (boost::format(R"(
std::string query =
(boost::format(R"(
SELECT
(
COALESCE(bit_or(rp.permission), '0'::bit(%1%))
Expand All @@ -76,8 +66,8 @@ namespace {
FROM role_has_permissions AS rp
JOIN account_has_roles AS ar on ar.role_id = rp.role_id
WHERE ar.account_id = %4%)")
% bits % perm_str % kRootRolePermStr % account_alias)
.str();
% bits % perm_str % iroha::ametsuchi::kRootRolePermStr % account_alias)
.str();
return query;
}

Expand Down Expand Up @@ -135,8 +125,8 @@ namespace {

return (cmd % getAccountRolePermissionCheckSql(Role::kRoot, creator_quoted)
% bits % creator % perm_str % all_perm_str % domain_perm_str
% target_account % getDomainFromName(creator)
% getDomainFromName(target_account))
% target_account % iroha::ametsuchi::getDomainFromName(creator)
% iroha::ametsuchi::getDomainFromName(target_account))
.str();
}

Expand Down
94 changes: 39 additions & 55 deletions irohad/multi_sig_transactions/transport/impl/mst_transport_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include "ametsuchi/tx_presence_cache.hpp"
#include "backend/protobuf/deserialize_repeated_transactions.hpp"
#include "backend/protobuf/transaction.hpp"
#include "interfaces/iroha_internal/parse_and_create_batches.hpp"
#include "interfaces/iroha_internal/transaction_batch.hpp"
#include "interfaces/transaction.hpp"
#include "logger/logger.hpp"
Expand Down Expand Up @@ -57,70 +59,52 @@ MstTransportGrpc::MstTransportGrpc(
log_(std::move(log)),
sender_factory_(sender_factory) {}

shared_model::interface::types::SharedTxsCollectionType
MstTransportGrpc::deserializeTransactions(const transport::MstState *request) {
return boost::copy_range<
shared_model::interface::types::SharedTxsCollectionType>(
request->transactions()
| boost::adaptors::transformed(
[&](const auto &tx) { return transaction_factory_->build(tx); })
| boost::adaptors::filtered([&](const auto &result) {
return result.match(
[](const auto &) { return true; },
[&](const auto &error) {
log_->info("Transaction deserialization failed: hash {}, {}",
error.error.hash,
error.error.error);
return false;
});
})
| boost::adaptors::transformed([&](auto result) {
return std::move(
boost::get<iroha::expected::ValueOf<decltype(result)>>(
result))
.value;
}));
}

grpc::Status MstTransportGrpc::SendState(
::grpc::ServerContext *context,
const ::iroha::network::transport::MstState *request,
::google::protobuf::Empty *response) {
log_->info("MstState Received");
auto transactions = deserializeTransactions(request);

auto batches = batch_parser_->parseBatches(transactions);
auto transactions = shared_model::proto::deserializeTransactions(
*transaction_factory_, request->transactions());
if (auto e = expected::resultToOptionalError(transactions)) {
log_->warn(
"Transaction deserialization failed: hash {}, {}", e->hash, e->error);
return ::grpc::Status::OK;
}

auto batches = shared_model::interface::parseAndCreateBatches(
*batch_parser_,
*batch_factory_,
*expected::resultToOptionalValue(std::move(transactions)));
if (auto e = expected::resultToOptionalError(batches)) {
log_->warn("Batch deserialization failed: {}", *e);
return ::grpc::Status::OK;
}
MstState new_state = MstState::empty(mst_state_logger_, mst_completer_);

for (auto &batch : batches) {
batch_factory_->createTransactionBatch(batch).match(
[&](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
log_->warn("Check tx presence database error. Batch: {}",
*value.value);
return;
}
auto is_replay = std::any_of(
cache_presence->begin(),
cache_presence->end(),
[](const auto &tx_status) {
return iroha::visit_in_place(
tx_status,
[](const iroha::ametsuchi::tx_cache_status_responses::
Missing &) { return false; },
[](const auto &) { return true; });
});

if (not is_replay) {
new_state += std::move(value).value;
}
},
[&](const auto &error) {
log_->warn("Batch deserialization failed: {}", error.error);
auto opt_batches = expected::resultToOptionalValue(std::move(batches));
for (auto &batch : *opt_batches) {
auto cache_presence = tx_presence_cache_->check(*batch);
if (not cache_presence) {
// TODO andrei 30.11.18 IR-51 Handle database error
log_->warn("Check tx presence database error. Batch: {}", *batch);
continue;
}
auto is_replay = std::any_of(
cache_presence->begin(),
cache_presence->end(),
[](const auto &tx_status) {
return iroha::visit_in_place(
tx_status,
[](const iroha::ametsuchi::tx_cache_status_responses::Missing &) {
return false;
},
[](const auto &) { return true; });
});

if (not is_replay) {
new_state += std::move(batch);
}
}

log_->info("batches in MstState: {}", new_state.getBatches().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ namespace iroha {
ConstRefState providing_state) override;

private:
/**
* Flat map transport transactions to shared model
*/
shared_model::interface::types::SharedTxsCollectionType
deserializeTransactions(const transport::MstState *request);

std::weak_ptr<MstTransportNotification> subscriber_;
std::shared_ptr<network::AsyncGrpcClient<google::protobuf::Empty>>
async_call_;
Expand Down
55 changes: 18 additions & 37 deletions irohad/ordering/impl/on_demand_os_server_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

#include <boost/range/adaptor/filtered.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include "backend/protobuf/deserialize_repeated_transactions.hpp"
#include "backend/protobuf/proposal.hpp"
#include "common/bind.hpp"
#include "interfaces/iroha_internal/parse_and_create_batches.hpp"
#include "interfaces/iroha_internal/transaction_batch.hpp"
#include "logger/logger.hpp"

Expand All @@ -31,50 +33,29 @@ OnDemandOsServerGrpc::OnDemandOsServerGrpc(
batch_factory_(std::move(transaction_batch_factory)),
log_(std::move(log)) {}

shared_model::interface::types::SharedTxsCollectionType
OnDemandOsServerGrpc::deserializeTransactions(
const proto::BatchesRequest *request) {
return boost::copy_range<
shared_model::interface::types::SharedTxsCollectionType>(
request->transactions()
| boost::adaptors::transformed(
[&](const auto &tx) { return transaction_factory_->build(tx); })
| boost::adaptors::filtered([&](const auto &result) {
return result.match(
[](const auto &) { return true; },
[&](const auto &error) {
log_->info("Transaction deserialization failed: hash {}, {}",
error.error.hash,
error.error.error);
return false;
});
})
| boost::adaptors::transformed([](auto result) {
return std::move(
boost::get<iroha::expected::ValueOf<decltype(result)>>(
result))
.value;
}));
}

grpc::Status OnDemandOsServerGrpc::SendBatches(
::grpc::ServerContext *context,
const proto::BatchesRequest *request,
::google::protobuf::Empty *response) {
auto transactions = deserializeTransactions(request);

auto batch_candidates = batch_parser_->parseBatches(std::move(transactions));
auto transactions = shared_model::proto::deserializeTransactions(
*transaction_factory_, request->transactions());
if (auto e = expected::resultToOptionalError(transactions)) {
log_->warn(
"Transaction deserialization failed: hash {}, {}", e->hash, e->error);
return ::grpc::Status::OK;
}

OdOsNotification::CollectionType batches;
for (auto &cand : batch_candidates) {
batch_factory_->createTransactionBatch(cand).match(
[&](auto &&value) { batches.push_back(std::move(value).value); },
[&](const auto &error) {
log_->warn("Batch deserialization failed: {}", error.error);
});
auto batches = shared_model::interface::parseAndCreateBatches(
*batch_parser_,
*batch_factory_,
*expected::resultToOptionalValue(std::move(transactions)));
if (auto e = expected::resultToOptionalError(batches)) {
log_->warn("Batch deserialization failed: {}", *e);
return ::grpc::Status::OK;
}

ordering_service_->onBatches(std::move(batches));
ordering_service_->onBatches(
*expected::resultToOptionalValue(std::move(batches)));

return ::grpc::Status::OK;
}
Expand Down
6 changes: 0 additions & 6 deletions irohad/ordering/impl/on_demand_os_server_grpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ namespace iroha {
proto::ProposalResponse *response) override;

private:
/**
* Flat map transport transactions to shared model
*/
shared_model::interface::types::SharedTxsCollectionType
deserializeTransactions(const proto::BatchesRequest *request);

std::shared_ptr<OdOsNotification> ordering_service_;

std::shared_ptr<TransportFactoryType> transaction_factory_;
Expand Down

0 comments on commit fbe494d

Please sign in to comment.