Debian Codegraph @

src/net/dandelionpp.h

raw symbols SCIP
1 // Copyright (c) 2019-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #pragma once
30
31 #include <boost/uuid/uuid.hpp>
32 #include <cstddef>
33 #include <memory>
34 #include <utility>
35 #include <vector>
36
37 #include "span.h"
38
39 namespace net
40 {
41 namespace dandelionpp
42 {
43 //! Assists with mapping source -> stem and tracking connections for stem.
44 class connection_map
45 {
46 // Make sure to update clone method if changing members
47 std::vector<boost::uuids::uuid> out_mapping_; //<! Current outgoing uuid connection at index.
48 std::vector<std::pair<boost::uuids::uuid, std::size_t>> in_mapping_; //<! uuid source to an `out_mapping_` index.
49 std::vector<std::size_t> usage_count_;
50
51 // Use clone method to prevent "hidden" copies.
52 connection_map(const connection_map&) = default;
53
54 public:
55 using value_type = boost::uuids::uuid;
56 using size_type = std::vector<boost::uuids::uuid>::size_type;
57 using difference_type = std::vector<boost::uuids::uuid>::difference_type;
58 using reference = const boost::uuids::uuid&;
59 using const_reference = reference;
60 using iterator = std::vector<boost::uuids::uuid>::const_iterator;
61 using const_iterator = iterator;
62
63 //! Initialized with zero stem connections.
64 explicit connection_map()
65 : connection_map(std::vector<boost::uuids::uuid>{}, 0)
66 {}
67
68 //! Initialized with `out_connections` and `stem_count`.
69 explicit connection_map(std::vector<boost::uuids::uuid> out_connections, std::size_t stems);
70
71 connection_map(connection_map&&) = default;
72 ~connection_map() noexcept;
73 connection_map& operator=(connection_map&&) = default;
74 connection_map& operator=(const connection_map&) = delete;
75
76 //! \return An exact duplicate of `this` map.
77 connection_map clone() const;
78
79 //! \return First stem connection.
80 const_iterator begin() const noexcept
81 {
82 return out_mapping_.begin();
83 }
84
85 //! \return One-past the last stem connection.
86 const_iterator end() const noexcept
87 {
88 return out_mapping_.end();
89 }
90
91 /*! Merges in current connections with the previous set of connections.
92 If a connection died, a new one will take its place in the stem or
93 the stem is marked as dead.
94
95 \param connections Current outbound connection ids.
96 \return True if any updates to `get_connections()` was made. */
97 bool update(std::vector<boost::uuids::uuid> current);
98
99 //! \return Number of outgoing connections in use.
100 std::size_t size() const noexcept;
101
102 //! \return Current stem mapping for `source` or `nil_uuid()` if none is possible.
103 boost::uuids::uuid get_stem(const boost::uuids::uuid& source);
104 };
105 } // dandelionpp
106 } // net