Skip to content

add: Await InBlock and Broadcast Status When Sending Extrinsic #69

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions src/examples/example_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ fn main() {

println!("[+] Composed extrinsic: {:?}\n", xt);

// send and watch extrinsic until finalized
// send and watch extrinsic until InBlock
let tx_hash = api
.send_extrinsic(xt.hex_encode(), XtStatus::Finalized)
.send_extrinsic(xt.hex_encode(), XtStatus::InBlock)
.unwrap();
println!("[+] Transaction got finalized. Hash: {:?}\n", tx_hash);
println!("[+] Transaction got inBlock. Hash: {:?}\n", tx_hash);

// verify that Bob's free Balance increased
let bob = api.get_account_data(&to).unwrap();
Expand Down
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,19 @@ where
info!("ready: {}", res);
Ok(None)
}
_ => panic!("can only wait for finalized or ready extrinsic status"),
XtStatus::Broadcast => {
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
let res = result_out.recv().unwrap();
info!("broadcast: {}", res);
Ok(None)
}
XtStatus::InBlock => {
rpc::send_extrinsic(self.url.clone(), jsonreq, result_in);
let res = result_out.recv().unwrap();
info!("inBlock: {}", res);
Ok(Some(hexstr_to_hash(res).unwrap()))
}
_ => panic!("can only wait for finalized, ready, inBlock or broadcast extrinsic status"),
}
}

Expand Down
73 changes: 72 additions & 1 deletion 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 @@ -85,6 +87,41 @@ pub fn on_subscription_msg(msg: Message, _out: Sender, result: ThreadOut<String>
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, e) => end_process(out, result, e),
_ => (),
};
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, e) => end_process(out, result, e),
_ => (),
};
Ok(())
}

pub fn on_extrinsic_msg_until_finalized(
msg: Message,
out: Sender,
Expand Down Expand Up @@ -114,6 +151,8 @@ pub fn on_extrinsic_msg_until_ready(
match parse_status(retstr) {
(XtStatus::Finalized, val) => end_process(out, result, val),
(XtStatus::Ready, _) => end_process(out, result, None),
(XtStatus::InBlock, _) => end_process(out, result, None),
(XtStatus::Broadcast, _) => end_process(out, result, None),
(XtStatus::Future, _) => end_process(out, result, None),
(XtStatus::Error, e) => end_process(out, result, e),
_ => (),
Expand Down Expand Up @@ -150,7 +189,16 @@ 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 {
}
else if let Some(hash) = obj.get("inBlock") {
info!("inBlock: {:?}", hash);
(XtStatus::InBlock, Some(hash.to_string()))
}
else if let Some(hash) = obj.get("broadcast") {
info!("Broadcast: {:?}", array);
(XtStatus::Broadcast, Some(array.to_string()))
}
else {
(XtStatus::Unknown, None)
}
}
Expand Down Expand Up @@ -188,6 +236,29 @@ mod tests {
)
);

let msg = "{\"jsonrpc\":\"2.0\",\"method\":\"author_extrinsicUpdate\",\"params\":{\"result\":{\"broadcast\":[\"QmfSF4VYWNqNf5KYHpDEdY8Rt1nPUgSkMweDkYzhSWirGY\",\"Qmchhx9SRFeNvqjUK4ZVQ9jH4zhARFkutf9KhbbAmZWBLx\",\"QmQJAqr98EF1X3YfjVKNwQUG9RryqX4Hv33RqGChbz3Ncg\"]},\"subscription\":232}}";
assert_eq!(parse_status(msg), (XtStatus::Broadcast, None)); 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\":\"future\",\"subscription\":2}}";
assert_eq!(parse_status(msg), (XtStatus::Future, None));

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