Skip to content

Commit

Permalink
Shared model/new yac hash (hyperledger-iroha#1116)
Browse files Browse the repository at this point in the history
* remove polymorphic wrapper from block loader and replace with shared_ptr
add stateless validation in transport

Signed-off-by: Alexey Chernyshov <[email protected]>

* replace BlockValidator with DefaultBlockValidator

Signed-off-by: Alexey Chernyshov <[email protected]>

* new yac

Signed-off-by: Dumitru <[email protected]>

* rework chain_validator with shared_model

Signed-off-by: Alexey Chernyshov <[email protected]>

* Yac to new model

Signed-off-by: Dumitru <[email protected]>

* Yac tests

Signed-off-by: Dumitru <[email protected]>

* Yac tests

Signed-off-by: Dumitru <[email protected]>

* Apply clang format

Signed-off-by: Dumitru <[email protected]>

* Remove unused imports

Signed-off-by: Dumitru <[email protected]>

* Remove old block from yac gate test

Signed-off-by: Dumitru <[email protected]>

* Remove unused old model, fix clang format, add const ref

Signed-off-by: Dumitru <[email protected]>

* new yac

Signed-off-by: Dumitru <[email protected]>

* Yac tests

Signed-off-by: Dumitru <[email protected]>

* Apply clang format

Signed-off-by: Dumitru <[email protected]>

* YacHash

Signed-off-by: Dumitru <[email protected]>

* Apply clang-format

Signed-off-by: Dumitru <[email protected]>

* Copy to clone

Signed-off-by: Dumitru <[email protected]>

* Use interface signature builder

Signed-off-by: Dumitru <[email protected]>

* Add valid keypair

Signed-off-by: Dumitru <[email protected]>

* Apply clang format

Signed-off-by: Dumitru <[email protected]>
  • Loading branch information
x3medima17 committed Mar 30, 2018
1 parent 292dccb commit b52a3e3
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 69 deletions.
6 changes: 6 additions & 0 deletions irohad/consensus/yac/cluster_order.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ namespace shared_model {
}
} // namespace shared_model

namespace shared_model {
namespace interface {
class Peer;
}
} // namespace shared_model

namespace iroha {
namespace consensus {
namespace yac {
Expand Down
4 changes: 2 additions & 2 deletions irohad/consensus/yac/impl/cluster_order.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ namespace iroha {
std::vector<std::shared_ptr<shared_model::interface::Peer>> order)
: order_(std::move(order)) {}

// TODO : 24/03/2018 x3medima17: make it const, IR-1164
const shared_model::interface::Peer &ClusterOrdering::currentLeader() {
// TODO : 24/03/2018 x3medima17: make it const, IR-1164
const shared_model::interface::Peer& ClusterOrdering::currentLeader() {
if (index_ >= order_.size()) {
index_ = 0;
}
Expand Down
21 changes: 5 additions & 16 deletions irohad/consensus/yac/impl/yac_gate_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ namespace iroha {
}

void YacGateImpl::vote(const shared_model::interface::Block &block) {
std::unique_ptr<model::Block> bl(block.makeOldModel());
auto hash = hash_provider_->makeHash(*bl);
auto hash = hash_provider_->makeHash(block);
log_->info("vote for block ({}, {})",
hash.proposal_hash,
block.hash().toString());
Expand All @@ -63,9 +62,7 @@ namespace iroha {
log_->error("ordering doesn't provide peers => pass round");
return;
}
current_block_ = std::make_pair(
hash,
clone(block));
current_block_ = std::make_pair(hash, clone(block));
hash_gate_->vote(hash, *order);
}

Expand Down Expand Up @@ -109,8 +106,7 @@ namespace iroha {
shared_model::crypto::PublicKey(
{vote.signature.pubkey.begin(),
vote.signature.pubkey.end()}),
shared_model::crypto::Hash(
{model_hash.begin(), model_hash.end()}));
shared_model::crypto::Hash(model_hash));
// if load is successful
if (block) {
subscriber.on_next(block.value());
Expand Down Expand Up @@ -139,15 +135,8 @@ namespace iroha {
current_block_.second->clearSignatures();
for (const auto &vote : commit.votes) {
auto sig = vote.hash.block_signature;
auto tmp =
shared_model::proto::SignatureBuilder()
.signedData(shared_model::interface::Signature::SignedType(
sig.signature.to_string()))
.publicKey(
shared_model::crypto::PublicKey(sig.pubkey.to_string()))
.build();
auto wrap = shared_model::detail::makePolymorphic<
shared_model::proto::Signature>(tmp.getTransport());
auto wrap = shared_model::detail::PolymorphicWrapper<
shared_model::interface::Signature>(sig);
current_block_.second->addSignature(wrap);
}
}
Expand Down
20 changes: 11 additions & 9 deletions irohad/consensus/yac/impl/yac_hash_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@
*/

#include "consensus/yac/impl/yac_hash_provider_impl.hpp"
#include "common/byteutils.hpp"

#include "interfaces/iroha_internal/block.hpp"

namespace iroha {
namespace consensus {
namespace yac {

YacHash YacHashProviderImpl::makeHash(const model::Block &block) const {
YacHash YacHashProviderImpl::makeHash(
const shared_model::interface::Block &block) const {
YacHash result;
// TODO 01/08/17 Muratov: add proposal hash to block,
// block.proposal_hash IR-505
auto hex_hash = block.hash.to_hexstring();
auto hex_hash = block.hash().hex();
result.proposal_hash = hex_hash;
result.block_hash = hex_hash;
result.block_signature = block.sigs.front();
const auto &sig = *block.signatures().begin();
result.block_signature = clone(*sig);
return result;
}

model::Block::HashType YacHashProviderImpl::toModelHash(
shared_model::interface::types::HashType YacHashProviderImpl::toModelHash(
const YacHash &hash) const {
return hexstringToArray<model::Block::HashType::size()>(hash.block_hash)
.value();
auto blob = shared_model::crypto::Blob::fromHexString(hash.block_hash);
auto string_blob = shared_model::crypto::toBinaryString(blob);
return shared_model::interface::types::HashType(string_blob);
}
} // namespace yac
} // namespace consensus
Expand Down
6 changes: 4 additions & 2 deletions irohad/consensus/yac/impl/yac_hash_provider_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ namespace iroha {
namespace yac {
class YacHashProviderImpl : public YacHashProvider {
public:
YacHash makeHash(const model::Block &block) const override;
YacHash makeHash(
const shared_model::interface::Block &block) const override;

model::Block::HashType toModelHash(const YacHash &hash) const override;
shared_model::interface::types::HashType toModelHash(
const YacHash &hash) const override;
};
} // namespace yac
} // namespace consensus
Expand Down
51 changes: 41 additions & 10 deletions irohad/consensus/yac/transport/yac_pb_converters.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
#ifndef IROHA_YAC_PB_CONVERTERS_HPP
#define IROHA_YAC_PB_CONVERTERS_HPP

#include "builders/default_builders.hpp"
#include "common/byteutils.hpp"
#include "consensus/yac/messages.hpp"
#include "cryptography/crypto_provider/crypto_defaults.hpp"
#include "interfaces/common_objects/signature.hpp"
#include "yac.pb.h"

namespace iroha {
Expand All @@ -35,10 +38,31 @@ namespace iroha {
hash->set_proposal(vote.hash.proposal_hash);

auto block_signature = hash->mutable_block_signature();
block_signature->set_signature(
vote.hash.block_signature.signature.to_string());
block_signature->set_pubkey(
vote.hash.block_signature.pubkey.to_string());

// Will fix it in the next PR, very soon, don't worry
if (vote.hash.block_signature == nullptr) {
auto peer_key = shared_model::crypto::DefaultCryptoAlgorithmType::
generateKeypair()
.publicKey();
shared_model::builder::DefaultSignatureBuilder()
.publicKey(peer_key)
.signedData(shared_model::crypto::Signed(""))
.build()
.match(
[&](iroha::expected::Value<std::shared_ptr<
shared_model::interface::Signature>> &sig) {
const_cast<VoteMessage &>(vote).hash.block_signature =
sig.value;
},
[](iroha::expected::Error<std::shared_ptr<std::string>>) {
});
}

block_signature->set_signature(shared_model::crypto::toBinaryString(
vote.hash.block_signature->signedData()));

block_signature->set_pubkey(shared_model::crypto::toBinaryString(
vote.hash.block_signature->publicKey()));

auto signature = pb_vote.mutable_signature();
signature->set_signature(vote.signature.signature.to_string());
Expand All @@ -52,12 +76,19 @@ namespace iroha {
VoteMessage vote;
vote.hash.proposal_hash = pb_vote.hash().proposal();
vote.hash.block_hash = pb_vote.hash().block();
vote.hash.block_signature.signature =
*stringToBlob<iroha::sig_t::size()>(
pb_vote.hash().block_signature().signature());
vote.hash.block_signature.pubkey =
*stringToBlob<iroha::pubkey_t::size()>(
pb_vote.hash().block_signature().pubkey());

shared_model::builder::DefaultSignatureBuilder()
.publicKey(shared_model::crypto::PublicKey(
pb_vote.hash().block_signature().pubkey()))
.signedData(shared_model::crypto::Signed(
pb_vote.hash().block_signature().signature()))
.build()
.match(
[&](iroha::expected::Value<
std::shared_ptr<shared_model::interface::Signature>>
&sig) { vote.hash.block_signature = sig.value; },
[](iroha::expected::Error<std::shared_ptr<std::string>>) {});

vote.signature.signature = *stringToBlob<iroha::sig_t::size()>(
pb_vote.signature().signature());
vote.signature.pubkey = *stringToBlob<iroha::pubkey_t::size()>(
Expand Down
19 changes: 14 additions & 5 deletions irohad/consensus/yac/yac_hash_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@
#ifndef IROHA_YAC_HASH_PROVIDER_HPP
#define IROHA_YAC_HASH_PROVIDER_HPP

#include <memory>
#include <string>
#include "model/block.hpp" // for Block::HashType
#include "model/signature.hpp" // for model::Signature

#include "interfaces/common_objects/types.hpp" // for model::Signature

namespace shared_model {
namespace interface {
class Block;
class Signature;
} // namespace interface
} // namespace shared_model

namespace iroha {
namespace consensus {
Expand All @@ -47,7 +55,7 @@ namespace iroha {
/**
* Peer signature of block
*/
model::Signature block_signature;
std::shared_ptr<shared_model::interface::Signature> block_signature;

bool operator==(const YacHash &obj) const {
return proposal_hash == obj.proposal_hash
Expand All @@ -69,14 +77,15 @@ namespace iroha {
* @param block - for hashing
* @return hashed value of block
*/
virtual YacHash makeHash(const model::Block &block) const = 0;
virtual YacHash makeHash(
const shared_model::interface::Block &block) const = 0;

/**
* Convert YacHash to model hash
* @param hash - for converting
* @return HashType of model hash
*/
virtual model::Block::HashType toModelHash(
virtual shared_model::interface::types::HashType toModelHash(
const YacHash &hash) const = 0;

virtual ~YacHashProvider() = default;
Expand Down
1 change: 0 additions & 1 deletion libs/common/byteutils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <string>

#include "common/types.hpp"

namespace iroha {

/**
Expand Down
5 changes: 5 additions & 0 deletions shared_model/builders/default_builders.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ namespace shared_model {
using DefaultTransactionStatusBuilder =
shared_model::builder::TransactionStatusBuilder<
shared_model::proto::TransactionStatusBuilder>;

using DefaultSignatureBuilder = shared_model::builder::SignatureBuilder<
shared_model::proto::SignatureBuilder,
shared_model::validation::FieldValidator>;

} // namespace builder
} // namespace shared_model

Expand Down
1 change: 0 additions & 1 deletion shared_model/cryptography/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ namespace shared_model {
std::string hex_;
};


} // namespace crypto
} // namespace shared_model
#endif // IROHA_SHARED_MODEL_BLOB_HPP
6 changes: 6 additions & 0 deletions test/module/irohad/consensus/yac/network_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ namespace iroha {
message.hash.proposal_hash = "proposal";
message.hash.block_hash = "block";

auto sig = shared_model::proto::SignatureBuilder()
.publicKey(shared_model::crypto::PublicKey("key"))
.signedData(shared_model::crypto::Signed("data"))
.build();

message.hash.block_signature = clone(sig);
network->subscribe(notifications);

grpc::ServerBuilder builder;
Expand Down
19 changes: 15 additions & 4 deletions test/module/irohad/consensus/yac/yac_crypto_provider_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
*/

#include <gtest/gtest.h>
#include "builders/protobuf/common_objects/proto_signature_builder.hpp"
#include "consensus/yac/impl/yac_crypto_provider_impl.hpp"
#include "consensus/yac/impl/yac_hash_provider_impl.hpp"
#include "consensus/yac/messages.hpp"
#include "cryptography/ed25519_sha3_impl/internal/ed25519_impl.hpp"

const auto pubkey = std::string('0', 32);
const auto signed_data = std::string('1', 32);
namespace iroha {
namespace consensus {
namespace yac {
Expand All @@ -38,8 +41,12 @@ namespace iroha {

TEST_F(YacCryptoProviderTest, ValidWhenSameMessage) {
YacHash hash("1", "1");
hash.block_signature.pubkey.fill('0');
hash.block_signature.signature.fill('1');
auto sig = shared_model::proto::SignatureBuilder()
.publicKey(shared_model::crypto::PublicKey(pubkey))
.signedData(shared_model::crypto::Signed(signed_data))
.build();

hash.block_signature = clone(sig);

auto vote = crypto_provider->getVote(hash);

Expand All @@ -48,8 +55,12 @@ namespace iroha {

TEST_F(YacCryptoProviderTest, InvalidWhenMessageChanged) {
YacHash hash("1", "1");
hash.block_signature.pubkey.fill('0');
hash.block_signature.signature.fill('1');
auto sig = shared_model::proto::SignatureBuilder()
.publicKey(shared_model::crypto::PublicKey(pubkey))
.signedData(shared_model::crypto::Signed(signed_data))
.build();

hash.block_signature = clone(sig);

auto vote = crypto_provider->getVote(hash);

Expand Down
13 changes: 6 additions & 7 deletions test/module/irohad/consensus/yac/yac_gate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "module/irohad/consensus/yac/yac_mocks.hpp"
#include "module/irohad/network/network_mocks.hpp"
#include "module/irohad/simulator/simulator_mocks.hpp"
#include "cryptography/hash.hpp"

using namespace iroha::consensus::yac;
using namespace iroha::network;
Expand Down Expand Up @@ -66,7 +67,7 @@ class YacGateTest : public ::testing::Test {
const auto old_signature =
*std::unique_ptr<iroha::model::Signature>(signature.makeOldModel());

expected_hash.block_signature = old_signature;
expected_hash.block_signature = clone(signature);
message.hash = expected_hash;
message.signature = old_signature;
commit_message = CommitMessage({message});
Expand Down Expand Up @@ -132,8 +133,9 @@ TEST_F(YacGateTest, YacGateSubscriptionTest) {

// verify that yac gate emit expected block
auto gate_wrapper = make_test_subscriber<CallExact>(gate->on_commit(), 1);
gate_wrapper.subscribe(
[this](auto block) { ASSERT_EQ(*block, *expected_block); });
gate_wrapper.subscribe([this](auto block) {
ASSERT_EQ(*block, *expected_block);
});

ASSERT_TRUE(gate_wrapper.validate());
}
Expand Down Expand Up @@ -188,11 +190,8 @@ TEST_F(YacGateTest, LoadBlockWhenDifferentCommit) {
EXPECT_CALL(*hash_gate, on_commit()).WillOnce(Return(expected_commit));

// convert yac hash to model hash
auto old_hash =
(*std::unique_ptr<iroha::model::Block>(expected_block->makeOldModel()))
.hash;
EXPECT_CALL(*hash_provider, toModelHash(expected_hash))
.WillOnce(Return(old_hash));
.WillOnce(Return(expected_block->hash()));

// load block
auto sig = expected_block->signatures().begin();
Expand Down
Loading

0 comments on commit b52a3e3

Please sign in to comment.