3

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 3

5

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.

2
  • 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.
    – Squirrel
    Jun 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
1

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

1
0

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.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.