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.7k
Fix the runtime-version choice. #11209
Closed
RGafiyatullin
wants to merge
6
commits into
paritytech:master
from
RGafiyatullin:rgafiyatullin-substrate-9997
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
97a55d2
Define `code-for-block(n) = state(n - 1).get(CODE)`
de4e6e3
staet-db: a note on how the pruning-configuration is implemented
6f0a404
cargo fmt
4c4078d
Merge remote-tracking branch 'origin/master' into rgafiyatullin-subst…
b0fc796
cargo fmt
8ce5095
Typo: s/constraitns/constraints/
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,26 @@ where | |
|
||
Ok(code) | ||
} | ||
|
||
fn state_with_runtime_code( | ||
&self, | ||
block_id: BlockId<Block>, | ||
) -> sp_blockchain::Result<<B as backend::Backend<Block>>::State> { | ||
use sp_runtime::traits::{One, Zero}; | ||
|
||
let block_num = self | ||
.backend | ||
.blockchain() | ||
.block_number_from_id(&block_id)? | ||
.ok_or_else(|| sp_blockchain::Error::UnknownBlock(block_id.to_string()))?; | ||
|
||
let block_num_code = if block_num.is_zero() { block_num } else { block_num - One::one() }; | ||
let block_id_code = BlockId::<Block>::Number(block_num_code); | ||
|
||
let state_code = self.backend.state_at(block_id_code)?; | ||
|
||
Ok(state_code) | ||
} | ||
} | ||
|
||
impl<Block: BlockT, B, E> Clone for LocalCallExecutor<Block, B, E> | ||
|
@@ -151,19 +171,20 @@ where | |
extensions: Option<Extensions>, | ||
) -> sp_blockchain::Result<Vec<u8>> { | ||
let mut changes = OverlayedChanges::default(); | ||
let state = self.backend.state_at(*at)?; | ||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); | ||
let state_data = self.backend.state_at(*at)?; | ||
let state_code = self.state_with_runtime_code(*at)?; | ||
|
||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state_code); | ||
Comment on lines
+174
to
+177
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. This executor will be used in both the context of general runtime calls as well as block construction (correct me if I'm wrong here @bkchr). We will need to somehow make that distinction because in the case of block construction we don't want to use the logic of picking the code from the parent, otherwise we'd end up in a situation where: we want to build block N, therefore we use the state from N-1 and the code from N-2. In the case of block construction we basically want to keep the logic as is, i.e. to create block N we already use the code from block N-1 (since N doesn't even exist in the first place). |
||
let runtime_code = | ||
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?; | ||
|
||
let runtime_code = self.check_override(runtime_code, at)?; | ||
|
||
let at_hash = self.backend.blockchain().block_hash_from_id(at)?.ok_or_else(|| { | ||
sp_blockchain::Error::UnknownBlock(format!("Could not find block hash for {:?}", at)) | ||
})?; | ||
|
||
let return_data = StateMachine::new( | ||
&state, | ||
&state_data, | ||
&mut changes, | ||
&self.executor, | ||
method, | ||
|
@@ -205,7 +226,8 @@ where | |
{ | ||
let mut storage_transaction_cache = storage_transaction_cache.map(|c| c.borrow_mut()); | ||
|
||
let state = self.backend.state_at(*at)?; | ||
let state_data = self.backend.state_at(*at)?; | ||
let state_code = self.state_with_runtime_code(*at)?; | ||
|
||
let changes = &mut *changes.borrow_mut(); | ||
|
||
|
@@ -216,15 +238,15 @@ where | |
// It is important to extract the runtime code here before we create the proof | ||
// recorder to not record it. We also need to fetch the runtime code from `state` to | ||
// make sure we use the caching layers. | ||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); | ||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state_code); | ||
|
||
let runtime_code = | ||
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?; | ||
let runtime_code = self.check_override(runtime_code, at)?; | ||
|
||
match recorder { | ||
Some(recorder) => { | ||
let trie_state = state.as_trie_backend().ok_or_else(|| { | ||
let trie_state = state_data.as_trie_backend().ok_or_else(|| { | ||
Box::new(sp_state_machine::ExecutionError::UnableToGenerateProof) | ||
as Box<dyn sp_state_machine::Error> | ||
})?; | ||
|
@@ -253,7 +275,7 @@ where | |
}, | ||
None => { | ||
let mut state_machine = StateMachine::new( | ||
&state, | ||
&state_data, | ||
changes, | ||
&self.executor, | ||
method, | ||
|
@@ -275,10 +297,11 @@ where | |
|
||
fn runtime_version(&self, id: &BlockId<Block>) -> sp_blockchain::Result<RuntimeVersion> { | ||
let mut overlay = OverlayedChanges::default(); | ||
let state = self.backend.state_at(*id)?; | ||
let state_data = self.backend.state_at(*id)?; | ||
let state_code = self.state_with_runtime_code(*id)?; | ||
let mut cache = StorageTransactionCache::<Block, B::State>::default(); | ||
let mut ext = Ext::new(&mut overlay, &mut cache, &state, None); | ||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state); | ||
let mut ext = Ext::new(&mut overlay, &mut cache, &state_data, None); | ||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state_code); | ||
let runtime_code = | ||
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?; | ||
self.executor | ||
|
@@ -292,14 +315,15 @@ where | |
method: &str, | ||
call_data: &[u8], | ||
) -> sp_blockchain::Result<(Vec<u8>, StorageProof)> { | ||
let state = self.backend.state_at(*at)?; | ||
let state_data = self.backend.state_at(*at)?; | ||
let state_code = self.state_with_runtime_code(*at)?; | ||
|
||
let trie_backend = state.as_trie_backend().ok_or_else(|| { | ||
let trie_backend = state_data.as_trie_backend().ok_or_else(|| { | ||
Box::new(sp_state_machine::ExecutionError::UnableToGenerateProof) | ||
as Box<dyn sp_state_machine::Error> | ||
})?; | ||
|
||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(trie_backend); | ||
let state_runtime_code = sp_state_machine::backend::BackendRuntimeCode::new(&state_code); | ||
let runtime_code = | ||
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?; | ||
let runtime_code = self.check_override(runtime_code, at)?; | ||
|
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
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.
As discussed earlier I think here we should fetch the header first (either by block number or block hash, depending on the block id that's passed), and then we fetch the parent hash from it. The assumption we make is that the caller of this method will make the right decision on whether to use a block number or a hash.