Search local states by time

I have a question on searching the contract local states by time.

For example, I am constructing a smart contract at 3 pm today. Before 4 pm, there are 20 users opting-into the application.
At 5 pm, the admin would like to search all local states of the users opted-into the contract before 4 pm. However, there are another 30 users opting-into the app between 4-5 pm. The admin would like to distinguish the first 20 users from the last 30 users.

So, is there any function in smart contract that meet this requirement without assigning time stamp to opted-in users?

@yfmao,

Smart contract doesn’t have the ability to “search” or “scan” accounts for a good reason - performance.

Searching is an operation that involves loading large number of accounts into memory and making some tests over that data. Eventually, some “bad actors” would slow down the network performance for everyone.

Optimizing these searches is “classically” implemented using an index. However, adding that functionality to the node would mean that everyone would need to run that locally. This could limit the Algorand solution on certain platform that doesn’t have the required resources.

Algorand ( in a similar way to other block chains, who’s name I won’t mention ), chose to exclude that functionality from the core product. In order to accomplish that functionality, you would need to use an external product ( such as the indexer ) or have some other infrastructure to track your own application users.

To complement @tsachi’s answer, in your case, one simple solution is just to store the round of the opt-in transaction in the local state of the opted-in user.

1 Like

Thanks for the reply, @fabrice and @tsachi

I am now using indexer to search the local state with limitations on application id and round number. I am expecting the results as the local states of the opted-in accounts previous than the input round number in the specified app.

But it is showing the error of IndexHTTPError, saying that server is not supportive to this kind of request.

I will put my code and the error information here.

Any thoughts on this error?

It’s rather… buried, but you have to pass the -dev-mode flag on the indexer cmd line:

	daemonCmd.Flags().BoolVarP(&developerMode, "dev-mode", "", false, "allow performance intensive operations like searching for accounts at a particular round")

As @aojjazz explained, you indeed need to enable developer mode to get access to this feature.
It’s indeed quite buried.

That being said, unfortunately, you will most likely hit another issue: the indexer can only get the local state of applications at the current round.

Issue tracking this: backend: rewind Application state · Issue #62 · algorand/indexer · GitHub

Why do you want to get local state at a previous round?

Why do you want to keep the SHA256 values in the global state?
What extra security property are you trying to achieve?

Algorand ensures complete integrity of the local and global state of the smart contract.
The only way a local state can be changed is through an approved call to the smart contract.
If your smart contract has no bug, it is impossible for an adversary to break integrity of the local state.
If your smart contract has a bug, adding the SHA256 may not help.

Not sure of their pricing, etc. but parsiq may be useful as well if you don’t want to wrap it yourself:

Thanks @fabrice and @aojjazz for your reply. I will re-design my smart contract to avoid this problem.