From 4c8189bd49859b8f2a98006f1ab58745b68979ee Mon Sep 17 00:00:00 2001 From: grimadas Date: Mon, 31 Jul 2017 14:53:45 +0300 Subject: [PATCH 01/12] Implement test for iroha-cli: - Add minor comments - Add client test for valid transaction case --- iroha-cli/CMakeLists.txt | 13 +++- iroha-cli/client.cpp | 25 ++++--- iroha-cli/client.hpp | 9 +-- iroha-cli/main.cpp | 6 +- test/module/iroha-cli/CMakeLists.txt | 6 ++ test/module/iroha-cli/client_test.cpp | 99 +++++++++++++++++++++++++++ 6 files changed, 138 insertions(+), 20 deletions(-) create mode 100644 test/module/iroha-cli/client_test.cpp diff --git a/iroha-cli/CMakeLists.txt b/iroha-cli/CMakeLists.txt index 0e711dc473..7adb025ed3 100644 --- a/iroha-cli/CMakeLists.txt +++ b/iroha-cli/CMakeLists.txt @@ -22,13 +22,23 @@ target_link_libraries(bootstrap_network add_library(cli-flags_validators validators.cpp) target_link_libraries(cli-flags_validators gflags) +add_library(client client.cpp) +target_link_libraries(client + model + crypto + optional + rapidjson + command_client + ametsuchi + ) + # IrohaCli add_executable(iroha-cli main.cpp validators.cpp - client.cpp ) target_link_libraries(iroha-cli + client crypto cli-flags_validators bootstrap_network @@ -37,5 +47,4 @@ target_link_libraries(iroha-cli ametsuchi model optional - command_client ) diff --git a/iroha-cli/client.cpp b/iroha-cli/client.cpp index 207841f8e1..6f94ff8ff3 100644 --- a/iroha-cli/client.cpp +++ b/iroha-cli/client.cpp @@ -50,8 +50,9 @@ namespace iroha_cli { if (not tx_opt.has_value()) { return "Wrong transaction format"; } - iroha::model::converters::PbTransactionFactory factory; + auto model_tx = tx_opt.value(); + // Get hash of transaction iroha::model::HashProviderImpl hashProvider; auto tx_hash = hashProvider.get_hash(model_tx); auto pubkey = iroha::hex2bytes(client_pub_key_); @@ -62,22 +63,24 @@ namespace iroha_cli { iroha::ed25519::privkey_t ed_privkey; std::copy(privkey.begin(), privkey.end(), ed_privkey.begin()); + // Sign transaction iroha::model::Signature sign; sign.pubkey = ed_pubkey; - sign.signature = iroha::sign(tx_hash.data(), tx_hash.size(), - ed_pubkey, - ed_privkey); + sign.signature = + iroha::sign(tx_hash.data(), tx_hash.size(), ed_pubkey, ed_privkey); model_tx.signatures = {sign}; - + // Convert to protobuf + iroha::model::converters::PbTransactionFactory factory; auto pb_tx = factory.serialize(model_tx); + // Send to iroha: iroha::protocol::ToriiResponse response; auto stat = client_.Torii(pb_tx, response); - if (response.validation() == iroha::protocol::STATELESS_VALIDATION_SUCCESS){ - std::cout << "Stateless validation success" << std::endl; - } else{ - std::cout << "Stateless validation error" << std::endl; + if (response.validation() == + iroha::protocol::STATELESS_VALIDATION_SUCCESS) { + return "Stateless validation success"; + } else { + return "Stateless validation error"; } - return ""; } -}; +}; // namespace iroha_cli diff --git a/iroha-cli/client.hpp b/iroha-cli/client.hpp index bfebedf7b4..cc8eed81ba 100644 --- a/iroha-cli/client.hpp +++ b/iroha-cli/client.hpp @@ -18,18 +18,19 @@ #ifndef IROHA_CLIENT_HPP #define IROHA_CLIENT_HPP +#include #include #include #include "ametsuchi/block_serializer.hpp" -#include "torii/command_client.hpp" -#include #include "common/types.hpp" +#include "torii/command_client.hpp" namespace iroha_cli { class CliClient { public: - explicit CliClient(std::string target_ip, int port, std::string account_name); + explicit CliClient(std::string target_ip, int port, + std::string account_name); /** * Send transaction to Iroha-Network * @param json_tx @@ -42,6 +43,6 @@ namespace iroha_cli { std::string client_priv_key_; std::string client_pub_key_; }; -}; +} // namespace iroha_cli #endif // IROHA_CLIENT_CPP_HPP diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp index 1cccaf212b..254dbb959b 100644 --- a/iroha-cli/main.cpp +++ b/iroha-cli/main.cpp @@ -32,10 +32,10 @@ // https://hackmd.io/GwRmwQ2BmCFoCsAGARtOAWBIBMcAcS0GcAZjhNNPvpAKZIDGQA== DEFINE_string(config, "", "Trusted peer's ip addresses"); -DEFINE_validator(config, &iroha_cli::validate_config); +//DEFINE_validator(config, &iroha_cli::validate_config); DEFINE_string(genesis_block, "", "Genesis block for sending network"); -DEFINE_validator(genesis_block, &iroha_cli::validate_genesis_block); +//DEFINE_validator(genesis_block, &iroha_cli::validate_genesis_block); DEFINE_bool(new_account, false, "Choose if account does not exist"); DEFINE_string(name, "", "Name of the account"); @@ -44,7 +44,7 @@ DEFINE_string(name, "", "Name of the account"); DEFINE_bool(grpc, false, "Send sample transaction to IrohaNetwork"); DEFINE_string(address, "127.0.0.1", "Address of the Iroha node"); DEFINE_int32(torii_port, 50051, "Port of iroha's Torii"); -DEFINE_validator(torii_port, &iroha_cli::validate_port); +//DEFINE_validator(torii_port, &iroha_cli::validate_port); DEFINE_string(json_transaction, "", "Transaction in json format"); diff --git a/test/module/iroha-cli/CMakeLists.txt b/test/module/iroha-cli/CMakeLists.txt index d537f8bdd1..f35dd10ebf 100644 --- a/test/module/iroha-cli/CMakeLists.txt +++ b/test/module/iroha-cli/CMakeLists.txt @@ -5,3 +5,9 @@ target_link_libraries(bootstrap_network_test ametsuchi ) +addtest(client_test client_test.cpp) +target_link_libraries(client_test + client + processors + server_runner + ) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp new file mode 100644 index 0000000000..0700a40184 --- /dev/null +++ b/test/module/iroha-cli/client_test.cpp @@ -0,0 +1,99 @@ +/** + * 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 "../../../iroha-cli/client.hpp" +#include +#include + +#include "../irohad/torii/mock_classes.hpp" +#include "main/server_runner.hpp" +#include "torii/processor/query_processor_impl.hpp" +#include "torii/processor/transaction_processor_impl.hpp" + +constexpr const char *Ip = "0.0.0.0"; +constexpr int Port = 50051; + +using ::testing::Return; +using ::testing::A; +using ::testing::_; +using ::testing::AtLeast; + +class ClientTest : public testing::Test { + public: + virtual void SetUp() { + // Run a server + runner = new ServerRunner(Ip, Port); + th = std::thread([ this] { + // ----------- Command Service -------------- + auto tx_processor = + iroha::torii::TransactionProcessorImpl(pcsMock, svMock); + iroha::model::converters::PbTransactionFactory pb_tx_factory; + auto command_service = + std::make_unique(pb_tx_factory, tx_processor); + + //----------- Query Service ---------- + iroha::model::QueryProcessingFactory qpf(wsv_query, block_query); + + iroha::torii::QueryProcessorImpl qpi(qpf, svMock); + + iroha::model::converters::PbQueryFactory pb_query_factory; + iroha::model::converters::PbQueryResponseFactory pb_query_resp_factory; + + auto query_service = std::make_unique( + pb_query_factory, pb_query_resp_factory, qpi); + + //----------- Server run ---------------- + runner->run(std::move(command_service), std::move(query_service)); + }); + + runner->waitForServersReady(); + } + + virtual void TearDown() { + runner->shutdown(); + delete runner; + th.join(); + } + + ServerRunner *runner; + std::thread th; + PCSMock pcsMock; + SVMock svMock; + WsvQueryMock wsv_query; + BlockQueryMock block_query; +}; + +TEST_F(ClientTest, SendTxWhenValid) { + iroha_cli::CliClient client(Ip, Port, "test"); + EXPECT_CALL(svMock, validate(A())) + .WillRepeatedly(Return(true)); + EXPECT_CALL(pcsMock,propagate_transaction(_)).Times(1); + + auto json_tx = + "{" + "\"creator_account_id\": \"123\",\n" + "\"tx_counter\": 0,\n" + "\"commands\": [{\n" + " \"command_type\": \"AddPeer\",\n" + " \"address\": \"localhost\",\n" + " \"peer_key\": " + "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" + "}]}"; + std::cout << "Sending json transaction to Iroha" << std::endl; + std::cout << client.sendTx(json_tx) << std::endl; + +} From 333e485a896f5bcd68530c4c73c6e219551da9f8 Mon Sep 17 00:00:00 2001 From: grimadas Date: Mon, 31 Jul 2017 16:48:34 +0300 Subject: [PATCH 02/12] Fix json in client_test --- test/module/iroha-cli/client_test.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index 0700a40184..39f91c75fe 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -83,17 +83,15 @@ TEST_F(ClientTest, SendTxWhenValid) { .WillRepeatedly(Return(true)); EXPECT_CALL(pcsMock,propagate_transaction(_)).Times(1); - auto json_tx = - "{" - "\"creator_account_id\": \"123\",\n" - "\"tx_counter\": 0,\n" - "\"commands\": [{\n" + auto json_tx ="{\n" + " \"creator_account_id\": \"test\", \n" + " \"tx_counter\": 0,\n" + " \"commands\":[{\n" " \"command_type\": \"AddPeer\",\n" - " \"address\": \"localhost\",\n" - " \"peer_key\": " - "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" - "}]}"; + " \"address\": \"localhost\",\n" + " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " }]\n" + "}"; std::cout << "Sending json transaction to Iroha" << std::endl; std::cout << client.sendTx(json_tx) << std::endl; - } From 792efab009ab1dbcc2bd99bba969847ab126a072 Mon Sep 17 00:00:00 2001 From: grimadas Date: Mon, 31 Jul 2017 19:33:27 +0300 Subject: [PATCH 03/12] Add test for invalid json case --- test/module/iroha-cli/client_test.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index 39f91c75fe..9c59919445 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -95,3 +95,23 @@ TEST_F(ClientTest, SendTxWhenValid) { std::cout << "Sending json transaction to Iroha" << std::endl; std::cout << client.sendTx(json_tx) << std::endl; } + +TEST_F(ClientTest, SendTxWhenInvalidJson) { + iroha_cli::CliClient client(Ip, Port, "test"); + EXPECT_CALL(svMock, validate(A())) + .WillRepeatedly(Return(true)); + EXPECT_CALL(pcsMock,propagate_transaction(_)).Times(1); + // Json with no Transaction + auto json_tx ="{\n" + " \"creator_account_id\": \"test\", \n" + " \"commands\":[{\n" + " \"command_type\": \"AddPeer\",\n" + " \"address\": \"localhost\",\n" + " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " }]\n" + "}"; + std::cout << "Sending json transaction to Iroha" << std::endl; + std::cout << client.sendTx(json_tx) << std::endl; + +} + From 218ea0304deb1ad8ab3b77b1b21e7f4bda77dec3 Mon Sep 17 00:00:00 2001 From: grimadas Date: Tue, 1 Aug 2017 12:41:07 +0300 Subject: [PATCH 04/12] Fix client test --- test/module/iroha-cli/client_test.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index 9c59919445..b8b31ea915 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -100,7 +100,6 @@ TEST_F(ClientTest, SendTxWhenInvalidJson) { iroha_cli::CliClient client(Ip, Port, "test"); EXPECT_CALL(svMock, validate(A())) .WillRepeatedly(Return(true)); - EXPECT_CALL(pcsMock,propagate_transaction(_)).Times(1); // Json with no Transaction auto json_tx ="{\n" " \"creator_account_id\": \"test\", \n" From 57984f23695231ac584ff77b6b8a42a3bcb3afb3 Mon Sep 17 00:00:00 2001 From: grimadas Date: Tue, 1 Aug 2017 14:57:56 +0300 Subject: [PATCH 05/12] Refactor client: - Add tests for invalid cases - Move creation of public and private key from main to client --- iroha-cli/client.cpp | 75 ++++++++++++++++++++------- iroha-cli/client.hpp | 22 ++++++-- iroha-cli/main.cpp | 58 ++++++--------------- test/module/iroha-cli/client_test.cpp | 65 +++++++++++++++++++++-- 4 files changed, 152 insertions(+), 68 deletions(-) diff --git a/iroha-cli/client.cpp b/iroha-cli/client.cpp index 6f94ff8ff3..7ab0059ecf 100644 --- a/iroha-cli/client.cpp +++ b/iroha-cli/client.cpp @@ -31,27 +31,37 @@ #include "model/converters/pb_transaction_factory.hpp" #include "model/model_hash_provider_impl.hpp" -namespace iroha_cli { +#include - CliClient::CliClient(std::string target_ip, int port, std::string name) - : client_(target_ip, port) { - std::ifstream priv_file(name + ".priv"); - priv_file >> client_priv_key_; - priv_file.close(); +namespace iroha_cli { - std::ifstream pub_file(name + ".pub"); - pub_file >> client_pub_key_; - pub_file.close(); - } + CliClient::CliClient(std::string target_ip, int port) + : client_(target_ip, port) {} - std::string CliClient::sendTx(std::string json_tx) { + CliClient::Status CliClient::sendTx(std::string json_tx) { iroha::ametsuchi::BlockSerializer serializer; auto tx_opt = serializer.deserialize(json_tx); if (not tx_opt.has_value()) { - return "Wrong transaction format"; + return WRONG_FORMAT; } - auto model_tx = tx_opt.value(); + // Get private and public key of transaction creator + std::string client_pub_key_; + std::string client_priv_key_; + std::ifstream priv_file(model_tx.creator_account_id + ".priv"); + if (not priv_file) { + return NO_KEYS; + } + priv_file >> client_priv_key_; + priv_file.close(); + + std::ifstream pub_file(model_tx.creator_account_id + ".pub"); + if (not pub_file) { + return NO_KEYS; + } + pub_file >> client_pub_key_; + pub_file.close(); + // Get hash of transaction iroha::model::HashProviderImpl hashProvider; auto tx_hash = hashProvider.get_hash(model_tx); @@ -75,12 +85,41 @@ namespace iroha_cli { // Send to iroha: iroha::protocol::ToriiResponse response; auto stat = client_.Torii(pb_tx, response); - if (response.validation() == - iroha::protocol::STATELESS_VALIDATION_SUCCESS) { - return "Stateless validation success"; - } else { - return "Stateless validation error"; + + return response.validation() == + iroha::protocol::STATELESS_VALIDATION_SUCCESS + ? OK + : NOT_VALID; + } + + std::string CliClient::hex_str(unsigned char* data, int len) { + constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; + std::string s((unsigned long)(len * 2), ' '); + for (int i = 0; i < len; ++i) { + s[2 * i] = hexmap[(data[i] & 0xF0) >> 4]; + s[2 * i + 1] = hexmap[data[i] & 0x0F]; } + return s; + } + + void CliClient::create_account(std::string account_name) { + unsigned char public_key[32], private_key[64], seed[32]; + + ed25519_create_keypair(public_key, private_key, seed); + auto pub_hex = hex_str(public_key, 32); + + auto priv_hex = hex_str(private_key, 64); + + // Save pubkey to file + std::ofstream pub_file(account_name + ".pub"); + pub_file << pub_hex; + pub_file.close(); + + // Save privkey to file + std::ofstream priv_file(account_name + ".priv"); + priv_file << priv_hex; + priv_file.close(); } }; // namespace iroha_cli diff --git a/iroha-cli/client.hpp b/iroha-cli/client.hpp index cc8eed81ba..4326fce208 100644 --- a/iroha-cli/client.hpp +++ b/iroha-cli/client.hpp @@ -25,23 +25,35 @@ #include "common/types.hpp" #include "torii/command_client.hpp" + namespace iroha_cli { class CliClient { public: - explicit CliClient(std::string target_ip, int port, - std::string account_name); + enum Status { + WRONG_FORMAT, + NO_KEYS, + NOT_VALID, + OK + }; + + + explicit CliClient(std::string target_ip, int port); /** * Send transaction to Iroha-Network * @param json_tx * @return */ - std::string sendTx(std::string json_tx); + Status sendTx(std::string json_tx); + + static void create_account(std::string account_name); + + iroha::protocol::ToriiResponse get_Tx_response(); private: torii::CommandSyncClient client_; - std::string client_priv_key_; - std::string client_pub_key_; + + static std::string hex_str(unsigned char* data, int len); }; } // namespace iroha_cli diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp index 254dbb959b..6e595add24 100644 --- a/iroha-cli/main.cpp +++ b/iroha-cli/main.cpp @@ -15,9 +15,7 @@ * limitations under the License. */ -#include #include -#include #include #include #include "validators.hpp" @@ -59,7 +57,10 @@ int main(int argc, char* argv[]) { if (std::ifstream(FLAGS_name + ".pub")) { assert_config::assert_fatal(false, "File already exists"); } - create_account(FLAGS_name); + iroha_cli::CliClient::create_account(FLAGS_name); + std::cout << "Public and private key has been generated in current directory" + << std::endl; + } else if (not FLAGS_config.empty() && not FLAGS_genesis_block.empty()) { iroha_cli::GenesisBlockClientImpl genesis_block_client; auto bootstrap = iroha_cli::BootstrapNetwork(genesis_block_client); @@ -71,48 +72,23 @@ int main(int argc, char* argv[]) { std::cout << "Send transaction to " << FLAGS_address << ":" << FLAGS_torii_port << std::endl; - iroha_cli::CliClient client(FLAGS_address, FLAGS_torii_port, FLAGS_name); + iroha_cli::CliClient client(FLAGS_address, FLAGS_torii_port); + auto status = client.sendTx(FLAGS_json_transaction); + switch(status) { + case iroha_cli::CliClient::OK: std::cout << "Transaction successfully sent" << std::endl; + break; + case iroha_cli::CliClient::WRONG_FORMAT: std::cout << "Transaction wrong json format" << std::endl; + break; + case iroha_cli::CliClient::NO_KEYS: std::cout << "No public and private key found. Run with new_account flag." << std::endl; + break; + case iroha_cli::CliClient::NOT_VALID: std::cout << "Transaction is not valid." << std::endl; + break; + } + - client.sendTx(FLAGS_json_transaction); - return 0; } else { assert_config::assert_fatal(false, "Invalid flags"); } return 0; } -std::string hex_str(unsigned char* data, int len) { - constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - std::string s((unsigned long)(len * 2), ' '); - for (int i = 0; i < len; ++i) { - s[2 * i] = hexmap[(data[i] & 0xF0) >> 4]; - s[2 * i + 1] = hexmap[data[i] & 0x0F]; - } - return s; -} - -/** - * Command to create a new account using the interactive console. - */ -void create_account(std::string name) { - unsigned char public_key[32], private_key[64], seed[32]; - - ed25519_create_keypair(public_key, private_key, seed); - auto pub_hex = hex_str(public_key, 32); - - auto priv_hex = hex_str(private_key, 64); - - // Save pubkey to file - std::ofstream pub_file(name + ".pub"); - pub_file << pub_hex; - pub_file.close(); - - // Save privkey to file - std::ofstream priv_file(name + ".priv"); - priv_file << priv_hex; - priv_file.close(); - - std::cout << "Public and private key has been generated in current directory" - << std::endl; -} diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index b8b31ea915..a6de4fb5dd 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -35,6 +35,8 @@ using ::testing::AtLeast; class ClientTest : public testing::Test { public: virtual void SetUp() { + // Create public key file + // Run a server runner = new ServerRunner(Ip, Port); th = std::thread([ this] { @@ -78,7 +80,9 @@ class ClientTest : public testing::Test { }; TEST_F(ClientTest, SendTxWhenValid) { - iroha_cli::CliClient client(Ip, Port, "test"); + std::string account_name = "test"; + iroha_cli::CliClient::create_account(account_name); + iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) .WillRepeatedly(Return(true)); EXPECT_CALL(pcsMock,propagate_transaction(_)).Times(1); @@ -93,11 +97,17 @@ TEST_F(ClientTest, SendTxWhenValid) { " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; - std::cout << client.sendTx(json_tx) << std::endl; + auto status = client.sendTx(json_tx); + ASSERT_EQ(status, iroha_cli::CliClient::OK); + // Remove private and public key + std::remove((account_name+".pub").c_str()); + std::remove((account_name+".priv").c_str()); } TEST_F(ClientTest, SendTxWhenInvalidJson) { - iroha_cli::CliClient client(Ip, Port, "test"); + std::string account_name = "test"; + iroha_cli::CliClient::create_account(account_name); + iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) .WillRepeatedly(Return(true)); // Json with no Transaction @@ -110,7 +120,54 @@ TEST_F(ClientTest, SendTxWhenInvalidJson) { " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; - std::cout << client.sendTx(json_tx) << std::endl; + ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::WRONG_FORMAT); + // Remove private and public key + std::remove((account_name+".pub").c_str()); + std::remove((account_name+".priv").c_str()); +} + +TEST_F(ClientTest, SendTxWhenStatelessInvalid) { + std::string account_name = "test"; + iroha_cli::CliClient::create_account(account_name); + iroha_cli::CliClient client(Ip, Port); + EXPECT_CALL(svMock, validate(A())) + .WillRepeatedly(Return(false)); + // Json with no Transaction + auto json_tx ="{\n" + " \"creator_account_id\": \"test\", \n" + " \"tx_counter\": 0,\n" + " \"commands\":[{\n" + " \"command_type\": \"AddPeer\",\n" + " \"address\": \"localhost\",\n" + " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " }]\n" + "}"; + std::cout << "Sending json transaction to Iroha" << std::endl; + ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NOT_VALID); + // Remove private and public key + std::remove((account_name+".pub").c_str()); + std::remove((account_name+".priv").c_str()); } + + +TEST_F(ClientTest, SendTxWhenNoKeys) { + std::string account_name = "test"; + // Client without public, private keys + iroha_cli::CliClient client(Ip, Port); + EXPECT_CALL(svMock, validate(A())) + .WillRepeatedly(Return(true)); + // Json with no Transaction + auto json_tx ="{\n" + " \"creator_account_id\": \"test\", \n" + " \"tx_counter\": 0,\n" + " \"commands\":[{\n" + " \"command_type\": \"AddPeer\",\n" + " \"address\": \"localhost\",\n" + " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " }]\n" + "}"; + std::cout << "Sending json transaction to Iroha" << std::endl; + ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NO_KEYS); +} \ No newline at end of file From 79b817faaa1a6232bb17e39e83ec8f52d0610327 Mon Sep 17 00:00:00 2001 From: kamilsa Date: Tue, 1 Aug 2017 16:41:28 +0300 Subject: [PATCH 06/12] Fix (unused variable, NO_KEY renamed to NO_ACCOUNT) --- iroha-cli/client.cpp | 4 ++-- iroha-cli/client.hpp | 2 +- iroha-cli/main.cpp | 2 +- test/module/iroha-cli/client_test.cpp | 5 ++--- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/iroha-cli/client.cpp b/iroha-cli/client.cpp index 7ab0059ecf..0e15f780ea 100644 --- a/iroha-cli/client.cpp +++ b/iroha-cli/client.cpp @@ -50,14 +50,14 @@ namespace iroha_cli { std::string client_priv_key_; std::ifstream priv_file(model_tx.creator_account_id + ".priv"); if (not priv_file) { - return NO_KEYS; + return NO_ACCOUNT; } priv_file >> client_priv_key_; priv_file.close(); std::ifstream pub_file(model_tx.creator_account_id + ".pub"); if (not pub_file) { - return NO_KEYS; + return NO_ACCOUNT; } pub_file >> client_pub_key_; pub_file.close(); diff --git a/iroha-cli/client.hpp b/iroha-cli/client.hpp index 4326fce208..a0cf61ff16 100644 --- a/iroha-cli/client.hpp +++ b/iroha-cli/client.hpp @@ -32,7 +32,7 @@ namespace iroha_cli { public: enum Status { WRONG_FORMAT, - NO_KEYS, + NO_ACCOUNT, NOT_VALID, OK }; diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp index 6e595add24..bca3adeb1a 100644 --- a/iroha-cli/main.cpp +++ b/iroha-cli/main.cpp @@ -79,7 +79,7 @@ int main(int argc, char* argv[]) { break; case iroha_cli::CliClient::WRONG_FORMAT: std::cout << "Transaction wrong json format" << std::endl; break; - case iroha_cli::CliClient::NO_KEYS: std::cout << "No public and private key found. Run with new_account flag." << std::endl; + case iroha_cli::CliClient::NO_ACCOUNT: std::cout << "No public and private key found. Run with new_account flag." << std::endl; break; case iroha_cli::CliClient::NOT_VALID: std::cout << "Transaction is not valid." << std::endl; break; diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index a6de4fb5dd..c36c0b2154 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -153,7 +153,6 @@ TEST_F(ClientTest, SendTxWhenStatelessInvalid) { TEST_F(ClientTest, SendTxWhenNoKeys) { - std::string account_name = "test"; // Client without public, private keys iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) @@ -169,5 +168,5 @@ TEST_F(ClientTest, SendTxWhenNoKeys) { " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; - ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NO_KEYS); -} \ No newline at end of file + ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NO_ACCOUNT); +} From 55e2c65601e2812da94a6181a7dc5911b7804c2a Mon Sep 17 00:00:00 2001 From: grimadas Date: Tue, 1 Aug 2017 17:21:06 +0300 Subject: [PATCH 07/12] Fix client test issues: - Add minor comments to client test - Recomment validators in iroha-cli - Add pass phrase to create account method --- iroha-cli/CMakeLists.txt | 60 ++++++++++++------------- iroha-cli/client.cpp | 31 ++++--------- iroha-cli/client.hpp | 6 +-- iroha-cli/main.cpp | 12 ++--- test/module/iroha-cli/CMakeLists.txt | 16 +++---- test/module/iroha-cli/client_test.cpp | 64 +++++++++++++++------------ 6 files changed, 89 insertions(+), 100 deletions(-) diff --git a/iroha-cli/CMakeLists.txt b/iroha-cli/CMakeLists.txt index 7adb025ed3..d02df696ca 100644 --- a/iroha-cli/CMakeLists.txt +++ b/iroha-cli/CMakeLists.txt @@ -3,20 +3,20 @@ set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) # GenesisBlockClient add_library(genesis_block_client genesis_block_client_impl.cpp) target_link_libraries(genesis_block_client - endpoint - optional - model - ) + endpoint + optional + model + ) # BootstrapNetwork add_library(bootstrap_network bootstrap_network.cpp) target_link_libraries(bootstrap_network - genesis_block_client - rapidjson - model - ametsuchi - ip_tools - ) + genesis_block_client + rapidjson + model + ametsuchi + ip_tools + ) # Gflags config validators add_library(cli-flags_validators validators.cpp) @@ -24,27 +24,27 @@ target_link_libraries(cli-flags_validators gflags) add_library(client client.cpp) target_link_libraries(client - model - crypto - optional - rapidjson - command_client - ametsuchi - ) + model + crypto + optional + rapidjson + command_client + ametsuchi + ) # IrohaCli add_executable(iroha-cli - main.cpp - validators.cpp - ) + main.cpp + validators.cpp + ) target_link_libraries(iroha-cli - client - crypto - cli-flags_validators - bootstrap_network - rapidjson - logger - ametsuchi - model - optional - ) + client + crypto + cli-flags_validators + bootstrap_network + rapidjson + logger + ametsuchi + model + optional + ) diff --git a/iroha-cli/client.cpp b/iroha-cli/client.cpp index 0e15f780ea..7c61ed5736 100644 --- a/iroha-cli/client.cpp +++ b/iroha-cli/client.cpp @@ -31,7 +31,8 @@ #include "model/converters/pb_transaction_factory.hpp" #include "model/model_hash_provider_impl.hpp" -#include +#include "crypto/crypto.hpp" +#include "common/types.hpp" namespace iroha_cli { @@ -76,6 +77,7 @@ namespace iroha_cli { // Sign transaction iroha::model::Signature sign; sign.pubkey = ed_pubkey; + // TODO: Fix signature to work with data, not hash sign.signature = iroha::sign(tx_hash.data(), tx_hash.size(), ed_pubkey, ed_privkey); model_tx.signatures = {sign}; @@ -92,34 +94,17 @@ namespace iroha_cli { : NOT_VALID; } - std::string CliClient::hex_str(unsigned char* data, int len) { - constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - std::string s((unsigned long)(len * 2), ' '); - for (int i = 0; i < len; ++i) { - s[2 * i] = hexmap[(data[i] & 0xF0) >> 4]; - s[2 * i + 1] = hexmap[data[i] & 0x0F]; - } - return s; - } - - void CliClient::create_account(std::string account_name) { - unsigned char public_key[32], private_key[64], seed[32]; - - ed25519_create_keypair(public_key, private_key, seed); - auto pub_hex = hex_str(public_key, 32); + void CliClient::create_account(std::string account_name, std::string pass_phrase) { - auto priv_hex = hex_str(private_key, 64); + auto seed = iroha::create_seed(pass_phrase); + auto key_pairs = iroha::create_keypair(seed); // Save pubkey to file std::ofstream pub_file(account_name + ".pub"); - pub_file << pub_hex; - pub_file.close(); - + pub_file << key_pairs.pubkey.to_hexstring(); // Save privkey to file std::ofstream priv_file(account_name + ".priv"); - priv_file << priv_hex; - priv_file.close(); + priv_file << key_pairs.privkey.to_hexstring(); } }; // namespace iroha_cli diff --git a/iroha-cli/client.hpp b/iroha-cli/client.hpp index a0cf61ff16..e7d83bb48a 100644 --- a/iroha-cli/client.hpp +++ b/iroha-cli/client.hpp @@ -46,14 +46,10 @@ namespace iroha_cli { */ Status sendTx(std::string json_tx); - static void create_account(std::string account_name); - - iroha::protocol::ToriiResponse get_Tx_response(); + static void create_account(std::string account_name, std::string pass_phrase); private: torii::CommandSyncClient client_; - - static std::string hex_str(unsigned char* data, int len); }; } // namespace iroha_cli diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp index bca3adeb1a..37aa8d0e74 100644 --- a/iroha-cli/main.cpp +++ b/iroha-cli/main.cpp @@ -30,22 +30,24 @@ // https://hackmd.io/GwRmwQ2BmCFoCsAGARtOAWBIBMcAcS0GcAZjhNNPvpAKZIDGQA== DEFINE_string(config, "", "Trusted peer's ip addresses"); -//DEFINE_validator(config, &iroha_cli::validate_config); +DEFINE_validator(config, &iroha_cli::validate_config); DEFINE_string(genesis_block, "", "Genesis block for sending network"); -//DEFINE_validator(genesis_block, &iroha_cli::validate_genesis_block); +DEFINE_validator(genesis_block, &iroha_cli::validate_genesis_block); DEFINE_bool(new_account, false, "Choose if account does not exist"); DEFINE_string(name, "", "Name of the account"); +DEFINE_string(pass_phrase, "", "Name of the account"); // Sending transaction to Iroha DEFINE_bool(grpc, false, "Send sample transaction to IrohaNetwork"); -DEFINE_string(address, "127.0.0.1", "Address of the Iroha node"); +DEFINE_string(address, "0.0.0.0", "Address of the Iroha node"); DEFINE_int32(torii_port, 50051, "Port of iroha's Torii"); -//DEFINE_validator(torii_port, &iroha_cli::validate_port); +DEFINE_validator(torii_port, &iroha_cli::validate_port); DEFINE_string(json_transaction, "", "Transaction in json format"); + void create_account(std::string name); int main(int argc, char* argv[]) { @@ -57,7 +59,7 @@ int main(int argc, char* argv[]) { if (std::ifstream(FLAGS_name + ".pub")) { assert_config::assert_fatal(false, "File already exists"); } - iroha_cli::CliClient::create_account(FLAGS_name); + iroha_cli::CliClient::create_account(FLAGS_name, FLAGS_pass_phrase); std::cout << "Public and private key has been generated in current directory" << std::endl; diff --git a/test/module/iroha-cli/CMakeLists.txt b/test/module/iroha-cli/CMakeLists.txt index f35dd10ebf..98d40e4669 100644 --- a/test/module/iroha-cli/CMakeLists.txt +++ b/test/module/iroha-cli/CMakeLists.txt @@ -1,13 +1,13 @@ addtest(bootstrap_network_test bootstrap_network_test.cpp) target_link_libraries(bootstrap_network_test - bootstrap_network - crypto - ametsuchi - ) + bootstrap_network + crypto + ametsuchi + ) addtest(client_test client_test.cpp) target_link_libraries(client_test - client - processors - server_runner - ) + client + processors + server_runner + ) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index c36c0b2154..3e83739a4a 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -38,8 +38,8 @@ class ClientTest : public testing::Test { // Create public key file // Run a server - runner = new ServerRunner(Ip, Port); - th = std::thread([ this] { + runner = std::make_unique(Ip, Port); + th = std::thread([this] { // ----------- Command Service -------------- auto tx_processor = iroha::torii::TransactionProcessorImpl(pcsMock, svMock); @@ -67,11 +67,10 @@ class ClientTest : public testing::Test { virtual void TearDown() { runner->shutdown(); - delete runner; th.join(); } - ServerRunner *runner; + std::unique_ptr runner; std::thread th; PCSMock pcsMock; SVMock svMock; @@ -81,90 +80,97 @@ class ClientTest : public testing::Test { TEST_F(ClientTest, SendTxWhenValid) { std::string account_name = "test"; - iroha_cli::CliClient::create_account(account_name); + iroha_cli::CliClient::create_account(account_name, ""); iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) - .WillRepeatedly(Return(true)); - EXPECT_CALL(pcsMock,propagate_transaction(_)).Times(1); + .WillOnce(Return(true)); + EXPECT_CALL(pcsMock, propagate_transaction(_)).Times(1); - auto json_tx ="{\n" + auto json_tx = + "{\n" " \"creator_account_id\": \"test\", \n" " \"tx_counter\": 0,\n" " \"commands\":[{\n" " \"command_type\": \"AddPeer\",\n" " \"address\": \"localhost\",\n" - " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " \"peer_key\": " + "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; auto status = client.sendTx(json_tx); ASSERT_EQ(status, iroha_cli::CliClient::OK); // Remove private and public key - std::remove((account_name+".pub").c_str()); - std::remove((account_name+".priv").c_str()); + std::remove((account_name + ".pub").c_str()); + std::remove((account_name + ".priv").c_str()); } TEST_F(ClientTest, SendTxWhenInvalidJson) { std::string account_name = "test"; - iroha_cli::CliClient::create_account(account_name); + iroha_cli::CliClient::create_account(account_name, ""); iroha_cli::CliClient client(Ip, Port); + // Must not call stateful validation EXPECT_CALL(svMock, validate(A())) - .WillRepeatedly(Return(true)); + .Times(0); // Json with no Transaction - auto json_tx ="{\n" + auto json_tx = + "{\n" " \"creator_account_id\": \"test\", \n" " \"commands\":[{\n" " \"command_type\": \"AddPeer\",\n" " \"address\": \"localhost\",\n" - " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " \"peer_key\": " + "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::WRONG_FORMAT); // Remove private and public key - std::remove((account_name+".pub").c_str()); - std::remove((account_name+".priv").c_str()); + std::remove((account_name + ".pub").c_str()); + std::remove((account_name + ".priv").c_str()); } - TEST_F(ClientTest, SendTxWhenStatelessInvalid) { std::string account_name = "test"; - iroha_cli::CliClient::create_account(account_name); + iroha_cli::CliClient::create_account(account_name, ""); iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) - .WillRepeatedly(Return(false)); + .WillOnce(Return(false)); // Json with no Transaction - auto json_tx ="{\n" + auto json_tx = + "{\n" " \"creator_account_id\": \"test\", \n" " \"tx_counter\": 0,\n" " \"commands\":[{\n" " \"command_type\": \"AddPeer\",\n" " \"address\": \"localhost\",\n" - " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " \"peer_key\": " + "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NOT_VALID); // Remove private and public key - std::remove((account_name+".pub").c_str()); - std::remove((account_name+".priv").c_str()); + std::remove((account_name + ".pub").c_str()); + std::remove((account_name + ".priv").c_str()); } - - TEST_F(ClientTest, SendTxWhenNoKeys) { // Client without public, private keys iroha_cli::CliClient client(Ip, Port); + // Must not call stateful validation EXPECT_CALL(svMock, validate(A())) - .WillRepeatedly(Return(true)); + .Times(0); // Json with no Transaction - auto json_tx ="{\n" + auto json_tx = + "{\n" " \"creator_account_id\": \"test\", \n" " \"tx_counter\": 0,\n" " \"commands\":[{\n" " \"command_type\": \"AddPeer\",\n" " \"address\": \"localhost\",\n" - " \"peer_key\": \"2323232323232323232323232323232323232323232323232323232323232323\"\n" + " \"peer_key\": " + "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" " }]\n" "}"; std::cout << "Sending json transaction to Iroha" << std::endl; From dc33d1deb71f341d7dbfaa4656332262994a4bb5 Mon Sep 17 00:00:00 2001 From: grimadas Date: Tue, 1 Aug 2017 18:45:53 +0300 Subject: [PATCH 08/12] Remove redundant comment --- test/module/iroha-cli/client_test.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index 3e83739a4a..7814c56444 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -35,8 +35,6 @@ using ::testing::AtLeast; class ClientTest : public testing::Test { public: virtual void SetUp() { - // Create public key file - // Run a server runner = std::make_unique(Ip, Port); th = std::thread([this] { From 8d1a2de59235081467fd35d42e95955aa347fce5 Mon Sep 17 00:00:00 2001 From: grimadas Date: Wed, 2 Aug 2017 15:25:28 +0300 Subject: [PATCH 09/12] Redesign iroha client: - Add KeyManager interface and implementation - Change client to receive only signed json tx - Refactor iroha-cli main - Refactor block serializer: signature is now required - Refactor client test: Change json-files --- iroha-cli/CMakeLists.txt | 13 +++- iroha-cli/client.cpp | 63 ---------------- iroha-cli/client.hpp | 17 +---- iroha-cli/impl/keys_manager_impl.cpp | 70 ++++++++++++++++++ iroha-cli/impl/keys_manager_impl.hpp | 35 +++++++++ iroha-cli/keys_manager.hpp | 46 ++++++++++++ iroha-cli/main.cpp | 40 +++++------ irohad/ametsuchi/impl/block_serializer.cpp | 56 ++++++--------- test/module/iroha-cli/client_test.cpp | 84 ++++++++-------------- 9 files changed, 231 insertions(+), 193 deletions(-) create mode 100644 iroha-cli/impl/keys_manager_impl.cpp create mode 100644 iroha-cli/impl/keys_manager_impl.hpp create mode 100644 iroha-cli/keys_manager.hpp diff --git a/iroha-cli/CMakeLists.txt b/iroha-cli/CMakeLists.txt index d02df696ca..1a418769c1 100644 --- a/iroha-cli/CMakeLists.txt +++ b/iroha-cli/CMakeLists.txt @@ -22,7 +22,15 @@ target_link_libraries(bootstrap_network add_library(cli-flags_validators validators.cpp) target_link_libraries(cli-flags_validators gflags) -add_library(client client.cpp) +add_library(keys_manager impl/keys_manager_impl.cpp) +target_link_libraries(keys_manager + optional + crypto + ) + +add_library(client + client.cpp + ) target_link_libraries(client model crypto @@ -39,12 +47,11 @@ add_executable(iroha-cli ) target_link_libraries(iroha-cli client - crypto cli-flags_validators bootstrap_network rapidjson logger ametsuchi model - optional + keys_manager ) diff --git a/iroha-cli/client.cpp b/iroha-cli/client.cpp index 7c61ed5736..554c8dd6e0 100644 --- a/iroha-cli/client.cpp +++ b/iroha-cli/client.cpp @@ -15,24 +15,9 @@ * limitations under the License. */ -// In iroha-cli only, " is used. #include "client.hpp" - -#include -#include -#include -#include -#include - -#include -#include - #include "ametsuchi/block_serializer.hpp" #include "model/converters/pb_transaction_factory.hpp" -#include "model/model_hash_provider_impl.hpp" - -#include "crypto/crypto.hpp" -#include "common/types.hpp" namespace iroha_cli { @@ -46,41 +31,6 @@ namespace iroha_cli { return WRONG_FORMAT; } auto model_tx = tx_opt.value(); - // Get private and public key of transaction creator - std::string client_pub_key_; - std::string client_priv_key_; - std::ifstream priv_file(model_tx.creator_account_id + ".priv"); - if (not priv_file) { - return NO_ACCOUNT; - } - priv_file >> client_priv_key_; - priv_file.close(); - - std::ifstream pub_file(model_tx.creator_account_id + ".pub"); - if (not pub_file) { - return NO_ACCOUNT; - } - pub_file >> client_pub_key_; - pub_file.close(); - - // Get hash of transaction - iroha::model::HashProviderImpl hashProvider; - auto tx_hash = hashProvider.get_hash(model_tx); - auto pubkey = iroha::hex2bytes(client_pub_key_); - auto privkey = iroha::hex2bytes(client_priv_key_); - - iroha::ed25519::pubkey_t ed_pubkey; - std::copy(pubkey.begin(), pubkey.end(), ed_pubkey.begin()); - - iroha::ed25519::privkey_t ed_privkey; - std::copy(privkey.begin(), privkey.end(), ed_privkey.begin()); - // Sign transaction - iroha::model::Signature sign; - sign.pubkey = ed_pubkey; - // TODO: Fix signature to work with data, not hash - sign.signature = - iroha::sign(tx_hash.data(), tx_hash.size(), ed_pubkey, ed_privkey); - model_tx.signatures = {sign}; // Convert to protobuf iroha::model::converters::PbTransactionFactory factory; auto pb_tx = factory.serialize(model_tx); @@ -94,17 +44,4 @@ namespace iroha_cli { : NOT_VALID; } - void CliClient::create_account(std::string account_name, std::string pass_phrase) { - - auto seed = iroha::create_seed(pass_phrase); - auto key_pairs = iroha::create_keypair(seed); - - // Save pubkey to file - std::ofstream pub_file(account_name + ".pub"); - pub_file << key_pairs.pubkey.to_hexstring(); - // Save privkey to file - std::ofstream priv_file(account_name + ".priv"); - priv_file << key_pairs.privkey.to_hexstring(); - } - }; // namespace iroha_cli diff --git a/iroha-cli/client.hpp b/iroha-cli/client.hpp index e7d83bb48a..558967a811 100644 --- a/iroha-cli/client.hpp +++ b/iroha-cli/client.hpp @@ -18,27 +18,16 @@ #ifndef IROHA_CLIENT_HPP #define IROHA_CLIENT_HPP -#include -#include #include -#include "ametsuchi/block_serializer.hpp" -#include "common/types.hpp" #include "torii/command_client.hpp" - namespace iroha_cli { class CliClient { public: - enum Status { - WRONG_FORMAT, - NO_ACCOUNT, - NOT_VALID, - OK - }; - + enum Status { WRONG_FORMAT, NOT_VALID, OK }; - explicit CliClient(std::string target_ip, int port); + CliClient(std::string target_ip, int port); /** * Send transaction to Iroha-Network * @param json_tx @@ -46,8 +35,6 @@ namespace iroha_cli { */ Status sendTx(std::string json_tx); - static void create_account(std::string account_name, std::string pass_phrase); - private: torii::CommandSyncClient client_; }; diff --git a/iroha-cli/impl/keys_manager_impl.cpp b/iroha-cli/impl/keys_manager_impl.cpp new file mode 100644 index 0000000000..c98df7941b --- /dev/null +++ b/iroha-cli/impl/keys_manager_impl.cpp @@ -0,0 +1,70 @@ +/** + * 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 "keys_manager_impl.hpp" +#include +#include "common/types.hpp" + +namespace iroha_cli { + + KeysManagerImpl::KeysManagerImpl(std::string account_name) + : account_name_(account_name) {} + + nonstd::optional KeysManagerImpl::loadKeys() { + // Try to load from local file + std::ifstream priv_file(account_name_ + ".priv"); + std::ifstream pub_file(account_name_ + ".pub"); + if (not priv_file || not pub_file) { + return nonstd::nullopt; + } + std::string client_pub_key_; + std::string client_priv_key_; + priv_file >> client_priv_key_; + pub_file >> client_pub_key_; + + iroha::ed25519::keypair_t keypair; + auto privkey = iroha::hex2bytes(client_priv_key_); + std::copy(privkey.begin(), privkey.end(), keypair.privkey.begin()); + auto pubkey = iroha::hex2bytes(client_pub_key_); + std::copy(pubkey.begin(), pubkey.end(), keypair.pubkey.begin()); + return keypair; + } + + bool KeysManagerImpl::createKeys(std::string pass_phrase) { + auto seed = iroha::create_seed(pass_phrase); + auto key_pairs = iroha::create_keypair(seed); + std::ifstream pb_file(account_name_ + ".pub"); + std::ifstream pr_file(account_name_ + ".priv"); + if (pb_file && pr_file) { + return false; + } + // Save pubkey to file + std::ofstream pub_file(account_name_ + ".pub"); + if (not pub_file) { + return false; + } + pub_file << key_pairs.pubkey.to_hexstring(); + // Save privkey to file + std::ofstream priv_file(account_name_ + ".priv"); + if (not priv_file) { + return false; + } + priv_file << key_pairs.privkey.to_hexstring(); + return true; + } + +} // namespace iroha_cli diff --git a/iroha-cli/impl/keys_manager_impl.hpp b/iroha-cli/impl/keys_manager_impl.hpp new file mode 100644 index 0000000000..743b9c77cb --- /dev/null +++ b/iroha-cli/impl/keys_manager_impl.hpp @@ -0,0 +1,35 @@ +/** + * 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_CLI_KEYS_MANAGER_IMPL_HPP +#define IROHA_CLI_KEYS_MANAGER_IMPL_HPP +#include "../keys_manager.hpp" + +namespace iroha_cli { + class KeysManagerImpl : public KeysManager { + public: + KeysManagerImpl(std::string account_name); + + nonstd::optional loadKeys() override; + + bool createKeys(std::string pass_phrase) override; + + private: + std::string account_name_; + }; +} // namespace iroha_cli +#endif // IROHA_CLI_KEYS_MANAGER_IMPL_HPP diff --git a/iroha-cli/keys_manager.hpp b/iroha-cli/keys_manager.hpp new file mode 100644 index 0000000000..3e41e4cb46 --- /dev/null +++ b/iroha-cli/keys_manager.hpp @@ -0,0 +1,46 @@ +/** + * 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_CLI_KEYS_MANAGER_HPP +#define IROHA_CLI_KEYS_MANAGER_HPP + +#include +#include "crypto/crypto.hpp" + +namespace iroha_cli { + +class KeysManager { + public: + /** + * Load keys associated with account + * @param account_name + * @return nullopt if no keypair found locally + */ + virtual nonstd::optional loadKeys() = 0; + + /** + * Create keys and associate with account + * @param account_name + * @param pass_phrase + * @return false if create account failed + */ + virtual bool createKeys(std::string pass_phrase) = 0; + +}; + +} // namepsace iroha_cli +#endif // IROHA_CLI_KEYS_MANAGER_HPP diff --git a/iroha-cli/main.cpp b/iroha-cli/main.cpp index 37aa8d0e74..0534c4ce36 100644 --- a/iroha-cli/main.cpp +++ b/iroha-cli/main.cpp @@ -18,12 +18,13 @@ #include #include #include -#include "validators.hpp" #include "bootstrap_network.hpp" #include "common/assert_config.hpp" #include "genesis_block_client_impl.hpp" +#include "validators.hpp" #include "client.hpp" +#include "impl/keys_manager_impl.hpp" // ** Genesis Block and Provisioning ** // // Reference is here (TODO: move to doc): @@ -46,8 +47,6 @@ DEFINE_int32(torii_port, 50051, "Port of iroha's Torii"); DEFINE_validator(torii_port, &iroha_cli::validate_port); DEFINE_string(json_transaction, "", "Transaction in json format"); - - void create_account(std::string name); int main(int argc, char* argv[]) { @@ -56,13 +55,14 @@ int main(int argc, char* argv[]) { if (FLAGS_new_account) { // Create new pub/priv key - if (std::ifstream(FLAGS_name + ".pub")) { - assert_config::assert_fatal(false, "File already exists"); - } - iroha_cli::CliClient::create_account(FLAGS_name, FLAGS_pass_phrase); - std::cout << "Public and private key has been generated in current directory" - << std::endl; - + auto keysManager = iroha_cli::KeysManagerImpl(FLAGS_name); + if (not keysManager.createKeys(FLAGS_pass_phrase)) { + std::cout << "Keys already exist" << std::endl; + } else { + std::cout + << "Public and private key has been generated in current directory" + << std::endl; + }; } else if (not FLAGS_config.empty() && not FLAGS_genesis_block.empty()) { iroha_cli::GenesisBlockClientImpl genesis_block_client; auto bootstrap = iroha_cli::BootstrapNetwork(genesis_block_client); @@ -70,27 +70,25 @@ int main(int argc, char* argv[]) { auto block = bootstrap.parse_genesis_block(FLAGS_genesis_block); block = bootstrap.merge_tx_add_trusted_peers(block, peers); bootstrap.run_network(peers, block); - } else if (FLAGS_grpc) { + } else if (FLAGS_grpc) { std::cout << "Send transaction to " << FLAGS_address << ":" << FLAGS_torii_port << std::endl; - iroha_cli::CliClient client(FLAGS_address, FLAGS_torii_port); auto status = client.sendTx(FLAGS_json_transaction); - switch(status) { - case iroha_cli::CliClient::OK: std::cout << "Transaction successfully sent" << std::endl; - break; - case iroha_cli::CliClient::WRONG_FORMAT: std::cout << "Transaction wrong json format" << std::endl; + switch (status) { + case iroha_cli::CliClient::OK: + std::cout << "Transaction successfully sent" << std::endl; break; - case iroha_cli::CliClient::NO_ACCOUNT: std::cout << "No public and private key found. Run with new_account flag." << std::endl; + case iroha_cli::CliClient::WRONG_FORMAT: + std::cout << "Transaction wrong json format" << std::endl; break; - case iroha_cli::CliClient::NOT_VALID: std::cout << "Transaction is not valid." << std::endl; + case iroha_cli::CliClient::NOT_VALID: + std::cout << "Transaction is not valid." << std::endl; break; - } - + } } else { assert_config::assert_fatal(false, "Invalid flags"); } return 0; } - diff --git a/irohad/ametsuchi/impl/block_serializer.cpp b/irohad/ametsuchi/impl/block_serializer.cpp index 1ec60fd255..50a31593c0 100644 --- a/irohad/ametsuchi/impl/block_serializer.cpp +++ b/irohad/ametsuchi/impl/block_serializer.cpp @@ -177,7 +177,7 @@ namespace iroha { } void BlockSerializer::serialize_add_peer(PrettyWriter& writer, - const model::Command& command) { + const model::Command& command) { auto add_peer = static_cast(command); writer.StartObject(); @@ -195,8 +195,7 @@ namespace iroha { } void BlockSerializer::serialize_add_asset_quantity( - PrettyWriter& writer, - const model::Command& command) { + PrettyWriter& writer, const model::Command& command) { auto add_asset_quantity = static_cast(command); @@ -242,8 +241,7 @@ namespace iroha { } void BlockSerializer::serialize_assign_master_key( - PrettyWriter& writer, - const model::Command& command) { + PrettyWriter& writer, const model::Command& command) { auto assign_master_key = static_cast(command); @@ -262,8 +260,7 @@ namespace iroha { } void BlockSerializer::serialize_create_account( - PrettyWriter& writer, - const model::Command& command) { + PrettyWriter& writer, const model::Command& command) { auto create_account = static_cast(command); writer.StartObject(); @@ -320,8 +317,7 @@ namespace iroha { } void BlockSerializer::serialize_remove_signatory( - PrettyWriter& writer, - const model::Command& command) { + PrettyWriter& writer, const model::Command& command) { auto remove_signatory = static_cast(command); @@ -340,8 +336,7 @@ namespace iroha { } void BlockSerializer::serialize_set_account_permissions( - PrettyWriter& writer, - const model::Command& command) { + PrettyWriter& writer, const model::Command& command) { auto set_account_permissions = static_cast(command); @@ -410,8 +405,7 @@ namespace iroha { } void BlockSerializer::serialize_transfer_asset( - PrettyWriter& writer, - const model::Command& command) { + PrettyWriter& writer, const model::Command& command) { auto transfer_asset = static_cast(command); writer.StartObject(); @@ -449,8 +443,8 @@ namespace iroha { return nonstd::nullopt; } - auto req_fields = {"hash", "signatures", "created_ts", "height", - "prev_hash", "txs_number"}; + auto req_fields = {"hash", "signatures", "created_ts", + "height", "prev_hash", "txs_number"}; auto verify_member = [&doc](auto&& field) { return not doc.HasMember(field); }; @@ -501,7 +495,8 @@ namespace iroha { GenericValue>::Object& json_tx) { model::Transaction tx{}; - auto req_fields = {"creator_account_id", "tx_counter", "commands"}; + auto req_fields = {"creator_account_id", "tx_counter", "commands", + "signatures", "created_ts"}; auto verify_member = [&json_tx](auto&& field) { return not json_tx.HasMember(field); }; @@ -509,19 +504,15 @@ namespace iroha { return nonstd::nullopt; } - if (json_tx.HasMember("signatures")) { - deserialize(json_tx["signatures"].GetArray(), tx.signatures); - } - if (json_tx.HasMember("created_ts")) { - // created_ts - tx.created_ts = json_tx["created_ts"].GetUint64(); + if (not deserialize(json_tx["signatures"].GetArray(), tx.signatures)) { + return nonstd::nullopt; } + // Created ts + tx.created_ts = json_tx["created_ts"].GetUint64(); // creator_account_id tx.creator_account_id = json_tx["creator_account_id"].GetString(); - // tx_counter tx.tx_counter = json_tx["tx_counter"].GetUint64(); - // deserialize commands if (not deserialize(json_tx, tx.commands)) { return nonstd::nullopt; @@ -583,7 +574,7 @@ namespace iroha { std::vector>& commands) { auto json_commands = json_tx["commands"].GetArray(); - auto deserialize_command = [this, &commands](auto &&value) { + auto deserialize_command = [this, &commands](auto&& value) { auto json_command = value.GetObject(); if (not json_command.HasMember("command_type")) { return false; @@ -638,8 +629,7 @@ namespace iroha { return add_asset_quantity; } - std::shared_ptr - BlockSerializer::deserialize_add_signatory( + std::shared_ptr BlockSerializer::deserialize_add_signatory( GenericValue>::Object& json_command) { // TODO: make this function return nullopt when some field is missed auto add_signatory = std::make_shared(); @@ -669,8 +659,7 @@ namespace iroha { return assign_master_key; } - std::shared_ptr - BlockSerializer::deserialize_create_account( + std::shared_ptr BlockSerializer::deserialize_create_account( GenericValue>::Object& json_command) { // TODO: make this function return nullopt when some field is missed auto create_account = std::make_shared(); @@ -687,8 +676,7 @@ namespace iroha { return create_account; } - std::shared_ptr - BlockSerializer::deserialize_create_asset( + std::shared_ptr BlockSerializer::deserialize_create_asset( GenericValue>::Object& json_command) { // TODO: make this function return nullopt when some field is missed auto createAsset = std::make_shared(); @@ -705,8 +693,7 @@ namespace iroha { return createAsset; } - std::shared_ptr - BlockSerializer::deserialize_create_domain( + std::shared_ptr BlockSerializer::deserialize_create_domain( GenericValue>::Object& json_command) { // TODO: make this function return nullopt when some field is missed auto createDomain = std::make_shared(); @@ -781,8 +768,7 @@ namespace iroha { return setQuorum; } - std::shared_ptr - BlockSerializer::deserialize_transfer_asset( + std::shared_ptr BlockSerializer::deserialize_transfer_asset( GenericValue>::Object& json_command) { auto transferAsset = std::make_shared(); diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index 7814c56444..c79a92e8d4 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -77,35 +77,33 @@ class ClientTest : public testing::Test { }; TEST_F(ClientTest, SendTxWhenValid) { - std::string account_name = "test"; - iroha_cli::CliClient::create_account(account_name, ""); iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) .WillOnce(Return(true)); EXPECT_CALL(pcsMock, propagate_transaction(_)).Times(1); auto json_tx = - "{\n" - " \"creator_account_id\": \"test\", \n" - " \"tx_counter\": 0,\n" - " \"commands\":[{\n" - " \"command_type\": \"AddPeer\",\n" - " \"address\": \"localhost\",\n" - " \"peer_key\": " + "{\"signatures\": [ {\n" + " \"pubkey\": " + "\"2323232323232323232323232323232323232323232323232323232323232323\",\n" + " \"signature\": " + "\"2323232323232323232323232323232323232323232323232323232323232323232323" + "2323232323232323232323232323232323232323232323232323232323\"\n" + " }], \"created_ts\": 0,\n" + " \"creator_account_id\": \"123\",\n" + " \"tx_counter\": 0,\n" + " \"commands\": [{\n" + " \"command_type\": \"AddPeer\",\n" + " \"address\": \"localhost\",\n" + " \"peer_key\": " "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" - " }]\n" - "}"; + " }]}"; std::cout << "Sending json transaction to Iroha" << std::endl; auto status = client.sendTx(json_tx); ASSERT_EQ(status, iroha_cli::CliClient::OK); - // Remove private and public key - std::remove((account_name + ".pub").c_str()); - std::remove((account_name + ".priv").c_str()); } TEST_F(ClientTest, SendTxWhenInvalidJson) { - std::string account_name = "test"; - iroha_cli::CliClient::create_account(account_name, ""); iroha_cli::CliClient client(Ip, Port); // Must not call stateful validation EXPECT_CALL(svMock, validate(A())) @@ -123,54 +121,28 @@ TEST_F(ClientTest, SendTxWhenInvalidJson) { "}"; std::cout << "Sending json transaction to Iroha" << std::endl; ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::WRONG_FORMAT); - // Remove private and public key - std::remove((account_name + ".pub").c_str()); - std::remove((account_name + ".priv").c_str()); } TEST_F(ClientTest, SendTxWhenStatelessInvalid) { - std::string account_name = "test"; - iroha_cli::CliClient::create_account(account_name, ""); iroha_cli::CliClient client(Ip, Port); EXPECT_CALL(svMock, validate(A())) .WillOnce(Return(false)); - // Json with no Transaction auto json_tx = - "{\n" - " \"creator_account_id\": \"test\", \n" - " \"tx_counter\": 0,\n" - " \"commands\":[{\n" - " \"command_type\": \"AddPeer\",\n" - " \"address\": \"localhost\",\n" - " \"peer_key\": " + "{\"signatures\": [ {\n" + " \"pubkey\": " + "\"2423232323232323232323232323232323232323232323232323232323232323\",\n" + " \"signature\": " + "\"2323232323232323232323232323232323232323232323232323232323232323232323" + "2323232323232323232323232323232323232323232323232323232323\"\n" + " }], \"created_ts\": 0,\n" + " \"creator_account_id\": \"123\",\n" + " \"tx_counter\": 0,\n" + " \"commands\": [{\n" + " \"command_type\": \"AddPeer\",\n" + " \"address\": \"localhost\",\n" + " \"peer_key\": " "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" - " }]\n" - "}"; + " }]}"; std::cout << "Sending json transaction to Iroha" << std::endl; ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NOT_VALID); - // Remove private and public key - std::remove((account_name + ".pub").c_str()); - std::remove((account_name + ".priv").c_str()); -} - -TEST_F(ClientTest, SendTxWhenNoKeys) { - // Client without public, private keys - iroha_cli::CliClient client(Ip, Port); - // Must not call stateful validation - EXPECT_CALL(svMock, validate(A())) - .Times(0); - // Json with no Transaction - auto json_tx = - "{\n" - " \"creator_account_id\": \"test\", \n" - " \"tx_counter\": 0,\n" - " \"commands\":[{\n" - " \"command_type\": \"AddPeer\",\n" - " \"address\": \"localhost\",\n" - " \"peer_key\": " - "\"2323232323232323232323232323232323232323232323232323232323232323\"\n" - " }]\n" - "}"; - std::cout << "Sending json transaction to Iroha" << std::endl; - ASSERT_EQ(client.sendTx(json_tx), iroha_cli::CliClient::NO_ACCOUNT); } From 81e5925654cbc1e413ea0d17ba537208a0815e02 Mon Sep 17 00:00:00 2001 From: grimadas Date: Wed, 2 Aug 2017 15:26:16 +0300 Subject: [PATCH 10/12] Change crypto_provider constructor --- irohad/model/model_crypto_provider_impl.cpp | 5 ++--- irohad/model/model_crypto_provider_impl.hpp | 3 +-- test/libs/model/model_crypto_provider_test.cpp | 3 +-- .../stateless/transaction_validator_test.cpp | 13 ++++--------- 4 files changed, 8 insertions(+), 16 deletions(-) diff --git a/irohad/model/model_crypto_provider_impl.cpp b/irohad/model/model_crypto_provider_impl.cpp index b445d2d755..99b845f06c 100644 --- a/irohad/model/model_crypto_provider_impl.cpp +++ b/irohad/model/model_crypto_provider_impl.cpp @@ -21,9 +21,8 @@ namespace iroha { namespace model { - ModelCryptoProviderImpl::ModelCryptoProviderImpl(ed25519::privkey_t privkey, - ed25519::pubkey_t pubkey) - : privkey_(privkey), pubkey_(pubkey) {} + ModelCryptoProviderImpl::ModelCryptoProviderImpl(ed25519::keypair_t keypair) + : privkey_(keypair.privkey), pubkey_(keypair.pubkey) {} bool ModelCryptoProviderImpl::verify(const Transaction &tx) const { HashProviderImpl hash_provider; diff --git a/irohad/model/model_crypto_provider_impl.hpp b/irohad/model/model_crypto_provider_impl.hpp index 14602740d9..53b32dc0fc 100644 --- a/irohad/model/model_crypto_provider_impl.hpp +++ b/irohad/model/model_crypto_provider_impl.hpp @@ -26,8 +26,7 @@ namespace iroha { class ModelCryptoProviderImpl : public ModelCryptoProvider { public: - ModelCryptoProviderImpl(ed25519::privkey_t privkey, - ed25519::pubkey_t pubkey); + ModelCryptoProviderImpl(ed25519::keypair_t); bool verify(const Transaction &tx) const override; bool verify(std::shared_ptr tx) const override; diff --git a/test/libs/model/model_crypto_provider_test.cpp b/test/libs/model/model_crypto_provider_test.cpp index 4e56231fd9..5c68b0a6da 100644 --- a/test/libs/model/model_crypto_provider_test.cpp +++ b/test/libs/model/model_crypto_provider_test.cpp @@ -54,8 +54,7 @@ TEST(CryptoProvider, SignAndVerify) { auto model_tx = create_transaction(); - iroha::model::ModelCryptoProviderImpl crypto_provider(keypair.privkey, - keypair.pubkey); + iroha::model::ModelCryptoProviderImpl crypto_provider(keypair); sign(model_tx, keypair.privkey, keypair.pubkey); ASSERT_TRUE(crypto_provider.verify(model_tx)); diff --git a/test/module/irohad/validation/stateless/transaction_validator_test.cpp b/test/module/irohad/validation/stateless/transaction_validator_test.cpp index af816543e3..9bba73f886 100644 --- a/test/module/irohad/validation/stateless/transaction_validator_test.cpp +++ b/test/module/irohad/validation/stateless/transaction_validator_test.cpp @@ -55,9 +55,7 @@ iroha::model::Transaction create_transaction() { TEST(stateless_validation, stateless_validation_when_valid) { auto seed = iroha::create_seed(); auto keypair = iroha::create_keypair(seed); - - iroha::model::ModelCryptoProviderImpl crypto_provider(keypair.privkey, - keypair.pubkey); + iroha::model::ModelCryptoProviderImpl crypto_provider(keypair); iroha::validation::StatelessValidatorImpl transaction_validator( crypto_provider); @@ -71,8 +69,7 @@ TEST(stateless_validation, stateless_validation_when_invalid_wrong_signature) { auto seed = iroha::create_seed(); auto keypair = iroha::create_keypair(seed); - iroha::model::ModelCryptoProviderImpl crypto_provider(keypair.privkey, - keypair.pubkey); + iroha::model::ModelCryptoProviderImpl crypto_provider(keypair); iroha::validation::StatelessValidatorImpl transaction_validator( crypto_provider); @@ -89,8 +86,7 @@ TEST(stateless_validation, auto seed = iroha::create_seed(); auto keypair = iroha::create_keypair(seed); - iroha::model::ModelCryptoProviderImpl crypto_provider(keypair.privkey, - keypair.pubkey); + iroha::model::ModelCryptoProviderImpl crypto_provider(keypair); iroha::validation::StatelessValidatorImpl transaction_validator( crypto_provider); @@ -111,8 +107,7 @@ TEST(stateless_validation, auto seed = iroha::create_seed(); auto keypair = iroha::create_keypair(seed); - iroha::model::ModelCryptoProviderImpl crypto_provider(keypair.privkey, - keypair.pubkey); + iroha::model::ModelCryptoProviderImpl crypto_provider(keypair); iroha::validation::StatelessValidatorImpl transaction_validator( crypto_provider); From d802184544179dc8f2601996175a10b904f446e9 Mon Sep 17 00:00:00 2001 From: Fyodor Muratov Date: Wed, 2 Aug 2017 20:37:23 +0300 Subject: [PATCH 11/12] Fix codacy issues --- iroha-cli/client.cpp | 5 +++-- iroha-cli/impl/keys_manager_impl.cpp | 3 ++- iroha-cli/impl/keys_manager_impl.hpp | 2 +- irohad/model/model_crypto_provider_impl.hpp | 3 ++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/iroha-cli/client.cpp b/iroha-cli/client.cpp index 554c8dd6e0..73d5979b4b 100644 --- a/iroha-cli/client.cpp +++ b/iroha-cli/client.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include #include "client.hpp" #include "ametsuchi/block_serializer.hpp" #include "model/converters/pb_transaction_factory.hpp" @@ -22,11 +23,11 @@ namespace iroha_cli { CliClient::CliClient(std::string target_ip, int port) - : client_(target_ip, port) {} + : client_(std::move(target_ip), port) {} CliClient::Status CliClient::sendTx(std::string json_tx) { iroha::ametsuchi::BlockSerializer serializer; - auto tx_opt = serializer.deserialize(json_tx); + auto tx_opt = serializer.deserialize(std::move(json_tx)); if (not tx_opt.has_value()) { return WRONG_FORMAT; } diff --git a/iroha-cli/impl/keys_manager_impl.cpp b/iroha-cli/impl/keys_manager_impl.cpp index c98df7941b..077a3933f6 100644 --- a/iroha-cli/impl/keys_manager_impl.cpp +++ b/iroha-cli/impl/keys_manager_impl.cpp @@ -15,6 +15,7 @@ * limitations under the License. */ +#include #include "keys_manager_impl.hpp" #include #include "common/types.hpp" @@ -22,7 +23,7 @@ namespace iroha_cli { KeysManagerImpl::KeysManagerImpl(std::string account_name) - : account_name_(account_name) {} + : account_name_(std::move(account_name)) {} nonstd::optional KeysManagerImpl::loadKeys() { // Try to load from local file diff --git a/iroha-cli/impl/keys_manager_impl.hpp b/iroha-cli/impl/keys_manager_impl.hpp index 743b9c77cb..ed5806bfb9 100644 --- a/iroha-cli/impl/keys_manager_impl.hpp +++ b/iroha-cli/impl/keys_manager_impl.hpp @@ -22,7 +22,7 @@ namespace iroha_cli { class KeysManagerImpl : public KeysManager { public: - KeysManagerImpl(std::string account_name); + explicit KeysManagerImpl(std::string account_name); nonstd::optional loadKeys() override; diff --git a/irohad/model/model_crypto_provider_impl.hpp b/irohad/model/model_crypto_provider_impl.hpp index 53b32dc0fc..c211d8bb6b 100644 --- a/irohad/model/model_crypto_provider_impl.hpp +++ b/irohad/model/model_crypto_provider_impl.hpp @@ -26,9 +26,10 @@ namespace iroha { class ModelCryptoProviderImpl : public ModelCryptoProvider { public: - ModelCryptoProviderImpl(ed25519::keypair_t); + explicit ModelCryptoProviderImpl(ed25519::keypair_t); bool verify(const Transaction &tx) const override; + bool verify(std::shared_ptr tx) const override; bool verify(const Block& block) const override; From d7b4f7ca46b34a212fe55ad454759fa11a45545f Mon Sep 17 00:00:00 2001 From: Fyodor Muratov Date: Wed, 2 Aug 2017 21:36:32 +0300 Subject: [PATCH 12/12] Fix rebase conflict: - rename SvMock Fix broken include paths --- test/module/iroha-cli/CMakeLists.txt | 1 + test/module/iroha-cli/client_test.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test/module/iroha-cli/CMakeLists.txt b/test/module/iroha-cli/CMakeLists.txt index 98d40e4669..31ce5c2e0e 100644 --- a/test/module/iroha-cli/CMakeLists.txt +++ b/test/module/iroha-cli/CMakeLists.txt @@ -11,3 +11,4 @@ target_link_libraries(client_test processors server_runner ) +target_include_directories(client_test PUBLIC ${PROJECT_SOURCE_DIR}/iroha-cli) diff --git a/test/module/iroha-cli/client_test.cpp b/test/module/iroha-cli/client_test.cpp index c79a92e8d4..49b241df9e 100644 --- a/test/module/iroha-cli/client_test.cpp +++ b/test/module/iroha-cli/client_test.cpp @@ -15,11 +15,11 @@ * limitations under the License. */ -#include "../../../iroha-cli/client.hpp" -#include #include +#include +#include "client.hpp" -#include "../irohad/torii/mock_classes.hpp" +#include "module/irohad/torii/mock_classes.hpp" #include "main/server_runner.hpp" #include "torii/processor/query_processor_impl.hpp" #include "torii/processor/transaction_processor_impl.hpp" @@ -71,7 +71,7 @@ class ClientTest : public testing::Test { std::unique_ptr runner; std::thread th; PCSMock pcsMock; - SVMock svMock; + StatelessValidatorMock svMock; WsvQueryMock wsv_query; BlockQueryMock block_query; };