Skip to content

Commit

Permalink
Deserialize prev_hash of the block
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilsa authored and lebdron committed Jul 18, 2017
1 parent c4b0eb1 commit b700329
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
19 changes: 13 additions & 6 deletions irohad/ametsuchi/impl/block_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,21 @@ namespace iroha {

// TODO: return nullopt when some necessary field is missed
std::string block_json(bytes.begin(), bytes.end());
rapidjson::Document d;
if (d.Parse(block_json.c_str()).HasParseError()){
rapidjson::Document doc;
if (doc.Parse(block_json.c_str()).HasParseError()){
return nonstd::nullopt;
}

model::Block block{};

// hash
d["hash"].GetString();
std::string hash_str(d["hash"].GetString(), d["hash"].GetStringLength());
doc["hash"].GetString();
std::string hash_str(doc["hash"].GetString(), doc["hash"].GetStringLength());
auto hash_bytes = hex2bytes(hash_str);
std::copy(hash_bytes.begin(), hash_bytes.end(), block.hash.begin());

//signatures
auto json_sigs = d["signatures"].GetArray();
auto json_sigs = doc["signatures"].GetArray();

for (auto iter = json_sigs.begin(); iter < json_sigs.end(); ++iter){
auto json_sig = iter->GetObject();
Expand All @@ -438,9 +438,16 @@ namespace iroha {
}

// created_ts
block.created_ts = d["created_ts"].GetUint64();
block.created_ts = doc["created_ts"].GetUint64();

//height
block.height = doc["height"].GetUint64();

// hash
doc["prev_hash"].GetString();
std::string prev_hash_str(doc["prev_hash"].GetString(), doc["prev_hash"].GetStringLength());
auto prev_hash_bytes = hex2bytes(prev_hash_str);
std::copy(prev_hash_bytes.begin(), prev_hash_bytes.end(), block.prev_hash.begin());

return block;
}
Expand Down
19 changes: 3 additions & 16 deletions test/module/irohad/ametsuchi/block_serializer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@

#include <gtest/gtest.h>
#include <ametsuchi/block_serializer.hpp>
#include <rapidjson/document.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>
#include <rapidjson/prettywriter.h>
#include <model/commands/add_peer.hpp>
#include <model/commands/add_asset_quantity.hpp>
#include <model/commands/add_signatory.hpp>
#include <model/commands/assign_master_key.hpp>
#include <model/commands/create_account.hpp>
#include <model/commands/create_asset.hpp>
#include <model/commands/create_domain.hpp>
#include <model/commands/remove_signatory.hpp>
#include <model/commands/set_permissions.hpp>
#include <model/commands/set_quorum.hpp>
#include <model/commands/transfer_asset.hpp>

iroha::model::Signature create_signature();
iroha::model::Transaction create_transaction();
Expand Down Expand Up @@ -144,7 +129,7 @@ iroha::model::Block create_block() {
block.sigs.push_back(create_signature());
block.created_ts = 0;
block.height = 0;
memset(block.prev_hash.data(), 0x123, iroha::ed25519::pubkey_t::size());
memset(block.prev_hash.data(), 0x5, iroha::ed25519::pubkey_t::size());
block.txs_number = 0;
memset(block.merkle_root.data(), 0x123, iroha::ed25519::pubkey_t::size());
block.transactions.push_back(create_transaction());
Expand Down Expand Up @@ -176,5 +161,7 @@ TEST(block_serialize, block_serialize_test){
ASSERT_EQ(block.sigs[i].signature, deserialized.sigs[i].signature);
ASSERT_EQ(block.sigs[i].pubkey, deserialized.sigs[i].pubkey);
}

ASSERT_EQ(block.prev_hash, deserialized.prev_hash);
}
}

0 comments on commit b700329

Please sign in to comment.