LCOV - code coverage report
Current view: top level - irohad/model/converters/impl - json_block_factory.cpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 41 57 71.9 %
Date: 2019-03-07 14:46:43 Functions: 5 8 62.5 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #include "model/converters/json_block_factory.hpp"
       7             : 
       8             : using namespace rapidjson;
       9             : 
      10             : namespace iroha {
      11             :   namespace model {
      12             :     namespace converters {
      13             : 
      14             :       JsonBlockFactory::JsonBlockFactory(logger::LoggerPtr log)
      15           2 :           : log_{std::move(log)} {}
      16             : 
      17             :       Document JsonBlockFactory::serialize(const Block &block) {
      18           2 :         Document document;
      19           2 :         auto &allocator = document.GetAllocator();
      20           2 :         document.SetObject();
      21             : 
      22           2 :         Value signatures;
      23           2 :         signatures.SetArray();
      24           2 :         for (const auto &signature : block.sigs) {
      25           0 :           signatures.PushBack(serializeSignature(signature, allocator),
      26           0 :                               allocator);
      27             :         }
      28           2 :         document.AddMember("signatures", signatures, allocator);
      29             : 
      30           2 :         document.AddMember("created_ts", block.created_ts, allocator);
      31           2 :         document.AddMember("hash", block.hash.to_hexstring(), allocator);
      32           2 :         document.AddMember(
      33           2 :             "prev_hash", block.prev_hash.to_hexstring(), allocator);
      34           2 :         document.AddMember("height", block.height, allocator);
      35           2 :         document.AddMember("txs_number", block.txs_number, allocator);
      36             : 
      37           2 :         Value commands;
      38           2 :         commands.SetArray();
      39           2 :         for (auto &&transaction : block.transactions) {
      40           0 :           commands.PushBack(
      41           0 :               Document(&allocator)
      42           0 :                   .CopyFrom(factory_.serialize(transaction), allocator),
      43           0 :               allocator);
      44             :         }
      45           2 :         document.AddMember("transactions", commands, allocator);
      46             : 
      47           2 :         Value rejected_transactions;
      48           2 :         rejected_transactions.SetArray();
      49           2 :         for (auto &&hash : block.rejected_transactions_hashes) {
      50           0 :           Value serialized_hash;
      51           0 :           serialized_hash.SetString(hash.to_hexstring().c_str(), allocator);
      52           0 :           rejected_transactions.PushBack(serialized_hash, allocator);
      53           0 :         }
      54           2 :         document.AddMember(
      55           2 :             "rejected_transactions", rejected_transactions, allocator);
      56             : 
      57           2 :         return document;
      58           2 :       }
      59             : 
      60             :       boost::optional<Block> JsonBlockFactory::deserialize(
      61             :           const Document &document) {
      62           2 :         auto des = makeFieldDeserializer(document);
      63             :         auto des_transactions = [this](auto array) {
      64             :           auto acc_transactions = [this](auto init, auto &x) {
      65             :             return init | [this, &x](auto transactions) {
      66           0 :               return factory_.deserialize(x) |
      67             :                   [&transactions](auto transaction) {
      68           0 :                     transactions.push_back(transaction);
      69           0 :                     return boost::make_optional(std::move(transactions));
      70             :                   };
      71           0 :             };
      72             :           };
      73           1 :           return std::accumulate(
      74           1 :               array.begin(),
      75           1 :               array.end(),
      76           1 :               boost::make_optional(Block::TransactionsType()),
      77           1 :               acc_transactions);
      78           0 :         };
      79           2 :         return boost::make_optional(model::Block())
      80           2 :             | des.Uint64(&Block::created_ts, "created_ts")
      81           2 :             | des.Uint64(&Block::height, "height")
      82           2 :             | des.Uint(&Block::txs_number, "txs_number")
      83           2 :             | des.String(&Block::hash, "hash")
      84           2 :             | des.String(&Block::prev_hash, "prev_hash")
      85           2 :             | des.Array(&Block::sigs, "signatures")
      86           2 :             | des.Array(&Block::transactions, "transactions", des_transactions)
      87           2 :             | des.Array(&Block::rejected_transactions_hashes,
      88           2 :                         "rejected_transactions");
      89           0 :       }
      90             : 
      91             :     }  // namespace converters
      92             :   }    // namespace model
      93             : }  // namespace iroha

Generated by: LCOV version 1.13