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
use crate::block::commit_sig::CommitSig;
use crate::block::{Height, Id, Round};
use crate::{Error, Kind};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use tendermint_proto::types::Commit as RawCommit;
#[derive(Clone, PartialEq, Debug, Serialize, Deserialize)]
#[serde(try_from = "RawCommit", into = "RawCommit")]
pub struct Commit {
pub height: Height,
pub round: Round,
pub block_id: Id,
pub signatures: Vec<CommitSig>,
}
impl TryFrom<RawCommit> for Commit {
type Error = Error;
fn try_from(value: RawCommit) -> Result<Self, Self::Error> {
let signatures: Result<Vec<CommitSig>, Error> = value
.signatures
.into_iter()
.map(TryFrom::try_from)
.collect();
Ok(Self {
height: value.height.try_into()?,
round: value.round.try_into()?,
block_id: value.block_id.ok_or(Kind::InvalidBlock)?.try_into()?,
signatures: signatures?,
})
}
}
impl From<Commit> for RawCommit {
fn from(value: Commit) -> Self {
RawCommit {
height: value.height.into(),
round: value.round.into(),
block_id: Some(value.block_id.into()),
signatures: value.signatures.into_iter().map(Into::into).collect(),
}
}
}
impl Default for Commit {
fn default() -> Self {
Commit {
height: Height::from(0_u32),
round: Default::default(),
block_id: Default::default(),
signatures: vec![],
}
}
}