use borsh::{BorshDeserialize, BorshSerialize};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use crate::da::DaSpec;
use crate::zk::{ValidityCondition, Zkvm};
#[cfg(any(all(test, feature = "sha2"), feature = "fuzzing"))]
pub mod fuzzing;
pub struct ProverConfig;
pub struct ZkConfig;
pub struct StandardConfig;
pub trait StateTransitionConfig: sealed::Sealed {}
impl StateTransitionConfig for ProverConfig {}
impl StateTransitionConfig for ZkConfig {}
impl StateTransitionConfig for StandardConfig {}
mod sealed {
use super::{ProverConfig, StandardConfig, ZkConfig};
pub trait Sealed {}
impl Sealed for ProverConfig {}
impl Sealed for ZkConfig {}
impl Sealed for StandardConfig {}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransactionReceipt<R> {
pub tx_hash: [u8; 32],
pub body_to_save: Option<Vec<u8>>,
pub events: Vec<Event>,
pub receipt: R,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchReceipt<BatchReceiptContents, TxReceiptContents> {
pub batch_hash: [u8; 32],
pub tx_receipts: Vec<TransactionReceipt<TxReceiptContents>>,
pub inner: BatchReceiptContents,
}
pub struct SlotResult<S, B, T, W> {
pub state_root: S,
pub batch_receipts: Vec<BatchReceipt<B, T>>,
pub witness: W,
}
pub trait StateTransitionFunction<Vm: Zkvm, Da: DaSpec> {
type StateRoot: Serialize + DeserializeOwned + Clone + AsRef<[u8]>;
type InitialState;
type TxReceiptContents: Serialize + DeserializeOwned + Clone;
type BatchReceiptContents: Serialize + DeserializeOwned + Clone;
type Witness: Default + Serialize + DeserializeOwned;
type Condition: ValidityCondition;
fn init_chain(&mut self, params: Self::InitialState) -> Self::StateRoot;
fn apply_slot<'a, I>(
&mut self,
pre_state_root: &Self::StateRoot,
witness: Self::Witness,
slot_header: &Da::BlockHeader,
validity_condition: &Da::ValidityCondition,
blobs: I,
) -> SlotResult<
Self::StateRoot,
Self::BatchReceiptContents,
Self::TxReceiptContents,
Self::Witness,
>
where
I: IntoIterator<Item = &'a mut Da::BlobTransaction>;
}
#[derive(Debug, Clone, PartialEq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
pub struct Event {
key: EventKey,
value: EventValue,
}
impl Event {
pub fn new(key: &str, value: &str) -> Self {
Self {
key: EventKey(key.as_bytes().to_vec()),
value: EventValue(value.as_bytes().to_vec()),
}
}
pub fn key(&self) -> &EventKey {
&self.key
}
pub fn value(&self) -> &EventValue {
&self.value
}
}
#[derive(
Debug,
Clone,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
pub struct EventKey(Vec<u8>);
impl EventKey {
pub fn inner(&self) -> &Vec<u8> {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "fuzzing"), derive(proptest_derive::Arbitrary))]
pub struct EventValue(Vec<u8>);
impl EventValue {
pub fn inner(&self) -> &Vec<u8> {
&self.0
}
}