TARAXA
dag.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <boost/function.hpp>
4 #include <boost/graph/adjacency_list.hpp>
5 #include <boost/graph/breadth_first_search.hpp>
6 #include <boost/graph/depth_first_search.hpp>
7 #include <boost/graph/graph_traits.hpp>
8 #include <boost/graph/graphviz.hpp>
9 #include <boost/graph/labeled_graph.hpp>
10 #include <boost/graph/properties.hpp>
11 #include <boost/property_map/property_map.hpp>
12 #include <boost/thread.hpp>
13 #include <iostream>
14 #include <string>
15 
16 #include "common/types.hpp"
17 #include "logger/logger.hpp"
18 
19 namespace taraxa {
20 
24 class DagManager;
25 class Network;
26 
30 class Dag {
31  public:
32  // properties
34  boost::property<boost::vertex_index_t, blk_hash_t, boost::property<boost::vertex_index1_t, uint64_t>>;
35  using edge_property_t = boost::property<boost::edge_index_t, uint64_t>;
36 
37  // graph def
38  using adj_list_t =
39  boost::adjacency_list<boost::setS, boost::hash_setS, boost::directedS, vertex_property_t, edge_property_t>;
40  using graph_t = boost::labeled_graph<adj_list_t, blk_hash_t, boost::hash_mapS>;
41  using vertex_t = boost::graph_traits<graph_t>::vertex_descriptor;
42  using edge_t = boost::graph_traits<graph_t>::edge_descriptor;
43  using vertex_iter_t = boost::graph_traits<graph_t>::vertex_iterator;
44  using edge_iter_t = boost::graph_traits<graph_t>::edge_iterator;
45  using vertex_adj_iter_t = boost::graph_traits<graph_t>::adjacency_iterator;
46 
47  // property_map
48  using vertex_index_map_const_t = boost::property_map<graph_t, boost::vertex_index_t>::const_type;
49  using vertex_index_map_t = boost::property_map<graph_t, boost::vertex_index_t>::type;
50 
51  using vertex_period_map_const_t = boost::property_map<graph_t, boost::vertex_index1_t>::const_type;
52  using vertex_period_map_t = boost::property_map<graph_t, boost::vertex_index1_t>::type;
53 
54  using edge_index_map_const_t = boost::property_map<graph_t, boost::edge_index_t>::const_type;
55  using edge_index_map_t = boost::property_map<graph_t, boost::edge_index_t>::type;
56 
57  friend DagManager;
58 
59  explicit Dag(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr);
60  virtual ~Dag() = default;
61 
62  Dag(const Dag &) = default;
63  Dag(Dag &&) = default;
64  Dag &operator=(const Dag &) = default;
65  Dag &operator=(Dag &&) = default;
66 
67  uint64_t getNumVertices() const;
68  uint64_t getNumEdges() const;
69  bool hasVertex(blk_hash_t const &v) const;
70  bool addVEEs(blk_hash_t const &new_vertex, blk_hash_t const &pivot, std::vector<blk_hash_t> const &tips);
71 
72  void getLeaves(std::vector<blk_hash_t> &tips) const;
73  void drawGraph(std::string const &filename) const;
74 
75  bool computeOrder(const blk_hash_t &anchor, std::vector<blk_hash_t> &ordered_period_vertices,
76  const std::map<uint64_t, std::unordered_set<blk_hash_t>> &non_finalized_blks);
77 
78  void clear();
79 
80  protected:
81  // Note: private functions does not lock
82 
83  // traverser API
84  bool reachable(vertex_t const &from, vertex_t const &to) const;
85 
86  void collectLeafVertices(std::vector<vertex_t> &leaves) const;
87 
89 
90  protected:
92 };
93 
99 class PivotTree : public Dag {
100  public:
101  friend DagManager;
102  explicit PivotTree(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr)
103  : Dag(dag_genesis_block_hash, node_addr) {}
104  virtual ~PivotTree() = default;
105 
106  PivotTree(const PivotTree &) = default;
107  PivotTree(PivotTree &&) = default;
108  PivotTree &operator=(const PivotTree &) = default;
109  PivotTree &operator=(PivotTree &&) = default;
110 
113  using Dag::vertex_t;
114 
115  std::vector<blk_hash_t> getGhostPath(const blk_hash_t &vertex) const;
116 };
117 class DagBuffer;
118 class FullNode;
119 class KeyManager;
120 
121 // for graphviz
122 template <class Property1>
124  public:
126  template <class Vertex>
127  void operator()(std::ostream &out, const Vertex &v) const {
128  out << "[label=\"" << name[v].toString().substr(0, 8) << " "
129  << "\"]";
130  }
131 
132  private:
133  Property1 name;
134 };
135 
136 template <class Property>
138  public:
139  explicit edge_label_writer(Property weight) : weight(weight) {}
140  template <class Edge>
141  void operator()(std::ostream &out, const Edge &e) const {
142  if (weight[e] == 0) {
143  out << "[style=\"dashed\" dir=\"back\"]";
144  } else {
145  out << "[dir=\"back\"]";
146  }
147  }
148 
149  private:
150  Property weight;
151 };
154 } // namespace taraxa
Definition: node.hpp:43
Definition: key_manager.hpp:11
boost::property< boost::vertex_index_t, blk_hash_t, boost::property< boost::vertex_index1_t, uint64_t > > vertex_property_t
Definition: dag.hpp:34
virtual ~PivotTree()=default
graph_t graph_
Definition: dag.hpp:88
edge_label_writer(Property weight)
Definition: dag.hpp:139
boost::property_map< graph_t, boost::vertex_index_t >::const_type vertex_index_map_const_t
Definition: dag.hpp:48
PivotTree & operator=(PivotTree &&)=default
boost::property< boost::edge_index_t, uint64_t > edge_property_t
Definition: dag.hpp:35
boost::graph_traits< graph_t >::adjacency_iterator vertex_adj_iter_t
Definition: dag.hpp:45
bool computeOrder(const blk_hash_t &anchor, std::vector< blk_hash_t > &ordered_period_vertices, const std::map< uint64_t, std::unordered_set< blk_hash_t >> &non_finalized_blks)
Definition: dag.cpp:103
boost::property_map< graph_t, boost::vertex_index1_t >::type vertex_period_map_t
Definition: dag.hpp:52
void collectLeafVertices(std::vector< vertex_t > &leaves) const
Definition: dag.cpp:89
bool reachable(vertex_t const &from, vertex_t const &to) const
Definition: dag.cpp:173
void getLeaves(std::vector< blk_hash_t > &tips) const
Definition: dag.cpp:29
boost::property_map< graph_t, boost::edge_index_t >::const_type edge_index_map_const_t
Definition: dag.hpp:54
bool hasVertex(blk_hash_t const &v) const
Definition: dag.cpp:27
std::vector< blk_hash_t > getGhostPath(const blk_hash_t &vertex) const
Definition: dag.cpp:204
friend DagManager
Definition: dag.hpp:57
uint64_t getNumEdges() const
Definition: dag.cpp:25
virtual ~Dag()=default
bool addVEEs(blk_hash_t const &new_vertex, blk_hash_t const &pivot, std::vector< blk_hash_t > const &tips)
Definition: dag.cpp:37
boost::property_map< graph_t, boost::vertex_index_t >::type vertex_index_map_t
Definition: dag.hpp:49
Dag & operator=(Dag &&)=default
Dag(const Dag &)=default
boost::graph_traits< graph_t >::edge_iterator edge_iter_t
Definition: dag.hpp:44
vertex_label_writer(Property1 name)
Definition: dag.hpp:125
boost::labeled_graph< adj_list_t, blk_hash_t, boost::hash_mapS > graph_t
Definition: dag.hpp:40
boost::graph_traits< graph_t >::vertex_iterator vertex_iter_t
Definition: dag.hpp:43
boost::property_map< graph_t, boost::vertex_index1_t >::const_type vertex_period_map_const_t
Definition: dag.hpp:51
Dag & operator=(const Dag &)=default
Dag(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr)
Definition: dag.cpp:17
PivotTree(const PivotTree &)=default
Dag(Dag &&)=default
void drawGraph(std::string const &filename) const
Definition: dag.cpp:77
void operator()(std::ostream &out, const Vertex &v) const
Definition: dag.hpp:127
boost::graph_traits< graph_t >::edge_descriptor edge_t
Definition: dag.hpp:42
Property1 name
Definition: dag.hpp:133
PivotTree & operator=(const PivotTree &)=default
boost::property_map< graph_t, boost::edge_index_t >::type edge_index_map_t
Definition: dag.hpp:55
Property weight
Definition: dag.hpp:150
friend DagManager
Definition: dag.hpp:101
PivotTree(blk_hash_t const &dag_genesis_block_hash, addr_t node_addr)
Definition: dag.hpp:102
boost::adjacency_list< boost::setS, boost::hash_setS, boost::directedS, vertex_property_t, edge_property_t > adj_list_t
Definition: dag.hpp:39
PivotTree(PivotTree &&)=default
boost::graph_traits< graph_t >::vertex_descriptor vertex_t
Definition: dag.hpp:41
void clear()
Definition: dag.cpp:87
uint64_t getNumVertices() const
Definition: dag.cpp:24
void operator()(std::ostream &out, const Edge &e) const
Definition: dag.hpp:141
Thread safe. Labelled graph.
Definition: dag.hpp:30
Definition: dag.hpp:99
Definition: dag.hpp:137
Definition: dag.hpp:123
#define LOG_OBJECTS_DEFINE
Definition: logger.hpp:60
Definition: config.hpp:8