avail_rust_core/rpc/
kate.rs

1use super::Error;
2use crate::avail;
3use codec::{Decode, Encode};
4use primitive_types::{H256, U256};
5use serde::{Deserialize, Serialize};
6use subxt_rpcs::{RpcClient, rpc_params};
7
8/// Compatible with `kate::com::Cell`
9#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)]
10pub struct Cell {
11	#[codec(compact)]
12	pub row: u32,
13	#[codec(compact)]
14	pub col: u32,
15}
16
17impl<R, C> From<(R, C)> for Cell
18where
19	R: Into<u32>,
20	C: Into<u32>,
21{
22	fn from((row, col): (R, C)) -> Self {
23		Self { row: row.into(), col: col.into() }
24	}
25}
26
27pub type GRawScalar = U256;
28pub type GRow = Vec<GRawScalar>;
29pub type GDataProof = (GRawScalar, GProof);
30pub type GMultiProof = (Vec<GRawScalar>, GProof);
31
32#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
33#[serde(try_from = "Vec<u8>", into = "Vec<u8>")]
34pub struct GProof(pub [u8; 48]);
35
36impl From<GProof> for Vec<u8> {
37	fn from(proof: GProof) -> Self {
38		proof.0.to_vec()
39	}
40}
41
42impl TryFrom<Vec<u8>> for GProof {
43	type Error = u32;
44
45	fn try_from(data: Vec<u8>) -> Result<Self, Self::Error> {
46		if data.len() != 48 {
47			return Err(data.len() as u32);
48		};
49
50		let mut proof = [0u8; 48];
51		proof.copy_from_slice(&data);
52		Ok(GProof(proof))
53	}
54}
55
56#[derive(Debug, Clone, Copy, Deserialize)]
57pub struct PerDispatchClassU32 {
58	pub normal: u32,
59	pub operational: u32,
60	pub mandatory: u32,
61}
62
63#[derive(Debug, Clone, Deserialize)]
64pub struct BlockLength {
65	pub max: PerDispatchClassU32,
66	pub cols: u32,
67	pub rows: u32,
68	pub chunk_size: u32,
69}
70
71#[derive(Clone, Debug, Serialize, Deserialize)]
72#[serde(rename_all = "camelCase")]
73pub struct ProofResponse {
74	pub data_proof: DataProof,
75	pub message: Option<avail::vector::types::AddressedMessage>,
76}
77
78/// Wrapper of `binary-merkle-tree::MerkleProof` with codec support.
79#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
80#[serde(rename_all = "camelCase")]
81pub struct DataProof {
82	pub roots: TxDataRoots,
83	/// Proof items (does not contain the leaf hash, nor the root obviously).
84	///
85	/// This vec contains all inner node hashes necessary to reconstruct the root hash given the
86	/// leaf hash.
87	pub proof: Vec<H256>,
88	/// Number of leaves in the original tree.
89	///
90	/// This is needed to detect a case where we have an odd number of leaves that "get promoted"
91	/// to upper layers.
92	pub number_of_leaves: u32,
93	/// Index of the leaf the proof is for (0-based).
94	pub leaf_index: u32,
95	/// Leaf content.
96	pub leaf: H256,
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase")]
101pub struct TxDataRoots {
102	/// Global Merkle root
103	pub data_root: H256,
104	/// Merkle root hash of submitted data
105	pub blob_root: H256,
106	/// Merkle root of bridged data
107	pub bridge_root: H256,
108}
109
110#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
111pub struct GCellBlock {
112	pub start_x: u32,
113	pub start_y: u32,
114	pub end_x: u32,
115	pub end_y: u32,
116}
117
118pub async fn block_length(client: &RpcClient, at: Option<H256>) -> Result<BlockLength, Error> {
119	let params = rpc_params![at];
120	let value = client.request("kate_blockLength", params).await?;
121	Ok(value)
122}
123
124pub async fn query_data_proof(
125	client: &RpcClient,
126	transaction_index: u32,
127	at: Option<H256>,
128) -> Result<ProofResponse, Error> {
129	let params = rpc_params![transaction_index, at];
130	let value = client.request("kate_queryDataProof", params).await?;
131	Ok(value)
132}
133
134pub async fn query_proof(client: &RpcClient, cells: Vec<Cell>, at: Option<H256>) -> Result<Vec<GDataProof>, Error> {
135	let params = rpc_params![cells, at];
136	let value = client.request("kate_queryProof", params).await?;
137	Ok(value)
138}
139
140/// Constraint: You can pass up to 64 rows
141pub async fn query_rows(client: &RpcClient, rows: Vec<u32>, at: Option<H256>) -> Result<Vec<GRow>, Error> {
142	let params = rpc_params![rows, at];
143	let value = client.request("kate_queryRows", params).await?;
144	Ok(value)
145}
146
147pub async fn query_multi_proof(
148	client: &RpcClient,
149	cells: Vec<Cell>,
150	at: Option<H256>,
151) -> Result<Vec<(GMultiProof, GCellBlock)>, Error> {
152	let params = rpc_params![cells.to_vec(), at];
153	let proofs: Vec<(GMultiProof, GCellBlock)> = client.request("kate_queryMultiProof", params).await?;
154
155	Ok(proofs)
156}