6#ifndef ALTINTEGRATION_ADDENDORSEMENT_HPP
7#define ALTINTEGRATION_ADDENDORSEMENT_HPP
9#include <veriblock/pop/blockchain/alt_chain_params.hpp>
10#include <veriblock/pop/blockchain/block_index.hpp>
11#include <veriblock/pop/blockchain/btc_chain_params.hpp>
12#include <veriblock/pop/blockchain/chain.hpp>
13#include <veriblock/pop/blockchain/command.hpp>
14#include <veriblock/pop/blockchain/vbk_chain_params.hpp>
15#include <veriblock/pop/entities/altblock.hpp>
16#include <veriblock/pop/entities/endorsements.hpp>
17#include <veriblock/pop/fmt.hpp>
22template <
typename ProtectingTree,
typename ProtectedTree>
23struct AddEndorsement :
public Command {
24 using hash_t =
typename ProtectedTree::hash_t;
25 using protected_block_t =
typename ProtectedTree::block_t;
26 using protecting_block_t =
typename ProtectingTree::block_t;
27 using endorsement_t =
typename ProtectedTree::index_t::endorsement_t;
28 using protected_index_t =
typename ProtectedTree::index_t;
30 ~AddEndorsement()
override =
default;
32 explicit AddEndorsement(ProtectingTree& ing,
34 std::shared_ptr<endorsement_t> e)
35 : ing_(&ing), ed_(&ed), e_(std::move(e)) {}
37 bool Execute(ValidationState& state)
noexcept override {
38 auto* containing = ed_->getBlockIndex(e_->containingHash);
39 if (containing ==
nullptr) {
41 protected_block_t::name() +
"-no-containing",
42 format(
"Can not find containing block in endorsement={}",
43 e_->toPrettyString()));
46 auto* endorsed = ed_->getBlockIndex(e_->endorsedHash);
47 if (endorsed ==
nullptr) {
48 return state.Invalid(protected_block_t::name() +
"-no-endorsed-block",
49 format(
"Endorsed block={} not found in the tree",
50 HexStr(e_->endorsedHash)));
53 auto actualEndorsed = containing->getAncestor(endorsed->getHeight());
54 if (actualEndorsed ==
nullptr || endorsed != actualEndorsed) {
56 protected_block_t::name() +
"-block-differs",
57 format(
"Endorsed block is on a different chain. Expected: {}, got {}",
58 endorsed->toShortPrettyString(),
59 (actualEndorsed ? actualEndorsed->toShortPrettyString()
63 if (containing->getHeight() - endorsed->getHeight() >
64 (
int)ed_->getParams().getEndorsementSettlementInterval()) {
65 return state.Invalid(protected_block_t::name() +
"-expired",
66 "Endorsement expired");
69 auto* blockOfProof = ing_->getBlockIndex(e_->blockOfProof);
70 if (blockOfProof ==
nullptr) {
72 protected_block_t::name() +
"-block-of-proof-not-found",
73 format(
"Can not find block of proof in SP Chain ({})",
74 HexStr(e_->blockOfProof)));
77 containing->insertContainingEndorsement(e_);
78 endorsed->insertEndorsedBy(e_.get());
79 blockOfProof->insertBlockOfProofEndorsement(e_.get());
84 void UnExecute() noexcept
override {
85 auto* containing = ed_->getBlockIndex(e_->containingHash);
87 containing !=
nullptr,
88 "failed to roll back AddEndorsement: the containing block does not "
90 e_->toPrettyString());
92 auto* endorsed = ed_->getBlockIndex(e_->endorsedHash);
95 "failed to roll back AddEndorsement: the endorsed block does not "
97 e_->toPrettyString());
99 auto* blockOfProof = ing_->getBlockIndex(e_->blockOfProof);
100 VBK_ASSERT_MSG(blockOfProof !=
nullptr,
101 "failed to roll back AddEndorsement: the blockOfProof "
102 "block does not exist %s",
103 e_->toPrettyString());
108 auto Eit = containing->findContainingEndorsement(e_->id);
109 VBK_ASSERT_MSG(Eit != containing->getContainingEndorsements().end(),
110 "state corruption: containing endorsement not found");
113 bool p1 = endorsed->eraseLastFromEndorsedBy(Eit->second.get());
115 "Failed to remove endorsement %s from endorsedBy in "
116 "AddEndorsement::Unexecute",
117 e_->toPrettyString());
121 blockOfProof->eraseLastFromBlockOfProofEndorsement(Eit->second.get());
123 "Failed to remove endorsement %s from blockOfProof in "
124 "AddEndorsement::Unexecute",
125 e_->toPrettyString());
128 containing->removeContainingEndorsement(Eit);
132 ProtectingTree* ing_ =
nullptr;
133 ProtectedTree* ed_ =
nullptr;
134 std::shared_ptr<endorsement_t> e_ =
nullptr;
139template <
typename Block,
typename ChainParams>
143using AddVbkEndorsement =
144 AddEndorsement<BlockTree<BtcBlock, BtcChainParams>, VbkBlockTree>;
147using AddAltEndorsement = AddEndorsement<VbkBlockTree, AltBlockTree>;
std::string HexStr(const T itbegin, const T itend)
Convert bytes to hex.