I read about everything I could about certified data and query calls. But I would like to understand how I can issue “query” calls but get the latest mutated data. Where can I get more information about it? In particular is there any specific state maintained in the state tree to make this happen? Thanks in advance.
Can you please clarify more?
Query calls do not modify state so you will not be able to get any certified data.
Yes thanks for the prompt. Query calls are directed to a single node in the subnet and the response returned irrespective of whether the data is certified or not. But let’s talk about certified data in particular. I understand the client library verifies the proof and the signature before returning the result to the client. However how do we know if this is the latest data and that some other nodes do not have a more recent version of this state? Does that make sense? Thanks in advance.
That’s a very good question!
A query call gets randomly distributed to one of the replicas in the subnet and is executed against the latest certified state available on that replica. It may be that that particular replica is a bit behind (i.e. other replicas may have a newer state available). It is not always possible to tell from the certificate if this is the case. One hint can be the timestamp available in the certificate: if this is significantly behind the current time (or behind some other timestamp you’ve seen before) this indicates that the certified data is older than the latest available. Another possibility is that the canister data itself (if you’re querying a canister) may tell you that the data returned by the replica is old: for example not finding some data that you know it should be there.
Issuing multiple queries should help gain more confidence in the freshness of the data that you get, if this is crucial for the application.
I hope this helps
In case of agent-js, it checks if the certificate is not too old (by default 5 minutes) and additionally it uses a watermark to make sure the data you receive is newer than the data you received previously.
Previously before the watermark was implemented there was a risk in case of e.g. polling that you’d get state bugs like: old>new>old>newer.
This does mean in practice that it’s recommended to use a single agent instead of multiple agent instances. Else every agent would be keeping track of individual watermarks defeating the purpose of a watermark.
Thanks for the detailed response. Could you please explain to me the purpose of the /time node in the state tree? Does it just record the timestamp when a block is generated? Thanks in advance.
You’re essentially right. I think of this timestamp as being produced by the the local clock of the subnet.
In a bit more detail: the blocks produced by the consensus layer include a timestamp (this is set by the replica that produces the block and it is guaranteed to be sane: not in the past and not too far in the future). As you suggest: when a block is processed by the execution layer the timestamp is also included in the state tree.