1use std::future::Future;
8use std::pin::pin;
9use std::task::{Context, Poll, Waker};
10
11use crate::ChainError;
12
13pub const EXECUTOR_META: &str = "executor";
15
16pub type StateRow = (Vec<u8>, Vec<u8>);
18
19pub type Change = (Vec<u8>, Option<Vec<u8>>);
21
22pub struct BlockWrite<'a> {
24 pub height: u64,
25 pub hash: [u8; 32],
26 pub bytes: &'a [u8],
27 pub state: Vec<Change>,
29 pub nodes: Vec<Change>,
31 pub statement: Vec<u8>,
32 pub voters: Option<(u32, Vec<u8>)>,
34 pub meta: Vec<u8>,
36}
37
38pub struct Prune {
40 pub checkpoints_below: u64,
42 pub history: Option<(u64, u64)>,
44 pub blocks: Option<(u64, u32)>,
46}
47
48pub struct Pruned {
50 pub checkpoints: usize,
51 pub rows: u64,
52 pub remaining: bool,
54}
55
56#[allow(async_fn_in_trait)]
58pub trait ChainStore {
59 async fn load_state(&self) -> Result<(Vec<u8>, Vec<StateRow>, Vec<StateRow>), ChainError>;
61 async fn load_partial_state(
63 &self,
64 ) -> Result<(Vec<u8>, Vec<StateRow>, Vec<StateRow>), ChainError>;
65 async fn block(&self, height: u64) -> Result<Option<Vec<u8>>, ChainError>;
66 async fn blocks_from(&self) -> Result<u64, ChainError>;
68 async fn proof(&self, epoch: u32) -> Result<Option<Vec<u8>>, ChainError>;
69 async fn proofs_tip(&self) -> Result<u32, ChainError>;
71 async fn voters(&self, epoch: u32) -> Result<Option<Vec<u8>>, ChainError>;
72 async fn put_proof(&self, epoch: u32, proof: &[u8]) -> Result<(), ChainError>;
73 async fn commit_block(&self, write: BlockWrite<'_>) -> Result<(), ChainError>;
75 async fn prune(&self, prune: Prune) -> Result<Pruned, ChainError>;
76 async fn checkpoint_at_or_below(
78 &self,
79 height: u64,
80 ) -> Result<Option<(u64, Vec<u8>)>, ChainError>;
81 async fn changes_above(&self, height: u64) -> Result<Vec<Change>, ChainError>;
83 async fn rollback(
86 &self,
87 height: u64,
88 voters_from: u32,
89 orphans: &[(u64, [u8; 32], &[u8])],
90 ) -> Result<(), ChainError>;
91}
92
93pub fn now_or_never<T>(
95 future: impl Future<Output = Result<T, ChainError>>,
96) -> Result<T, ChainError> {
97 let mut future = pin!(future);
98 match future
99 .as_mut()
100 .poll(&mut Context::from_waker(Waker::noop()))
101 {
102 Poll::Ready(result) => result,
103 Poll::Pending => Err("保存の口が、その場で終わらずに待ちました".into()),
104 }
105}