Skip to content

Commit c71c16a

Browse files
committed
feat(node/web): expose transaction pool rpcs
1 parent 05867af commit c71c16a

File tree

4 files changed

+125
-15
lines changed

4 files changed

+125
-15
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub use sender::RpcSender;
33

44
pub mod state;
55
pub mod stats;
6+
pub mod transaction_pool;
67

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use node::rpc::*;
1010

1111
use super::state::State;
1212
use super::stats::Stats;
13+
use super::transaction_pool::TransactionPool;
1314
use super::NodeRpcRequest;
1415

1516
#[derive(Clone)]
@@ -76,6 +77,10 @@ impl RpcSender {
7677
pub fn stats(&self) -> Stats {
7778
Stats::new(self.clone())
7879
}
80+
81+
pub fn transaction_pool(&self) -> TransactionPool {
82+
TransactionPool::new(self.clone())
83+
}
7984
}
8085

8186
#[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(|cmd| v2::MinaBaseUserCommandStableV2::try_from(cmd))
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+
}

node/native/src/http_server.rs

Lines changed: 17 additions & 15 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

@@ -489,19 +487,23 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
489487
let rpc_sender_clone = rpc_sender_clone.clone();
490488

491489
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-
))
490+
match rpc_sender_clone
491+
.transaction_pool()
492+
.inject()
493+
.payment(body)
498494
.await
499-
.map_or_else(
495+
{
496+
Err(err) => with_status(
497+
warp::reply::json(&serde_json::json!({"error": err})),
498+
StatusCode::INTERNAL_SERVER_ERROR,
499+
),
500+
Ok(res) => res.map_or_else(
500501
dropped_channel_response,
501502
|reply: node::rpc::RpcTransactionInjectResponse| {
502503
with_json_reply(&reply, StatusCode::OK)
503504
},
504-
)
505+
),
506+
}
505507
}
506508
});
507509

0 commit comments

Comments
 (0)