Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "validators/protobuf/proto_block_validator.hpp"
7 :
8 : #include <boost/format.hpp>
9 : #include <boost/range/adaptors.hpp>
10 : #include <boost/range/algorithm/for_each.hpp>
11 :
12 : #include "validators/validators_common.hpp"
13 :
14 : namespace shared_model {
15 : namespace validation {
16 : Answer ProtoBlockValidator::validate(
17 : const iroha::protocol::Block &block) const {
18 6 : Answer answer;
19 6 : std::string tx_reason_name = "Protobuf Block";
20 6 : ReasonsGroupType reason{tx_reason_name, GroupedReasons()};
21 :
22 : // make sure version one_of field of the Block is set
23 6 : if (block.block_version_case()
24 6 : == iroha::protocol::Block::BLOCK_VERSION_NOT_SET) {
25 1 : reason.second.emplace_back("Block version is not set");
26 1 : answer.addReason(std::move(reason));
27 1 : return answer;
28 : }
29 :
30 5 : const auto &rejected_hashes =
31 5 : block.block_v1().payload().rejected_transactions_hashes();
32 :
33 5 : boost::for_each(rejected_hashes | boost::adaptors::indexed(0),
34 : [&reason](const auto &hash) {
35 2 : if (not validateHexString(hash.value())) {
36 1 : reason.second.emplace_back(
37 1 : (boost::format("Rejected hash '%s' with index "
38 : "'%d' is not in hash format")
39 1 : % hash.value() % hash.index())
40 1 : .str());
41 1 : }
42 2 : });
43 5 : if (not validateHexString(block.block_v1().payload().prev_block_hash())) {
44 1 : reason.second.emplace_back("Prev block hash has incorrect format");
45 1 : }
46 5 : if (not reason.second.empty()) {
47 2 : answer.addReason(std::move(reason));
48 2 : }
49 :
50 5 : return answer;
51 6 : }
52 : } // namespace validation
53 : } // namespace shared_model
|