Skip to content

Commit

Permalink
Change API for populate_events function
Browse files Browse the repository at this point in the history
  • Loading branch information
Niederb committed Oct 9, 2024
1 parent 833db41 commit 96696ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
27 changes: 11 additions & 16 deletions src/api/rpc_api/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ pub trait SubmitAndWatch {
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Self::Hash>>;

/// Query the events for the specified `report` and attach them.
async fn populate_events(
&self,
report: ExtrinsicReport<Self::Hash>,
) -> Result<ExtrinsicReport<Self::Hash>>;
/// Query the events for the specified `report` and attaches them to the returned report.
/// If the function fails the report is not modified.
///
/// This method is blocking if the sync-api feature is activated
async fn populate_events(&self, report: &mut ExtrinsicReport<Self::Hash>) -> Result<()>;
}

#[maybe_async::maybe_async(?Send)]
Expand Down Expand Up @@ -275,20 +275,18 @@ where
encoded_extrinsic: &Bytes,
watch_until: XtStatus,
) -> Result<ExtrinsicReport<Self::Hash>> {
let report = self
let mut report = self
.submit_and_watch_opaque_extrinsic_until_without_events(encoded_extrinsic, watch_until)
.await?;

if watch_until < XtStatus::InBlock {
return Ok(report)
}
self.populate_events(report).await
self.populate_events(&mut report).await?;
return Ok(report);
}

async fn populate_events(
&self,
mut report: ExtrinsicReport<Self::Hash>,
) -> Result<ExtrinsicReport<Self::Hash>> {
async fn populate_events(&self, report: &mut ExtrinsicReport<Self::Hash>) -> Result<()> {
if report.events.is_some() {
return Err(Error::Other("Report already contains events".into()))
}
Expand All @@ -304,17 +302,14 @@ where
break
}
}

report.events = Some(extrinsic_events.into_iter().map(|event| event.to_raw()).collect());

if let Some(dispatch_error) = maybe_dispatch_error {
return Err(Error::FailedExtrinsic(FailedExtrinsicError::new(
dispatch_error,
report.encode(),
)))
}

Ok(report)
report.events = Some(extrinsic_events.into_iter().map(|event| event.to_raw()).collect());
Ok(())
}

async fn submit_and_watch_extrinsic_until_without_events<
Expand Down
7 changes: 4 additions & 3 deletions testing/async/examples/author_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async fn test_submit_and_watch_extrinsic_until_in_block_without_events(
// Wait a little, otherwise we may run into future
std::thread::sleep(std::time::Duration::from_secs(1));
let xt = api.compose_extrinsic_offline(transfer_call, nonce);
let report = api
let mut report = api
.submit_and_watch_extrinsic_until_without_events(xt, XtStatus::InBlock)
.await
.unwrap();
Expand All @@ -193,13 +193,14 @@ async fn test_submit_and_watch_extrinsic_until_in_block_without_events(
assert!(report.events.is_none());

// Now we fetch the events separately
let report = api.populate_events(report).await.unwrap();
api.populate_events(&mut report).await.unwrap();
assert!(report.events.is_some());
let events = report.events.as_ref().unwrap();
assert_associated_events_match_expected(&events);

// Can populate events only once
assert!(api.populate_events(report).await.is_err());
let result = api.populate_events(&mut report).await;
assert!(result.is_err());
}

fn assert_associated_events_match_expected(events: &[RawEventDetails<Hash>]) {
Expand Down

0 comments on commit 96696ac

Please sign in to comment.