Skip to content

Commit 9344146

Browse files
committed
add: Await InBlock and Broadcast Status When Sending Extrinsic #65
1 parent 47edb70 commit 9344146

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

src/lib.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,19 @@ where
344344
info!("ready: {}", res);
345345
Ok(None)
346346
}
347-
_ => panic!("can only wait for finalized or ready extrinsic status"),
347+
XtStatus::Broadcast => {
348+
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
349+
let res = result_out.recv().unwrap();
350+
info!("broadcast: {}", res);
351+
Ok(None)
352+
}
353+
XtStatus::InBlock => {
354+
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
355+
let res = result_out.recv().unwrap();
356+
info!("inBlock: {}", res);
357+
Ok(None)
358+
}
359+
_ => panic!("can only wait for finalized, ready, inBlock or broadcast extrinsic status"),
348360
}
349361
}
350362

src/rpc/client.rs

+72-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use ws::{CloseCode, Handler, Handshake, Message, Result, Sender};
2222
#[derive(Debug, PartialEq)]
2323
pub enum XtStatus {
2424
Finalized,
25+
InBlock,
26+
Broadcast,
2527
Ready,
2628
Future,
2729
Error,
@@ -85,6 +87,41 @@ pub fn on_subscription_msg(msg: Message, _out: Sender, result: ThreadOut<String>
8587
Ok(())
8688
}
8789

90+
pub fn on_extrinsic_msg_until_in_block(
91+
msg: Message,
92+
out: Sender,
93+
result: ThreadOut<String>,
94+
) -> Result<()> {
95+
let retstr = msg.as_text().unwrap();
96+
debug!("got msg {}", retstr);
97+
match parse_status(retstr) {
98+
(XtStatus::Finalized, val) => end_process(out, result, val),
99+
(XtStatus::InBlock, val) => end_process(out, result, val),
100+
(XtStatus::Future, _) => end_process(out, result, None),
101+
(XtStatus::Error, _) => end_process(out, result, None),
102+
_ => (),
103+
};
104+
Ok(())
105+
}
106+
107+
108+
pub fn on_extrinsic_msg_until_broadcast(
109+
msg: Message,
110+
out: Sender,
111+
result: ThreadOut<String>,
112+
) -> Result<()> {
113+
let retstr = msg.as_text().unwrap();
114+
debug!("got msg {}", retstr);
115+
match parse_status(retstr) {
116+
(XtStatus::Finalized, val) => end_process(out, result, val),
117+
(XtStatus::Broadcast, _) => end_process(out, result, None),
118+
(XtStatus::Future, _) => end_process(out, result, None),
119+
(XtStatus::Error, _) => end_process(out, result, None),
120+
_ => (),
121+
};
122+
Ok(())
123+
}
124+
88125
pub fn on_extrinsic_msg_until_finalized(
89126
msg: Message,
90127
out: Sender,
@@ -114,6 +151,8 @@ pub fn on_extrinsic_msg_until_ready(
114151
match parse_status(retstr) {
115152
(XtStatus::Finalized, val) => end_process(out, result, val),
116153
(XtStatus::Ready, _) => end_process(out, result, None),
154+
(XtStatus::InBlock, _) => end_process(out, result, None),
155+
(XtStatus::Broadcast, _) => end_process(out, result, None),
117156
(XtStatus::Future, _) => end_process(out, result, None),
118157
(XtStatus::Error, e) => end_process(out, result, e),
119158
_ => (),
@@ -150,7 +189,16 @@ fn parse_status(msg: &str) -> (XtStatus, Option<String>) {
150189
if let Some(hash) = obj.get("finalized") {
151190
info!("finalized: {:?}", hash);
152191
(XtStatus::Finalized, Some(hash.to_string()))
153-
} else {
192+
}
193+
else if let Some(hash) = obj.get("inBlock") {
194+
info!("inBlock: {:?}", hash);
195+
(XtStatus::InBlock, Some(hash.to_string()))
196+
}
197+
else if let Some(hash) = obj.get("broadcast") {
198+
info!("Broadcast: {:?}", hash);
199+
(XtStatus::Broadcast, Some(hash.to_string())) (XtStatus::InBlock, Some(hash.to_string()))
200+
}
201+
else {
154202
(XtStatus::Unknown, None)
155203
}
156204
}
@@ -188,6 +236,29 @@ mod tests {
188236
)
189237
);
190238

239+
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"broadcast\":[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]},\"subscription\":232}}";
240+
assert_eq!(parse_status(msg), (XtStatus::Broadcast, None)); assert_eq!(
241+
parse_status(msg), (
242+
XtStatus::Broadcast,
243+
Some(
244+
"[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]"
245+
.to_string()
246+
)
247+
)
248+
);
249+
250+
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"inBlock\":\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\"},\"subscription\":232}}";
251+
assert_eq!(
252+
parse_status(msg), (
253+
XtStatus::InBlock,
254+
Some(
255+
"\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\""
256+
.to_string()
257+
)
258+
)
259+
);
260+
261+
191262
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":\"future\",\"subscription\":2}}";
192263
assert_eq!(parse_status(msg), (XtStatus::Future, None));
193264

src/rpc/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ pub fn send_extrinsic(url: String, json_req: String, result_in: ThreadOut<String
3434
start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_ready)
3535
}
3636

37+
pub fn send_extrinsic_and_wait_until_broadcast(url: String, json_req: String, result_in: ThreadOut<String>) {
38+
start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_broadcast)
39+
}
40+
41+
pub fn send_extrinsic_and_wait_until_in_block(url: String, json_req: String, result_in: ThreadOut<String>) {
42+
start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_in_block)
43+
}
44+
3745
pub fn send_extrinsic_and_wait_until_finalized(
3846
url: String,
3947
json_req: String,

0 commit comments

Comments
 (0)