-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Problem
For some use cases, particularly when Smart Contract are based on time or blocks' height conditions, it is useful to conveniently "wait" for a given timestamp or block.
Moreover, when things run on a LocalNet in DevMode it is possible to "fast forward" both time and blocks.
It would be very useful if such methods could have a nice UX (a less boilerplate code).
Solution
Add to AlgorandClient and LocalNet (or some better place) convenient methods to fetch time/blocks, wait for time/blocks conditions, jump to time/block (in DevMode).
Proposal
Add to the AlgorandClient the following methods:
getLastRound: returns the last committed round fromAlgodgetLatestTimestamp: returns the latest UNIX timestamp committed with the last round fromAlgodwaitUntilRound: is a "sleep" conditions that alts the execution until a given round is committed (it could leveragegetLastRound)waitUntilTimestamp: is a "sleep" conditions that alts the execution until a given UNIX timestamp is committed (it could leveragegetLatestTimestamp)
In order to speed up tests when things run LocalNet (in DevMode), the following methods can be added (not in AlgorandClient, since it's DevMode specific):
blockWarp: generate blocks to jump directly to a given blocks' height (blocks are instantly generated as soon as a transaction is committed in DevMode)timeWarp: set a proper time offset to commit a block with the desired UNIX timestamp.
Specifically for timeWarp I'd suggest the following implementation to jump directly to toTimestamp:
- Get the
latestTimestampwithgetLatestTimestamp - Set the timestamp offset with the
Algodmethod to:toTimestamp-latestTimestamp - Generate a block to commit the desired offset (this could use
blockWarpwith a single block generation) - Reset the timestamp offset to
0
Pros and Cons
Pros: speed up test, less boilerplate code, nicer UX.
Cons: none.
Dependencies
Algorand SDK and LocalNet in DevMode