Skip to content

Commit

Permalink
chore: improve Error enum names so we can remove clippy::enum_variant…
Browse files Browse the repository at this point in the history
…_name (#85)
  • Loading branch information
a-moreira authored Oct 23, 2023
1 parent c377f25 commit 9762cf3
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 101 deletions.
20 changes: 10 additions & 10 deletions crates/floresta-chain/src/pruned_utreexo/chain_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,9 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
return Err(BlockValidationErrors::NotEnoughPow.into());
}

let block_hash = block_header.validate_pow(&actual_target).map_err(|_| {
BlockchainError::BlockValidationError(BlockValidationErrors::NotEnoughPow)
})?;
let block_hash = block_header
.validate_pow(&actual_target)
.map_err(|_| BlockchainError::BlockValidation(BlockValidationErrors::NotEnoughPow))?;
Ok(block_hash)
}

Expand Down Expand Up @@ -655,24 +655,24 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
) -> Result<(), BlockchainError> {
let prev_block = self.get_ancestor(&block.header)?;
if block.header.prev_blockhash != prev_block.block_hash() {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BlockExtendsAnOrphanChain,
));
}
if !block.check_merkle_root() {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BadMerkleRoot,
));
}
if height >= self.chain_params().bip34_activation_height
&& block.bip34_block_height() != Ok(height as u64)
{
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BadBip34,
));
}
if !block.check_witness_commitment() {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BadWitnessCommitment,
));
}
Expand All @@ -686,9 +686,9 @@ impl<PersistedState: ChainStore> ChainState<PersistedState> {
let flags = 0;
Consensus::verify_block_transactions(inputs, &block.txdata, subsidy, verify_script, flags)
.map_err(|err| {
BlockchainError::BlockValidationError(BlockValidationErrors::InvalidTx(
alloc::format!("{:?}", err),
))
BlockchainError::BlockValidation(BlockValidationErrors::InvalidTx(alloc::format!(
"{:?}", err
)))
})?;
Ok(())
}
Expand Down
20 changes: 10 additions & 10 deletions crates/floresta-chain/src/pruned_utreexo/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ pub enum BlockchainError {
#[cfg(feature = "cli-blockchain")]
#[error("Json-Rpc error")]
JsonRpcError(#[from] UtreexodError),
ParsingError(bitcoin::hashes::hex::Error),
BlockValidationError(BlockValidationErrors),
Parsing(bitcoin::hashes::hex::Error),
BlockValidation(BlockValidationErrors),
InvalidProof,
UtreexoError(String),
DatabaseError(Box<dyn DatabaseError>),
ConsensusDecodeError(bitcoin::consensus::encode::Error),
Database(Box<dyn DatabaseError>),
ConsensusDecode(bitcoin::consensus::encode::Error),
ChainNotInitialized,
InvalidTip(String),
ScriptValidationFailed(script::Error),
IoError(ioError),
Io(ioError),
}

#[derive(Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -72,18 +72,18 @@ impl Display for BlockValidationErrors {

impl<T: DatabaseError> From<T> for BlockchainError {
fn from(value: T) -> Self {
BlockchainError::DatabaseError(Box::new(value))
BlockchainError::Database(Box::new(value))
}
}

impl_error_from!(BlockchainError, ioError, IoError);
impl_error_from!(BlockchainError, ioError, Io);
impl_error_from!(
BlockchainError,
bitcoin::consensus::encode::Error,
ConsensusDecodeError
ConsensusDecode
);
impl_error_from!(BlockchainError, BlockValidationErrors, BlockValidationError);
impl_error_from!(BlockchainError, bitcoin::hashes::hex::Error, ParsingError);
impl_error_from!(BlockchainError, BlockValidationErrors, BlockValidation);
impl_error_from!(BlockchainError, bitcoin::hashes::hex::Error, Parsing);
impl_error_from!(BlockchainError, String, UtreexoError);
impl_error_from!(BlockchainError, script::Error, ScriptValidationFailed);

Expand Down
13 changes: 6 additions & 7 deletions crates/floresta-chain/src/pruned_utreexo/partial_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,7 @@ impl PartialChainState {
del_hashes: Vec<bitcoin::hashes::sha256::Hash>,
) -> bool {
let height = self.current_height + 1;
if let Err(BlockchainError::BlockValidationError(e)) =
self.validate_block(block, height, inputs)
if let Err(BlockchainError::BlockValidation(e)) = self.validate_block(block, height, inputs)
{
self.error = Some(e);
return false;
Expand Down Expand Up @@ -175,25 +174,25 @@ impl PartialChainState {
inputs: HashMap<bitcoin::OutPoint, bitcoin::TxOut>,
) -> Result<(), BlockchainError> {
if !block.check_merkle_root() {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BadMerkleRoot,
));
}
if height >= self.chain_params().bip34_activation_height
&& block.bip34_block_height() != Ok(height as u64)
{
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BadBip34,
));
}
if !block.check_witness_commitment() {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BadWitnessCommitment,
));
}
let prev_block = self.get_ancestor(height)?;
if block.header.prev_blockhash != prev_block.block_hash() {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::BlockExtendsAnOrphanChain,
));
}
Expand All @@ -212,7 +211,7 @@ impl PartialChainState {
flags,
)?;
if !valid {
return Err(BlockchainError::BlockValidationError(
return Err(BlockchainError::BlockValidation(
BlockValidationErrors::InvalidTx(String::from("invalid block transactions")),
));
}
Expand Down
10 changes: 5 additions & 5 deletions crates/floresta-electrum/src/electrum_protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl<Blockchain: BlockchainInterface> ElectrumServer<Blockchain> {
let header = self
.chain
.get_block_header(&hash)
.map_err(|e| super::error::Error::ChainError(Box::new(e)))?;
.map_err(|e| super::error::Error::Blockchain(Box::new(e)))?;
let header = serialize(&header).to_hex();
json_rpc_res!(request, header)
}
Expand All @@ -143,7 +143,7 @@ impl<Blockchain: BlockchainInterface> ElectrumServer<Blockchain> {
let header = self
.chain
.get_block_header(&hash)
.map_err(|e| super::error::Error::ChainError(Box::new(e)))?;
.map_err(|e| super::error::Error::Blockchain(Box::new(e)))?;
let header = serialize(&header).to_hex();
headers.push_str(&header);
}
Expand All @@ -158,11 +158,11 @@ impl<Blockchain: BlockchainInterface> ElectrumServer<Blockchain> {
let (height, hash) = self
.chain
.get_best_block()
.map_err(|e| super::error::Error::ChainError(Box::new(e)))?;
.map_err(|e| super::error::Error::Blockchain(Box::new(e)))?;
let header = self
.chain
.get_block_header(&hash)
.map_err(|e| super::error::Error::ChainError(Box::new(e)))?;
.map_err(|e| super::error::Error::Blockchain(Box::new(e)))?;
let result = json!({
"height": height,
"hex": serialize(&header).to_hex()
Expand Down Expand Up @@ -269,7 +269,7 @@ impl<Blockchain: BlockchainInterface> ElectrumServer<Blockchain> {
deserialize(&hex).map_err(|_| super::error::Error::InvalidParams)?;
self.chain
.broadcast(&tx)
.map_err(|e| super::error::Error::ChainError(Box::new(e)))?;
.map_err(|e| super::error::Error::Blockchain(Box::new(e)))?;
let id = tx.txid();
let updated = self
.address_cache
Expand Down
6 changes: 3 additions & 3 deletions crates/floresta-electrum/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ pub enum Error {
#[error("Invalid params passed in")]
InvalidParams,
#[error("Invalid json string {0}")]
ParsingError(#[from] serde_json::Error),
Parsing(#[from] serde_json::Error),
#[error("Blockchain error")]
ChainError(Box<dyn core2::error::Error + Send + 'static>),
Blockchain(Box<dyn core2::error::Error + Send + 'static>),
#[error("IO error")]
IoError(#[from] std::io::Error),
Io(#[from] std::io::Error),
}
10 changes: 5 additions & 5 deletions crates/floresta-wire/src/p2p_wire/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use super::peer::PeerError;
#[derive(Error, Debug)]
pub enum WireError {
#[error("Blockchain error")]
BlockchainError(BlockchainError),
Blockchain(BlockchainError),
#[error("Error while writing into a channel")]
ChannelSendError(async_std::channel::SendError<NodeRequest>),
ChannelSend(async_std::channel::SendError<NodeRequest>),
#[error("Peer error")]
PeerError(PeerError),
#[error("Coinbase didn't mature")]
Expand All @@ -23,13 +23,13 @@ pub enum WireError {
#[error("Our peer is misbehaving")]
PeerMisbehaving,
#[error("Error while reading from a channel")]
ChannelRecvError(#[from] async_std::channel::RecvError),
ChannelRecv(#[from] async_std::channel::RecvError),
#[error("Generic io error")]
IoError(std::io::Error),
Io(std::io::Error),
#[error("We don't have any utreexo peers")]
NoUtreexoPeersAvailable,
#[error("We couldn't find a peer to send the request")]
NoPeerToSendRequest,
}
impl_error_from!(WireError, PeerError, PeerError);
impl_error_from!(WireError, BlockchainError, BlockchainError);
impl_error_from!(WireError, BlockchainError, Blockchain);
21 changes: 9 additions & 12 deletions crates/floresta-wire/src/p2p_wire/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ where
peer.channel
.send(req)
.await
.map_err(WireError::ChannelSendError)?;
.map_err(WireError::ChannelSend)?;
}
}
Ok(())
Expand Down Expand Up @@ -456,7 +456,7 @@ where
peer.channel
.send(req.clone())
.await
.map_err(WireError::ChannelSendError)?;
.map_err(WireError::ChannelSend)?;
return Ok(idx);
}
}
Expand All @@ -474,7 +474,7 @@ where
self.network,
&get_chain_dns_seeds(self.network),
)
.map_err(WireError::IoError)?;
.map_err(WireError::Io)?;
for address in anchors {
self.open_connection(false, address.id, address).await;
}
Expand Down Expand Up @@ -507,14 +507,14 @@ where
peer.channel
.send(NodeRequest::BroadcastTransaction(txid))
.await
.map_err(WireError::ChannelSendError)?;
.map_err(WireError::ChannelSend)?;
}
let stale = self.mempool.write().await.get_stale();
for tx in stale {
peer.channel
.send(NodeRequest::BroadcastTransaction(tx))
.await
.map_err(WireError::ChannelSendError)?;
.map_err(WireError::ChannelSend)?;
}
}
Ok(())
Expand All @@ -528,7 +528,7 @@ where
fn save_peers(&self) -> Result<(), WireError> {
self.address_man
.dump_peers(&self.datadir)
.map_err(WireError::IoError)
.map_err(WireError::Io)
}
fn get_blocks_to_download(&mut self) -> Result<Vec<BlockHash>, WireError> {
let mut blocks = Vec::new();
Expand Down Expand Up @@ -745,7 +745,7 @@ where
try_and_log!(chain
.connect_block(&block.block, proof, inputs, del_hashes)
.map_err(|e| {
if let BlockchainError::BlockValidationError(_) = &e {
if let BlockchainError::BlockValidation(_) = &e {
try_and_log!(chain.invalidate_block(block.block.block_hash()));
}
error!(
Expand Down Expand Up @@ -824,10 +824,7 @@ where
if self.state == NodeState::DownloadBlocks {
self.process_queued_blocks().await.or_else(|err| {
// This usually means we just processed all blocks, and we are done.
if matches!(
err,
WireError::BlockchainError(BlockchainError::BlockNotPresent)
) {
if matches!(err, WireError::Blockchain(BlockchainError::BlockNotPresent)) {
info!("Finished downloading blocks");
self.chain.toggle_ibd(false);
Ok(())
Expand Down Expand Up @@ -1336,7 +1333,7 @@ where
{
error!("Invalid block received by peer {} reason: {:?}", peer, e);

if let BlockchainError::BlockValidationError(e) = e {
if let BlockchainError::BlockValidation(e) = e {
// Because the proof isn't committed to the block, we can't invalidate
// it if the proof is invalid. Any other error should cause the block
// to be invalidated.
Expand Down
6 changes: 3 additions & 3 deletions crates/floresta-wire/src/p2p_wire/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ pub struct Peer<T: Transport> {
#[derive(Debug, Error)]
pub enum PeerError {
#[error("Error while sending to peer")]
SendError,
Send,
#[error("Error while reading from peer")]
ReadError(#[from] std::io::Error),
Read(#[from] std::io::Error),
#[error("Error while parsing message")]
ParseError(#[from] bitcoin::consensus::encode::Error),
Parse(#[from] bitcoin::consensus::encode::Error),
#[error("Peer sent us a message that we aren't expecting")]
UnexpectedMessage,
#[error("Peer sent us a message that is too big")]
Expand Down
Loading

0 comments on commit 9762cf3

Please sign in to comment.