veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
alt-util.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_UTIL_HPP
7#define ALT_INTEGRATION_UTIL_HPP
8
9#include <algorithm>
10#include <cstddef>
11#include <cstdint>
12#include <limits>
13#include <vector>
14
15#include "hashutil.hpp"
16#include "keystone_util.hpp"
17#include "veriblock/pop/blockchain/alt_block_tree.hpp"
18#include "veriblock/pop/blockchain/block_index.hpp"
19#include "veriblock/pop/blockchain/blocktree.hpp"
20#include "veriblock/pop/entities/atv.hpp"
21#include "veriblock/pop/entities/context_info_container.hpp"
22#include "veriblock/pop/entities/popdata.hpp"
23#include "veriblock/pop/entities/publication_data.hpp"
24#include "veriblock/pop/entities/vbkblock.hpp"
25#include "veriblock/pop/entities/vtb.hpp"
26#include "veriblock/pop/uint.hpp"
27#include "veriblock/pop/validation_state.hpp"
28
29namespace altintegration {
30class ValidationState;
31struct AltBlock;
32struct AltBlockTree;
33struct AltChainParams;
34struct AuthenticatedContextInfoContainer;
35struct ContextInfoContainer;
36struct PopData;
37template <typename Block, typename ChainParams>
38struct BlockTree;
39
42template <typename BlockTreeT>
43std::vector<typename BlockTreeT::block_t> getContext(
44 const BlockTreeT& tree,
45 typename BlockTreeT::hash_t tip,
46 size_t size = std::numeric_limits<size_t>::max()) {
47 std::vector<typename BlockTreeT::block_t> ret;
48 auto* cursor = tree.getBlockIndex(tip);
49 size_t i = 0;
50 while (cursor != nullptr && i++ < size) {
51 ret.push_back(cursor->getHeader());
52 cursor = cursor->pprev;
53 }
54 return ret;
55}
56
58template <typename BlockTree>
59std::vector<std::vector<uint8_t>> getLastKnownBlocks(const BlockTree& tree,
60 size_t size) {
61 std::vector<std::vector<uint8_t>> ret;
62 ret.reserve(size);
63
64 auto* tip = tree.getBestChain().tip();
65 for (size_t i = 0; i < size && tip != nullptr; i++) {
66 ret.push_back(tip->getHash().asVector());
67 tip = tip->pprev;
68 }
69
70 // reverse them since we add them in reverse order (tip -> genesis)
71 std::reverse(ret.begin(), ret.end());
72 return ret;
73}
74
76template <typename Block, typename ChainParams>
78 const std::vector<std::vector<uint8_t>>& blocks,
79 ValidationState& state) {
80 for (const auto& b : blocks) {
81 Block block = Block::fromRaw(b);
82 if (!tree.acceptBlockHeader(block, state)) {
83 return false;
84 }
85 }
86
87 return true;
88}
89
102bool GeneratePublicationData(const std::vector<uint8_t>& endorsedBlockHeader,
103 const std::vector<uint8_t>& txMerkleRoot,
104 const PopData& popData,
105 const std::vector<uint8_t>& payoutInfo,
106 const AltBlockTree& tree,
107 PublicationData& out);
108
111 const std::vector<uint8_t>& endorsedBlockHeader,
112 const BlockIndex<AltBlock>& endorsedBlock,
113 const std::vector<uint8_t>& txMerkleRoot,
114 const PopData& popData,
115 const std::vector<uint8_t>& payoutInfo,
116 const AltChainParams& params);
117
120 const std::vector<uint8_t>& endorsedBlockHeader,
121 const BlockIndex<AltBlock>& endorsedBlock,
122 const std::vector<uint8_t>& txMerkleRoot,
123 uint32_t version,
124 const std::vector<ATV::id_t>& atv_ids,
125 const std::vector<VTB::id_t>& vtb_ids,
126 const std::vector<VbkBlock::id_t>& vbk_ids,
127 const std::vector<uint8_t>& payoutInfo,
128 const AltChainParams& params);
129
132uint256 CalculateTopLevelMerkleRoot(const std::vector<uint8_t>& txMerkleRoot,
133 const uint256& popDataMerkleRoot,
134 const ContextInfoContainer& ctx);
135
139
141uint256 CalculateTopLevelMerkleRoot(const std::vector<uint8_t>& txMerkleRoot,
142 const PopData& popData,
143 const BlockIndex<AltBlock>* prevBlock,
144 const AltChainParams& params);
145
154int getMaxAtvsInVbkBlock(uint64_t altchainId);
155
156} // namespace altintegration
157
158#endif // ALT_INTEGRATION_UTIL_HPP
Class that is used for storing validation state.
Defines logging helpers.
Definition: block.hpp:14
uint256 CalculateTopLevelMerkleRoot(const std::vector< uint8_t > &txMerkleRoot, const uint256 &popDataMerkleRoot, const ContextInfoContainer &ctx)
calculates top level merkle root that cryptographically authenticates block content (transactions,...
bool addBlocks(BlockTree< Block, ChainParams > &tree, const std::vector< std::vector< uint8_t > > &blocks, ValidationState &state)
helper to accept multiple blocks
Definition: alt-util.hpp:77
bool GeneratePublicationData(const std::vector< uint8_t > &endorsedBlockHeader, const std::vector< uint8_t > &txMerkleRoot, const PopData &popData, const std::vector< uint8_t > &payoutInfo, const AltBlockTree &tree, PublicationData &out)
Creates a PublicationData - an entity, which stores information about Altchain block endorsement.
int getMaxAtvsInVbkBlock(uint64_t altchainId)
The second to the last byte is used to determine the max number of occurrences The first 7 bits deter...
std::vector< std::vector< uint8_t > > getLastKnownBlocks(const BlockTree &tree, size_t size)
helper to get last N known block hashes
Definition: alt-util.hpp:59
std::vector< typename BlockTreeT::block_t > getContext(const BlockTreeT &tree, typename BlockTreeT::hash_t tip, size_t size=std::numeric_limits< size_t >::max())
Build a context (blocks necessary to connect) in a tree from tip backwards size elements.
Definition: alt-util.hpp:43
Represents simplified view on Altchain's block tree, maintains VBK tree and BTC tree.
Base class for all Altchain-related configs.
Contains ContextInfoContainer and merkle path which authenticates hash of ContextInfoContainer to a b...
const Chain< index_t > & getBestChain() const
Getter for currently Active Chain.
A node in a block tree.
Definition: block_index.hpp:31
BlockTree is a tree of blocks with single "bootstrap" block as root.
Definition: blocktree.hpp:30
Container of context info for endorsed block.
Represents ALT block body of POP-related info.
Definition: popdata.hpp:27
Publication data about ALT block inside VBK blockchain.