Skip to content

Commit c1a246a

Browse files
Fix FFI type diversion (#8261)
A #7375 follow-up fix
1 parent d88a7e4 commit c1a246a

6 files changed

Lines changed: 54 additions & 43 deletions

File tree

prdoc/pr_7375.prdoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ doc:
5050
The `#[runtime_interface]` machinery automatically "strips" away the marshalling strategy wrappers,
5151
so neither the body of the `say_hello_world` function here nor its callers need to be changed.
5252

53+
Please pay attention that `enum`s with explicit discriminant numbers (if different from implicit, that is,
54+
not starting from zero or not sequential) using the old `PassByEnum` strategy, should be carefully
55+
migrated to preserve compatibility, as `PassByEnum` was always using implicitly generated discriminants
56+
starting from zero, and the `PassAs` strategy introduced by this PR passes actual values.
57+
5358
Furthermore, to explicitly distinguish between the runtime compilation mode and native mode,
5459
`#[cfg(substrate_runtime)]` is now used instead of `#[cfg(not(feature = "std"))]`. That allows for fine-tuning
5560
the compilation behavior without relying solely on the `std` feature.

substrate/frame/benchmarking/src/utils.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use serde::{Deserialize, Serialize};
2525
use sp_io::hashing::blake2_256;
2626
use sp_runtime::{traits::TrailingZeroInput, DispatchError};
2727
use sp_runtime_interface::pass_by::{
28-
AllocateAndReturnByCodec, PassFatPointerAndDecode, PassFatPointerAndRead, PassPointerAndWrite,
28+
AllocateAndReturnByCodec, AllocateAndReturnPointer, PassFatPointerAndDecode,
29+
PassFatPointerAndRead,
2930
};
3031
use sp_storage::TrackedStorageKey;
3132

@@ -253,9 +254,7 @@ sp_api::decl_runtime_apis! {
253254
/// WARNING! This is a non-deterministic call. Do not use this within
254255
/// consensus critical logic.
255256
pub fn current_time() -> u128 {
256-
let mut out = [0; 16];
257-
self::benchmarking::current_time(&mut out);
258-
u128::from_le_bytes(out)
257+
u128::from_le_bytes(self::benchmarking::current_time())
259258
}
260259

261260
/// Interface that provides functions for benchmarking the runtime.
@@ -267,12 +266,12 @@ pub trait Benchmarking {
267266
///
268267
/// WARNING! This is a non-deterministic call. Do not use this within
269268
/// consensus critical logic.
270-
fn current_time(out: PassPointerAndWrite<&mut [u8; 16], 16>) {
271-
*out = std::time::SystemTime::now()
269+
fn current_time() -> AllocateAndReturnPointer<[u8; 16], 16> {
270+
std::time::SystemTime::now()
272271
.duration_since(std::time::SystemTime::UNIX_EPOCH)
273272
.expect("Unix time doesn't go backwards; qed")
274273
.as_nanos()
275-
.to_le_bytes();
274+
.to_le_bytes()
276275
}
277276

278277
/// Reset the trie database to the genesis state.

substrate/primitives/core/src/lib.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -200,53 +200,53 @@ pub trait TypeId {
200200
/// A log level matching the one from `log` crate.
201201
///
202202
/// Used internally by `sp_io::logging::log` method.
203-
#[derive(Encode, Decode, Copy, Clone)]
204-
pub enum LogLevel {
203+
#[derive(Copy, Clone)]
204+
pub enum RuntimeInterfaceLogLevel {
205205
/// `Error` log level.
206-
Error = 1_isize,
206+
Error = 0_isize,
207207
/// `Warn` log level.
208-
Warn = 2_isize,
208+
Warn = 1_isize,
209209
/// `Info` log level.
210-
Info = 3_isize,
210+
Info = 2_isize,
211211
/// `Debug` log level.
212-
Debug = 4_isize,
212+
Debug = 3_isize,
213213
/// `Trace` log level.
214-
Trace = 5_isize,
214+
Trace = 4_isize,
215215
}
216216

217-
impl TryFrom<u8> for LogLevel {
217+
impl TryFrom<u8> for RuntimeInterfaceLogLevel {
218218
type Error = ();
219219
fn try_from(value: u8) -> Result<Self, ()> {
220220
match value {
221-
1 => Ok(Self::Error),
222-
2 => Ok(Self::Warn),
223-
3 => Ok(Self::Info),
224-
4 => Ok(Self::Debug),
225-
5 => Ok(Self::Trace),
221+
0 => Ok(Self::Error),
222+
1 => Ok(Self::Warn),
223+
2 => Ok(Self::Info),
224+
3 => Ok(Self::Debug),
225+
4 => Ok(Self::Trace),
226226
_ => Err(()),
227227
}
228228
}
229229
}
230230

231-
impl From<LogLevel> for u8 {
232-
fn from(value: LogLevel) -> Self {
231+
impl From<RuntimeInterfaceLogLevel> for u8 {
232+
fn from(value: RuntimeInterfaceLogLevel) -> Self {
233233
value as Self
234234
}
235235
}
236236

237-
impl From<u32> for LogLevel {
237+
impl From<u32> for RuntimeInterfaceLogLevel {
238238
fn from(val: u32) -> Self {
239239
match val {
240-
x if x == LogLevel::Warn as u32 => LogLevel::Warn,
241-
x if x == LogLevel::Info as u32 => LogLevel::Info,
242-
x if x == LogLevel::Debug as u32 => LogLevel::Debug,
243-
x if x == LogLevel::Trace as u32 => LogLevel::Trace,
244-
_ => LogLevel::Error,
240+
x if x == RuntimeInterfaceLogLevel::Warn as u32 => RuntimeInterfaceLogLevel::Warn,
241+
x if x == RuntimeInterfaceLogLevel::Info as u32 => RuntimeInterfaceLogLevel::Info,
242+
x if x == RuntimeInterfaceLogLevel::Debug as u32 => RuntimeInterfaceLogLevel::Debug,
243+
x if x == RuntimeInterfaceLogLevel::Trace as u32 => RuntimeInterfaceLogLevel::Trace,
244+
_ => RuntimeInterfaceLogLevel::Error,
245245
}
246246
}
247247
}
248248

249-
impl From<log::Level> for LogLevel {
249+
impl From<log::Level> for RuntimeInterfaceLogLevel {
250250
fn from(l: log::Level) -> Self {
251251
use log::Level::*;
252252
match l {
@@ -259,9 +259,9 @@ impl From<log::Level> for LogLevel {
259259
}
260260
}
261261

262-
impl From<LogLevel> for log::Level {
263-
fn from(l: LogLevel) -> Self {
264-
use self::LogLevel::*;
262+
impl From<RuntimeInterfaceLogLevel> for log::Level {
263+
fn from(l: RuntimeInterfaceLogLevel) -> Self {
264+
use self::RuntimeInterfaceLogLevel::*;
265265
match l {
266266
Error => Self::Error,
267267
Warn => Self::Warn,

substrate/primitives/core/src/offchain/mod.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub trait OffchainStorage: Clone + Send + Sync {
5656
}
5757

5858
/// A type of supported crypto.
59-
#[derive(Clone, Copy, PartialEq, Eq, Encode, Decode, RuntimeDebug)]
59+
#[derive(Clone, Copy, PartialEq, Eq, RuntimeDebug)]
6060
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6161
#[repr(C)]
6262
pub enum StorageKind {
@@ -66,12 +66,12 @@ pub enum StorageKind {
6666
/// that is re-run at block `N(hash2)`.
6767
/// This storage can be used by offchain workers to handle forks
6868
/// and coordinate offchain workers running on different forks.
69-
PERSISTENT = 1_isize,
69+
PERSISTENT = 0_isize,
7070
/// Local storage is revertible and fork-aware. It means that any value
7171
/// set by the offchain worker triggered at block `N(hash1)` is reverted
7272
/// if that block is reverted as non-canonical and is NOT available for the worker
7373
/// that is re-run at block `N(hash2)`.
74-
LOCAL = 2_isize,
74+
LOCAL = 1_isize,
7575
}
7676

7777
impl TryFrom<u32> for StorageKind {
@@ -120,11 +120,14 @@ impl From<HttpRequestId> for u32 {
120120
#[repr(C)]
121121
pub enum HttpError {
122122
/// The requested action couldn't been completed within a deadline.
123-
DeadlineReached = 1_isize,
123+
#[codec(index = 1)]
124+
DeadlineReached = 0_isize,
124125
/// There was an IO Error while processing the request.
125-
IoError = 2_isize,
126+
#[codec(index = 2)]
127+
IoError = 1_isize,
126128
/// The ID of the request is invalid in this context.
127-
Invalid = 3_isize,
129+
#[codec(index = 3)]
130+
Invalid = 2_isize,
128131
}
129132

130133
impl TryFrom<u32> for HttpError {

substrate/primitives/io/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ use sp_core::{
104104
},
105105
sr25519,
106106
storage::StateVersion,
107-
LogLevel, LogLevelFilter, OpaquePeerId, H256,
107+
LogLevelFilter, OpaquePeerId, RuntimeInterfaceLogLevel, H256,
108108
};
109109

110110
#[cfg(feature = "bls-experimental")]
@@ -1767,7 +1767,7 @@ pub trait Logging {
17671767
///
17681768
/// Instead of using directly, prefer setting up `RuntimeLogger` and using `log` macros.
17691769
fn log(
1770-
level: PassAs<LogLevel, u8>,
1770+
level: PassAs<RuntimeInterfaceLogLevel, u8>,
17711771
target: PassFatPointerAndRead<&str>,
17721772
message: PassFatPointerAndRead<&[u8]>,
17731773
) {
@@ -1933,7 +1933,7 @@ pub fn panic(info: &core::panic::PanicInfo) -> ! {
19331933
}
19341934
#[cfg(not(feature = "improved_panic_error_reporting"))]
19351935
{
1936-
logging::log(LogLevel::Error, "runtime", message.as_bytes());
1936+
logging::log(RuntimeInterfaceLogLevel::Error, "runtime", message.as_bytes());
19371937
unreachable();
19381938
}
19391939
}
@@ -1948,7 +1948,11 @@ pub fn oom(_: core::alloc::Layout) -> ! {
19481948
}
19491949
#[cfg(not(feature = "improved_panic_error_reporting"))]
19501950
{
1951-
logging::log(LogLevel::Error, "runtime", b"Runtime memory exhausted. Aborting");
1951+
logging::log(
1952+
RuntimeInterfaceLogLevel::Error,
1953+
"runtime",
1954+
b"Runtime memory exhausted. Aborting",
1955+
);
19521956
unreachable();
19531957
}
19541958
}

substrate/primitives/runtime-interface/src/pass_by.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ where
465465
<U as FromFFIValue>::from_ffi_value(context, arg).and_then(|value| value.try_into()
466466
.map_err(|_| format!(
467467
"failed to convert '{}' (passed as '{}') into '{}' when marshalling hostcall's arguments through the FFI boundary",
468-
type_name::<T>(),
468+
type_name::<U>(),
469469
type_name::<Self::FFIType>(),
470470
type_name::<Self::Owned>()
471471
)))

0 commit comments

Comments
 (0)