-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect timestamp is used when querying at previous block heights #12226
Comments
I looked into this bug and it does seem to be an sdk thing. A number of contracts check block height or block time when performing queries. For example, to see if a voting period is open, or the state of a vesting contract. Using this info to calculate the result. Naturally, we pass in what we get from Context. However, this doesn't seem to be set for any historical query. This should affect eg. historical x/gov voting queries as well and is not just a "CosmWasm thing" but an oversight in the sdk made more clear by the many developers using CosmWasm for more and more use cases. I'd love some feedback here |
Am I correct in understanding that you want |
I think it'd be that |
Yes, but we'd have to query for it I think @ValarDragon. Alternatively, we can keep them in memory (at |
It would be good to have a proper historical environment for the historical queries. I think setting If this is not possible, we could just document this limitation of historical queries, but it seems a number of teams are assuming they work. If there were an alternative (indexing?) service that could return this info, not the chain, that could also work. We do not need proofs on this, as those are executing logic (it doesn't affect raw queries that can be proven) |
Given that there are query paths that completely bypass Tendermint all together, I just can't think of a clean way to handle this (see The only relatively clean solution I can really think of, within the constraints of |
Hey wanted to check in on this. This is an important issue for clients / things to be resyncable! I see its in the sprint label, just wanted to comment that if folks don't have capacity I can try to help with this. Also can we get the solution for this backported to the SDK 43 and 45 lines? I believe its needed back to 43 to ensure accuracy of queries for any indexer trying to rebuild query results. |
For you @ValarDragon, I'll do it. |
TYSM!! :) |
naive question, could someone share me a query where a block in the past has a timestamp in the present? The demo in the issue is a historical query from within the state machine. This isnt the best example, i would say, since historical queries from within the state machine aren't supported and should not be used by apps |
So the Osmosis TWAP query "GeometricTwapToNow" will return an error in the state machine if "StartTime" is after "ctx.BlockTime()". Source: https://github.com/osmosis-labs/osmosis/blob/main/x/twap/api.go#L122 This block https://www.mintscan.io/osmosis/blocks/8770000 occured yesterday, March 19th. So if I provide a start time of March 20th at that block height, we would have gotten the mentioned error. Here is what happens if I actually do that:
What I should get if timestamp were set properly in the historical context (done by using a timestamp in future relative to time of query):
In the latter, you see that the end time is the time of query, even though I used a historic block whose time was on march 19th / march 18th depending on your timezone. (Depending on time you query, you may need to adjust the start time in the latter) |
i see, im worried this may create a mess with our pruning strategy. If i prune blocks but not app state then this wouldnt work either. I dont think the extra query we do now in the PR proposed is sufficient. Storing a sort of map in baseapp or somewhere of height: timestamp. Then the sdk pruning would influence the pruning of the map. |
A map will not work unless it's persisted to disk, which is redundant I think. I have another solution which is to store the timestamp in |
Summary of Bug
When querying at a previous block height, the function
createQueryContext
is run inbaseapp/abci.go
. At the end of this function the context is returned as:This uses the latest block's time, rather than the timestamp of the requested block. This causes problems for example when querying CosmWasm contracts with SmartQuery, as this context is passed to the CosmWasm querier and passed into the contract's SmartQuery inside the
env.block.time
. If the SmartQuery depends on time, an incorrect result will be returned.Version
0.45.5
Steps to Reproduce
I have created a minimum viable example of this bug in this repo: https://github.com/apollodao/querier-bug-demo
It simply returns the time when queried with query
{"get_time":{}}
. I deployed this contract on Terra classic. As you can see if you query it with a previous block height, the timestamp returned is the current: https://fcd.terra.dev/terra/wasm/v1beta1/contracts/terra1pdtgxyfhpmx9jw32r28lw66983rxt824lqsmy9/store?query_msg=eyJnZXRfdGltZSI6e319&height=7983128I also confirmed it by modifying
createQueryContext
in Cosmos SDK to print out the time of ctx, and it is indeed the latest time.For Admin Use
The text was updated successfully, but these errors were encountered: