fix: generated a custom key for the cache in blocks endpoint #1381
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue
When we request a block in Sidecar with a query param set to a specific value, e.g. :
/blocks/1820744?extrinsicDocs=false
: return block and show the docs in each extrinsicand right away I request again the same block but now with the query param set to a different value, e.g. :
/blocks/1820744?extrinsicDocs=true
: return block and do not show the docs in each extrinsicthe response is not updated so it still doesn't show me the docs in each extrinsic.
Root Cause
This happens because of how we cache the response. Wen we cache the block here we set the key in the cache "array" equal to the block hash. So when we request again the same block it will only check here if it finds this block hash and it will ignore the different query params. It will then find that this block was requested before and it will return the saved response. The problem is that this saved response has
extrinsicDocs
false so it will still not show me the docs.Proposed Solution
Create a key that is a combination of the block hash (request param) and the query params of the request. This key will then be used to save/retrieve responses to/from the cache.
The query params values were concatenated as
0
or1
s (since they are boolean indicatingfalse
ortrue
values).For the generation of the key, only the query params
are used since those are the ones that can be changed by the user. The options
queryFinalizedHead
andomitFinalizedTag
are not included since they depend on the params set above.Testing
This solution was tested while running sidecar connected to Polkadot. For example, the requested block :
http://127.0.0.1:8080/blocks/1820747?extrinsicDocs=false&eventDocs=true
is updated when changing the query param
extrinsicDocs
andeventDocs
to different boolean values.