Skip to content

Commit

Permalink
Deserialize hash, sigs, created_ts 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 1cdadd4 commit c4b0eb1
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
54 changes: 42 additions & 12 deletions irohad/ametsuchi/impl/block_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace iroha {
const model::Block& block) {
writer.StartObject();
writer.String("hash");
writer.String(block.hash.to_string().c_str());
writer.String(block.hash.to_hexstring().c_str());

writer.String("signatures");
writer.StartArray();
Expand All @@ -58,13 +58,13 @@ namespace iroha {
writer.Uint64(block.height);

writer.String("prev_hash");
writer.String(block.prev_hash.to_string().c_str());
writer.String(block.prev_hash.to_hexstring().c_str());

writer.String("txs_number");
writer.Uint(block.txs_number);

writer.String("merkle_root");
writer.String(block.merkle_root.to_string().c_str());
writer.String(block.merkle_root.to_hexstring().c_str());

writer.String("transactions");
writer.StartArray();
Expand All @@ -81,10 +81,10 @@ namespace iroha {
writer.StartObject();

writer.String("pubkey");
writer.String(signature.pubkey.to_string().c_str());
writer.String(signature.pubkey.to_hexstring().c_str());

writer.String("signature");
writer.String(signature.signature.to_string().c_str());
writer.String(signature.signature.to_hexstring().c_str());

writer.EndObject();
}
Expand Down Expand Up @@ -182,7 +182,7 @@ namespace iroha {
writer.String(add_peer.address.c_str());

writer.String("peer_key");
writer.String(add_peer.peer_key.to_string().c_str());
writer.String(add_peer.peer_key.to_hexstring().c_str());

writer.EndObject();
}
Expand Down Expand Up @@ -220,7 +220,7 @@ namespace iroha {
writer.String(add_signatory.account_id.c_str());

writer.String("pubkey");
writer.String(add_signatory.pubkey.to_string().c_str());
writer.String(add_signatory.pubkey.to_hexstring().c_str());

writer.EndObject();
}
Expand All @@ -237,7 +237,7 @@ namespace iroha {
writer.String(assign_master_key.account_id.c_str());

writer.String("pubkey");
writer.String(assign_master_key.pubkey.to_string().c_str());
writer.String(assign_master_key.pubkey.to_hexstring().c_str());

writer.EndObject();
}
Expand All @@ -257,7 +257,7 @@ namespace iroha {
writer.String(create_account.account_name.c_str());

writer.String("pubkey");
writer.String(create_account.pubkey.to_string().c_str());
writer.String(create_account.pubkey.to_hexstring().c_str());

writer.EndObject();
}
Expand Down Expand Up @@ -306,7 +306,7 @@ namespace iroha {
writer.String(remove_signatory.account_id.c_str());

writer.String("pubkey");
writer.String(remove_signatory.pubkey.to_string().c_str());
writer.String(remove_signatory.pubkey.to_hexstring().c_str());

writer.EndObject();
}
Expand Down Expand Up @@ -403,15 +403,45 @@ namespace iroha {

nonstd::optional<model::Block> BlockSerializer::deserialize(
const std::vector<uint8_t>& bytes) {

// 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()){
return nonstd::nullopt;
}

model::Block block{};
// auto hash_bytes = base64_decode(hash_str);
// std::copy_n(std::make_move_iterator(hash_bytes.begin()), hash_bytes.size(), block.hash.begin());

// hash
d["hash"].GetString();
std::string hash_str(d["hash"].GetString(), d["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();

for (auto iter = json_sigs.begin(); iter < json_sigs.end(); ++iter){
auto json_sig = iter->GetObject();
model::Signature signature{};

std::string sig_pubkey(json_sig["pubkey"].GetString(), json_sig["pubkey"].GetStringLength());
auto sig_pubkey_bytes = hex2bytes(sig_pubkey);
std::copy(sig_pubkey_bytes.begin(), sig_pubkey_bytes.end(), signature.pubkey.begin());

std::string sig_sign(json_sig["signature"].GetString(), json_sig["signature"].GetStringLength());
auto sig_sign_bytes = hex2bytes(sig_sign);
std::copy(sig_sign_bytes.begin(), sig_sign_bytes.end(), signature.signature.begin());

block.sigs.push_back(signature);
}

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



return block;
}
}
Expand Down
9 changes: 9 additions & 0 deletions libs/common/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,16 @@ namespace iroha {
}
};

static std::vector<uint8_t> hex2bytes(const std::string& hex) {
std::vector<uint8_t> bytes;

for (size_t i = 0; i < hex.length(); i += 2) {
std::string byteString = hex.substr(i, 2);
uint8_t byte = (uint8_t)strtol(byteString.c_str(), NULL, 16);
bytes.push_back(byte);
}
return bytes;
}

template <size_t size>
using hash_t = blob_t<size>;
Expand Down
21 changes: 15 additions & 6 deletions test/module/irohad/ametsuchi/block_serializer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ iroha::model::Proposal create_proposal() {

iroha::model::Block create_block() {
iroha::model::Block block{};
memset(block.hash.data(), 0x123, iroha::ed25519::pubkey_t::size());
memset(block.hash.data(), 0x1, iroha::ed25519::pubkey_t::size());
block.sigs.push_back(create_signature());
block.created_ts = 0;
block.height = 0;
Expand All @@ -158,14 +158,23 @@ TEST(block_serialize, block_serialize_test){

iroha::ametsuchi::BlockSerializer blockSerializer;

std::cout << unsigned(block.hash[0]) << std::endl;

auto bytes = blockSerializer.serialize(block);
std::string str(bytes.begin(), bytes.end());
// std::cout << str << std::endl;
std::cout << str << std::endl;
// deserialize

auto res = blockSerializer.deserialize(bytes);
// if (res){
// auto deserialized = res.value();
// ASSERT_EQ(block.hash, deserialized.hash);
// }
if (res){
auto deserialized = res.value();
ASSERT_EQ(block.hash, deserialized.hash);
ASSERT_EQ(block.created_ts, deserialized.created_ts);

ASSERT_TRUE(block.sigs.size() > 0);
for (int i = 0; i < block.sigs.size(); i++){
ASSERT_EQ(block.sigs[i].signature, deserialized.sigs[i].signature);
ASSERT_EQ(block.sigs[i].pubkey, deserialized.sigs[i].pubkey);
}
}
}

0 comments on commit c4b0eb1

Please sign in to comment.