Skip to content

Commit

Permalink
refactor!(core): change ActorIds implementation of From<u64> (#4377)
Browse files Browse the repository at this point in the history
  • Loading branch information
breathx authored Dec 10, 2024
1 parent e29a699 commit c2a71ed
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 69 deletions.
2 changes: 1 addition & 1 deletion common/numerated/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ impl<T: Numerated> IntervalsTree<T> {
/// Iterating complexity: `O(n + m)`, where
/// - `n` is amount of intervals in `self`
/// - `m` is amount of intervals in `other`
pub fn difference<'a>(&'a self, other: &'a Self) -> impl Iterator<Item = Interval<T>> + '_ {
pub fn difference<'a>(&'a self, other: &'a Self) -> impl Iterator<Item = Interval<T>> + 'a {
DifferenceIterator {
iter1: self.iter(),
iter2: other.iter(),
Expand Down
1 change: 1 addition & 0 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub use gas_provider::{
/// Type alias for gas entity.
pub type Gas = u64;

/// NOTE: Implementation of this for `u64` places bytes from idx=0.
pub trait Origin: Sized {
fn into_origin(self) -> H256;
fn from_origin(val: H256) -> Self;
Expand Down
12 changes: 11 additions & 1 deletion gprimitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ pub struct MessageHandle(u32);
/// struct. The source `ActorId` for a message being processed can be obtained
/// using `gstd::msg::source()` function. Also, each send function has a target
/// `ActorId` as one of the arguments.
///
/// NOTE: Implementation of `From<u64>` places bytes from idx=12 for Eth compatibility.
#[derive(Clone, Copy, Default, Hash, Ord, PartialEq, PartialOrd, Eq, From, Into, AsRef, AsMut)]
#[as_ref(forward)]
#[as_mut(forward)]
#[cfg_attr(feature = "codec", derive(TypeInfo, Encode, Decode, MaxEncodedLen), codec(crate = scale))]
pub struct ActorId([u8; 32]);

macros::impl_primitive!(new zero into_bytes from_u64 from_h256 into_h256 try_from_slice debug, ActorId);
macros::impl_primitive!(new zero into_bytes from_h256 into_h256 try_from_slice debug, ActorId);

impl ActorId {
/// Returns the ss58-check address with default ss58 version.
Expand All @@ -111,6 +113,14 @@ impl ActorId {
}
}

impl From<u64> for ActorId {
fn from(value: u64) -> Self {
let mut id = Self::zero();
id.0[12..20].copy_from_slice(&value.to_le_bytes()[..]);
id
}
}

impl From<H160> for ActorId {
fn from(h160: H160) -> Self {
let mut actor_id = Self::zero();
Expand Down
21 changes: 14 additions & 7 deletions gtest/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,11 +867,8 @@ pub mod gbuild {
#[cfg(test)]
mod tests {
use super::Program;

use crate::{Log, ProgramIdWrapper, System, Value, DEFAULT_USER_ALICE, EXISTENTIAL_DEPOSIT};
use demo_constructor::{Arg, Scheme};
use gear_common::Origin;

use gear_core::ids::ActorId;
use gear_core_errors::{ErrorReplyReason, ReplyCode, SimpleExecutionError};

Expand Down Expand Up @@ -1085,7 +1082,7 @@ mod tests {

#[test]
#[should_panic(
expected = "Insufficient balance: user (0x0500000000000000000000000000000000000000000000000000000000000000) \
expected = "Insufficient balance: user (0x0000000000000000000000000500000000000000000000000000000000000000) \
tries to send (1000000000001) value, (4500000000000) gas and ED (1000000000000), while his balance (1000000000000)"
)]
fn fails_on_insufficient_balance() {
Expand Down Expand Up @@ -1446,7 +1443,12 @@ mod tests {
let handle = Calls::builder()
.reserve_gas(10_000_000_000, 5)
.store("reservation")
.reservation_send_value("reservation", user_id.into_origin().0, payload.clone(), 0);
.reservation_send_value(
"reservation",
ActorId::from(user_id).into_bytes(),
payload.clone(),
0,
);
let msg_id = prog.send(user_id, handle);
let res = sys.run_next_block();
assert!(res.succeed.contains(&msg_id));
Expand All @@ -1459,7 +1461,7 @@ mod tests {
let new_prog_id = 4343;
let new_program = Program::from_binary_with_id(&sys, new_prog_id, WASM_BINARY);
let payload = b"sup!".to_vec();
let handle = Calls::builder().send(user_id.into_origin().0, payload.clone());
let handle = Calls::builder().send(ActorId::from(user_id).into_bytes(), payload.clone());
let scheme = Scheme::predefined(
Calls::builder().noop(),
handle,
Expand All @@ -1474,7 +1476,12 @@ mod tests {
let handle = Calls::builder()
.reserve_gas(10_000_000_000, 5)
.store("reservation")
.reservation_send_value("reservation", new_prog_id.into_origin().0, [], 0);
.reservation_send_value(
"reservation",
ActorId::from(new_prog_id).into_bytes(),
[],
0,
);
let msg_id = prog.send(user_id, handle);
let res = sys.run_next_block();
assert!(res.succeed.contains(&msg_id));
Expand Down
2 changes: 1 addition & 1 deletion pallets/gear-builtin/src/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn calculate_gas_info_works() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER) && message.details().is_some() && {
message.destination() == SIGNER.cast() && message.details().is_some() && {
let details = message.details().expect("Value checked above");
details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
Expand Down
42 changes: 21 additions & 21 deletions pallets/gear-builtin/src/tests/bls381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ fn decoding_error() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::UserspacePanic,
Expand Down Expand Up @@ -137,7 +137,7 @@ fn multi_miller_loop() {

assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::UserspacePanic,
Expand Down Expand Up @@ -172,7 +172,7 @@ fn multi_miller_loop() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -197,7 +197,7 @@ fn multi_miller_loop() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -256,7 +256,7 @@ fn final_exponentiation() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -281,7 +281,7 @@ fn final_exponentiation() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -339,7 +339,7 @@ fn msm_g1() {

assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::UserspacePanic,
Expand Down Expand Up @@ -374,7 +374,7 @@ fn msm_g1() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -399,7 +399,7 @@ fn msm_g1() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -457,7 +457,7 @@ fn msm_g2() {

assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::UserspacePanic,
Expand Down Expand Up @@ -492,7 +492,7 @@ fn msm_g2() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -517,7 +517,7 @@ fn msm_g2() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -574,7 +574,7 @@ fn mul_projective_g1() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -599,7 +599,7 @@ fn mul_projective_g1() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -656,7 +656,7 @@ fn mul_projective_g2() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -681,7 +681,7 @@ fn mul_projective_g2() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -717,7 +717,7 @@ fn aggregate_g1() {
let encoded_points = ark_points.encode();

let payload = Request::AggregateG1 { points: encoded_points }.encode();
let builtin_actor_id: ProgramId = H256::from(ACTOR_ID).cast();
let builtin_actor_id = ACTOR_ID.into();
let gas_info = get_gas_info(builtin_actor_id, payload.clone());

// Check the case of insufficient gas
Expand All @@ -736,7 +736,7 @@ fn aggregate_g1() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -761,7 +761,7 @@ fn aggregate_g1() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down Expand Up @@ -816,7 +816,7 @@ fn map_to_g2affine() {
// An error reply should have been sent.
assert!(System::events().into_iter().any(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
message.destination() == ProgramId::from(SIGNER)
message.destination() == SIGNER.cast()
&& matches!(message.details(), Some(details) if details.to_reply_code()
== ReplyCode::Error(ErrorReplyReason::Execution(
SimpleExecutionError::RanOutOfGas,
Expand All @@ -841,7 +841,7 @@ fn map_to_g2affine() {

let response = match System::events().into_iter().find_map(|e| match e.event {
RuntimeEvent::Gear(pallet_gear::Event::<Test>::UserMessageSent { message, .. }) => {
assert_eq!(message.destination(), ProgramId::from(SIGNER));
assert_eq!(message.destination(), SIGNER.cast());
assert!(matches!(message.details(), Some(details) if matches!(details.to_reply_code(), ReplyCode::Success(..))));

Some(message.payload_bytes().to_vec())
Expand Down
12 changes: 7 additions & 5 deletions pallets/gear-debug/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use super::*;
use crate::mock::*;
use common::{self, event::MessageEntry, CodeStorage, Origin as _};
use common::{self, event::MessageEntry, CodeStorage, Origin};
use frame_support::assert_ok;
use gear_core::{
ids::{prelude::*, CodeId, MessageId, ProgramId},
Expand Down Expand Up @@ -251,7 +251,7 @@ fn debug_mode_works() {
DispatchKind::Handle,
StoredMessage::new(
message_id_1,
1.into(),
1.cast(),
program_id_1,
Default::default(),
0,
Expand All @@ -263,7 +263,7 @@ fn debug_mode_works() {
DispatchKind::Handle,
StoredMessage::new(
message_id_2,
1.into(),
1.cast(),
program_id_2,
Default::default(),
0,
Expand Down Expand Up @@ -356,10 +356,12 @@ fn get_last_program_id() -> ProgramId {
}

#[track_caller]
fn maybe_last_message(account: u64) -> Option<UserMessage> {
fn maybe_last_message(account: impl Origin) -> Option<UserMessage> {
let account = account.cast();

System::events().into_iter().rev().find_map(|e| {
if let super::mock::RuntimeEvent::Gear(Event::UserMessageSent { message, .. }) = e.event {
if message.destination() == account.into() {
if message.destination() == account {
Some(message)
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions pallets/gear-scheduler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ fn wl_cost_for(amount_of_blocks: u64) -> u128 {
gas_price(<Pallet<Test> as Scheduler>::CostsPerBlock::waitlist() * amount_of_blocks)
}

fn dispatch_from(src: impl Into<ProgramId>) -> StoredDispatch {
fn dispatch_from(src: impl Origin) -> StoredDispatch {
StoredDispatch::new(
DispatchKind::Handle,
StoredMessage::new(
H256::random().cast(),
src.into(),
src.cast(),
H256::random().cast(),
Default::default(),
0,
Expand Down
Loading

0 comments on commit c2a71ed

Please sign in to comment.