How to update a while loop variable properly #946
Answered
by
hmzdot
onyedikachi-david
asked this question in
Q&A
-
I've been trying to update a while loop variable as seen in the below but I dont know where I am getting it wrong... const usersPaidSet = new Set();
const contributorsSet = new Set();
var [usersPaid, numUsers] = [0, 0];
invariant(usersPaid <= numUsers);
while(true) {
commit();
PC.publish()
PP.phase(Phase.Contribution())
const start = lastConsensusTime() + 2;
const deadline = start + duration;
const [ timeRemaining, keepGoing ] = makeDeadline(deadline);
const [IusersPaid, InumUsers] =
parallelReduce([ usersPaid, numUsers ])
.invariant(usersPaid <= numUsers)
.while(keepGoing())
.api(
C.contribute,
(() => amt),
((returnFunc) => {
contributorsSet.insert(this);
returnFunc(null)
// InumUsers = InumUsers + 1;
return [IusersPaid, InumUsers + 1]
})
)
.timeRemaining(timeRemaining());;
commit()
PC.publish()
PP.phase(Phase.Payment());
commit();
const [[], returnPayFunc] =
call(A.requestPayment)
.pay(() => 0)
.assume(() => {
check(!usersPaidSet.member(this));
})
transfer(balance()).to(this)
usersPaidSet.insert(this);
returnPayFunc(null);
commit();
PC.publish();
[usersPaid, numUsers] = [usersPaid + 1, InumUsers];
continue;
}
commit();
PC.publish()
PP.phase(Phase.Finished());
commit();
exit();
})
; |
Beta Was this translation helpful? Give feedback.
Answered by
hmzdot
May 10, 2022
Replies: 1 comment 3 replies
-
Error says code can have I suggest you to either make sure this scenario doesn't happen, or update you loop variables in such way |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
jeapostrophe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error says code can have
usersPaid = 0
andInumUsers = 0
after the parallelReduce and in that case it would break the invariant of the while loop because you update the loop with[usersPaid + 1, InumUsers]
I suggest you to either make sure this scenario doesn't happen, or update you loop variables in such way