Can we query block data by timestamp? If so what time is the correct one to use? For a typical block object, there's a time property that seems to always be undefined
, but the first extrinsic (index 0) usually provides timestamp. Is there a way to query details of all blocks whose timestamp is between some date range?
3 Answers
It's currently not possible to do that with substrate, short of manually searching through the blocks (you can use binary search). This kind of functionality will most likely never be provided by substrate as it can be built by external tools that create indexed databases, that allow answering such queries efficiently. You can find a list here of tooling that already exists that maybe helpful for your use case.
-
Would be nice to be able to go from timestamp to nearest block header though. Holding that data and having an RPC call for it would be a lot more efficient than having users binary search you via RPC calls. At the moment there's no linkage between the relay chains except via timestamps - it's the only way to find out what kusama block is happening when polkadot is on block X. Those external tools will be able to do it too but they will be centralised to one degree or another.– SquirrelJun 4, 2022 at 8:12
-
Both subsquid and subql are open-source tools that you can run yourself to create your own index, they are only centralized if you want to use someone else's already indexed data. Substrate does not even require that blockchains have a timestamp so creating an RPC call for this would be breaking this abstraction. While I do agree that timestamp is probably a very common and useful dimension to query I still believe that this is a job for other tools (and thankfully those tools already exist).– AndréJun 24, 2022 at 16:32
Can we query block data by timestamp?
See André's answer above.
... what time is the correct one to use?
Just want to confirm that the first argument of extrinsic timestamp seems to be the conventional way. See example in Subquery tutorial
-
The parameter passed to
timestamp.set
is indeed the current UNIX timestamp. You can see the definition of thetimestamp.set
extrinsic here: github.com/paritytech/substrate/blob/master/frame/timestamp/src/…– AndréMar 2, 2022 at 14:49
We can do better than a binary search through block numbers to find the nearest timestamp.
If we have a starting timestamp A from the chain in question then we can use the estimation techniques listed in this question to tell us how many blocks forward or backwards we likely need to go (request blocks by block number not by block hash):
How to estimate the timestamp of a future block in Substrate?
Our first guess gets us timestamp B. If we diff A and B them we get a much more realistic value of how long blocks take over that period, and from that we can make a more accurate prediction than our initial prediction.
-
1Yeah I wrote a function that does better than binary search based on the block time: github.com/joepetrowski/rpc-sidecar-requests/blob/083a9f0/… Jun 4, 2022 at 14:41
-
My rough cut rust version: github.com/gilescope/dotsamatown/blob/… - alas didn't see your impl until I had written it.– SquirrelJun 4, 2022 at 18:21