Skip to content

Commit

Permalink
Merge ValuedMammal/bdk-kyoto#49: fix: refine error sources
Browse files Browse the repository at this point in the history
20d8dbb fix: refine error sources (Rob N)

Pull request description:

  in `Client`, the only reason we have an error variant is for constructing the `Client`, which only throws one type, and sending messages, which also only throws one type of error. i removed the library error and just return those directly. the error in `builder` is now `BuilderError` and returns either a missing genesis or database error

ACKs for top commit:
  ValuedMammal:
    ACK 20d8dbb

Tree-SHA512: 795752fbbdf672aa9e76d2037b44a39581f2418e07356949dbc04ba0e16abd2cbd45ff4bd7c72a00e64aad3fb7a8c90402d83a6dec7d7106a0a0b5b537cc9c68
  • Loading branch information
ValuedMammal committed Sep 12, 2024
2 parents 62cabfb + 20d8dbb commit 419a615
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 67 deletions.
29 changes: 15 additions & 14 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

use std::{collections::HashSet, path::PathBuf, str::FromStr, time::Duration};

use bdk_chain::local_chain::MissingGenesisError;
use bdk_wallet::{KeychainKind, Wallet};
use kyoto::{
chain::checkpoints::{
Expand Down Expand Up @@ -164,7 +165,7 @@ impl<'a> LightClientBuilder<'a> {
}

/// Build a light client node and a client to interact with the node.
pub fn build(self) -> Result<(Node, Client<KeychainKind>), Error> {
pub fn build(self) -> Result<(Node, Client<KeychainKind>), BuilderError> {
let network = self.wallet.network();
let mut node_builder = NodeBuilder::new(network);
if let Some(whitelist) = self.peers {
Expand Down Expand Up @@ -232,39 +233,39 @@ impl<'a> LightClientBuilder<'a> {

/// Errors thrown by a client.
#[derive(Debug)]
pub enum Error {
pub enum BuilderError {
/// The `LocalChain` was not initialized with a genesis block.
MissingGenesis(crate::Error),
Chain(MissingGenesisError),
/// The database encountered a fatal error.
Database(DatabaseError),
}

impl std::fmt::Display for Error {
impl std::fmt::Display for BuilderError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::MissingGenesis(e) => write!(f, "genesis block not found: {e}"),
Error::Database(e) => write!(f, "fatal database error: {e}"),
BuilderError::Chain(e) => write!(f, "genesis block not found: {e}"),
BuilderError::Database(e) => write!(f, "fatal database error: {e}"),
}
}
}

impl std::error::Error for Error {
impl std::error::Error for BuilderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::MissingGenesis(e) => Some(e),
Error::Database(e) => Some(e),
BuilderError::Chain(e) => Some(e),
BuilderError::Database(e) => Some(e),
}
}
}

impl From<crate::Error> for Error {
fn from(value: crate::Error) -> Self {
Error::MissingGenesis(value)
impl From<MissingGenesisError> for BuilderError {
fn from(value: MissingGenesisError) -> Self {
BuilderError::Chain(value)
}
}

impl From<DatabaseError> for Error {
impl From<DatabaseError> for BuilderError {
fn from(value: DatabaseError) -> Self {
Error::Database(value)
BuilderError::Database(value)
}
}
64 changes: 11 additions & 53 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ where
cp: CheckPoint,
index: &KeychainTxOutIndex<K>,
client: kyoto::Client,
) -> Result<Self, Error> {
) -> Result<Self, MissingGenesisError> {
let (sender, receiver) = client.split();
Ok(Self {
sender,
Expand Down Expand Up @@ -269,25 +269,28 @@ where
}

/// Broadcast a [`Transaction`] with a [`TxBroadcastPolicy`] strategy.
pub async fn broadcast(&self, tx: Transaction, policy: TxBroadcastPolicy) -> Result<(), Error> {
pub async fn broadcast(
&self,
tx: Transaction,
policy: TxBroadcastPolicy,
) -> Result<(), ClientError> {
self.sender
.broadcast_tx(TxBroadcast {
tx,
broadcast_policy: policy,
})
.await
.map_err(Error::from)
}

/// Add more scripts to the node. For example, a user may reveal a Bitcoin address to receive a payment,
/// so this script should be added to the [`Node`].
pub async fn add_script(&self, script: impl Into<ScriptBuf>) -> Result<(), Error> {
self.sender.add_script(script).await.map_err(Error::from)
pub async fn add_script(&self, script: impl Into<ScriptBuf>) -> Result<(), ClientError> {
self.sender.add_script(script).await
}

/// Shutdown the node.
pub async fn shutdown(&self) -> Result<(), Error> {
self.sender.shutdown().await.map_err(Error::from)
pub async fn shutdown(&self) -> Result<(), ClientError> {
self.sender.shutdown().await
}

/// Get a structure to broadcast transactions. Useful for broadcasting transactions and updating concurrently.
Expand Down Expand Up @@ -318,57 +321,12 @@ impl TransactionBroadcaster {
&mut self,
tx: &Transaction,
policy: TxBroadcastPolicy,
) -> Result<(), Error> {
) -> Result<(), ClientError> {
self.sender
.broadcast_tx(TxBroadcast {
tx: tx.clone(),
broadcast_policy: policy,
})
.await
.map_err(Error::from)
}
}

/// Errors thrown by a client.
#[derive(Debug)]
pub enum Error {
/// The channel to receive a message was closed. Likely the node has stopped running.
Sender(ClientError),
/// The [`LocalChain`] provided has no genesis block.
MissingGenesis(MissingGenesisError),
}

impl core::fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Sender(e) => write!(
f,
"the receiving channel has been close. Is the node still running?: {e}"
),
Self::MissingGenesis(e) => {
write!(f, "the local chain provided has no genesis block: {e}")
}
}
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Sender(s) => Some(s),
Error::MissingGenesis(m) => Some(m),
}
}
}

impl From<MissingGenesisError> for Error {
fn from(value: MissingGenesisError) -> Self {
Error::MissingGenesis(value)
}
}

impl From<ClientError> for Error {
fn from(value: ClientError) -> Self {
Error::Sender(value)
}
}

0 comments on commit 419a615

Please sign in to comment.