splinter 0.6.14

Splinter is a privacy-focused platform for distributed applications that provides a blockchain-inspired networking environment for communication and transactions between organizations.
Documentation
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
// Copyright 2018-2022 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Websocket-backed implementation of the AdminServiceEventClient.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::mpsc::{sync_channel, Receiver, TryRecvError, TrySendError};
use std::sync::Arc;

use crate::admin::client::event::{
    AdminServiceEvent, AdminServiceEventClient, EventType, NextEventError, PublicKey,
};
use crate::admin::client::{
    CircuitMembers, CircuitService, ProposalCircuitSlice, ProposalSlice, VoteRecord,
};
use crate::admin::messages;
use crate::error::{InternalError, InvalidStateError};
use crate::events::{
    Igniter, ParseBytes, ParseError, Reactor, WebSocketClient, WebSocketError, WsResponse,
};
use crate::hex;
use crate::rest_api::SPLINTER_PROTOCOL_VERSION;
use crate::threading::lifecycle::ShutdownHandle;

enum WsRuntime {
    Reactor(Option<Reactor>),
    Igniter(Igniter),
}

/// Constructs a new AwcAdminServiceEventClient.
#[derive(Default)]
pub struct AwcAdminServiceEventClientBuilder {
    ws_runtime: Option<WsRuntime>,
    root_url: Option<String>,
    event_type: Option<String>,
    authorization: Option<String>,
    last_event_id: Option<u64>,
}

impl AwcAdminServiceEventClientBuilder {
    /// Constructs a new builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the event reactor to use with this client instance.
    ///
    /// This enables multiple clients to be created on the same reactor.
    pub fn with_reactor(mut self, reactor: &Reactor) -> Self {
        self.ws_runtime = Some(WsRuntime::Igniter(reactor.igniter()));
        self
    }

    /// Sets the base Splinter REST API URL.
    ///
    /// This field is required by the final AwcAdminServiceEventClient.
    pub fn with_splinter_url(mut self, splinter_url: String) -> Self {
        self.root_url = Some(splinter_url);
        self
    }

    /// Sets the event type to receive.
    ///
    /// This field is required by the final AwcAdminServiceEventClient.
    pub fn with_event_type(mut self, event_type: String) -> Self {
        self.event_type = Some(event_type);
        self
    }

    /// Sets the authorization value that will be sent with any REST API requests.
    ///
    /// This field is required by the final AwcAdminServiceEventClient.
    pub fn with_authorization(mut self, authorization: String) -> Self {
        self.authorization = Some(authorization);
        self
    }

    /// Sets the last event id.  This allows the client to start at a given event id, vs starting
    /// from the beginning of time.
    pub fn with_last_event_id(mut self, last_event_id: Option<u64>) -> Self {
        self.last_event_id = last_event_id;
        self
    }

    /// Build the runnable (but not started) AwcAdminServiceEventClient.
    ///
    /// # Errors
    ///
    /// Returns an InvalidStateError if any required fields are missing.
    pub fn build(self) -> Result<RunnableAwcAdminServiceEventClient, InvalidStateError> {
        let root_url = self
            .root_url
            .ok_or_else(|| InvalidStateError::with_message("A splinter url is required.".into()))?;
        let event_type = self
            .event_type
            .ok_or_else(|| InvalidStateError::with_message("An event type is required.".into()))?;
        let authorization = self.authorization.ok_or_else(|| {
            InvalidStateError::with_message("An authorization field is required.".into())
        })?;

        let ws_runtime = self
            .ws_runtime
            .unwrap_or_else(|| WsRuntime::Reactor(Some(Reactor::new())));
        let last_event_id = self.last_event_id;

        Ok(RunnableAwcAdminServiceEventClient {
            ws_runtime,
            root_url,
            event_type,
            authorization,
            last_event_id,
        })
    }
}

/// A configured, but not yet started AwcAdminServiceEventClient.
pub struct RunnableAwcAdminServiceEventClient {
    ws_runtime: WsRuntime,
    root_url: String,
    event_type: String,
    authorization: String,
    last_event_id: Option<u64>,
}

impl RunnableAwcAdminServiceEventClient {
    /// Starts the AwcAdminServiceEventClient.
    ///
    /// # Errors
    ///
    /// Returns an InternalError if the client is unable to start.
    pub fn run(self) -> Result<AwcAdminServiceEventClient, InternalError> {
        let Self {
            ws_runtime,
            root_url,
            event_type,
            authorization,
            last_event_id,
        } = self;

        let full_url = if let Some(id) = last_event_id.as_ref() {
            format!(
                "{}/ws/admin/register/{}?last={}",
                &root_url, &event_type, id
            )
        } else {
            format!("{}/ws/admin/register/{}", &root_url, &event_type,)
        };

        let (event_sender, event_receiver) = sync_channel(256);
        let last_event_id = Arc::new(AtomicU64::new(last_event_id.unwrap_or(0)));
        let received_id = last_event_id.clone();
        let received_sender = event_sender.clone();
        let mut ws_client = WebSocketClient::new(
            &full_url,
            &authorization,
            move |_, event: AdminServiceEvent| {
                let event_id = *event.event_id();
                match received_sender.try_send(Ok(event)) {
                    // This will block.  An async sleep would be better here, but we don't have a
                    // way of doing that, as this closure is hiding the fact that this closure is
                    // executed in a future.
                    Err(TrySendError::Full(evt)) => {
                        if received_sender.send(evt).is_err() {
                            error!("Receiver was dropped without shutting down the reactor.");
                            return WsResponse::Close;
                        }
                    }
                    Err(TrySendError::Disconnected(_evt_res)) => {
                        error!("Receiver was dropped without shutting down the reactor.");
                        return WsResponse::Close;
                    }
                    Ok(()) => (),
                }
                received_id.store(event_id, Ordering::SeqCst);
                WsResponse::Empty
            },
        );

        ws_client.header(
            "SplinterProtocolVersion",
            SPLINTER_PROTOCOL_VERSION.to_string(),
        );

        ws_client.set_reconnect(true);
        ws_client.set_reconnect_limit(10);
        ws_client.set_timeout(60);

        ws_client.on_error(move |err, _| {
            match event_sender.try_send(Err(err)) {
                // This will block.  An async sleep would be better here, but we don't have a
                // way of doing that, as this closure is hiding the fact that this closure is
                // executed in a future.
                Err(TrySendError::Full(e)) => {
                    if event_sender.send(e).is_err() {
                        error!("Receiver was dropped without shutting down the reactor.");
                    }
                }
                Err(TrySendError::Disconnected(_)) => {
                    error!("Receiver was dropped without shutting down the reactor.");
                }
                Ok(()) => (),
            }
            Ok(())
        });

        ws_client.on_reconnect(move |ws| {
            let last_seen_id = last_event_id.load(Ordering::SeqCst);
            let full_url = format!(
                "{}/ws/admin/register/{}?last={}",
                root_url, event_type, last_seen_id
            );
            ws.set_url(&full_url);
        });

        let igniter = match &ws_runtime {
            WsRuntime::Reactor(Some(reactor)) => reactor.igniter(),
            // This state cannot be reached at this point, as nothing can replace the value of this
            // option with None until the running client is shutdown.
            WsRuntime::Reactor(None) => unreachable!(),
            WsRuntime::Igniter(igniter) => igniter.clone(),
        };
        igniter
            .start_ws(&ws_client)
            .map_err(|e| InternalError::from_source(Box::new(e)))?;

        Ok(AwcAdminServiceEventClient {
            ws_runtime,
            event_receiver,
        })
    }
}

pub struct AwcAdminServiceEventClient {
    ws_runtime: WsRuntime,
    event_receiver: Receiver<Result<AdminServiceEvent, WebSocketError>>,
}

impl ShutdownHandle for AwcAdminServiceEventClient {
    fn signal_shutdown(&mut self) {
        if let WsRuntime::Reactor(Some(reactor)) = &self.ws_runtime {
            if let Err(err) = reactor.shutdown_signaler().signal_shutdown() {
                error!(
                    "unable to signal event reactor to cleanly shutdown: {}",
                    err
                );
            }
        }
    }

    fn wait_for_shutdown(mut self) -> Result<(), InternalError> {
        match &mut self.ws_runtime {
            WsRuntime::Reactor(reactor) => {
                if let Some(reactor) = reactor.take() {
                    reactor
                        .wait_for_shutdown()
                        .map_err(|e| InternalError::from_source(Box::new(e)))
                } else {
                    // Calling this function will have consumed this object, so we don't have any
                    // alternative branches
                    unreachable!()
                }
            }
            _ => Ok(()),
        }
    }
}

impl Drop for AwcAdminServiceEventClient {
    fn drop(&mut self) {
        self.signal_shutdown();
    }
}

impl AdminServiceEventClient for AwcAdminServiceEventClient {
    /// Non-blocking
    fn try_next_event(&self) -> Result<Option<AdminServiceEvent>, NextEventError> {
        let evt_result = match self.event_receiver.try_recv() {
            Ok(res) => res,
            Err(TryRecvError::Empty) => return Ok(None),
            Err(TryRecvError::Disconnected) => return Err(NextEventError::Disconnected),
        };

        evt_result
            .map(Some)
            .map_err(|e| NextEventError::InternalError(InternalError::from_source(Box::new(e))))
    }

    /// Blocking
    fn next_event(&self) -> Result<AdminServiceEvent, NextEventError> {
        let evt_result = self
            .event_receiver
            .recv()
            .map_err(|_| NextEventError::Disconnected)?;
        evt_result
            .map_err(|e| NextEventError::InternalError(InternalError::from_source(Box::new(e))))
    }
}

impl ParseBytes<AdminServiceEvent> for AdminServiceEvent {
    fn from_bytes(bytes: &[u8]) -> Result<AdminServiceEvent, ParseError> {
        let json_event: Event = serde_json::from_slice(bytes)
            .map_err(|err| ParseError::MalformedMessage(Box::new(err)))?;

        use messages::AdminServiceEvent::*;
        let (proposal, event_type) = match json_event.admin_event {
            ProposalSubmitted(proposal) => (proposal, EventType::ProposalSubmitted),
            ProposalVote((proposal, pub_key_bytes)) => (
                proposal,
                EventType::ProposalVote {
                    requester: PublicKey(pub_key_bytes),
                },
            ),
            ProposalAccepted((proposal, pub_key_bytes)) => (
                proposal,
                EventType::ProposalAccepted {
                    requester: PublicKey(pub_key_bytes),
                },
            ),
            ProposalRejected((proposal, pub_key_bytes)) => (
                proposal,
                EventType::ProposalRejected {
                    requester: PublicKey(pub_key_bytes),
                },
            ),
            CircuitReady(proposal) => (proposal, EventType::CircuitReady),
            CircuitDisbanded(proposal) => (proposal, EventType::CircuitDisbanded),
        };

        Ok(AdminServiceEvent {
            event_id: json_event.event_id,
            event_type,
            proposal: proposal.into(),
        })
    }
}

#[derive(Deserialize, Debug)]
struct Event {
    event_id: u64,

    #[serde(flatten)]
    admin_event: messages::AdminServiceEvent,
}

impl From<messages::CircuitProposal> for ProposalSlice {
    fn from(proposal: messages::CircuitProposal) -> Self {
        use messages::ProposalType::*;
        let proposal_type = match proposal.proposal_type {
            Create => "Create",
            UpdateRoster => "UpdateRoster",
            AddNode => "AddNode",
            RemoveNode => "RemoveNode",
            Disband => "Disband",
        }
        .to_owned();

        Self {
            proposal_type,
            circuit_id: proposal.circuit_id,
            circuit_hash: proposal.circuit_hash,
            circuit: proposal.circuit.into(),
            votes: proposal.votes.into_iter().map(VoteRecord::from).collect(),
            requester: hex::to_hex(&proposal.requester),
            requester_node_id: proposal.requester_node_id,
        }
    }
}

impl From<messages::CreateCircuit> for ProposalCircuitSlice {
    fn from(create_circuit: messages::CreateCircuit) -> Self {
        Self {
            circuit_id: create_circuit.circuit_id,
            members: create_circuit
                .members
                .into_iter()
                .map(CircuitMembers::from)
                .collect(),
            roster: create_circuit
                .roster
                .into_iter()
                .map(CircuitService::from)
                .collect(),
            management_type: create_circuit.circuit_management_type,
            comments: create_circuit.comments,
            display_name: create_circuit.display_name,
        }
    }
}

impl From<messages::VoteRecord> for VoteRecord {
    fn from(vote_record: messages::VoteRecord) -> Self {
        Self {
            public_key: hex::to_hex(&vote_record.public_key),
            vote: match vote_record.vote {
                messages::Vote::Accept => "Accept",
                messages::Vote::Reject => "Reject",
            }
            .into(),
            voter_node_id: vote_record.voter_node_id,
        }
    }
}

impl From<messages::SplinterNode> for CircuitMembers {
    fn from(splinter_node: messages::SplinterNode) -> Self {
        Self {
            node_id: splinter_node.node_id,
            endpoints: splinter_node.endpoints,
            public_key: splinter_node
                .public_key
                .as_ref()
                .map(|public_key| hex::to_hex(public_key)),
        }
    }
}

impl From<messages::SplinterService> for CircuitService {
    fn from(splinter_service: messages::SplinterService) -> Self {
        Self {
            service_id: splinter_service.service_id,
            service_type: splinter_service.service_type,
            node_id: splinter_service
                .allowed_nodes
                .into_iter()
                .next()
                .unwrap_or_else(|| String::from("<NONE>")),
            arguments: splinter_service
                .arguments
                .into_iter()
                .map(|(k, v)| vec![k, v])
                .collect(),
        }
    }
}