streamlette 0.2.7

Streamlet-based pluggable oneshot consensus
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use std::{
    collections::{BTreeMap, HashMap, HashSet},
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
};

use bytes::Bytes;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use tmelcrypt::{Ed25519PK, Ed25519SK, HashVal};

use crate::msg::{Message, Proposal, Solicit, Vote};

/// Core consensus logic. Stores the tree, etc.
#[derive(Clone)]
pub struct Core {
    valid_proposals: BTreeMap<HashVal, Proposal>,
    vote_solicits: BTreeMap<HashVal, Solicit>,
    votes: BTreeMap<HashVal, Vote>,
    tick_source: HashSet<(u64, Ed25519PK)>,
    nonce: u128,

    tick_to_leader: Arc<dyn Fn(u64) -> Ed25519PK + Send + Sync + 'static>,
    vote_map: BTreeMap<Ed25519PK, u64>,
    total_votes: u64,

    max_tick: Arc<AtomicU64>,
}

/// An enum of different possible messages, used to represent a "diff" between different [Core]s.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum DiffMessage {
    Proposal(Proposal),
    Solicit(Solicit),
    Vote(Vote),
}

impl Core {
    /// Sets the max tick of the core.
    pub(crate) fn set_max_tick(&self, tick: u64) {
        self.max_tick.store(tick, Ordering::SeqCst);
    }

    /// Gets the max tick of the core.
    pub(crate) fn max_tick(&self) -> u64 {
        self.max_tick.load(Ordering::SeqCst)
    }

    /// Obtain a summary of the whole status, as a mapping between message hash and the *XOR of all the hashes of the votes pointing to it*. This is then used when asking around for missing messages.
    pub fn summary(&self) -> HashMap<HashVal, HashVal> {
        let mut toret = HashMap::new();
        for &h in self.valid_proposals.keys() {
            toret.insert(h, HashVal::default());
        }
        for &h in self.vote_solicits.keys() {
            toret.insert(h, HashVal::default());
        }
        for (h, vote) in self.votes.iter() {
            let xx = toret.entry(vote.voting_for).or_default();
            let mut b = xx.0;
            for i in 0..32 {
                b[i] ^= h[i]
            }
            *xx = HashVal(b)
        }
        toret
    }

    /// Obtains a diff, given somebody else's summary. We return an ordered vector of messages.
    pub fn get_diff(&self, their_summary: &HashMap<HashVal, HashVal>) -> Vec<DiffMessage> {
        let our_summary = self.summary();

        let mut toret = vec![];
        let votes_by_candidate: HashMap<HashVal, Vec<Vote>> =
            self.votes.values().fold(HashMap::new(), |mut hm, v| {
                hm.entry(v.voting_for).or_default().push(v.clone());
                hm
            });
        for (hash, prop) in self.valid_proposals.iter() {
            if our_summary.get(hash) != their_summary.get(hash) {
                if !their_summary.contains_key(hash) {
                    toret.push(DiffMessage::Proposal(prop.clone()));
                }
                if let Some(v) = votes_by_candidate.get(hash) {
                    for v in v.iter() {
                        toret.push(DiffMessage::Vote(v.clone()))
                    }
                }
            }
        }
        for (hash, solc) in self.vote_solicits.iter() {
            if our_summary.get(hash) != their_summary.get(hash) {
                if !their_summary.contains_key(hash) {
                    toret.push(DiffMessage::Solicit(solc.clone()));
                }
                if let Some(v) = votes_by_candidate.get(hash) {
                    for v in v.iter() {
                        toret.push(DiffMessage::Vote(v.clone()))
                    }
                }
            }
        }
        // sort by epoch
        toret.sort_unstable_by_key(|s| match s {
            DiffMessage::Proposal(p) => p.tick,
            DiffMessage::Solicit(s) => s.tick,
            DiffMessage::Vote(_) => u64::MAX,
        });
        toret
    }

    /// Applies a particular DiffMessage
    pub fn apply_one_diff(&mut self, dmsg: DiffMessage) -> anyhow::Result<()> {
        match dmsg {
            DiffMessage::Proposal(p) => self.insert_proposal(p),
            DiffMessage::Solicit(s) => self.insert_solicit(s),
            DiffMessage::Vote(v) => self.insert_vote(v),
        }
    }

    /// Create a new Core with the given logic.
    pub(crate) fn new(
        nonce: u128,
        player_votes: impl IntoIterator<Item = (Ed25519PK, u64)>,
        tick_to_leader: impl Fn(u64) -> Ed25519PK + Send + Sync + 'static,
    ) -> Self {
        let vote_map = player_votes.into_iter().collect::<BTreeMap<_, _>>();
        let total_votes = vote_map.values().copied().sum();
        Core {
            valid_proposals: Default::default(),
            vote_solicits: Default::default(),
            votes: Default::default(),
            tick_source: Default::default(),
            nonce,
            tick_to_leader: Arc::new(tick_to_leader),
            vote_map,
            total_votes,
            max_tick: Arc::new(AtomicU64::new(1)),
        }
    }

    /// Insert *my* votes into the tree. We vote for everything that extends from a longest notarized chain; there cannot be duplicates within an epoch because of the tick_source thing.
    pub(crate) fn insert_my_votes(&mut self, my_sk: Ed25519SK) {
        let tips: HashSet<HashVal> = self.get_lnc_tips().into_iter().collect();
        if tips.is_empty() {
            log::debug!("tips are empty, so we vote for all the proposal");
            // we vote for all the proposals --- they must all be valid to vote for due to checks when adding them
            for prop in self.valid_proposals.keys().copied().collect_vec() {
                let vote = Vote::new(self.nonce, prop, my_sk);
                self.insert_vote(vote)
                    .expect("own vote for a proposal could not be inserted");
            }
        } else {
            // we vote for every solicit that *points to* the tip of a LNC.
            let mut to_insert = vec![];
            for (hash, solicit) in self.vote_solicits.iter() {
                if tips.contains(&solicit.previous) {
                    to_insert.push(Vote::new(self.nonce, *hash, my_sk));
                }
            }
            for vote in to_insert {
                self.insert_vote(vote)
                    .expect("own vote for a solicit could not be inserted")
            }
        }
    }

    /// Insert *my* proposal or solicit. If it's not my turn, literally do nothing.
    pub(crate) fn insert_my_prop_or_solicit(
        &mut self,
        tick: u64,
        my_sk: Ed25519SK,
        gen_prop: impl FnOnce() -> Bytes,
    ) {
        if (self.tick_to_leader)(tick) != my_sk.to_public() {
            return; // not my turn
        }
        let tips = self.get_lnc_tips();
        if let Some(&tip) = tips.first() {
            log::debug!("we have a LNC, so we insert a solicit");
            // we arbitrarily picked a longest-notarized-chain tip. send a solicit extending from it.
            let solicit = Solicit::new(self.nonce, tick, tip, my_sk);
            if let Err(err) = self.insert_solicit(solicit) {
                log::warn!("self-insert solicit failed: {}", err);
            }
        } else {
            log::debug!("we do NOT have a LNC, so we insert a proposal");
            // shoot, we need to insert a proposal
            let proposal = gen_prop();
            let proposal = Proposal::new(self.nonce, tick, proposal, my_sk);
            self.insert_proposal(proposal)
                .expect("could not insert my OWN proposal!");
        }
    }

    /// Obtains the finalized proposal, if such a proposal exists.
    pub(crate) fn get_finalized(&self) -> Option<&Proposal> {
        // tips are solicits that do not have any other solicits pointing to them
        let lnc = self.get_lnc_tips();
        let notarized_tips = self
            .vote_solicits
            .keys()
            .filter(|hash| lnc.contains(hash))
            .copied();
        for tip in notarized_tips {
            // we go all the way back to a proposal, checking whether we see *three consecutive tick numbers*.
            let mut tick_numbers = vec![self.vote_solicits[&tip].tick];
            let mut tip_ptr = tip;
            let tr;
            loop {
                if let Some(solicit) = self.vote_solicits.get(&tip_ptr) {
                    tick_numbers.push(solicit.tick);
                    tip_ptr = solicit.previous;
                } else if let Some(prop) = self.valid_proposals.get(&tip_ptr) {
                    tick_numbers.push(prop.tick);
                    tr = prop;
                    break;
                } else {
                    panic!("string of vote solicits that dangle at the end?!?!?!")
                }
            }
            let mut this_is_it = false;
            for window in tick_numbers.windows(3) {
                // DESCENDING ticks
                if window[0] == window[1] + 1 && window[1] == window[2] + 1 {
                    this_is_it = true;
                    break;
                }
            }
            if this_is_it {
                return Some(tr);
            }
        }
        None
    }

    /// Obtains the tips of the longest notarized chain(s).
    pub(crate) fn get_lnc_tips(&self) -> Vec<HashVal> {
        let mut memo = HashMap::new();
        let mut hash_and_len = self
            .valid_proposals
            .keys()
            .chain(self.vote_solicits.keys())
            .filter(|hash| self.is_notarized(**hash))
            .map(|h| (*h, self.lookup_len(*h, &mut memo)))
            .collect_vec();
        hash_and_len.sort_unstable_by_key(|a| a.1);
        hash_and_len.reverse();
        let longest_len = hash_and_len.first().map(|s| s.1);
        hash_and_len
            .into_iter()
            .take_while(|s| Some(s.1) == longest_len)
            .map(|s| s.0)
            .sorted()
            .collect_vec()
    }

    /// Insert a proposal.
    pub(crate) fn insert_proposal(&mut self, prop: Proposal) -> anyhow::Result<()> {
        if !prop.verify_sig() && (self.tick_to_leader)(prop.tick) == prop.source {
            anyhow::bail!("bad signature")
        }
        if prop.nonce != self.nonce {
            anyhow::bail!("bad nonce")
        }
        if prop.tick > self.max_tick() {
            anyhow::bail!(
                "proposal has tick {} > max tick {}",
                prop.tick,
                self.max_tick()
            )
        }
        if !self.tick_source.insert((prop.tick, prop.source)) {
            anyhow::bail!("this player already sent something for this tick")
        }
        // Now we insert this into the system
        self.valid_proposals.insert(prop.chash(), prop);
        Ok(())
    }

    /// Insert a vote.
    pub(crate) fn insert_vote(&mut self, vote: Vote) -> anyhow::Result<()> {
        if !vote.verify_sig() {
            anyhow::bail!("bad signature")
        }
        if vote.nonce != self.nonce {
            anyhow::bail!("bad nonce")
        }
        // check that this  vote actually votes for something
        if !self.vote_solicits.contains_key(&vote.voting_for)
            && !self.valid_proposals.contains_key(&vote.voting_for)
        {
            anyhow::bail!("vote not voting for anything")
        }

        self.votes.insert(vote.chash(), vote.clone());
        log::debug!(
            "{:?} voting for {}, who now has {} votes",
            vote.source,
            vote.voting_for,
            self.votes
                .values()
                .filter(|v| v.voting_for == vote.voting_for)
                .count()
        );
        Ok(())
    }

    /// Inserts a vote solicitation.
    pub(crate) fn insert_solicit(&mut self, solicit: Solicit) -> anyhow::Result<()> {
        if !solicit.verify_sig() && (self.tick_to_leader)(solicit.tick) == solicit.source {
            anyhow::bail!("bad signature")
        }
        if solicit.nonce != self.nonce {
            anyhow::bail!("bad nonce")
        }
        if solicit.tick > self.max_tick() {
            anyhow::bail!(
                "proposal has tick {} > max tick {}",
                solicit.tick,
                self.max_tick()
            )
        }
        if !self.vote_solicits.contains_key(&solicit.previous)
            && !self.valid_proposals.contains_key(&solicit.previous)
        {
            anyhow::bail!("solicit not growing from anything")
        }
        if solicit.tick
            <= self
                .vote_solicits
                .get(&solicit.previous)
                .map(|s| s.tick)
                .or_else(|| self.valid_proposals.get(&solicit.previous).map(|s| s.tick))
                .unwrap()
        {
            anyhow::bail!("tick of vote solicit cannot go backwards in time lol")
        }
        if !self.tick_source.insert((solicit.tick, solicit.source)) {
            anyhow::bail!("this player already sent something for this tick")
        }

        self.vote_solicits.insert(solicit.chash(), solicit);
        Ok(())
    }

    fn lookup_len(&self, h: HashVal, memo: &mut HashMap<HashVal, u64>) -> u64 {
        if let Some(v) = memo.get(&h) {
            *v
        } else {
            let v = if self.valid_proposals.contains_key(&h) {
                0
            } else if let Some(v) = self.vote_solicits.get(&h) {
                self.lookup_len(v.previous, memo) + 1
            } else {
                0
            };
            memo.insert(h, v);
            v
        }
    }

    fn is_notarized(&self, h: HashVal) -> bool {
        self.votes
            .values()
            .filter(|v| v.voting_for == h)
            .map(|v| self.vote_map.get(&v.source).copied().unwrap_or_default())
            .sum::<u64>()
            > self.total_votes * 2 / 3
    }

    /// Produces the graphviz representation of the whole state.
    pub fn debug_graphviz(&self) -> String {
        use std::fmt::Write;

        let tips = self.get_lnc_tips();
        let mut output = String::new();
        output += "digraph G {\n";
        for (h, prop) in self.valid_proposals.iter() {
            writeln!(
                output,
                "{:?} [label={:?}, shape=diamond];",
                h.to_string(),
                format!("{}", String::from_utf8_lossy(&prop.body),)
            )
            .unwrap();
        }
        for (h, solc) in self.vote_solicits.iter() {
            writeln!(
                output,
                "{:?} [label={:?}, shape=box, style=filled, fillcolor={}];",
                h.to_string(),
                &format!("{}[{}]", &h.to_string()[0..8], solc.tick),
                if tips.contains(h) {
                    "aliceblue"
                } else {
                    "whitesmoke"
                }
            )
            .unwrap();
            writeln!(
                output,
                "{:?} -> {:?};",
                h.to_string(),
                solc.previous.to_string()
            )
            .unwrap();
        }

        output += "}\n";
        output
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use itertools::Itertools;

    #[test]
    fn normal_case() {
        let players = (0..10).map(|_| tmelcrypt::ed25519_keygen().1).collect_vec();
        let mut core = Core::new(
            0,
            players
                .iter()
                .copied()
                .map(|p| (p.to_public(), 1))
                .collect_vec(),
            {
                let players = players.clone();
                move |i| players[(i as usize) % players.len()].to_public()
            },
        );
        for tick in 0.. {
            if tick > 100 {
                panic!("took too long to finalize");
            }
            for (pno, my_sk) in players.iter().copied().enumerate() {
                core.insert_my_prop_or_solicit(tick, my_sk, || {
                    Bytes::copy_from_slice(format!("prop-{}", pno).as_bytes())
                });
            }
            for my_sk in players.iter().copied() {
                if fastrand::f64() > 0.3 {
                    core.insert_my_votes(my_sk)
                }
            }
            if let Some(_v) = core.get_finalized() {
                println!("FINALIZED!!!!");
                break;
            }
        }
        println!("{}", core.debug_graphviz());
    }
}