Skip to content

Commit 550ca41

Browse files
authored
Merge pull request #786 from openmina/feat/node/web/rpcs
Webnode: expose rpcs necessary for benchmarks page
2 parents 05867af + 0428beb commit 550ca41

File tree

6 files changed

+274
-23
lines changed

6 files changed

+274
-23
lines changed

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 Ledger {
12+
#[allow(unused)]
13+
sender: RpcSender,
14+
}
15+
16+
#[derive(Clone)]
17+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
18+
pub struct LedgerSelected {
19+
#[allow(unused)]
20+
sender: RpcSender,
21+
}
22+
23+
#[derive(Clone)]
24+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
25+
pub struct LedgerAccounts {
26+
#[allow(unused)]
27+
sender: RpcSender,
28+
}
29+
30+
impl Ledger {
31+
pub fn new(sender: RpcSender) -> Self {
32+
Self { sender }
33+
}
34+
35+
pub fn latest(&self) -> LedgerSelected {
36+
LedgerSelected {
37+
sender: self.sender.clone(),
38+
}
39+
}
40+
}
41+
42+
impl LedgerSelected {
43+
pub fn accounts(&self) -> LedgerAccounts {
44+
LedgerAccounts {
45+
sender: self.sender.clone(),
46+
}
47+
}
48+
}
49+
50+
impl LedgerAccounts {
51+
async fn _all(&self) -> Option<RpcLedgerSlimAccountsResponse> {
52+
self.sender
53+
.oneshot_request(RpcRequest::LedgerAccountsGet(AccountQuery::All))
54+
.await
55+
}
56+
}
57+
58+
#[cfg(not(target_family = "wasm"))]
59+
impl LedgerAccounts {
60+
pub async fn all(&self) -> Option<RpcLedgerSlimAccountsResponse> {
61+
self._all().await
62+
}
63+
}
64+
65+
#[cfg(target_family = "wasm")]
66+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
67+
impl LedgerAccounts {
68+
pub async fn all(&self) -> JsValue {
69+
JsValue::from_serde(&self._all().await).unwrap_or_default()
70+
}
71+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
mod sender;
22
pub use sender::RpcSender;
33

4+
pub mod ledger;
45
pub mod state;
56
pub mod stats;
7+
pub mod transaction_pool;
8+
pub mod transition_frontier;
69

710
use node::rpc::{
811
RpcBestChainResponse, RpcBlockProducerStatsGetResponse, RpcConsensusConstantsGetResponse,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ use node::core::channels::{mpsc, oneshot};
88
use node::p2p::connection::outgoing::P2pConnectionOutgoingInitOpts;
99
use node::rpc::*;
1010

11+
use super::ledger::Ledger;
1112
use super::state::State;
1213
use super::stats::Stats;
14+
use super::transaction_pool::TransactionPool;
15+
use super::transition_frontier::TransitionFrontier;
1316
use super::NodeRpcRequest;
1417

1518
#[derive(Clone)]
@@ -76,6 +79,18 @@ impl RpcSender {
7679
pub fn stats(&self) -> Stats {
7780
Stats::new(self.clone())
7881
}
82+
83+
pub fn transaction_pool(&self) -> TransactionPool {
84+
TransactionPool::new(self.clone())
85+
}
86+
87+
pub fn transition_frontier(&self) -> TransitionFrontier {
88+
TransitionFrontier::new(self.clone())
89+
}
90+
91+
pub fn ledger(&self) -> Ledger {
92+
Ledger::new(self.clone())
93+
}
7994
}
8095

8196
#[cfg(target_family = "wasm")]
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#[cfg(target_family = "wasm")]
2+
use gloo_utils::format::JsValueSerdeExt;
3+
use mina_p2p_messages::v2;
4+
use node::rpc::*;
5+
#[cfg(target_family = "wasm")]
6+
use wasm_bindgen::prelude::*;
7+
8+
use super::RpcSender;
9+
10+
#[derive(Clone)]
11+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
12+
pub struct TransactionPool {
13+
#[allow(unused)]
14+
sender: RpcSender,
15+
}
16+
17+
#[derive(Clone)]
18+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
19+
pub struct TransactionPoolInject {
20+
#[allow(unused)]
21+
sender: RpcSender,
22+
}
23+
24+
impl TransactionPool {
25+
pub fn new(sender: RpcSender) -> Self {
26+
Self { sender }
27+
}
28+
29+
pub fn inject(&self) -> TransactionPoolInject {
30+
TransactionPoolInject {
31+
sender: self.sender.clone(),
32+
}
33+
}
34+
35+
async fn _get(&self) -> Option<RpcTransactionPoolResponse> {
36+
self.sender
37+
.oneshot_request(RpcRequest::TransactionPoolGet)
38+
.await
39+
}
40+
}
41+
42+
#[cfg(not(target_family = "wasm"))]
43+
impl TransactionPool {
44+
pub async fn get(&self) -> Option<RpcTransactionPoolResponse> {
45+
self._get().await
46+
}
47+
}
48+
49+
#[cfg(target_family = "wasm")]
50+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
51+
impl TransactionPool {
52+
pub async fn get(&self) -> JsValue {
53+
JsValue::from_serde(&self._get().await).unwrap_or_default()
54+
}
55+
}
56+
57+
impl TransactionPoolInject {
58+
async fn _payment(
59+
&self,
60+
payments: Vec<RpcInjectPayment>,
61+
) -> Result<Option<RpcTransactionInjectResponse>, String> {
62+
let res = self
63+
.sender
64+
.oneshot_request(RpcRequest::TransactionInject(
65+
payments
66+
.into_iter()
67+
.map(v2::MinaBaseUserCommandStableV2::try_from)
68+
.collect::<Result<_, _>>()
69+
.map_err(|err| err.to_string())?,
70+
))
71+
.await;
72+
Ok(res)
73+
}
74+
}
75+
76+
#[cfg(not(target_family = "wasm"))]
77+
impl TransactionPoolInject {
78+
pub async fn payment(
79+
&self,
80+
payments: Vec<RpcInjectPayment>,
81+
) -> Result<Option<RpcTransactionInjectResponse>, String> {
82+
self._payment(payments).await
83+
}
84+
}
85+
86+
#[cfg(target_family = "wasm")]
87+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
88+
impl TransactionPoolInject {
89+
pub async fn payment(&self, payments: JsValue) -> Result<JsValue, JsValue> {
90+
let payments: Vec<RpcInjectPayment> = if payments.is_array() {
91+
payments.into_serde().map_err(|err| err.to_string())?
92+
} else {
93+
let payment = payments.into_serde().map_err(|err| err.to_string())?;
94+
vec![payment]
95+
};
96+
97+
self._payment(payments)
98+
.await
99+
.map(|res| JsValue::from_serde(&res).unwrap_or_default())
100+
.map_err(Into::into)
101+
}
102+
}
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: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -453,14 +453,12 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
453453
let rpc_sender_clone = rpc_sender_clone.clone();
454454
async move {
455455
rpc_sender_clone
456-
.oneshot_request(RpcRequest::TransactionPoolGet)
456+
.transaction_pool()
457+
.get()
457458
.await
458-
.map_or_else(
459-
dropped_channel_response,
460-
|reply: node::rpc::RpcTransactionPoolResponse| {
461-
with_json_reply(&reply, StatusCode::OK)
462-
},
463-
)
459+
.map_or_else(dropped_channel_response, |reply| {
460+
with_json_reply(&reply, StatusCode::OK)
461+
})
464462
}
465463
});
466464

@@ -470,7 +468,10 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
470468

471469
async move {
472470
rpc_sender_clone
473-
.oneshot_request(RpcRequest::LedgerAccountsGet(AccountQuery::All))
471+
.ledger()
472+
.latest()
473+
.accounts()
474+
.all()
474475
.await
475476
.map_or_else(
476477
dropped_channel_response,
@@ -489,19 +490,23 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
489490
let rpc_sender_clone = rpc_sender_clone.clone();
490491

491492
async move {
492-
rpc_sender_clone
493-
.oneshot_request(RpcRequest::TransactionInject(
494-
body.into_iter()
495-
.map(|cmd| cmd.try_into().unwrap())
496-
.collect(),
497-
))
493+
match rpc_sender_clone
494+
.transaction_pool()
495+
.inject()
496+
.payment(body)
498497
.await
499-
.map_or_else(
498+
{
499+
Err(err) => with_status(
500+
warp::reply::json(&serde_json::json!({"error": err})),
501+
StatusCode::INTERNAL_SERVER_ERROR,
502+
),
503+
Ok(res) => res.map_or_else(
500504
dropped_channel_response,
501505
|reply: node::rpc::RpcTransactionInjectResponse| {
502506
with_json_reply(&reply, StatusCode::OK)
503507
},
504-
)
508+
),
509+
}
505510
}
506511
});
507512

@@ -513,14 +518,13 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
513518

514519
async move {
515520
rpc_sender_clone
516-
.oneshot_request(RpcRequest::TransitionFrontierUserCommandsGet)
521+
.transition_frontier()
522+
.best_chain()
523+
.user_commands()
517524
.await
518-
.map_or_else(
519-
dropped_channel_response,
520-
|reply: node::rpc::RpcTransitionFrontierUserCommandsResponse| {
521-
with_json_reply(&reply, StatusCode::OK)
522-
},
523-
)
525+
.map_or_else(dropped_channel_response, |reply| {
526+
with_json_reply(&reply, StatusCode::OK)
527+
})
524528
}
525529
});
526530

0 commit comments

Comments
 (0)