use std::{sync::Arc, time::Duration};
use futures::prelude::*;
use sp_runtime::{
traits::{Block as BlockT, HashFor},
Digest,
};
use sp_state_machine::StorageProof;
pub mod block_validation;
pub mod error;
mod select_chain;
pub use self::error::Error;
pub use select_chain::SelectChain;
pub use sp_inherents::InherentData;
pub use sp_state_machine::Backend as StateBackend;
pub type CacheKeyId = [u8; 4];
#[derive(Debug, PartialEq, Eq)]
pub enum BlockStatus {
Queued,
InChainWithState,
InChainPruned,
KnownBad,
Unknown,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum BlockOrigin {
Genesis,
NetworkInitialSync,
NetworkBroadcast,
ConsensusBroadcast,
Own,
File,
}
impl From<BlockOrigin> for sp_core::ExecutionContext {
fn from(origin: BlockOrigin) -> Self {
if origin == BlockOrigin::NetworkInitialSync {
sp_core::ExecutionContext::Syncing
} else {
sp_core::ExecutionContext::Importing
}
}
}
pub trait Environment<B: BlockT> {
type Proposer: Proposer<B> + Send + 'static;
type CreateProposer: Future<Output = Result<Self::Proposer, Self::Error>>
+ Send
+ Unpin
+ 'static;
type Error: From<Error> + std::error::Error + 'static;
fn init(&mut self, parent_header: &B::Header) -> Self::CreateProposer;
}
pub struct Proposal<Block: BlockT, Transaction, Proof> {
pub block: Block,
pub proof: Proof,
pub storage_changes: sp_state_machine::StorageChanges<Transaction, HashFor<Block>>,
}
#[derive(Debug, thiserror::Error)]
#[error("Proof should be recorded, but no proof was provided.")]
pub struct NoProofRecorded;
pub trait ProofRecording: Send + Sync + private::Sealed + 'static {
type Proof: Send + Sync + 'static;
const ENABLED: bool;
fn into_proof(storage_proof: Option<StorageProof>) -> Result<Self::Proof, NoProofRecorded>;
}
pub struct DisableProofRecording;
impl ProofRecording for DisableProofRecording {
type Proof = ();
const ENABLED: bool = false;
fn into_proof(_: Option<StorageProof>) -> Result<Self::Proof, NoProofRecorded> {
Ok(())
}
}
pub struct EnableProofRecording;
impl ProofRecording for EnableProofRecording {
type Proof = sp_state_machine::StorageProof;
const ENABLED: bool = true;
fn into_proof(proof: Option<StorageProof>) -> Result<Self::Proof, NoProofRecorded> {
proof.ok_or(NoProofRecorded)
}
}
mod private {
pub trait Sealed {}
impl Sealed for super::DisableProofRecording {}
impl Sealed for super::EnableProofRecording {}
}
pub trait Proposer<B: BlockT> {
type Error: From<Error> + std::error::Error + 'static;
type Transaction: Default + Send + 'static;
type Proposal: Future<Output = Result<Proposal<B, Self::Transaction, Self::Proof>, Self::Error>>
+ Send
+ Unpin
+ 'static;
type ProofRecording: self::ProofRecording<Proof = Self::Proof> + Send + Sync + 'static;
type Proof: Send + Sync + 'static;
fn propose(
self,
inherent_data: InherentData,
inherent_digests: Digest,
max_duration: Duration,
block_size_limit: Option<usize>,
) -> Self::Proposal;
}
pub trait SyncOracle {
fn is_major_syncing(&self) -> bool;
fn is_offline(&self) -> bool;
}
#[derive(Clone, Copy, Debug)]
pub struct NoNetwork;
impl SyncOracle for NoNetwork {
fn is_major_syncing(&self) -> bool {
false
}
fn is_offline(&self) -> bool {
false
}
}
impl<T> SyncOracle for Arc<T>
where
T: ?Sized,
T: SyncOracle,
{
fn is_major_syncing(&self) -> bool {
T::is_major_syncing(self)
}
fn is_offline(&self) -> bool {
T::is_offline(self)
}
}