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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::marker::PhantomData;

use futures::{stream, Stream};
use tracing::Span;

use cuprate_wire::BasicNodeData;

use crate::{
    client::{handshaker::HandShaker, InternalPeerID},
    AddressBook, BroadcastMessage, CoreSyncSvc, NetworkZone, PeerSyncSvc, ProtocolRequestHandler,
};

mod dummy;
pub use dummy::{
    DummyAddressBook, DummyCoreSyncSvc, DummyPeerSyncSvc, DummyProtocolRequestHandler,
};

/// A [`HandShaker`] [`Service`](tower::Service) builder.
///
/// This builder applies default values to make usage easier, behaviour and drawbacks of the defaults are documented
/// on the `with_*` method to change it, for example [`HandshakerBuilder::with_protocol_request_handler`].
///
/// If you want to use any network other than [`Mainnet`](crate::Network::Mainnet)
/// you will need to change the core sync service with [`HandshakerBuilder::with_core_sync_svc`],
/// see that method for details.
#[derive(Debug, Clone)]
pub struct HandshakerBuilder<
    N: NetworkZone,
    AdrBook = DummyAddressBook,
    CSync = DummyCoreSyncSvc,
    PSync = DummyPeerSyncSvc,
    ProtoHdlr = DummyProtocolRequestHandler,
    BrdcstStrmMkr = fn(
        InternalPeerID<<N as NetworkZone>::Addr>,
    ) -> stream::Pending<BroadcastMessage>,
> {
    /// The address book service.
    address_book: AdrBook,
    /// The core sync data service.
    core_sync_svc: CSync,
    /// The peer sync service.
    peer_sync_svc: PSync,
    /// The protocol request service.
    protocol_request_svc: ProtoHdlr,
    /// Our [`BasicNodeData`]
    our_basic_node_data: BasicNodeData,
    /// A function that returns a stream that will give items to be broadcast by a connection.
    broadcast_stream_maker: BrdcstStrmMkr,
    /// The [`Span`] that will set as the parent to the connection [`Span`].
    connection_parent_span: Option<Span>,

    /// The network zone.
    _zone: PhantomData<N>,
}

impl<N: NetworkZone> HandshakerBuilder<N> {
    /// Creates a new builder with our node's basic node data.
    pub fn new(our_basic_node_data: BasicNodeData) -> Self {
        Self {
            address_book: DummyAddressBook,
            core_sync_svc: DummyCoreSyncSvc::static_mainnet_genesis(),
            peer_sync_svc: DummyPeerSyncSvc,
            protocol_request_svc: DummyProtocolRequestHandler,
            our_basic_node_data,
            broadcast_stream_maker: |_| stream::pending(),
            connection_parent_span: None,
            _zone: PhantomData,
        }
    }
}

impl<N: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
    HandshakerBuilder<N, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
{
    /// Changes the address book to the provided one.
    ///
    /// ## Default Address Book
    ///
    /// The default address book is used if this function is not called.
    ///
    /// The default address book's only drawback is that it does not keep track of peers and therefore
    /// bans.
    pub fn with_address_book<NAdrBook>(
        self,
        new_address_book: NAdrBook,
    ) -> HandshakerBuilder<N, NAdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
    where
        NAdrBook: AddressBook<N> + Clone,
    {
        let HandshakerBuilder {
            core_sync_svc,
            peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
            ..
        } = self;

        HandshakerBuilder {
            address_book: new_address_book,
            core_sync_svc,
            peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
        }
    }

    /// Changes the core sync service to the provided one.
    ///
    /// The core sync service should keep track of our nodes core sync data.
    ///
    /// ## Default Core Sync Service
    ///
    /// The default core sync service is used if this method is not called.
    ///
    /// The default core sync service will just use the mainnet genesis block, to use other network's
    /// genesis see [`DummyCoreSyncSvc::static_stagenet_genesis`] and [`DummyCoreSyncSvc::static_testnet_genesis`].
    /// The drawbacks to keeping this the default is that it will always return the mainnet genesis as our nodes
    /// sync info, which means peers won't know our actual chain height, this may or may not be a problem for
    /// different use cases.
    pub fn with_core_sync_svc<NCSync>(
        self,
        new_core_sync_svc: NCSync,
    ) -> HandshakerBuilder<N, AdrBook, NCSync, PSync, ProtoHdlr, BrdcstStrmMkr>
    where
        NCSync: CoreSyncSvc + Clone,
    {
        let HandshakerBuilder {
            address_book,
            peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
            ..
        } = self;

        HandshakerBuilder {
            address_book,
            core_sync_svc: new_core_sync_svc,
            peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
        }
    }

    /// Changes the peer sync service, which keeps track of peers sync states.
    ///
    /// ## Default Peer Sync Service
    ///
    /// The default peer sync service will be used if this method is not called.
    ///
    /// The default peer sync service will not keep track of peers sync states.
    pub fn with_peer_sync_svc<NPSync>(
        self,
        new_peer_sync_svc: NPSync,
    ) -> HandshakerBuilder<N, AdrBook, CSync, NPSync, ProtoHdlr, BrdcstStrmMkr>
    where
        NPSync: PeerSyncSvc<N> + Clone,
    {
        let HandshakerBuilder {
            address_book,
            core_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
            ..
        } = self;

        HandshakerBuilder {
            address_book,
            core_sync_svc,
            peer_sync_svc: new_peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
        }
    }

    /// Changes the protocol request handler, which handles [`ProtocolRequest`](crate::ProtocolRequest)s to our node.
    ///
    /// ## Default Protocol Request Handler
    ///
    /// The default protocol request handler will not respond to any protocol requests, this should not
    /// be an issue as long as peers do not think we are ahead of them, if they do they will send requests
    /// for our blocks, and we won't respond which will cause them to disconnect.
    pub fn with_protocol_request_handler<NProtoHdlr>(
        self,
        new_protocol_handler: NProtoHdlr,
    ) -> HandshakerBuilder<N, AdrBook, CSync, PSync, NProtoHdlr, BrdcstStrmMkr>
    where
        NProtoHdlr: ProtocolRequestHandler + Clone,
    {
        let HandshakerBuilder {
            address_book,
            core_sync_svc,
            peer_sync_svc,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
            ..
        } = self;

        HandshakerBuilder {
            address_book,
            core_sync_svc,
            peer_sync_svc,
            protocol_request_svc: new_protocol_handler,
            our_basic_node_data,
            broadcast_stream_maker,
            connection_parent_span,
            _zone,
        }
    }

    /// Changes the broadcast stream maker, which is used to create streams that yield messages to broadcast.
    ///
    /// ## Default Broadcast Stream Maker
    ///
    /// The default broadcast stream maker just returns [`stream::Pending`], i.e. the returned stream will not
    /// produce any messages to broadcast, this is not a problem if your use case does not require broadcasting
    /// messages.
    pub fn with_broadcast_stream_maker<NBrdcstStrmMkr, BrdcstStrm>(
        self,
        new_broadcast_stream_maker: NBrdcstStrmMkr,
    ) -> HandshakerBuilder<N, AdrBook, CSync, PSync, ProtoHdlr, NBrdcstStrmMkr>
    where
        BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
        NBrdcstStrmMkr: Fn(InternalPeerID<N::Addr>) -> BrdcstStrm + Clone + Send + 'static,
    {
        let HandshakerBuilder {
            address_book,
            core_sync_svc,
            peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            connection_parent_span,
            _zone,
            ..
        } = self;

        HandshakerBuilder {
            address_book,
            core_sync_svc,
            peer_sync_svc,
            protocol_request_svc,
            our_basic_node_data,
            broadcast_stream_maker: new_broadcast_stream_maker,
            connection_parent_span,
            _zone,
        }
    }

    /// Changes the parent [`Span`] of the connection task to the one provided.
    ///
    /// ## Default Connection Parent Span
    ///
    /// The default connection span will be [`Span::none`].
    pub fn with_connection_parent_span(self, connection_parent_span: Span) -> Self {
        Self {
            connection_parent_span: Some(connection_parent_span),
            ..self
        }
    }

    /// Builds the [`HandShaker`].
    pub fn build(self) -> HandShaker<N, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr> {
        HandShaker::new(
            self.address_book,
            self.peer_sync_svc,
            self.core_sync_svc,
            self.protocol_request_svc,
            self.broadcast_stream_maker,
            self.our_basic_node_data,
            self.connection_parent_span.unwrap_or(Span::none()),
        )
    }
}