Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
Module publish (#170)
Browse files Browse the repository at this point in the history
* publish module

* lock

* lock

* add sudo

* update: fix addresses, some tests

* remove: sudo deps, and from mock

* remove debug and reuse rawpublish

* refactor, delete obsolete comments

* add method destiption

* typo

* typo

* rename test

* remove redurant clone

* remove dublicate

* correct error codes

Co-authored-by: borispovod <[email protected]>
  • Loading branch information
tikhono and borispovod authored Jan 28, 2022
1 parent a78085b commit 44dacae
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 33 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,3 @@ members = [
[profile.release]
panic = 'unwind'
rpath = false
# opt size:
# lto = "thin"
# opt-level = 's'
1 change: 1 addition & 0 deletions pallets/sp-mvm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ orml-traits = { default-features = false, git = 'https://github.com/open-web3-st
# serde is for lcs/bcs only
# used for benchmarking (runtime, std, no-std)
serde-alt = { version = "1", default-features = false, package = "alt_serde", features = ["derive", "alloc"], optional = true }

[dependencies.bcs-alt]
package = "bcs"
default-features = false
Expand Down
52 changes: 34 additions & 18 deletions pallets/sp-mvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub mod pallet {
type GasWeightMapping: gas::GasWeightMapping;

/// The AccountId that can perform a standard library update or deploy module under 0x address.
type UpdaterOrigin: EnsureOrigin<Self::Origin>;
type UpdateOrigin: EnsureOrigin<Self::Origin>;

/// Describes weights for Move VM extrinsics.
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -165,9 +165,9 @@ pub mod pallet {
/// [account]
ModulePublished(T::AccountId),

/// Event about successful move-module publishing
/// Event about successful move-package published
/// [account]
StdModulePublished,
PackagePublished(T::AccountId),
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
Expand Down Expand Up @@ -220,17 +220,18 @@ pub mod pallet {
module_bc: Vec<u8>,
gas_limit: u64,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
debug!("executing `publish` with signed {:?}", who);
// Allows to update Standard Library if root.
let (sender, signer) = Self::ensure_and_convert(origin)?;
debug!("executing `publish module` with signed {:?}", sender);

// Publish module.
let vm_result = Self::raw_publish_module(&who, module_bc, gas_limit, false)?;
let vm_result = Self::raw_publish_module(&signer, module_bc, gas_limit, false)?;

// produce result with spended gas:
let result = result::from_vm_result::<T>(vm_result)?;

// Emit an event:
Self::deposit_event(Event::ModulePublished(who));
Self::deposit_event(Event::ModulePublished(signer));

Ok(result)
}
Expand All @@ -252,17 +253,8 @@ pub mod pallet {
gas_limit: u64,
) -> DispatchResultWithPostInfo {
// Allows to update Standard Library if root.
let sender = match T::UpdaterOrigin::ensure_origin(origin.clone()) {
Ok(_) => {
debug!("executing `publish package` with root");
CORE_CODE_ADDRESS
}
Err(_) => {
let signer = ensure_signed(origin)?;
debug!("executing `publish package` with signed {:?}", signer);
addr::account_to_account_address(&signer)
}
};
let (sender, signer) = Self::ensure_and_convert(origin)?;
debug!("executing `publish package` with signed {:?}", sender);

let vm = Self::get_vm()?;
let gas = Self::get_move_gas_limit(gas_limit)?;
Expand All @@ -278,6 +270,9 @@ pub mod pallet {
// produce result with spended gas:
let result = result::from_vm_result::<T>(vm_result)?;

// Emit an event:
Self::deposit_event(Event::PackagePublished(signer));

Ok(result)
}
}
Expand Down Expand Up @@ -464,6 +459,25 @@ pub mod pallet {
Ok(res)
}

/// Ensures origin is root or signed and returns account id with associated move-address.
pub fn ensure_and_convert(
origin: OriginFor<T>,
) -> Result<(AccountAddress, T::AccountId), Error<T>> {
// Allows to update Standard Library if root.
match T::UpdateOrigin::ensure_origin(origin.clone()) {
Ok(_) => {
let signer = addr::address_to_account(&CORE_CODE_ADDRESS)
.map_err(|_| Error::<T>::AccountAddressConversionError)?;
Ok((CORE_CODE_ADDRESS, signer))
}
Err(_) => {
let signer =
ensure_signed(origin).map_err(|_| Error::<T>::InvalidSignature)?;
Ok((addr::account_to_account_address(&signer), signer))
}
}
}

/// Publish Move module script with provided account, module bytecode, gas limit, and dry run configuration.
/// In case of dry run nothing would be written to storage after execution (required mostly by RPC calls, e.g. estimate gas etc).
pub fn raw_publish_module(
Expand Down Expand Up @@ -609,6 +623,8 @@ pub mod pallet {
TransactionValidationError,
/// Transaction signers num isn't eq signers
TransactionSignersNumError,
/// AccountAddress conversion error.
AccountAddressConversionError,
/// Transaction is not allowed.
TransactionIsNotAllowedError,

Expand Down
4 changes: 2 additions & 2 deletions pallets/sp-mvm/tests/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn transfer_vested_fails() {
result,
DispatchError::Module {
index: 6,
error: 154,
error: 155,
message: Some("Aborted")
}
);
Expand Down Expand Up @@ -233,7 +233,7 @@ fn transfer_token_vested_fails() {
result,
DispatchError::Module {
index: 6,
error: 154,
error: 155,
message: Some("Aborted")
}
);
Expand Down
2 changes: 1 addition & 1 deletion pallets/sp-mvm/tests/common/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ parameter_types! {
impl sp_mvm::Config for Test {
type Event = Event;
type GasWeightMapping = MoveVMGasWeightMapping;
type UpdaterOrigin = EnsureRoot<AccountId>;
type UpdateOrigin = EnsureRoot<AccountId>;
type PalletId = MVMPalletId;
type CurrencyId = CurrencyId;
type Currencies = Currencies;
Expand Down
33 changes: 32 additions & 1 deletion pallets/sp-mvm/tests/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use move_core_types::language_storage::StructTag;
use move_core_types::language_storage::TypeTag;
use move_vm::io::state::State;
use move_vm::types::ModulePackage;
use move_core_types::language_storage::CORE_CODE_ADDRESS;
use move_core_types::resolver::ModuleResolver;
use move_core_types::resolver::ResourceResolver;

Expand Down Expand Up @@ -41,6 +42,21 @@ pub fn publish_module_unchecked(
Mvm::publish_module(Origin::signed(signer), module.bytes().to_vec(), gas_limit)
}

/// Publish module as root __without__ storage check
pub fn publish_module_as_root_unchecked(module: &Asset, gas_limit: Option<u64>) -> PsResult {
let gas_limit = gas_limit.unwrap_or(DEFAULT_GAS_LIMIT);
Mvm::publish_module(Origin::root(), module.bytes().to_vec(), gas_limit)
}

/// Publish module as root __with__ storage check
pub fn publish_module_as_root(module: &Asset, gas_limit: Option<u64>) -> PsResult {
let bytecode = module.bytes().to_vec();
let name = module.name();
let result = publish_module_as_root_unchecked(module, gas_limit)?;
check_storage_module(CORE_CODE_ADDRESS, bytecode, name);
Ok(result)
}

/// Publish package.
///
/// Publish package __with__ storage check
Expand All @@ -52,7 +68,22 @@ pub fn publish_package(signer: AccountId, package: &Package, gas_limit: Option<u
Ok(result)
}

/// Publish module __without__ storage check
/// Publish package as root __with__ storage check.
pub fn publish_package_as_root(package: &Package, gas_limit: Option<u64>) -> PsResult {
let bytecode = package.bytes().to_vec();
let names = package.modules();
let result = publish_package_unchecked_as_root(package, gas_limit)?;
check_storage_package(CORE_CODE_ADDRESS, bytecode, names);
Ok(result)
}

/// Publish package as root __without__ storage checks.
pub fn publish_package_unchecked_as_root(package: &Package, gas_limit: Option<u64>) -> PsResult {
let gas_limit = gas_limit.unwrap_or(DEFAULT_GAS_LIMIT);
Mvm::publish_package(Origin::root(), package.bytes().to_vec(), gas_limit)
}

/// Publish package __without__ storage check
pub fn publish_package_unchecked(
signer: AccountId,
package: &Package,
Expand Down
2 changes: 1 addition & 1 deletion pallets/sp-mvm/tests/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use common::mock::*;
use common::addr::*;
use common::utils;

const OUT_OF_GAS_ERROR_CODE: u8 = 149;
const OUT_OF_GAS_ERROR_CODE: u8 = 150;
const MINIMAL_GAS_LIMIT: u64 = 1;

/// Check status == out of gas
Expand Down
28 changes: 23 additions & 5 deletions pallets/sp-mvm/tests/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ use common::addr::*;
use common::utils;

#[test]
/// publish modules personally as root
fn publish_module() {
/// publish modules personally as root unchecked
fn publish_module_as_root() {
new_test_ext().execute_with(|| {
utils::publish_module_as_root(&modules::root::EVENT_PROXY, None).unwrap();
});
}

#[test]
/// publish modules personally as root (ps)
fn publish_module_as_root_ps() {
new_test_ext().execute_with(|| {
let root = root_ps_acc();
utils::publish_module(root, &modules::root::EVENT_PROXY, None).unwrap();
Expand Down Expand Up @@ -68,16 +76,16 @@ fn execute_script_as_root() {
result,
DispatchError::Module {
index: 6,
error: 6,
error: 7,
message: Some("TransactionIsNotAllowedError")
}
);
});
}

#[test]
/// publish package as root
fn publish_package_as_root() {
/// publish package as root (ps)
fn publish_package_as_root_ps() {
new_test_ext().execute_with(|| {
let package = &ROOT_PACKAGE;
let root = root_ps_acc();
Expand All @@ -86,6 +94,16 @@ fn publish_package_as_root() {
});
}

/// publish package as root.
#[test]
fn publish_package_as_root() {
new_test_ext().execute_with(|| {
let package = &ROOT_PACKAGE;

utils::publish_package_as_root(package, None).unwrap();
});
}

#[test]
/// publish package as origin
fn publish_package_as_origin() {
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ impl sp_mvm::Config for Runtime {
type GasWeightMapping = MoveVMGasWeightMapping;

/// Only sudo can deploy modules under 0x or update standard library.
type UpdaterOrigin = EnsureRoot<AccountId>;
type UpdateOrigin = EnsureRoot<AccountId>;

/// Pallet Id.
type PalletId = MVMPalletId;
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/tests/mvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ fn transfer_vested_balance_fails() {
),
DispatchError::Module {
index: 67,
error: 154,
error: 155,
message: Some("Aborted")
},
);
Expand Down

0 comments on commit 44dacae

Please sign in to comment.