This repository has been 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
Use changes tries in query_storage RPC #1082
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6a8cc30
use changes tries in query_storage RPC
svyatonik 3c79a75
Merge branch 'master' into use_changes_tries_in_RPC
svyatonik dbb687b
Merge branch 'master' into use_changes_tries_in_RPC
svyatonik f8444a5
let + match + return + call -> match
svyatonik 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 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 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 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 |
---|---|---|
|
@@ -42,7 +42,7 @@ use state_machine::{ | |
key_changes, key_changes_proof, OverlayedChanges | ||
}; | ||
|
||
use backend::{self, BlockImportOperation}; | ||
use backend::{self, BlockImportOperation, PrunableStateChangesTrieStorage}; | ||
use blockchain::{self, Info as ChainInfo, Backend as ChainBackend, HeaderBackend as ChainHeaderBackend}; | ||
use call_executor::{CallExecutor, LocalCallExecutor}; | ||
use executor::{RuntimeVersion, RuntimeInfo}; | ||
|
@@ -339,35 +339,54 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
Ok((header, proof)) | ||
} | ||
|
||
/// Get longest range within [first; last] that is possible to use in `key_changes` | ||
/// and `key_changes_proof` calls. | ||
/// Range could be shortened from the beginning if some changes tries have been pruned. | ||
/// Returns Ok(None) if changes trues are not supported. | ||
pub fn max_key_changes_range( | ||
&self, | ||
first: NumberFor<Block>, | ||
last: BlockId<Block>, | ||
) -> error::Result<Option<(NumberFor<Block>, BlockId<Block>)>> { | ||
let (config, storage) = match self.require_changes_trie().ok() { | ||
Some((config, storage)) => (config, storage), | ||
None => return Ok(None), | ||
}; | ||
let first = first.as_(); | ||
let last_num = self.backend.blockchain().expect_block_number_from_id(&last)?.as_(); | ||
if first > last_num { | ||
return Err(error::ErrorKind::ChangesTrieAccessFailed("Invalid changes trie range".into()).into()); | ||
} | ||
let finalized_number = self.backend.blockchain().info()?.finalized_number; | ||
let oldest = storage.oldest_changes_trie_block(&config, finalized_number.as_()); | ||
let first = As::sa(::std::cmp::max(first, oldest)); | ||
Ok(Some((first, last))) | ||
} | ||
|
||
/// Get pairs of (block, extrinsic) where key has been changed at given blocks range. | ||
/// Works only for runtimes that are supporting changes tries. | ||
pub fn key_changes( | ||
&self, | ||
first: Block::Hash, | ||
last: Block::Hash, | ||
key: &[u8] | ||
first: NumberFor<Block>, | ||
last: BlockId<Block>, | ||
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. any reason to switch API from hash to number? 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. When you identify blocks range by two hashes, there's additional ambiguity - blocks could be from different forks. When you identify range by |
||
key: &StorageKey | ||
) -> error::Result<Vec<(NumberFor<Block>, u32)>> { | ||
let config = self.changes_trie_config()?; | ||
let storage = self.backend.changes_trie_storage(); | ||
let (config, storage) = match (config, storage) { | ||
(Some(config), Some(storage)) => (config, storage), | ||
_ => return Err(error::ErrorKind::ChangesTriesNotSupported.into()), | ||
}; | ||
let (config, storage) = self.require_changes_trie()?; | ||
let last_number = self.backend.blockchain().expect_block_number_from_id(&last)?.as_(); | ||
let last_hash = self.backend.blockchain().expect_block_hash_from_id(&last)?; | ||
|
||
let first_number = self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(first))?.as_(); | ||
let last_number = self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(last))?.as_(); | ||
key_changes::<_, Blake2Hasher>( | ||
&config, | ||
storage, | ||
first_number, | ||
&*storage, | ||
first.as_(), | ||
&ChangesTrieAnchorBlockId { | ||
hash: convert_hash(&last), | ||
hash: convert_hash(&last_hash), | ||
number: last_number, | ||
}, | ||
self.backend.blockchain().info()?.best_number.as_(), | ||
key) | ||
&key.0) | ||
.and_then(|r| r.map(|r| r.map(|(block, tx)| (As::sa(block), tx))).collect::<Result<_, _>>()) | ||
.map_err(|err| error::ErrorKind::ChangesTrieAccessFailed(err).into()) | ||
.map(|r| r.into_iter().map(|(b, e)| (As::sa(b), e)).collect()) | ||
} | ||
|
||
/// Get proof for computation of (block, extrinsic) pairs where key has been changed at given blocks range. | ||
|
@@ -382,7 +401,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
last: Block::Hash, | ||
min: Block::Hash, | ||
max: Block::Hash, | ||
key: &[u8] | ||
key: &StorageKey | ||
) -> error::Result<ChangesProof<Block::Header>> { | ||
self.key_changes_proof_with_cht_size( | ||
first, | ||
|
@@ -401,7 +420,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
last: Block::Hash, | ||
min: Block::Hash, | ||
max: Block::Hash, | ||
key: &[u8], | ||
key: &StorageKey, | ||
cht_size: u64, | ||
) -> error::Result<ChangesProof<Block::Header>> { | ||
struct AccessedRootsRecorder<'a, Block: BlockT> { | ||
|
@@ -431,14 +450,9 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
} | ||
} | ||
|
||
let config = self.changes_trie_config()?; | ||
let storage = self.backend.changes_trie_storage(); | ||
let (config, storage) = match (config, storage) { | ||
(Some(config), Some(storage)) => (config, storage), | ||
_ => return Err(error::ErrorKind::ChangesTriesNotSupported.into()), | ||
}; | ||
|
||
let (config, storage) = self.require_changes_trie()?; | ||
let min_number = self.backend.blockchain().expect_block_number_from_id(&BlockId::Hash(min))?; | ||
|
||
let recording_storage = AccessedRootsRecorder::<Block> { | ||
storage, | ||
min: min_number.as_(), | ||
|
@@ -462,7 +476,7 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
number: last_number, | ||
}, | ||
max_number.as_(), | ||
key | ||
&key.0 | ||
) | ||
.map_err(|err| error::Error::from(error::ErrorKind::ChangesTrieAccessFailed(err)))?; | ||
|
||
|
@@ -512,6 +526,16 @@ impl<B, E, Block, RA> Client<B, E, Block, RA> where | |
Ok(proof) | ||
} | ||
|
||
/// Returns changes trie configuration and storage or an error if it is not supported. | ||
fn require_changes_trie(&self) -> error::Result<(ChangesTrieConfiguration, &B::ChangesTrieStorage)> { | ||
let config = self.changes_trie_config()?; | ||
let storage = self.backend.changes_trie_storage(); | ||
match (config, storage) { | ||
(Some(config), Some(storage)) => Ok((config, storage)), | ||
_ => Err(error::ErrorKind::ChangesTriesNotSupported.into()), | ||
} | ||
} | ||
|
||
/// Create a new block, built on the head of the chain. | ||
pub fn new_block<InherentData>( | ||
&self | ||
|
@@ -1697,9 +1721,8 @@ pub(crate) mod tests { | |
let (client, _, test_cases) = prepare_client_with_key_changes(); | ||
|
||
for (index, (begin, end, key, expected_result)) in test_cases.into_iter().enumerate() { | ||
let begin = client.block_hash(begin).unwrap().unwrap(); | ||
let end = client.block_hash(end).unwrap().unwrap(); | ||
let actual_result = client.key_changes(begin, end, &key).unwrap(); | ||
let actual_result = client.key_changes(begin, BlockId::Hash(end), &StorageKey(key)).unwrap(); | ||
match actual_result == expected_result { | ||
true => (), | ||
false => panic!(format!("Failed test {}: actual = {:?}, expected = {:?}", | ||
|
This file contains 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 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 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 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
Oops, something went wrong.
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.
Sorry for being late to the party, this can just be replaced by
self.min_blocks_to_keep.unwrap_or(1)
.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.
Welcome! :) No, it can't, because
None
branch hasreturn
in it. I've changed code a bit so that now there's no explicit return + temp var.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.
Ohh okay. ty :)