block.cpp 3.7 KB
Newer Older
N
Nathan Hourt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*
 * Copyright (c) 2017, Respective Authors.
 *
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#include <eos/chain/protocol/block.hpp>
#include <fc/io/raw.hpp>
#include <fc/bitutil.hpp>
#include <algorithm>

namespace eos { namespace chain {
   digest_type block_header::digest()const
   {
      return digest_type::hash(*this);
   }

   uint32_t block_header::num_from_id(const block_id_type& id)
   {
      return fc::endian_reverse_u32(id._hash[0]);
   }

   block_id_type signed_block_header::id()const
   {
      auto tmp = fc::sha224::hash( *this );
      tmp._hash[0] = fc::endian_reverse_u32(block_num()); // store the block num in the ID, 160 bits is plenty for the hash
      static_assert( sizeof(tmp._hash[0]) == 4, "should be 4 bytes" );
      block_id_type result;
      memcpy(result._hash, tmp._hash, std::min(sizeof(result), sizeof(tmp)));
      return result;
   }

   fc::ecc::public_key signed_block_header::signee()const
   {
      return fc::ecc::public_key( producer_signature, digest(), true/*enforce canonical*/ );
   }

   void signed_block_header::sign( const fc::ecc::private_key& signer )
   {
      producer_signature = signer.sign_compact( digest() );
   }

   bool signed_block_header::validate_signee( const fc::ecc::public_key& expected_signee )const
   {
      return signee() == expected_signee;
   }

N
Nathan Hourt 已提交
65 66 67 68 69 70 71 72 73 74 75 76
   digest_type merkle(vector<digest_type> ids) {
      while (ids.size() > 1) {
         if (ids.size() % 2)
            ids.push_back(ids.back());
         for (int i = 0; i < ids.size() / 2; ++i)
            ids[i/2] = digest_type::hash(std::make_pair(ids[i], ids[i+1]));
         ids.resize(ids.size() / 2);
      }

      return ids.front();
   }

N
Nathan Hourt 已提交
77 78
   checksum_type signed_block::calculate_merkle_root()const
   {
N
Nathan Hourt 已提交
79
      if(cycles.empty())
N
Nathan Hourt 已提交
80 81 82
         return checksum_type();

      vector<digest_type> ids;
N
Nathan Hourt 已提交
83 84 85
      for (const auto& cycle : cycles)
         for (const auto& thread : cycle)
            ids.emplace_back(thread.merkle_digest());
N
Nathan Hourt 已提交
86

N
Nathan Hourt 已提交
87 88
      return checksum_type::hash(merkle(ids));
   }
N
Nathan Hourt 已提交
89

N
Nathan Hourt 已提交
90 91 92 93 94 95 96 97 98 99
   digest_type thread::merkle_digest() const {
      vector<digest_type> ids;
      std::transform(input_transactions.begin(), input_transactions.end(), std::back_inserter(ids),
                     [](const input_transaction& trx) {
         if (trx.which() == input_transaction::tag<signed_transaction>::value)
            return trx.get<signed_transaction>().merkle_digest();
#warning How do I get the digest from a generated_transaction_id_type?...
      });
      std::transform(output_transactions.begin(), output_transactions.end(), std::back_inserter(ids),
                     std::bind(&generated_transaction::merkle_digest, std::placeholders::_1));
N
Nathan Hourt 已提交
100

N
Nathan Hourt 已提交
101
      return merkle(ids);
N
Nathan Hourt 已提交
102 103 104
   }

} }