use super::Error;
use crate::avail;
use codec::{Decode, Encode};
use primitive_types::{H256, U256};
use serde::{Deserialize, Serialize};
use subxt_rpcs::{RpcClient, rpc_params};
#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
pub struct Cell {
#[codec(compact)]
pub row: u32,
#[codec(compact)]
pub col: u32,
}
impl<R, C> From<(R, C)> for Cell
where
R: Into<u32>,
C: Into<u32>,
{
fn from((row, col): (R, C)) -> Self {
Self { row: row.into(), col: col.into() }
}
}
pub type GRawScalar = U256;
pub type GRow = Vec<GRawScalar>;
pub type GDataProof = (GRawScalar, GProof);
pub type GMultiProof = (Vec<GRawScalar>, GProof);
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(try_from = "Vec<u8>", into = "Vec<u8>")]
pub struct GProof(pub [u8; 48]);
impl From<GProof> for Vec<u8> {
fn from(proof: GProof) -> Self {
proof.0.to_vec()
}
}
impl TryFrom<Vec<u8>> for GProof {
type Error = u32;
fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
if data.len() != 48 {
return Err(u32::try_from(data.len()).unwrap_or(u32::MAX));
};
let mut proof = [0u8; 48];
proof.copy_from_slice(&data);
Ok(GProof(proof))
}
}
#[derive(Debug, Clone, Copy, Deserialize)]
pub struct PerDispatchClassU32 {
pub normal: u32,
pub operational: u32,
pub mandatory: u32,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BlockLength {
pub max: PerDispatchClassU32,
pub cols: u32,
pub rows: u32,
pub chunk_size: u32,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProofResponse {
pub data_proof: DataProof,
pub message: Option<avail::vector::types::AddressedMessage>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DataProof {
pub roots: TxDataRoots,
pub proof: Vec<H256>,
pub number_of_leaves: u32,
pub leaf_index: u32,
pub leaf: H256,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TxDataRoots {
pub data_root: H256,
pub blob_root: H256,
pub bridge_root: H256,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
pub struct GCellBlock {
pub start_x: u32,
pub start_y: u32,
pub end_x: u32,
pub end_y: u32,
}
pub async fn block_length(client: &RpcClient, at: Option<H256>) -> Result<BlockLength, Error> {
let params = rpc_params![at];
let value = client.request("kate_blockLength", params).await?;
Ok(value)
}
pub async fn query_data_proof(
client: &RpcClient,
transaction_index: u32,
at: Option<H256>,
) -> Result<ProofResponse, Error> {
let params = rpc_params![transaction_index, at];
let value = client.request("kate_queryDataProof", params).await?;
Ok(value)
}
pub async fn query_proof(client: &RpcClient, cells: Vec<Cell>, at: Option<H256>) -> Result<Vec<GDataProof>, Error> {
let params = rpc_params![cells, at];
let value = client.request("kate_queryProof", params).await?;
Ok(value)
}
pub async fn query_rows(client: &RpcClient, rows: Vec<u32>, at: Option<H256>) -> Result<Vec<GRow>, Error> {
let params = rpc_params![rows, at];
let value = client.request("kate_queryRows", params).await?;
Ok(value)
}
pub async fn query_multi_proof(
client: &RpcClient,
cells: Vec<Cell>,
at: Option<H256>,
) -> Result<Vec<(GMultiProof, GCellBlock)>, Error> {
let params = rpc_params![cells.to_vec(), at];
let proofs: Vec<(GMultiProof, GCellBlock)> = client.request("kate_queryMultiProof", params).await?;
Ok(proofs)
}