Skip to content

Commit

Permalink
Merge branch 'develop' into feature/query_processor_shared_model
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsa committed Feb 22, 2018
2 parents 4e48e14 + dfc70a1 commit 6798778
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 47 deletions.
10 changes: 5 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ jobs:
command: |
cmake --build $IROHA_BUILD -- -j3
- run: ccache --show-stats
- save_cache:
key: build-linux-debug-{{ arch }}-{{ .Branch }}-{{ epoch }}
paths:
- ~/.ccache
when: always
- run:
name: unit tests, generate xunit-*.xml
command: cmake --build $IROHA_BUILD --target test
Expand All @@ -67,6 +62,11 @@ jobs:
- run:
name: codecov.io
command: bash <(curl -s https://codecov.io/bash) -f $IROHA_BUILD/reports/gcovr.xml || echo "Codecov did not collect coverage reports"
- save_cache:
key: build-linux-debug-{{ arch }}-{{ .Branch }}-{{ epoch }}
paths:
- ~/.ccache
when: always
- persist_to_workspace:
root: /opt/iroha/build
paths:
Expand Down
10 changes: 8 additions & 2 deletions irohad/simulator/impl/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include "simulator/impl/simulator.hpp"
#include "backend/protobuf/from_old_model.hpp"
#include "model/sha3_hash.hpp"

namespace iroha {
Expand Down Expand Up @@ -63,8 +64,13 @@ namespace iroha {
temporaryStorageResult.match(
[&](expected::Value<std::unique_ptr<ametsuchi::TemporaryWsv>>
&temporaryStorage) {
notifier_.get_subscriber().on_next(
validator_->validate(proposal, *(temporaryStorage.value)));
auto shm_proposal = shared_model::proto::Proposal(
shared_model::proto::from_old(proposal));
auto validated_proposal =
validator_->validate(shm_proposal, *temporaryStorage.value);
std::unique_ptr<model::Proposal> old_proposal(
validated_proposal->makeOldModel());
notifier_.get_subscriber().on_next(*old_proposal);
},
[&](expected::Error<std::string> &error) {
log_->error(error.error);
Expand Down
1 change: 1 addition & 0 deletions irohad/validation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ target_link_libraries(stateful_validator
optional
rxcpp
model
model_interfaces
logger
)

Expand Down
82 changes: 55 additions & 27 deletions irohad/validation/impl/stateful_validator_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#include <numeric>
#include <set>

#include "datetime/time.hpp"
#include "backend/protobuf/from_old_model.hpp"
#include "builders/protobuf/proposal.hpp"
#include "model/account.hpp"
#include "validation/impl/stateful_validator_impl.hpp"

Expand All @@ -29,11 +30,13 @@ namespace iroha {
log_ = logger::log("SFV");
}

model::Proposal StatefulValidatorImpl::validate(
const model::Proposal &proposal,
std::shared_ptr<shared_model::interface::Proposal>
StatefulValidatorImpl::validate(
const shared_model::interface::Proposal &proposal,
ametsuchi::TemporaryWsv &temporaryWsv) {
log_->info("transactions in proposal: {}", proposal.transactions.size());
auto checking_transaction = [this](auto &tx, auto &queries) {
log_->info("transactions in proposal: {}",
proposal.transactions().size());
auto checking_transaction = [this](const auto &tx, auto &queries) {
return (queries.getAccount(tx.creator_account_id) |
[&](const auto &account) {
// Check if tx creator has account and has quorum to execute
Expand All @@ -44,9 +47,19 @@ namespace iroha {
}
|
[&](const auto &signatories) {
auto model_signatories =
signatories
| boost::adaptors::transformed([](const auto &signatory) {
return shared_model::crypto::PublicKey(
signatory.to_string());
});
// Check if signatures in transaction are account signatory
return this->signaturesSubset(tx.signatures, signatories)
? nonstd::make_optional(signatories)
return this->signaturesSubset(
shared_model::proto::from_old(tx).signatures(),
std::vector<shared_model::crypto::PublicKey>(
model_signatories.begin(),
model_signatories.end()))
? nonstd::make_optional(model_signatories)
: nonstd::nullopt;
})
.has_value();
Expand All @@ -55,42 +68,57 @@ namespace iroha {
// Filter only valid transactions
auto filter = [&temporaryWsv, checking_transaction](auto &acc,
const auto &tx) {
auto answer = temporaryWsv.apply(tx, checking_transaction);
std::unique_ptr<model::Transaction> old_tx(tx->makeOldModel());
auto answer = temporaryWsv.apply(*old_tx, checking_transaction);
if (answer) {
acc.push_back(tx);
}
return acc;
};

auto &txs = proposal.transactions;
auto &txs = proposal.transactions();
decltype(txs) valid = {};

model::Proposal validated_proposal(
std::accumulate(txs.begin(), txs.end(), valid, filter));
validated_proposal.height = proposal.height;
validated_proposal.created_time = proposal.created_time;
auto valid_txs = std::accumulate(txs.begin(), txs.end(), valid, filter);

// TODO: kamilsa IR-1010 20.02.2018 rework validation logic, so that this
// cast is not needed and stateful validator does not know about the
// transport
auto valid_proto_txs =
valid_txs
| boost::adaptors::transformed([](const auto &polymorphic_tx) {
return static_cast<const shared_model::proto::Transaction &>(
*polymorphic_tx.operator->());
});
auto validated_proposal = shared_model::proto::ProposalBuilder()
.createdTime(proposal.created_time())
.height(proposal.height())
.transactions(valid_proto_txs)
.createdTime(proposal.created_time())
.build();

log_->info("transactions in verified proposal: {}",
validated_proposal.transactions.size());
return validated_proposal;
validated_proposal.transactions().size());
return std::make_shared<decltype(validated_proposal)>(
validated_proposal.getTransport());
}

bool StatefulValidatorImpl::signaturesSubset(
const model::Transaction::SignaturesType &signatures,
const std::vector<pubkey_t> &public_keys) {
const shared_model::interface::SignatureSetType
&signatures,
const std::vector<shared_model::crypto::PublicKey> &public_keys) {
// TODO 09/10/17 Lebedev: simplify the subset verification IR-510
// #goodfirstissue
std::set<pubkey_t> txPubkeys;
std::unordered_set<std::string> txPubkeys;
for (auto sign : signatures) {
txPubkeys.insert(sign.pubkey);
}
std::set<pubkey_t> accPubkeys;
for (auto pubkey : public_keys) {
accPubkeys.insert(pubkey);
txPubkeys.insert(sign->publicKey().toString());
}
return std::includes(accPubkeys.begin(),
accPubkeys.end(),
txPubkeys.begin(),
txPubkeys.end());
return std::all_of(public_keys.begin(),
public_keys.end(),
[&txPubkeys](const auto &public_key) {
return txPubkeys.find(public_key.toString())
!= txPubkeys.end();
});
}

} // namespace validation
Expand Down
10 changes: 6 additions & 4 deletions irohad/validation/impl/stateful_validator_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ namespace iroha {
* all changes after removing wsv will be ignored
* @return proposal with valid transactions
*/
model::Proposal validate(const model::Proposal &proposal,
ametsuchi::TemporaryWsv &temporaryWsv) override;
std::shared_ptr<shared_model::interface::Proposal> validate(
const shared_model::interface::Proposal &proposal,
ametsuchi::TemporaryWsv &temporaryWsv) override;

private:
/**
Expand All @@ -52,8 +53,9 @@ namespace iroha {
* pubkeys
*/
bool signaturesSubset(
const model::Transaction::SignaturesType &signatures,
const std::vector<pubkey_t> &public_keys);
const shared_model::interface::SignatureSetType
&signatures,
const std::vector<shared_model::crypto::PublicKey> &public_keys);

logger::Logger log_;
};
Expand Down
6 changes: 3 additions & 3 deletions irohad/validation/stateful_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
#define IROHA_VALIDATION_STATEFUL_VALIDATOR_HPP

#include "ametsuchi/temporary_wsv.hpp"
#include "model/proposal.hpp"
#include "interfaces/iroha_internal/proposal.hpp"

namespace iroha {
namespace validation {
Expand All @@ -38,8 +38,8 @@ namespace iroha {
* all changes after removing wsv will be ignored
* @return proposal with valid transactions
*/
virtual model::Proposal validate(
const model::Proposal &proposal,
virtual std::shared_ptr<shared_model::interface::Proposal> validate(
const shared_model::interface::Proposal &proposal,
ametsuchi::TemporaryWsv &temporaryWsv) = 0;
};
} // namespace validation
Expand Down
4 changes: 2 additions & 2 deletions test/module/irohad/ametsuchi/ametsuchi_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ namespace iroha {
MOCK_METHOD2(
apply,
bool(const model::Block &,
std::function<bool(
const model::Block &, WsvQuery &, const hash256_t &)>));
std::function<
bool(const model::Block &, WsvQuery &, const hash256_t &)>));
};

/**
Expand Down
8 changes: 6 additions & 2 deletions test/module/irohad/simulator/simulator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

#include "backend/protobuf/from_old_model.hpp"
#include "module/irohad/ametsuchi/ametsuchi_mocks.hpp"
#include "module/irohad/model/model_mocks.hpp"
#include "module/irohad/network/network_mocks.hpp"
Expand Down Expand Up @@ -78,11 +79,14 @@ TEST_F(SimulatorTest, ValidWhenPreviousBlock) {
block.height = proposal.height - 1;

EXPECT_CALL(*factory, createTemporaryWsv()).Times(1);

EXPECT_CALL(*query, getTopBlocks(1))
.WillOnce(Return(rxcpp::observable<>::just(block)));

EXPECT_CALL(*validator, validate(_, _)).WillOnce(Return(proposal));
std::shared_ptr<shared_model::interface::Proposal> iprop =
std::make_shared<shared_model::proto::Proposal>(
shared_model::proto::from_old(proposal));

EXPECT_CALL(*validator, validate(_, _)).WillOnce(Return(iprop));

EXPECT_CALL(*ordering_gate, on_proposal())
.WillOnce(Return(rxcpp::observable<>::empty<Proposal>()));
Expand Down
6 changes: 4 additions & 2 deletions test/module/irohad/validation/validation_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define IROHA_VALIDATION_MOCKS_HPP

#include <gmock/gmock.h>
#include "interfaces/iroha_internal/proposal.hpp"
#include "validation/chain_validator.hpp"
#include "validation/stateful_validator.hpp"
#include "validation/stateless_validator.hpp"
Expand All @@ -34,8 +35,9 @@ namespace iroha {
class MockStatefulValidator : public validation::StatefulValidator {
public:
MOCK_METHOD2(validate,
model::Proposal(const model::Proposal &,
ametsuchi::TemporaryWsv &));
std::shared_ptr<shared_model::interface::Proposal>(
const shared_model::interface::Proposal &,
ametsuchi::TemporaryWsv &));
};

class MockChainValidator : public ChainValidator {
Expand Down

0 comments on commit 6798778

Please sign in to comment.