Skip to content

Commit

Permalink
Feature/update signature type (hyperledger-iroha#1248)
Browse files Browse the repository at this point in the history
* Add ranges

Signed-off-by: kamilsa <[email protected]>
  • Loading branch information
kamilsa authored May 4, 2018
1 parent c9b56f9 commit 8f9b7b9
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 197 deletions.
13 changes: 8 additions & 5 deletions irohad/consensus/yac/impl/supermajority_checker_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@

#include "consensus/yac/impl/supermajority_checker_impl.hpp"
#include "interfaces/common_objects/peer.hpp"
#include "interfaces/common_objects/signature.hpp"

namespace iroha {
namespace consensus {
namespace yac {

bool SupermajorityCheckerImpl::hasSupermajority(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType &signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>>
&peers) const {
return checkSize(signatures.size(), peers.size())
return checkSize(boost::size(signatures), peers.size())
and peersSubset(signatures, peers);
}

Expand All @@ -40,17 +41,19 @@ namespace iroha {
}

bool SupermajorityCheckerImpl::peersSubset(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType &signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>>
&peers) const {
return std::all_of(
signatures.begin(), signatures.end(), [&peers](auto signature) {
signatures.begin(),
signatures.end(),
[&peers](const auto &signature) {
return std::find_if(
peers.begin(),
peers.end(),
[&signature](const std::shared_ptr<
shared_model::interface::Peer> &peer) {
return signature->publicKey() == peer->pubkey();
return signature.publicKey() == peer->pubkey();
})
!= peers.end();
});
Expand Down
10 changes: 6 additions & 4 deletions irohad/consensus/yac/impl/supermajority_checker_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,16 @@ namespace iroha {
* @return true on supermajority is achieved or false otherwise
*/
virtual bool hasSupermajority(
const shared_model::interface::SignatureSetType &signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>> &peers)
const override;
const shared_model::interface::types::SignatureRangeType
&signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>>
&peers) const override;

virtual bool checkSize(uint64_t current, uint64_t all) const override;

virtual bool peersSubset(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType
&signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>>
&peers) const override;

Expand Down
2 changes: 1 addition & 1 deletion irohad/consensus/yac/impl/yac_hash_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace iroha {
result.proposal_hash = hex_hash;
result.block_hash = hex_hash;
const auto &sig = *block.signatures().begin();
result.block_signature = clone(*sig);
result.block_signature = clone(sig);
return result;
}

Expand Down
8 changes: 5 additions & 3 deletions irohad/consensus/yac/supermajority_checker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#define IROHA_CONSENSUS_SUPERMAJORITY_CHECKER_HPP

#include <vector>
#include "interfaces/common_objects/signable_hash.hpp"
#include "interfaces/common_objects/types.hpp"

namespace shared_model {
namespace interface {
Expand All @@ -46,7 +46,8 @@ namespace iroha {
* @return true on supermajority is achieved or false otherwise
*/
virtual bool hasSupermajority(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType
&signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>>
&peers) const = 0;

Expand All @@ -65,7 +66,8 @@ namespace iroha {
* @return true if is subset or false otherwise
*/
virtual bool peersSubset(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType
&signatures,
const std::vector<std::shared_ptr<shared_model::interface::Peer>>
&peers) const = 0;

Expand Down
2 changes: 1 addition & 1 deletion irohad/synchronizer/impl/synchronizer_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ namespace iroha {
return;
}
auto chain = blockLoader_->retrieveBlocks(
shared_model::crypto::PublicKey(signature->publicKey()));
shared_model::crypto::PublicKey(signature.publicKey()));
// Check chain last commit
auto is_chain_end_expected =
chain.as_blocking().last()->hash() == commit_message->hash();
Expand Down
33 changes: 3 additions & 30 deletions irohad/torii/processor/impl/query_processor_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,9 @@
*/

#include "torii/processor/query_processor_impl.hpp"
#include "backend/protobuf/from_old_model.hpp"
#include "backend/protobuf/query_responses/proto_query_response.hpp"

namespace iroha {
namespace torii {

/**
* Checks if public keys are subset of given signaturies
* @param signatures user signatories, an iterable collection
* @param public_keys vector of public keys
* @return true if user has needed signatories, false instead
*/
bool signaturesSubset(
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
// TODO 30/04/2018 x3medima17: remove code duplication in query_processor
// IR-1192 and stateful_validator
std::unordered_set<std::string> txPubkeys;
for (auto sign : signatures) {
txPubkeys.insert(sign->publicKey().toString());
}
return std::all_of(public_keys.begin(),
public_keys.end(),
[&txPubkeys](const auto &public_key) {
return txPubkeys.find(public_key.toString())
!= txPubkeys.end();
});
}

/**
* Builds QueryResponse that contains StatefulError
* @param hash - original query hash
Expand Down Expand Up @@ -76,8 +48,9 @@ namespace iroha {
if (not signatories) {
return false;
}
bool result = signaturesSubset({sig}, *signatories);
return result;
return std::find(
signatories->begin(), signatories->end(), sig.publicKey())
!= signatories->end();
}

void QueryProcessorImpl::queryHandle(
Expand Down
8 changes: 4 additions & 4 deletions irohad/validation/impl/stateful_validator_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace iroha {
[&](const auto &account) {
// Check if tx creator has account and has quorum to
// execute transaction
return tx.signatures().size() >= account->quorum()
return boost::size(tx.signatures()) >= account->quorum()
? queries.getSignatories(tx.creatorAccountId())
: boost::none;
}
Expand Down Expand Up @@ -93,13 +93,13 @@ namespace iroha {
}

bool StatefulValidatorImpl::signaturesSubset(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType &signatures,
const std::vector<shared_model::crypto::PublicKey> &public_keys) {
// TODO 09/10/17 Lebedev: simplify the subset verification IR-510
// #goodfirstissue
std::unordered_set<std::string> txPubkeys;
for (auto sign : signatures) {
txPubkeys.insert(sign->publicKey().toString());
for (auto &sign : signatures) {
txPubkeys.insert(sign.publicKey().toString());
}
return std::all_of(public_keys.begin(),
public_keys.end(),
Expand Down
2 changes: 1 addition & 1 deletion irohad/validation/impl/stateful_validator_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace iroha {
* pubkeys
*/
bool signaturesSubset(
const shared_model::interface::SignatureSetType &signatures,
const shared_model::interface::types::SignatureRangeType &signatures,
const std::vector<shared_model::crypto::PublicKey> &public_keys);

logger::Logger log_;
Expand Down
19 changes: 9 additions & 10 deletions shared_model/backend/protobuf/block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace shared_model {
return *blob_;
}

const interface::SignatureSetType &signatures() const override {
interface::types::SignatureRangeType signatures() const override {
return *signatures_;
}

Expand All @@ -75,11 +75,11 @@ namespace shared_model {
const crypto::PublicKey &public_key) override {
// if already has such signature
if (std::find_if(signatures_->begin(),
signatures_->end(),
[&signed_blob, &public_key](auto signature) {
return signature->signedData() == signed_blob
and signature->publicKey() == public_key;
}) != signatures_->end()) {
signatures_->end(),
[&public_key](const auto &signature) {
return signature.publicKey() == public_key;
})
!= signatures_->end()) {
return false;
}

Expand Down Expand Up @@ -126,11 +126,10 @@ namespace shared_model {
return interface::types::HashType(proto_->payload().prev_block_hash());
}};

const Lazy<interface::SignatureSetType> signatures_{[this] {
interface::SignatureSetType sigs;
const Lazy<SignatureSetType<proto::Signature>> signatures_{[this] {
SignatureSetType<proto::Signature> sigs;
for (const auto &sig : proto_->signatures()) {
auto curr = detail::makePolymorphic<proto::Signature>(sig);
sigs.insert(curr);
sigs.emplace(sig);
}
return sigs;
}};
Expand Down
9 changes: 4 additions & 5 deletions shared_model/backend/protobuf/queries/proto_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#define IROHA_SHARED_MODEL_PROTO_QUERY_HPP

#include <boost/range/numeric.hpp>

#include "backend/protobuf/common_objects/signature.hpp"
#include "backend/protobuf/common_objects/trivial_proto.hpp"
#include "interfaces/queries/query.hpp"
Expand Down Expand Up @@ -119,7 +118,7 @@ namespace shared_model {
}

// ------------------------| Signable override |-------------------------
const interface::SignatureSetType &signatures() const override {
interface::types::SignatureRangeType signatures() const override {
return *signatures_;
}

Expand Down Expand Up @@ -151,10 +150,10 @@ namespace shared_model {
const Lazy<interface::types::BlobType> payload_{
[this] { return makeBlob(proto_->payload()); }};

const Lazy<interface::SignatureSetType> signatures_{[this] {
interface::SignatureSetType set;
const Lazy<SignatureSetType<proto::Signature>> signatures_{[this] {
SignatureSetType<proto::Signature> set;
if (proto_->has_signature()) {
set.emplace(new Signature(proto_->signature()));
set.emplace(proto_->signature());
}
return set;
}};
Expand Down
14 changes: 6 additions & 8 deletions shared_model/backend/protobuf/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "interfaces/transaction.hpp"

#include <boost/range/numeric.hpp>

#include "backend/protobuf/commands/proto_command.hpp"
#include "backend/protobuf/common_objects/signature.hpp"
#include "block.pb.h"
Expand Down Expand Up @@ -58,7 +57,7 @@ namespace shared_model {
return *blobTypePayload_;
}

const interface::SignatureSetType &signatures() const override {
interface::types::SignatureRangeType signatures() const override {
return *signatures_;
}

Expand All @@ -67,9 +66,8 @@ namespace shared_model {
// if already has such signature
if (std::find_if(signatures_->begin(),
signatures_->end(),
[&signed_blob, &public_key](auto signature) {
return signature->signedData() == signed_blob
and signature->publicKey() == public_key;
[&public_key](const auto& signature) {
return signature.publicKey() == public_key;
})
!= signatures_->end()) {
return false;
Expand Down Expand Up @@ -109,11 +107,11 @@ namespace shared_model {
const Lazy<interface::types::BlobType> blobTypePayload_{
[this] { return makeBlob(payload_); }};

const Lazy<interface::SignatureSetType> signatures_{[this] {
const Lazy<SignatureSetType<proto::Signature>> signatures_{[this] {
return boost::accumulate(proto_->signatures(),
interface::SignatureSetType{},
SignatureSetType<proto::Signature>{},
[](auto &&acc, const auto &sig) {
acc.emplace(new Signature(sig));
acc.emplace(sig);
return std::forward<decltype(acc)>(acc);
});
}};
Expand Down
46 changes: 42 additions & 4 deletions shared_model/interfaces/base/signable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
#ifndef IROHA_SIGNABLE_HPP
#define IROHA_SIGNABLE_HPP

#include <boost/functional/hash.hpp>
#include <boost/optional.hpp>
#include <unordered_set>

#include "cryptography/default_hash_provider.hpp"
#include "interfaces/common_objects/signable_hash.hpp"
#include "interfaces/common_objects/signature.hpp"
#include "interfaces/common_objects/types.hpp"
#include "utils/string_builder.hpp"
Expand Down Expand Up @@ -60,7 +61,7 @@ namespace shared_model {
/**
* @return attached signatures
*/
virtual const SignatureSetType &signatures() const = 0;
virtual types::SignatureRangeType signatures() const = 0;

/**
* Attach signature to object
Expand Down Expand Up @@ -92,7 +93,7 @@ namespace shared_model {
*/
bool operator==(const Model &rhs) const override {
return this->hash() == rhs.hash()
and this->signatures() == rhs.signatures()
and boost::equal(this->signatures(), rhs.signatures())
and this->createdTime() == rhs.createdTime();
}

Expand All @@ -110,10 +111,47 @@ namespace shared_model {
.init("Signable")
.append("created_time", std::to_string(createdTime()))
.appendAll(signatures(),
[](auto &signature) { return signature->toString(); })
[](auto &signature) { return signature.toString(); })
.finalize();
}

protected:
/**
* Type of set of signatures
*
* Note: we can't use const SignatureType due to unordered_set
* limitations: it requires to have write access for elements for some
* internal operations.
*/

protected:
class SignatureSetTypeOps {
public:
/**
* @param sig is item to find hash from
* @return calculated hash of public key
*/
template <typename T>
size_t operator()(const T &sig) const {
return std::hash<std::string>{}(sig.publicKey().hex());
}

/**
* Function for set elements uniqueness by public key
* @param lhs
* @param rhs
* @return true, if public keys are the same
*/
template <typename T>
bool operator()(const T &lhs, const T &rhs) const {
return lhs.publicKey() == rhs.publicKey();
}
};

template <typename T>
using SignatureSetType =
std::unordered_set<T, SignatureSetTypeOps, SignatureSetTypeOps>;

private:
mutable boost::optional<types::HashType> hash_;
};
Expand Down
Loading

0 comments on commit 8f9b7b9

Please sign in to comment.