TARAXA
dag_manager.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "dag.hpp"
4 #include "dag/dag_block.hpp"
5 #include "pbft/pbft_chain.hpp"
7 #include "storage/storage.hpp"
9 
10 namespace taraxa {
11 
15 class Network;
16 class DagBuffer;
17 class FullNode;
18 class KeyManager;
19 struct DagConfig;
20 
30 class DagManager : public std::enable_shared_from_this<DagManager> {
31  public:
35  enum class VerifyBlockReturnType : uint32_t {
36  Verified = 0,
38  AheadBlock,
47  };
48 
49  explicit DagManager(const FullNodeConfig &config, addr_t node_addr, std::shared_ptr<TransactionManager> trx_mgr,
50  std::shared_ptr<PbftChain> pbft_chain, std::shared_ptr<final_chain::FinalChain> final_chain,
51  std::shared_ptr<DbStorage> db, std::shared_ptr<KeyManager> key_manager);
52 
53  DagManager(const DagManager &) = delete;
54  DagManager(DagManager &&) = delete;
55  DagManager &operator=(const DagManager &) = delete;
57 
58  std::shared_ptr<DagManager> getShared();
59 
60  void setNetwork(std::weak_ptr<Network> network) { network_ = std::move(network); }
61 
66  bool isDagBlockKnown(const blk_hash_t &hash) const;
67 
73  std::shared_ptr<DagBlock> getDagBlock(const blk_hash_t &hash) const;
74 
81  std::pair<VerifyBlockReturnType, SharedTransactions> verifyBlock(
82  const DagBlock &blk, const std::unordered_map<trx_hash_t, std::shared_ptr<Transaction>> &trxs = {});
83 
89  std::pair<bool, std::vector<blk_hash_t>> pivotAndTipsAvailable(DagBlock const &blk);
90 
98  std::pair<bool, std::vector<blk_hash_t>> addDagBlock(DagBlock &&blk, SharedTransactions &&trxs = {},
99  bool proposed = false,
100  bool save = true); // insert to buffer if fail
101 
109  vec_blk_t getDagBlockOrder(blk_hash_t const &anchor, PbftPeriod period);
110 
122  uint setDagBlockOrder(blk_hash_t const &anchor, PbftPeriod period, vec_blk_t const &dag_order);
123 
124  std::optional<std::pair<blk_hash_t, std::vector<blk_hash_t>>> getLatestPivotAndTips() const;
125 
131  std::vector<blk_hash_t> getGhostPath(const blk_hash_t &source) const;
132 
137  std::vector<blk_hash_t> getGhostPath() const; // get ghost path from last anchor
138 
139  // ----- Total graph
140  void drawTotalGraph(std::string const &str) const;
141 
142  // ----- Pivot graph
143  // can return self as pivot chain
144  void drawPivotGraph(std::string const &str) const;
145  void drawGraph(std::string const &dotfile) const;
146 
147  std::pair<uint64_t, uint64_t> getNumVerticesInDag() const;
148  std::pair<uint64_t, uint64_t> getNumEdgesInDag() const;
149  level_t getMaxLevel() const { return max_level_; }
150 
151  // DAG anchors
153  std::shared_lock lock(mutex_);
154  return period_;
155  }
156  std::pair<blk_hash_t, blk_hash_t> getAnchors() const {
157  std::shared_lock lock(mutex_);
158  return std::make_pair(old_anchor_, anchor_);
159  }
160 
166  uint32_t getDagExpiryLimit() const { return dag_expiry_limit_; }
167 
168  const std::pair<PbftPeriod, std::map<uint64_t, std::unordered_set<blk_hash_t>>> getNonFinalizedBlocks() const;
169 
177  const std::tuple<PbftPeriod, std::vector<std::shared_ptr<DagBlock>>, SharedTransactions>
178  getNonFinalizedBlocksWithTransactions(const std::unordered_set<blk_hash_t> &known_hashes) const;
179 
181 
185  std::pair<size_t, size_t> getNonFinalizedBlocksSize() const;
186 
188 
194  std::shared_mutex &getDagMutex() { return mutex_; }
195 
202 
208  const DagConfig &getDagConfig() const { return dag_config_; }
209 
215  uint64_t getDagExpiryLevel() { return dag_expiry_level_; }
216 
222  static dev::bytes getVdfMessage(blk_hash_t const &hash, SharedTransactions const &trxs);
223 
229  static dev::bytes getVdfMessage(blk_hash_t const &hash, std::vector<trx_hash_t> const &trx_hashes);
230 
235  void clearLightNodeHistory(uint64_t light_node_history);
236 
237  private:
238  void recoverDag();
239  void addToDag(blk_hash_t const &hash, blk_hash_t const &pivot, std::vector<blk_hash_t> const &tips, uint64_t level,
240  bool finalized = false);
241  bool validateBlockNotExpired(const std::shared_ptr<DagBlock> &dag_block,
242  std::unordered_map<blk_hash_t, std::shared_ptr<DagBlock>> &expired_dag_blocks_to_remove);
243  void handleExpiredDagBlocksTransactions(const std::vector<trx_hash_t> &transactions_from_expired_dag_blocks) const;
244 
245  std::pair<blk_hash_t, std::vector<blk_hash_t>> getFrontier() const; // return pivot and tips
246  void updateFrontier();
247  std::atomic<level_t> max_level_ = 0;
248  mutable std::shared_mutex mutex_;
249  mutable std::shared_mutex order_dag_blocks_mutex_;
250  std::shared_ptr<PivotTree> pivot_tree_; // only contains pivot edges
251  std::shared_ptr<Dag> total_dag_; // contains both pivot and tips
252  std::shared_ptr<TransactionManager> trx_mgr_;
253  std::shared_ptr<PbftChain> pbft_chain_;
254  std::weak_ptr<Network> network_;
255  std::shared_ptr<DbStorage> db_;
256  std::shared_ptr<KeyManager> key_manager_;
257  blk_hash_t anchor_; // anchor of the last period
258  blk_hash_t old_anchor_; // anchor of the second to last period
259  PbftPeriod period_; // last period
260  std::map<uint64_t, std::unordered_set<blk_hash_t>> non_finalized_blks_;
264  const std::shared_ptr<DagBlock> genesis_block_;
265  const uint32_t max_levels_per_period_;
266  const uint32_t dag_expiry_limit_; // Any non finalized dag block with a level smaller by
267  // dag_expiry_limit_ than the current period anchor level is considered
268  // expired and it should be ignored or removed from DAG
269 
270  std::atomic_uint64_t dag_expiry_level_ =
271  0; // Level below which dag blocks are considered expired, it's value is
272  // always current anchor level minus dag_expiry_limit_ of non empty pbft periods
273 
274  const uint32_t cache_max_size_ = 10000;
275  const uint32_t cache_delete_step_ = 100;
277  std::shared_ptr<final_chain::FinalChain> final_chain_;
278  const uint64_t kPbftGasLimit;
280  const uint64_t kValidatorMaxVote;
281 
283 };
284 
287 } // namespace taraxa
Definition: util.hpp:346
Definition: FixedHash.h:35
DagBlock class is a DAG block class which main data is a list of transaction hashes included in the b...
Definition: dag_block.hpp:16
Definition: event.hpp:60
blk_hash_t old_anchor_
Definition: dag_manager.hpp:258
uint64_t getDagExpiryLevel()
Retrieves Dag expiry level.
Definition: dag_manager.hpp:215
uint setDagBlockOrder(blk_hash_t const &anchor, PbftPeriod period, vec_blk_t const &dag_order)
Sets the dag block order on finalizing PBFT block IMPORTANT: This method is invoked on finalizing a p...
Definition: dag_manager.cpp:308
void handleExpiredDagBlocksTransactions(const std::vector< trx_hash_t > &transactions_from_expired_dag_blocks) const
Definition: dag_manager.cpp:398
const uint32_t max_levels_per_period_
Definition: dag_manager.hpp:265
const uint64_t kValidatorMaxVote
Definition: dag_manager.hpp:280
void clearLightNodeHistory(uint64_t light_node_history)
Clears light node history.
Definition: dag_manager.cpp:284
DagManager(DagManager &&)=delete
std::shared_ptr< PivotTree > pivot_tree_
Definition: dag_manager.hpp:250
std::shared_ptr< KeyManager > key_manager_
Definition: dag_manager.hpp:256
std::pair< VerifyBlockReturnType, SharedTransactions > verifyBlock(const DagBlock &blk, const std::unordered_map< trx_hash_t, std::shared_ptr< Transaction >> &trxs={})
Verifies new DAG block.
Definition: dag_manager.cpp:585
std::shared_ptr< TransactionManager > trx_mgr_
Definition: dag_manager.hpp:252
void setNetwork(std::weak_ptr< Network > network)
Definition: dag_manager.hpp:60
VerifyBlockReturnType
return type of verifyBlock
Definition: dag_manager.hpp:35
std::weak_ptr< Network > network_
Definition: dag_manager.hpp:254
std::shared_ptr< PbftChain > pbft_chain_
Definition: dag_manager.hpp:253
const HardforksConfig kHardforks
Definition: dag_manager.hpp:279
void drawGraph(std::string const &dotfile) const
Definition: dag_manager.cpp:188
std::pair< uint64_t, uint64_t > getNumEdgesInDag() const
Definition: dag_manager.cpp:76
bool isDagBlockKnown(const blk_hash_t &hash) const
Definition: dag_manager.cpp:735
void updateFrontier()
Definition: dag_manager.cpp:230
void drawTotalGraph(std::string const &str) const
Definition: dag_manager.cpp:81
std::shared_ptr< Dag > total_dag_
Definition: dag_manager.hpp:251
DagManager & operator=(DagManager &&)=delete
blk_hash_t anchor_
Definition: dag_manager.hpp:257
DagManager & operator=(const DagManager &)=delete
bool validateBlockNotExpired(const std::shared_ptr< DagBlock > &dag_block, std::unordered_map< blk_hash_t, std::shared_ptr< DagBlock >> &expired_dag_blocks_to_remove)
Definition: dag_manager.cpp:422
void drawPivotGraph(std::string const &str) const
Definition: dag_manager.cpp:86
static dev::bytes getVdfMessage(blk_hash_t const &hash, SharedTransactions const &trxs)
Retrieves VDF message from block hash and transactions.
Definition: dag_manager.cpp:755
std::shared_mutex order_dag_blocks_mutex_
Definition: dag_manager.hpp:249
const uint32_t dag_expiry_limit_
Definition: dag_manager.hpp:266
DagManager(const FullNodeConfig &config, addr_t node_addr, std::shared_ptr< TransactionManager > trx_mgr, std::shared_ptr< PbftChain > pbft_chain, std::shared_ptr< final_chain::FinalChain > final_chain, std::shared_ptr< DbStorage > db, std::shared_ptr< KeyManager > key_manager)
Definition: dag_manager.cpp:19
const DagConfig & getDagConfig() const
Retrieves Dag config.
Definition: dag_manager.hpp:208
SortitionParamsManager & sortitionParamsManager()
Retrieves sortition manager.
Definition: dag_manager.hpp:201
util::Event< DagManager, DagBlock > const block_verified_
Definition: dag_manager.hpp:187
std::atomic_uint64_t dag_expiry_level_
Definition: dag_manager.hpp:270
std::pair< size_t, size_t > getNonFinalizedBlocksSize() const
Definition: dag_manager.cpp:574
const uint32_t cache_delete_step_
Definition: dag_manager.hpp:275
std::shared_ptr< DbStorage > db_
Definition: dag_manager.hpp:255
SortitionParamsManager sortition_params_manager_
Definition: dag_manager.hpp:262
const uint64_t kPbftGasLimit
Definition: dag_manager.hpp:278
ExpirationCacheMap< blk_hash_t, DagBlock > seen_blocks_
Definition: dag_manager.hpp:276
std::pair< blk_hash_t, blk_hash_t > getAnchors() const
Definition: dag_manager.hpp:156
std::shared_mutex & getDagMutex()
Retrieves Dag Manager mutex, only to be used when finalizing pbft block.
Definition: dag_manager.hpp:194
std::shared_mutex mutex_
Definition: dag_manager.hpp:248
std::map< uint64_t, std::unordered_set< blk_hash_t > > non_finalized_blks_
Definition: dag_manager.hpp:260
std::pair< uint64_t, uint64_t > getNumVerticesInDag() const
Definition: dag_manager.cpp:71
DagManager(const DagManager &)=delete
std::shared_ptr< DagManager > getShared()
Definition: dag_manager.cpp:62
DagFrontier frontier_
Definition: dag_manager.hpp:261
PbftPeriod period_
Definition: dag_manager.hpp:259
std::atomic< level_t > max_level_
Definition: dag_manager.hpp:247
level_t getMaxLevel() const
Definition: dag_manager.hpp:149
std::shared_ptr< DagBlock > getDagBlock(const blk_hash_t &hash) const
Gets dag block from either local memory cache or db.
Definition: dag_manager.cpp:743
const std::shared_ptr< DagBlock > genesis_block_
Definition: dag_manager.hpp:264
PbftPeriod getLatestPeriod() const
Definition: dag_manager.hpp:152
DagFrontier getDagFrontier()
Definition: dag_manager.cpp:128
std::vector< blk_hash_t > getGhostPath() const
Retrieves ghost path which is ordered list of non finalized pivot blocks for last anchor.
Definition: dag_manager.cpp:249
std::shared_ptr< final_chain::FinalChain > final_chain_
Definition: dag_manager.hpp:277
uint32_t getDagExpiryLimit() const
Retrieves Dag expiry limit.
Definition: dag_manager.hpp:166
const std::tuple< PbftPeriod, std::vector< std::shared_ptr< DagBlock > >, SharedTransactions > getNonFinalizedBlocksWithTransactions(const std::unordered_set< blk_hash_t > &known_hashes) const
Retrieves current period together with non finalized blocks with the unique list of non finalized tra...
Definition: dag_manager.cpp:546
void recoverDag()
Definition: dag_manager.cpp:447
const uint32_t cache_max_size_
Definition: dag_manager.hpp:274
vec_blk_t getDagBlockOrder(blk_hash_t const &anchor, PbftPeriod period)
Retrieves DAG block order for specified anchor. Order should always be the same for the same anchor i...
Definition: dag_manager.cpp:256
void addToDag(blk_hash_t const &hash, blk_hash_t const &pivot, std::vector< blk_hash_t > const &tips, uint64_t level, bool finalized=false)
Definition: dag_manager.cpp:194
std::pair< bool, std::vector< blk_hash_t > > addDagBlock(DagBlock &&blk, SharedTransactions &&trxs={}, bool proposed=false, bool save=true)
adds verified DAG block in the DAG
Definition: dag_manager.cpp:133
const DagConfig & dag_config_
Definition: dag_manager.hpp:263
std::pair< blk_hash_t, std::vector< blk_hash_t > > getFrontier() const
Definition: dag_manager.cpp:214
const std::pair< PbftPeriod, std::map< uint64_t, std::unordered_set< blk_hash_t > > > getNonFinalizedBlocks() const
Definition: dag_manager.cpp:539
std::pair< bool, std::vector< blk_hash_t > > pivotAndTipsAvailable(DagBlock const &blk)
Checks if block pivot and tips are in DAG.
Definition: dag_manager.cpp:91
std::optional< std::pair< blk_hash_t, std::vector< blk_hash_t > > > getLatestPivotAndTips() const
Definition: dag_manager.cpp:209
DagManager class contains in memory representation of part of the DAG that is not yet finalized in a ...
Definition: dag_manager.hpp:30
SortitionParamsManager class calculates DAG efficiency and makes VDF difficulty adjustments.
Definition: sortition_params_manager.hpp:36
#define LOG_OBJECTS_DEFINE
Definition: logger.hpp:60
std::vector<::byte > bytes
Definition: Common.h:46
Definition: config.hpp:8
std::vector< blk_hash_t > vec_blk_t
Definition: types.hpp:50
uint256_hash_t blk_hash_t
Definition: types.hpp:42
EthBlockNumber PbftPeriod
Definition: types.hpp:24
std::vector< SharedTransaction > SharedTransactions
Definition: transaction.hpp:81
uint64_t level_t
Definition: types.hpp:46
Definition: dag_config.hpp:17
Definition: dag_block.hpp:120
Definition: config.hpp:24
Definition: hardfork.hpp:93