Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/target
/data
/light_client_data
/kyoto
.DS_Store
.idea/
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ tokio = { version = "1", default-features = false, features = [
rusqlite = { version = "0.31.0", features = ["bundled"], optional = true }

[features]
default = ["database"]
database = ["rusqlite"]
default = ["rusqlite"]
rusqlite = ["dep:rusqlite"]
filter-control = []

[dev-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use bitcoin::Network;
use bitcoin::ScriptBuf;

use super::{client::Client, config::NodeConfig, node::Node};
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
use crate::db::error::SqlInitializationError;
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
use crate::db::sqlite::{headers::SqliteHeaderDb, peers::SqlitePeerDb};
use crate::network::dns::{DnsResolver, DNS_RESOLVER_PORT};
use crate::network::ConnectionType;
Expand All @@ -18,7 +18,7 @@ use crate::{
};
use crate::{FilterSyncPolicy, LogLevel, PeerStoreSizeConfig, TrustedPeer};

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
/// The default node returned from the [`NodeBuilder`](crate::core).
pub type NodeDefault = Node<SqliteHeaderDb, SqlitePeerDb>;

Expand Down Expand Up @@ -210,7 +210,7 @@ impl NodeBuilder {
/// # Errors
///
/// Building a node and client will error if a database connection is denied or cannot be found.
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
pub fn build(&mut self) -> Result<(NodeDefault, Client), SqlInitializationError> {
let peer_store = SqlitePeerDb::new(self.network, self.config.data_path.clone())?;
let header_store = SqliteHeaderDb::new(self.network, self.config.data_path.clone())?;
Expand Down
52 changes: 28 additions & 24 deletions src/db/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;

/// Errors when initializing a SQL-based backend.
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
#[derive(Debug)]
pub enum SqlInitializationError {
/// A file or directory could not be opened or created.
Expand All @@ -10,7 +10,7 @@ pub enum SqlInitializationError {
SQL(rusqlite::Error),
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl core::fmt::Display for SqlInitializationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -24,7 +24,7 @@ impl core::fmt::Display for SqlInitializationError {
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl std::error::Error for SqlInitializationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand All @@ -34,22 +34,22 @@ impl std::error::Error for SqlInitializationError {
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl From<rusqlite::Error> for SqlInitializationError {
fn from(value: rusqlite::Error) -> Self {
Self::SQL(value)
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl From<std::io::Error> for SqlInitializationError {
fn from(value: std::io::Error) -> Self {
Self::IO(value)
}
}

/// Errors while reading or writing to and from a SQL-based peer backend.
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
#[derive(Debug)]
pub enum SqlPeerStoreError {
/// A consensus critical data structure is malformed.
Expand All @@ -60,7 +60,7 @@ pub enum SqlPeerStoreError {
SQL(rusqlite::Error),
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl core::fmt::Display for SqlPeerStoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -80,7 +80,7 @@ impl core::fmt::Display for SqlPeerStoreError {
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl std::error::Error for SqlPeerStoreError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand All @@ -91,66 +91,70 @@ impl std::error::Error for SqlPeerStoreError {
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl From<rusqlite::Error> for SqlPeerStoreError {
fn from(value: rusqlite::Error) -> Self {
Self::SQL(value)
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl From<bitcoin::consensus::encode::Error> for SqlPeerStoreError {
fn from(value: bitcoin::consensus::encode::Error) -> Self {
Self::Deserialize(value)
}
}

/// Errors while reading or writing to and from a SQL-based block header backend.
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
#[derive(Debug)]
pub enum SqlHeaderStoreError {
/// A consensus critical data structure is malformed.
/// The headers do not link together.
Corruption,
/// A string could not be deserialized into a known datatype.
StringConversion,
/// Consensus deserialization failed.
Deserialize(bitcoin::consensus::encode::Error),
/// An error occured performing a SQL operation.
SQL(rusqlite::Error),
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl core::fmt::Display for SqlHeaderStoreError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SqlHeaderStoreError::StringConversion => {
write!(
f,
"a string could not be deserialized into a known datatype."
)
}
SqlHeaderStoreError::SQL(e) => {
write!(f, "reading or writing from the database failed: {e}")
}
SqlHeaderStoreError::Deserialize(e) => {
write!(f, "consensus decoding failed {e}")
}
SqlHeaderStoreError::Corruption => {
write!(f, "a consensus critical data structure is malformed.")
}
}
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl std::error::Error for SqlHeaderStoreError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
SqlHeaderStoreError::Corruption => None,
SqlHeaderStoreError::StringConversion => None,
SqlHeaderStoreError::SQL(error) => Some(error),
SqlHeaderStoreError::Deserialize(error) => Some(error),
}
}
}

#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
impl From<rusqlite::Error> for SqlHeaderStoreError {
fn from(value: rusqlite::Error) -> Self {
Self::SQL(value)
}
}

#[cfg(feature = "rusqlite")]
impl From<bitcoin::consensus::encode::Error> for SqlHeaderStoreError {
fn from(value: bitcoin::consensus::encode::Error) -> Self {
Self::Deserialize(value)
}
}
2 changes: 1 addition & 1 deletion src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::chain::IndexedHeader;
/// Errors a database backend may produce.
pub mod error;
/// Persistence traits defined with SQL Lite to store data between sessions.
#[cfg(feature = "database")]
#[cfg(feature = "rusqlite")]
pub mod sqlite;
/// Traits that define the header and peer databases.
pub mod traits;
Expand Down
Loading