generated from Warchant/cmake-hunter-seed
/
gossip.hpp
166 lines (126 loc) · 4.88 KB
/
gossip.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef LIBP2P_GOSSIP_HPP
#define LIBP2P_GOSSIP_HPP
#include <chrono>
#include <functional>
#include <set>
#include <string>
#include <vector>
#include <boost/optional.hpp>
#include <libp2p/common/byteutil.hpp>
#include <libp2p/multi/multiaddress.hpp>
#include <libp2p/peer/peer_id.hpp>
#include <libp2p/protocol/common/subscription.hpp>
namespace libp2p {
struct Host;
namespace basic {
class Scheduler;
}
namespace crypto {
class CryptoProvider;
namespace marshaller {
class KeyMarshaller;
}
} // namespace crypto
namespace peer {
class IdentityManager;
}
} // namespace libp2p
namespace libp2p::protocol::gossip {
/// Gossip pub-sub protocol config
struct Config {
/// Network density factors for gossip meshes
size_t D_min = 5;
size_t D_max = 10;
/// Ideal number of connected peers to support the network
size_t ideal_connections_num = 100;
/// Maximum number of simultaneous connections after which new
/// incoming peers will be rejected
size_t max_connections_num = 1000;
/// Forward messages to all subscribers not in mesh
/// (floodsub mode compatibility)
bool floodsub_forward_mode = false;
/// Forward local message to local subscribers
bool echo_forward_mode = false;
/// Read or write timeout per whole network operation
std::chrono::milliseconds rw_timeout_msec{std::chrono::seconds(10)};
/// Lifetime of a message in message cache
std::chrono::milliseconds message_cache_lifetime_msec{
std::chrono::minutes(2)};
/// Topic's message seen cache lifetime
std::chrono::milliseconds seen_cache_lifetime_msec{
message_cache_lifetime_msec * 3 / 4};
/// Topic's seen cache limit
unsigned seen_cache_limit = 100;
/// Heartbeat interval
std::chrono::milliseconds heartbeat_interval_msec{1000};
/// Ban interval between dial attempts to peer
std::chrono::milliseconds ban_interval_msec{std::chrono::minutes(1)};
/// Max number of dial attempts before peer is forgotten
unsigned max_dial_attempts = 3;
/// Expiration of gossip peers' addresses in address repository
std::chrono::milliseconds address_expiration_msec{std::chrono::hours(1)};
/// Max RPC message size
size_t max_message_size = 1 << 24;
/// Protocol version
std::string protocol_version = "/meshsub/1.0.0";
/// Sign published messages
bool sign_messages = false;
};
using common::ByteArray;
using TopicId = std::string;
using TopicList = std::vector<TopicId>;
using TopicSet = std::set<TopicId>;
/// Gossip protocol interface
class Gossip {
public:
virtual ~Gossip() = default;
/// Adds bootstrap peer to the set of connectable peers
virtual void addBootstrapPeer(
const peer::PeerId &id,
boost::optional<multi::Multiaddress> address) = 0;
/// Adds bootstrap peer address in string form
virtual outcome::result<void> addBootstrapPeer(
const std::string &address) = 0;
/// Starts client and server
virtual void start() = 0;
/// Stops client and server
virtual void stop() = 0;
/// Message received on subscription.
/// Temporary struct of fields the subscriber may store if they want
struct Message {
const ByteArray &from;
const TopicId &topic;
const ByteArray &data;
};
/// Validator of messages arriving from the wire
using Validator =
std::function<bool(const ByteArray &from, const ByteArray &data)>;
/// Sets message validator for topic
virtual void setValidator(const TopicId &topic, Validator validator) = 0;
/// Creates unique message ID out of message fields
using MessageIdFn = std::function<ByteArray(
const ByteArray &from, const ByteArray &seq, const ByteArray &data)>;
/// Sets message ID funtion that differs from default (from+sec_no)
virtual void setMessageIdFn(MessageIdFn fn) = 0;
/// Empty message means EOS (end of subscription data stream)
using SubscriptionData = boost::optional<const Message &>;
using SubscriptionCallback = std::function<void(SubscriptionData)>;
/// Subscribes to topics
virtual Subscription subscribe(TopicSet topics,
SubscriptionCallback callback) = 0;
/// Publishes to topics. Returns false if validation fails or not started
virtual bool publish(TopicId topic, ByteArray data) = 0;
};
// Creates Gossip object
std::shared_ptr<Gossip> create(
std::shared_ptr<basic::Scheduler> scheduler, std::shared_ptr<Host> host,
std::shared_ptr<peer::IdentityManager> idmgr,
std::shared_ptr<crypto::CryptoProvider> crypto_provider,
std::shared_ptr<crypto::marshaller::KeyMarshaller> key_marshaller,
Config config = Config{});
} // namespace libp2p::protocol::gossip
#endif // LIBP2P_GOSSIP_HPP