Skip to content

Commit c65cc35

Browse files
refactor: simplify error handling and improve code readability
1 parent 8b9cfac commit c65cc35

File tree

4 files changed

+73
-88
lines changed

4 files changed

+73
-88
lines changed

dash/src/serde_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub mod hex_bytes {
283283
if let Ok(hex) = core::str::from_utf8(v) {
284284
FromHex::from_hex(hex).map_err(E::custom)
285285
} else {
286-
return Err(E::invalid_value(serde::de::Unexpected::Bytes(v), &self));
286+
Err(E::invalid_value(serde::de::Unexpected::Bytes(v), &self))
287287
}
288288
}
289289

dash/src/sml/masternode_list_engine/message_request_verification.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl MasternodeListEngine {
280280
// Attempt verification using the "before" masternode list
281281
let initial_error = if let Some(before) = before {
282282
let Err(e) =
283-
self.verify_chain_lock_with_masternode_list(chain_lock, &before, &request_id)
283+
self.verify_chain_lock_with_masternode_list(chain_lock, before, &request_id)
284284
else {
285285
return Ok(());
286286
};
@@ -303,13 +303,11 @@ impl MasternodeListEngine {
303303
if do_check {
304304
return self.verify_chain_lock_with_masternode_list(
305305
chain_lock,
306-
&after,
306+
after,
307307
&request_id,
308308
);
309-
} else {
310-
if let Some(initial_error) = initial_error {
311-
return Err(initial_error);
312-
}
309+
} else if let Some(initial_error) = initial_error {
310+
return Err(initial_error);
313311
}
314312
}
315313

dash/src/sml/masternode_list_engine/mod.rs

Lines changed: 67 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -722,14 +722,12 @@ impl MasternodeListEngine {
722722
for (heights, quorum_type, quorum_hash, new_status) in updates {
723723
for height in heights {
724724
if let Some(masternode_list_at_height) = self.masternode_lists.get_mut(&height)
725-
{
726-
if let Some(quorum_entry_at_height) = masternode_list_at_height
725+
&& let Some(quorum_entry_at_height) = masternode_list_at_height
727726
.quorums
728727
.get_mut(&quorum_type)
729728
.and_then(|quorums| quorums.get_mut(&quorum_hash))
730-
{
731-
quorum_entry_at_height.verified = new_status.clone();
732-
}
729+
{
730+
quorum_entry_at_height.verified = new_status.clone();
733731
}
734732
}
735733
}
@@ -760,39 +758,37 @@ impl MasternodeListEngine {
760758
LLMQEntryVerificationStatus,
761759
)> = Vec::new();
762760

763-
if let Some(masternode_list_at_h) = self.masternode_lists.get_mut(&h_height) {
764-
if let Some(rotated_quorums_at_h) =
761+
if let Some(masternode_list_at_h) = self.masternode_lists.get_mut(&h_height)
762+
&& let Some(rotated_quorums_at_h) =
765763
masternode_list_at_h.quorums.get_mut(&rotation_quorum_type)
766-
{
767-
for (quorum_hash, quorum_entry) in rotated_quorums_at_h.iter_mut() {
768-
if let Some(new_status) = validation_statuses.get(quorum_hash) {
769-
if &quorum_entry.verified != new_status {
770-
quorum_entry.verified = new_status.clone();
771-
let masternode_lists_having_quorum_hash_for_quorum_type = self
772-
.quorum_statuses
773-
.entry(rotation_quorum_type)
774-
.or_default();
775-
776-
let (heights, _, status) =
777-
masternode_lists_having_quorum_hash_for_quorum_type
778-
.entry(*quorum_hash)
779-
.or_insert((
780-
BTreeSet::default(),
781-
quorum_entry.quorum_entry.quorum_public_key,
782-
LLMQEntryVerificationStatus::Unknown,
783-
));
784-
785-
updates.push((
786-
heights.clone(),
787-
rotation_quorum_type,
788-
*quorum_hash,
789-
new_status.clone(),
764+
{
765+
for (quorum_hash, quorum_entry) in rotated_quorums_at_h.iter_mut() {
766+
if let Some(new_status) = validation_statuses.get(quorum_hash)
767+
&& &quorum_entry.verified != new_status {
768+
quorum_entry.verified = new_status.clone();
769+
let masternode_lists_having_quorum_hash_for_quorum_type = self
770+
.quorum_statuses
771+
.entry(rotation_quorum_type)
772+
.or_default();
773+
774+
let (heights, _, status) =
775+
masternode_lists_having_quorum_hash_for_quorum_type
776+
.entry(*quorum_hash)
777+
.or_insert((
778+
BTreeSet::default(),
779+
quorum_entry.quorum_entry.quorum_public_key,
780+
LLMQEntryVerificationStatus::Unknown,
790781
));
791782

792-
heights.insert(h_height);
793-
*status = new_status.clone();
794-
}
795-
}
783+
updates.push((
784+
heights.clone(),
785+
rotation_quorum_type,
786+
*quorum_hash,
787+
new_status.clone(),
788+
));
789+
790+
heights.insert(h_height);
791+
*status = new_status.clone();
796792
}
797793
}
798794
}
@@ -802,28 +798,24 @@ impl MasternodeListEngine {
802798
for height in heights {
803799
if let Some(masternode_list_at_height) =
804800
self.masternode_lists.get_mut(&height)
805-
{
806-
if let Some(quorum_entry_at_height) = masternode_list_at_height
801+
&& let Some(quorum_entry_at_height) = masternode_list_at_height
807802
.quorums
808803
.get_mut(&quorum_type)
809804
.and_then(|quorums| quorums.get_mut(&quorum_hash))
810-
{
811-
quorum_entry_at_height.verified = new_status.clone();
812-
}
805+
{
806+
quorum_entry_at_height.verified = new_status.clone();
813807
}
814808
}
815809
}
816810
}
817-
} else {
818-
if let Some(qualified_rotated_quorums_per_cycle) =
819-
qualified_last_commitment_per_index.first().map(|quorum_entry| {
820-
self.rotated_quorums_per_cycle
821-
.entry(quorum_entry.quorum_entry.quorum_hash)
822-
.or_default()
823-
})
824-
{
825-
*qualified_rotated_quorums_per_cycle = qualified_last_commitment_per_index;
826-
}
811+
} else if let Some(qualified_rotated_quorums_per_cycle) =
812+
qualified_last_commitment_per_index.first().map(|quorum_entry| {
813+
self.rotated_quorums_per_cycle
814+
.entry(quorum_entry.quorum_entry.quorum_hash)
815+
.or_default()
816+
})
817+
{
818+
*qualified_rotated_quorums_per_cycle = qualified_last_commitment_per_index;
827819
}
828820

829821
#[cfg(not(feature = "quorum_validation"))]
@@ -933,16 +925,14 @@ impl MasternodeListEngine {
933925
for height in heights.iter() {
934926
if let Some(masternode_list_at_height) =
935927
self.masternode_lists.get_mut(height)
936-
{
937-
if let Some(quorum_entry) = masternode_list_at_height
928+
&& let Some(quorum_entry) = masternode_list_at_height
938929
.quorums
939930
.get_mut(quorum_type)
940931
.and_then(|quorums| {
941932
quorums.get_mut(&quorum.quorum_entry.quorum_hash)
942933
})
943-
{
944-
quorum_entry.verified = quorum.verified.clone();
945-
}
934+
{
935+
quorum_entry.verified = quorum.verified.clone();
946936
}
947937
}
948938
}
@@ -1062,28 +1052,26 @@ impl MasternodeListEngine {
10621052
.values()
10631053
.find(|quorum_entry| quorum_entry.quorum_entry.quorum_index == Some(0))
10641054
.map(|quorum_entry| quorum_entry.quorum_entry.quorum_hash)
1065-
{
1066-
if let Some(cycle_quorums) = self.rotated_quorums_per_cycle.get(&cycle_hash) {
1067-
// Only update rotating quorum statuses based on last commitment entries
1068-
for quorum in cycle_quorums {
1069-
if let Some(quorum_entry) =
1070-
hash_to_quorum_entries.get_mut(&quorum.quorum_entry.quorum_hash)
1071-
{
1072-
quorum_entry.verified = quorum.verified.clone();
1073-
}
1055+
&& let Some(cycle_quorums) = self.rotated_quorums_per_cycle.get(&cycle_hash) {
1056+
// Only update rotating quorum statuses based on last commitment entries
1057+
for quorum in cycle_quorums {
1058+
if let Some(quorum_entry) =
1059+
hash_to_quorum_entries.get_mut(&quorum.quorum_entry.quorum_hash)
1060+
{
1061+
quorum_entry.verified = quorum.verified.clone();
1062+
}
10741063

1075-
let (heights, _, status) =
1076-
masternode_lists_having_quorum_hash_for_quorum_type
1077-
.entry(quorum.quorum_entry.quorum_hash)
1078-
.or_insert((
1079-
BTreeSet::default(),
1080-
quorum.quorum_entry.quorum_public_key,
1081-
LLMQEntryVerificationStatus::Unknown,
1082-
));
1064+
let (heights, _, status) =
1065+
masternode_lists_having_quorum_hash_for_quorum_type
1066+
.entry(quorum.quorum_entry.quorum_hash)
1067+
.or_insert((
1068+
BTreeSet::default(),
1069+
quorum.quorum_entry.quorum_public_key,
1070+
LLMQEntryVerificationStatus::Unknown,
1071+
));
10831072

1084-
heights.insert(block_height);
1085-
*status = quorum.verified.clone();
1086-
}
1073+
heights.insert(block_height);
1074+
*status = quorum.verified.clone();
10871075
}
10881076
}
10891077
} else {
@@ -1119,14 +1107,13 @@ impl MasternodeListEngine {
11191107
}
11201108

11211109
for (height, quorum_type, quorum_hash, new_status) in updates {
1122-
if let Some(masternode_list_at_height) = self.masternode_lists.get_mut(&height) {
1123-
if let Some(quorum_entry_at_height) = masternode_list_at_height
1110+
if let Some(masternode_list_at_height) = self.masternode_lists.get_mut(&height)
1111+
&& let Some(quorum_entry_at_height) = masternode_list_at_height
11241112
.quorums
11251113
.get_mut(&quorum_type)
11261114
.and_then(|quorums| quorums.get_mut(&quorum_hash))
1127-
{
1128-
quorum_entry_at_height.verified = new_status;
1129-
}
1115+
{
1116+
quorum_entry_at_height.verified = new_status;
11301117
}
11311118
}
11321119

hashes/src/serde_macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub mod serde_details {
4444
if let Ok(hex) = str::from_utf8(v) {
4545
Self::Value::from_str(hex).map_err(E::custom)
4646
} else {
47-
return Err(E::invalid_value(de::Unexpected::Bytes(v), &self));
47+
Err(E::invalid_value(de::Unexpected::Bytes(v), &self))
4848
}
4949
}
5050

0 commit comments

Comments
 (0)