Skip to content

Commit c1913ab

Browse files
Merge pull request #345 from timilehin01010-byte/fix/issue-334-reset-guardian-votes-reset-guardian-cancel
fix: clear stale guardian votes when guardians are removed
2 parents df1f5f3 + ef85f28 commit c1913ab

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

contracts/will/src/regression_test.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,3 +211,43 @@ fn issue_193_paginate_with_remove_readd_beneficiary() {
211211
assert_eq!(ids_in_pages.len(), page1.len(), "Should retrieve all beneficiary wills without gaps");
212212
}
213213
}
214+
215+
/// Regression test for stale guardian votes after a guardian is removed and re-added.
216+
#[test]
217+
fn stale_guardian_vote_cleared_when_guardian_removed() {
218+
let (env, client, owner, _token, token_address) = setup();
219+
let guardian = Address::generate(&env);
220+
let replacement_guardian = Address::generate(&env);
221+
let beneficiary = Address::generate(&env);
222+
223+
let beneficiaries: SorobanVec<Beneficiary> = vec![
224+
&env,
225+
Beneficiary {
226+
address: beneficiary.clone(),
227+
allocation: Allocation::Percentage(10_000),
228+
},
229+
];
230+
let tokens: SorobanVec<(Address, i128)> = vec![&env, (token_address, 100_000_i128)];
231+
let will_id = client.create_will(
232+
&owner,
233+
&tokens,
234+
&beneficiaries,
235+
&90,
236+
&7,
237+
&vec![&env, guardian.clone()],
238+
&1,
239+
&None,
240+
&0,
241+
);
242+
243+
client.submit_guardian_vote(&will_id, &guardian);
244+
assert!(client.has_guardian_voted(&will_id, &guardian));
245+
246+
client.update_guardians(&will_id, &owner, &vec![&env, replacement_guardian.clone()]);
247+
client.update_guardians(&will_id, &owner, &vec![&env, guardian.clone()]);
248+
249+
assert!(
250+
!client.has_guardian_voted(&will_id, &guardian),
251+
"re-added guardian should not retain a vote from before removal"
252+
);
253+
}

contracts/will/src/storage.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ pub fn adjust_locked_value(env: &Env, token: &Address, delta: i128) {
206206
/// it just stops being renewed.
207207
pub fn save_will(env: &Env, will: &Will) {
208208
let key = DataKey::Will(will.id);
209+
if let Some(previous) = env.storage().persistent().get::<_, Will>(&key) {
210+
for guardian in previous.guardians.iter() {
211+
let address = guardian.address.clone();
212+
let mut still_guardian = false;
213+
for current in will.guardians.iter() {
214+
if current.address == address {
215+
still_guardian = true;
216+
break;
217+
}
218+
}
219+
if !still_guardian {
220+
env.storage()
221+
.persistent()
222+
.remove(&DataKey::GuardianVote(will.id, address.clone()));
223+
env.storage()
224+
.persistent()
225+
.remove(&DataKey::GuardianCancelVote(will.id, address));
226+
}
227+
}
228+
}
209229
env.storage().persistent().set(&key, will);
210230
if !matches!(will.status, WillStatus::Released | WillStatus::Cancelled) {
211231
env.storage()

0 commit comments

Comments
 (0)