-
Notifications
You must be signed in to change notification settings - Fork 225
/
block.rs
140 lines (125 loc) · 3.8 KB
/
block.rs
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
//! Blocks within the chains of a Tendermint network
mod commit;
pub mod commit_sig;
pub mod header;
mod height;
mod id;
mod meta;
pub mod parts;
mod round;
pub mod signed_header;
mod size;
pub use self::{
commit::*,
commit_sig::*,
header::Header,
height::*,
id::{Id, ParseId},
meta::Meta,
round::*,
size::Size,
};
use crate::{abci::transaction, evidence, Error, Kind};
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use tendermint_proto::types::Block as RawBlock;
use tendermint_proto::Protobuf;
/// Blocks consist of a header, transactions, votes (the commit), and a list of
/// evidence of malfeasance (i.e. signing conflicting votes).
///
/// <https://github.com/tendermint/spec/blob/d46cd7f573a2c6a2399fcab2cde981330aa63f37/spec/core/data_structures.md#block>
// Default serialization - all fields serialize; used by /block endpoint
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct Block {
/// Block header
pub header: Header,
/// Transaction data
pub data: transaction::Data,
/// Evidence of malfeasance
pub evidence: evidence::Data,
/// Last commit
#[serde(with = "crate::serializers::optional")]
pub last_commit: Option<Commit>,
}
impl Protobuf<RawBlock> for Block {}
impl TryFrom<RawBlock> for Block {
type Error = Error;
fn try_from(value: RawBlock) -> Result<Self, Self::Error> {
let header: Header = value.header.ok_or(Kind::MissingHeader)?.try_into()?;
// if last_commit is Commit::Default, it is considered nil by Go.
let last_commit = value
.last_commit
.map(TryInto::try_into)
.transpose()?
.filter(|c| c != &Commit::default());
if last_commit.is_none() && header.height.value() != 1 {
return Err(Kind::InvalidBlock
.context("last_commit is empty on non-first block")
.into());
}
// Todo: Figure out requirements.
//if last_commit.is_some() && header.height.value() == 1 {
// return Err(Kind::InvalidFirstBlock.context("last_commit is not null on first
// height").into());
//}
Ok(Block {
header,
data: value.data.ok_or(Kind::MissingData)?.try_into()?,
evidence: value.evidence.ok_or(Kind::MissingEvidence)?.try_into()?,
last_commit,
})
}
}
impl From<Block> for RawBlock {
fn from(value: Block) -> Self {
RawBlock {
header: Some(value.header.into()),
data: Some(value.data.into()),
evidence: Some(value.evidence.into()),
last_commit: value.last_commit.map(Into::into),
}
}
}
impl Block {
/// constructor
pub fn new(
header: Header,
data: transaction::Data,
evidence: evidence::Data,
last_commit: Option<Commit>,
) -> Result<Self, Error> {
if last_commit.is_none() && header.height.value() != 1 {
return Err(Kind::InvalidBlock
.context("last_commit is empty on non-first block")
.into());
}
if last_commit.is_some() && header.height.value() == 1 {
return Err(Kind::InvalidBlock
.context("last_commit is filled on first block")
.into());
}
Ok(Block {
header,
data,
evidence,
last_commit,
})
}
/// Get header
pub fn header(&self) -> &Header {
&self.header
}
/// Get data
pub fn data(&self) -> &transaction::Data {
&self.data
}
/// Get evidence
pub fn evidence(&self) -> &evidence::Data {
&self.evidence
}
/// Get last commit
pub fn last_commit(&self) -> &Option<Commit> {
&self.last_commit
}
}