BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
fork_database.hpp
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 #pragma once
25 
27 
28 #include <graphene/chain/types.hpp>
29 
30 #include <boost/multi_index_container.hpp>
31 #include <boost/multi_index/member.hpp>
32 #include <boost/multi_index/ordered_index.hpp>
33 #include <boost/multi_index/hashed_index.hpp>
34 #include <boost/multi_index/mem_fun.hpp>
35 
36 namespace graphene { namespace chain {
37  using boost::multi_index_container;
38  using namespace boost::multi_index;
39 
40  struct fork_item
41  {
43  :num(d.block_num()),id(d.id()),data( std::move(d) ){}
44 
45  block_id_type previous_id()const { return data.previous; }
46 
47  weak_ptr< fork_item > prev;
48  uint32_t num; // initialized in ctor
51 
52  // contains witness block signing keys scheduled *after* the block has been applied
53  shared_ptr< vector< pair< witness_id_type, public_key_type > > > scheduled_witnesses;
54  uint64_t next_block_aslot = 0;
56  };
57  typedef shared_ptr<fork_item> item_ptr;
58 
59 
71  {
72  public:
73  typedef vector<item_ptr> branch_type;
75  const static int MAX_BLOCK_REORDERING = 1024;
76 
77  fork_database();
78  void reset();
79 
80  void start_block(signed_block b);
81  void remove(block_id_type b);
82  void set_head(shared_ptr<fork_item> h);
83  bool is_known_block(const block_id_type& id)const;
84  shared_ptr<fork_item> fetch_block(const block_id_type& id)const;
85  vector<item_ptr> fetch_block_by_number(uint32_t n)const;
86 
90  shared_ptr<fork_item> push_block(const signed_block& b);
91  shared_ptr<fork_item> head()const { return _head; }
92  void pop_block();
93 
98  pair< branch_type, branch_type > fetch_branch_from(block_id_type first,
99  block_id_type second)const;
100 
101  struct block_id;
102  struct block_num;
103  typedef multi_index_container<
104  item_ptr,
105  indexed_by<
106  hashed_unique<tag<block_id>, member<fork_item, block_id_type, &fork_item::id>, std::hash<fc::ripemd160>>,
107  ordered_non_unique<tag<block_num>, member<fork_item,uint32_t,&fork_item::num>>
108  >
110 
111  void set_max_size( uint32_t s );
112 
113  private:
115  void _push_block(const item_ptr& b );
116  void _push_next(const item_ptr& newly_inserted);
117 
118  uint32_t _max_size = 1024;
119 
120  fork_multi_index_type _index;
121  shared_ptr<fork_item> _head;
122  };
123 } } // graphene::chain
graphene::chain::fork_item::previous_id
block_id_type previous_id() const
Definition: fork_database.hpp:45
std::hash< fc::ripemd160 >
Definition: ripemd160.hpp:102
fc::typelist::first
at< List, 0 > first
Get the type at the beginning of the list.
Definition: typelist.hpp:190
graphene::chain::fork_item::data
signed_block data
Definition: fork_database.hpp:50
graphene::chain::fork_item::id
block_id_type id
Definition: fork_database.hpp:49
block.hpp
graphene::chain::item_ptr
shared_ptr< fork_item > item_ptr
Definition: fork_database.hpp:57
graphene::chain::fork_item::next_block_time
fc::time_point_sec next_block_time
Definition: fork_database.hpp:55
graphene::chain::fork_database::head
shared_ptr< fork_item > head() const
Definition: fork_database.hpp:91
graphene::chain::fork_item::num
uint32_t num
Definition: fork_database.hpp:48
graphene::chain::fork_item::prev
weak_ptr< fork_item > prev
Definition: fork_database.hpp:47
fc::remove
bool remove(const path &p)
Definition: filesystem.cpp:327
graphene::chain::fork_item
Definition: fork_database.hpp:40
graphene::chain::fork_database::fork_multi_index_type
multi_index_container< item_ptr, indexed_by< hashed_unique< tag< block_id >, member< fork_item, block_id_type, &fork_item::id >, std::hash< fc::ripemd160 > >, ordered_non_unique< tag< block_num >, member< fork_item, uint32_t,&fork_item::num > > > > fork_multi_index_type
Definition: fork_database.hpp:102
fc::time_point_sec
Definition: time.hpp:74
fc::ripemd160
Definition: ripemd160.hpp:11
graphene::chain::fork_database::branch_type
vector< item_ptr > branch_type
Definition: fork_database.hpp:73
std
Definition: zeroed_array.hpp:76
graphene::chain::fork_item::fork_item
fork_item(signed_block d)
Definition: fork_database.hpp:42
types.hpp
graphene::chain::fork_database
Definition: fork_database.hpp:70
graphene::protocol::signed_block
Definition: block.hpp:64
graphene
Definition: api.cpp:48
graphene::chain::fork_item::scheduled_witnesses
shared_ptr< vector< pair< witness_id_type, public_key_type > > > scheduled_witnesses
Definition: fork_database.hpp:53