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/transactions_collection/transactions_collection_validator.hpp"
7 :
8 : #include <algorithm>
9 :
10 : #include <boost/format.hpp>
11 : #include <boost/range/adaptor/indirected.hpp>
12 : #include "interfaces/common_objects/transaction_sequence_common.hpp"
13 : #include "validators/default_validator.hpp"
14 : #include "validators/field_validator.hpp"
15 : #include "validators/signable_validator.hpp"
16 : #include "validators/transaction_validator.hpp"
17 : #include "validators/transactions_collection/batch_order_validator.hpp"
18 :
19 : namespace shared_model {
20 : namespace validation {
21 :
22 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
23 : TransactionsCollectionValidator<TransactionValidator,
24 : CollectionCanBeEmpty>::
25 : TransactionsCollectionValidator(
26 : const TransactionValidator &transactions_validator)
27 757 : : transaction_validator_(transactions_validator) {}
28 :
29 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
30 : template <typename Validator>
31 : Answer TransactionsCollectionValidator<TransactionValidator,
32 : CollectionCanBeEmpty>::
33 : validateImpl(const interface::types::TransactionsForwardCollectionType
34 : &transactions,
35 : Validator &&validator) const {
36 1434 : Answer res;
37 1434 : ReasonsGroupType reason;
38 1434 : reason.first = "Transaction list";
39 :
40 1434 : auto tx_collection_empty = boost::empty(transactions);
41 1434 : if (tx_collection_empty and CollectionCanBeEmpty) {
42 1 : return res;
43 : }
44 1434 : if (tx_collection_empty) {
45 0 : reason.second.emplace_back("Transaction sequence can not be empty");
46 0 : res.addReason(std::move(reason));
47 0 : return res;
48 : }
49 :
50 2899 : for (const auto &tx : transactions) {
51 1465 : auto answer = std::forward<Validator>(validator)(tx);
52 1465 : if (answer.hasErrors()) {
53 : auto message =
54 2 : (boost::format("Tx %s : %s") % tx.hash().hex() % answer.reason())
55 2 : .str();
56 2 : reason.second.push_back(message);
57 2 : }
58 1465 : }
59 :
60 1434 : if (not reason.second.empty()) {
61 2 : res.addReason(std::move(reason));
62 2 : }
63 1434 : return res;
64 1434 : }
65 :
66 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
67 : Answer TransactionsCollectionValidator<TransactionValidator,
68 : CollectionCanBeEmpty>::
69 : validate(const shared_model::interface::types::
70 : TransactionsForwardCollectionType &transactions) const {
71 : return validateImpl(transactions, [this](const auto &tx) {
72 0 : return transaction_validator_.validate(tx);
73 : });
74 : }
75 :
76 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
77 : Answer TransactionsCollectionValidator<TransactionValidator,
78 : CollectionCanBeEmpty>::
79 : validate(const shared_model::interface::types::SharedTxsCollectionType
80 : &transactions) const {
81 0 : return validate(transactions | boost::adaptors::indirected);
82 0 : }
83 :
84 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
85 : Answer TransactionsCollectionValidator<TransactionValidator,
86 : CollectionCanBeEmpty>::
87 : validate(const interface::types::TransactionsForwardCollectionType
88 : &transactions,
89 : interface::types::TimestampType current_timestamp) const {
90 1434 : return validateImpl(
91 : transactions, [this, current_timestamp](const auto &tx) {
92 1465 : return transaction_validator_.validate(tx, current_timestamp);
93 : });
94 : }
95 :
96 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
97 : Answer TransactionsCollectionValidator<TransactionValidator,
98 : CollectionCanBeEmpty>::
99 : validate(const interface::types::SharedTxsCollectionType &transactions,
100 : interface::types::TimestampType current_timestamp) const {
101 0 : return validate(transactions | boost::adaptors::indirected,
102 0 : current_timestamp);
103 0 : }
104 :
105 : template <typename TransactionValidator, bool CollectionCanBeEmpty>
106 : const TransactionValidator &TransactionsCollectionValidator<
107 : TransactionValidator,
108 : CollectionCanBeEmpty>::getTransactionValidator() const {
109 10 : return transaction_validator_;
110 : }
111 :
112 : template class TransactionsCollectionValidator<
113 : DefaultUnsignedTransactionValidator,
114 : true>;
115 :
116 : template class TransactionsCollectionValidator<
117 : DefaultUnsignedTransactionValidator,
118 : false>;
119 :
120 : template class TransactionsCollectionValidator<
121 : DefaultSignedTransactionValidator,
122 : true>;
123 :
124 : template class TransactionsCollectionValidator<
125 : DefaultSignedTransactionValidator,
126 : false>;
127 :
128 : } // namespace validation
129 : } // namespace shared_model
|