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

Commit 2849c13

Browse files
authored
Fix Protocol and Interface Validators (#1879)
Signed-off-by: Akvinikym <[email protected]>
1 parent bd15663 commit 2849c13

21 files changed

+337
-194
lines changed

irohad/main/application.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
#include "multi_sig_transactions/transport/mst_transport_stub.hpp"
2929
#include "torii/impl/command_service_impl.hpp"
3030
#include "torii/impl/status_bus_impl.hpp"
31-
#include "validators/default_proto_validator.hpp"
3231
#include "validators/field_validator.hpp"
32+
#include "validators/protobuf/proto_query_validator.hpp"
33+
#include "validators/protobuf/proto_transaction_validator.hpp"
3334

3435
using namespace iroha;
3536
using namespace iroha::ametsuchi;
@@ -192,21 +193,43 @@ void Irohad::initNetworkClient() {
192193
}
193194

194195
void Irohad::initFactories() {
196+
// transaction factories
195197
transaction_batch_factory_ =
196198
std::make_shared<shared_model::interface::TransactionBatchFactoryImpl>();
199+
197200
std::unique_ptr<shared_model::validation::AbstractValidator<
198201
shared_model::interface::Transaction>>
199-
transaction_validator = std::make_unique<
200-
shared_model::validation::
201-
DefaultOptionalSignedProtoTransactionValidator>();
202+
transaction_validator =
203+
std::make_unique<shared_model::validation::
204+
DefaultOptionalSignedTransactionValidator>();
205+
std::unique_ptr<
206+
shared_model::validation::AbstractValidator<iroha::protocol::Transaction>>
207+
proto_transaction_validator = std::make_unique<
208+
shared_model::validation::ProtoTransactionValidator>();
202209
transaction_factory =
203210
std::make_shared<shared_model::proto::ProtoTransportFactory<
204211
shared_model::interface::Transaction,
205-
shared_model::proto::Transaction>>(std::move(transaction_validator));
212+
shared_model::proto::Transaction>>(
213+
std::move(transaction_validator),
214+
std::move(proto_transaction_validator));
206215

216+
// query factories
207217
query_response_factory_ =
208218
std::make_shared<shared_model::proto::ProtoQueryResponseFactory>();
209219

220+
std::unique_ptr<shared_model::validation::AbstractValidator<
221+
shared_model::interface::Query>>
222+
query_validator = std::make_unique<
223+
shared_model::validation::DefaultSignedQueryValidator>();
224+
std::unique_ptr<
225+
shared_model::validation::AbstractValidator<iroha::protocol::Query>>
226+
proto_query_validator =
227+
std::make_unique<shared_model::validation::ProtoQueryValidator>();
228+
query_factory = std::make_shared<
229+
shared_model::proto::ProtoTransportFactory<shared_model::interface::Query,
230+
shared_model::proto::Query>>(
231+
std::move(query_validator), std::move(proto_query_validator));
232+
210233
log_->info("[Init] => factories");
211234
}
212235

@@ -382,7 +405,8 @@ void Irohad::initQueryService() {
382405
auto query_processor = std::make_shared<QueryProcessorImpl>(
383406
storage, storage, pending_txs_storage_, query_response_factory_);
384407

385-
query_service = std::make_shared<::torii::QueryService>(query_processor);
408+
query_service =
409+
std::make_shared<::torii::QueryService>(query_processor, query_factory);
386410

387411
log_->info("[Init] => query service");
388412
}

irohad/main/application.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ namespace iroha {
4949

5050
class Irohad {
5151
public:
52-
5352
using RunResult = iroha::expected::Result<void, std::string>;
5453

5554
/**
@@ -65,8 +64,8 @@ class Irohad {
6564
* @param proposal_delay - maximum waiting time util emitting new proposal
6665
* @param vote_delay - waiting time before sending vote to next peer
6766
* @param keypair - public and private keys for crypto signer
68-
* @param opt_mst_gossip_params - parameters for Gossip MST propagation (optional).
69-
* If not provided, disables mst processing support
67+
* @param opt_mst_gossip_params - parameters for Gossip MST propagation
68+
* (optional). If not provided, disables mst processing support
7069
*
7170
* TODO mboldyrev 03.11.2018 IR-1844 Refactor the constructor.
7271
*/
@@ -223,6 +222,12 @@ class Irohad {
223222
iroha::protocol::Transaction>>
224223
transaction_factory;
225224

225+
// query factory
226+
std::shared_ptr<shared_model::interface::AbstractTransportFactory<
227+
shared_model::interface::Query,
228+
iroha::protocol::Query>>
229+
query_factory;
230+
226231
// query response factory
227232
std::shared_ptr<shared_model::interface::QueryResponseFactory>
228233
query_response_factory_;

irohad/torii/impl/query_service.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
*/
55

66
#include "torii/query_service.hpp"
7+
78
#include "backend/protobuf/query_responses/proto_block_query_response.hpp"
89
#include "backend/protobuf/query_responses/proto_query_response.hpp"
910
#include "cryptography/default_hash_provider.hpp"
11+
#include "interfaces/iroha_internal/abstract_transport_factory.hpp"
1012
#include "validators/default_validator.hpp"
1113

1214
namespace torii {
1315

1416
QueryService::QueryService(
15-
std::shared_ptr<iroha::torii::QueryProcessor> query_processor)
16-
: query_processor_(query_processor), log_(logger::log("Query Service")) {}
17+
std::shared_ptr<iroha::torii::QueryProcessor> query_processor,
18+
std::shared_ptr<QueryFactoryType> query_factory)
19+
: query_processor_{std::move(query_processor)},
20+
query_factory_{std::move(query_factory)},
21+
log_{logger::log("Query Service")} {}
1722

1823
void QueryService::Find(iroha::protocol::Query const &request,
1924
iroha::protocol::QueryResponse &response) {
@@ -28,30 +33,24 @@ namespace torii {
2833
return;
2934
}
3035

31-
shared_model::proto::TransportBuilder<
32-
shared_model::proto::Query,
33-
shared_model::validation::DefaultSignedQueryValidator>()
34-
.build(request)
35-
.match(
36-
[this, &hash, &response](
37-
const iroha::expected::Value<shared_model::proto::Query>
38-
&query) {
39-
// Send query to iroha
40-
auto result_response =
41-
static_cast<shared_model::proto::QueryResponse &>(
42-
*query_processor_->queryHandle(query.value))
43-
.getTransport();
44-
response.CopyFrom(result_response);
45-
cache_.addItem(hash, response);
46-
},
47-
[&hash,
48-
&response](const iroha::expected::Error<std::string> &error) {
49-
response.set_query_hash(hash.hex());
50-
response.mutable_error_response()->set_reason(
51-
iroha::protocol::ErrorResponse::STATELESS_INVALID);
52-
response.mutable_error_response()->set_message(
53-
std::move(error.error));
54-
});
36+
query_factory_->build(request).match(
37+
[this, &hash, &response](
38+
const iroha::expected::Value<
39+
std::unique_ptr<shared_model::interface::Query>> &query) {
40+
// Send query to iroha
41+
response = static_cast<shared_model::proto::QueryResponse &>(
42+
*query_processor_->queryHandle(*query.value))
43+
.getTransport();
44+
cache_.addItem(hash, response);
45+
},
46+
[&hash, &response](
47+
const iroha::expected::Error<QueryFactoryType::Error> &error) {
48+
response.set_query_hash(hash.hex());
49+
response.mutable_error_response()->set_reason(
50+
iroha::protocol::ErrorResponse::STATELESS_INVALID);
51+
response.mutable_error_response()->set_message(
52+
std::move(error.error.error));
53+
});
5554
}
5655

5756
grpc::Status QueryService::Find(grpc::ServerContext *context,

irohad/torii/query_service.hpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
/*
2-
Copyright 2017 Soramitsu Co., Ltd.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
165

176
#ifndef TORII_QUERY_SERVICE_HPP
187
#define TORII_QUERY_SERVICE_HPP
@@ -30,6 +19,13 @@ limitations under the License.
3019

3120
#include "logger/logger.hpp"
3221

22+
namespace shared_model {
23+
namespace interface {
24+
template <typename Interface, typename Transport>
25+
class AbstractTransportFactory;
26+
}
27+
} // namespace shared_model
28+
3329
namespace torii {
3430
/**
3531
* Actual implementation of async QueryService.
@@ -38,7 +34,13 @@ namespace torii {
3834
*/
3935
class QueryService : public iroha::protocol::QueryService::Service {
4036
public:
41-
QueryService(std::shared_ptr<iroha::torii::QueryProcessor> query_processor);
37+
using QueryFactoryType =
38+
shared_model::interface::AbstractTransportFactory<
39+
shared_model::interface::Query,
40+
iroha::protocol::Query>;
41+
42+
QueryService(std::shared_ptr<iroha::torii::QueryProcessor> query_processor,
43+
std::shared_ptr<QueryFactoryType> query_factory);
4244

4345
QueryService(const QueryService &) = delete;
4446
QueryService &operator=(const QueryService &) = delete;
@@ -63,6 +65,7 @@ namespace torii {
6365

6466
private:
6567
std::shared_ptr<iroha::torii::QueryProcessor> query_processor_;
68+
std::shared_ptr<QueryFactoryType> query_factory_;
6669

6770
iroha::cache::Cache<shared_model::crypto::Hash,
6871
iroha::protocol::QueryResponse,

shared_model/backend/protobuf/proto_transport_factory.hpp

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include "interfaces/iroha_internal/abstract_transport_factory.hpp"
1010

11+
#include "backend/protobuf/util.hpp"
12+
#include "cryptography/hash_providers/sha3_256.hpp"
1113
#include "validators/abstract_validator.hpp"
1214

1315
namespace shared_model {
@@ -23,23 +25,37 @@ namespace shared_model {
2325
typename Proto::TransportType>::Error;
2426
using ValidatorType = std::unique_ptr<
2527
shared_model::validation::AbstractValidator<Interface>>;
28+
using ProtoValidatorType =
29+
std::unique_ptr<shared_model::validation::AbstractValidator<
30+
typename Proto::TransportType>>;
2631

27-
explicit ProtoTransportFactory(ValidatorType validator)
28-
: validator_(std::move(validator)) {}
32+
ProtoTransportFactory(ValidatorType interface_validator,
33+
ProtoValidatorType proto_validator)
34+
: interface_validator_(std::move(interface_validator)),
35+
proto_validator_{std::move(proto_validator)} {}
2936

3037
iroha::expected::Result<std::unique_ptr<Interface>, Error> build(
3138
typename Proto::TransportType m) const override {
39+
if (auto answer = proto_validator_->validate(m)) {
40+
return iroha::expected::makeError(Error{
41+
HashProvider::makeHash(makeBlob(m.payload())), answer.reason()});
42+
}
43+
3244
std::unique_ptr<Interface> result =
3345
std::make_unique<Proto>(std::move(m));
34-
if (auto answer = validator_->validate(*result)) {
46+
if (auto answer = interface_validator_->validate(*result)) {
3547
return iroha::expected::makeError(
3648
Error{result->hash(), answer.reason()});
3749
}
50+
3851
return iroha::expected::makeValue(std::move(result));
3952
}
4053

4154
private:
42-
ValidatorType validator_;
55+
using HashProvider = shared_model::crypto::Sha3_256;
56+
57+
ValidatorType interface_validator_;
58+
ProtoValidatorType proto_validator_;
4359
};
4460

4561
} // namespace proto

shared_model/validators/default_proto_validator.hpp

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#ifndef IROHA_PROTO_QUERY_VALIDATOR_HPP
7+
#define IROHA_PROTO_QUERY_VALIDATOR_HPP
8+
9+
#include "validators/abstract_validator.hpp"
10+
11+
#include "queries.pb.h"
12+
13+
namespace shared_model {
14+
namespace validation {
15+
16+
class ProtoQueryValidator
17+
: public AbstractValidator<iroha::protocol::Query> {
18+
private:
19+
Answer validateProtoQuery(const iroha::protocol::Query &qry) const {
20+
Answer answer;
21+
if (qry.payload().query_case()
22+
== iroha::protocol::Query_Payload::QUERY_NOT_SET) {
23+
ReasonsGroupType reason;
24+
reason.first = "Undefined";
25+
reason.second.emplace_back("query is undefined");
26+
answer.addReason(std::move(reason));
27+
}
28+
return answer;
29+
}
30+
31+
public:
32+
Answer validate(const iroha::protocol::Query &query) const override {
33+
return validateProtoQuery(query);
34+
}
35+
};
36+
37+
} // namespace validation
38+
} // namespace shared_model
39+
40+
#endif // IROHA_PROTO_QUERY_VALIDATOR_HPP

shared_model/validators/protobuf/proto_transaction_validator.hpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
#ifndef IROHA_PROTO_TRANSACTION_VALIDATOR_HPP
77
#define IROHA_PROTO_TRANSACTION_VALIDATOR_HPP
88

9-
#include "validators/transaction_validator.hpp"
9+
#include "validators/abstract_validator.hpp"
1010

11-
#include "backend/protobuf/transaction.hpp"
11+
#include "transaction.pb.h"
1212

1313
namespace shared_model {
1414
namespace validation {
1515

16-
template <typename FieldValidator, typename CommandValidator>
1716
class ProtoTransactionValidator
18-
: public TransactionValidator<FieldValidator, CommandValidator> {
17+
: public AbstractValidator<iroha::protocol::Transaction> {
1918
private:
2019
Answer validateProtoTx(
2120
const iroha::protocol::Transaction &transaction) const {
@@ -70,18 +69,8 @@ namespace shared_model {
7069
}
7170

7271
public:
73-
using TransactionValidator<FieldValidator,
74-
CommandValidator>::TransactionValidator;
75-
Answer validate(const interface::Transaction &tx) const override {
76-
// validate proto-backend of transaction
77-
auto proto_validation_answer = validateProtoTx(
78-
static_cast<const proto::Transaction &>(tx).getTransport());
79-
if (proto_validation_answer.hasErrors()) {
80-
return proto_validation_answer;
81-
}
82-
83-
return TransactionValidator<FieldValidator, CommandValidator>::validate(
84-
tx);
72+
Answer validate(const iroha::protocol::Transaction &tx) const override {
73+
return validateProtoTx(tx);
8574
};
8675
};
8776
} // namespace validation

0 commit comments

Comments
 (0)