Skip to content

Commit

Permalink
feat(rpc)!: Introduce payload parameter for read_state_using_wasm rpc…
Browse files Browse the repository at this point in the history
… call (#3173)
  • Loading branch information
DennisInSky authored and shamilsan committed Sep 11, 2023
1 parent 9d98005 commit ce4d76d
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 23 deletions.
2 changes: 1 addition & 1 deletion gcli/src/cmd/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Program {
at: Option<H256>,
) -> Result<()> {
let state = api
.read_state_using_wasm(pid, method, wasm, args, at)
.read_state_using_wasm(pid, Default::default(), method, wasm, args, at)
.await?;
println!("{}", state);
Ok(())
Expand Down
32 changes: 26 additions & 6 deletions gclient/src/api/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,20 @@ impl GearApi {
pub async fn read_state_bytes_using_wasm(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
wasm: Vec<u8>,
argument: Option<Vec<u8>>,
) -> Result<Vec<u8>> {
self.read_state_bytes_using_wasm_at(program_id, fn_name, wasm, argument, None)
self.read_state_bytes_using_wasm_at(program_id, payload, fn_name, wasm, argument, None)
.await
}

/// Same as [`read_state_bytes_using_wasm`](Self::read_state_bytes_using_wasm), but reads the program's state at the block identified by its hash.
pub async fn read_state_bytes_using_wasm_at(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
wasm: Vec<u8>,
argument: Option<Vec<u8>>,
Expand All @@ -282,7 +284,14 @@ impl GearApi {
let response: String = self
.0
.api()
.read_state_using_wasm(H256(program_id.into()), fn_name, wasm, argument, at)
.read_state_using_wasm(
H256(program_id.into()),
payload,
fn_name,
wasm,
argument,
at,
)
.await?;
crate::utils::hex_to_vec(response).map_err(Into::into)
}
Expand All @@ -291,11 +300,12 @@ impl GearApi {
pub async fn read_state_using_wasm<E: Encode, D: Decode>(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
wasm: Vec<u8>,
argument: Option<E>,
) -> Result<D> {
self.read_state_using_wasm_at(program_id, fn_name, wasm, argument, None)
self.read_state_using_wasm_at(program_id, payload, fn_name, wasm, argument, None)
.await
}

Expand All @@ -304,6 +314,7 @@ impl GearApi {
pub async fn read_state_using_wasm_at<E: Encode, D: Decode>(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
wasm: Vec<u8>,
argument: Option<E>,
Expand All @@ -312,6 +323,7 @@ impl GearApi {
let bytes = self
.read_state_bytes_using_wasm_at(
program_id,
payload,
fn_name,
wasm,
argument.map(|v| v.encode()),
Expand All @@ -327,25 +339,30 @@ impl GearApi {
pub async fn read_state_bytes_using_wasm_by_path(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
path: impl AsRef<Path>,
argument: Option<Vec<u8>>,
) -> Result<Vec<u8>> {
self.read_state_bytes_using_wasm_by_path_at(program_id, fn_name, path, argument, None)
.await
self.read_state_bytes_using_wasm_by_path_at(
program_id, payload, fn_name, path, argument, None,
)
.await
}

/// Same as [`read_state_using_wasm_by_path`](Self::read_state_using_wasm_by_path), but reads the program's state at the block identified by its hash.
pub async fn read_state_bytes_using_wasm_by_path_at(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
path: impl AsRef<Path>,
argument: Option<Vec<u8>>,
at: Option<H256>,
) -> Result<Vec<u8>> {
self.read_state_bytes_using_wasm_at(
program_id,
payload,
fn_name,
utils::code_from_os(path.as_ref())?,
argument,
Expand All @@ -359,18 +376,20 @@ impl GearApi {
pub async fn read_state_using_wasm_by_path<E: Encode, D: Decode>(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
path: impl AsRef<Path>,
argument: Option<E>,
) -> Result<D> {
self.read_state_using_wasm_by_path_at(program_id, fn_name, path, argument, None)
self.read_state_using_wasm_by_path_at(program_id, payload, fn_name, path, argument, None)
.await
}

/// Same as [`read_state_using_wasm_by_path`](Self::read_state_using_wasm_by_path), but reads the program's state at the block identified by its hash.
pub async fn read_state_using_wasm_by_path_at<E: Encode, D: Decode>(
&self,
program_id: ProgramId,
payload: Vec<u8>,
fn_name: &str,
path: impl AsRef<Path>,
argument: Option<E>,
Expand All @@ -379,6 +398,7 @@ impl GearApi {
let bytes = self
.read_state_bytes_using_wasm_by_path_at(
program_id,
payload,
fn_name,
path,
argument.map(|v| v.encode()),
Expand Down
1 change: 1 addition & 0 deletions gclient/tests/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ async fn get_state() -> anyhow::Result<()> {
let wallet: Option<Wallet> = api
.read_state_using_wasm(
program_id,
Default::default(),
"first_wallet",
demo_new_meta::META_WASM_V1.to_vec(),
<Option<()>>::None,
Expand Down
2 changes: 2 additions & 0 deletions gsdk/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ impl Api {
pub async fn read_state_using_wasm(
&self,
pid: H256,
payload: Vec<u8>,
method: &str,
wasm: Vec<u8>,
args: Option<Vec<u8>>,
Expand All @@ -166,6 +167,7 @@ impl Api {
"gear_readStateUsingWasm",
rpc_params![
pid,
hex::encode(payload),
hex::encode(method),
hex::encode(wasm),
args.map(hex::encode),
Expand Down
3 changes: 2 additions & 1 deletion pallets/gear/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ sp_api::decl_runtime_apis! {

fn read_state(program_id: H256, payload: Vec<u8>) -> Result<Vec<u8>, Vec<u8>>;

fn read_state_using_wasm(program_id: H256, fn_name: Vec<u8>, wasm: Vec<u8>, argument: Option<Vec<u8>>) -> Result<Vec<u8>, Vec<u8>>;
#[allow(clippy::too_many_arguments)]
fn read_state_using_wasm(program_id: H256, payload: Vec<u8>, fn_name: Vec<u8>, wasm: Vec<u8>, argument: Option<Vec<u8>>) -> Result<Vec<u8>, Vec<u8>>;

fn read_metahash(program_id: H256) -> Result<H256, Vec<u8>>;
}
Expand Down
3 changes: 3 additions & 0 deletions pallets/gear/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub trait GearApi<BlockHash, ResponseType> {
fn read_state_using_wasm(
&self,
program_id: H256,
payload: Bytes,
fn_name: Bytes,
wasm: Bytes,
argument: Option<Bytes>,
Expand Down Expand Up @@ -338,6 +339,7 @@ where
fn read_state_using_wasm(
&self,
program_id: H256,
payload: Bytes,
fn_name: Bytes,
wasm: Bytes,
argument: Option<Bytes>,
Expand All @@ -349,6 +351,7 @@ where
api.read_state_using_wasm(
at_hash,
program_id,
payload.to_vec(),
fn_name.to_vec(),
wasm.to_vec(),
argument.map(|v| v.to_vec()),
Expand Down
3 changes: 2 additions & 1 deletion pallets/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ pub mod pallet {

pub fn read_state_using_wasm(
program_id: H256,
payload: Vec<u8>,
fn_name: Vec<u8>,
wasm: Vec<u8>,
argument: Option<Vec<u8>>,
Expand All @@ -706,7 +707,7 @@ pub mod pallet {
let fn_name = String::from_utf8(fn_name)
.map_err(|_| "Non-utf8 function name".as_bytes().to_vec())?;

Self::read_state_using_wasm_impl(program_id, fn_name, wasm, argument)
Self::read_state_using_wasm_impl(program_id, payload, fn_name, wasm, argument)
.map_err(String::into_bytes)
}

Expand Down
4 changes: 3 additions & 1 deletion pallets/gear/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ where

pub(crate) fn read_state_using_wasm_impl(
program_id: ProgramId,
payload: Vec<u8>,
function: impl Into<String>,
wasm: Vec<u8>,
argument: Option<Vec<u8>>,
Expand Down Expand Up @@ -307,8 +308,9 @@ where

let instrumented_code = code_and_id.into_parts().0;

let payload_arg = payload;
let mut payload = argument.unwrap_or_default();
payload.append(&mut Self::read_state_impl(program_id, Default::default())?);
payload.append(&mut Self::read_state_impl(program_id, payload_arg)?);

let block_info = BlockInfo {
height: Self::block_number().unique_saturated_into(),
Expand Down
56 changes: 44 additions & 12 deletions pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,8 +2038,14 @@ fn read_state_using_wasm_works() {
let func1 = "last_wallet";
assert!(META_EXPORTS_V1.contains(&func1));

let res = Gear::read_state_using_wasm_impl(program_id, func1, META_WASM_V1.to_vec(), None)
.expect("Failed to read state");
let res = Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
func1,
META_WASM_V1.to_vec(),
None,
)
.expect("Failed to read state");

assert_eq!(res, expected);

Expand All @@ -2059,6 +2065,7 @@ fn read_state_using_wasm_works() {

let res = Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
func2,
META_WASM_V2.to_vec(),
Some(id.encode()),
Expand All @@ -2078,6 +2085,7 @@ fn read_state_bn_and_timestamp_works() {

let res = Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"block_number",
META_WASM_V3.to_vec(),
None,
Expand All @@ -2091,6 +2099,7 @@ fn read_state_bn_and_timestamp_works() {

let res = Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"block_timestamp",
META_WASM_V3.to_vec(),
None,
Expand Down Expand Up @@ -2149,9 +2158,14 @@ fn wasm_metadata_generation_works() {

assert!(Gear::is_initialized(program_id));

let m1 =
Gear::read_state_using_wasm_impl(program_id, "metadata", META_WASM_V1.to_vec(), None)
.expect("Failed to read state");
let m1 = Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"metadata",
META_WASM_V1.to_vec(),
None,
)
.expect("Failed to read state");

let metadata1 =
gmeta::MetawasmData::decode(&mut m1.as_ref()).expect("Failed to decode metadata");
Expand All @@ -2162,9 +2176,14 @@ fn wasm_metadata_generation_works() {
expected_exports_1.sort();
assert_eq!(exports1, expected_exports_1);

let m2 =
Gear::read_state_using_wasm_impl(program_id, "metadata", META_WASM_V2.to_vec(), None)
.expect("Failed to read state");
let m2 = Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"metadata",
META_WASM_V2.to_vec(),
None,
)
.expect("Failed to read state");

let metadata2 =
gmeta::MetawasmData::decode(&mut m2.as_ref()).expect("Failed to decode metadata");
Expand Down Expand Up @@ -2212,17 +2231,30 @@ fn read_state_using_wasm_errors() {
// Inexistent function
assert!(Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"inexistent",
meta_wasm.clone(),
None
)
.is_err());
// Empty function
assert!(
Gear::read_state_using_wasm_impl(program_id, "empty", meta_wasm.clone(), None).is_err()
);
assert!(Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"empty",
meta_wasm.clone(),
None
)
.is_err());
// Greed function
assert!(Gear::read_state_using_wasm_impl(program_id, "loop", meta_wasm, None).is_err());
assert!(Gear::read_state_using_wasm_impl(
program_id,
Default::default(),
"loop",
meta_wasm,
None
)
.is_err());
});
}

Expand Down
3 changes: 2 additions & 1 deletion runtime/common/src/apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ macro_rules! impl_runtime_apis_plus_common {

fn read_state_using_wasm(
program_id: H256,
payload: Vec<u8>,
fn_name: Vec<u8>,
wasm: Vec<u8>,
argument: Option<Vec<u8>>,
) -> Result<Vec<u8>, Vec<u8>> {
Gear::read_state_using_wasm(program_id, fn_name, wasm, argument)
Gear::read_state_using_wasm(program_id, payload, fn_name, wasm, argument)
}

fn read_metahash(program_id: H256) -> Result<H256, Vec<u8>> {
Expand Down

0 comments on commit ce4d76d

Please sign in to comment.