Skip to content

Commit

Permalink
add StatusType::combine helper fn
Browse files Browse the repository at this point in the history
  • Loading branch information
AloeareV committed Feb 18, 2025
1 parent 4c04fc7 commit 745cd85
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 91 deletions.
40 changes: 2 additions & 38 deletions zaino-state/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,7 @@ impl ZcashService for FetchService {
let mempool_status = self.mempool.status();
let block_cache_status = self.block_cache.status();

match (mempool_status, block_cache_status) {
// If either is Closing, return Closing.
(StatusType::Closing, _) | (_, StatusType::Closing) => StatusType::Closing,
// If either is Offline or CriticalError, return CriticalError.
(StatusType::Offline, _)
| (_, StatusType::Offline)
| (StatusType::CriticalError, _)
| (_, StatusType::CriticalError) => StatusType::CriticalError,
// If either is RecoverableError, return RecoverableError.
(StatusType::RecoverableError, _) | (_, StatusType::RecoverableError) => {
StatusType::RecoverableError
}
// If either is Spawning, return Spawning.
(StatusType::Spawning, _) | (_, StatusType::Spawning) => StatusType::Spawning,
// If either is Syncing, return Syncing.
(StatusType::Syncing, _) | (_, StatusType::Syncing) => StatusType::Syncing,
// Otherwise, return Ready.
_ => StatusType::Ready,
}
mempool_status.combine(block_cache_status)
}

/// Shuts down the StateService.
Expand Down Expand Up @@ -182,25 +164,7 @@ impl FetchServiceSubscriber {
let mempool_status = self.mempool.status();
let block_cache_status = self.block_cache.status();

match (mempool_status, block_cache_status) {
// If either is Closing, return Closing.
(StatusType::Closing, _) | (_, StatusType::Closing) => StatusType::Closing,
// If either is Offline or CriticalError, return CriticalError.
(StatusType::Offline, _)
| (_, StatusType::Offline)
| (StatusType::CriticalError, _)
| (_, StatusType::CriticalError) => StatusType::CriticalError,
// If either is RecoverableError, return RecoverableError.
(StatusType::RecoverableError, _) | (_, StatusType::RecoverableError) => {
StatusType::RecoverableError
}
// If either is Spawning, return Spawning.
(StatusType::Spawning, _) | (_, StatusType::Spawning) => StatusType::Spawning,
// If either is Syncing, return Syncing.
(StatusType::Syncing, _) | (_, StatusType::Syncing) => StatusType::Syncing,
// Otherwise, return Ready.
_ => StatusType::Ready,
}
mempool_status.combine(block_cache_status)
}
}

Expand Down
40 changes: 2 additions & 38 deletions zaino-state/src/local_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,7 @@ impl BlockCache {
},
};

match (non_finalised_state_status, finalised_state_status) {
// If either is Closing, return Closing.
(StatusType::Closing, _) | (_, StatusType::Closing) => StatusType::Closing,
// If either is Offline or CriticalError, return CriticalError.
(StatusType::Offline, _)
| (_, StatusType::Offline)
| (StatusType::CriticalError, _)
| (_, StatusType::CriticalError) => StatusType::CriticalError,
// If either is RecoverableError, return RecoverableError.
(StatusType::RecoverableError, _) | (_, StatusType::RecoverableError) => {
StatusType::RecoverableError
}
// If either is Spawning, return Spawning.
(StatusType::Spawning, _) | (_, StatusType::Spawning) => StatusType::Spawning,
// If either is Syncing, return Syncing.
(StatusType::Syncing, _) | (_, StatusType::Syncing) => StatusType::Syncing,
// Otherwise, return Ready.
_ => StatusType::Ready,
}
non_finalised_state_status.combine(finalised_state_status)
}

/// Sets the block cache to close gracefully.
Expand Down Expand Up @@ -220,25 +202,7 @@ impl BlockCacheSubscriber {
},
};

match (non_finalised_state_status, finalised_state_status) {
// If either is Closing, return Closing.
(StatusType::Closing, _) | (_, StatusType::Closing) => StatusType::Closing,
// If either is Offline or CriticalError, return CriticalError.
(StatusType::Offline, _)
| (_, StatusType::Offline)
| (StatusType::CriticalError, _)
| (_, StatusType::CriticalError) => StatusType::CriticalError,
// If either is RecoverableError, return RecoverableError.
(StatusType::RecoverableError, _) | (_, StatusType::RecoverableError) => {
StatusType::RecoverableError
}
// If either is Spawning, return Spawning.
(StatusType::Spawning, _) | (_, StatusType::Spawning) => StatusType::Spawning,
// If either is Syncing, return Syncing.
(StatusType::Syncing, _) | (_, StatusType::Syncing) => StatusType::Syncing,
// Otherwise, return Ready.
_ => StatusType::Ready,
}
non_finalised_state_status.combine(finalised_state_status)
}
}

Expand Down
24 changes: 24 additions & 0 deletions zaino-state/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,28 @@ impl StatusType {

format!("{}{}{}", color_code, symbol, "\x1b[0m")
}

/// Look at two statuses, and return the more
/// 'severe' of the two statuses
pub fn combine(self, other: StatusType) -> StatusType {
match (self, other) {
// If either is Closing, return Closing.
(StatusType::Closing, _) | (_, StatusType::Closing) => StatusType::Closing,
// If either is Offline or CriticalError, return CriticalError.
(StatusType::Offline, _)
| (_, StatusType::Offline)
| (StatusType::CriticalError, _)
| (_, StatusType::CriticalError) => StatusType::CriticalError,
// If either is RecoverableError, return RecoverableError.
(StatusType::RecoverableError, _) | (_, StatusType::RecoverableError) => {
StatusType::RecoverableError
}
// If either is Spawning, return Spawning.
(StatusType::Spawning, _) | (_, StatusType::Spawning) => StatusType::Spawning,
// If either is Syncing, return Syncing.
(StatusType::Syncing, _) | (_, StatusType::Syncing) => StatusType::Syncing,
// Otherwise, return Ready.
_ => StatusType::Ready,
}
}
}
16 changes: 1 addition & 15 deletions zainod/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,7 @@ impl Indexer {
None => return 7,
};

match (service_status, server_status) {
// If either is Closing, return Closing.
(StatusType::Closing, _) | (_, StatusType::Closing) => 4,
// If either is Offline or CriticalError, return CriticalError.
(StatusType::Offline, _)
| (_, StatusType::Offline)
| (StatusType::CriticalError, _)
| (_, StatusType::CriticalError) => 7,
// If either is Spawning, return Spawning.
(StatusType::Spawning, _) | (_, StatusType::Spawning) => 0,
// If either is Syncing, return Syncing.
(StatusType::Syncing, _) | (_, StatusType::Syncing) => 1,
// Otherwise, return Ready.
_ => 2,
}
StatusType::combine(service_status, server_status) as u16
}

/// Returns the current StatusType of the indexer.
Expand Down

0 comments on commit 745cd85

Please sign in to comment.