From 0deb8be5444f03f74ea3970e160dd7e9a2ee3f54 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Sun, 29 Dec 2024 15:06:40 +0100 Subject: [PATCH 1/2] test: unclaimed UDs out of UD reeval window --- .../universal-dividend/src/compute_claim_uds.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pallets/universal-dividend/src/compute_claim_uds.rs b/pallets/universal-dividend/src/compute_claim_uds.rs index 7d58edcea..231aac883 100644 --- a/pallets/universal-dividend/src/compute_claim_uds.rs +++ b/pallets/universal-dividend/src/compute_claim_uds.rs @@ -109,4 +109,19 @@ mod tests { (2, 110_000) ); } + + #[test] + fn very_old_unclaimed_ud_out_of_reevals() { + let past_reevals = vec![ + // (3, 100 as Balance), "old" reeval which has gone out of reevals window. + (4, 1_000 as Balance), + (5, 10_000 as Balance), + (6, 100_000 as Balance), + ]; + // All the UDs out of the reeval window must produce 0 money units + assert_eq!( + compute_claim_uds(7, 1, past_reevals.into_iter()), + (3, 111_000) + ); + } } -- GitLab From 074f4141f546126c7c6b6bdc53117ac5e6a249a9 Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Sun, 29 Dec 2024 15:07:14 +0100 Subject: [PATCH 2/2] refac: comments + renaming `ud_index` which is confusiong --- .../universal-dividend/src/compute_claim_uds.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pallets/universal-dividend/src/compute_claim_uds.rs b/pallets/universal-dividend/src/compute_claim_uds.rs index 231aac883..1c2cbe8b7 100644 --- a/pallets/universal-dividend/src/compute_claim_uds.rs +++ b/pallets/universal-dividend/src/compute_claim_uds.rs @@ -24,18 +24,22 @@ pub(super) fn compute_claim_uds<Balance: AtLeast32BitUnsigned>( ) -> (UdIndex, Balance) { let mut total_amount = Zero::zero(); let mut total_count = 0; - for (ud_index, ud_amount) in past_reevals.rev() { - if ud_index <= first_ud_index { + // We start in reverse order, i.e. the most recent reeval first + for (reeval_index, ud_amount) in past_reevals.rev() { + // Therefore, if our first UD is above the current reeval index, we have reached our final useful reeval and must break + if reeval_index <= first_ud_index { let count = current_ud_index - first_ud_index; total_amount += Balance::from(count) * ud_amount; total_count += count; // First unclaimed UD is reached; stop counting now. break; - } else { - let count = current_ud_index - ud_index; + } + // Otherwise, we consume the full reeval contained UDs + else { + let count = current_ud_index - reeval_index; total_amount += Balance::from(count) * ud_amount; total_count += count; - current_ud_index = ud_index; + current_ud_index = reeval_index; } } -- GitLab