Skip to content

Commit a2d9bf0

Browse files
committed
feat(node/web): expose best chain user commands rpc
1 parent c71c16a commit a2d9bf0

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

node/common/src/service/rpc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub use sender::RpcSender;
44
pub mod state;
55
pub mod stats;
66
pub mod transaction_pool;
7+
pub mod transition_frontier;
78

89
use node::rpc::{
910
RpcBestChainResponse, RpcBlockProducerStatsGetResponse, RpcConsensusConstantsGetResponse,

node/common/src/service/rpc/sender.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use node::rpc::*;
1111
use super::state::State;
1212
use super::stats::Stats;
1313
use super::transaction_pool::TransactionPool;
14+
use super::transition_frontier::TransitionFrontier;
1415
use super::NodeRpcRequest;
1516

1617
#[derive(Clone)]
@@ -81,6 +82,10 @@ impl RpcSender {
8182
pub fn transaction_pool(&self) -> TransactionPool {
8283
TransactionPool::new(self.clone())
8384
}
85+
86+
pub fn transition_frontier(&self) -> TransitionFrontier {
87+
TransitionFrontier::new(self.clone())
88+
}
8489
}
8590

8691
#[cfg(target_family = "wasm")]
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#[cfg(target_family = "wasm")]
2+
use gloo_utils::format::JsValueSerdeExt;
3+
use node::rpc::*;
4+
#[cfg(target_family = "wasm")]
5+
use wasm_bindgen::prelude::*;
6+
7+
use super::RpcSender;
8+
9+
#[derive(Clone)]
10+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
11+
pub struct TransitionFrontier {
12+
#[allow(unused)]
13+
sender: RpcSender,
14+
}
15+
16+
#[derive(Clone)]
17+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
18+
pub struct TransitionFrontierBestChain {
19+
#[allow(unused)]
20+
sender: RpcSender,
21+
}
22+
23+
impl TransitionFrontier {
24+
pub fn new(sender: RpcSender) -> Self {
25+
Self { sender }
26+
}
27+
28+
pub fn best_chain(&self) -> TransitionFrontierBestChain {
29+
TransitionFrontierBestChain {
30+
sender: self.sender.clone(),
31+
}
32+
}
33+
}
34+
35+
impl TransitionFrontierBestChain {
36+
async fn _user_commands(&self) -> Option<RpcTransitionFrontierUserCommandsResponse> {
37+
self.sender
38+
.oneshot_request(RpcRequest::TransitionFrontierUserCommandsGet)
39+
.await
40+
}
41+
}
42+
43+
#[cfg(not(target_family = "wasm"))]
44+
impl TransitionFrontierBestChain {
45+
pub async fn user_commands(&self) -> Option<RpcTransitionFrontierUserCommandsResponse> {
46+
self._user_commands().await
47+
}
48+
}
49+
50+
#[cfg(target_family = "wasm")]
51+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
52+
impl TransitionFrontierBestChain {
53+
pub async fn user_commands(&self) -> JsValue {
54+
JsValue::from_serde(&self._user_commands().await).unwrap_or_default()
55+
}
56+
}

node/native/src/http_server.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,14 +515,13 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
515515

516516
async move {
517517
rpc_sender_clone
518-
.oneshot_request(RpcRequest::TransitionFrontierUserCommandsGet)
518+
.transition_frontier()
519+
.best_chain()
520+
.user_commands()
519521
.await
520-
.map_or_else(
521-
dropped_channel_response,
522-
|reply: node::rpc::RpcTransitionFrontierUserCommandsResponse| {
523-
with_json_reply(&reply, StatusCode::OK)
524-
},
525-
)
522+
.map_or_else(dropped_channel_response, |reply| {
523+
with_json_reply(&reply, StatusCode::OK)
524+
})
526525
}
527526
});
528527

0 commit comments

Comments
 (0)