Skip to content

Commit 0085f73

Browse files
committed
CRC: remove clones by passing by reference where possible
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 1e31382 commit 0085f73

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

stacks-signer/src/v0/signer_state.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ impl GlobalStateEvaluator {
7171
fn determine_latest_supported_signer_protocol_version(
7272
&mut self,
7373
local_address: StacksAddress,
74-
local_update: StateMachineUpdateMessage,
74+
local_update: &StateMachineUpdateMessage,
7575
) -> Option<u64> {
76-
self.insert_update(local_address, local_update);
76+
self.insert_update(local_address, local_update.clone());
7777
let mut protocol_versions = HashMap::new();
7878
for (address, update) in &self.address_updates {
7979
let Some(weight) = self.address_weights.get(address) else {
@@ -101,9 +101,9 @@ impl GlobalStateEvaluator {
101101
pub fn determine_global_burn_view(
102102
&mut self,
103103
local_address: StacksAddress,
104-
local_update: StateMachineUpdateMessage,
104+
local_update: &StateMachineUpdateMessage,
105105
) -> Option<(ConsensusHash, u64)> {
106-
self.insert_update(local_address, local_update);
106+
self.insert_update(local_address, local_update.clone());
107107
let mut burn_blocks = HashMap::new();
108108
for (address, update) in &self.address_updates {
109109
let Some(weight) = self.address_weights.get(address) else {
@@ -126,11 +126,11 @@ impl GlobalStateEvaluator {
126126
None
127127
}
128128

129-
/// Check if there is an agreed upon global current miner viewpoint
129+
/// Check if there is an agreed upon global state
130130
pub fn determine_global_state(
131131
&mut self,
132132
local_address: StacksAddress,
133-
local_update: StateMachineUpdateMessage,
133+
local_update: &StateMachineUpdateMessage,
134134
) -> Option<SignerStateMachine> {
135135
let active_signer_protocol_version =
136136
self.determine_latest_supported_signer_protocol_version(local_address, local_update)?;
@@ -169,12 +169,12 @@ impl GlobalStateEvaluator {
169169
&mut self,
170170
signerdb: &mut SignerDb,
171171
local_address: StacksAddress,
172-
local_update: StateMachineUpdateMessage,
172+
local_update: &StateMachineUpdateMessage,
173173
) -> Option<StateMachineUpdateMinerState> {
174174
let StateMachineUpdateContent::V0 {
175175
burn_block: current_burn_block,
176176
..
177-
} = local_update.content.clone();
177+
} = local_update.content;
178178
let (global_burn_view, _) = self.determine_global_burn_view(local_address, local_update)?;
179179
if current_burn_block != global_burn_view {
180180
return None;
@@ -194,8 +194,7 @@ impl GlobalStateEvaluator {
194194
continue;
195195
}
196196

197-
let StateMachineUpdateMinerState::ActiveMiner { tenure_id, .. } = current_miner.clone()
198-
else {
197+
let StateMachineUpdateMinerState::ActiveMiner { tenure_id, .. } = current_miner else {
199198
continue;
200199
};
201200

@@ -204,7 +203,7 @@ impl GlobalStateEvaluator {
204203

205204
if *entry >= self.total_weight * 3 / 10 {
206205
let nmb_blocks = signerdb
207-
.get_globally_accepted_block_count_in_tenure(&tenure_id)
206+
.get_globally_accepted_block_count_in_tenure(tenure_id)
208207
.unwrap_or(0);
209208
if nmb_blocks > 0 || self.reached_agreement(*entry) {
210209
return Some(current_miner.clone());
@@ -762,7 +761,7 @@ impl LocalStateMachine {
762761
let old_protocol_version = local_update.active_signer_protocol_version;
763762
// First check if we should update our active protocol version
764763
let active_signer_protocol_version = eval
765-
.determine_latest_supported_signer_protocol_version(local_address, local_update.clone())
764+
.determine_latest_supported_signer_protocol_version(local_address, &local_update)
766765
.unwrap_or(old_protocol_version);
767766

768767
let StateMachineUpdateContent::V0 {
@@ -789,8 +788,7 @@ impl LocalStateMachine {
789788
}
790789

791790
// Check if we should also capitulate our miner viewpoint
792-
let Some(new_miner) =
793-
eval.capitulate_miner_view(signerdb, local_address, local_update.clone())
791+
let Some(new_miner) = eval.capitulate_miner_view(signerdb, local_address, &local_update)
794792
else {
795793
return;
796794
};
@@ -884,14 +882,7 @@ mod tests {
884882
.clone();
885883
assert_eq!(
886884
global_eval
887-
.determine_latest_supported_signer_protocol_version(
888-
local_address,
889-
global_eval
890-
.address_updates
891-
.get(&local_address)
892-
.unwrap()
893-
.clone(),
894-
)
885+
.determine_latest_supported_signer_protocol_version(local_address, &local_update,)
895886
.unwrap(),
896887
local_update.local_supported_signer_protocol_version
897888
);
@@ -926,7 +917,7 @@ mod tests {
926917

927918
assert_eq!(
928919
global_eval
929-
.determine_latest_supported_signer_protocol_version(local_address, local_update)
920+
.determine_latest_supported_signer_protocol_version(local_address, &local_update)
930921
.unwrap(),
931922
local_supported_signer_protocol_version
932923
);
@@ -946,7 +937,7 @@ mod tests {
946937

947938
assert_eq!(
948939
global_eval
949-
.determine_latest_supported_signer_protocol_version(local_address, local_update)
940+
.determine_latest_supported_signer_protocol_version(local_address, &local_update)
950941
.unwrap(),
951942
local_supported_signer_protocol_version + 1
952943
);
@@ -977,7 +968,7 @@ mod tests {
977968

978969
assert_eq!(
979970
global_eval
980-
.determine_global_burn_view(local_address, local_update.clone(),)
971+
.determine_global_burn_view(local_address, &local_update)
981972
.unwrap(),
982973
(burn_block, burn_block_height)
983974
);
@@ -999,15 +990,15 @@ mod tests {
999990

1000991
assert!(
1001992
global_eval
1002-
.determine_global_burn_view(local_address, local_update,)
993+
.determine_global_burn_view(local_address, &local_update)
1003994
.is_none(),
1004995
"We should not have reached agreement on the burn block height"
1005996
);
1006997

1007998
// Let's tip the scales over to burn block height + 1
1008999
assert_eq!(
10091000
global_eval
1010-
.determine_global_burn_view(local_address, new_update,)
1001+
.determine_global_burn_view(local_address, &new_update)
10111002
.unwrap(),
10121003
(burn_block, burn_block_height.wrapping_add(1))
10131004
);
@@ -1045,7 +1036,7 @@ mod tests {
10451036

10461037
assert_eq!(
10471038
global_eval
1048-
.determine_global_state(local_address, local_update.clone(),)
1039+
.determine_global_state(local_address, &local_update)
10491040
.unwrap(),
10501041
state_machine
10511042
);
@@ -1075,7 +1066,7 @@ mod tests {
10751066

10761067
assert!(
10771068
global_eval
1078-
.determine_global_state(local_address, local_update,)
1069+
.determine_global_state(local_address, &local_update)
10791070
.is_none(),
10801071
"We should have a disagreement about the current miner"
10811072
);
@@ -1090,7 +1081,7 @@ mod tests {
10901081
// Let's tip the scales over to a different miner
10911082
assert_eq!(
10921083
global_eval
1093-
.determine_global_state(local_address, new_update,)
1084+
.determine_global_state(local_address, &new_update)
10941085
.unwrap(),
10951086
state_machine
10961087
)
@@ -1152,7 +1143,7 @@ mod tests {
11521143
// Let's update only our own view: the evaluator will tell me to revert my viewpoint to the original miner
11531144
assert_eq!(
11541145
global_eval
1155-
.capitulate_miner_view(&mut db, local_address, new_update.clone())
1146+
.capitulate_miner_view(&mut db, local_address, &new_update)
11561147
.unwrap(),
11571148
current_miner
11581149
);
@@ -1165,7 +1156,7 @@ mod tests {
11651156
}
11661157
assert!(
11671158
global_eval
1168-
.capitulate_miner_view(&mut db, local_address, new_update.clone())
1159+
.capitulate_miner_view(&mut db, local_address, &new_update)
11691160
.is_none(),
11701161
"Evaluator should have been unable to determine a majority view and return none"
11711162
);
@@ -1177,7 +1168,7 @@ mod tests {
11771168
// Now that the blocking minority references a tenure which would actually get reorged, lets capitulate to their view
11781169
assert_eq!(
11791170
global_eval
1180-
.capitulate_miner_view(&mut db, local_address, new_update)
1171+
.capitulate_miner_view(&mut db, local_address, &new_update)
11811172
.unwrap(),
11821173
new_miner
11831174
);

0 commit comments

Comments
 (0)