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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
use core::fmt::Debug;

use std::{
  sync::Arc,
  time::{SystemTime, Instant, Duration},
  collections::{VecDeque, HashMap},
};

use parity_scale_codec::{Encode, Decode, IoReader};

use futures_channel::mpsc;
use futures_util::{
  FutureExt, StreamExt, SinkExt,
  future::{self, Fuse},
};
use patchable_async_sleep::sleep;

use serai_db::{Get, DbTxn, Db};

pub mod time;
use time::{sys_time, CanonicalInstant};

pub mod round;
use round::RoundData;

mod block;
use block::BlockData;

pub(crate) mod message_log;

/// Traits and types of the external network being integrated with to provide consensus over.
pub mod ext;
use ext::*;

const MESSAGE_TAPE_KEY: &[u8] = b"tendermint-machine-message_tape";
fn message_tape_key(genesis: [u8; 32]) -> Vec<u8> {
  [MESSAGE_TAPE_KEY, &genesis].concat()
}

pub fn commit_msg(end_time: u64, id: &[u8]) -> Vec<u8> {
  [&end_time.to_le_bytes(), id].concat()
}

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Encode, Decode)]
pub enum Step {
  Propose,
  Prevote,
  Precommit,
}

#[derive(Clone, Eq, Debug, Encode, Decode)]
pub enum Data<B: Block, S: Signature> {
  Proposal(Option<RoundNumber>, B),
  Prevote(Option<B::Id>),
  Precommit(Option<(B::Id, S)>),
}

impl<B: Block, S: Signature> PartialEq for Data<B, S> {
  fn eq(&self, other: &Data<B, S>) -> bool {
    match (self, other) {
      (Data::Proposal(valid_round, block), Data::Proposal(valid_round2, block2)) => {
        (valid_round == valid_round2) && (block == block2)
      }
      (Data::Prevote(id), Data::Prevote(id2)) => id == id2,
      (Data::Precommit(None), Data::Precommit(None)) => true,
      (Data::Precommit(Some((id, _))), Data::Precommit(Some((id2, _)))) => id == id2,
      _ => false,
    }
  }
}

impl<B: Block, S: Signature> core::hash::Hash for Data<B, S> {
  fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
    match self {
      Data::Proposal(valid_round, block) => (0, valid_round, block.id().as_ref()).hash(state),
      Data::Prevote(id) => (1, id.as_ref().map(AsRef::<[u8]>::as_ref)).hash(state),
      Data::Precommit(None) => (2, 0).hash(state),
      Data::Precommit(Some((id, _))) => (2, 1, id.as_ref()).hash(state),
    }
  }
}

impl<B: Block, S: Signature> Data<B, S> {
  pub fn step(&self) -> Step {
    match self {
      Data::Proposal(..) => Step::Propose,
      Data::Prevote(..) => Step::Prevote,
      Data::Precommit(..) => Step::Precommit,
    }
  }
}

#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub struct Message<V: ValidatorId, B: Block, S: Signature> {
  pub sender: V,
  pub block: BlockNumber,
  pub round: RoundNumber,

  pub data: Data<B, S>,
}

/// A signed Tendermint consensus message to be broadcast to the other validators.
#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub struct SignedMessage<V: ValidatorId, B: Block, S: Signature> {
  pub msg: Message<V, B, S>,
  pub sig: S,
}

impl<V: ValidatorId, B: Block, S: Signature> SignedMessage<V, B, S> {
  /// Number of the block this message is attempting to add to the chain.
  pub fn block(&self) -> BlockNumber {
    self.msg.block
  }

  #[must_use]
  pub fn verify_signature<Scheme: SignatureScheme<ValidatorId = V, Signature = S>>(
    &self,
    signer: &Scheme,
  ) -> bool {
    signer.verify(self.msg.sender, &self.msg.encode(), &self.sig)
  }
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Encode, Decode)]
pub enum SlashReason {
  FailToPropose,
  InvalidBlock,
  InvalidProposer,
}

#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub enum Evidence {
  ConflictingMessages(Vec<u8>, Vec<u8>),
  InvalidPrecommit(Vec<u8>),
  InvalidValidRound(Vec<u8>),
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TendermintError {
  Malicious,
  Temporal,
  AlreadyHandled,
  InvalidEvidence,
}

// Type aliases to abstract over generic hell
pub type DataFor<N> =
  Data<<N as Network>::Block, <<N as Network>::SignatureScheme as SignatureScheme>::Signature>;
pub(crate) type MessageFor<N> = Message<
  <N as Network>::ValidatorId,
  <N as Network>::Block,
  <<N as Network>::SignatureScheme as SignatureScheme>::Signature,
>;
/// Type alias to the SignedMessage type for a given Network
pub type SignedMessageFor<N> = SignedMessage<
  <N as Network>::ValidatorId,
  <N as Network>::Block,
  <<N as Network>::SignatureScheme as SignatureScheme>::Signature,
>;

pub fn decode_signed_message<N: Network>(mut data: &[u8]) -> Option<SignedMessageFor<N>> {
  SignedMessageFor::<N>::decode(&mut data).ok()
}

fn decode_and_verify_signed_message<N: Network>(
  data: &[u8],
  schema: &N::SignatureScheme,
) -> Result<SignedMessageFor<N>, TendermintError> {
  let msg = decode_signed_message::<N>(data).ok_or(TendermintError::InvalidEvidence)?;

  // verify that evidence messages are signed correctly
  if !msg.verify_signature(schema) {
    Err(TendermintError::InvalidEvidence)?;
  }

  Ok(msg)
}

pub fn verify_tendermint_evidence<N: Network>(
  evidence: &Evidence,
  schema: &N::SignatureScheme,
  commit: impl Fn(u64) -> Option<Commit<N::SignatureScheme>>,
) -> Result<(), TendermintError> {
  match evidence {
    Evidence::ConflictingMessages(first, second) => {
      let first = decode_and_verify_signed_message::<N>(first, schema)?.msg;
      let second = decode_and_verify_signed_message::<N>(second, schema)?.msg;

      // Make sure they're distinct messages, from the same sender, within the same block
      if (first == second) || (first.sender != second.sender) || (first.block != second.block) {
        Err(TendermintError::InvalidEvidence)?;
      }

      // Distinct messages within the same step
      if !((first.round == second.round) && (first.data.step() == second.data.step())) {
        Err(TendermintError::InvalidEvidence)?;
      }
    }
    Evidence::InvalidPrecommit(msg) => {
      let msg = decode_and_verify_signed_message::<N>(msg, schema)?.msg;

      let Data::Precommit(Some((id, sig))) = &msg.data else {
        Err(TendermintError::InvalidEvidence)?
      };
      // TODO: We need to be passed in the genesis time to handle this edge case
      if msg.block.0 == 0 {
        Err(TendermintError::InvalidEvidence)?
        // todo!("invalid precommit signature on first block")
      }

      // get the last commit
      let prior_commit = match commit(msg.block.0 - 1) {
        Some(c) => c,
        // If we have yet to sync the block in question, we will return InvalidEvidence based
        // on our own temporal ambiguity
        // This will also cause an InvalidEvidence for anything using a non-existent block,
        // yet that's valid behavior
        // TODO: Double check the ramifications of this
        _ => Err(TendermintError::InvalidEvidence)?,
      };

      // calculate the end time till the msg round
      let mut last_end_time = CanonicalInstant::new(prior_commit.end_time);
      for r in 0 ..= msg.round.0 {
        last_end_time = RoundData::<N>::new(RoundNumber(r), last_end_time).end_time();
      }

      // verify that the commit was actually invalid
      if schema.verify(msg.sender, &commit_msg(last_end_time.canonical(), id.as_ref()), sig) {
        Err(TendermintError::InvalidEvidence)?
      }
    }
    Evidence::InvalidValidRound(msg) => {
      let msg = decode_and_verify_signed_message::<N>(msg, schema)?.msg;

      let Data::Proposal(Some(vr), _) = &msg.data else { Err(TendermintError::InvalidEvidence)? };
      if vr.0 < msg.round.0 {
        Err(TendermintError::InvalidEvidence)?
      }
    }
  }
  Ok(())
}

#[derive(Clone, PartialEq, Eq, Debug)]
pub enum SlashEvent {
  Id(SlashReason, u64, u32),
  WithEvidence(Evidence),
}

// Struct for if various upon handlers have been triggered to ensure they don't trigger multiple
// times.
#[derive(Clone, PartialEq, Eq, Debug)]
struct Upons {
  upon_prevotes: bool,
  upon_successful_current_round_prevotes: bool,
  upon_negative_current_round_prevotes: bool,
  upon_precommits: bool,
}

/// A machine executing the Tendermint protocol.
pub struct TendermintMachine<N: Network> {
  db: N::Db,
  genesis: [u8; 32],

  network: N,
  signer: <N::SignatureScheme as SignatureScheme>::Signer,
  validators: N::SignatureScheme,
  weights: Arc<N::Weights>,

  queue: VecDeque<MessageFor<N>>,
  msg_recv: mpsc::UnboundedReceiver<SignedMessageFor<N>>,
  synced_block_recv: mpsc::UnboundedReceiver<SyncedBlock<N>>,
  synced_block_result_send: mpsc::UnboundedSender<bool>,

  block: BlockData<N>,
  // TODO: Move this into the Block struct
  round_proposals: HashMap<RoundNumber, (Option<RoundNumber>, N::Block)>,
  // TODO: Move this into the Round struct
  upons: Upons,
}

pub struct SyncedBlock<N: Network> {
  pub number: BlockNumber,
  pub block: <N as Network>::Block,
  pub commit: Commit<<N as Network>::SignatureScheme>,
}

pub type SyncedBlockSender<N> = mpsc::UnboundedSender<SyncedBlock<N>>;
pub type SyncedBlockResultReceiver = mpsc::UnboundedReceiver<bool>;

pub type MessageSender<N> = mpsc::UnboundedSender<SignedMessageFor<N>>;

/// A Tendermint machine and its channel to receive messages from the gossip layer over.
pub struct TendermintHandle<N: Network> {
  /// Channel to trigger the machine to move to the next block.
  /// Takes in the the previous block's commit, along with the new proposal.
  pub synced_block: SyncedBlockSender<N>,
  /// A channel to communicate the result of a synced_block message.
  pub synced_block_result: SyncedBlockResultReceiver,
  /// Channel to send messages received from the P2P layer.
  pub messages: MessageSender<N>,
  /// Tendermint machine to be run on an asynchronous task.
  pub machine: TendermintMachine<N>,
}

impl<N: Network + 'static> TendermintMachine<N> {
  // Broadcast the given piece of data
  // Tendermint messages always specify their block/round, yet Tendermint only ever broadcasts for
  // the current block/round. Accordingly, instead of manually fetching those at every call-site,
  // this function can simply pass the data to the block which can contextualize it
  fn broadcast(&mut self, data: DataFor<N>) {
    if let Some(msg) = self.block.message(data) {
      // Push it on to the queue. This is done so we only handle one message at a time, and so we
      // can handle our own message before broadcasting it. That way, we fail before before
      // becoming malicious
      self.queue.push_back(msg);
    }
  }

  // Start a new round. Returns true if we were the proposer
  fn round(&mut self, round: RoundNumber, time: Option<CanonicalInstant>) -> bool {
    // Clear upons
    self.upons = Upons {
      upon_prevotes: false,
      upon_successful_current_round_prevotes: false,
      upon_negative_current_round_prevotes: false,
      upon_precommits: false,
    };

    let proposer = self.weights.proposer(self.block.number, round);
    let res = if let Some(data) = self.block.new_round(round, proposer, time) {
      self.broadcast(data);
      true
    } else {
      false
    };
    log::debug!(
      target: "tendermint",
      "proposer for block {}, round {round:?} was {} (me: {res})",
      self.block.number.0,
      hex::encode(proposer.encode()),
    );
    res
  }

  // 53-54
  async fn reset(&mut self, end_round: RoundNumber, proposal: Option<N::Block>) {
    // Ensure we have the end time data for the last round
    self.block.populate_end_time(end_round);

    // Sleep until this round ends
    let round_end = self.block.end_time[&end_round];
    let time_until_round_end = round_end.instant().saturating_duration_since(Instant::now());
    if time_until_round_end == Duration::ZERO {
      log::trace!(
        target: "tendermint",
        "resetting when prior round ended {}ms ago",
        Instant::now().saturating_duration_since(round_end.instant()).as_millis(),
      );
    }
    log::trace!(
      target: "tendermint",
      "sleeping until round ends in {}ms",
      time_until_round_end.as_millis(),
    );
    sleep(time_until_round_end).await;

    // Clear the message tape
    {
      let mut txn = self.db.txn();
      txn.del(message_tape_key(self.genesis));
      txn.commit();
    }

    // Clear our outbound message queue
    self.queue = VecDeque::new();

    // Create the new block
    self.block = BlockData::new(
      self.db.clone(),
      self.genesis,
      self.weights.clone(),
      BlockNumber(self.block.number.0 + 1),
      self.signer.validator_id().await,
      proposal,
    );

    // Reset the round proposals
    self.round_proposals = HashMap::new();

    // Start the first round
    self.round(RoundNumber(0), Some(round_end));
  }

  async fn reset_by_commit(
    &mut self,
    commit: Commit<N::SignatureScheme>,
    proposal: Option<N::Block>,
  ) {
    let mut round = self.block.round().number;
    // If this commit is for a round we don't have, jump up to it
    while self.block.end_time[&round].canonical() < commit.end_time {
      round.0 += 1;
      self.block.populate_end_time(round);
    }
    // If this commit is for a prior round, find it
    while self.block.end_time[&round].canonical() > commit.end_time {
      if round.0 == 0 {
        panic!("commit isn't for this machine's next block");
      }
      round.0 -= 1;
    }
    debug_assert_eq!(self.block.end_time[&round].canonical(), commit.end_time);

    self.reset(round, proposal).await;
  }

  async fn slash(&mut self, validator: N::ValidatorId, slash_event: SlashEvent) {
    // TODO: If the new slash event has evidence, emit to prevent a low-importance slash from
    // cancelling emission of high-importance slashes
    if !self.block.slashes.contains(&validator) {
      log::info!(target: "tendermint", "Slashing validator {}", hex::encode(validator.encode()));
      self.block.slashes.insert(validator);
      self.network.slash(validator, slash_event).await;
    }
  }

  fn proposal_for_round(&self, round: RoundNumber) -> Option<(Option<RoundNumber>, &N::Block)> {
    self.round_proposals.get(&round).map(|(round, block)| (*round, block))
  }

  // L22-27
  fn upon_proposal_without_valid_round(&mut self) {
    if self.block.round().step != Step::Propose {
      return;
    }

    // If we have the proposal message...
    let Some((None, block)) = self.proposal_for_round(self.block.round().number) else {
      return;
    };

    // There either needs to not be a locked value or it must be equivalent
    #[allow(clippy::map_unwrap_or)]
    if self
      .block
      .locked
      .as_ref()
      .map(|(_round, locked_block)| block.id() == *locked_block)
      .unwrap_or(true)
    {
      self.broadcast(Data::Prevote(Some(block.id())));
    } else {
      self.broadcast(Data::Prevote(None));
    }
  }

  // L28-33
  fn upon_proposal_with_valid_round(&mut self) {
    if self.block.round().step != Step::Propose {
      return;
    }

    // If we have the proposal message...
    let Some((Some(proposal_valid_round), block)) =
      self.proposal_for_round(self.block.round().number)
    else {
      return;
    };

    // Check we have the necessary prevotes
    if !self.block.log.has_consensus(proposal_valid_round, &Data::Prevote(Some(block.id()))) {
      return;
    }

    // We don't check valid round < current round as the `message` function does

    // If locked is None, lockedRoundp is -1 and less than valid round
    #[allow(clippy::map_unwrap_or)]
    let locked_clause_1 = self
      .block
      .locked
      .as_ref()
      .map(|(locked_round, _block)| locked_round.0 <= proposal_valid_round.0)
      .unwrap_or(true);
    // The second clause is if the locked values are equivalent. If no value is locked, they aren't
    #[allow(clippy::map_unwrap_or)]
    let locked_clause_2 = self
      .block
      .locked
      .as_ref()
      .map(|(_round, locked_block)| block.id() == *locked_block)
      .unwrap_or(false);

    if locked_clause_1 || locked_clause_2 {
      self.broadcast(Data::Prevote(Some(block.id())));
    } else {
      self.broadcast(Data::Prevote(None));
    }
  }

  // L34-35
  fn upon_prevotes(&mut self) {
    if self.upons.upon_prevotes || (self.block.round().step != Step::Prevote) {
      return;
    }

    if self.block.log.has_participation(self.block.round().number, Step::Prevote) {
      self.block.round_mut().set_timeout(Step::Prevote);
      self.upons.upon_prevotes = true;
    }
  }

  // L36-43
  async fn upon_successful_current_round_prevotes(&mut self) {
    // Returning if `self.step == Step::Propose` is equivalent to guarding `step >= prevote`
    if self.upons.upon_successful_current_round_prevotes ||
      (self.block.round().step == Step::Propose)
    {
      return;
    }

    // If we have the proposal message...
    let Some((_, block)) = self.proposal_for_round(self.block.round().number) else {
      return;
    };

    // Check we have the necessary prevotes
    if !self.block.log.has_consensus(self.block.round().number, &Data::Prevote(Some(block.id()))) {
      return;
    }

    let block = block.clone();
    self.upons.upon_successful_current_round_prevotes = true;

    if self.block.round().step == Step::Prevote {
      self.block.locked = Some((self.block.round().number, block.id()));
      let signature = self
        .signer
        .sign(&commit_msg(
          self.block.end_time[&self.block.round().number].canonical(),
          block.id().as_ref(),
        ))
        .await;
      self.broadcast(Data::Precommit(Some((block.id(), signature))));
    }
    self.block.valid = Some((self.block.round().number, block));
  }

  // L44-46
  fn upon_negative_current_round_prevotes(&mut self) {
    if self.upons.upon_negative_current_round_prevotes || (self.block.round().step != Step::Prevote)
    {
      return;
    }

    if self.block.log.has_consensus(self.block.round().number, &Data::Prevote(None)) {
      self.broadcast(Data::Precommit(None));
    }

    self.upons.upon_negative_current_round_prevotes = true;
  }

  // L47-48
  fn upon_precommits(&mut self) {
    if self.upons.upon_precommits {
      return;
    }

    if self.block.log.has_participation(self.block.round().number, Step::Precommit) {
      self.block.round_mut().set_timeout(Step::Precommit);
      self.upons.upon_precommits = true;
    }
  }

  // L22-48
  async fn all_current_round_upons(&mut self) {
    self.upon_proposal_without_valid_round();
    self.upon_proposal_with_valid_round();
    self.upon_prevotes();
    self.upon_successful_current_round_prevotes().await;
    self.upon_negative_current_round_prevotes();
    self.upon_precommits();
  }

  // L49-54
  async fn upon_successful_precommits(&mut self, round: RoundNumber) -> bool {
    // If we have the proposal message...
    let Some((_, block)) = self.proposal_for_round(round) else { return false };

    // Check we have the necessary precommits
    // The precommit we check we have consensus upon uses a junk signature since message equality
    // disregards the signature
    if !self
      .block
      .log
      .has_consensus(round, &Data::Precommit(Some((block.id(), self.signer.sign(&[]).await))))
    {
      return false;
    }

    // Get all participants in this commit
    let mut validators = vec![];
    let mut sigs = vec![];
    // Get all precommits for this round
    for (validator, msgs) in &self.block.log.log[&round] {
      if let Some(signed) = msgs.get(&Step::Precommit) {
        if let Data::Precommit(Some((id, sig))) = &signed.msg.data {
          // If this precommit was for this block, include it
          if *id == block.id() {
            validators.push(*validator);
            sigs.push(sig.clone());
          }
        }
      }
    }

    // Form the commit itself
    let commit_msg = commit_msg(self.block.end_time[&round].canonical(), block.id().as_ref());
    let commit = Commit {
      end_time: self.block.end_time[&round].canonical(),
      validators: validators.clone(),
      signature: self.network.signature_scheme().aggregate(&validators, &commit_msg, &sigs),
    };
    debug_assert!(self.network.verify_commit(block.id(), &commit));

    // Add the block and reset the machine
    log::info!(
      target: "tendermint",
      "TendermintMachine produced block {}",
      hex::encode(block.id().as_ref()),
    );
    let id = block.id();
    let proposal = self.network.add_block(block.clone(), commit).await;
    log::trace!(
      target: "tendermint",
      "added block {} (produced by machine)",
      hex::encode(id.as_ref()),
    );
    self.reset(round, proposal).await;

    true
  }

  // L49-54
  async fn all_any_round_upons(&mut self, round: RoundNumber) -> bool {
    self.upon_successful_precommits(round).await
  }

  // Returns Ok(true) if this was a Precommit which had either no signature or its signature
  // validated
  // Returns Ok(false) if it wasn't a Precommit or the signature wasn't validated yet
  // Returns Err if the signature was invalid
  async fn verify_precommit_signature(
    &mut self,
    signed: &SignedMessageFor<N>,
  ) -> Result<bool, TendermintError> {
    let msg = &signed.msg;
    if let Data::Precommit(precommit) = &msg.data {
      let Some((id, sig)) = precommit else { return Ok(true) };
      // Also verify the end_time of the commit
      // Only perform this verification if we already have the end_time
      // Else, there's a DoS where we receive a precommit for some round infinitely in the future
      // which forces us to calculate every end time
      if let Some(end_time) = self.block.end_time.get(&msg.round) {
        if !self.validators.verify(msg.sender, &commit_msg(end_time.canonical(), id.as_ref()), sig)
        {
          log::warn!(target: "tendermint", "validator produced an invalid commit signature");
          self
            .slash(
              msg.sender,
              SlashEvent::WithEvidence(Evidence::InvalidPrecommit(signed.encode())),
            )
            .await;
          Err(TendermintError::Malicious)?;
        }
        return Ok(true);
      }
    }
    Ok(false)
  }

  async fn message(&mut self, signed: &SignedMessageFor<N>) -> Result<(), TendermintError> {
    let msg = &signed.msg;
    if msg.block != self.block.number {
      Err(TendermintError::Temporal)?;
    }

    // If this is a precommit, verify its signature
    self.verify_precommit_signature(signed).await?;

    // Only let the proposer propose
    if matches!(msg.data, Data::Proposal(..)) &&
      (msg.sender != self.weights.proposer(msg.block, msg.round))
    {
      log::warn!(target: "tendermint", "validator who wasn't the proposer proposed");
      // TODO: This should have evidence
      self
        .slash(msg.sender, SlashEvent::Id(SlashReason::InvalidProposer, msg.block.0, msg.round.0))
        .await;
      Err(TendermintError::Malicious)?;
    };

    // If this is a proposal, verify the block
    // If the block is invalid, drop the message, letting the timeout cover it
    // This prevents needing to check if valid inside every `upon` block
    if let Data::Proposal(_, block) = &msg.data {
      match self.network.validate(block).await {
        Ok(()) => {}
        Err(BlockError::Temporal) => {
          if self.block.round().step == Step::Propose {
            self.broadcast(Data::Prevote(None));
          }
          Err(TendermintError::Temporal)?;
        }
        Err(BlockError::Fatal) => {
          log::warn!(target: "tendermint", "validator proposed a fatally invalid block");
          if self.block.round().step == Step::Propose {
            self.broadcast(Data::Prevote(None));
          }
          self
            .slash(
              msg.sender,
              SlashEvent::Id(SlashReason::InvalidBlock, self.block.number.0, msg.round.0),
            )
            .await;
          Err(TendermintError::Malicious)?;
        }
      };
    }

    // If this is a proposal, verify the valid round isn't fundamentally invalid
    if let Data::Proposal(Some(valid_round), _) = msg.data {
      if valid_round.0 >= msg.round.0 {
        log::warn!(
          target: "tendermint",
          "proposed proposed with a syntactically invalid valid round",
        );
        if self.block.round().step == Step::Propose {
          self.broadcast(Data::Prevote(None));
        }
        self
          .slash(msg.sender, SlashEvent::WithEvidence(Evidence::InvalidValidRound(msg.encode())))
          .await;
        Err(TendermintError::Malicious)?;
      }
    }

    // Add it to the log, returning if it was already handled
    match self.block.log.log(signed.clone()) {
      Ok(true) => {}
      Ok(false) => Err(TendermintError::AlreadyHandled)?,
      Err(evidence) => {
        self.slash(msg.sender, SlashEvent::WithEvidence(evidence)).await;
        Err(TendermintError::Malicious)?;
      }
    }
    log::debug!(
      target: "tendermint",
      "received new tendermint message (block: {}, round: {}, step: {:?})",
      msg.block.0,
      msg.round.0,
      msg.data.step(),
    );

    // If this is a proposal, insert it
    if let Data::Proposal(vr, block) = &msg.data {
      self.round_proposals.insert(msg.round, (*vr, block.clone()));
    }

    // L55-56
    // Jump ahead if we should
    if (msg.round.0 > self.block.round().number.0) &&
      (self.block.log.round_participation(msg.round) >= self.weights.fault_threshold())
    {
      log::debug!(
        target: "tendermint",
        "jumping from round {} to round {}",
        self.block.round().number.0,
        msg.round.0,
      );

      // Jump to the new round.
      let old_round = self.block.round().number;
      self.round(msg.round, None);

      // If any jumped over/to round already has precommit messages, verify their signatures
      for jumped in (old_round.0 + 1) ..= msg.round.0 {
        let jumped = RoundNumber(jumped);
        let round_msgs = self.block.log.log.get(&jumped).cloned().unwrap_or_default();
        for (validator, msgs) in &round_msgs {
          if let Some(existing) = msgs.get(&Step::Precommit) {
            if let Ok(res) = self.verify_precommit_signature(existing).await {
              // Ensure this actually verified the signature instead of believing it shouldn't yet
              assert!(res);
            } else {
              // Remove the message so it isn't counted towards forming a commit/included in one
              // This won't remove the fact they precommitted for this block hash in the MessageLog
              // TODO: Don't even log these in the first place until we jump, preventing needing
              // to do this in the first place
              self
                .block
                .log
                .log
                .get_mut(&jumped)
                .unwrap()
                .get_mut(validator)
                .unwrap()
                .remove(&Step::Precommit)
                .unwrap();
            }
          }
        }
      }
    }

    // Now that we've jumped, and:
    // 1) If this is a message for an old round, verified the precommit signatures
    // 2) If this is a message for what was the current round, verified the precommit signatures
    // 3) If this is a message for what was a future round, verified the precommit signatures if it
    //    has 34+% participation
    // Run all `upons` run for any round, which may produce a Commit if it has 67+% participation
    // (returning true if it does, letting us return now)
    // It's necessary to verify the precommit signatures before Commit production is allowed, hence
    // this specific flow
    if self.all_any_round_upons(msg.round).await {
      return Ok(());
    }

    // If this is a historic round, or a future round without sufficient participation, return
    if msg.round.0 != self.block.round().number.0 {
      return Ok(());
    }
    // msg.round is now guaranteed to be equal to self.block.round().number
    debug_assert_eq!(msg.round, self.block.round().number);

    // Run all `upons` run for the current round
    self.all_current_round_upons().await;

    Ok(())
  }

  /// Create a new Tendermint machine, from the specified point, with the specified block as the
  /// one to propose next. This will return a channel to send messages from the gossip layer and
  /// the machine itself. The machine should have `run` called from an asynchronous task.
  #[allow(clippy::new_ret_no_self)]
  pub async fn new(
    db: N::Db,
    network: N,
    genesis: [u8; 32],
    last_block: BlockNumber,
    last_time: u64,
    proposal: N::Block,
  ) -> TendermintHandle<N> {
    let (msg_send, msg_recv) = mpsc::unbounded();
    let (synced_block_send, synced_block_recv) = mpsc::unbounded();
    let (synced_block_result_send, synced_block_result_recv) = mpsc::unbounded();
    TendermintHandle {
      synced_block: synced_block_send,
      synced_block_result: synced_block_result_recv,
      messages: msg_send,
      machine: {
        let now = SystemTime::now();
        let sys_time = sys_time(last_time);
        let mut negative = false;
        let time_until = sys_time.duration_since(now).unwrap_or_else(|_| {
          negative = true;
          now.duration_since(sys_time).unwrap_or(Duration::ZERO)
        });
        log::info!(
          target: "tendermint",
          "new TendermintMachine building off block {} is scheduled to start in {}{}s",
          last_block.0,
          if negative { "-" } else { "" },
          time_until.as_secs(),
        );

        // If the last block hasn't ended yet, sleep until it has
        if !negative {
          sleep(time_until).await;
        }

        let signer = network.signer();
        let validators = network.signature_scheme();
        let weights = Arc::new(network.weights());
        let validator_id = signer.validator_id().await;
        // L01-10
        let mut machine = TendermintMachine {
          db: db.clone(),
          genesis,

          network,
          signer,
          validators,
          weights: weights.clone(),

          queue: VecDeque::new(),
          msg_recv,
          synced_block_recv,
          synced_block_result_send,

          block: BlockData::new(
            db,
            genesis,
            weights,
            BlockNumber(last_block.0 + 1),
            validator_id,
            Some(proposal),
          ),

          round_proposals: HashMap::new(),

          upons: Upons {
            upon_prevotes: false,
            upon_successful_current_round_prevotes: false,
            upon_negative_current_round_prevotes: false,
            upon_precommits: false,
          },
        };

        // The end time of the last block is the start time for this one
        // The Commit explicitly contains the end time, so loading the last commit will provide
        // this. The only exception is for the genesis block, which doesn't have a commit
        // Using the genesis time in place will cause this block to be created immediately
        // after it, without the standard amount of separation (so their times will be
        // equivalent or minimally offset)
        // For callers wishing to avoid this, they should pass (0, GENESIS + N::block_time())
        machine.round(RoundNumber(0), Some(CanonicalInstant::new(last_time)));
        machine
      },
    }
  }

  pub async fn run(mut self) {
    log::debug!(target: "tendermint", "running TendermintMachine");

    let mut rebroadcast_future = Box::pin(sleep(Duration::from_secs(60))).fuse();
    loop {
      // Also create a future for if the queue has a message
      // Does not pop_front as if another message has higher priority, its future will be handled
      // instead in this loop, and the popped value would be dropped with the next iteration
      let mut queue_future =
        if self.queue.is_empty() { Fuse::terminated() } else { future::ready(()).fuse() };

      if let Some((our_message, msg, mut sig)) = futures_util::select_biased! {
        // Handle a new block occurring externally (from an external sync loop)
        // Has the highest priority as it makes all other futures here irrelevant
        msg = self.synced_block_recv.next() => {
          if let Some(SyncedBlock { number, block, commit }) = msg {
            // Commit is for a block we've already moved past
            if number != self.block.number {
              self.synced_block_result_send.send(false).await.unwrap();
              continue;
            }

            // Commit is invalid
            if !self.network.verify_commit(block.id(), &commit) {
              self.synced_block_result_send.send(false).await.unwrap();
              continue;
            }

            log::debug!(
              target: "tendermint",
              "TendermintMachine received a block from the external sync loop",
            );
            let proposal = self.network.add_block(block, commit.clone()).await;
            self.reset_by_commit(commit, proposal).await;
            self.synced_block_result_send.send(true).await.unwrap();
            None
          } else {
            break;
          }
        },

        // Handle our messages
        () = queue_future => {
          Some((true, self.queue.pop_front().unwrap(), None))
        },

        // L57-67
        // Handle any timeouts
        step = self.block.round().timeout_future().fuse() => {
          // Remove the timeout so it doesn't persist, always being the selected future due to bias
          // While this does enable the timeout to be entered again, the timeout setting code will
          // never attempt to add a timeout after its timeout has expired
          // (due to it setting an `upon` boolean)
          self.block.round_mut().timeouts.remove(&step);

          match step {
            Step::Propose => {
              // Only run if it's still the step in question
              if self.block.round().step == step {
                // Slash the validator for not proposing when they should've
                log::debug!(target: "tendermint", "validator didn't propose when they should have");
                // this slash will be voted on.
                self.slash(
                  self.weights.proposer(self.block.number, self.block.round().number),
                  SlashEvent::Id(
                    SlashReason::FailToPropose,
                    self.block.number.0,
                    self.block.round().number.0
                  ),
                ).await;
                self.broadcast(Data::Prevote(None));
              }
            },
            Step::Prevote => {
              // Only run if it's still the step in question
              if self.block.round().step == step {
                self.broadcast(Data::Precommit(None))
              }
            },
            Step::Precommit => {
              self.round(RoundNumber(self.block.round().number.0 + 1), None);
            }
          };

          // Execute the upons now that the state has changed
          self.all_any_round_upons(self.block.round().number).await;
          self.all_current_round_upons().await;

          None
        },

        // If it's been more than 60s, rebroadcast our own messages
        () = rebroadcast_future => {
          log::trace!("rebroadcast future hit within tendermint machine");
          let key = message_tape_key(self.genesis);
          let messages = self.db.get(key).unwrap_or(vec![]);
          let mut messages = messages.as_slice();

          while !messages.is_empty() {
            self.network.broadcast(
              SignedMessageFor::<N>::decode(&mut IoReader(&mut messages))
                .expect("saved invalid message to DB")
            ).await;
          }

          // Reset the rebroadcast future
          rebroadcast_future = Box::pin(sleep(core::time::Duration::from_secs(60))).fuse();

          None
        },

        // Handle any received messages
        msg = self.msg_recv.next() => {
          if let Some(msg) = msg {
            if !msg.verify_signature(&self.validators) {
              continue;
            }
            Some((false, msg.msg, Some(msg.sig)))
          } else {
            break;
          }
        }
      } {
        if our_message {
          assert!(sig.is_none());
          sig = Some(self.signer.sign(&msg.encode()).await);
        }
        let sig = sig.unwrap();

        let signed_msg = SignedMessage { msg: msg.clone(), sig: sig.clone() };
        let res = self.message(&signed_msg).await;
        // If this is our message, and we hit an invariant, we could be slashed.
        // We only broadcast our message after running it ourselves, to ensure it doesn't error, to
        // ensure we don't get slashed on invariants.
        if res.is_err() && our_message {
          panic!("honest node (ourselves) had invalid behavior");
        }

        // Save this message to a linear tape of all our messages for this block, if ours
        // TODO: Since we do this after we mark this message as sent to prevent equivocations, a
        // precisely time reboot could cause this message marked as sent yet not added to the tape
        if our_message {
          let message_tape_key = message_tape_key(self.genesis);
          let mut txn = self.db.txn();
          let mut message_tape = txn.get(&message_tape_key).unwrap_or(vec![]);
          message_tape.extend(signed_msg.encode());
          txn.put(&message_tape_key, message_tape);
          txn.commit();
        }

        // Re-broadcast this since it's an original consensus message worth handling
        if res.is_ok() {
          self.network.broadcast(signed_msg).await;
        }
      }
    }
  }
}