Skip to content

Commit

Permalink
Fix query rows RPC. (availproject#284)
Browse files Browse the repository at this point in the history
* Fix query rows RPC.

* Query_row rpc now returns an error if the rows are out of scope

* Added missing header changes for deno examples

---------

Co-authored-by: Marko Petrlic <[email protected]>
  • Loading branch information
aterentic-ethernal and markopoloparadox authored Nov 4, 2023
1 parent 3626bf7 commit 5e12b25
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
26 changes: 11 additions & 15 deletions e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ pub mod tests {
Ok(app_extrinsics)
}

async fn query_row(
async fn query_rows(
rpc: &Rpc<AvailConfig>,
rows: &[usize],
block_hash: H256,
) -> anyhow::Result<Vec<Option<Vec<u8>>>> {
) -> anyhow::Result<Vec<Vec<u8>>> {
let mut params = RpcParams::new();
params.push(rows)?;
params.push(Some(block_hash))?;
let rows: Vec<Option<Vec<u8>>> = rpc.request("kate_queryRows", params).await?;
let rows: Vec<Vec<u8>> = rpc.request("kate_queryRows", params).await?;

Ok(rows)
}
Expand Down Expand Up @@ -179,21 +179,17 @@ pub mod tests {
assert_eq!(grid.row(0), extended_grid.row(0));
assert_eq!(grid.row(0), extended_grid.row(1));

// RPC call
let actual_rows = query_row(rpc, &[0, 1, 2], block_hash).await.unwrap();
// RPC call: Querying non existing rows should fail
assert!(query_rows(rpc, &[2], block_hash).await.is_err());

let mut expected_rows: Vec<Option<Vec<u8>>> = Vec::new();
let iter = [
extended_grid.row(0),
extended_grid.row(1),
extended_grid.row(2),
];
for row in iter {
// RPC call: Querying existing rows should NOT fail
let actual_rows = query_rows(rpc, &[0, 1], block_hash).await.unwrap();

let mut expected_rows: Vec<Vec<u8>> = Vec::new();
for row in [extended_grid.row(0), extended_grid.row(1)] {
if let Some(row) = row {
let flat_row: Vec<u8> = row.iter().flat_map(|r| r.to_bytes().unwrap()).collect();
expected_rows.push(Some(flat_row))
} else {
expected_rows.push(None);
expected_rows.push(flat_row);
}
}

Expand Down
10 changes: 5 additions & 5 deletions examples/deno/api_options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const API_RPC = {
isOptional: true
}
],
type: 'Vec<Option<Vec<u8>>>',
type: 'Vec<Vec<u8>>',
}
}
}
Expand All @@ -87,12 +87,12 @@ export const API_TYPES = {
KateCommitment: {
rows: 'Compact<u16>',
cols: 'Compact<u16>',
dataRoot: 'H256',
commitment: 'Vec<u8>'
commitment: 'Vec<u8>',
dataRoot: 'H256'
},
V1HeaderExtension: {
commitment: 'KateCommitment',
appLookup: 'DataLookup'
appLookup: 'DataLookup',
commitment: 'KateCommitment'
},
VTHeaderExtension: {
newField: 'Vec<u8>',
Expand Down
4 changes: 2 additions & 2 deletions examples/ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default {
```
- Query application data returns data fot the given application id and block hash.
```
ts-node ts-node query_app_data.ts
ts-node query_app_data.ts
```
- Creation of app_id
```
Expand All @@ -78,7 +78,7 @@ export default {
Here `i` is the app_id that we would like to create(default is 1).
- Submit a proposal using the council.
```
ts-node ts-node submit_proposal.ts
ts-node submit_proposal.ts
```
You can also use yarn commands to run the scripts. For example
- Start by building the project:
Expand Down
28 changes: 15 additions & 13 deletions rpc/kate-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
&self,
rows: Vec<u32>,
at: Option<HashOf<Block>>,
) -> RpcResult<Vec<Option<Vec<u8>>>>;
) -> RpcResult<Vec<Vec<u8>>>;

#[method(name = "kate_queryAppData")]
async fn query_app_data(
Expand Down Expand Up @@ -271,22 +271,24 @@ where
&self,
rows: Vec<u32>,
at: Option<HashOf<Block>>,
) -> RpcResult<Vec<Option<Vec<u8>>>> {
) -> RpcResult<Vec<Vec<u8>>> {
let signed_block = self.get_signed_and_finalized_block(at)?;
let evals = self.get_eval_grid(&signed_block).await?;

let rows: Vec<Option<Vec<u8>>> = rows
.iter()
.map(|i| {
evals.row(*i as usize).map(|row| {
row.iter()
.flat_map(|a| a.to_bytes().expect("Ser cannot fail"))
.collect::<Vec<u8>>()
})
})
.collect();
let mut data_rows = Vec::with_capacity(rows.len());
for index in rows {
let Some(data) = evals.row(index as usize) else {
return Err(internal_err!("Non existing row: {:?}", index));
};
let data: Vec<u8> = data
.iter()
.flat_map(|a| a.to_bytes().expect("Ser cannot fail"))
.collect();

data_rows.push(data);
}

Ok(rows)
Ok(data_rows)
}

async fn query_app_data(
Expand Down

0 comments on commit 5e12b25

Please sign in to comment.