veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
alt_block_tree.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_ALT_BLOCK_TREE_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_ALT_BLOCK_TREE_HPP_
8
9#include <memory>
10#include <set>
11#include <string>
12#include <unordered_map>
13#include <vector>
14#include <veriblock/pop/blockchain/commands/alt_command_group_store.hpp>
15#include <veriblock/pop/entities/altblock.hpp>
16#include <veriblock/pop/entities/btcblock.hpp>
17#include <veriblock/pop/entities/popdata.hpp>
18#include <veriblock/pop/entities/vbkblock.hpp>
19#include <veriblock/pop/fmt.hpp>
20#include <veriblock/pop/storage/block_reader.hpp>
21#include <veriblock/pop/storage/payloads_provider.hpp>
22#include <veriblock/pop/trace.hpp>
23#include <veriblock/pop/validation_state.hpp>
24
25#include "alt_chain_params.hpp"
26#include "base_block_tree.hpp"
27#include "chain.hpp"
28#include "pop/fork_resolution.hpp"
29#include "pop/vbk_block_tree.hpp"
30namespace altintegration {
31
32// defined in alt_block_tree.cpp
33extern template struct BlockIndex<AltBlock>;
34extern template struct BaseBlockTree<AltBlock>;
35
36// clang-format off
176
177// clang-format on
183 using index_t = base::index_t;
185 using endorsement_t = typename index_t::endorsement_t;
186 using eid_t = typename endorsement_t::id_t;
187 using hash_t = typename AltBlock::hash_t;
189
190 using PopForkComparator = PopAwareForkResolutionComparator<AltBlock,
194
195 ~AltBlockTree() override = default;
196
197 explicit AltBlockTree(const alt_config_t& alt_config,
198 const vbk_config_t& vbk_config,
199 const btc_config_t& btc_config,
200 PayloadsStorage& payloadsProvider,
201 BlockReader& blockProvider);
202
208 void bootstrap();
209
216 VBK_CHECK_RETURN bool acceptBlockHeader(const AltBlock& block,
217 ValidationState& state);
218
238 void acceptBlock(const hash_t& block, const PopData& payloads);
240 void acceptBlock(index_t& index,
241 const PopData& payloads,
242 ValidationState& state);
244 void acceptBlock(index_t& index, const PopData& payloads);
245
249 using tree_t = AltBlockTree;
250 using block_index_t = typename tree_t::index_t;
251 using id_vector_t = std::vector<uint8_t>;
252
254 block_index_t& block,
257
259 bool isStatefulDuplicate(const id_vector_t& payload_id);
262 bool isStatelessDuplicate(const id_vector_t& payload_id);
263
272 template <typename Payload>
273 bool add(const Payload& payload, ValidationState& state);
274
275 block_index_t& getBlock() { return block_; }
276
277 private:
278 tree_t& tree_;
279 block_index_t& block_;
280 std::unordered_set<std::vector<uint8_t>> ids_;
284 };
285
294
300 std::vector<const index_t*> getConnectedTipsAfter(const index_t& index) const;
301
303 signals::Signal<void(index_t& index, ValidationState&)>
305
307 signals::Signal<void(index_t& index)> onBlockConnected;
308
322 VBK_CHECK_RETURN bool loadBlockForward(const stored_index_t& index,
323 bool fast_load,
324 ValidationState& state) override;
325
332 VBK_CHECK_RETURN bool loadTip(const hash_t& hash,
333 ValidationState& state) override;
334
377 VBK_CHECK_RETURN int comparePopScore(const AltBlock::hash_t& A,
378 const AltBlock::hash_t& B);
379
399 VBK_CHECK_RETURN bool setState(index_t& to, ValidationState& state) override;
401 using base::setState;
402
404 void finalizeBlocks();
405
411 void removePayloads(const hash_t& hash);
412
413 // clang-format off
415 VbkBlockTree& vbk() { return cmp_.getProtectingBlockTree(); }
417 const VbkBlockTree& vbk() const { return cmp_.getProtectingBlockTree(); }
419 VbkBlockTree::BtcTree& btc() { return cmp_.getProtectingBlockTree().btc(); }
421 const VbkBlockTree::BtcTree& btc() const { return cmp_.getProtectingBlockTree().btc(); }
423 const PopForkComparator& getComparator() const { return cmp_; }
425 const AltChainParams& getParams() const { return alt_config_; }
427 PayloadsStorage& getPayloadsProvider() { return payloadsProvider_; }
429 const PayloadsStorage& getPayloadsProvider() const { return payloadsProvider_; }
431 AltCommandGroupStore& getCommandGroupStore() { return commandGroupStore_; }
433 const AltCommandGroupStore& getCommandGroupStore() const { return commandGroupStore_; }
434
435 // clang-format on
436
438 std::string toPrettyString(size_t level = 0) const;
439
442 void overrideTip(index_t& to) override;
443
444 friend struct MemPoolBlockTree;
445
447 using base::removeLeaf;
448
449 private:
450 const alt_config_t& alt_config_;
451 PopForkComparator cmp_;
452 PayloadsStorage& payloadsProvider_;
453 command_group_store_t commandGroupStore_;
454
456 bool loadBlockInner(const stored_index_t& index,
457 bool fast_load,
458 ValidationState& state);
459
461 void determineBestChain(index_t& candidate, ValidationState& state) override;
462
464 void onBeforeLeafRemoved(const index_t& block) override;
466 void setPayloads(index_t& index, const PopData& payloads);
467
474 bool connectBlock(index_t& index, ValidationState& state);
475
477 void removeAllPayloads(index_t& index);
478};
479
481template <typename JsonValue>
482JsonValue ToJSON(const BlockIndex<AltBlock>& i) {
483 auto obj = json::makeEmptyObject<JsonValue>();
484 std::vector<uint256> endorsements;
485 for (const auto& e : i.getContainingEndorsements()) {
486 endorsements.push_back(e.first);
487 }
488 json::putArrayKV(obj, "containingEndorsements", endorsements);
489
490 std::vector<uint256> endorsedBy;
491 for (const auto* e : i.getEndorsedBy()) {
492 endorsedBy.push_back(e->id);
493 }
494 json::putArrayKV(obj, "endorsedBy", endorsedBy);
495 json::putIntKV(obj, "status", i.getStatus());
496
497 auto stored = json::makeEmptyObject<JsonValue>();
498 json::putArrayKV(stored, "vbkblocks", i.getPayloadIds<VbkBlock>());
499 json::putArrayKV(stored, "vtbs", i.getPayloadIds<VTB>());
500 json::putArrayKV(stored, "atvs", i.getPayloadIds<ATV>());
501
502 json::putKV(obj, "stored", stored);
503
504 return obj;
505}
506
508inline void PrintTo(const AltBlockTree& tree, std::ostream* os) {
509 *os << tree.toPrettyString();
510}
511
512} // namespace altintegration
513
514#endif // !
Class that is used for storing validation state.
Defines logging helpers.
Definition: block.hpp:14
void PrintTo(const ArithUint256 &uint, ::std::ostream *os)
custom gtest printer, which prints Blob of any size as hexstring @ private
bool add(const Payload &payload, ValidationState &state)
Add a payload to a leaf connected block, apply the payload if the block is applied.
bool isStatefulDuplicate(const id_vector_t &payload_id)
stateful duplicate payload check as performed by connectBlock()
bool isStatelessDuplicate(const id_vector_t &payload_id)
stateless payload duplicate check functionally equivalent to checkPopDataForDuplicates
Represents simplified view on Altchain's block tree, maintains VBK tree and BTC tree.
void acceptBlock(const hash_t &block, const PopData &payloads)
Attach "block body" - PopData to block header, which already exists in AltBlockTree.
void bootstrap()
Set the very first (bootstrap) altchain block with POP enabled.
signals::Signal< void(index_t &index, ValidationState &)> onInvalidBlockConnected
a block has been handed over to the underlying tree and flagged as invalid
signals::Signal< void(index_t &index)> onBlockConnected
a block has been successfully handed over to the underlying tree
VBK_CHECK_RETURN int comparePopScore(const AltBlock::hash_t &A, const AltBlock::hash_t &B)
Efficiently compares current tip (A) and any other block (B).
VBK_CHECK_RETURN bool setState(index_t &to, ValidationState &state) override
Switch AltBlockTree from the current tip to different block, while doing all validations of intermedi...
const VbkBlockTree & vbk() const
Accessor for VBK tree.
void acceptBlock(index_t &index, const PopData &payloads, ValidationState &state)
This is an overloaded member function, provided for convenience. It differs from the above function o...
VBK_CHECK_RETURN bool loadTip(const hash_t &hash, ValidationState &state) override
After all blocks loaded, efficiently set current tip.
BlockPayloadMutator makeConnectedLeafPayloadMutator(index_t &block)
Create a session object that allows incremental payload list modification in the given block.
const AltChainParams & getParams() const
Accessor for Network Parameters stored in this tree.
const VbkBlockTree::BtcTree & btc() const
Accessor for BTC tree.
VBK_CHECK_RETURN bool loadBlockForward(const stored_index_t &index, bool fast_load, ValidationState &state) override
Efficiently connect block loaded from disk as a leaf.
void acceptBlock(index_t &index, const PopData &payloads)
This is an overloaded member function, provided for convenience. It differs from the above function o...
VBK_CHECK_RETURN bool acceptBlockHeader(const AltBlock &block, ValidationState &state)
Validate and add ALT block header to AltBlockTree.
std::vector< const index_t * > getConnectedTipsAfter(const index_t &index) const
Get all connected tips after given block.
Represents a view on Altchain block - i.e.
Definition: altblock.hpp:29
Base class for all Altchain-related configs.
A wrapper for the payload store that constructs command objects.
A node in a block tree.
Definition: block_index.hpp:31
An abstraction over on-disk storage iterator.
BlockTree is a tree of blocks with single "bootstrap" block as root.
Definition: blocktree.hpp:30
base class for BTC params
Accessor for ATV/VTB/VbkBlock bodies given hash.
Represents ALT block body of POP-related info.
Definition: popdata.hpp:27
Veriblock block tree.
VeriBlock chain parameters.