-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathmain.rs
More file actions
173 lines (150 loc) · 5.4 KB
/
Copy pathmain.rs
File metadata and controls
173 lines (150 loc) · 5.4 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use std::{fmt::Debug, num::NonZeroU64, time::Duration};
use gno_light_client_types::{ClientState, ConsensusState, Fraction};
use ics23::ibc_api::GNO_SPECS;
use jsonrpsee::{Extensions, core::async_trait};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tracing::instrument;
use unionlabs::{
ibc::core::{client::height::Height, commitment::merkle_root::MerkleRoot},
result_unwrap,
};
use voyager_sdk::{
anyhow, ensure_null,
plugin::ClientBootstrapModule,
primitives::{ChainId, ClientType},
rpc::{ClientBootstrapModuleServer, RpcError, RpcResult, types::ClientBootstrapModuleInfo},
};
#[tokio::main(flavor = "multi_thread")]
async fn main() {
Module::run().await
}
#[derive(Debug, Clone)]
pub struct Module {
pub chain_id: ChainId,
pub gno_client: gno_rpc::Client,
pub ibc_core_realm: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
pub rpc_url: String,
pub ibc_core_realm: String,
}
impl ClientBootstrapModule for Module {
type Config = Config;
async fn new(config: Self::Config, info: ClientBootstrapModuleInfo) -> anyhow::Result<Self> {
let gno_client = gno_rpc::Client::new(config.rpc_url).await?;
let chain_id = gno_client.status(None).await?.node_info.network.to_string();
info.ensure_chain_id(&chain_id)?;
info.ensure_client_type(ClientType::GNO)?;
Ok(Self {
gno_client,
chain_id: ChainId::new(chain_id),
ibc_core_realm: config.ibc_core_realm,
})
}
}
impl Module {
async fn fetch_unbonding_period(&self, _height: Height) -> Duration {
// let params = self
// .gno_client
// .grpc_abci_query::<_, protos::cosmos::staking::v1beta1::QueryParamsResponse>(
// "/cosmos.staking.v1beta1.Query/Params",
// &protos::cosmos::staking::v1beta1::QueryParamsRequest {},
// Some(i64::try_from(height.height()).unwrap().try_into().unwrap()),
// false,
// )
// .await
// .unwrap()
// .value
// .unwrap()
// .params
// .unwrap();
// let unbonding_period = params.unbonding_time.clone().unwrap();
// TODO: Query from the chain properly, see: https://github.com/gnolang/gno/issues/4829
Duration::from_hours(24 * 3)
}
}
#[async_trait]
impl ClientBootstrapModuleServer for Module {
#[instrument(skip_all, fields(chain_id = %self.chain_id))]
async fn self_client_state(
&self,
_: &Extensions,
height: Height,
config: Value,
) -> RpcResult<Value> {
ensure_null(config)?;
let unbonding_period = self.fetch_unbonding_period(height).await;
let commit = self
.gno_client
.commit((height.height() as i64).try_into().unwrap())
.await
.unwrap();
let height = commit.signed_header.header.height;
Ok(serde_json::to_value(ClientState {
chain_id: __self.chain_id.to_string(),
trust_level: Fraction {
numerator: 1,
denominator: const { NonZeroU64::new(3).unwrap() },
},
trusting_period: unionlabs::google::protobuf::duration::Duration::new(
(unbonding_period * 85 / 100).as_secs().try_into().unwrap(),
(unbonding_period * 85 / 100)
.subsec_nanos()
.try_into()
.unwrap(),
)
.unwrap(),
unbonding_period: unionlabs::google::protobuf::duration::Duration::new(
unbonding_period.as_secs().try_into().unwrap(),
unbonding_period.subsec_nanos().try_into().unwrap(),
)
.unwrap(),
max_clock_drift: const {
result_unwrap!(unionlabs::google::protobuf::duration::Duration::new(
60 * 10,
0
))
},
frozen_height: None,
latest_height: Height::new_with_revision(
0,
height.inner().try_into().expect("is within bounds; qed;"),
),
proof_specs: GNO_SPECS.into(),
upgrade_path: vec!["upgrade".into(), "upgradedIBCState".into()],
realm: self.ibc_core_realm.clone(),
})
.unwrap())
}
/// The consensus state on this chain at the specified `Height`.
#[instrument(skip_all, fields(chain_id = %self.chain_id))]
async fn self_consensus_state(
&self,
_: &Extensions,
height: Height,
config: Value,
) -> RpcResult<Value> {
ensure_null(config)?;
let commit = self
.gno_client
.commit((height.height() as i64).try_into().unwrap())
.await
.map_err(RpcError::retryable("error fetching commit"))?;
Ok(serde_json::to_value(&ConsensusState {
root: MerkleRoot {
hash: commit
.signed_header
.header
.app_hash
.ok_or(RpcError::fatal_from_message("no app_hash in commit"))?
.into_encoding(),
},
next_validators_hash: commit.signed_header.header.next_validators_hash,
timestamp: commit.signed_header.header.time,
})
.unwrap())
}
}