-
Notifications
You must be signed in to change notification settings - Fork 332
/
query.rs
121 lines (102 loc) · 3.18 KB
/
query.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
use tendermint_rpc::endpoint::abci_query::AbciQuery;
use tendermint::abci;
use crate::ics23_commitment::{CommitmentPath, CommitmentProof};
use crate::ics24_host::identifier::ConnectionId;
use crate::error;
use crate::ics03_connection::connection::ConnectionEnd;
use crate::path::{ConnectionPath, Path};
use crate::query::{IbcQuery, IbcResponse};
use crate::Height;
use crate::ics03_connection::error::Error;
// Import protobuf definitions.
use ibc_proto::connection::ConnectionEnd as ProtoConnectionEnd;
use bytes::Bytes;
use prost::Message;
pub struct QueryConnection {
pub chain_height: Height,
pub connection_id: ConnectionId,
pub connection_path: ConnectionPath,
pub prove: bool,
}
impl QueryConnection {
pub fn new(chain_height: Height, connection_id: ConnectionId, prove: bool) -> Self {
Self {
chain_height,
connection_id: connection_id.clone(),
connection_path: ConnectionPath::new(connection_id),
prove,
}
}
}
impl IbcQuery for QueryConnection {
type Response = ConnectionResponse;
fn path(&self) -> abci::Path {
"/store/ibc/key".parse().unwrap()
}
fn height(&self) -> Height {
self.chain_height
}
fn prove(&self) -> bool {
self.prove
}
fn data(&self) -> Vec<u8> {
self.connection_path.to_key().into()
}
}
pub struct ConnectionResponse {
pub connection: IdentifiedConnectionEnd,
pub proof: Option<CommitmentProof>,
pub proof_path: CommitmentPath,
pub proof_height: Height,
}
impl ConnectionResponse {
pub fn new(
connection_id: ConnectionId,
connection: ConnectionEnd,
abci_proof: Option<CommitmentProof>,
proof_height: Height,
) -> Self {
let proof_path = CommitmentPath::from_path(ConnectionPath::new(connection_id.clone()));
let identified_connection_end = IdentifiedConnectionEnd::new(connection, connection_id);
ConnectionResponse {
connection: identified_connection_end,
proof: abci_proof,
proof_path,
proof_height,
}
}
}
impl IbcResponse<QueryConnection> for ConnectionResponse {
fn from_abci_response(
query: QueryConnection,
response: AbciQuery,
) -> Result<Self, error::Error> {
match proto_unmarshal(response.value) {
Ok(decoded_conn) => Ok(ConnectionResponse::new(
query.connection_id,
decoded_conn,
response.proof,
response.height.into(),
)),
Err(e) => Err(error::Kind::ResponseParsing.context(e).into()),
}
}
}
#[derive(Debug)]
pub struct IdentifiedConnectionEnd {
connection_end: ConnectionEnd,
connection_id: ConnectionId,
}
impl IdentifiedConnectionEnd {
pub fn new(connection_end: ConnectionEnd, connection_id: ConnectionId) -> Self {
IdentifiedConnectionEnd {
connection_end,
connection_id,
}
}
}
fn proto_unmarshal(bytes: Vec<u8>) -> Result<ConnectionEnd, Error> {
let buf = Bytes::from(bytes);
let decoded = ProtoConnectionEnd::decode(buf).unwrap();
ConnectionEnd::from_proto_connection_end(decoded)
}