TARAXA
Loading...
Searching...
No Matches
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/logging.hpp"
18
19namespace taraxa {
20
24class DagManager;
25class Network;
26
30class 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);
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
99class PivotTree : public Dag {
100 public:
102 explicit PivotTree(blk_hash_t const &dag_genesis_block_hash) : Dag(dag_genesis_block_hash) {}
103 virtual ~PivotTree() = default;
104
105 PivotTree(const PivotTree &) = default;
106 PivotTree(PivotTree &&) = default;
107 PivotTree &operator=(const PivotTree &) = default;
109
112 using Dag::vertex_t;
113
114 std::vector<blk_hash_t> getGhostPath(const blk_hash_t &vertex) const;
115};
116class KeyManager;
117
118// for graphviz
119template <class Property1>
121 public:
123 template <class Vertex>
124 void operator()(std::ostream &out, const Vertex &v) const {
125 out << "[label=\"" << name[v].toString().substr(0, 8) << " " << "\"]";
126 }
127
128 private:
129 Property1 name;
130};
131
132template <class Property>
134 public:
135 explicit edge_label_writer(Property weight) : weight(weight) {}
136 template <class Edge>
137 void operator()(std::ostream &out, const Edge &e) const {
138 if (weight[e] == 0) {
139 out << "[style=\"dashed\" dir=\"back\"]";
140 } else {
141 out << "[dir=\"back\"]";
142 }
143 }
144
145 private:
146 Property weight;
147};
150} // namespace taraxa
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:135
boost::property_map< graph_t, boost::vertex_index_t >::const_type vertex_index_map_const_t
Definition dag.hpp:48
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
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(const Dag &)=default
boost::graph_traits< graph_t >::edge_iterator edge_iter_t
Definition dag.hpp:44
PivotTree(blk_hash_t const &dag_genesis_block_hash)
Definition dag.hpp:102
vertex_label_writer(Property1 name)
Definition dag.hpp:122
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::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
PivotTree(const PivotTree &)=default
Dag & operator=(Dag &&)=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:124
logger::Logger logger_
Definition dag.hpp:91
boost::graph_traits< graph_t >::edge_descriptor edge_t
Definition dag.hpp:42
Property1 name
Definition dag.hpp:129
boost::property_map< graph_t, boost::edge_index_t >::type edge_index_map_t
Definition dag.hpp:55
Property weight
Definition dag.hpp:146
Dag & operator=(const Dag &)=default
friend DagManager
Definition dag.hpp:101
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
PivotTree & operator=(const PivotTree &)=default
void operator()(std::ostream &out, const Edge &e) const
Definition dag.hpp:137
PivotTree & operator=(PivotTree &&)=default
Thread safe. Labelled graph.
Definition dag.hpp:30
Definition dag.hpp:99
Definition dag.hpp:133
Definition dag.hpp:120
std::shared_ptr< spdlog::logger > Logger
Definition logging.hpp:12
Definition app.hpp:16