Skip to content

Commit

Permalink
feat: implement rpc functionality within the pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
neutrinoks committed Mar 28, 2024
1 parent c41ee43 commit c2357d7
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pallet/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ sp_api::decl_runtime_apis! {
fn estimate_gas_execute_script(transaction: Vec<u8>) -> Result<MoveApiEstimation, DispatchError>;

// Get module binary by its address.
fn get_module(address: String, name: String) -> Result<Option<Vec<u8>>, Vec<u8>>;
fn get_module(address: AccountId, name: String) -> Result<Option<Vec<u8>>, Vec<u8>>;

// Get module ABI by its address.
fn get_module_abi(address: String, name: String) -> Result<Option<ModuleAbi>, Vec<u8>>;
fn get_module_abi(address: AccountId, name: String) -> Result<Option<ModuleAbi>, Vec<u8>>;

// Get resource.
fn get_resource(account: AccountId, tag: Vec<u8>) -> Result<Option<Vec<u8>>, Vec<u8>>;
Expand Down
95 changes: 95 additions & 0 deletions pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ pub mod pallet {

use super::*;
use crate::{
api::MoveApiEstimation,
balance::{BalanceAdapter, BalanceOf},
signer::*,
storage::{MoveVmStorage, StorageAdapter},
Expand Down Expand Up @@ -629,6 +630,100 @@ pub mod pallet {
}
}

// RPC method implementation for simple node integration.
impl<T: Config> Pallet<T> {
pub fn rpc_gas_to_weight(_gas_limit: u64) -> Weight {
// TODO (eiger): implement in M3
Weight::from_parts(1_123_123, 0) // Hardcoded for testing
}

pub fn rpc_weight_to_gas(_weight: Weight) -> u64 {
// TODO (eiger): implement in M3
100
}

pub fn rpc_estimate_gas_publish_module(
account: &T::AccountId,
bytecode: Vec<u8>,
) -> Result<MoveApiEstimation, DispatchError> {
let address = Self::to_move_address(account)?;
let vm_result = Self::raw_publish_module(&address, bytecode, GasStrategy::DryRun)?;

Ok(MoveApiEstimation {
vm_status_code: vm_result.status_code.into(),
gas_used: vm_result.gas_used,
})
}

pub fn rpc_estimate_gas_publish_bundle(
account: &T::AccountId,
bytecode: Vec<u8>,
) -> Result<MoveApiEstimation, DispatchError> {
let address = Self::to_move_address(account)?;
let vm_result = Self::raw_publish_bundle(&address, bytecode, GasStrategy::DryRun)?;

Ok(MoveApiEstimation {
vm_status_code: vm_result.status_code.into(),
gas_used: vm_result.gas_used,
})
}

pub fn rpc_estimate_gas_execute_script(
transaction_bc: Vec<u8>,
) -> Result<MoveApiEstimation, DispatchError> {
// Main input for the VM are these script parameters.
let ScriptTransaction {
bytecode,
args,
type_args,
} = ScriptTransaction::try_from(transaction_bc.as_ref())
.map_err(|_| Error::<T>::InvalidScriptTransaction)?;
let args: Vec<&[u8]> = args.iter().map(AsRef::as_ref).collect();

// Make sure the script parameters are valid.
let signer_count =
verify_script_integrity_and_check_signers(&bytecode).map_err(Error::<T>::from)?;

// In the case of a dry run, we have an "unlimited" balance (u128::MAX) because it is
// not relevant to the gas estimation (no changes will be applied).
let unlimited_balance = BalanceAdapter::<T>::for_dry_run(&args, signer_count)?;

let vm_result = Self::raw_execute_script(
&bytecode,
type_args,
args,
GasStrategy::DryRun,
unlimited_balance,
)?;

Ok(MoveApiEstimation {
vm_status_code: vm_result.status_code.into(),
gas_used: vm_result.gas_used,
})
}

pub fn rpc_get_module(
account: T::AccountId,
name: String,
) -> Result<Option<Vec<u8>>, Vec<u8>> {
Self::get_module(&account, &name)
}

pub fn rpc_get_module_abi(
account: T::AccountId,
name: String,
) -> Result<Option<ModuleAbi>, Vec<u8>> {
Self::get_module_abi(&account, &name)
}

pub fn rpc_get_resource(
account: T::AccountId,
tag: Vec<u8>,
) -> Result<Option<Vec<u8>>, Vec<u8>> {
Self::get_resource(&account, tag.as_slice())
}
}

#[pallet::error]
pub enum Error<T> {
// General errors
Expand Down
12 changes: 6 additions & 6 deletions rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub trait MoveApi<BlockHash, AccountId> {
#[method(name = "mvm_getModuleABI")]
fn get_module_abi(
&self,
address: &str,
address: AccountId,
name: &str,
at: Option<BlockHash>,
) -> RpcResult<Option<ModuleAbi>>;
Expand All @@ -90,7 +90,7 @@ pub trait MoveApi<BlockHash, AccountId> {
#[method(name = "mvm_getModule")]
fn get_module(
&self,
address: &str,
address: AccountId,
name: &str,
at: Option<BlockHash>,
) -> RpcResult<Option<Vec<u8>>>;
Expand Down Expand Up @@ -210,14 +210,14 @@ where

fn get_module_abi(
&self,
address: &str,
address: AccountId,
name: &str,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<ModuleAbi>> {
let api = self.client.runtime_api();
let res = api.get_module_abi(
at.unwrap_or_else(|| self.client.info().best_hash),
address.to_string(),
address,
name.to_string(),
);

Expand All @@ -227,14 +227,14 @@ where

fn get_module(
&self,
address: &str,
address: AccountId,
name: &str,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<Option<Vec<u8>>> {
let api = self.client.runtime_api();
let res = api.get_module(
at.unwrap_or_else(|| self.client.info().best_hash),
address.to_string(),
address,
name.to_string(),
);

Expand Down

0 comments on commit c2357d7

Please sign in to comment.