6#ifndef ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_ALT_BLOCK_TREE_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_ALT_BLOCK_TREE_HPP_
12#include <unordered_map>
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>
25#include "alt_chain_params.hpp"
26#include "base_block_tree.hpp"
28#include "pop/fork_resolution.hpp"
29#include "pop/vbk_block_tree.hpp"
33extern template struct BlockIndex<AltBlock>;
34extern template struct BaseBlockTree<AltBlock>;
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;
190 using PopForkComparator = PopAwareForkResolutionComparator<
AltBlock,
251 using id_vector_t = std::vector<uint8_t>;
254 block_index_t& block,
272 template <
typename Payload>
275 block_index_t& getBlock() {
return block_; }
279 block_index_t& block_;
280 std::unordered_set<std::vector<uint8_t>> ids_;
332 VBK_CHECK_RETURN
bool loadTip(
const hash_t& hash,
378 const AltBlock::hash_t& B);
401 using base::setState;
404 void finalizeBlocks();
411 void removePayloads(
const hash_t& hash);
415 VbkBlockTree& vbk() {
return cmp_.getProtectingBlockTree(); }
423 const PopForkComparator& getComparator()
const {
return cmp_; }
429 const PayloadsStorage& getPayloadsProvider()
const {
return payloadsProvider_; }
431 AltCommandGroupStore& getCommandGroupStore() {
return commandGroupStore_; }
433 const AltCommandGroupStore& getCommandGroupStore()
const {
return commandGroupStore_; }
438 std::string toPrettyString(
size_t level = 0)
const;
442 void overrideTip(index_t& to)
override;
444 friend struct MemPoolBlockTree;
447 using base::removeLeaf;
450 const alt_config_t& alt_config_;
451 PopForkComparator cmp_;
452 PayloadsStorage& payloadsProvider_;
453 command_group_store_t commandGroupStore_;
456 bool loadBlockInner(
const stored_index_t& index,
458 ValidationState& state);
461 void determineBestChain(index_t& candidate, ValidationState& state)
override;
464 void onBeforeLeafRemoved(
const index_t& block)
override;
466 void setPayloads(index_t& index,
const PopData& payloads);
474 bool connectBlock(index_t& index, ValidationState& state);
477 void removeAllPayloads(index_t& index);
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);
488 json::putArrayKV(obj,
"containingEndorsements", endorsements);
490 std::vector<uint256> endorsedBy;
491 for (
const auto* e : i.getEndorsedBy()) {
492 endorsedBy.push_back(e->id);
494 json::putArrayKV(obj,
"endorsedBy", endorsedBy);
495 json::putIntKV(obj,
"status", i.getStatus());
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>());
502 json::putKV(obj,
"stored", stored);
508inline void PrintTo(
const AltBlockTree& tree, std::ostream* os) {
509 *os << tree.toPrettyString();
Class that is used for storing validation state.
void PrintTo(const ArithUint256 &uint, ::std::ostream *os)
custom gtest printer, which prints Blob of any size as hexstring @ private
an incremental block builder
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.
Base class for all Altchain-related configs.
A wrapper for the payload store that constructs command objects.
An abstraction over on-disk storage iterator.
BlockTree is a tree of blocks with single "bootstrap" block as root.
base class for BTC params
Accessor for ATV/VTB/VbkBlock bodies given hash.
Represents ALT block body of POP-related info.
VeriBlock chain parameters.