This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Do not return empty entries from state_queryStorage #2906
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -258,25 +258,30 @@ impl<B, E, Block: BlockT, RA> State<B, E, Block, RA> where | |
| &self, | ||
| range: &QueryStorageRange<Block>, | ||
| keys: &[StorageKey], | ||
| last_values: &mut HashMap<StorageKey, Option<StorageData>>, | ||
| changes: &mut Vec<StorageChangeSet<Block::Hash>>, | ||
| ) -> Result<()> { | ||
| let mut last_state: HashMap<_, Option<_>> = Default::default(); | ||
| for block in range.unfiltered_range.start..range.unfiltered_range.end { | ||
| let block_hash = range.hashes[block].clone(); | ||
| let mut block_changes = StorageChangeSet { block: block_hash.clone(), changes: Vec::new() }; | ||
| let id = BlockId::hash(block_hash); | ||
| for key in keys { | ||
| let (has_changed, data) = { | ||
| let curr_data = self.client.storage(&id, key)?; | ||
| let prev_data = last_state.get(key).and_then(|x| x.as_ref()); | ||
| let prev_data = last_values.get(key).and_then(|x| x.as_ref()); | ||
| (curr_data.as_ref() != prev_data, curr_data) | ||
| }; | ||
| if has_changed { | ||
| block_changes.changes.push((key.clone(), data.clone())); | ||
| } | ||
| last_state.insert(key.clone(), data); | ||
| last_values.insert(key.clone(), data); | ||
| } | ||
|
|
||
| // always insert entry for the first block, because we're claiming to provide values of | ||
| // all keys in this entry (if key has None value, it will be missing from the changes field) | ||
| if changes.is_empty() || !block_changes.changes.is_empty() { | ||
| changes.push(block_changes); | ||
| } | ||
| changes.push(block_changes); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
@@ -286,6 +291,7 @@ impl<B, E, Block: BlockT, RA> State<B, E, Block, RA> where | |
| &self, | ||
| range: &QueryStorageRange<Block>, | ||
| keys: &[StorageKey], | ||
| last_values: &HashMap<StorageKey, Option<StorageData>>, | ||
| changes: &mut Vec<StorageChangeSet<Block::Hash>>, | ||
| ) -> Result<()> { | ||
| let (begin, end) = match range.filtered_range { | ||
|
|
@@ -298,17 +304,24 @@ impl<B, E, Block: BlockT, RA> State<B, E, Block, RA> where | |
| let mut changes_map: BTreeMap<NumberFor<Block>, StorageChangeSet<Block::Hash>> = BTreeMap::new(); | ||
| for key in keys { | ||
| let mut last_block = None; | ||
| for (block, _) in self.client.key_changes(begin, end, key)? { | ||
| let mut last_value = last_values.get(key).cloned().unwrap_or_default(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have to clone here? Can't we compare with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is also used as storage for the last value (it is updated in the loop). I could use the same |
||
| for (block, _) in self.client.key_changes(begin, end, key)?.into_iter().rev() { | ||
| if last_block == Some(block) { | ||
| continue; | ||
| } | ||
|
|
||
| let block_hash = range.hashes[(block - range.first_number).saturated_into::<usize>()].clone(); | ||
| let id = BlockId::Hash(block_hash); | ||
| let value_at_block = self.client.storage(&id, key)?; | ||
| if last_value == value_at_block { | ||
| continue; | ||
| } | ||
|
|
||
| changes_map.entry(block) | ||
| .or_insert_with(|| StorageChangeSet { block: block_hash, changes: Vec::new() }) | ||
| .changes.push((key.clone(), value_at_block)); | ||
| .changes.push((key.clone(), value_at_block.clone())); | ||
| last_block = Some(block); | ||
| last_value = value_at_block; | ||
| } | ||
| } | ||
| if let Some(additional_capacity) = changes_map.len().checked_sub(changes.len()) { | ||
|
|
@@ -432,8 +445,9 @@ impl<B, E, Block, RA> StateApi<Block::Hash> for State<B, E, Block, RA> where | |
| ) -> Result<Vec<StorageChangeSet<Block::Hash>>> { | ||
| let range = self.split_query_storage_range(from, to)?; | ||
| let mut changes = Vec::new(); | ||
| self.query_storage_unfiltered(&range, &keys, &mut changes)?; | ||
| self.query_storage_filtered(&range, &keys, &mut changes)?; | ||
| let mut last_values = HashMap::new(); | ||
| self.query_storage_unfiltered(&range, &keys, &mut last_values, &mut changes)?; | ||
| self.query_storage_filtered(&range, &keys, &last_values, &mut changes)?; | ||
| Ok(changes) | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RPC method description claims that "This first returned result contains the initial state of storage for all keys.". But actually the first entry won't have a value for the key if the value is
None:{"jsonrpc":"2.0","result":[{"block":"0x61..0c","changes":[]}],"id":1}But if later the key has been changed to
Some(_)and then back toNone, the changes will look like that (mindnullin the last block' changes):{"jsonrpc":"2.0","result":[{"block":"0x61..0c","changes":[]},{"block":"0x81..8d","changes":[["0x05","0x06"]]},{"block":"0x91..9e","changes":[["0x05",null]]}],"id":1}So we are using both absence-of-key and
nullto markNonevalues. What do you think - does it worth to use onlynullfor that? If so, I could revert last commit, that itself reverts this fix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a bit confusing but it seems fine for me. The alternative would be to print out all the keys with
nullvalue in the initial block, right?While we could do that if user subscribes to specific keys (still a bit wasteful), it would not be feasible in case of a wildcard.
I'm fine with both, maybe let's hear @jacogr's opinion on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer consistency, so always showing "something" - currently on the middleware side we have to handle both
undefinedandnull- either way, as long as the order is correct, should not break anything.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, so it'll be
nulleverywhere (speaking of this method only, of course)