Skip to content

Commit

Permalink
ProtoCommandValidator: separate from ProtoTransactionValidator
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Lebedev <lebdron@gmail.com>
  • Loading branch information
lebdron committed Aug 21, 2019
1 parent d3092cf commit cf2a720
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 73 deletions.
1 change: 1 addition & 0 deletions shared_model/validators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(shared_model_stateless_validation
transactions_collection/batch_order_validator.cpp
protobuf/proto_block_validator.cpp
protobuf/proto_query_validator.cpp
protobuf/proto_command_validator.cpp
protobuf/proto_transaction_validator.cpp
protobuf/proto_proposal_validator.cpp
transaction_batch_validator.cpp
Expand Down
88 changes: 88 additions & 0 deletions shared_model/validators/protobuf/proto_command_validator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "validators/protobuf/proto_command_validator.hpp"

#include "commands.pb.h"
#include "validators/validators_common.hpp"

namespace shared_model {
namespace validation {

void validatePublicKey(const std::string &public_key,
ReasonsGroupType &reason) {
if (not validateHexString(public_key)) {
reason.second.emplace_back("Public key is not in hex format");
}
}

Answer ProtoCommandValidator::validate(
const iroha::protocol::Command &command) const {
Answer answer;
std::string tx_reason_name = "Protobuf Command";
ReasonsGroupType reason(tx_reason_name, GroupedReasons());
switch (command.command_case()) {
case iroha::protocol::Command::COMMAND_NOT_SET: {
reason.second.emplace_back("Undefined command is found");
answer.addReason(std::move(reason));
return answer;
}
case iroha::protocol::Command::kAddSignatory: {
const auto &as = command.add_signatory();
validatePublicKey(as.public_key(), reason);
break;
}
case iroha::protocol::Command::kCreateAccount: {
const auto &ca = command.create_account();
validatePublicKey(ca.public_key(), reason);
break;
}
case iroha::protocol::Command::kRemoveSignatory: {
const auto &rs = command.remove_signatory();
validatePublicKey(rs.public_key(), reason);
break;
}
case iroha::protocol::Command::kAddPeer: {
const auto &ap = command.add_peer();
validatePublicKey(ap.peer().peer_key(), reason);
break;
}
case iroha::protocol::Command::kCreateRole: {
const auto &cr = command.create_role();
bool all_permissions_valid = std::all_of(
cr.permissions().begin(),
cr.permissions().end(),
[](const auto &perm) {
return iroha::protocol::RolePermission_IsValid(perm);
});
if (not all_permissions_valid) {
reason.second.emplace_back("Invalid role permission");
}
break;
}
case iroha::protocol::Command::kGrantPermission: {
if (not iroha::protocol::GrantablePermission_IsValid(
command.grant_permission().permission())) {
reason.second.emplace_back("Invalid grantable permission");
}
break;
}
case iroha::protocol::Command::kRevokePermission: {
if (not iroha::protocol::GrantablePermission_IsValid(
command.revoke_permission().permission())) {
reason.second.emplace_back("Invalid grantable permission");
}
break;
}
default:
break;
}
if (not reason.second.empty()) {
answer.addReason(std::move(reason));
}
return answer;
}
} // namespace validation
} // namespace shared_model
28 changes: 28 additions & 0 deletions shared_model/validators/protobuf/proto_command_validator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#ifndef IROHA_PROTO_COMMAND_VALIDATOR_HPP
#define IROHA_PROTO_COMMAND_VALIDATOR_HPP

#include "validators/abstract_validator.hpp"

namespace iroha {
namespace protocol {
class Command;
}
} // namespace iroha

namespace shared_model {
namespace validation {

class ProtoCommandValidator
: public AbstractValidator<iroha::protocol::Command> {
public:
Answer validate(const iroha::protocol::Command &command) const override;
};
} // namespace validation
} // namespace shared_model

#endif // IROHA_PROTO_COMMAND_VALIDATOR_HPP
81 changes: 9 additions & 72 deletions shared_model/validators/protobuf/proto_transaction_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,84 +5,26 @@

#include "validators/protobuf/proto_transaction_validator.hpp"

#include "transaction.pb.h"
#include "validators/validators_common.hpp"

namespace shared_model {
namespace validation {

void validatePublicKey(const std::string &public_key,
ReasonsGroupType &reason) {
if (not validateHexString(public_key)) {
reason.second.emplace_back("Public key is not in hex format");
}
}

Answer validateProtoTx(const iroha::protocol::Transaction &transaction) {
Answer ProtoTransactionValidator::validate(
const iroha::protocol::Transaction &tx) const {
Answer answer;
std::string tx_reason_name = "Protobuf Transaction";
ReasonsGroupType reason(tx_reason_name, GroupedReasons());
for (const auto &command :
transaction.payload().reduced_payload().commands()) {
switch (command.command_case()) {
case iroha::protocol::Command::COMMAND_NOT_SET: {
reason.second.emplace_back("Undefined command is found");
answer.addReason(std::move(reason));
return answer;
}
case iroha::protocol::Command::kAddSignatory: {
const auto &as = command.add_signatory();
validatePublicKey(as.public_key(), reason);
break;
}
case iroha::protocol::Command::kCreateAccount: {
const auto &ca = command.create_account();
validatePublicKey(ca.public_key(), reason);
break;
}
case iroha::protocol::Command::kRemoveSignatory: {
const auto &rs = command.remove_signatory();
validatePublicKey(rs.public_key(), reason);
break;
}
case iroha::protocol::Command::kAddPeer: {
const auto &ap = command.add_peer();
validatePublicKey(ap.peer().peer_key(), reason);
break;
}
case iroha::protocol::Command::kCreateRole: {
const auto &cr = command.create_role();
bool all_permissions_valid = std::all_of(
cr.permissions().begin(),
cr.permissions().end(),
[](const auto &perm) {
return iroha::protocol::RolePermission_IsValid(perm);
});
if (not all_permissions_valid) {
reason.second.emplace_back("Invalid role permission");
}
break;
}
case iroha::protocol::Command::kGrantPermission: {
if (not iroha::protocol::GrantablePermission_IsValid(
command.grant_permission().permission())) {
reason.second.emplace_back("Invalid grantable permission");
}
break;
}
case iroha::protocol::Command::kRevokePermission: {
if (not iroha::protocol::GrantablePermission_IsValid(
command.revoke_permission().permission())) {
reason.second.emplace_back("Invalid grantable permission");
}
break;
}
default:
break;
for (const auto &command : tx.payload().reduced_payload().commands()) {
auto result = command_validator_.validate(command);
if (result.hasErrors()) {
reason.second.push_back(result.reason());
}
}
if (transaction.payload().has_batch()) {
if (tx.payload().has_batch()) {
if (not iroha::protocol::Transaction_Payload_BatchMeta::
BatchType_IsValid(transaction.payload().batch().type())) {
BatchType_IsValid(tx.payload().batch().type())) {
reason.second.emplace_back("Invalid batch type");
}
}
Expand All @@ -91,10 +33,5 @@ namespace shared_model {
}
return answer;
}

Answer ProtoTransactionValidator::validate(
const iroha::protocol::Transaction &tx) const {
return validateProtoTx(tx);
}
} // namespace validation
} // namespace shared_model
11 changes: 10 additions & 1 deletion shared_model/validators/protobuf/proto_transaction_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@

#include "validators/abstract_validator.hpp"

#include "transaction.pb.h"
#include "validators/protobuf/proto_command_validator.hpp"

namespace iroha {
namespace protocol {
class Transaction;
}
} // namespace iroha

namespace shared_model {
namespace validation {
Expand All @@ -17,6 +23,9 @@ namespace shared_model {
: public AbstractValidator<iroha::protocol::Transaction> {
public:
Answer validate(const iroha::protocol::Transaction &tx) const override;

private:
ProtoCommandValidator command_validator_;
};
} // namespace validation
} // namespace shared_model
Expand Down

0 comments on commit cf2a720

Please sign in to comment.