Skip to content

Add WalletDb Initialization to StorageNode::new() in src/storage.rs #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
```rust
use crate::comms_handler::{CommsError, Event, Node, TcpTlsConfig};
use crate::configurations::{ExtraNodeParams, StorageNodeConfig, TlsPrivateInfo};
use crate::constants::{
Expand All @@ -18,6 +19,7 @@ use crate::utils::{
to_route_pow_infos, ApiKeys, LocalEvent, LocalEventChannel, LocalEventSender, ResponseResult,
RoutesPoWInfo,
};
use crate::wallet::{WalletDb, WalletDbError, DB_SPEC}; // added WalletDb imports
use bincode::{deserialize, serialize};
use bytes::Bytes;
use serde::Serialize;
Expand Down Expand Up @@ -88,6 +90,7 @@ pub enum StorageError {
Network(CommsError),
DbError(SimpleDbError),
Serialization(bincode::Error),
WalletError(WalletDbError), // Include WalletDbError
}

impl fmt::Display for StorageError {
Expand All @@ -97,6 +100,7 @@ impl fmt::Display for StorageError {
Self::Network(err) => write!(f, "Network error: {err}"),
Self::DbError(err) => write!(f, "DB error: {err}"),
Self::Serialization(err) => write!(f, "Serialization error: {err}"),
Self::WalletError(err) => write!(f, "Wallet DB error: {err}"), // Display this error
}
}
}
Expand All @@ -108,6 +112,7 @@ impl Error for StorageError {
Self::Network(ref e) => Some(e),
Self::DbError(ref e) => Some(e),
Self::Serialization(ref e) => Some(e),
Self::WalletError(ref e) => Some(e), // Source for WalletDbError
}
}
}
Expand All @@ -130,6 +135,12 @@ impl From<bincode::Error> for StorageError {
}
}

impl From<WalletDbError> for StorageError {
fn from(other: WalletDbError) -> Self {
Self::WalletError(other)
}
}

#[derive(Debug)]
pub struct StorageNode {
node: Node,
Expand All @@ -142,10 +153,11 @@ pub struct StorageNode {
whitelisted: HashMap<SocketAddr, bool>,
shutdown_group: BTreeSet<SocketAddr>,
blockchain_item_fetched: Option<(String, BlockchainItem, SocketAddr)>,
wallet_db: Arc<Mutex<WalletDb>>, // Add WalletDb
}

impl StorageNode {
///Constructor for a new StorageNode
/// Constructor for a new StorageNode
///
/// ### Arguments
///
Expand Down Expand Up @@ -196,6 +208,8 @@ impl StorageNode {
Arc::new(Mutex::new(raw_db))
};

let wallet_db = Arc::new(Mutex::new(WalletDb::new(&DB_SPEC)?)); // Initialize WalletDb

let shutdown_group = {
let mempool = std::iter::once(mempool_addr);
let raft_peers = node_raft.raft_peer_addrs().copied();
Expand All @@ -213,6 +227,7 @@ impl StorageNode {
whitelisted: Default::default(),
shutdown_group,
blockchain_item_fetched: Default::default(),
wallet_db, // Assign WalletDb
}
.load_local_db()
}
Expand Down Expand Up @@ -241,7 +256,7 @@ impl StorageNode {
(self.db.clone(), api_addr, api_tls, api_keys, api_pow_info)
}

///Adds a uses data as the payload to create a frame, from the peer address, in the node object of this class.
/// Adds a uses data as the payload to create a frame, from the peer address, in the node object of this class.
///
/// ### Arguments
///
Expand Down Expand Up @@ -462,7 +477,7 @@ impl StorageNode {
}
}

///Handle a local event
/// Handle a local event
///
/// ### Arguments
///
Expand Down Expand Up @@ -491,7 +506,7 @@ impl StorageNode {
}
}

///Handle commit data
/// Handle commit data
///
/// ### Arguments
///
Expand Down Expand Up @@ -558,7 +573,7 @@ impl StorageNode {
}
}

/// Hanldes a new incoming message from a peer.
/// Handles a new incoming message from a peer.
///
/// ### Arguments
///
Expand Down Expand Up @@ -614,7 +629,7 @@ impl StorageNode {
}
}

///Sends the latest blockchain item fetched from storage.
/// Sends the latest blockchain item fetched from storage.
pub async fn send_blockchain_item(&mut self) -> Result<()> {
if let Some((key, item, peer)) = self.blockchain_item_fetched.take() {
match self.node.get_peer_node_type(peer).await? {
Expand All @@ -634,7 +649,7 @@ impl StorageNode {
Ok(())
}

///Stores a completed block including transactions and mining transactions.
/// Stores a completed block including transactions and mining transactions.
///
/// ### Arguments
///
Expand Down Expand Up @@ -785,7 +800,7 @@ impl StorageNode {
last_block_stored_info
}

///Stores a completed block including transactions and mining transactions.
/// Stores a completed block including transactions and mining transactions.
///
/// ### Arguments
///
Expand Down Expand Up @@ -1300,10 +1315,11 @@ pub fn decode_version_pointer(pointer: &[u8]) -> (u32, &'static str, &[u8]) {
(*version, cf, key)
}

/// Return an option, emiting a warning for errors converted to
/// Return an option, emitting a warning for errors converted to
fn ok_or_warn<V, E: fmt::Display>(r: std::result::Result<Option<V>, E>, tag: &str) -> Option<V> {
r.unwrap_or_else(|e| {
warn!("{}: {}", tag, e);
None
})
}
```
Loading