Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/compactproofs.h
| // Copyright (c) 2022 The Bitcoin developers | // Copyright (c) 2022 The Bitcoin developers | ||||
| // Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
| // file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
| #ifndef BITCOIN_AVALANCHE_COMPACTPROOFS_H | #ifndef BITCOIN_AVALANCHE_COMPACTPROOFS_H | ||||
| #define BITCOIN_AVALANCHE_COMPACTPROOFS_H | #define BITCOIN_AVALANCHE_COMPACTPROOFS_H | ||||
| #include <avalanche/proof.h> | #include <avalanche/proof.h> | ||||
| #include <avalanche/proofradixtreeadapter.h> | #include <avalanche/proofradixtreeadapter.h> | ||||
| #include <radix.h> | #include <radix.h> | ||||
| #include <random.h> | #include <random.h> | ||||
| #include <serialize.h> | #include <serialize.h> | ||||
| #include <shortidprocessor.h> | |||||
| #include <cstdint> | #include <cstdint> | ||||
| #include <ios> | #include <ios> | ||||
| #include <limits> | #include <limits> | ||||
| #include <utility> | #include <utility> | ||||
| #include <vector> | #include <vector> | ||||
| namespace avalanche { | namespace avalanche { | ||||
| namespace { | namespace { | ||||
| struct TestCompactProofs; | struct TestCompactProofs; | ||||
| } | } | ||||
| struct ProofId; | struct ProofId; | ||||
| struct PrefilledProof { | struct PrefilledProof { | ||||
| // Used as an offset since last prefilled proof in CompactProofs | // Used as an offset since last prefilled proof in CompactProofs | ||||
| uint32_t index; | uint32_t index; | ||||
| avalanche::ProofRef proof; | avalanche::ProofRef proof; | ||||
| template <typename Stream> void SerData(Stream &s) { s << proof; } | template <typename Stream> void SerData(Stream &s) { s << proof; } | ||||
| template <typename Stream> void UnserData(Stream &s) { s >> proof; } | template <typename Stream> void UnserData(Stream &s) { s >> proof; } | ||||
| }; | }; | ||||
| struct ShortIdProcessorPrefilledProofAdapter { | |||||
| uint32_t getIndex(const PrefilledProof &pp) const { return pp.index; } | |||||
| ProofRef getItem(const PrefilledProof &pp) const { return pp.proof; } | |||||
| }; | |||||
| struct ProofRefCompare { | |||||
| bool operator()(const ProofRef &lhs, const ProofRef &rhs) const { | |||||
| return lhs->getId() == rhs->getId(); | |||||
| } | |||||
| }; | |||||
| using ProofShortIdProcessor = | |||||
| ShortIdProcessor<PrefilledProof, ShortIdProcessorPrefilledProofAdapter, | |||||
| ProofRefCompare>; | |||||
| class CompactProofs { | class CompactProofs { | ||||
| private: | private: | ||||
| uint64_t shortproofidk0, shortproofidk1; | uint64_t shortproofidk0, shortproofidk1; | ||||
| std::vector<uint64_t> shortproofids; | std::vector<uint64_t> shortproofids; | ||||
| std::vector<PrefilledProof> prefilledProofs; | std::vector<PrefilledProof> prefilledProofs; | ||||
| public: | public: | ||||
| static constexpr int SHORTPROOFIDS_LENGTH = 6; | static constexpr int SHORTPROOFIDS_LENGTH = 6; | ||||
| CompactProofs() | CompactProofs() | ||||
| : shortproofidk0(GetRand(std::numeric_limits<uint64_t>::max())), | : shortproofidk0(GetRand(std::numeric_limits<uint64_t>::max())), | ||||
| shortproofidk1(GetRand(std::numeric_limits<uint64_t>::max())) {} | shortproofidk1(GetRand(std::numeric_limits<uint64_t>::max())) {} | ||||
| CompactProofs(const RadixTree<const Proof, ProofRadixTreeAdapter> &proofs); | CompactProofs(const RadixTree<const Proof, ProofRadixTreeAdapter> &proofs); | ||||
| uint64_t getShortID(const ProofId &proofid) const; | uint64_t getShortID(const ProofId &proofid) const; | ||||
| size_t size() const { | size_t size() const { | ||||
| return shortproofids.size() + prefilledProofs.size(); | return shortproofids.size() + prefilledProofs.size(); | ||||
| } | } | ||||
| std::pair<uint64_t, uint64_t> getKeys() const { | std::pair<uint64_t, uint64_t> getKeys() const { | ||||
| return std::make_pair(shortproofidk0, shortproofidk1); | return std::make_pair(shortproofidk0, shortproofidk1); | ||||
| } | } | ||||
| const std::vector<PrefilledProof> &getPrefilledProofs() const { | |||||
| return prefilledProofs; | |||||
| } | |||||
| const std::vector<uint64_t> &getShortIDs() const { return shortproofids; } | |||||
| SERIALIZE_METHODS(CompactProofs, obj) { | SERIALIZE_METHODS(CompactProofs, obj) { | ||||
| READWRITE( | READWRITE( | ||||
| obj.shortproofidk0, obj.shortproofidk1, | obj.shortproofidk0, obj.shortproofidk1, | ||||
| Using<VectorFormatter<CustomUintFormatter<SHORTPROOFIDS_LENGTH>>>( | Using<VectorFormatter<CustomUintFormatter<SHORTPROOFIDS_LENGTH>>>( | ||||
| obj.shortproofids), | obj.shortproofids), | ||||
| Using<VectorFormatter<DifferentialIndexedItemFormatter>>( | Using<VectorFormatter<DifferentialIndexedItemFormatter>>( | ||||
| obj.prefilledProofs)); | obj.prefilledProofs)); | ||||
| Show All 18 Lines | SERIALIZE_METHODS(CompactProofs, obj) { | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| private: | private: | ||||
| friend struct ::avalanche::TestCompactProofs; | friend struct ::avalanche::TestCompactProofs; | ||||
| }; | }; | ||||
| class ProofsRequest { | |||||
| public: | |||||
| std::vector<uint32_t> indices; | |||||
| SERIALIZE_METHODS(ProofsRequest, obj) { | |||||
| READWRITE(Using<VectorFormatter<DifferenceFormatter>>(obj.indices)); | |||||
| } | |||||
| }; | |||||
| } // namespace avalanche | } // namespace avalanche | ||||
| #endif // BITCOIN_AVALANCHE_COMPACTPROOFS_H | #endif // BITCOIN_AVALANCHE_COMPACTPROOFS_H | ||||