kabu_core_blockchain/
blockchain_state.rs1use kabu_core_actors::SharedState;
2use kabu_evm_db::DatabaseKabuExt;
3use kabu_types_blockchain::KabuDataTypes;
4use kabu_types_entities::{BlockHistory, BlockHistoryState, MarketState};
5use revm::{Database, DatabaseCommit, DatabaseRef};
6
7#[derive(Clone)]
8pub struct BlockchainState<DB: Clone + Send + Sync + 'static, LDT: KabuDataTypes> {
9 market_state: SharedState<MarketState<DB>>,
10 block_history_state: SharedState<BlockHistory<DB, LDT>>,
11}
12
13impl<
14 DB: DatabaseRef + Database + DatabaseCommit + BlockHistoryState<LDT> + DatabaseKabuExt + Send + Sync + Clone + Default + 'static,
15 LDT: KabuDataTypes,
16 > Default for BlockchainState<DB, LDT>
17{
18 fn default() -> Self {
19 Self::new()
20 }
21}
22
23impl<
24 DB: DatabaseRef + Database + DatabaseCommit + BlockHistoryState<LDT> + DatabaseKabuExt + Send + Sync + Clone + Default + 'static,
25 LDT: KabuDataTypes,
26 > BlockchainState<DB, LDT>
27{
28 pub fn new() -> Self {
29 BlockchainState {
30 market_state: SharedState::new(MarketState::new(DB::default())),
31 block_history_state: SharedState::new(BlockHistory::<DB, LDT>::new(10)),
32 }
33 }
34
35 pub fn new_with_market_state(market_state: MarketState<DB>) -> Self {
36 Self { market_state: SharedState::new(market_state), block_history_state: SharedState::new(BlockHistory::new(10)) }
37 }
38
39 pub fn with_market_state(self, market_state: MarketState<DB>) -> BlockchainState<DB, LDT> {
40 BlockchainState { market_state: SharedState::new(market_state), ..self.clone() }
41 }
42}
43
44impl<DB: Clone + Send + Sync, LDT: KabuDataTypes> BlockchainState<DB, LDT> {
45 pub fn market_state_commit(&self) -> SharedState<MarketState<DB>> {
46 self.market_state.clone()
47 }
48
49 pub fn market_state(&self) -> SharedState<MarketState<DB>> {
50 self.market_state.clone()
51 }
52
53 pub fn block_history(&self) -> SharedState<BlockHistory<DB, LDT>> {
54 self.block_history_state.clone()
55 }
56}