BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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
shared_ptr< fork_item > item_ptr
Definition: api.cpp:56
weak_ptr< fork_item > prev
shared_ptr< vector< pair< witness_id_type, public_key_type > > > scheduled_witnesses
fc::time_point_sec next_block_time
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
at< List, 0 > first
Get the type at the beginning of the list.
Definition: typelist.hpp:190
shared_ptr< fork_item > head() const
vector< item_ptr > branch_type
block_id_type previous_id() const