Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251/** Default constructor is deleted. */252Minisketch() noexcept = delete;254/** Move constructor. */255Minisketch(Minisketch&&) noexcept = default;257/** Move assignment. */258Minisketch& operator=(Minisketch&&) noexcept = default;260/** Construct a Minisketch object with the specified parameters.261*262* If bits is not BitsSupported(), or the combination of bits and capacity is not263* ImplementationSupported(), or OOM occurs internally, an invalid Minisketch264* object will be constructed. Use operator bool() to check that this isn't the265* case before performing any other operations. */266Minisketch(uint32_t bits, uint32_t implementation, size_t capacity) noexcept267{268m_minisketch = std::unique_ptr<minisketch, Deleter>(minisketch_create(bits, implementation, capacity));269}271/** Create a Minisketch object sufficiently large for the specified number of elements at given fpbits.272* It may construct an invalid object, which you may need to check for. */273static Minisketch CreateFP(uint32_t bits, uint32_t implementation, size_t max_elements, uint32_t fpbits) noexcept274{275return Minisketch(bits, implementation, ComputeCapacity(bits, max_elements, fpbits));276}278/** Return the field size for a (valid) Minisketch object. */279uint32_t GetBits() const noexcept { return minisketch_bits(m_minisketch.get()); }281/** Return the capacity for a (valid) Minisketch object. */282size_t GetCapacity() const noexcept { return minisketch_capacity(m_minisketch.get()); }284/** Return the implementation number for a (valid) Minisketch object. */285uint32_t GetImplementation() const noexcept { return minisketch_implementation(m_minisketch.get()); }287/** Set the seed for a (valid) Minisketch object. See minisketch_set_seed(). */288Minisketch& SetSeed(uint64_t seed) noexcept289{290minisketch_set_seed(m_minisketch.get(), seed);291return *this;292}294/** Add (or remove, if already present) an element to a (valid) Minisketch object.295* See minisketch_add_uint64(). */296Minisketch& Add(uint64_t element) noexcept297{298minisketch_add_uint64(m_minisketch.get(), element);299return *this;300}302/** Merge sketch into *this; both have to be valid Minisketch objects.303* See minisketch_merge for details. */304Minisketch& Merge(const Minisketch& sketch) noexcept305{306minisketch_merge(m_minisketch.get(), sketch.m_minisketch.get());307return *this;308}310/** Decode this (valid) Minisketch object into the result vector, up to as many elements as the311* vector's size permits. */312bool Decode(std::vector<uint64_t>& result) const313{314ssize_t ret = minisketch_decode(m_minisketch.get(), result.size(), result.data());315if (ret == -1) return false;316result.resize(ret);317return true;318}320/** Get the serialized size in bytes for this (valid) Minisketch object.. */321size_t GetSerializedSize() const noexcept { return minisketch_serialized_size(m_minisketch.get()); }323/** Serialize this (valid) Minisketch object as a byte vector. */324std::vector<unsigned char> Serialize() const325{326std::vector<unsigned char> result(GetSerializedSize());327minisketch_serialize(m_minisketch.get(), result.data());328return result;329}331/** Deserialize into this (valid) Minisketch from an object containing its bytes (which has data()332* and size() members). */333template<typename T>334Minisketch& Deserialize(335const T& obj,336typename std::enable_if<337std::is_convertible<typename std::remove_pointer<decltype(obj.data())>::type (*)[], const unsigned char (*)[]>::value &&338std::is_convertible<decltype(obj.size()), std::size_t>::value,339std::nullptr_t340>::type = nullptr) noexcept341{342assert(GetSerializedSize() == obj.size());343minisketch_deserialize(m_minisketch.get(), obj.data());344return *this;345}347#if __cplusplus >= 201703L348/** C++17 only: like Decode(), but up to a specified number of elements into an optional vector. */349std::optional<std::vector<uint64_t>> Decode(size_t max_elements) const350{351std::vector<uint64_t> result(max_elements);352ssize_t ret = minisketch_decode(m_minisketch.get(), max_elements, result.data());353if (ret == -1) return {};354result.resize(ret);355return result;356}358/** C++17 only: similar to Decode(), but with specified false positive probability. */359std::optional<std::vector<uint64_t>> DecodeFP(uint32_t fpbits) const360{361return Decode(ComputeMaxElements(GetBits(), GetCapacity(), fpbits));362}363#endif // __cplusplus >= 201703L364};365#endif // __cplusplus >= 201103L366#endif // __cplusplus368#endif // _MINISKETCH_H_
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.