TARAXA
Loading...
Searching...
No Matches
pillar_chain_manager.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <memory>
4
5#include "common/event.hpp"
7#include "logger/logger.hpp"
10
11namespace taraxa {
12class DbStorage;
13class Network;
14class KeyManager;
15struct FicusHardforkConfig;
16} // namespace taraxa
17
18namespace taraxa::final_chain {
19class FinalChain;
20}
21
22namespace taraxa::pillar_chain {
23
32 private:
34
35 public:
38
39 public:
40 PillarChainManager(const FicusHardforkConfig& ficus_hf_config, std::shared_ptr<DbStorage> db,
41 std::shared_ptr<final_chain::FinalChain> final_chain, std::shared_ptr<KeyManager> key_manager,
42 addr_t node_addr);
43
54 std::shared_ptr<PillarBlock> createPillarBlock(PbftPeriod period,
55 const std::shared_ptr<const final_chain::BlockHeader>& block_header,
56 const h256& bridge_root, const h256& bridge_epoch);
57
68 std::shared_ptr<PillarVote> genAndPlacePillarVote(PbftPeriod period, const blk_hash_t& pillar_block_hash,
69 const secret_t& node_sk, bool broadcast_vote);
70
75 void setNetwork(std::weak_ptr<Network> network);
76
83 bool isRelevantPillarVote(const std::shared_ptr<PillarVote> vote) const;
84
91 bool validatePillarVote(const std::shared_ptr<PillarVote> vote) const;
92
96 bool isPillarBlockLatestFinalized(const blk_hash_t& block_hash) const;
97
101 std::shared_ptr<PillarBlock> getLastFinalizedPillarBlock() const;
102
109 uint64_t addVerifiedPillarVote(const std::shared_ptr<PillarVote>& vote);
110
117 std::vector<std::shared_ptr<PillarVote>> finalizePillarBlock(const blk_hash_t& pillar_block_hash);
118
122 std::shared_ptr<PillarBlock> getCurrentPillarBlock() const;
123
134 std::vector<std::shared_ptr<PillarVote>> getVerifiedPillarVotes(PbftPeriod period, const blk_hash_t pillar_block_hash,
135 bool above_threshold = false) const;
136
140 bool isValidPillarBlock(const std::shared_ptr<PillarBlock>& pillar_block) const;
141
146 std::optional<uint64_t> getPillarConsensusThreshold(PbftPeriod period) const;
147
148 private:
157 std::vector<PillarBlock::ValidatorVoteCountChange> getOrderedValidatorsVoteCountsChanges(
158 const std::vector<state_api::ValidatorVoteCount>& current_vote_counts,
159 const std::vector<state_api::ValidatorVoteCount>& previous_pillar_block_vote_counts);
160
167 void saveNewPillarBlock(const std::shared_ptr<PillarBlock>& pillar_block,
168 std::vector<state_api::ValidatorVoteCount>&& new_vote_counts);
169
170 private:
171 // Node config
173
174 std::shared_ptr<DbStorage> db_;
175 std::weak_ptr<Network> network_;
176 std::shared_ptr<final_chain::FinalChain> final_chain_;
177 std::shared_ptr<KeyManager> key_manager_;
178
180
181 // Last finalized pillar block - saved into db together with > threshold votes
182 std::shared_ptr<PillarBlock> last_finalized_pillar_block_;
183 // Current pillar block
184 std::shared_ptr<PillarBlock> current_pillar_block_;
185 // Full list of validators vote counts for tbe current pillar block period - no concurrent access protection needed
186 std::vector<state_api::ValidatorVoteCount> current_pillar_block_vote_counts_;
187
188 // Pillar votes for latest_pillar_block_.period - 1, latest_pillar_block_.period and potential +1 future pillar
189 // block period
191
192 // Protects last_finalized_pillar_block_ & current_pillar_block_
193 mutable std::shared_mutex mutex_;
194
196};
197
200} // namespace taraxa::pillar_chain
Definition FixedHash.h:35
Definition pillar_votes.hpp:10
bool isRelevantPillarVote(const std::shared_ptr< PillarVote > vote) const
Checks if vote is related to saved latest_pillar_block_ and it is not already saved.
Definition pillar_chain_manager.cpp:214
std::shared_ptr< PillarVote > genAndPlacePillarVote(PbftPeriod period, const blk_hash_t &pillar_block_hash, const secret_t &node_sk, bool broadcast_vote)
Generate and place pillar vote for provided pillar_block_hash in case the whole pillar block is prese...
Definition pillar_chain_manager.cpp:117
std::vector< std::shared_ptr< PillarVote > > finalizePillarBlock(const blk_hash_t &pillar_block_hash)
Finalize pillar block.
Definition pillar_chain_manager.cpp:143
const decltype(pillar_block_finalized_emitter_) ::Subscriber & pillar_block_finalized_
Definition pillar_chain_manager.hpp:36
std::vector< state_api::ValidatorVoteCount > current_pillar_block_vote_counts_
Definition pillar_chain_manager.hpp:186
const addr_t node_addr_
Definition pillar_chain_manager.hpp:179
std::shared_ptr< PillarBlock > current_pillar_block_
Definition pillar_chain_manager.hpp:184
std::weak_ptr< Network > network_
Definition pillar_chain_manager.hpp:175
bool validatePillarVote(const std::shared_ptr< PillarVote > vote) const
Validates pillar vote.
Definition pillar_chain_manager.cpp:247
std::shared_ptr< final_chain::FinalChain > final_chain_
Definition pillar_chain_manager.hpp:176
std::shared_ptr< DbStorage > db_
Definition pillar_chain_manager.hpp:174
bool isValidPillarBlock(const std::shared_ptr< PillarBlock > &pillar_block) const
Definition pillar_chain_manager.cpp:327
std::shared_ptr< PillarBlock > last_finalized_pillar_block_
Definition pillar_chain_manager.hpp:182
std::shared_ptr< PillarBlock > createPillarBlock(PbftPeriod period, const std::shared_ptr< const final_chain::BlockHeader > &block_header, const h256 &bridge_root, const h256 &bridge_epoch)
Definition pillar_chain_manager.cpp:52
std::vector< std::shared_ptr< PillarVote > > getVerifiedPillarVotes(PbftPeriod period, const blk_hash_t pillar_block_hash, bool above_threshold=false) const
Get all pillar votes for specified pillar block.
Definition pillar_chain_manager.cpp:314
std::vector< PillarBlock::ValidatorVoteCountChange > getOrderedValidatorsVoteCountsChanges(const std::vector< state_api::ValidatorVoteCount > &current_vote_counts, const std::vector< state_api::ValidatorVoteCount > &previous_pillar_block_vote_counts)
Return a vector of validators vote counts changes between the current and previous pillar block Chang...
Definition pillar_chain_manager.cpp:362
const FicusHardforkConfig & kFicusHfConfig
Definition pillar_chain_manager.hpp:172
void saveNewPillarBlock(const std::shared_ptr< PillarBlock > &pillar_block, std::vector< state_api::ValidatorVoteCount > &&new_vote_counts)
Save new pillar block into db & class data members.
Definition pillar_chain_manager.cpp:109
std::optional< uint64_t > getPillarConsensusThreshold(PbftPeriod period) const
Definition pillar_chain_manager.cpp:348
std::shared_mutex mutex_
Definition pillar_chain_manager.hpp:193
const util::event::EventEmitter< const PillarBlockData & > pillar_block_finalized_emitter_
Definition pillar_chain_manager.hpp:33
void setNetwork(std::weak_ptr< Network > network)
Set network as a weak pointer.
Definition pillar_chain_manager.cpp:427
std::shared_ptr< PillarBlock > getCurrentPillarBlock() const
Definition pillar_chain_manager.cpp:209
PillarVotes pillar_votes_
Definition pillar_chain_manager.hpp:190
bool isPillarBlockLatestFinalized(const blk_hash_t &block_hash) const
Definition pillar_chain_manager.cpp:193
std::shared_ptr< PillarBlock > getLastFinalizedPillarBlock() const
Definition pillar_chain_manager.cpp:204
uint64_t addVerifiedPillarVote(const std::shared_ptr< PillarVote > &vote)
Add a vote to the pillar votes map.
Definition pillar_chain_manager.cpp:277
std::shared_ptr< KeyManager > key_manager_
Definition pillar_chain_manager.hpp:177
PillarChainMgr class contains functionality related to pillar chain.
Definition pillar_chain_manager.hpp:31
#define LOG_OBJECTS_DEFINE
Definition logger.hpp:60
Definition app_base.hpp:23
Definition app_base.hpp:26
Definition app.hpp:16
EthBlockNumber PbftPeriod
Definition types.hpp:24
Definition hardfork.hpp:43
Definition event.hpp:48