From 18d394a254da197f7319f9518659051deb7dbf1c Mon Sep 17 00:00:00 2001 From: Linguists <95207870+linguists@users.noreply.github.com> Date: Wed, 25 Jun 2025 08:50:25 +0000 Subject: [PATCH 1/3] fix: keep kicked out validator --- src/lib.rs | 6 ++---- tests/test_vote.rs | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c35dc81..12a43b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,9 +77,7 @@ impl Contract { for (account_id, _) in votes { let account_current_stake = validator_stake(&account_id); self.total_voted_stake += account_current_stake; - if account_current_stake > 0 { - self.votes.insert(account_id, account_current_stake); - } + self.votes.insert(account_id, account_current_stake); } self.check_result(); self.last_epoch_height = cur_epoch_height; @@ -497,7 +495,7 @@ mod tests { // ping will update total voted stake contract.ping(); assert_eq!((contract.get_total_voted_stake().0).0, 0); - assert_eq!(contract.get_votes().len(), 0); + assert_eq!(contract.get_votes().len(), 1); } #[test] diff --git a/tests/test_vote.rs b/tests/test_vote.rs index b2e8b0d..50e455d 100644 --- a/tests/test_vote.rs +++ b/tests/test_vote.rs @@ -493,7 +493,7 @@ async fn test_unstake_after_voting() -> Result<(), Box> { let votes = owner.view(voting_contract.id(), "get_votes").await?; let votes = votes.json::>()?; - assert_eq!(votes.len(), 1); + assert_eq!(votes.len(), 2); assert!(votes.contains_key(staking_pool_contracts[1].id())); Ok(()) From 01d71cac4900ce9ff173455624978623971efe7e Mon Sep 17 00:00:00 2001 From: Linguists <95207870+linguists@users.noreply.github.com> Date: Wed, 25 Jun 2025 09:01:51 +0000 Subject: [PATCH 2/3] refactor: avoid memory allocation --- src/lib.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 12a43b8..22fbe8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,12 +72,11 @@ impl Contract { require!(self.result.is_none(), "Voting has already ended"); let cur_epoch_height = env::epoch_height(); if cur_epoch_height != self.last_epoch_height { - let votes = std::mem::take(&mut self.votes); self.total_voted_stake = 0; - for (account_id, _) in votes { - let account_current_stake = validator_stake(&account_id); + for (account_id, stake) in self.votes.iter_mut() { + let account_current_stake = validator_stake(account_id); self.total_voted_stake += account_current_stake; - self.votes.insert(account_id, account_current_stake); + *stake = account_current_stake; } self.check_result(); self.last_epoch_height = cur_epoch_height; From 2e8e82e83885b6e54ef785694d66b46cecb2375c Mon Sep 17 00:00:00 2001 From: Linguists <95207870+linguists@users.noreply.github.com> Date: Wed, 25 Jun 2025 09:16:05 +0000 Subject: [PATCH 3/3] test: kicked out validator joins again --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 22fbe8f..349d558 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -495,6 +495,14 @@ mod tests { contract.ping(); assert_eq!((contract.get_total_voted_stake().0).0, 0); assert_eq!(contract.get_votes().len(), 1); + // validator(1) is back to validator set at epoch 3 + validators.insert(validator(1).to_string(), NearToken::from_yoctonear(40)); + let context = get_context_with_epoch_height(&voting_contract_id(), 3); + set_context_and_validators(&context, &validators); + // ping will update total voted stake after validator(1) is back + contract.ping(); + assert_eq!((contract.get_total_voted_stake().0).0, 40); + assert_eq!(contract.get_votes().len(), 1); } #[test]