Skip to content

Commit

Permalink
Add tx index from block mapping to data index.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xSasaPrsic committed Aug 17, 2023
1 parent 363e93f commit 90b07bb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/kate-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ edition = "2021"
[dependencies]
avail-base = { path = "../../base", default-features = false }
da-runtime = { path = "../../runtime", default-features = false }
da-control = { path = "../../pallets/dactr" }


avail-core = { version = "0.5", default-features = false, features = ["runtime"] }
kate = { version = "0.8", default-features = false }
Expand Down
41 changes: 36 additions & 5 deletions rpc/kate-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use avail_core::{
header::HeaderExtension, traits::ExtendedHeader, AppExtrinsic, AppId, BlockLengthColumns,
BlockLengthRows, DataProof, OpaqueExtrinsic, BLOCK_CHUNK_SIZE,
};
use da_runtime::{apis::DataAvailApi, Runtime, UncheckedExtrinsic};
use da_runtime::{apis::DataAvailApi, Runtime, RuntimeCall, UncheckedExtrinsic};
use frame_system::{limits::BlockLength, submitted_data};
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
Expand Down Expand Up @@ -405,7 +405,7 @@ where

async fn query_data_proof(
&self,
data_index: u32,
transaction_index: u32,
at: Option<HashOf<Block>>,
) -> RpcResult<DataProof> {
// Fetch block
Expand All @@ -418,6 +418,37 @@ where
.ok_or_else(|| internal_err!("Missing block hash {:?}", at))?
.block;

// data index starts from 0
let mut data_idx: i32 = -1;
// keep track of the last transaction
let mut is_da_transaction: bool = false;
for (i, ex) in block.extrinsics().iter().enumerate() {
let o = UncheckedExtrinsic::try_from(ex).ok();
// todo is unwrap safe
match &o.unwrap().function {
RuntimeCall::DataAvailability(da_control::Call::submit_data { data }) => {
data_idx = data_idx + 1;
is_da_transaction = true;
},
_ => {
is_da_transaction = false;
},
};
// loop until all transactions are filtered to the given transaction index
if transaction_index == i as u32 {
break;
}
}

// passed transaction index must be the last DA transaction in the block
if !is_da_transaction {
return Err(internal_err!(
"Data proof cannot be generated for transaction index={} at block {:?}. Not a data transaction.",
transaction_index,
at
));
}

// Get Opaque Extrinsics and transform into AppUncheckedExt.
let calls = block
.extrinsics()
Expand All @@ -426,11 +457,11 @@ where
.map(|app_ext| app_ext.function);

// Build the proof.
let merkle_proof = submitted_data::calls_proof::<Runtime, _, _>(calls, data_index)
let merkle_proof = submitted_data::calls_proof::<Runtime, _, _>(calls, data_idx as u32)
.ok_or_else(|| {
internal_err!(
"Data proof cannot be generated for index={} at block {:?}",
data_index,
"Data proof cannot be generated for transaction index={} at block {:?}",
transaction_index,
at
)
})?;
Expand Down

0 comments on commit 90b07bb

Please sign in to comment.