veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
stateless_validation.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_VERIBLOCK_STATELESS_VALIDATION_H
7#define ALT_INTEGRATION_VERIBLOCK_STATELESS_VALIDATION_H
8
9#include "blockchain/alt_chain_params.hpp"
10#include "blockchain/btc_chain_params.hpp"
11#include "blockchain/vbk_chain_params.hpp"
12#include "crypto/secp256k1.hpp"
13#include "entities/atv.hpp"
14#include "entities/btcblock.hpp"
15#include "entities/popdata.hpp"
16#include "entities/vbkblock.hpp"
17#include "entities/vtb.hpp"
18#include "pop_stateless_validator.hpp"
19#include "time.hpp"
20#include "validation_state.hpp"
21
22namespace altintegration {
23
25bool containsSplit(const std::vector<uint8_t>& pop_data,
26 const std::vector<uint8_t>& btcTx_data,
27 ValidationState& state);
28
30template <typename MerklePathType, typename HashType1, typename HashType2>
31bool checkMerklePath(const MerklePathType& merklePath,
32 const HashType1& transactionHash,
33 const HashType2& merkleRoot,
34 ValidationState& state) {
35 if (merklePath.subject != transactionHash) {
36 return state.Invalid("invalid-merklepath",
37 "Transaction hash cannot be proven by merkle path");
38 }
39
40 auto root = merklePath.calculateMerkleRoot();
41 if (root != merkleRoot) {
42 return state.Invalid("invalid-merklepath",
43 "Wrong merkle root. Expected: " + merkleRoot.toHex() +
44 ", got: " + root.toHex());
45 }
46
47 return true;
48}
49
51bool checkSignature(const VbkTx& tx, ValidationState& state);
52
54bool checkSignature(const VbkPopTx& tx, ValidationState& state);
55
57bool checkBtcBlocks(const std::vector<BtcBlock>& btcBlocks,
58 ValidationState& state,
59 const BtcChainParams& param);
60
62bool checkVbkBlocks(const std::vector<VbkBlock>& vbkBlocks,
63 ValidationState& state,
64 const VbkChainParams& param);
65
68 ValidationState& state);
69
71bool checkProofOfWork(const BtcBlock& block, const BtcChainParams& param);
73bool checkProofOfWork(const VbkBlock& block, const VbkChainParams& param);
74
76bool checkVbkPopTx(const VbkPopTx& tx,
77 ValidationState& state,
78 const BtcChainParams& param,
79 const VbkChainParams& vbk);
80
82bool checkVbkTx(const VbkTx& tx,
83 const AltChainParams& params,
84 const VbkChainParams& vbkparams,
85 ValidationState& state);
86
89 const AltChainParams& params,
90 ValidationState& state);
91
93bool checkBlock(const BtcBlock& block,
94 ValidationState& state,
95 const BtcChainParams& params);
96
100 ValidationState& state,
101 const VbkChainParams& params);
102
104bool checkBlock(const VbkBlock& block,
105 ValidationState& state,
106 const VbkChainParams& params);
107
109bool checkATV(const ATV& atv,
110 ValidationState& state,
111 const AltChainParams& alt,
112 const VbkChainParams& vbkp);
113
115bool checkVTB(const VTB& vtb,
116 ValidationState& state,
117 const BtcChainParams& btc,
118 const VbkChainParams& vbk);
119
121template <typename P>
122bool hasDuplicateIdsOf(const std::vector<typename P::id_t>& payloadIds) {
123 std::unordered_set<typename P::id_t> ids;
124 for (const auto& id : payloadIds) {
125 bool inserted = ids.insert(id).second;
126 if (!inserted) {
127 return true;
128 }
129 }
130 return false;
131}
132
134template <typename P>
135bool hasDuplicateIdsOf(const std::vector<typename P::id_t>& payloadIds,
136 ValidationState& state) {
137 if (hasDuplicateIdsOf<P>(payloadIds)) {
138 return !state.Invalid(P::name() + "-duplicate",
139 format("duplicate {} ids", P::name()));
140 }
141 return false;
142}
143
145bool checkPopDataForDuplicates(const PopData& popData, ValidationState& state);
146
148bool checkPopData(PopValidator& validator,
149 const PopData& popData,
150 ValidationState& state);
151} // namespace altintegration
152
153#endif // ! ALT_INTEGRATION_INCLUDE_VERIBLOCK_STATELESS_VALIDATION_H
Class that is used for storing validation state.
Defines logging helpers.
Definition: block.hpp:14
bool checkVbkPopTx(const VbkPopTx &tx, ValidationState &state, const BtcChainParams &param, const VbkChainParams &vbk)
Stateless validation for VbkPopTx.
bool checkVTB(const VTB &vtb, ValidationState &state, const BtcChainParams &btc, const VbkChainParams &vbk)
Stateless validation for VTB.
bool checkProofOfWork(const BtcBlock &block, const BtcChainParams &param)
Stateless validation for a Block.
bool checkPopData(PopValidator &validator, const PopData &popData, ValidationState &state)
Stateless validation for PopData and all internal PoP payloads.
bool checkVbkBlocks(const std::vector< VbkBlock > &vbkBlocks, ValidationState &state, const VbkChainParams &param)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool checkMerklePath(const MerklePathType &merklePath, const HashType1 &transactionHash, const HashType2 &merkleRoot, ValidationState &state)
Verify merkle path.
bool checkVbkTx(const VbkTx &tx, const AltChainParams &params, const VbkChainParams &vbkparams, ValidationState &state)
Stateless validation for VbkTx.
bool checkBlock(const BtcBlock &block, ValidationState &state, const BtcChainParams &params)
Stateless validation for a single block.
bool checkVbkBlockPlausibility(const VbkBlock &block, ValidationState &state, const VbkChainParams &params)
Optimization: VBK block is not plausible if its combination of height+time do not correspond to poten...
bool checkATV(const ATV &atv, ValidationState &state, const AltChainParams &alt, const VbkChainParams &vbkp)
Stateless validation for ATV.
bool checkPublicationData(const PublicationData &pub, const AltChainParams &params, ValidationState &state)
Stateless validation for PublicationData.
bool checkBtcBlocks(const std::vector< BtcBlock > &btcBlocks, ValidationState &state, const BtcChainParams &param)
Stateless validation for a chain of Blocks.
bool checkSignature(const VbkTx &tx, ValidationState &state)
Verify signature of a transaction.
bool checkBitcoinTransactionForPoPData(const VbkPopTx &tx, ValidationState &state)
Search BtcTx for POP data.
Atlchain endorsement.
Definition: atv.hpp:36
Base class for all Altchain-related configs.
Bitcoin block.
Definition: btcblock.hpp:41
base class for BTC params
Represents ALT block body of POP-related info.
Definition: popdata.hpp:27
Publication data about ALT block inside VBK blockchain.
Veriblock to Bitcoin publication, committed to Veriblock blockchain in containingBlock.
Definition: vtb.hpp:37
Veriblock block.
Definition: vbkblock.hpp:32
VeriBlock chain parameters.
Veriblock POP transaction, which endorses VBK block in BTC blockchain.
Definition: vbkpoptx.hpp:42
Veriblock transaction, which endorses ALT block in VBK blockchain.
Definition: vbktx.hpp:40