Scorum
fork_database.hpp
Go to the documentation of this file.
1 #pragma once
3 
4 #include <boost/multi_index_container.hpp>
5 #include <boost/multi_index/member.hpp>
6 #include <boost/multi_index/ordered_index.hpp>
7 #include <boost/multi_index/hashed_index.hpp>
8 #include <boost/multi_index/mem_fun.hpp>
9 
10 namespace scorum {
11 namespace chain {
12 using boost::multi_index_container;
13 using namespace boost::multi_index;
14 
17 
18 struct fork_item
19 {
21  : num(d.block_num())
22  , id(d.id())
23  , data(std::move(d))
24  {
25  }
26 
28  {
29  return data.previous;
30  }
31 
32  std::weak_ptr<fork_item> prev;
33  uint32_t num; // initialized in ctor
38  bool invalid = false;
41 };
42 typedef std::shared_ptr<fork_item> item_ptr;
43 
55 {
56 public:
57  typedef std::vector<item_ptr> branch_type;
59  const static int MAX_BLOCK_REORDERING = 1024;
60 
61  fork_database();
62  void reset();
63 
64  void start_block(signed_block b);
65  void remove(block_id_type b);
66  void set_head(std::shared_ptr<fork_item> h);
67  bool is_known_block(const block_id_type& id) const;
68  std::shared_ptr<fork_item> fetch_block(const block_id_type& id) const;
69  std::vector<item_ptr> fetch_block_by_number(uint32_t n) const;
70 
74  std::shared_ptr<fork_item> push_block(const signed_block& b);
75  std::shared_ptr<fork_item> head() const
76  {
77  return _head;
78  }
79  void pop_block();
80 
85  std::pair<branch_type, branch_type> fetch_branch_from(block_id_type first, block_id_type second) const;
86  std::shared_ptr<fork_item> walk_main_branch_to_num(uint32_t block_num) const;
87  std::shared_ptr<fork_item> fetch_block_on_main_branch_by_number(uint32_t block_num) const;
88 
89  struct block_id;
90  struct block_num;
91  struct by_previous;
92  typedef multi_index_container<item_ptr,
93  indexed_by<hashed_unique<tag<block_id>,
94  member<fork_item, block_id_type, &fork_item::id>,
95  std::hash<fc::ripemd160>>,
96  hashed_non_unique<tag<by_previous>,
97  const_mem_fun<fork_item,
100  std::hash<fc::ripemd160>>,
101  ordered_non_unique<tag<block_num>,
102  member<fork_item, uint32_t, &fork_item::num>>>>
104 
105  void set_max_size(uint32_t s);
106 
107 private:
109  void _push_block(const item_ptr& b);
110  void _push_next(const item_ptr& newly_inserted);
111 
112  uint32_t _max_size = 1024;
113 
114  fork_multi_index_type _unlinked_index;
115  fork_multi_index_type _index;
116  std::shared_ptr<fork_item> _head;
117 };
118 
119 } // namespace chain
120 } // namespace scorum
std::vector< item_ptr > branch_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 > >, hashed_non_unique< tag< by_previous >, const_mem_fun< fork_item, block_id_type, &fork_item::previous_id >, std::hash< fc::ripemd160 > >, ordered_non_unique< tag< block_num >, member< fork_item, uint32_t, &fork_item::num > > > > fork_multi_index_type
std::shared_ptr< fork_item > head() const
void remove(db_index &db_idx, const TObject &o)
Definition: db_accessor.hpp:57
std::shared_ptr< fork_item > item_ptr
fc::ripemd160 block_id_type
Definition: types.hpp:63
Definition: asset.cpp:15
block_id_type previous_id() const
fork_item(signed_block d)
std::weak_ptr< fork_item > prev