use codec::{Decode, Encode};
use pezkuwi_pez_node_primitives::{
AvailableData, DisputeMessage, ErasureChunk, PoV, Proof, UncheckedDisputeMessage,
};
use pezkuwi_primitives::{
CandidateHash, CandidateReceiptV2 as CandidateReceipt, Hash, HeadData, Id as ParaId,
ValidatorIndex,
};
use super::{IsRequest, Protocol};
#[derive(Debug, Copy, Clone, Encode, Decode)]
pub struct ChunkFetchingRequest {
pub candidate_hash: CandidateHash,
pub index: ValidatorIndex,
}
#[derive(Debug, Clone, Encode, Decode)]
pub enum ChunkFetchingResponse {
#[codec(index = 0)]
Chunk(ChunkResponse),
#[codec(index = 1)]
NoSuchChunk,
}
impl From<Option<ChunkResponse>> for ChunkFetchingResponse {
fn from(x: Option<ChunkResponse>) -> Self {
match x {
Some(c) => ChunkFetchingResponse::Chunk(c),
None => ChunkFetchingResponse::NoSuchChunk,
}
}
}
impl From<ChunkFetchingResponse> for Option<ChunkResponse> {
fn from(x: ChunkFetchingResponse) -> Self {
match x {
ChunkFetchingResponse::Chunk(c) => Some(c),
ChunkFetchingResponse::NoSuchChunk => None,
}
}
}
#[derive(Debug, Clone, Encode, Decode)]
pub struct ChunkResponse {
pub chunk: Vec<u8>,
pub proof: Proof,
}
impl From<ErasureChunk> for ChunkResponse {
fn from(ErasureChunk { chunk, index: _, proof }: ErasureChunk) -> Self {
ChunkResponse { chunk, proof }
}
}
impl ChunkResponse {
pub fn recombine_into_chunk(self, req: &ChunkFetchingRequest) -> ErasureChunk {
ErasureChunk { chunk: self.chunk, proof: self.proof, index: req.index.into() }
}
}
impl IsRequest for ChunkFetchingRequest {
type Response = ChunkFetchingResponse;
const PROTOCOL: Protocol = Protocol::ChunkFetchingV1;
}
#[derive(Debug, Clone, Encode, Decode)]
pub struct CollationFetchingRequest {
pub relay_parent: Hash,
pub para_id: ParaId,
}
#[derive(Debug, Clone, Encode, Decode)]
pub enum CollationFetchingResponse {
#[codec(index = 0)]
Collation(CandidateReceipt, PoV),
#[codec(index = 1)]
CollationWithParentHeadData {
receipt: CandidateReceipt,
pov: PoV,
parent_head_data: HeadData,
},
}
impl IsRequest for CollationFetchingRequest {
type Response = CollationFetchingResponse;
const PROTOCOL: Protocol = Protocol::CollationFetchingV1;
}
#[derive(Debug, Clone, Encode, Decode)]
pub struct PoVFetchingRequest {
pub candidate_hash: CandidateHash,
}
#[derive(Debug, Clone, Encode, Decode)]
pub enum PoVFetchingResponse {
#[codec(index = 0)]
PoV(PoV),
#[codec(index = 1)]
NoSuchPoV,
}
impl IsRequest for PoVFetchingRequest {
type Response = PoVFetchingResponse;
const PROTOCOL: Protocol = Protocol::PoVFetchingV1;
}
#[derive(Debug, Clone, Encode, Decode)]
pub struct AvailableDataFetchingRequest {
pub candidate_hash: CandidateHash,
}
#[derive(Debug, Clone, Encode, Decode)]
pub enum AvailableDataFetchingResponse {
#[codec(index = 0)]
AvailableData(AvailableData),
#[codec(index = 1)]
NoSuchData,
}
impl From<Option<AvailableData>> for AvailableDataFetchingResponse {
fn from(x: Option<AvailableData>) -> Self {
match x {
Some(data) => AvailableDataFetchingResponse::AvailableData(data),
None => AvailableDataFetchingResponse::NoSuchData,
}
}
}
impl IsRequest for AvailableDataFetchingRequest {
type Response = AvailableDataFetchingResponse;
const PROTOCOL: Protocol = Protocol::AvailableDataFetchingV1;
}
#[derive(Clone, Encode, Decode, Debug)]
pub struct DisputeRequest(pub UncheckedDisputeMessage);
impl From<DisputeMessage> for DisputeRequest {
fn from(msg: DisputeMessage) -> Self {
Self(msg.into())
}
}
#[derive(Encode, Decode, Debug, PartialEq, Eq)]
pub enum DisputeResponse {
#[codec(index = 0)]
Confirmed,
}
impl IsRequest for DisputeRequest {
type Response = DisputeResponse;
const PROTOCOL: Protocol = Protocol::DisputeSendingV1;
}