How to set deadlines in an application? #944
-
This is an answer to Turbooo#5024's question: In order to have deadlines for certain parts of the program you have to compare current timestamp with a last acceptable timestamp. Most of the time you'll get the current timestamp with A very simple time check would be A.only(() => {
const expiration = declassify(interact.getExpiration())
})
A.publish(expiration)
// Time functions return an Either type, where its Left field is block number and Right field is seconds
// relativeSecs will return seconds so we will only care about the right part
const getSecs = (t) => t.match({ Left: (l) => 0, Right: (r) => { return r }})
const deadline = thisConsensusSecs() + getSecs(relativeSecs(expiration))
commit()
B.publish()
if (lastConsensusSecs() < deadline) {
// Not expired
} else {
// Expired
} This check is simplified with In most cases we want to keep doing something until we reach the deadline. In order to keep doing something we'll use a loop and makeDeadline. A simple while loop that'd run until a deadline is reached would be: const [timeRemaining, keepGoing] = makeDeadline(expiration)
var [] = []
invariant(balance() == 0)
while(keepGoing()) {
commit()
A.interact.log(timeRemaining())
/*
timeRemaining returns the time remaining until the deadline, If current time was 5 and deadline was 15
timeRemaining would return 10. Under this line we timeout when A publishes after enough time has passed
to exceed the deadline
*/
A.publish().timeout(timeRemaining(), () => {
Anybody.publish()
continue
})
continue;
} Using parallel reduce we can simplify this process more. // I renamed timeRemaining and keepGoing
const [untilExpire, notExpired] = makeDeadline(expiration)
const [] = parallelReduce()
.invariant(balance() == 0)
.while(notExpired())
// Here you can have .case's for participants
// and .api's for API functions
.timeRemaining(untilExpire()) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
is it possible for an account connecting to the contract after the deadline is passed to get the final values of something like the views used in the contract? |
Beta Was this translation helpful? Give feedback.
is it possible for an account connecting to the contract after the deadline is passed to get the final values of something like the views used in the contract?