Skip to content

Commit

Permalink
Merge branch 'feature/json-factory' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lebdron committed Aug 6, 2017
2 parents c6bab88 + 0698b2f commit 552d28a
Show file tree
Hide file tree
Showing 29 changed files with 1,539 additions and 1,444 deletions.
76 changes: 11 additions & 65 deletions iroha-cli/bootstrap_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <iostream>
#include <string>
#include <vector>
#include "ametsuchi/block_serializer.hpp"
#include "model/commands/add_peer.hpp"
#include "common/assert_config.hpp"
#include "common/types.hpp"
#include "ip_tools/ip_tools.hpp"
Expand All @@ -35,63 +35,6 @@ using namespace assert_config;

namespace iroha_cli {

void validate_command(rapidjson::Value &json_val_cmd) {
auto json_cmd = json_val_cmd.GetObject();
const char *MemberCommandType = "command_type";
assert_fatal(json_cmd.HasMember(MemberCommandType),
no_member_error(MemberCommandType));

// TODO: validate command.
}

void validate_transactions(rapidjson::Document &doc) {
const char *MemberTxs = "transactions";
assert_fatal(doc.HasMember(MemberTxs), no_member_error(MemberTxs));
assert_fatal(doc[MemberTxs].IsArray(), type_error(MemberTxs, "array"));
auto json_txs = doc[MemberTxs].GetArray();

for (auto json_tx_iter = json_txs.begin(); json_tx_iter != json_txs.end();
++json_tx_iter) {
auto json_tx = json_tx_iter->GetObject();
const char *MemberTxSigs = "signatures";
assert_fatal(json_tx.HasMember(MemberTxSigs),
no_member_error(MemberTxSigs));

auto json_sigs = json_tx[MemberTxSigs].GetArray();
for (auto json_sigs_iter = json_sigs.begin();
json_sigs_iter != json_sigs.end(); ++json_sigs_iter) {
assert_fatal(json_sigs_iter->GetObject().HasMember("pubkey"),
no_member_error("pubkey"));
assert_fatal(json_sigs_iter->GetObject().HasMember("signature"),
no_member_error("signature"));
}

const char *MemberTxCreatedTs = "created_ts";
// FIXME: Should iroha decide default value?
assert_fatal(json_tx.HasMember(MemberTxCreatedTs),
no_member_error(MemberTxCreatedTs));

const char *MemberTxAccountId = "creator_account_id";
assert_fatal(json_tx.HasMember(MemberTxAccountId),
no_member_error(MemberTxAccountId));

const char *MemberTxCounter = "tx_counter";
// FIXME: Should iroha decide default value?
assert_fatal(json_tx.HasMember(MemberTxCounter),
no_member_error(MemberTxCounter));

const char *MemberTxCommands = "commands";
assert_fatal(json_tx.HasMember(MemberTxCommands),
no_member_error(MemberTxCommands));

auto json_commands = json_tx[MemberTxCommands].GetArray();
for (auto iter = json_commands.begin(); iter != json_commands.end();
++iter) {
validate_command(*iter);
}
}
}

/**
* parse trusted peers in `target.conf`
* @param target_conf_path
Expand Down Expand Up @@ -160,16 +103,19 @@ namespace iroha_cli {
assert_fatal(not doc.HasParseError(), parse_error(genesis_json_path));
assert_fatal(doc.IsObject(), type_error("JSON", "object"));

validate_transactions(doc);

// parse transactions
auto block_serializer = iroha::ametsuchi::BlockSerializer();
std::vector<iroha::model::Transaction> txs;

try {
block_serializer.deserialize(doc, txs);
} catch (...) {
assert_fatal(false, "Failed to parse command");
auto& transactions = doc["transactions"];
for (auto it = transactions.Begin(); it != transactions.End(); ++it) {
rapidjson::Document transaction_document;
auto &allocator = transaction_document.GetAllocator();
transaction_document.CopyFrom(*it, allocator);
auto transaction = factory_.deserialize(transaction_document);
if (not transaction) {
// TODO log transaction parsing failure
}
txs.emplace_back(transaction.value());
}

// create block
Expand Down
2 changes: 2 additions & 0 deletions iroha-cli/bootstrap_network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <string>
#include <vector>
#include "genesis_block_client.hpp"
#include "model/converters/json_transaction_factory.hpp"
#include "model/block.hpp"
#include "model/peer.hpp"

Expand Down Expand Up @@ -74,6 +75,7 @@ namespace iroha_cli {

private:
iroha_cli::GenesisBlockClient& client_;
iroha::model::converters::JsonTransactionFactory factory_;
};

} // namespace iroha_cli
Expand Down
13 changes: 9 additions & 4 deletions iroha-cli/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
* limitations under the License.
*/

#include <utility>
#include "client.hpp"
#include "ametsuchi/block_serializer.hpp"
#include <utility>
#include "model/converters/json_common.hpp"
#include "model/converters/json_transaction_factory.hpp"
#include "model/converters/pb_transaction_factory.hpp"

namespace iroha_cli {
Expand All @@ -26,8 +27,12 @@ namespace iroha_cli {
: client_(std::move(target_ip), port) {}

CliClient::Status CliClient::sendTx(std::string json_tx) {
iroha::ametsuchi::BlockSerializer serializer;
auto tx_opt = serializer.deserialize(std::move(json_tx));
iroha::model::converters::JsonTransactionFactory serializer;
auto doc = iroha::model::converters::stringToJson(std::move(json_tx));
if (not doc.has_value()) {
return WRONG_FORMAT;
}
auto tx_opt = serializer.deserialize(doc.value());
if (not tx_opt.has_value()) {
return WRONG_FORMAT;
}
Expand Down
5 changes: 2 additions & 3 deletions irohad/ametsuchi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ add_library(ametsuchi

impl/postgres_wsv_query.cpp
impl/postgres_wsv_command.cpp
impl/block_serializer.cpp

impl/peer_query_wsv.cpp
)
Expand All @@ -18,5 +17,5 @@ target_link_libraries(ametsuchi
optional
pqxx
cpp_redis
rapidjson
)
model
)
174 changes: 0 additions & 174 deletions irohad/ametsuchi/block_serializer.hpp

This file was deleted.

Loading

0 comments on commit 552d28a

Please sign in to comment.