Skip to content

Commit bec8de8

Browse files
committed
feat: clean up NakamotoNodeError with thiserror
1 parent 5e6c264 commit bec8de8

File tree

3 files changed

+26
-54
lines changed

3 files changed

+26
-54
lines changed

testnet/stacks-node/src/nakamoto_node.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use stacks::monitoring::update_active_miners_count_gauge;
2828
use stacks::net::atlas::AtlasConfig;
2929
use stacks::net::relay::Relayer;
3030
use stacks::net::stackerdb::StackerDBs;
31+
use stacks::net::Error as NetError;
3132
use stacks::util_lib::db::Error as DBError;
3233
use stacks_common::types::chainstate::SortitionId;
3334
use stacks_common::types::StacksEpochId;
@@ -116,7 +117,7 @@ pub enum Error {
116117
#[error("The miner didn't accept their own block")]
117118
CannotSelfSign,
118119
#[error("A failure occurred while mining: {0}")]
119-
MiningFailure(ChainstateError),
120+
MiningFailure(#[from] ChainstateError),
120121
/// The miner didn't accept their own block
121122
#[error("The miner didn't accept their own block: {0}")]
122123
AcceptFailure(ChainstateError),
@@ -136,6 +137,9 @@ pub enum Error {
136137
/// DBError wrapper
137138
#[error("DBError: {0}")]
138139
DBError(#[from] DBError),
140+
/// NetError wrapper
141+
#[error("NetError: {0}")]
142+
NetError(#[from] NetError),
139143
}
140144

141145
impl StacksNode {

testnet/stacks-node/src/nakamoto_node/miner.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ impl BlockMinerThread {
256256
globals.block_miner();
257257
let prior_miner_result = prior_miner
258258
.join()
259-
.map_err(|_| NakamotoNodeError::MiningFailure(ChainstateError::MinerAborted))?;
259+
.map_err(|_| ChainstateError::MinerAborted)?;
260260
if let Err(e) = prior_miner_result {
261261
// it's okay if the prior miner thread exited with an error.
262262
// in many cases this is expected (i.e., a burnchain block occurred)
@@ -285,8 +285,7 @@ impl BlockMinerThread {
285285
if let Some(prior_miner) = prior_miner {
286286
Self::stop_miner(&self.globals, prior_miner)?;
287287
}
288-
let mut stackerdbs = StackerDBs::connect(&self.config.get_stacker_db_file_path(), true)
289-
.map_err(|e| NakamotoNodeError::MiningFailure(ChainstateError::NetError(e)))?;
288+
let mut stackerdbs = StackerDBs::connect(&self.config.get_stacker_db_file_path(), true)?;
290289
let mut last_block_rejected = false;
291290

292291
// now, actually run this tenure
@@ -360,9 +359,7 @@ impl BlockMinerThread {
360359
// try again, in case a new sortition is pending
361360
self.globals
362361
.raise_initiative(format!("MiningFailure: {e:?}"));
363-
return Err(NakamotoNodeError::MiningFailure(
364-
ChainstateError::MinerAborted,
365-
));
362+
return Err(ChainstateError::MinerAborted.into());
366363
}
367364
}
368365
};
@@ -1052,9 +1049,7 @@ impl BlockMinerThread {
10521049
) {
10531050
// treat a too-soon-to-mine block as an interrupt: this will let the caller sleep and then re-evaluate
10541051
// all the pre-mining checks (burnchain tip changes, signal interrupts, etc.)
1055-
return Err(NakamotoNodeError::MiningFailure(
1056-
ChainstateError::MinerAborted,
1057-
));
1052+
return Err(ChainstateError::MinerAborted.into());
10581053
}
10591054

10601055
// build the block itself
@@ -1082,13 +1077,11 @@ impl BlockMinerThread {
10821077
) {
10831078
error!("Relayer: Failure mining anchored block: {e}");
10841079
}
1085-
NakamotoNodeError::MiningFailure(e)
1080+
e
10861081
})?;
10871082

10881083
if block.txs.is_empty() {
1089-
return Err(NakamotoNodeError::MiningFailure(
1090-
ChainstateError::NoTransactionsToMine,
1091-
));
1084+
return Err(ChainstateError::NoTransactionsToMine.into());
10921085
}
10931086
let mining_key = self.keychain.get_nakamoto_sk();
10941087
let miner_signature = mining_key

testnet/stacks-node/src/nakamoto_node/relayer.rs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -877,25 +877,6 @@ impl RelayerThread {
877877
Ok(())
878878
}
879879

880-
/// Get a snapshot for an existing burn chain block given its consensus hash.
881-
fn get_block_snapshot_consensus(
882-
&self,
883-
ch: &ConsensusHash,
884-
) -> Result<BlockSnapshot, NakamotoNodeError> {
885-
SortitionDB::get_block_snapshot_consensus(self.sortdb.conn(), ch)
886-
.map_err(|e| {
887-
error!(
888-
"Relayer: failed to get block snapshot for new burn view: {:?}",
889-
e
890-
);
891-
NakamotoNodeError::SnapshotNotFoundForChainTip
892-
})?
893-
.ok_or_else(|| {
894-
error!("Relayer: failed to get block snapshot for new burn view");
895-
NakamotoNodeError::SnapshotNotFoundForChainTip
896-
})
897-
}
898-
899880
/// Get the public key hash for the mining key.
900881
fn get_mining_key_pkh(&self) -> Option<Hash160> {
901882
let Some(ref mining_key) = self.config.miner.mining_key else {
@@ -922,13 +903,7 @@ impl RelayerThread {
922903
&mut self.chainstate.index_conn(),
923904
tip_block_id,
924905
&ch,
925-
)
926-
.map_err(|e| {
927-
error!(
928-
"Relayer: Failed to get tenure-start block header for stacks tip {tip_block_id}: {e:?}"
929-
);
930-
NakamotoNodeError::ParentNotFound
931-
})?
906+
)?
932907
.ok_or_else(|| {
933908
error!(
934909
"Relayer: Failed to find tenure-start block header for stacks tip {tip_block_id}"
@@ -1005,18 +980,6 @@ impl RelayerThread {
1005980
}
1006981
}
1007982

1008-
fn get_block_snapshot(&self, ch: &ConsensusHash) -> Result<BlockSnapshot, NakamotoNodeError> {
1009-
SortitionDB::get_block_snapshot_consensus(self.sortdb.conn(), &ch)
1010-
.map_err(|e| {
1011-
error!("Relayer: failed to get block snapshot for canonical tip: {e:?}");
1012-
NakamotoNodeError::SnapshotNotFoundForChainTip
1013-
})?
1014-
.ok_or_else(|| {
1015-
error!("Relayer: failed to get block snapshot for canonical tip");
1016-
NakamotoNodeError::SnapshotNotFoundForChainTip
1017-
})
1018-
}
1019-
1020983
/// Attempt to continue a miner's tenure into the next burn block.
1021984
/// This is allowed if the miner won the last good sortition and one of the
1022985
/// following conditions is met:
@@ -1032,7 +995,12 @@ impl RelayerThread {
1032995
debug!("Relayer: successfully stopped tenure.");
1033996

1034997
// Get the necessary snapshots and state
1035-
let burn_tip = self.get_block_snapshot_consensus(&new_burn_view)?;
998+
let burn_tip =
999+
SortitionDB::get_block_snapshot_consensus(self.sortdb.conn(), &new_burn_view)?
1000+
.ok_or_else(|| {
1001+
error!("Relayer: failed to get block snapshot for new burn view");
1002+
NakamotoNodeError::SnapshotNotFoundForChainTip
1003+
})?;
10361004
let (canonical_stacks_tip_ch, canonical_stacks_tip_bh) =
10371005
SortitionDB::get_canonical_stacks_chain_tip_hash(self.sortdb.conn()).unwrap();
10381006
let canonical_stacks_tip =
@@ -1066,7 +1034,14 @@ impl RelayerThread {
10661034
return Ok(());
10671035
}
10681036

1069-
let canonical_snapshot = self.get_block_snapshot(&canonical_stacks_tip_ch)?;
1037+
let canonical_snapshot = SortitionDB::get_block_snapshot_consensus(
1038+
self.sortdb.conn(),
1039+
&canonical_stacks_tip_ch,
1040+
)?
1041+
.ok_or_else(|| {
1042+
error!("Relayer: failed to get block snapshot for canonical tip");
1043+
NakamotoNodeError::SnapshotNotFoundForChainTip
1044+
})?;
10701045
let (parent_tenure_start, block_election_snapshot, reason) = self.determine_tenure_type(
10711046
canonical_snapshot,
10721047
last_good_block_election_snapshot,

0 commit comments

Comments
 (0)