Skip to content

Commit

Permalink
Update files:
Browse files Browse the repository at this point in the history
- Add protobuf convertor for queries
- Change namings from acc to acct
- Add implementation for query hash
- Add implementation for query crypto verification
- Add to query model created time and hash
- Add comments
- Update pb schema for query
  • Loading branch information
grimadas committed Jul 21, 2017
1 parent f824db6 commit 19445b8
Show file tree
Hide file tree
Showing 18 changed files with 330 additions and 67 deletions.
1 change: 1 addition & 0 deletions irohad/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ add_library(model
converters/impl/pb_transaction_factory.cpp
converters/impl/pb_command_factory.cpp
impl/query_execution.cpp
converters/impl/pb_query_factory.cpp
)

target_link_libraries(model
Expand Down
22 changes: 12 additions & 10 deletions irohad/model/converters/impl/pb_block_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* limitations under the License.
*/

#include <model/model_hash_provider_impl.hpp>
#include "model/converters/pb_block_factory.hpp"
#include <model/model_hash_provider_impl.hpp>
#include "model/converters/pb_transaction_factory.hpp"

namespace iroha {
Expand All @@ -29,7 +29,7 @@ namespace iroha {
// -----|Header|-----
auto header = pb_block.mutable_header();
header->set_created_time(block.created_ts);
for (auto sig: block.sigs){
for (auto sig : block.sigs) {
auto pb_sig = header->add_signatures();
pb_sig->set_pubkey(sig.pubkey.data(), sig.pubkey.size());
pb_sig->set_signature(sig.signature.data(), sig.signature.size());
Expand All @@ -47,7 +47,7 @@ namespace iroha {
// -----|Body|-----
auto body = pb_block.mutable_body();
PbTransactionFactory tx_factory;
for (auto tx: block.transactions){
for (auto tx : block.transactions) {
auto pb_tx = body->add_transactions();
pb_tx->CopyFrom(tx_factory.serialize(tx));
}
Expand All @@ -61,17 +61,19 @@ namespace iroha {
// -----|Header|-----
block.created_ts = pb_block.header().created_time();
auto header = pb_block.header();
for (auto pb_sig :header.signatures()){
for (auto pb_sig : header.signatures()) {
Signature sig{};
std::copy(pb_sig.pubkey().begin(), pb_sig.pubkey().end(), sig.pubkey.begin());
std::copy(pb_sig.signature().begin(), pb_sig.signature().end(), sig.signature.begin());
std::copy(pb_sig.pubkey().begin(), pb_sig.pubkey().end(),
sig.pubkey.begin());
std::copy(pb_sig.signature().begin(), pb_sig.signature().end(),
sig.signature.begin());
block.sigs.push_back(sig);
}

// -----|Meta|-----
auto meta = pb_block.meta();
// potential dangerous cast
block.txs_number = (uint16_t) meta.tx_number();
block.txs_number = (uint16_t)meta.tx_number();
block.height = meta.height();
std::copy(meta.merkle_root().begin(), meta.merkle_root().end(),
block.merkle_root.begin());
Expand All @@ -81,7 +83,7 @@ namespace iroha {
// -----|Body|-----
auto body = pb_block.body();
PbTransactionFactory tx_factory;
for (auto pb_tx: body.transactions()){
for (auto pb_tx : body.transactions()) {
block.transactions.push_back(tx_factory.deserialize(pb_tx));
}

Expand All @@ -90,6 +92,6 @@ namespace iroha {

return block;
}
} // namespace converters
} // namespace model
} // namespace converters
} // namespace model
} // namespace iroha
84 changes: 84 additions & 0 deletions irohad/model/converters/impl/pb_query_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* 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 "model/converters/pb_query_factory.hpp"
#include "model/queries/get_account.hpp"
#include "model/queries/get_account_assets.hpp"
#include "model/queries/get_signatories.hpp"
#include "model/queries/get_transactions.hpp"

namespace iroha {
namespace model {
namespace converters {

std::shared_ptr<model::Query> PbQueryFactory::deserialize(
protocol::Query &pb_query) {
std::shared_ptr<model::Query> val;

if (pb_query.has_get_account()) {
// Convert to get Account
auto pb_cast = pb_query.get_account();
auto account_query = GetAccount();
account_query.account_id = pb_cast.account_id();
val = std::make_shared<model::Query>(account_query);
}

if (pb_query.has_get_account_assets()) {
// Convert to get Account Asset
auto pb_cast = pb_query.get_account_assets();
auto query = GetAccountAssets();
query.account_id = pb_cast.account_id();
query.asset_id = pb_cast.asset_id();
val = std::make_shared<model::Query>(query);
}
if (pb_query.has_get_account_signatories()) {
// Convert to get Signatories
auto pb_cast = pb_query.get_account_signatories();
auto query = GetSignatories();
query.account_id = pb_cast.account_id();
val = std::make_shared<model::Query>(query);
}

if (pb_query.has_get_account_transactions()) {
// Convert to get Signatories
auto pb_cast = pb_query.get_account_transactions();
auto query = GetAccountTransactions();
query.account_id = pb_cast.account_id();
val = std::make_shared<model::Query>(query);
}

if (!val) {
// Query not implemented
return nullptr;
}

Signature sign;
auto pb_sign = pb_query.header().signature();
std::copy(pb_sign.pubkey().begin(), pb_sign.pubkey().end(),
sign.pubkey.begin());

std::copy(pb_sign.signature().begin(), pb_sign.signature().end(),
sign.signature.begin());

val->signature = sign;
val->created_ts = pb_query.header().created_time();
val->creator_account_id = pb_query.creator_account_id();
return val;
}
}
}
}
47 changes: 47 additions & 0 deletions irohad/model/converters/pb_query_factory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* 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_PB_QUERY_FACTORY_HPP
#define IROHA_PB_QUERY_FACTORY_HPP

#include "model/query.hpp"
#include "queries.pb.h"

namespace iroha {
namespace model {
namespace converters {

/**
* Converting business objects to protobuf and vice versa
*/
class PbQueryFactory {
public:

/**
* Convert proto query to model query
* @param pb_block - reference to proto query
* @return model Query
*/
std::shared_ptr<model::Query> deserialize(protocol::Query &pb_query);


};

} // namespace converters
} // namespace model
} // namespace iroha

#endif // IROHA_PB_QUERY_FACTORY_HPP
20 changes: 10 additions & 10 deletions irohad/model/impl/query_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool iroha::model::QueryProcessingFactory::validate(
}

bool iroha::model::QueryProcessingFactory::validate(
const model::GetAccountAsset& query) {
const model::GetAccountAssets& query) {
auto creator = _wsvQuery.getAccount(query.creator_account_id);
return
// Creator account exits
Expand Down Expand Up @@ -100,17 +100,17 @@ iroha::model::QueryProcessingFactory::executeGetAccount(
}

std::shared_ptr<iroha::model::QueryResponse>
iroha::model::QueryProcessingFactory::executeGetAccountAsset(
const model::GetAccountAsset& query) {
auto acc_assets = _wsvQuery.getAccountAsset(query.account_id, query.asset_id);
if (!acc_assets.has_value()) {
iroha::model::QueryProcessingFactory::executeGetAccountAssets(
const model::GetAccountAssets& query) {
auto acct_asset = _wsvQuery.getAccountAsset(query.account_id, query.asset_id);
if (!acct_asset.has_value()) {
iroha::model::ErrorResponse response;
response.query = query;
response.reason = "No Account Asset";
response.reason = "No Account Assets";
return std::make_shared<iroha::model::ErrorResponse>(response);
}
iroha::model::AccountAssetResponse response;
response.acc_asset = acc_assets.value();
response.acct_asset = acct_asset.value();
response.query = query;
return std::make_shared<iroha::model::AccountAssetResponse>(response);
}
Expand Down Expand Up @@ -164,15 +164,15 @@ std::shared_ptr<iroha::model::QueryResponse> iroha::model::QueryProcessingFactor
}
return executeGetAccount(qry);
}
if (instanceof <iroha::model::GetAccountAsset>(query)) {
auto qry = static_cast<const iroha::model::GetAccountAsset&>(query);
if (instanceof <iroha::model::GetAccountAssets>(query)) {
auto qry = static_cast<const iroha::model::GetAccountAssets&>(query);
if (!validate(qry)) {
iroha::model::ErrorResponse response;
response.query = qry;
response.reason = "Not valid query";
return std::make_shared<iroha::model::ErrorResponse>(response);
}
return executeGetAccountAsset(qry);
return executeGetAccountAssets(qry);
}
if (instanceof <iroha::model::GetSignatories>(query)) {
auto qry = static_cast<const iroha::model::GetSignatories&>(query);
Expand Down
8 changes: 8 additions & 0 deletions irohad/model/model_crypto_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#define IROHA_MODEL_CRYPTO_PROVIDER_HPP

#include <model/transaction.hpp>
#include "model/query.hpp"

namespace iroha {
namespace model {
Expand All @@ -35,6 +36,13 @@ 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
*/
virtual bool verify(const Query &tx) const = 0;


};
}
Expand Down
8 changes: 8 additions & 0 deletions irohad/model/model_crypto_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,13 @@ namespace iroha {
}
return true;
}

bool ModelCryptoProviderImpl::verify(const Query &query) const {
HashProviderImpl hashProvider;
auto query_hash = hashProvider.get_hash(query);
auto sign = query.signature;
return iroha::verify(query_hash.data(), query_hash.size(), sign.pubkey,
sign.signature);
}
}
}
1 change: 1 addition & 0 deletions irohad/model/model_crypto_provider_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace iroha {
ed25519::pubkey_t pubkey);

bool verify(const Transaction &tx) const override;
bool verify(const Query &tx) const override;

private:
ed25519::privkey_t privkey_;
Expand Down
7 changes: 7 additions & 0 deletions irohad/model/model_hash_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include <model/block.hpp>
#include <model/transaction.hpp>
#include <model/proposal.hpp>
#include "model/query.hpp"

namespace iroha {
namespace model {
Expand Down Expand Up @@ -49,6 +50,12 @@ namespace iroha {
* @param tx - source object for computing hash
*/
virtual blob_t<N> get_hash(const Transaction &tx) = 0;

/**
* Abstract method for computing hash on Model: Query
*
*/
virtual blob_t<N> get_hash(const Query &tx) = 0;
};
}
}
Expand Down
36 changes: 35 additions & 1 deletion irohad/model/model_hash_provider_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
*/

#include <model/model_hash_provider_impl.hpp>
#include <model/queries/get_account.hpp>
#include "common/types.hpp"
#include "model/queries/get_account_assets.hpp"
#include "model/queries/get_signatories.hpp"
#include "model/queries/get_transactions.hpp"

namespace iroha {
namespace model {
Expand Down Expand Up @@ -72,7 +77,7 @@ namespace iroha {
std::copy(command_blob.begin(), command_blob.end(),
std::back_inserter(concat_hash_commands_));
}

concat_hash_commands_ += tx.creator_account_id;

// TODO: Decide if the header should be included
Expand All @@ -95,5 +100,34 @@ namespace iroha {
return concat_hash;
}

iroha::hash256_t HashProviderImpl::get_hash(const Query &query) {
std::string result_hash;
if (instanceof <model::GetAccount>(query)) {
auto cast = static_cast<const GetAccount &>(query);
result_hash += cast.account_id;
result_hash += cast.creator_account_id;
}
if (instanceof <model::GetAccountAssets>(query)) {
auto cast = static_cast<const GetAccountAssets &>(query);
result_hash += cast.account_id;
result_hash += cast.asset_id;
result_hash += cast.creator_account_id;
}
if (instanceof <model::GetSignatories>(query)) {
auto cast = static_cast<const GetAccountAssets &>(query);
result_hash += cast.account_id;
result_hash += cast.creator_account_id;
}
if (instanceof <model::GetAccountTransactions>(query)) {
auto cast = static_cast<const GetAccountTransactions &>(query);
result_hash += cast.account_id;
result_hash += cast.creator_account_id;
}

std::vector<uint8_t> concat_hash_commands(result_hash.begin(),
result_hash.end());
return sha3_256(concat_hash_commands.data(), concat_hash_commands.size());
}

} // namespace model
} // namespace iroha
Loading

0 comments on commit 19445b8

Please sign in to comment.