BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
block.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cryptonomex, Inc., and contributors.
3  *
4  * The MIT License
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include <boost/endian/conversion.hpp>
27 #include <fc/io/raw.hpp>
28 #include <algorithm>
29 
30 namespace graphene { namespace protocol {
32  {
33  return digest_type::hash(*this);
34  }
35 
37  {
38  return boost::endian::endian_reverse(id._hash[0].value());
39  }
40 
42  {
43  if( 0 == _block_id._hash[0].value() )
44  {
45  auto tmp = fc::sha224::hash( *this );
46  tmp._hash[0] = boost::endian::endian_reverse(block_num()); // store the block num in the ID, 160 bits is plenty for the hash
47  static_assert( sizeof(tmp._hash[0]) == 4, "should be 4 bytes" );
48  memcpy(_block_id._hash, tmp._hash, std::min(sizeof(_block_id), sizeof(tmp)));
49  }
50  return _block_id;
51  }
52 
54  {
55  if( !_signee.valid() )
56  _signee = fc::ecc::public_key( witness_signature, digest(), true/*enforce canonical*/ );
57  return _signee;
58  }
59 
61  {
62  witness_signature = signer.sign_compact( digest() );
63  }
64 
65  bool signed_block_header::validate_signee( const fc::ecc::public_key& expected_signee )const
66  {
67  return signee() == expected_signee;
68  }
69 
71  {
72  static const checksum_type empty_checksum;
73  if( transactions.size() == 0 )
74  return empty_checksum;
75 
76  if( 0 == _calculated_merkle_root._hash[0].value() )
77  {
78  vector<digest_type> ids;
79  ids.resize( transactions.size() );
80  for( uint32_t i = 0; i < transactions.size(); ++i )
81  ids[i] = transactions[i].merkle_digest();
82 
83  vector<digest_type>::size_type current_number_of_hashes = ids.size();
84  while( current_number_of_hashes > 1 )
85  {
86  // hash ID's in pairs
87  uint32_t i_max = current_number_of_hashes - (current_number_of_hashes&1);
88  uint32_t k = 0;
89 
90  for( uint32_t i = 0; i < i_max; i += 2 )
91  ids[k++] = digest_type::hash( std::make_pair( ids[i], ids[i+1] ) );
92 
93  if( current_number_of_hashes&1 )
94  ids[k++] = ids[i_max];
95  current_number_of_hashes = k;
96  }
97  _calculated_merkle_root = checksum_type::hash( ids[0] );
98  }
99  return _calculated_merkle_root;
100  }
101 } }
102 
void sign(const fc::ecc::private_key &signer)
Definition: block.cpp:60
static uint32_t num_from_id(const block_id_type &id)
Definition: block.cpp:36
Definition: api.cpp:48
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
static sha224 hash(const char *d, uint32_t dlen)
Definition: sha224.cpp:34
compact_signature sign_compact(const fc::sha256 &digest, bool require_canonical=true) const
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
uint32_t block_num() const
Definition: block.hpp:34
const checksum_type & calculate_merkle_root() const
Definition: block.cpp:70
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
const block_id_type & id() const
Definition: block.cpp:41
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:86
const fc::ecc::public_key & signee() const
Definition: block.cpp:53
bool validate_signee(const fc::ecc::public_key &expected_signee) const
Definition: block.cpp:65
an elliptic curve private key.
Definition: elliptic.hpp:89
digest_type digest() const
Definition: block.cpp:31