Skip to content

Commit

Permalink
avoid double negatives
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed Oct 16, 2024
1 parent f032277 commit cc678c6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
28 changes: 14 additions & 14 deletions substrate/client/service/src/client/call_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ use sp_externalities::Extensions;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, HashingFor},
};
;
use sp_state_machine::{
backend::{AsTrieBackend, IgnorePendingCode},
backend::{AsTrieBackend, TryPendingCode},
OverlayedChanges, StateMachine, StorageProof,
};
use std::{cell::RefCell, sync::Arc};
Expand Down Expand Up @@ -107,12 +107,12 @@ where
let state = self.backend.state_at(at_hash)?;

// TODO: is this correct?
let ignore_pending_code = match context {
CallContext::Onchain => IgnorePendingCode::No,
CallContext::Offchain => IgnorePendingCode::Yes,
let try_pending_code = match context {
CallContext::Onchain => TryPendingCode::Yes,
CallContext::Offchain => TryPendingCode::No,
};
let state_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(&state, ignore_pending_code);
sp_state_machine::backend::BackendRuntimeCode::new(&state, try_pending_code);
let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;

Expand Down Expand Up @@ -150,15 +150,15 @@ where
let changes = &mut *changes.borrow_mut();

// TODO: is this correct?
let ignore_pending_code = match call_context {
CallContext::Onchain => IgnorePendingCode::No,
CallContext::Offchain => IgnorePendingCode::Yes,
let try_pending_code = match call_context {
CallContext::Onchain => TryPendingCode::Yes,
CallContext::Offchain => TryPendingCode::No,
};
// 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, ignore_pending_code);
sp_state_machine::backend::BackendRuntimeCode::new(&state, try_pending_code);

let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
Expand Down Expand Up @@ -207,9 +207,9 @@ where
fn runtime_version(&self, at_hash: Block::Hash) -> sp_blockchain::Result<RuntimeVersion> {
let state = self.backend.state_at(at_hash)?;
// TODO: is this correct?
let ignore_pending_code = IgnorePendingCode::Yes;
let try_pending_code = TryPendingCode::No;
let state_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(&state, ignore_pending_code);
sp_state_machine::backend::BackendRuntimeCode::new(&state, try_pending_code);

let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
Expand All @@ -231,9 +231,9 @@ where
let trie_backend = state.as_trie_backend();

// TODO: is this correct?
let ignore_pending_code = IgnorePendingCode::Yes;
let try_pending_code = TryPendingCode::No;
let state_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(trie_backend, ignore_pending_code);
sp_state_machine::backend::BackendRuntimeCode::new(trie_backend, try_pending_code);
let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;
let runtime_code = self.code_provider.maybe_override_code(runtime_code, &state, at_hash)?.0;
Expand Down
6 changes: 3 additions & 3 deletions substrate/client/service/src/client/code_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use sc_client_api::backend;
use sc_executor::{RuntimeVersion, RuntimeVersionOf};
use sp_core::traits::{FetchRuntimeCode, RuntimeCode};
use sp_runtime::traits::Block as BlockT;
use sp_state_machine::{backend::IgnorePendingCode, Ext, OverlayedChanges};
use sp_state_machine::{backend::TryPendingCode, Ext, OverlayedChanges};
use std::sync::Arc;

/// Provider for fetching `:code` of a block.
Expand Down Expand Up @@ -82,9 +82,9 @@ where
let state = self.backend.state_at(block)?;

// TODO: make sure this is correct
let ignore_pending_code = IgnorePendingCode::No;
let try_pending_code = TryPendingCode::Yes;
let state_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(&state, ignore_pending_code);
sp_state_machine::backend::BackendRuntimeCode::new(&state, try_pending_code);
let runtime_code =
state_runtime_code.runtime_code().map_err(sp_blockchain::Error::RuntimeCode)?;

Expand Down
8 changes: 4 additions & 4 deletions substrate/client/service/test/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use sp_runtime::{
ConsensusEngineId, Justifications, StateVersion,
};
use sp_state_machine::{
backend::{Backend as _, IgnorePendingCode},
backend::{Backend as _, TryPendingCode},
InMemoryBackend, OverlayedChanges, StateMachine,
};
use sp_storage::{ChildInfo, StorageKey};
Expand Down Expand Up @@ -76,7 +76,7 @@ fn construct_block(
};
let mut overlay = OverlayedChanges::default();
let backend_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(backend, IgnorePendingCode::Yes);
sp_state_machine::backend::BackendRuntimeCode::new(backend, TryPendingCode::No);
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");

StateMachine::new(
Expand Down Expand Up @@ -171,7 +171,7 @@ fn construct_genesis_should_work_with_native() {
let backend = InMemoryBackend::from((storage, StateVersion::default()));
let b1data = block1(genesis_hash, &backend);
let backend_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(&backend, IgnorePendingCode::Yes);
sp_state_machine::backend::BackendRuntimeCode::new(&backend, TryPendingCode::No);
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");

let mut overlay = OverlayedChanges::default();
Expand Down Expand Up @@ -203,7 +203,7 @@ fn construct_genesis_should_work_with_wasm() {
let backend = InMemoryBackend::from((storage, StateVersion::default()));
let b1data = block1(genesis_hash, &backend);
let backend_runtime_code =
sp_state_machine::backend::BackendRuntimeCode::new(&backend, IgnorePendingCode::Yes);
sp_state_machine::backend::BackendRuntimeCode::new(&backend, TryPendingCode::No);
let runtime_code = backend_runtime_code.runtime_code().expect("Code is part of the backend");

let mut overlay = OverlayedChanges::default();
Expand Down
22 changes: 11 additions & 11 deletions substrate/primitives/state-machine/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,26 +392,26 @@ pub trait AsTrieBackend<H: Hasher, C = sp_trie::cache::LocalTrieCache<H>> {
fn as_trie_backend(&self) -> &TrieBackend<Self::TrieBackendStorage, H, C>;
}

/// Whether to ignore `:pending_code` storage value
/// Whether to to take `:pending_code` into account
/// when fetching the runtime code.
///
/// We want to use `:pending_code` in block production and import
/// but avoid it using in runtime api calls.
///
/// See <https://github.com/paritytech/polkadot-sdk/issues/64> for more details.
#[cfg(feature = "std")]
pub enum IgnorePendingCode {
pub enum TryPendingCode {
/// Used by runtime api calls.
Yes,
/// Used by block import and production.
No,
/// Used by block import and production.
Yes,
}

/// Wrapper to create a [`RuntimeCode`] from a type that implements [`Backend`].
#[cfg(feature = "std")]
pub struct BackendRuntimeCode<'a, B, H> {
backend: &'a B,
ignore_pending_code: IgnorePendingCode,
try_pending_code: TryPendingCode,
_marker: PhantomData<H>,
}

Expand All @@ -420,7 +420,7 @@ impl<'a, B: Backend<H>, H: Hasher> sp_core::traits::FetchRuntimeCode
for BackendRuntimeCode<'a, B, H>
{
fn fetch_runtime_code(&self) -> Option<std::borrow::Cow<[u8]>> {
if matches!(self.ignore_pending_code, IgnorePendingCode::No) {
if matches!(self.try_pending_code, TryPendingCode::Yes) {
let pending_code = self
.backend
.storage(sp_core::storage::well_known_keys::PENDING_CODE)
Expand All @@ -446,16 +446,16 @@ where
H::Out: Encode,
{
/// Create a new instance.
pub fn new(backend: &'a B, ignore_pending_code: IgnorePendingCode) -> Self {
Self { backend, ignore_pending_code, _marker: PhantomData }
pub fn new(backend: &'a B, try_pending_code: TryPendingCode) -> Self {
Self { backend, try_pending_code, _marker: PhantomData }
}

/// Return the [`RuntimeCode`] build from the wrapped `backend`.
/// This method takes `:pending_code` into account.
pub fn runtime_code(&self) -> Result<RuntimeCode, &'static str> {
let maybe_pending_code_hash = match self.ignore_pending_code {
IgnorePendingCode::Yes => None,
IgnorePendingCode::No => self
let maybe_pending_code_hash = match self.try_pending_code {
TryPendingCode::No => None,
TryPendingCode::Yes => self
.backend
.storage_hash(sp_core::storage::well_known_keys::PENDING_CODE)
.ok()
Expand Down
4 changes: 2 additions & 2 deletions substrate/utils/frame/benchmarking-cli/src/pallet/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use sp_core::{
use sp_externalities::Extensions;
use sp_keystore::{testing::MemoryKeystore, KeystoreExt};
use sp_runtime::traits::Hash;
use sp_state_machine::{backend::IgnorePendingCode, StateMachine};
use sp_state_machine::{backend::TryPendingCode, StateMachine};
use sp_storage::{well_known_keys::CODE, Storage};
use sp_trie::{proof_size_extension::ProofSizeExt, recorder::Recorder};
use sp_wasm_interface::HostFunctions;
Expand Down Expand Up @@ -712,7 +712,7 @@ impl PalletCmd {
} else {
log::info!(target: LOG_TARGET, "Loading WASM from state");
let state =
sp_state_machine::backend::BackendRuntimeCode::new(state, IgnorePendingCode::Yes);
sp_state_machine::backend::BackendRuntimeCode::new(state, TryPendingCode::No);

Ok(FetchedCode::FromGenesis { state })
}
Expand Down

0 comments on commit cc678c6

Please sign in to comment.