6#ifndef ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_VBK_BLOCK_TREE_HPP_
7#define ALT_INTEGRATION_INCLUDE_VERIBLOCK_BLOCKCHAIN_VBK_BLOCK_TREE_HPP_
9#include <unordered_map>
12#include "veriblock/pop/blockchain/blocktree.hpp"
13#include "veriblock/pop/blockchain/commands/vbk_command_group_store.hpp"
14#include "veriblock/pop/blockchain/pop/fork_resolution.hpp"
15#include "veriblock/pop/blockchain/pop/pop_state_machine.hpp"
16#include "veriblock/pop/blockchain/vbk_block_addon.hpp"
17#include "veriblock/pop/blockchain/vbk_chain_params.hpp"
18#include "veriblock/pop/entities/btcblock.hpp"
19#include "veriblock/pop/finalizer.hpp"
20#include "veriblock/pop/storage/block_reader.hpp"
25inline void BaseBlockTree<BtcBlock>::decreaseAppliedBlockCount(
size_t) {
31extern template struct BlockIndex<BtcBlock>;
32extern template struct BlockTree<BtcBlock, BtcChainParams>;
33extern template struct BaseBlockTree<BtcBlock>;
34extern template struct BlockIndex<VbkBlock>;
35extern template struct BlockTree<VbkBlock, VbkChainParams>;
36extern template struct BaseBlockTree<VbkBlock>;
98 using index_t = VbkTree::index_t;
99 using stored_index_t = VbkTree::stored_index_t;
100 using payloads_t =
typename index_t::payloads_t;
101 using pid_t =
typename payloads_t::id_t;
102 using endorsement_t =
typename index_t::endorsement_t;
104 using PopForkComparator = PopAwareForkResolutionComparator<
VbkBlock,
126 BtcTree& btc() {
return cmp_.getProtectingBlockTree(); }
127 const BtcTree& btc()
const {
return cmp_.getProtectingBlockTree(); }
129 PopForkComparator& getComparator() {
return cmp_; }
130 const PopForkComparator& getComparator()
const {
return cmp_; }
132 VbkCommandGroupStore& getCommandGroupStore() {
return commandGroupStore_; }
134 const VbkCommandGroupStore& getCommandGroupStore()
const {
135 return commandGroupStore_;
138 bool loadTip(
const hash_t& hash, ValidationState& state)
override;
141 uint32_t estimateMissedNumberOfVTBs(
const index_t& index)
const;
150 const std::vector<payloads_t>& payloads,
153 void removePayloads(
const hash_t& hash,
const std::vector<pid_t>& pids);
154 void removePayloads(index_t& index,
const std::vector<pid_t>& pids);
169 void unsafelyRemovePayload(index_t& index,
171 bool shouldDetermineBestChain =
true);
173 std::string toPrettyString(
size_t level = 0)
const;
175 using base::setState;
178 void overrideTip(index_t& to)
override;
180 void finalizeBlocks();
183 bool loadBlockInner(
const stored_index_t& index,
187 bool validateBTCContext(
const payloads_t& vtb,
ValidationState& state);
196 bool addPayloadToAppliedBlock(index_t& index,
197 const payloads_t& payload,
200 void determineBestChain(index_t& candidate,
ValidationState& state)
override;
202 PopForkComparator cmp_;
216void assertBlockSanity(
const VbkBlock& block);
220std::vector<CommandGroup> payloadsToCommandGroups(
222 const std::vector<VTB>& pop,
223 const std::vector<uint8_t>& containinghash);
229 const std::vector<uint8_t>& containingHash,
230 std::vector<CommandPtr>& cmds);
233template <
typename JsonValue>
235 auto obj = json::makeEmptyObject<JsonValue>();
236 json::putStringKV(obj,
"chainWork", i.chainWork.toHex());
238 std::vector<uint256> endorsements;
239 for (
const auto& e : i.getContainingEndorsements()) {
240 endorsements.push_back(e.first);
242 json::putArrayKV(obj,
"containingEndorsements", endorsements);
244 std::vector<uint256> endorsedBy;
245 for (
const auto* e : i.getEndorsedBy()) {
246 endorsedBy.push_back(e->id);
248 json::putArrayKV(obj,
"endorsedBy", endorsedBy);
249 json::putIntKV(obj,
"height", i.getHeight());
250 json::putKV(obj,
"header", ToJSON<JsonValue>(i.getHeader()));
251 json::putIntKV(obj,
"status", i.getStatus());
252 json::putIntKV(obj,
"altrefs", i.refCount());
254 auto stored = json::makeEmptyObject<JsonValue>();
255 json::putArrayKV(stored,
"vtbids", i.getPayloadIds<
VTB>());
256 json::putKV(obj,
"stored", stored);
258 auto bopEndorsements = json::makeEmptyArray<JsonValue>();
259 for (
const auto* e : i.getBlockOfProofEndorsement()) {
263 json::arrayPushBack(bopEndorsements, ToJSON<JsonValue>(e->getId()));
265 json::putKV(obj,
"blockOfProofEndorsements", bopEndorsements);
271template <
typename JsonValue>
273 auto obj = json::makeEmptyObject<JsonValue>();
274 json::putStringKV(obj,
"chainWork", i.chainWork.toHex());
275 json::putIntKV(obj,
"height", i.getHeight());
276 json::putKV(obj,
"header", ToJSON<JsonValue>(i.getHeader()));
277 json::putIntKV(obj,
"status", i.getStatus());
278 json::putArrayKV(obj,
"vbkrefs", i.getRefs());
280 auto bopEndorsements = json::makeEmptyArray<JsonValue>();
281 for (
const auto* e : i.getBlockOfProofEndorsement()) {
285 json::arrayPushBack(bopEndorsements, ToJSON<JsonValue>(e->getId()));
287 json::putKV(obj,
"blockOfProofEndorsements", bopEndorsements);
307 const hash_t& h)
const {
309 return h.template trimLE<prev_block_hash_t::size()>();
314 *os << tree.toPrettyString();
Class that is used for storing validation state.
BlockTree< BtcBlock, BtcChainParams > BtcBlockTree
Bitcoin tree.
void PrintTo(const ArithUint256 &uint, ::std::ostream *os)
custom gtest printer, which prints Blob of any size as hexstring @ private
Base block tree that stores all blocks, maintains tree tips, maintains active chain.
Contiguous byte array of fixed size.
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.
Veriblock to Bitcoin publication, committed to Veriblock blockchain in containingBlock.
void unsafelyRemovePayload(const Blob< 24 > &hash, const pid_t &pid)
If we add payloads to the VBK tree in the following order: A1, B2, A3.
bool loadBlockForward(const stored_index_t &index, bool fast_load, ValidationState &state) override
efficiently connect index to current tree as a leaf, loaded from disk
bool addPayloads(const VbkBlock::hash_t &hash, const std::vector< payloads_t > &payloads, ValidationState &state)
Attempts to add payloads to the block and perform full validation.
VeriBlock chain parameters.
A wrapper for the payload store that constructs command objects.