BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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  {
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  }
98  }
100  }
101 } }
102 
graphene::protocol::signed_block::calculate_merkle_root
const checksum_type & calculate_merkle_root() const
Definition: block.cpp:70
graphene::protocol::signed_block_header::_signee
fc::ecc::public_key _signee
Definition: block.hpp:60
block.hpp
fee_schedule.hpp
fc::sha256
Definition: sha256.hpp:10
fc::ecc::public_key::valid
bool valid() const
Definition: elliptic_secp256k1.cpp:97
fc::ecc::public_key
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION
#define GRAPHENE_IMPLEMENT_EXTERNAL_SERIALIZATION(type)
Definition: types.hpp:86
fc::ripemd160::_hash
boost::endian::little_uint32_buf_t _hash[5]
Definition: ripemd160.hpp:71
graphene::protocol::signed_block::_calculated_merkle_root
checksum_type _calculated_merkle_root
Definition: block.hpp:70
graphene::protocol::block_header::block_num
uint32_t block_num() const
Definition: block.hpp:34
fc::ecc::private_key
an elliptic curve private key.
Definition: elliptic.hpp:89
graphene::protocol::signed_block_header::signee
const fc::ecc::public_key & signee() const
Definition: block.cpp:53
graphene::protocol::signed_block_header::_block_id
block_id_type _block_id
Definition: block.hpp:61
fc::ecc::private_key::sign_compact
compact_signature sign_compact(const fc::sha256 &digest, bool require_canonical=true) const
Definition: elliptic_impl_priv.cpp:89
graphene::protocol::block_header::digest
digest_type digest() const
Definition: block.cpp:31
graphene::protocol::signed_block::transactions
vector< processed_transaction > transactions
Definition: block.hpp:68
fc::ripemd160
Definition: ripemd160.hpp:11
graphene::protocol::signed_block_header::witness_signature
signature_type witness_signature
Definition: block.hpp:55
graphene::protocol::block_header::num_from_id
static uint32_t num_from_id(const block_id_type &id)
Definition: block.cpp:36
graphene::protocol::signed_block_header::validate_signee
bool validate_signee(const fc::ecc::public_key &expected_signee) const
Definition: block.cpp:65
graphene::protocol::signed_block_header
Definition: block.hpp:47
fc::ripemd160::hash
static ripemd160 hash(const fc::sha512 &h)
Definition: ripemd160.cpp:43
graphene::protocol::signed_block_header::sign
void sign(const fc::ecc::private_key &signer)
Definition: block.cpp:60
graphene::protocol::signed_block_header::id
const block_id_type & id() const
Definition: block.cpp:41
fc::sha224::hash
static sha224 hash(const char *d, uint32_t dlen)
Definition: sha224.cpp:34
graphene::protocol::signed_block
Definition: block.hpp:64
fc::sha256::hash
static sha256 hash(const char *d, uint32_t dlen)
Definition: sha256.cpp:41
graphene
Definition: api.cpp:48
raw.hpp
graphene::protocol::block_header
Definition: block.hpp:29