From dfebf977b05332aedecf84588eb1fce2fa47cf47 Mon Sep 17 00:00:00 2001 From: Scott Piriou <30843220+pscott@users.noreply.github.com> Date: Wed, 8 May 2024 14:15:29 +0200 Subject: [PATCH] fix draw scenario --- src/routes.rs | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/routes.rs b/src/routes.rs index 0ac2435..0baf18d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -521,20 +521,29 @@ impl ProposalInfo { return Err("no choices"); } - // Return an error if two scores are equal - if self.scores_by_choice.windows(2).any(|w| w[0] == w[1]) { - return Err("scores are equal"); + let index = self + .scores_by_choice + .iter() + .enumerate() + .max_by(|(_, a), (_, b)| a.total_cmp(b)) + .map(|(i, _)| i) + .unwrap() + + 1; // Adding +1 because the `choice` is 1-indexed on the hub side + + let highest_score = self.scores_by_choice[index - 1]; + + // Return an error if the highest score appears more than once + if self + .scores_by_choice + .iter() + .filter(|&s| *s == highest_score) + .count() + > 1 + { + Err("proposal ended in a draw") + } else { + Ok(Some(index)) } - - Ok(Some( - self.scores_by_choice - .iter() - .enumerate() - .max_by(|(_, a), (_, b)| a.total_cmp(b)) - .map(|(i, _)| i) - .unwrap() - + 1, // Adding +1 because the `choice` is 1-indexed on the hub side - )) } pub fn get_bribed_choice(&self, eligibility: &BoostEligibility) -> Result, &str> {