Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor get_gas_spent #1051

Merged
merged 37 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
dcb3c90
call the func twice for calculations
gshep Jun 13, 2022
a0f90d6
remove get_gas_burned
gshep Jun 13, 2022
ba3b62e
Merge remote-tracking branch 'origin/master'
gshep Jun 14, 2022
207c5ea
use get_gas_spent instead of calc_handle_gas_spent
gshep Jun 14, 2022
995bcac
remove calc_handle_gas_spent
gshep Jun 14, 2022
e643b0f
Merge remote-tracking branch 'origin/master'
gshep Jun 14, 2022
c20288c
bump spec version
gshep Jun 14, 2022
5c2cec4
introduce get_gas_spent result type
gshep Jun 14, 2022
d1c4e0b
make pre-commit
gshep Jun 14, 2022
d0c39c1
add todo
gshep Jun 14, 2022
be6466b
rename functions
gshep Jun 14, 2022
2ac415b
make fmt
gshep Jun 14, 2022
126a233
adjust rpc calls
gshep Jun 15, 2022
d3fbeea
Merge remote-tracking branch 'origin/master'
gshep Jun 15, 2022
7f6bc03
bump spec version
gshep Jun 15, 2022
4f4407e
Merge remote-tracking branch 'origin/master'
gshep Jun 15, 2022
179266f
Merge remote-tracking branch 'origin/master'
gshep Jun 16, 2022
52624d0
fix review remark: use IDs in HandleKind
gshep Jun 16, 2022
9d0ba38
fix review remark: remove empty lines
gshep Jun 16, 2022
c84f1da
add allow_other_panics flag
gshep Jun 16, 2022
78f165a
fix remark review: use different salts
gshep Jun 16, 2022
9fe89c4
fix review remarks: renamae & docs
gshep Jun 16, 2022
1521466
Merge remote-tracking branch 'origin/master'
gshep Jun 16, 2022
b47b60e
bump spec version
gshep Jun 16, 2022
c2c4cf1
make fmt
gshep Jun 16, 2022
88c16b5
pre-commit
gshep Jun 16, 2022
301bc0b
rename GasInfo field
gshep Jun 16, 2022
e82478f
fail if message with gas limit sent to user
gshep Jun 16, 2022
f153fdb
make pre-commit
gshep Jun 16, 2022
c1a2e92
fix remarks after review
gshep Jun 17, 2022
299b059
pre-commit
gshep Jun 17, 2022
4e289bf
update calc-gas-spent script
gshep Jun 17, 2022
5201da6
remove redundant function
gshep Jun 17, 2022
2f54f60
don't set balance to maximum
gshep Jun 17, 2022
731010b
Merge remote-tracking branch 'origin/master'
gshep Jun 17, 2022
aec889e
bump spec version
gshep Jun 17, 2022
bdf5b29
Merge remote-tracking branch 'origin/master'
gshep Jun 18, 2022
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions pallets/gear/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ sp-rpc = { version = "6.0.0", git = "https://github.com/gear-tech/substrate.git"
sp-runtime = { version = "6.0.0", git = "https://github.com/gear-tech/substrate.git", branch = "gear-stable", default-features = false }

# Local packages
gear-core = { path = "../../../core" }
gear-common = { path = "../../../common" }
pallet-gear-rpc-runtime-api = { version = "2.0.0", path = "./runtime-api" }
5 changes: 3 additions & 2 deletions pallets/gear/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

#![cfg_attr(not(feature = "std"), no_std)]

pub use pallet_gear::manager::HandleKind;
pub use pallet_gear::{manager::HandleKind, GasInfo};
use sp_core::H256;
use sp_std::vec::Vec;

sp_api::decl_runtime_apis! {
pub trait GearApi {
fn get_gas_spent(source: H256, kind: HandleKind, payload: Vec<u8>, value: u128) -> Result<u64, Vec<u8>>;
#[allow(clippy::too_many_arguments)]
fn calculate_gas_info(source: H256, kind: HandleKind, payload: Vec<u8>, value: u128, allow_other_panics: bool,) -> Result<GasInfo, Vec<u8>>;
}
}
82 changes: 38 additions & 44 deletions pallets/gear/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@

//! RPC interface for the gear module.

gshep marked this conversation as resolved.
Show resolved Hide resolved
use std::{convert::TryInto, sync::Arc};
#![allow(clippy::too_many_arguments)]

use gear_common::Origin;
use gear_core::ids::{MessageId, ProgramId};
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorObject},
};

pub use pallet_gear_rpc_runtime_api::HandleKind;
pub use pallet_gear_rpc_runtime_api::GearApi as GearRuntimeApi;
use pallet_gear_rpc_runtime_api::{GasInfo, HandleKind};
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_core::{Bytes, H256};
use sp_rpc::number::NumberOrHex;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};

pub use pallet_gear_rpc_runtime_api::GearApi as GearRuntimeApi;
use std::sync::Arc;

/// Converts a runtime trap into a [`CallError`].
fn runtime_error_into_rpc_error(err: impl std::fmt::Debug) -> JsonRpseeError {
Expand All @@ -47,36 +47,39 @@ fn runtime_error_into_rpc_error(err: impl std::fmt::Debug) -> JsonRpseeError {

#[rpc(client, server)]
pub trait GearApi<BlockHash, ResponseType> {
gshep marked this conversation as resolved.
Show resolved Hide resolved
#[method(name = "gear_getInitGasSpent")]
#[method(name = "gear_calculateInitGas")]
fn get_init_gas_spent(
&self,
source: H256,
code: Bytes,
payload: Bytes,
value: u128,
allow_other_panics: bool,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;
) -> RpcResult<GasInfo>;

#[method(name = "gear_getHandleGasSpent")]
#[method(name = "gear_calculateHandleGas")]
fn get_handle_gas_spent(
&self,
source: H256,
dest: H256,
payload: Bytes,
value: u128,
allow_other_panics: bool,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;
) -> RpcResult<GasInfo>;

#[method(name = "gear_getReplyGasSpent")]
#[method(name = "gear_calculateReplyGas")]
fn get_reply_gas_spent(
&self,
source: H256,
message_id: H256,
exit_code: i32,
payload: Bytes,
value: u128,
allow_other_panics: bool,
at: Option<BlockHash>,
) -> RpcResult<NumberOrHex>;
) -> RpcResult<GasInfo>;
}

/// A struct that implements the [`GearApi`].
Expand Down Expand Up @@ -127,29 +130,26 @@ where
code: Bytes,
payload: Bytes,
value: u128,
allow_other_panics: bool,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
) -> RpcResult<GasInfo> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));

let runtime_api_result = api
.get_gas_spent(
.calculate_gas_info(
&at,
source,
HandleKind::Init(code.to_vec()),
payload.to_vec(),
value,
allow_other_panics,
)
.map_err(runtime_error_into_rpc_error)?;

match runtime_api_result {
Ok(gas) => Ok(gas.try_into().map_err(runtime_error_into_rpc_error)?),
Err(message) => Err(runtime_error_into_rpc_error(String::from_utf8_lossy(
&message,
))),
}
runtime_api_result.map_err(|e| runtime_error_into_rpc_error(String::from_utf8_lossy(&e)))
}

fn get_handle_gas_spent(
Expand All @@ -158,29 +158,26 @@ where
dest: H256,
payload: Bytes,
value: u128,
allow_other_panics: bool,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
) -> RpcResult<GasInfo> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));

let runtime_api_result = api
.get_gas_spent(
.calculate_gas_info(
&at,
source,
HandleKind::Handle(dest),
HandleKind::Handle(ProgramId::from_origin(dest)),
payload.to_vec(),
value,
allow_other_panics,
)
.map_err(runtime_error_into_rpc_error)?;

match runtime_api_result {
Ok(gas) => Ok(gas.try_into().map_err(runtime_error_into_rpc_error)?),
Err(message) => Err(runtime_error_into_rpc_error(String::from_utf8_lossy(
&message,
))),
}
runtime_api_result.map_err(|e| runtime_error_into_rpc_error(String::from_utf8_lossy(&e)))
}

fn get_reply_gas_spent(
Expand All @@ -190,28 +187,25 @@ where
exit_code: i32,
payload: Bytes,
value: u128,
allow_other_panics: bool,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<NumberOrHex> {
) -> RpcResult<GasInfo> {
let api = self.client.runtime_api();
let at = BlockId::hash(at.unwrap_or_else(||
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));
// If the block hash is not supplied assume the best block.
self.client.info().best_hash));

let runtime_api_result = api
.get_gas_spent(
.calculate_gas_info(
&at,
source,
HandleKind::Reply(message_id, exit_code),
HandleKind::Reply(MessageId::from_origin(message_id), exit_code),
payload.to_vec(),
value,
allow_other_panics,
)
.map_err(runtime_error_into_rpc_error)?;

match runtime_api_result {
Ok(gas) => Ok(gas.try_into().map_err(runtime_error_into_rpc_error)?),
Err(message) => Err(runtime_error_into_rpc_error(String::from_utf8_lossy(
&message,
))),
}
runtime_api_result.map_err(|e| runtime_error_into_rpc_error(String::from_utf8_lossy(&e)))
}
}
Loading