-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathheader.rs
83 lines (69 loc) · 2.04 KB
/
header.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
use std::convert::{TryFrom, TryInto};
use serde_derive::{Deserialize, Serialize};
use tendermint_proto::Protobuf;
use ibc_proto::ibc::mock::Header as RawMockHeader;
use crate::ics02_client::client_consensus::AnyConsensusState;
use crate::ics02_client::client_type::ClientType;
use crate::ics02_client::error::{self, Error};
use crate::ics02_client::header::AnyHeader;
use crate::ics02_client::header::Header;
use crate::mock::client_state::MockConsensusState;
use crate::timestamp::Timestamp;
use crate::Height;
#[derive(Copy, Clone, Default, Debug, Deserialize, PartialEq, Eq, Serialize)]
pub struct MockHeader {
pub height: Height,
pub timestamp: Timestamp,
}
impl Protobuf<RawMockHeader> for MockHeader {}
impl TryFrom<RawMockHeader> for MockHeader {
type Error = Error;
fn try_from(raw: RawMockHeader) -> Result<Self, Self::Error> {
Ok(MockHeader {
height: raw
.height
.ok_or_else(|| error::Kind::InvalidRawHeader.context("missing height in header"))?
.try_into()
.map_err(|e| error::Kind::InvalidRawHeader.context(e))?,
timestamp: Timestamp::from_nanoseconds(raw.timestamp)
.map_err(|_| error::Kind::InvalidPacketTimestamp)?,
})
}
}
impl From<MockHeader> for RawMockHeader {
fn from(value: MockHeader) -> Self {
value.into()
}
}
impl MockHeader {
pub fn height(&self) -> Height {
self.height
}
pub fn new(height: Height) -> Self {
Self {
height,
timestamp: Default::default(),
}
}
}
impl From<MockHeader> for AnyHeader {
fn from(mh: MockHeader) -> Self {
Self::Mock(mh)
}
}
impl Header for MockHeader {
fn client_type(&self) -> ClientType {
ClientType::Mock
}
fn height(&self) -> Height {
todo!()
}
fn wrap_any(self) -> AnyHeader {
todo!()
}
}
impl From<MockHeader> for AnyConsensusState {
fn from(h: MockHeader) -> Self {
AnyConsensusState::Mock(MockConsensusState(h))
}
}