Skip to content

Commit

Permalink
Use cast everywhere if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx committed Dec 4, 2023
1 parent 450854f commit 6bdf4a1
Show file tree
Hide file tree
Showing 23 changed files with 139 additions and 194 deletions.
2 changes: 1 addition & 1 deletion common/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use sp_std::borrow::ToOwned;

pub fn account<AccountId: Origin>(name: &'static str, index: u32, seed: u32) -> AccountId {
let entropy = (name, index, seed).using_encoded(blake2_256);
AccountId::from_origin(H256::from_slice(&entropy[..]))
H256::from_slice(&entropy[..]).cast()
}

// A wasm module that allocates `$num_pages` of memory in `init` function.
Expand Down
5 changes: 1 addition & 4 deletions common/src/storage/primitives/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ impl<T: Origin> KeyFor for MailboxKeyGen<T> {
type Value = UserStoredMessage;

fn key_for(value: &Self::Value) -> Self::Key {
(
T::from_origin(value.destination().into_origin()),
value.id(),
)
(value.destination().cast(), value.id())
}
}

Expand Down
4 changes: 1 addition & 3 deletions lazy-pages/interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ extern crate alloc;

use byteorder::{ByteOrder, LittleEndian};
use core::fmt;
use gear_common::Origin;
use gear_core::{
ids::ProgramId,
memory::{HostPointer, Memory, MemoryInterval},
Expand Down Expand Up @@ -75,10 +74,9 @@ pub fn init_for_program(
wasm_mem_size: mem.size().raw(),
stack_end: stack_end.map(|p| p.raw()),
program_key: {
let program_id = <[u8; 32]>::from(program_id.into_origin());
let memory_infix = memory_infix.inner().to_le_bytes();

[&program_id[..], &memory_infix[..]].concat()
[program_id.as_ref(), memory_infix.as_ref()].concat()
},
globals_config,
weights,
Expand Down
2 changes: 1 addition & 1 deletion pallets/gas/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Gas = <Pallet<Test> as common::GasProvider>::GasTree;
type GasTree = GasNodes<Test>;

fn random_node_id() -> MessageId {
MessageId::from_origin(H256::random())
H256::random().cast()
}

const MULTIPLIER: GasMultiplier<u128, u64> = GasMultiplier::ValuePerGas(25);
Expand Down
9 changes: 4 additions & 5 deletions pallets/gear-debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub mod pallet {
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
use frame_system::pallet_prelude::*;
use gear_core::{
ids::{CodeId, ProgramId},
ids::ProgramId,
memory::PageBuf,
message::{StoredDispatch, StoredMessage},
pages::{GearPage, PageU32Size, WasmPage},
Expand Down Expand Up @@ -184,8 +184,8 @@ pub mod pallet {

let message = StoredMessage::new(
msg.id(),
ProgramId::from_origin(source),
ProgramId::from_origin(destination),
source.cast(),
destination.cast(),
(*msg.payload_bytes()).to_vec().try_into().unwrap(),
msg.value(),
msg.details(),
Expand All @@ -211,8 +211,7 @@ pub mod pallet {
}
}
};
let code_id = CodeId::from_origin(active.code_hash);
let static_pages = match T::CodeStorage::get_code(code_id) {
let static_pages = match T::CodeStorage::get_code(active.code_hash.cast()) {
Some(code) => code.static_pages(),
None => WasmPage::zero(),
};
Expand Down
2 changes: 1 addition & 1 deletion pallets/gear-debug/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ fn disabled_program_rent() {

let block_count = 2_000u32;
let rent = RentCostPerBlockOf::<Test>::get() * u128::from(block_count);
let pay_rent_account_id = AccountId::from_origin(pay_rent_id.into_origin());
let pay_rent_account_id = pay_rent_id.cast::<AccountId>();
let balance_before = Balances::free_balance(pay_rent_account_id);
assert_ok!(Gear::send_message(
RuntimeOrigin::signed(1),
Expand Down
6 changes: 3 additions & 3 deletions pallets/gear-scheduler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ fn dispatch_from(src: impl Into<ProgramId>) -> StoredDispatch {
StoredDispatch::new(
DispatchKind::Handle,
StoredMessage::new(
MessageId::from_origin(H256::random().into_origin()),
H256::random().cast(),
src.into(),
ProgramId::from_origin(H256::random().into_origin()),
H256::random().cast(),
Default::default(),
0,
None,
Expand Down Expand Up @@ -111,7 +111,7 @@ fn out_of_rent_reply_exists(
mid: impl Into<MessageId>,
pid: impl Into<ProgramId>,
) -> bool {
let src = ProgramId::from_origin(user_id.into_origin());
let src = user_id.cast();
let mid = mid.into();
let pid = pid.into();

Expand Down
6 changes: 3 additions & 3 deletions pallets/gear-voucher/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ benchmarks! {
100_000_000_000_000_u128.unique_saturated_into()
);
let holder = benchmarking::account::<T::AccountId>("caller", 0, 1);
let program_id = ProgramId::from_origin(
benchmarking::account::<T::AccountId>("program", 0, 100).into_origin()
);
let program_id =
benchmarking::account::<T::AccountId>("program", 0, 100).cast()
;

let holder_lookup = T::Lookup::unlookup(holder.clone());
}: _(RawOrigin::Signed(issuer), holder_lookup, program_id, 10_000_000_000_000_u128.unique_saturated_into())
Expand Down
4 changes: 2 additions & 2 deletions pallets/gear-voucher/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use primitive_types::H256;
#[test]
fn voucher_issue_works() {
new_test_ext().execute_with(|| {
let program_id = ProgramId::from_origin(H256::from(b"some//quasy//random//program//id"));
let program_id = H256::from(b"some//quasy//random//program//id").cast();
let synthesized = Voucher::voucher_account_id(&BOB, &program_id);

assert_ok!(Voucher::issue(
Expand Down Expand Up @@ -55,7 +55,7 @@ fn voucher_issue_works() {
#[test]
fn voucher_redemption_works() {
new_test_ext().execute_with(|| {
let program_id = ProgramId::from_origin(H256::from(b"some//quasy//random//program//id"));
let program_id = H256::from(b"some//quasy//random//program//id").cast();
let synthesized = Voucher::voucher_account_id(&BOB, &program_id);

assert_ok!(Voucher::issue(
Expand Down
13 changes: 6 additions & 7 deletions pallets/gear/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]

use gear_common::Origin;
use gear_core::ids::{CodeId, MessageId, ProgramId};
use gear_core_errors::*;
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
Expand Down Expand Up @@ -272,7 +271,7 @@ where
let GasInfo { min_limit, .. } = self.calculate_gas_info(
at_hash,
source,
HandleKind::InitByHash(CodeId::from_origin(code_id)),
HandleKind::InitByHash(code_id.cast()),
payload.to_vec(),
value,
allow_other_panics,
Expand All @@ -282,7 +281,7 @@ where
self.calculate_gas_info(
at_hash,
source,
HandleKind::InitByHash(CodeId::from_origin(code_id)),
HandleKind::InitByHash(code_id.cast()),
payload.to_vec(),
value,
allow_other_panics,
Expand Down Expand Up @@ -336,7 +335,7 @@ where
let GasInfo { min_limit, .. } = self.calculate_gas_info(
at_hash,
source,
HandleKind::Handle(ProgramId::from_origin(dest)),
HandleKind::Handle(dest.cast()),
payload.to_vec(),
value,
allow_other_panics,
Expand All @@ -346,7 +345,7 @@ where
self.calculate_gas_info(
at_hash,
source,
HandleKind::Handle(ProgramId::from_origin(dest)),
HandleKind::Handle(dest.cast()),
payload.to_vec(),
value,
allow_other_panics,
Expand All @@ -369,7 +368,7 @@ where
at_hash,
source,
HandleKind::Reply(
MessageId::from_origin(message_id),
message_id.cast(),
ReplyCode::Success(SuccessReplyReason::Manual),
),
payload.to_vec(),
Expand All @@ -382,7 +381,7 @@ where
at_hash,
source,
HandleKind::Reply(
MessageId::from_origin(message_id),
message_id.cast(),
ReplyCode::Success(SuccessReplyReason::Manual),
),
payload.to_vec(),
Expand Down
24 changes: 12 additions & 12 deletions pallets/gear/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ where
RawOrigin::Signed(caller).into(),
program_id,
program.allocations,
CodeId::from_origin(program.code_hash),
program.code_hash.cast(),
)
.expect("failed to start resume session");

Expand Down Expand Up @@ -469,8 +469,8 @@ benchmarks! {
let program_id = benchmarking::account::<T::AccountId>("program", 0, 100);
CurrencyOf::<T>::deposit_creating(&program_id, 100_000_000_000_000_u128.unique_saturated_into());
let code = benchmarking::generate_wasm2(16.into()).unwrap();
benchmarking::set_program::<ProgramStorageOf::<T>, _>(ProgramId::from_origin(program_id.clone().into_origin()), code, 1.into());
let original_message_id = MessageId::from_origin(benchmarking::account::<T::AccountId>("message", 0, 100).into_origin());
benchmarking::set_program::<ProgramStorageOf::<T>, _>(program_id.clone().cast(), code, 1.into());
let original_message_id = benchmarking::account::<T::AccountId>("message", 0, 100).cast();
let gas_limit = 50000;
let value = 10000u32.into();
let multiplier = <T as pallet_gear_bank::Config>::GasMultiplier::get();
Expand All @@ -479,8 +479,8 @@ benchmarks! {
GearBank::<T>::deposit_value(&program_id, value, true).unwrap_or_else(|e| unreachable!("Gear bank error: {e:?}"));
MailboxOf::<T>::insert(gear_core::message::StoredMessage::new(
original_message_id,
ProgramId::from_origin(program_id.into_origin()),
ProgramId::from_origin(caller.clone().into_origin()),
program_id.cast(),
caller.clone().cast(),
Default::default(),
value.unique_saturated_into(),
None,
Expand Down Expand Up @@ -531,7 +531,7 @@ benchmarks! {
.try_into()
.expect("program should be active");
ProgramStorageOf::<T>::pause_program(program_id, 100u32.into()).unwrap();
}: _(RawOrigin::Signed(caller.clone()), program_id, program.allocations, CodeId::from_origin(program.code_hash))
}: _(RawOrigin::Signed(caller.clone()), program_id, program.allocations, program.code_hash.cast())
verify {
assert!(ProgramStorageOf::<T>::paused_program_exists(&program_id));
assert!(
Expand Down Expand Up @@ -695,7 +695,7 @@ benchmarks! {
let caller = benchmarking::account("caller", 0, 0);
CurrencyOf::<T>::deposit_creating(&caller, 100_000_000_000_000_u128.unique_saturated_into());
let minimum_balance = CurrencyOf::<T>::minimum_balance();
let program_id = ProgramId::from_origin(benchmarking::account::<T::AccountId>("program", 0, 100).into_origin());
let program_id = benchmarking::account::<T::AccountId>("program", 0, 100).cast();
let code = benchmarking::generate_wasm2(16.into()).unwrap();
benchmarking::set_program::<ProgramStorageOf::<T>, _>(program_id, code, 1.into());
let payload = vec![0_u8; p as usize];
Expand All @@ -714,8 +714,8 @@ benchmarks! {
let program_id = benchmarking::account::<T::AccountId>("program", 0, 100);
CurrencyOf::<T>::deposit_creating(&program_id, 100_000_000_000_000_u128.unique_saturated_into());
let code = benchmarking::generate_wasm2(16.into()).unwrap();
benchmarking::set_program::<ProgramStorageOf::<T>, _>(ProgramId::from_origin(program_id.clone().into_origin()), code, 1.into());
let original_message_id = MessageId::from_origin(benchmarking::account::<T::AccountId>("message", 0, 100).into_origin());
benchmarking::set_program::<ProgramStorageOf::<T>, _>(program_id.clone().cast(), code, 1.into());
let original_message_id = benchmarking::account::<T::AccountId>("message", 0, 100).cast();
let gas_limit = 50000;
let value = (p % 2).into();
let multiplier = <T as pallet_gear_bank::Config>::GasMultiplier::get();
Expand All @@ -724,8 +724,8 @@ benchmarks! {
GearBank::<T>::deposit_value(&program_id, value, true).unwrap_or_else(|e| unreachable!("Gear bank error: {e:?}"));
MailboxOf::<T>::insert(gear_core::message::StoredMessage::new(
original_message_id,
ProgramId::from_origin(program_id.into_origin()),
ProgramId::from_origin(caller.clone().into_origin()),
program_id.cast(),
caller.clone().cast(),
Default::default(),
value.unique_saturated_into(),
None,
Expand Down Expand Up @@ -2851,7 +2851,7 @@ benchmarks! {
let (user, message_id) = tasks::remove_from_mailbox::<T>();
let mut ext_manager = ExtManager::<T>::default();
}: {
ext_manager.remove_from_mailbox(T::AccountId::from_origin(user.into_origin()), message_id);
ext_manager.remove_from_mailbox(user.cast(), message_id);
}

tasks_pause_program {
Expand Down
8 changes: 4 additions & 4 deletions pallets/gear/src/benchmarking/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ where
let instance = Program::<T>::new(module.into(), vec![])?;
utils::prepare_exec::<T>(
instance.caller.into_origin(),
HandleKind::Handle(ProgramId::from_origin(instance.addr)),
HandleKind::Handle(instance.addr.cast()),
vec![],
PrepareConfig {
value: value.into(),
Expand Down Expand Up @@ -159,7 +159,7 @@ where
let instance = Program::<T>::new(module.into(), vec![])?;
utils::prepare_exec::<T>(
instance.caller.into_origin(),
HandleKind::Handle(ProgramId::from_origin(instance.addr)),
HandleKind::Handle(instance.addr.cast()),
vec![],
PrepareConfig {
value: value.into(),
Expand All @@ -176,7 +176,7 @@ where
let instance = Program::<T>::new(module.into(), vec![])?;

// insert gas reservation slots
let program_id = ProgramId::from_origin(instance.addr);
let program_id = instance.addr.cast();
ProgramStorageOf::<T>::update_active_program(program_id, |program| {
for x in 0..repetitions {
program.gas_reservation_map.insert(
Expand Down Expand Up @@ -205,7 +205,7 @@ where
let instance = Program::<T>::new(module.into(), vec![])?;
utils::prepare_exec::<T>(
instance.caller.into_origin(),
HandleKind::Handle(ProgramId::from_origin(instance.addr)),
HandleKind::Handle(instance.addr.cast()),
vec![0xff; MAX_PAYLOAD_LEN as usize],
Default::default(),
)
Expand Down
10 changes: 5 additions & 5 deletions pallets/gear/src/benchmarking/tests/lazy_pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ where
};
let instance = Program::<T>::new(module.into(), vec![]).unwrap();
let source = instance.caller.into_origin();
let program_id = ProgramId::from_origin(instance.addr);
let program_id = instance.addr.cast();

// Append data in storage for some pages.
for page in (0..rng.gen_range(0..MAX_PAGES_WITH_DATA))
Expand Down Expand Up @@ -369,7 +369,7 @@ where
let instance = instance.clone();
let mut exec = common_utils::prepare_exec::<T>(
instance.caller.into_origin(),
HandleKind::Handle(ProgramId::from_origin(instance.addr)),
HandleKind::Handle(instance.addr.cast()),
vec![],
Default::default(),
)
Expand Down Expand Up @@ -529,7 +529,7 @@ where
let gas_burned = {
let mut exec = common_utils::prepare_exec::<T>(
source,
HandleKind::Handle(ProgramId::from_origin(origin)),
HandleKind::Handle(origin.cast()),
vec![],
Default::default(),
)
Expand Down Expand Up @@ -563,7 +563,7 @@ where
{
let mut exec = common_utils::prepare_exec::<T>(
source,
HandleKind::Handle(ProgramId::from_origin(origin)),
HandleKind::Handle(origin.cast()),
vec![],
PrepareConfig {
gas_limit: gas_burned,
Expand Down Expand Up @@ -601,7 +601,7 @@ where
{
let mut exec = common_utils::prepare_exec::<T>(
source,
HandleKind::Handle(ProgramId::from_origin(origin)),
HandleKind::Handle(origin.cast()),
vec![],
PrepareConfig {
gas_allowance: gas_burned,
Expand Down
Loading

0 comments on commit 6bdf4a1

Please sign in to comment.