Skip to content

Commit

Permalink
Add default hash provider (hyperledger-iroha#1213)
Browse files Browse the repository at this point in the history
* Add default hash provider

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

* Remove unused import

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

* Remove using from Signable

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

* Improve codestyle

Signed-off-by: Dumitru <[email protected]>
  • Loading branch information
x3medima17 authored Apr 17, 2018
1 parent 694b548 commit 803fe83
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
7 changes: 4 additions & 3 deletions irohad/torii/impl/command_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/

#include <thread>

#include "backend/protobuf/transaction_responses/proto_tx_response.hpp"

#include "ametsuchi/block_query.hpp"
Expand All @@ -24,6 +25,7 @@
#include "builders/protobuf/transport_builder.hpp"
#include "common/byteutils.hpp"
#include "common/types.hpp"
#include "cryptography/default_hash_provider.hpp"
#include "endpoint.pb.h"
#include "torii/command_service.hpp"
#include "validators/default_validator.hpp"
Expand Down Expand Up @@ -100,9 +102,8 @@ namespace torii {
// getting hash from invalid transaction
auto blobPayload =
shared_model::proto::makeBlob(request.payload());
tx_hash =
shared_model::proto::Transaction::HashProviderType::makeHash(
blobPayload);
tx_hash = shared_model::crypto::DefaultHashProvider::makeHash(
blobPayload);
log_->warn("Stateless invalid tx: {}, hash: {}",
error.error,
tx_hash.hex());
Expand Down
7 changes: 4 additions & 3 deletions irohad/torii/impl/query_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "torii/query_service.hpp"
#include "backend/protobuf/query_responses/proto_query_response.hpp"
#include "cryptography/default_hash_provider.hpp"
#include "validators/default_validator.hpp"

namespace torii {
Expand All @@ -41,7 +42,7 @@ namespace torii {
iroha::protocol::QueryResponse &response) {
shared_model::crypto::Hash hash;
auto blobPayload = shared_model::proto::makeBlob(request.payload());
hash = shared_model::proto::Query::HashProviderType::makeHash(blobPayload);
hash = shared_model::crypto::DefaultHashProvider::makeHash(blobPayload);

if (cache_.findItem(hash)) {
// Query was already processed
Expand Down Expand Up @@ -70,8 +71,8 @@ namespace torii {
cache_.addItem(hash, response);
}
},
[&hash, &response](
const iroha::expected::Error<std::string> &error) {
[&hash,
&response](const iroha::expected::Error<std::string> &error) {
response.set_query_hash(
shared_model::crypto::toBinaryString(hash));
response.mutable_error_response()->set_reason(
Expand Down
30 changes: 30 additions & 0 deletions shared_model/cryptography/default_hash_provider.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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_DEFAULT_HASH_PROVIDER_HPP
#define IROHA_DEFAULT_HASH_PROVIDER_HPP

#include "cryptography/hash_providers/sha3_256.hpp"

namespace shared_model {
namespace crypto {
// Default class that provides hashing functionality
using DefaultHashProvider = shared_model::crypto::Sha3_256;
} // namespace crypto
} // namespace shared_model

#endif // IROHA_DEFAULT_HASH_PROVIDER_HPP
9 changes: 4 additions & 5 deletions shared_model/interfaces/base/signable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#include <boost/optional.hpp>

#include "cryptography/hash_providers/sha3_256.hpp"
#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"
Expand Down Expand Up @@ -48,16 +48,15 @@ namespace shared_model {
#ifndef DISABLE_BACKWARD
template <typename Model,
typename OldModel,
typename HashProvider = shared_model::crypto::Sha3_256>
typename HashProvider = crypto::DefaultHashProvider>
class Signable : public Primitive<Model, OldModel> {
#else
template <typename Model,
typename HashProvider = shared_model::crypto::Sha3_256>
class Signable : public ModelPrimitive<Model> {
#endif
public:
using HashProviderType = HashProvider;

public:
/**
* @return attached signatures
*/
Expand Down Expand Up @@ -105,7 +104,7 @@ namespace shared_model {

const types::HashType &hash() const {
if (hash_ == boost::none) {
hash_.emplace(HashProviderType::makeHash(payload()));
hash_.emplace(HashProvider::makeHash(payload()));
}
return *hash_;
}
Expand Down

0 comments on commit 803fe83

Please sign in to comment.