eth_state_fold_types/
lib.rs

1// (c) Cartesi and individual authors (see AUTHORS)
2// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
3
4use ethereum_types::{Bloom, H256, U256, U64};
5use serde::{Deserialize, Serialize};
6use snafu::Snafu;
7use std::sync::Arc;
8
9#[cfg(feature = "ethers")]
10pub mod contract;
11
12pub use ethabi;
13pub use ethabi::ethereum_types;
14
15#[cfg(feature = "ethers")]
16pub use ethers;
17
18pub mod config_utils;
19
20#[derive(Clone, Debug, Serialize, Deserialize)]
21pub struct Block {
22    pub hash: H256,
23    pub number: U64,
24    pub parent_hash: H256,
25    pub timestamp: U256,
26    pub logs_bloom: Bloom,
27}
28
29impl PartialEq for Block {
30    fn eq(&self, other: &Self) -> bool {
31        self.hash.eq(&other.hash)
32    }
33}
34
35impl Eq for Block {}
36
37impl std::hash::Hash for Block {
38    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
39        self.hash.hash(state)
40    }
41}
42
43#[derive(Debug)]
44pub struct BlockState<State> {
45    pub block: Arc<Block>,
46    pub state: Arc<State>,
47}
48
49impl<State> Clone for BlockState<State> {
50    fn clone(&self) -> Self {
51        Self {
52            block: Arc::clone(&self.block),
53            state: Arc::clone(&self.state),
54        }
55    }
56}
57
58#[derive(Clone, Debug)]
59pub enum BlocksSince {
60    Normal(Vec<Arc<Block>>),
61    Reorg(Vec<Arc<Block>>),
62}
63
64#[derive(Clone, Debug)]
65pub enum BlockStreamItem {
66    NewBlock(Arc<Block>),
67    Reorg(Vec<Arc<Block>>),
68}
69
70#[derive(Clone, Debug)]
71pub enum StatesSince<T> {
72    Normal(Vec<BlockState<T>>),
73    Reorg(Vec<BlockState<T>>),
74}
75
76#[derive(Clone, Debug)]
77pub enum StateStreamItem<T> {
78    NewState(BlockState<T>),
79    Reorg(Vec<BlockState<T>>),
80}
81
82#[derive(Clone, Debug)]
83pub enum QueryBlock {
84    Latest,
85    BlockHash(H256),
86    BlockNumber(U64),
87    BlockDepth(usize),
88    Block(Arc<Block>),
89}
90
91/// Error that might occur when trying to convert [`ethers::Block`] into
92/// [`Block`].
93///
94/// [`Block`]: self::Block
95/// [`ethers::Block`]: self::ethers::types::Block
96#[derive(Snafu, Clone, Debug)]
97pub enum BlockError {
98    #[snafu(display("Block has no hash"))]
99    MissingHash,
100    #[snafu(display("Block has no number"))]
101    MissingNumber,
102    #[snafu(display("Block has no logs bloom"))]
103    MissingLogsBloom,
104}
105
106#[cfg(feature = "ethers")]
107impl<T> std::convert::TryFrom<self::ethers::types::Block<T>> for Block {
108    type Error = BlockError;
109
110    fn try_from(b: self::ethers::types::Block<T>) -> Result<Self, Self::Error> {
111        Ok(Self {
112            hash: H256::from(b.hash.ok_or(BlockError::MissingHash)?.0),
113            number: U64(b.number.ok_or(BlockError::MissingNumber)?.0),
114            parent_hash: H256::from(b.parent_hash.0),
115            timestamp: U256(b.timestamp.0),
116            logs_bloom: Bloom::from(b.logs_bloom.ok_or(BlockError::MissingLogsBloom)?.0),
117        })
118    }
119}
120
121impl From<H256> for QueryBlock {
122    fn from(h: H256) -> Self {
123        QueryBlock::BlockHash(h)
124    }
125}
126
127impl From<&H256> for QueryBlock {
128    fn from(h: &H256) -> Self {
129        QueryBlock::BlockHash(*h)
130    }
131}
132
133impl From<U64> for QueryBlock {
134    fn from(n: U64) -> Self {
135        QueryBlock::BlockNumber(n)
136    }
137}
138
139impl From<&U64> for QueryBlock {
140    fn from(n: &U64) -> Self {
141        QueryBlock::BlockNumber(*n)
142    }
143}
144
145impl From<Block> for QueryBlock {
146    fn from(b: Block) -> Self {
147        QueryBlock::Block(Arc::new(b))
148    }
149}
150
151impl From<Arc<Block>> for QueryBlock {
152    fn from(b: Arc<Block>) -> Self {
153        QueryBlock::Block(b)
154    }
155}
156
157impl From<&Block> for QueryBlock {
158    fn from(b: &Block) -> Self {
159        QueryBlock::from(b.clone())
160    }
161}