Skip to content

Commit

Permalink
Fix state machine return value assumption (#47)
Browse files Browse the repository at this point in the history
* remove state_machine_call_with_proof ret value assumption

* bump version
  • Loading branch information
liamaharon authored Oct 30, 2023
1 parent 7125cb2 commit c1bbbd7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ members = [
]

[workspace.package]
version = "0.3.4"
version = "0.3.5"
authors = ["Parity Technologies <[email protected]>"]
description = "Substrate's programmatic testing framework."
edition = "2021"
Expand Down
7 changes: 5 additions & 2 deletions core/src/commands/on_runtime_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ where
"🔬 Running TryRuntime_on_runtime_upgrade with checks: {:?}",
command.checks
);
let (_, proof, ref_time_results) = state_machine_call_with_proof::<Block, HostFns>(
let (_, proof, encoded_result) = state_machine_call_with_proof::<Block, HostFns>(
&ext,
&executor,
"TryRuntime_on_runtime_upgrade",
Expand All @@ -101,6 +101,8 @@ where
shared.export_proof.clone(),
)?;

let ref_time_results = encoded_result.try_into()?;

// If the above call ran with checks then we need to run the call again without checks to
// measure PoV correctly.
// Otherwise, storage lookups from try-runtime logic like pre/post hooks are included in the PoV
Expand All @@ -111,14 +113,15 @@ where
log::info!(
"🔬 TryRuntime_on_runtime_upgrade succeeded! Running it again without checks for weight measurements."
);
let (_, proof, ref_time_results) = state_machine_call_with_proof::<Block, HostFns>(
let (_, proof, encoded_result) = state_machine_call_with_proof::<Block, HostFns>(
&ext,
&executor,
"TryRuntime_on_runtime_upgrade",
UpgradeCheckSelect::None.encode().as_ref(),
Default::default(), // we don't really need any extensions here.
shared.export_proof,
)?;
let ref_time_results = encoded_result.try_into()?;
(proof, ref_time_results)
}
};
Expand Down
35 changes: 18 additions & 17 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ pub struct RefTimeInfo {
pub max: Duration,
}

impl TryFrom<Vec<u8>> for RefTimeInfo {
type Error = String;

/// try_from Vec encoded as (Weight, Weight) tuple
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
let (weight_used, weight_max) = <(Weight, Weight)>::decode_all(&mut &*value)
.map_err(|e| format!("failed to decode weight: {:?}", e))?;

Ok(RefTimeInfo {
// 1000 picoseconds == 1 nanosecond
used: Duration::from_nanos(weight_used.ref_time() / 1000),
max: Duration::from_nanos(weight_max.ref_time() / 1000),
})
}
}

/// Same as [`state_machine_call`], but it also computes and returns the storage proof and ref time
/// information.
///
Expand All @@ -141,11 +157,7 @@ pub(crate) fn state_machine_call_with_proof<Block: BlockT, HostFns: HostFunction
data: &[u8],
mut extensions: Extensions,
maybe_export_proof: Option<PathBuf>,
) -> sc_cli::Result<(
OverlayedChanges<HashingFor<Block>>,
StorageProof,
RefTimeInfo,
)> {
) -> sc_cli::Result<(OverlayedChanges<HashingFor<Block>>, StorageProof, Vec<u8>)> {
let mut changes = Default::default();
let backend = ext.backend.clone();
let runtime_code_backend = sp_state_machine::backend::BackendRuntimeCode::new(&backend);
Expand Down Expand Up @@ -198,18 +210,7 @@ pub(crate) fn state_machine_call_with_proof<Block: BlockT, HostFns: HostFunction
})?;
}

let (weight_used, weight_max) = <(Weight, Weight)>::decode_all(&mut &*encoded_result)
.map_err(|e| format!("failed to decode weight: {:?}", e))?;

Ok((
changes,
proof,
RefTimeInfo {
// 1000 picoseconds == 1 nanosecond
used: Duration::from_nanos(weight_used.ref_time() / 1000),
max: Duration::from_nanos(weight_max.ref_time() / 1000),
},
))
Ok((changes, proof, encoded_result))
}

/// Converts a [`sp_state_machine::StorageProof`] into a JSON string.
Expand Down

0 comments on commit c1bbbd7

Please sign in to comment.