Scorum
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
block.cpp
Go to the documentation of this file.
2 #include <fc/io/raw.hpp>
3 #include <fc/bitutil.hpp>
4 #include <algorithm>
5 #include <sstream>
6 
7 namespace scorum {
8 namespace protocol {
10 {
11  return digest_type::hash(*this);
12 }
13 
15 {
16  return fc::endian_reverse_u32(id._hash[0]);
17 }
18 
20 {
21  auto tmp = fc::sha224::hash(*this);
22  tmp._hash[0]
23  = fc::endian_reverse_u32(block_num()); // store the block num in the ID, 160 bits is plenty for the hash
24  static_assert(sizeof(tmp._hash[0]) == 4, "should be 4 bytes");
26  memcpy(result._hash, tmp._hash, std::min(sizeof(result), sizeof(tmp)));
27  return result;
28 }
29 
30 fc::ecc::public_key signed_block_header::signee() const
31 {
32  return fc::ecc::public_key(witness_signature, digest(), true /*enforce canonical*/);
33 }
34 
35 void signed_block_header::sign(const fc::ecc::private_key& signer)
36 {
37  witness_signature = signer.sign_compact(digest());
38 }
39 
40 bool signed_block_header::validate_signee(const fc::ecc::public_key& expected_signee) const
41 {
42  return signee() == expected_signee;
43 }
44 
46 {
47  if (transactions.size() == 0)
48  return checksum_type();
49 
50  std::vector<digest_type> ids;
51  ids.resize(transactions.size());
52  for (uint32_t i = 0; i < transactions.size(); ++i)
53  ids[i] = transactions[i].merkle_digest();
54 
55  std::vector<digest_type>::size_type current_number_of_hashes = ids.size();
56  while (current_number_of_hashes > 1)
57  {
58  // hash ID's in pairs
59  uint32_t i_max = current_number_of_hashes - (current_number_of_hashes & 1);
60  uint32_t k = 0;
61 
62  for (uint32_t i = 0; i < i_max; i += 2)
63  ids[k++] = digest_type::hash(std::make_pair(ids[i], ids[i + 1]));
64 
65  if (current_number_of_hashes & 1)
66  ids[k++] = ids[i_max];
67  current_number_of_hashes = k;
68  }
69  return checksum_type::hash(ids[0]);
70 }
71 }
72 
73 block_info::block_info(uint32_t block_num, std::string block_id, fc::time_point_sec when, std::string block_witness)
74  : _block_num(block_num)
75  , _block_id(block_id)
76  , _when(when)
77  , _block_witness(block_witness)
78 {
79 }
80 
82  : _block_num(block.block_num())
83  , _block_id(block.id().str())
84  , _when(block.timestamp)
85  , _block_witness(block.witness)
86 {
87 }
88 
89 block_info::block_info(const fc::time_point_sec& when, const std::string& witness_owner)
90  : _when(when)
91  , _block_witness(witness_owner)
92 {
93 }
94 
95 block_info::operator std::string() const
96 {
97  std::stringstream store;
98  store << _block_num << ":" << _block_id << "|";
99  store << _when.to_iso_string() << "~" << _block_witness;
100  return store.str();
101 }
102 } // scorum::protocol
fc::ripemd160 checksum_type
Definition: types.hpp:64
fc::sha256 digest_type
Definition: types.hpp:66
fc::ripemd160 block_id_type
Definition: types.hpp:63
Definition: asset.cpp:15
static uint32_t num_from_id(const block_id_type &id)
Definition: block.cpp:14
digest_type digest() const
Definition: block.cpp:9
fc::ecc::public_key signee() const
Definition: block.cpp:30
void sign(const fc::ecc::private_key &signer)
Definition: block.cpp:35
bool validate_signee(const fc::ecc::public_key &expected_signee) const
Definition: block.cpp:40
block_id_type id() const
Definition: block.cpp:19
std::vector< signed_transaction > transactions
Definition: block.hpp:13
checksum_type calculate_merkle_root() const
Definition: block.cpp:45