Bitcoin ABC 0.30.5
P2P Digital Currency
compactproofs.h
Go to the documentation of this file.
1// Copyright (c) 2022 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_AVALANCHE_COMPACTPROOFS_H
6#define BITCOIN_AVALANCHE_COMPACTPROOFS_H
7
8#include <avalanche/proof.h>
10
11#include <radix.h>
12#include <random.h>
13#include <serialize.h>
14#include <shortidprocessor.h>
15
16#include <cstdint>
17#include <ios>
18#include <limits>
19#include <utility>
20#include <vector>
21
22namespace avalanche {
23
24namespace {
25 struct TestCompactProofs;
26}
27
28struct ProofId;
29
31 // Used as an offset since last prefilled proof in CompactProofs
32 uint32_t index;
34
35 template <typename Stream> void SerData(Stream &s) { s << proof; }
36 template <typename Stream> void UnserData(Stream &s) { s >> proof; }
37};
38
40 uint32_t getIndex(const PrefilledProof &pp) const { return pp.index; }
41 ProofRef getItem(const PrefilledProof &pp) const { return pp.proof; }
42};
43
45 bool operator()(const ProofRef &lhs, const ProofRef &rhs) const {
46 return lhs->getId() == rhs->getId();
47 }
48};
49
53
55private:
57 std::vector<uint64_t> shortproofids;
58 std::vector<PrefilledProof> prefilledProofs;
59
60public:
61 static constexpr int SHORTPROOFIDS_LENGTH = 6;
62
64 : shortproofidk0(GetRand<uint64_t>()),
65 shortproofidk1(GetRand<uint64_t>()) {}
67
68 uint64_t getShortID(const ProofId &proofid) const;
69
70 size_t size() const {
71 return shortproofids.size() + prefilledProofs.size();
72 }
73 std::pair<uint64_t, uint64_t> getKeys() const {
74 return std::make_pair(shortproofidk0, shortproofidk1);
75 }
76 const std::vector<PrefilledProof> &getPrefilledProofs() const {
77 return prefilledProofs;
78 }
79 const std::vector<uint64_t> &getShortIDs() const { return shortproofids; }
80
83 obj.shortproofidk0, obj.shortproofidk1,
85 obj.shortproofids),
87 obj.prefilledProofs));
88
89 if (ser_action.ForRead() && obj.prefilledProofs.size() > 0) {
90 // Thanks to the DifferenceFormatter, the index values in the
91 // deserialized prefilled proofs are absolute and sorted, so the
92 // last vector item has the highest index value.
93 uint64_t highestPrefilledIndex = obj.prefilledProofs.back().index;
94
95 // Make sure the indexes do not overflow 32 bits.
96 if (highestPrefilledIndex + obj.shortproofids.size() >
97 std::numeric_limits<uint32_t>::max()) {
98 throw std::ios_base::failure("indexes overflowed 32 bits");
99 }
100
101 // Make sure the indexes are contiguous. E.g. if there is no shortid
102 // but 2 prefilled proofs with absolute indexes 0 and 2, then the
103 // proof at index 1 cannot be recovered.
104 if (highestPrefilledIndex >= obj.size()) {
105 throw std::ios_base::failure("non contiguous indexes");
106 }
107 }
108 }
109
110private:
111 friend struct ::avalanche::TestCompactProofs;
112};
113
115public:
116 std::vector<uint32_t> indices;
117
120 }
121};
122
123} // namespace avalanche
124
125#endif // BITCOIN_AVALANCHE_COMPACTPROOFS_H
SERIALIZE_METHODS(CompactProofs, obj)
Definition: compactproofs.h:81
std::vector< PrefilledProof > prefilledProofs
Definition: compactproofs.h:58
const std::vector< PrefilledProof > & getPrefilledProofs() const
Definition: compactproofs.h:76
uint64_t getShortID(const ProofId &proofid) const
std::pair< uint64_t, uint64_t > getKeys() const
Definition: compactproofs.h:73
const std::vector< uint64_t > & getShortIDs() const
Definition: compactproofs.h:79
std::vector< uint64_t > shortproofids
Definition: compactproofs.h:57
static constexpr int SHORTPROOFIDS_LENGTH
Definition: compactproofs.h:61
std::vector< uint32_t > indices
SERIALIZE_METHODS(ProofsRequest, obj)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition: random.h:85
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:574
#define READWRITE(...)
Definition: serialize.h:166
Serialization wrapper class for custom integers and enums.
Definition: serialize.h:606
Formatter to serialize/deserialize vector elements using another formatter.
Definition: serialize.h:718
void SerData(Stream &s)
Definition: compactproofs.h:35
void UnserData(Stream &s)
Definition: compactproofs.h:36
avalanche::ProofRef proof
Definition: compactproofs.h:33
bool operator()(const ProofRef &lhs, const ProofRef &rhs) const
Definition: compactproofs.h:45
ProofRef getItem(const PrefilledProof &pp) const
Definition: compactproofs.h:41
uint32_t getIndex(const PrefilledProof &pp) const
Definition: compactproofs.h:40