Skip to content

Add ability to exit on InBlock and Broadcast status for send_extrinsic #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,25 @@ where
info!("finalized: {}", res);
Ok(Some(hexstr_to_hash(res).unwrap()))
}
XtStatus::InBlock => {
rpc::send_extrinsic_and_wait_until_in_block(self.url.clone(), jsonreq, result_in);
let res = result_out.recv().unwrap();
info!("inBlock: {}", res);
Ok(Some(hexstr_to_hash(res).unwrap()))
}
XtStatus::Broadcast => {
rpc::send_extrinsic_and_wait_until_broadcast(self.url.clone(), jsonreq, result_in);
let res = result_out.recv().unwrap();
info!("broadcast: {}", res);
Ok(None)
}
XtStatus::Ready => {
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
let res = result_out.recv().unwrap();
info!("ready: {}", res);
Ok(None)
}
_ => panic!("can only wait for finalized or ready extrinsic status"),
_ => panic!("can only wait for finalized, in block, broadcast and ready extrinsic status"),
}
}

Expand Down
66 changes: 66 additions & 0 deletions src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use ws::{CloseCode, Handler, Handshake, Message, Result, Sender};
#[derive(Debug, PartialEq)]
pub enum XtStatus {
Finalized,
InBlock,
Broadcast,
Ready,
Future,
Error,
Expand Down Expand Up @@ -104,6 +106,40 @@ pub fn on_extrinsic_msg_until_finalized(
Ok(())
}

pub fn on_extrinsic_msg_until_in_block(
msg: Message,
out: Sender,
result: ThreadOut<String>,
) -> Result<()> {
let retstr = msg.as_text().unwrap();
debug!("got msg {}", retstr);
match parse_status(retstr) {
(XtStatus::Finalized, val) => end_process(out, result, val),
(XtStatus::InBlock, val) => end_process(out, result, val),
(XtStatus::Future, _) => end_process(out, result, None),
(XtStatus::Error, _) => end_process(out, result, None),
_ => (),
};
Ok(())
}

pub fn on_extrinsic_msg_until_broadcast(
msg: Message,
out: Sender,
result: ThreadOut<String>,
) -> Result<()> {
let retstr = msg.as_text().unwrap();
debug!("got msg {}", retstr);
match parse_status(retstr) {
(XtStatus::Finalized, val) => end_process(out, result, val),
(XtStatus::Broadcast, _) => end_process(out, result, None),
(XtStatus::Future, _) => end_process(out, result, None),
(XtStatus::Error, _) => end_process(out, result, None),
_ => (),
};
Ok(())
}

pub fn on_extrinsic_msg_until_ready(
msg: Message,
out: Sender,
Expand Down Expand Up @@ -144,6 +180,12 @@ fn parse_status(msg: &str) -> (XtStatus, Option<String>) {
if let Some(hash) = obj.get("finalized") {
info!("finalized: {:?}", hash);
(XtStatus::Finalized, Some(hash.to_string()))
} else if let Some(hash) = obj.get("inBlock") {
info!("inBlock: {:?}", hash);
(XtStatus::InBlock, Some(hash.to_string()))
} else if let Some(array) = obj.get("broadcast") {
info!("broadcast: {:?}", array);
(XtStatus::Broadcast, Some(array.to_string()))
} else {
(XtStatus::Unknown, None)
}
Expand All @@ -170,6 +212,30 @@ mod tests {
let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":\"ready\",\"subscription\":7185}}";
assert_eq!(parse_status(msg), (XtStatus::Ready, None));

let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"broadcast\":[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]},\"subscription\":232}}";
assert_eq!(
parse_status(msg),
(
XtStatus::Broadcast,
Some(
"[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]"
.to_string()
)
)
);

let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"inBlock\":\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\"},\"subscription\":232}}";
assert_eq!(
parse_status(msg),
(
XtStatus::InBlock,
Some(
"\"0x3104d362365ff5ddb61845e1de441b56c6722e94c1aee362f8aa8ba75bd7a3aa\""
.to_string()
)
)
);

let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"finalized\":\"0x934385b11c483498e2b5bca64c2e8ef76ad6c74d3372a05595d3a50caf758d52\"},\"subscription\":7185}}";
assert_eq!(
parse_status(msg),
Expand Down
8 changes: 8 additions & 0 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ pub fn send_extrinsic(url: String, json_req: String, result_in: ThreadOut<String
start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_ready)
}

pub fn send_extrinsic_and_wait_until_broadcast(url: String, json_req: String, result_in: ThreadOut<String>) {
start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_broadcast)
}

pub fn send_extrinsic_and_wait_until_in_block(url: String, json_req: String, result_in: ThreadOut<String>) {
start_rpc_client_thread(url, json_req, result_in, on_extrinsic_msg_until_in_block)
}

pub fn send_extrinsic_and_wait_until_finalized(
url: String,
json_req: String,
Expand Down