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
use crate::{
common::{Author, Round},
timeout::Timeout,
};
use anyhow::Context;
use diem_crypto::ed25519::Ed25519Signature;
use diem_types::validator_verifier::ValidatorVerifier;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, fmt};
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
pub struct TimeoutCertificate {
timeout: Timeout,
signatures: BTreeMap<Author, Ed25519Signature>,
}
impl fmt::Display for TimeoutCertificate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"TimeoutCertificate[epoch: {}, round: {}]",
self.timeout.epoch(),
self.timeout.round(),
)
}
}
impl TimeoutCertificate {
pub fn new(timeout: Timeout) -> Self {
Self {
timeout,
signatures: BTreeMap::new(),
}
}
pub fn verify(&self, validator: &ValidatorVerifier) -> anyhow::Result<()> {
validator
.verify_aggregated_struct_signature(&self.timeout, &self.signatures)
.context("Failed to verify TimeoutCertificate")?;
Ok(())
}
pub fn epoch(&self) -> u64 {
self.timeout.epoch()
}
pub fn round(&self) -> Round {
self.timeout.round()
}
pub fn signatures(&self) -> &BTreeMap<Author, Ed25519Signature> {
&self.signatures
}
pub fn add_signature(&mut self, author: Author, signature: Ed25519Signature) {
self.signatures.entry(author).or_insert(signature);
}
pub fn remove_signature(&mut self, author: Author) {
self.signatures.remove(&author);
}
}