hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
block_validator.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_BLOCK_VALIDATOR_HPP
7 #define IROHA_BLOCK_VALIDATOR_HPP
8 
9 #include <boost/format.hpp>
10 #include <boost/range/adaptor/indexed.hpp>
11 #include "datetime/time.hpp"
16 #include "validators/answer.hpp"
18 
19 namespace shared_model {
20  namespace validation {
21 
25  template <typename FieldValidator, typename TransactionsCollectionValidator>
27  : public ContainerValidator<interface::Block,
28  FieldValidator,
29  TransactionsCollectionValidator>,
30  public AbstractValidator<interface::Block> {
31  public:
32  using ContainerValidator<
41  Answer validate(const interface::Block &block) const override {
42  Answer answer = ContainerValidator<interface::Block,
43  FieldValidator,
45  validate(block, "Block", [this](auto &reason, const auto &cont) {
46  this->field_validator_.validateHash(reason, cont.prevHash());
47  });
48 
49  validation::ReasonsGroupType block_reason;
50  std::unordered_set<std::string> hashes = {};
51 
52  auto rejected_hashes = block.rejected_transactions_hashes();
53 
54  for (const auto &hash : rejected_hashes | boost::adaptors::indexed(0)) {
55  if (hashes.count(hash.value().hex())) {
56  block_reason.second.emplace_back(
57  (boost::format("Rejected hash '%s' with index "
58  "'%d' has already appeared in a block")
59  % hash.value().hex() % hash.index())
60  .str());
61  } else {
62  hashes.insert(hash.value().hex());
63  }
64  }
65 
66  auto transaction = block.transactions();
67 
68  for (const auto &tx : transaction | boost::adaptors::indexed(0)) {
69  auto hex = tx.value().hash().hex();
70  if (hashes.count(hex)) {
71  block_reason.second.emplace_back(
72  (boost::format("Hash '%s' of transaction "
73  "'%d' has already appeared in rejected hashes")
74  % hex % tx.index())
75  .str());
76  }
77  }
78 
79  if (not block_reason.second.empty()) {
80  answer.addReason(std::move(block_reason));
81  }
82 
83  return answer;
84  }
85  };
86 
87  } // namespace validation
88 } // namespace shared_model
89 
90 #endif // IROHA_BLOCK_VALIDATOR_HPP
virtual interface::types::HashCollectionType rejected_transactions_hashes() const =0
Definition: block_validator.hpp:26
Definition: container_validator.hpp:25
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
Definition: abstract_validator.hpp:16
ContainerValidator(FieldValidator field_validator, TransactionsCollectionValidator transactions_collection_validator)
Definition: container_validator.hpp:38
Definition: block.hpp:16
virtual types::TransactionsCollectionType transactions() const =0
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
Answer validate(const interface::Block &block) const override
Definition: block_validator.hpp:41
void validateHash(ReasonsGroupType &reason, const crypto::Hash &hash) const
Definition: field_validator.cpp:381
Definition: field_validator.hpp:35
std::pair< ReasonsGroupName, GroupedReasons > ReasonsGroupType
Definition: answer.hpp:22
Definition: command_executor.hpp:12
Definition: answer.hpp:28
Definition: transactions_collection_validator.hpp:22