Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ gear-workspace-hack.workspace = true
[dev-dependencies]
tracing-subscriber.workspace = true
wat.workspace = true
wasm-encoder.workspace = true
proptest.workspace = true
rand = { workspace = true, features = ["std", "std_rng"] }
numerated = { workspace = true, features = ["mock"] }
Expand Down
62 changes: 61 additions & 1 deletion core/src/code/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ mod utils;
pub use errors::*;
pub use instrumented::*;
pub use metadata::*;
pub use utils::{ALLOWED_EXPORTS, MAX_WASM_PAGES_AMOUNT, REQUIRED_EXPORTS};
pub use utils::{
ALLOWED_EXPORTS, MAX_WASM_PAGES_AMOUNT, REQUIRED_EXPORTS, get_custom_section_data,
};

use utils::CodeTypeSectionSizes;

Expand Down Expand Up @@ -1245,4 +1247,62 @@ mod tests {
))
));
}

mod custom_section_tests {
use crate::code::get_custom_section_data;
use alloc::{vec, vec::Vec};
use wasm_encoder::{CustomSection, Module, TypeSection};

fn make_wasm_with_custom_sections(sections: &[(&str, &[u8])]) -> Vec<u8> {
let mut module = Module::new();
// Add a minimal type section so it's a valid module
let mut types = TypeSection::new();
types.ty().function(vec![], vec![]);
module.section(&types);
// Append each custom section in order
for (name, data) in sections {
module.section(&CustomSection {
name: (*name).into(),
data: (*data).into(),
});
}
module.finish()
}

#[test]
fn section_found() {
let wasm = make_wasm_with_custom_sections(&[("sails:idl", b"hello idl")]);
let result = get_custom_section_data(&wasm, "sails:idl");
assert_eq!(result.unwrap().unwrap(), b"hello idl");
}

#[test]
fn section_not_found() {
let wasm = make_wasm_with_custom_sections(&[("other", b"data")]);
let result = get_custom_section_data(&wasm, "sails:idl");
assert_eq!(result.unwrap(), None);
}

#[test]
fn first_match_returned() {
let wasm = make_wasm_with_custom_sections(&[
("sails:idl", b"first"),
("sails:idl", b"second"),
]);
let result = get_custom_section_data(&wasm, "sails:idl");
assert_eq!(result.unwrap().unwrap(), b"first");
}

#[test]
fn invalid_wasm() {
let result = get_custom_section_data(b"not wasm at all", "sails:idl");
assert!(result.is_err());
}

#[test]
fn empty_input() {
let result = get_custom_section_data(b"", "sails:idl");
assert!(result.is_err());
}
}
}
20 changes: 20 additions & 0 deletions core/src/code/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,23 @@ pub fn get_code_type_sections_sizes(
type_section: type_section_size,
})
}

/// Extract data of a custom section by name from raw WASM bytes.
///
/// Returns `Ok(Some(data))` if the section is found, `Ok(None)` if the WASM
/// is valid but the section is not present, and `Err` if the WASM is malformed.
/// The error preserves the underlying parser diagnostic (offset + reason).
pub fn get_custom_section_data<'a>(
wasm: &'a [u8],
section_name: &str,
) -> Result<Option<&'a [u8]>, wasmparser::BinaryReaderError> {
for payload in wasmparser::Parser::new(0).parse_all(wasm) {
if let Payload::CustomSection(section) = payload?
&& section.name() == section_name
{
return Ok(Some(section.data()));
}
}

Ok(None)
}
27 changes: 27 additions & 0 deletions gsdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,33 @@ impl Api {
Ok(T::decode(&mut bytes.as_slice())?)
}

/// Reads a named custom section from the original WASM code stored
/// on-chain. When `block_hash` is `None`, the best block is used.
///
/// Actually calls `gear_readWasmCustomSection` RPC method. Returns
/// `None` if the code is not found or the section does not exist.
/// Primary use case: retrieving a Sails IDL embedded in the
/// `sails:idl` custom section at specified block.
#[at_block]
pub async fn read_wasm_custom_section_at(
&self,
code_id: CodeId,
section_name: impl AsRef<str>,
block_hash: Option<H256>,
) -> Result<Option<sp_core::Bytes>> {
self.rpc()
.request(
"gear_readWasmCustomSection",
rpc_params![
H256(code_id.into()),
section_name.as_ref().to_string(),
block_hash
],
)
.await
.map_err(Into::into)
}

/// Calls `runtime_wasmBlobVersion` RPC method at specified block.
#[at_block]
pub async fn runtime_wasm_blob_version_at(&self, block_hash: Option<H256>) -> Result<String> {
Expand Down
33 changes: 33 additions & 0 deletions gsdk/tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ async fn test_calculate_create_gas() -> Result<()> {
Ok(())
}

#[tokio::test]
async fn test_read_wasm_custom_section() -> Result<()> {
let (_node, api) = dev_node().await;

let wat_code = r#"
(module
(import "env" "memory" (memory 0))
(export "init" (func $init))
(func $init)
(@custom "sails:idl" "hello idl")
)
"#;
let wasm = wat::parse_str(wat_code).unwrap();

api.upload_code(wasm.clone()).await?;
let code_id = CodeId::generate(&wasm);

let present = api.read_wasm_custom_section(code_id, "sails:idl").await?;
assert_eq!(present.as_deref(), Some(b"hello idl".as_ref()));

let missing_section = api
.read_wasm_custom_section(code_id, "no:such:section")
.await?;
assert!(missing_section.is_none());

let unknown_code = api
.read_wasm_custom_section(CodeId::from([0u8; 32]), "sails:idl")
.await?;
assert!(unknown_code.is_none());

Ok(())
}

#[tokio::test]
async fn test_calculate_handle_gas() -> Result<()> {
let (_node, api) = dev_node().await;
Expand Down
3 changes: 3 additions & 0 deletions node/service/src/rpc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ use sp_keystore::KeystorePtr;

mod gear_events;
mod runtime_info;
mod wasm_section;

/// Extra dependencies for BABE.
pub struct BabeDeps {
Expand Down Expand Up @@ -144,6 +145,7 @@ where
use sc_sync_state_rpc::{SyncState, SyncStateApiServer};
use substrate_frame_rpc_system::{System, SystemApiServer};
use substrate_state_trie_migration_rpc::{StateMigration, StateMigrationApiServer};
use wasm_section::{WasmSectionApi, WasmSectionServer};

let mut io = RpcModule::new(());

Expand Down Expand Up @@ -224,6 +226,7 @@ where
}

io.merge(RuntimeInfoApi::<C, Block, B>::new(client.clone()).into_rpc())?;
io.merge(WasmSectionApi::<C, Block, B>::new(client.clone()).into_rpc())?;

io.merge(GearStakingRewards::new(client.clone()).into_rpc())?;

Expand Down
116 changes: 116 additions & 0 deletions node/service/src/rpc/wasm_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// This file is part of Gear.

// Copyright (C) 2021-2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! RPC endpoint for reading custom sections from program WASM original code.

use gear_core::code::get_custom_section_data;
use jsonrpsee::{core::RpcResult, proc_macros::rpc, types::ErrorObjectOwned};
use parity_scale_codec::{Compact, Decode};
use sc_client_api::StorageProvider;
use sp_blockchain::HeaderBackend;
use sp_core::{Bytes, H256, twox_128};
use sp_runtime::traits::Block as BlockT;
use sp_storage::StorageKey;
use std::{marker::PhantomData, sync::Arc};

const ERROR_CODE: i32 = 9000;

#[rpc(server)]
pub(crate) trait WasmSection<BlockHash> {
/// Read a custom section from the original WASM code stored on-chain.
///
/// This is commonly used to retrieve the Sails IDL embedded in the
/// `sails:idl` custom section. The returned bytes are raw section data;
/// for `sails:idl`, clients must parse the envelope (version + flags)
/// and decompress the payload.
///
/// Returns `null` if the code is not found or the section does not exist.
#[method(name = "gear_readWasmCustomSection")]
fn read_wasm_custom_section(
&self,
code_id: H256,
section_name: String,
at: Option<BlockHash>,
) -> RpcResult<Option<Bytes>>;
}

pub(crate) struct WasmSectionApi<C, Block, Backend> {
client: Arc<C>,
_marker: PhantomData<(Block, Backend)>,
}

impl<C, Block, Backend> WasmSectionApi<C, Block, Backend> {
pub(crate) fn new(client: Arc<C>) -> Self {
Self {
client,
_marker: PhantomData,
}
}
}

impl<C, Block, Backend> WasmSectionServer<<Block as BlockT>::Hash>
for WasmSectionApi<C, Block, Backend>
where
C: HeaderBackend<Block> + StorageProvider<Block, Backend> + Send + Sync + 'static,
Block: BlockT,
Backend: sc_client_api::Backend<Block> + Send + Sync + 'static,
{
fn read_wasm_custom_section(
&self,
code_id: H256,
section_name: String,
at: Option<Block::Hash>,
) -> RpcResult<Option<Bytes>> {
let at = at.unwrap_or_else(|| self.client.info().best_hash);

// Keep in sync with `pallet-gear-program::OriginalCodeStorage` (StorageMap, Identity hasher).
let mut storage_key = twox_128(b"GearProgram").to_vec();
storage_key.extend_from_slice(&twox_128(b"OriginalCodeStorage"));
storage_key.extend_from_slice(code_id.as_bytes());
Comment on lines +81 to +84
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move impl inside pallet gear, to use normally CodeStorage instead of this raw code


let wasm_data = self
.client
.storage(at, &StorageKey(storage_key))
.map_err(|e| rpc_err("WASM section read error", Some(e.to_string())))?;

let Some(wasm_data) = wasm_data else {
return Ok(None);
};

let mut input = wasm_data.0.as_slice();
let len = <Compact<u32>>::decode(&mut input)
.map_err(|e| rpc_err("Failed to decode stored WASM length", Some(e.to_string())))?
.0 as usize;
if input.len() < len {
return Err(rpc_err("Truncated stored WASM blob", None));
}
let wasm_bytes = &input[..len];

match get_custom_section_data(wasm_bytes, &section_name) {
Ok(Some(data)) => Ok(Some(Bytes(data.to_vec()))),
Ok(None) => Ok(None),
Err(e) => Err(rpc_err("Failed to parse stored WASM", Some(e.to_string()))),
}
}
}

fn rpc_err(message: &str, data: Option<String>) -> ErrorObjectOwned {
use jsonrpsee::types::error::ErrorObject;

ErrorObject::owned(ERROR_CODE, message, data)
}
Loading