forked from availproject/avail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_data_hash.rs
68 lines (57 loc) · 1.75 KB
/
retrieve_data_hash.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use super::{alice_nonce, allow_concurrency, local_connection};
use avail_core::AppId;
use avail_subxt::{
api,
api::{
data_availability::calls::types::SubmitData,
runtime_types::bounded_collections::bounded_vec::BoundedVec,
},
primitives::CheckAppId,
tx,
};
use subxt_signer::sr25519::dev;
use anyhow::Result;
use std::sync::atomic::Ordering::Relaxed;
use test_log::test;
use tracing::trace;
/// This example demonstrates submitting data in a block, and retrieving the data and the AppId from the block hash.
#[test(tokio::test)]
async fn test() -> Result<()> {
let _cg = allow_concurrency("retrieve_data_hash").await;
let client = local_connection().await?;
// Send the data and get the block hash
let alice = dev::alice();
let call = api::tx()
.data_availability()
.submit_data(BoundedVec(b"test_data".to_vec()));
let nonce = alice_nonce().await.fetch_add(1, Relaxed);
let tx_progress = tx::send_with_nonce(&client, &call, &alice, AppId(1), nonce).await?;
let block_hash = tx::then_in_block(tx_progress).await?.block_hash();
// Retrieve the data from the block hash
let block = client.blocks().at(block_hash).await?;
let extrinsics = block.extrinsics().await?;
let da_submissions = extrinsics.find::<SubmitData>();
let mut found = false;
for da_submission in da_submissions {
let da_submission = da_submission?;
found = true;
let submitted_data = String::from_utf8(da_submission.value.data.0)?;
let app_id = da_submission
.details
.signed_extensions()
.unwrap()
.find::<CheckAppId>()
.unwrap()
.unwrap();
trace!(
"DA submission found in block: {} \nSubmitted Data: {:?} \nApp Id: {:?}",
block_hash,
submitted_data,
app_id
);
}
if !found {
trace!("No DA submission found in block: {}", block_hash);
}
Ok(())
}