Skip to main content

snap_coin/light_node/
light_node_state.rs

1use serde::{Deserialize, Serialize};
2use tokio::sync::{RwLock, broadcast};
3
4use crate::{
5    bounded_set::BoundedSet,
6    core::{
7        block::Block,
8        transaction::{Transaction, TransactionId},
9    },
10    crypto::Hash,
11    light_node::block_meta_store::BlockMetaStore,
12    node::peer::PeerHandle,
13};
14use std::{collections::HashMap, net::SocketAddr, path::PathBuf};
15
16pub struct LightNodeState {
17    pub chain_events: broadcast::Sender<LightChainEvent>,
18    pub connected_peers: RwLock<HashMap<SocketAddr, PeerHandle>>,
19    pub seen_transactions: RwLock<BoundedSet<TransactionId>>,
20    pub seen_blocks: RwLock<BoundedSet<Hash>>,
21    meta_store: BlockMetaStore,
22}
23
24impl LightNodeState {
25    pub fn new_empty(node_path: PathBuf) -> Self {
26        Self {
27            connected_peers: RwLock::new(HashMap::new()),
28            meta_store: BlockMetaStore::new(node_path),
29            chain_events: broadcast::channel(12).0,
30            seen_transactions: RwLock::new(BoundedSet::new(1000)),
31            seen_blocks: RwLock::new(BoundedSet::new(100)),
32        }
33    }
34    pub fn meta_store(&self) -> &BlockMetaStore {
35        &self.meta_store
36    }
37}
38
39#[derive(Serialize, Deserialize, Clone)]
40pub enum LightChainEvent {
41    Block { block: Block },
42    Transaction { transaction: Transaction },
43}