Skip to content

Commit

Permalink
extract await status update
Browse files Browse the repository at this point in the history
  • Loading branch information
clangenb committed Oct 26, 2024
1 parent 61d9cd3 commit da566a0
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions cli/src/trusted_operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,25 +312,11 @@ pub(crate) fn send_direct_request(

loop {
debug!("waiting for update");
let response = receiver.recv().map_err(|e| {
error!("failed to receive rpc response: {:?}", e);
direct_api.close().unwrap();
into_default_trusted_op_err("failed to receive rpc response")
})?;
debug!("received response");

let subscription_update: RpcSubscriptionUpdate =
serde_json::from_str(&response).map_err(|e| {
into_default_trusted_op_err(format!(
"Error deserializing subscription update: {e:?}"
))
})?;

trace!("successfully decoded rpc response: {:?}", subscription_update);

let direct_request_status =
DirectRequestStatus::from_hex(&subscription_update.params.result).map_err(|e| {
into_default_trusted_op_err(format!("Error decoding direct_request_status: {e:?}"))
let (subscription_update, direct_request_status) =
await_status_update(&receiver).map_err(|e| {
error!("Error getting status update: {:?}", e);
direct_api.close().unwrap();
e
})?;

debug!("successfully decoded request status: {:?}", direct_request_status);
Expand Down Expand Up @@ -362,6 +348,29 @@ pub(crate) fn send_direct_request(
}
}

fn await_status_update(
receiver: &Receiver<String>,
) -> TrustedOpResult<(RpcSubscriptionUpdate, DirectRequestStatus)> {
let response = receiver.recv().map_err(|e| {
into_default_trusted_op_err(format!("failed to receive rpc response: {e:?}"))
})?;
debug!("received response");

let subscription_update: RpcSubscriptionUpdate =
serde_json::from_str(&response).map_err(|e| {
into_default_trusted_op_err(format!("Error deserializing subscription update: {e:?}"))
})?;

trace!("successfully decoded rpc response: {:?}", subscription_update);

let direct_request_status = DirectRequestStatus::from_hex(&subscription_update.params.result)
.map_err(|e| {
into_default_trusted_op_err(format!("Error decoding direct_request_status: {e:?}"))
})?;

Ok((subscription_update, direct_request_status))
}

fn decode_response_value<T: Decode, I: Input>(
value: &mut I,
) -> StdResult<T, TrustedOperationError> {
Expand Down

0 comments on commit da566a0

Please sign in to comment.