Skip to content

Commit

Permalink
Add keypair to crypto provider:
Browse files Browse the repository at this point in the history
- Reorder initialization, since crypto provider requires node pubkey
- Add sign(Block) method to crypto provider
- Add crypto provider to simulator
- Fix yac gate and hash provider test with new signature logic
- Fix simulator test
  • Loading branch information
lebdron committed Oct 2, 2017
1 parent ce1cb8e commit 2082173
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 90 deletions.
2 changes: 2 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ BasedOnStyle: Google
NamespaceIndentation: All
BreakBeforeBinaryOperators: NonAssignment
AlignOperands: false
BinPackArguments: false
BinPackParameters: false
86 changes: 48 additions & 38 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
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.
*/
/**
* Copyright Soramitsu Co., Ltd. 2017 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.
*/

#include "main/application.hpp"

Expand All @@ -31,9 +32,20 @@ using namespace iroha::consensus::yac;

class MockCryptoProvider : public ModelCryptoProvider {
public:
MockCryptoProvider(const pubkey_t &pubkey) : pubkey_(pubkey) {}

MOCK_CONST_METHOD1(verify, bool(const Transaction &));
MOCK_CONST_METHOD1(verify, bool(std::shared_ptr<const Query>));
MOCK_CONST_METHOD1(verify, bool(const Block &));

Block sign(const Block &block) const override {
auto signed_block = block;
signed_block.sigs.push_back(Signature{{}, pubkey_});
return signed_block;
}

private:
pubkey_t pubkey_;
};

Irohad::Irohad(const std::string &block_store_dir,
Expand Down Expand Up @@ -69,11 +81,11 @@ Irohad::~Irohad() {
void Irohad::init() {
initLoop();
initProtoFactories();
initPeerQuery();
initPeer();
initCryptoProvider();
initValidators();
initPeerQuery();
initPeerOrderer();
initPeerAddress();
initOrderingGate();
initSimulator();
initBlockLoader();
Expand Down Expand Up @@ -110,8 +122,20 @@ void Irohad::initProtoFactories() {
log_->info("[Init] => converters");
}

void Irohad::initPeerQuery() {
wsv = std::make_shared<ametsuchi::PeerQueryWsv>(storage->getWsvQuery());

log_->info("[Init] => peer query");
}

void Irohad::initPeer() {
peer = wsv->getLedgerPeers().value().at(peer_number_);

log_->info("[Init] => peer address is {}", peer.address);
}

void Irohad::initCryptoProvider() {
auto mock_crypto_verifier = std::make_shared<MockCryptoProvider>();
auto mock_crypto_verifier = std::make_shared<MockCryptoProvider>(peer.pubkey);

EXPECT_CALL(*mock_crypto_verifier,
verify(::testing::A<const Transaction &>()))
Expand All @@ -136,23 +160,11 @@ void Irohad::initValidators() {
log_->info("[Init] => validators");
}

void Irohad::initPeerQuery() {
wsv = std::make_shared<ametsuchi::PeerQueryWsv>(storage->getWsvQuery());

log_->info("[Init] => peer query");
}

void Irohad::initPeerOrderer() {
orderer = std::make_shared<PeerOrdererImpl>(wsv);
log_->info("[Init] => peer orderer");
}

void Irohad::initPeerAddress() {
peer_address = wsv->getLedgerPeers().value().at(peer_number_).address;

log_->info("[Init] => peer address is {}", peer_address);
}

void Irohad::initOrderingGate() {

// const set maximum transactions that possible appears in one proposal
Expand All @@ -174,7 +186,8 @@ void Irohad::initSimulator() {
simulator = std::make_shared<Simulator>(ordering_gate,
stateful_validator,
storage,
storage->getBlockQuery());
storage->getBlockQuery(),
crypto_verifier);

log_->info("[Init] => init simulator");
}
Expand All @@ -188,11 +201,8 @@ void Irohad::initBlockLoader() {
}

void Irohad::initConsensusGate() {
consensus_gate = yac_init.initConsensusGate(peer_address,
loop,
orderer,
simulator,
block_loader);
consensus_gate = yac_init.initConsensusGate(
peer.address, loop, orderer, simulator, block_loader);

log_->info("[Init] => consensus gate");
}
Expand Down Expand Up @@ -250,7 +260,8 @@ void Irohad::run() {

grpc::ServerBuilder builder;
int port = 0;
builder.AddListeningPort(peer_address, grpc::InsecureServerCredentials(), &port);
builder.AddListeningPort(
peer.address, grpc::InsecureServerCredentials(), &port);
builder.RegisterService(ordering_init.ordering_gate_transport.get());
builder.RegisterService(ordering_init.ordering_service.get());
builder.RegisterService(yac_init.consensus_network.get());
Expand All @@ -265,4 +276,3 @@ void Irohad::run() {
torii_server->waitForServersReady();
loop->run();
}

13 changes: 7 additions & 6 deletions irohad/main/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_APPLICATION_HPP
#define IROHA_APPLICATION_HPP

Expand Down Expand Up @@ -84,16 +85,16 @@ class Irohad {

virtual void initProtoFactories();

virtual void initPeerQuery();

virtual void initPeer();

virtual void initCryptoProvider();

virtual void initValidators();

virtual void initPeerQuery();

virtual void initPeerOrderer();

virtual void initPeerAddress();

virtual void initOrderingGate();

virtual void initSimulator();
Expand Down Expand Up @@ -141,8 +142,8 @@ class Irohad {
// peer orderer
std::shared_ptr<iroha::consensus::yac::YacPeerOrderer> orderer;

// peer address
iroha::model::Peer::AddressType peer_address;
// peer
iroha::model::Peer peer;

// ordering gate
std::shared_ptr<iroha::network::OrderingGate> ordering_gate;
Expand Down
1 change: 0 additions & 1 deletion irohad/model/commands/add_peer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace iroha {
std::string address;

bool operator==(const Command &command) const override;
bool operator!=(const Command &command) const override;

AddPeer() {}

Expand Down
15 changes: 10 additions & 5 deletions irohad/model/impl/model_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ namespace iroha {
return not operator==(rhs);
}

bool Proposal::operator!=(const Proposal &rhs) const {
return not operator==(rhs);
}

bool Transaction::operator!=(const Transaction &rhs) const {
return not operator==(rhs);
}

bool Signature::operator!=(const Signature &rhs) const {
return !operator==(rhs);
return not operator==(rhs);
};

bool AddPeer::operator!=(const Command &command) const {
return !operator==(command);
}

bool AppendRole::operator==(const Command &command) const {
if (! instanceof <AppendRole>(command)) return false;
auto cmd = static_cast<const AppendRole &>(command);
Expand Down Expand Up @@ -191,6 +191,11 @@ namespace iroha {
&& rhs.created_ts == created_ts;
}

/* Proposal */
bool Proposal::operator==(const Proposal &rhs) const {
return rhs.height == height and rhs.transactions == transactions;
}

/* Block */
bool Block::operator==(const Block &rhs) const {
return rhs.hash == hash && rhs.height == height
Expand Down
52 changes: 30 additions & 22 deletions irohad/model/model_crypto_provider.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
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.
*/
/**
* Copyright Soramitsu Co., Ltd. 2017 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_MODEL_CRYPTO_PROVIDER_HPP
#define IROHA_MODEL_CRYPTO_PROVIDER_HPP
Expand All @@ -38,18 +39,25 @@ namespace iroha {
virtual bool verify(const Transaction &tx) const = 0;

/**
* Method for signature verification of a transaction.
* @param tx - transaction for verification
* @return true if transaction signature is valid, otherwise false
* Method for signature verification of a query.
* @param query - query for verification
* @return true if query signature is valid, otherwise false
*/
virtual bool verify(std::shared_ptr<const Query> tx) const = 0;
virtual bool verify(std::shared_ptr<const Query> query) const = 0;

/**
*
* @param block
* @return
* Method for signature verification of a block.
* @param block - block for verification
* @return true if block signature is valid, otherwise false
*/
virtual bool verify(const Block &block) const = 0;

/**
* Method for signing a block with stored keypair
* @param block - block for signing
* @return signed block
*/
virtual Block sign(const Block &block) const = 0;
};
}
}
Expand Down
12 changes: 12 additions & 0 deletions irohad/model/model_crypto_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace iroha {
namespace model {
ModelCryptoProviderImpl::ModelCryptoProviderImpl(const keypair_t &keypair)
: keypair_(keypair) {}

bool ModelCryptoProviderImpl::verify(const Transaction &tx) const {
if (tx.signatures.empty()) return false;
Expand Down Expand Up @@ -53,5 +55,15 @@ namespace iroha {
}
return true;
}

const static model::converters::PbBlockFactory block_factory;

Block ModelCryptoProviderImpl::sign(const Block &block) const {
auto signed_block = block;
auto blob = block_factory.serialize(signed_block).SerializeAsString();
auto signature = iroha::sign(blob, keypair_.pubkey, keypair_.privkey);
signed_block.sigs.push_back(Signature{std::move(signature), keypair_.pubkey});
return signed_block;
}
}
}
12 changes: 9 additions & 3 deletions irohad/model/model_crypto_provider_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,25 @@
#ifndef IROHA_MODEL_CRYPTO_PROVIDER_IMPL_HPP
#define IROHA_MODEL_CRYPTO_PROVIDER_IMPL_HPP

#include <model/model_crypto_provider.hpp>
#include "model/model_crypto_provider.hpp"

namespace iroha {
namespace model {

class ModelCryptoProviderImpl : public ModelCryptoProvider {
public:
explicit ModelCryptoProviderImpl(const keypair_t &keypair);

bool verify(const Transaction &tx) const override;

bool verify(std::shared_ptr<const Query> tx) const override;
bool verify(std::shared_ptr<const Query> query) const override;

bool verify(const Block &block) const override;

bool verify(const Block& block) const override;
Block sign(const Block &block) const override;

private:
keypair_t keypair_;
};
}
}
Expand Down
8 changes: 5 additions & 3 deletions irohad/simulator/impl/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ namespace iroha {
std::shared_ptr<network::OrderingGate> ordering_gate,
std::shared_ptr<validation::StatefulValidator> statefulValidator,
std::shared_ptr<ametsuchi::TemporaryFactory> factory,
std::shared_ptr<ametsuchi::BlockQuery> blockQuery)
std::shared_ptr<ametsuchi::BlockQuery> blockQuery,
std::shared_ptr<model::ModelCryptoProvider> crypto_provider)
: validator_(std::move(statefulValidator)),
ametsuchi_factory_(std::move(factory)),
block_queries_(std::move(blockQuery)) {
block_queries_(std::move(blockQuery)),
crypto_provider_(std::move(crypto_provider)) {
log_ = logger::log("Simulator");
ordering_gate->on_proposal().subscribe(
[this](auto proposal) { this->process_proposal(proposal); });
Expand Down Expand Up @@ -69,7 +71,7 @@ namespace iroha {
new_block.created_ts = 0; // todo set timestamp from proposal
new_block.merkle_root.fill(0); // todo make effective impl
new_block.hash = hash(new_block);
new_block.sigs.push_back({});
new_block = crypto_provider_->sign(new_block);

block_notifier_.get_subscriber().on_next(new_block);
}
Expand Down
Loading

0 comments on commit 2082173

Please sign in to comment.