Skip to content

Commit

Permalink
sv block proposal hyperledger-iroha#1041
Browse files Browse the repository at this point in the history
Signed-off-by: Moonraker <[email protected]>
  • Loading branch information
Solonets authored and x3medima17 committed Mar 30, 2018
1 parent 1022749 commit 5400665
Show file tree
Hide file tree
Showing 24 changed files with 338 additions and 117 deletions.
4 changes: 2 additions & 2 deletions irohad/network/impl/block_loader_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ rxcpp::observable<std::shared_ptr<Block>> BlockLoaderImpl::retrieveBlocks(
std::make_shared<shared_model::proto::Block>(std::move(block));

// stateless validation of block
auto answer = stateless_validator_->validate(result);
auto answer = stateless_validator_->validate(*result);
if (answer.hasErrors()) {
log_->error(answer.reason());
context.TryCancel();
Expand Down Expand Up @@ -137,7 +137,7 @@ boost::optional<std::shared_ptr<Block>> BlockLoaderImpl::retrieveBlock(
}

// stateless validation of block
auto answer = stateless_validator_->validate(result);
auto answer = stateless_validator_->validate(*result);
if (answer.hasErrors()) {
log_->error(answer.reason());
return boost::none;
Expand Down
2 changes: 1 addition & 1 deletion irohad/simulator/impl/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace iroha {
});
new_block.transactions = txs;
new_block.txs_number = proposal.transactions().size();
new_block.created_ts = proposal.created_time();
new_block.created_ts = proposal.createdTime();
new_block.hash = hash(new_block);
crypto_provider_->sign(new_block);

Expand Down
4 changes: 2 additions & 2 deletions irohad/validation/impl/stateful_validator_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ namespace iroha {
*polymorphic_tx.operator->());
});
auto validated_proposal = shared_model::proto::ProposalBuilder()
.createdTime(proposal.created_time())
.createdTime(proposal.createdTime())
.height(proposal.height())
.transactions(valid_proto_txs)
.createdTime(proposal.created_time())
.createdTime(proposal.createdTime())
.build();

log_->info("transactions in verified proposal: {}",
Expand Down
2 changes: 1 addition & 1 deletion shared_model/backend/protobuf/proposal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace shared_model {
return *transactions_;
}

interface::types::TimestampType created_time() const override {
interface::types::TimestampType createdTime() const override {
return proto_->created_time();
}

Expand Down
3 changes: 2 additions & 1 deletion shared_model/interfaces/iroha_internal/proposal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace shared_model {
/**
* @return created time
*/
virtual types::TimestampType created_time() const = 0;
virtual types::TimestampType createdTime() const = 0;

#ifndef DISABLE_BACKWARD
iroha::model::Proposal *makeOldModel() const override {
Expand All @@ -67,6 +67,7 @@ namespace shared_model {

auto oldModel = new iroha::model::Proposal(txs);
oldModel->height = height();
oldModel->created_time = createdTime();
return oldModel;
}
#endif
Expand Down
26 changes: 10 additions & 16 deletions shared_model/validators/block_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef IROHA_BLOCK_VALIDATOR_HPP
#define IROHA_BLOCK_VALIDATOR_HPP

#include <boost/format.hpp>
#include "validators/container_validator.hpp"
#include "datetime/time.hpp"
#include "interfaces/common_objects/types.hpp"
#include "interfaces/iroha_internal/block.hpp"
Expand All @@ -32,30 +34,22 @@ namespace shared_model {
/**
* Class that validates block
*/
class BlockValidator {
template <typename FieldValidator, typename TransactionValidator>
class BlockValidator : public ContainerValidator<interface::Block,
FieldValidator,
TransactionValidator> {
public:

//TODO 05-03-2018 Alexey Chernyshov: remove polymorphic wrapper in IR-872
/**
* Applies validation on block
* @param block
* @return Answer containing found error if any
*/
Answer validate(const interface::Block &block) const {
return Answer();
return ContainerValidator<interface::Block,
FieldValidator,
TransactionValidator>::validate(block,
"Block");
}

/**
* Applies validation on block
* @param block
* @return Answer containing found error if any
*/
Answer validate(
std::shared_ptr<interface::Block> block) const {
return Answer();
}

Answer answer_;
};

} // namespace validation
Expand Down
95 changes: 95 additions & 0 deletions shared_model/validators/container_validator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_CONTAINER_VALIDATOR_HPP
#define IROHA_CONTAINER_VALIDATOR_HPP
#include <boost/format.hpp>
#include "datetime/time.hpp"
#include "interfaces/common_objects/types.hpp"
#include "interfaces/iroha_internal/block.hpp"
#include "utils/polymorphic_wrapper.hpp"
#include "validators/answer.hpp"

// TODO 22/01/2018 x3medima17: write stateless validator IR-837

namespace shared_model {
namespace validation {

/**
* Class that validates blocks and proposal common fieds
*/
template <typename Iface, typename FieldValidator, typename TransactionValidator>
class ContainerValidator {
protected:
void validateHeight(ReasonsGroupType &reason,
const interface::types::HeightType &height) const {
if (height <= 0) {
auto message =
(boost::format("Height should be > 0, passed value: %d") % height)
.str();
reason.second.push_back(message);
}
}
void validateTransaction(
ReasonsGroupType &reason,
const interface::Transaction &transaction) const {
auto answer = transaction_validator_.validate(transaction);
if (answer.hasErrors()) {
auto message = (boost::format("Tx #%d: %s")
% transaction.transactionCounter() % answer.reason())
.str();
reason.second.push_back(message);
}
}
void validateTransactions(
ReasonsGroupType &reason,
const interface::types::TransactionsCollectionType &transactions)
const {
for (const auto &tx : transactions) {
validateTransaction(reason, *tx);
}
}

public:
ContainerValidator(
const TransactionValidator &transaction_validator =
TransactionValidator(),
const FieldValidator &field_validator = FieldValidator())
: transaction_validator_(transaction_validator),
field_validator_(field_validator) {}
Answer validate(const Iface &cont, std::string reason_name) const {
Answer answer;
ReasonsGroupType reason;
reason.first = reason_name;
field_validator_.validateCreatedTime(reason, cont.createdTime());
validateHeight(reason, cont.height());
validateTransactions(reason, cont.transactions());
if (not reason.second.empty()) {
answer.addReason(std::move(reason));
}
return answer;
}
private:
TransactionValidator transaction_validator_;
protected:
FieldValidator field_validator_;
};

} // namespace validation
} // namespace shared_model

#endif // IROHA_CONTAINER_VALIDATOR_HPP
15 changes: 13 additions & 2 deletions shared_model/validators/default_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ namespace shared_model {
CommandValidatorVisitor<FieldValidator>>;
using DefaultQueryValidator =
QueryValidator<FieldValidator, QueryValidatorVisitor<FieldValidator>>;
using DefaultProposalValidator = ProposalValidator;
using DefaultProposalValidator =
ProposalValidator<FieldValidator, DefaultTransactionValidator>;

using DefaultBlockValidator = BlockValidator;
using DefaultBlockValidator =
BlockValidator<FieldValidator, DefaultTransactionValidator>;

using DefaultSignableTransactionValidator =
SignableModelValidator<DefaultTransactionValidator,
Expand All @@ -46,6 +48,15 @@ namespace shared_model {
const interface::Query &,
FieldValidator>;

using DefaultSignableProposalValidator =
SignableModelValidator<DefaultProposalValidator,
const interface::Proposal &,
FieldValidator>;

using DefaultSignableBlockValidator =
SignableModelValidator<DefaultBlockValidator,
const interface::Block &,
FieldValidator>;
} // namespace validation
} // namespace shared_model

Expand Down
38 changes: 9 additions & 29 deletions shared_model/validators/proposal_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#ifndef IROHA_PROPOSAL_VALIDATOR_HPP
#define IROHA_PROPOSAL_VALIDATOR_HPP

#include <boost/format.hpp>
#include <regex>
#include "datetime/time.hpp"
#include "interfaces/common_objects/types.hpp"
Expand All @@ -32,43 +33,22 @@ namespace shared_model {
/**
* Class that validates proposal
*/
class ProposalValidator {
private:
void validateTransaction(
ReasonsGroupType &reason,
const interface::Transaction &transaction) const {
// TODO 22/01/2018 x3medima17: add stateless validator IR-837
}

void validateHeight(ReasonsGroupType &reason,
const interface::types::HeightType &height) const {
// TODO 22/01/2018 x3medima17: add stateless validator IR-837
}

template <typename FieldValidator, typename TransactionValidator>
class ProposalValidator : public ContainerValidator<interface::Proposal,
FieldValidator,
TransactionValidator> {
public:
/**
* Applies validation on proposal
* @param proposal
* @return Answer containing found error if any
*/
Answer validate(const interface::Proposal &prop) const {
Answer answer;
// TODO 22/01/2018 x3medima17: add stateless validator IR-837
ReasonsGroupType reason;
reason.first = "Proposal";

validateHeight(reason, prop.height());
for (const auto &tx : prop.transactions()) {
validateTransaction(reason, *tx);
}
if (not reason.second.empty()) {
answer.addReason(std::move(reason));
}

return answer;
return ContainerValidator<interface::Proposal,
FieldValidator,
TransactionValidator>::validate(prop,
"Proposal");
}

Answer answer_;
};

} // namespace validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void TxPipelineIntegrationTestFixture::sendTxsInOrderAndValidate(
ASSERT_EQ(num_blocks, proposals.size());
// Update proposal timestamp and compare it
for (auto i = 0u; i < proposals.size(); ++i) {
expected_proposals[i].created_time = proposals[i]->created_time();
expected_proposals[i].created_time = proposals[i]->createdTime();
auto expected = shared_model::proto::from_old(expected_proposals[i]);
ASSERT_EQ(expected, *proposals[i]);
}
Expand Down
2 changes: 2 additions & 0 deletions test/module/irohad/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ addtest(block_query_test block_query_test.cpp)
target_link_libraries(block_query_test
ametsuchi
libs_common
shared_model_stateless_validation
)

addtest(block_query_transfer_test block_query_transfer_test.cpp)
target_link_libraries(block_query_transfer_test
ametsuchi
libs_common
ametsuchi_fixture
shared_model_stateless_validation
)

addtest(kv_storage_test kv_storage_test.cpp)
Expand Down
3 changes: 2 additions & 1 deletion test/module/irohad/network/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ addtest(block_loader_test block_loader_test.cpp)
target_link_libraries(block_loader_test
block_loader
block_loader_service
shared_model_cryptography
shared_model_stateless_validation
shared_model_cryptography
)
3 changes: 2 additions & 1 deletion test/module/irohad/ordering/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ target_link_libraries(ordering_gate_test

addtest(ordering_gate_service_test ordering_gate_service_test.cpp)
target_link_libraries(ordering_gate_service_test
ordering_service
ordering_service
shared_model_stateless_validation
)
Loading

0 comments on commit 5400665

Please sign in to comment.