Skip to content

refactor: use gstd::message_loop only for async ctors and service methods #939

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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
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
28 changes: 28 additions & 0 deletions rs/ethexe/Cargo.lock

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

3 changes: 2 additions & 1 deletion rs/ethexe/macros-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ prettyplease.workspace = true
proc-macro2.workspace = true
quote.workspace = true
sails-macros-core = { workspace = true }
sails-rs = { workspace = true }
sails-rs = { workspace = true, features = ["ethexe"] }
syn = { workspace = true, features = ["full", "extra-traits"] }
tokio = { workspace = true, features = ["full"] }
trybuild = "1.0"
13 changes: 0 additions & 13 deletions rs/ethexe/macros-tests/tests/program_insta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,6 @@ fn generates_handle_for_multiple_services_with_non_empty_routes() {
insta::assert_snapshot!(result);
}

#[test]
fn generates_handle_with_gprogram_attributes() {
let args = quote!(handle_signal = my_handle_signal);
let input = quote! {
impl MyProgram {}
};

let result = gprogram(args, input).to_string();
let result = prettyplease::unparse(&syn::parse_str(&result).unwrap());

insta::assert_snapshot!(result);
}

#[test]
fn generates_handle_with_crate_path() {
let args = quote!(crate = sails_rename,);
Expand Down
64 changes: 39 additions & 25 deletions rs/ethexe/macros-tests/tests/service_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ async fn service_with_basics() {
const DO_THIS: &str = "DoThis";
let input = (0u128, false, 42u32, "correct".to_owned()).abi_encode_sequence();

let mut exposure = MyService.expose(MessageId::from(123), &[1, 2, 3]);

assert!(
exposure
.try_handle_solidity(&DO_THIS.encode(), &input)
.is_none()
);

// Check asyncness for `DoThis`.
assert!(exposure.check_asyncness(&DO_THIS.encode()).unwrap());

// act
let (output, ..) = MyService
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&DO_THIS.encode(), &input)
let (output, ..) = exposure
.try_handle_solidity_async(&DO_THIS.encode(), &input)
.await
.unwrap();

Expand All @@ -41,7 +51,7 @@ async fn service_with_basics_with_encode_reply() {
// act
let (output, ..) = MyService
.expose(message_id, &[1, 2, 3])
.try_handle_solidity(&DO_THIS.encode(), &input)
.try_handle_solidity_async(&DO_THIS.encode(), &input)
.await
.unwrap();

Expand All @@ -63,8 +73,8 @@ fn service_with_events() {
assert_eq!(events[0], MyEvents::Event1);
}

#[tokio::test]
async fn service_with_lifetimes_and_events() {
#[test]
fn service_with_lifetimes_and_events() {
use service_with_events_and_lifetimes::{MyEvents, MyGenericEventsService};

const DO_THIS: &str = "DoThis";
Expand All @@ -73,9 +83,10 @@ async fn service_with_lifetimes_and_events() {
let my_service = MyGenericEventsService::<'_, String>::default();
let mut exposure = my_service.expose(MessageId::from(123), &[1, 2, 3]);

assert!(!exposure.check_asyncness(&DO_THIS.encode()).unwrap());

let (output, ..) = exposure
.try_handle_solidity(&DO_THIS.encode(), input.as_slice())
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
Expand All @@ -86,8 +97,8 @@ async fn service_with_lifetimes_and_events() {
assert_eq!(events[0], MyEvents::Event1);
}

#[tokio::test]
async fn service_with_extends() {
#[test]
fn service_with_extends() {
use service_with_extends::{
base::{BASE_NAME_RESULT, Base},
extended::{EXTENDED_NAME_RESULT, Extended, NAME_RESULT},
Expand All @@ -100,34 +111,42 @@ async fn service_with_extends() {

let mut extended_svc = Extended::new(Base).expose(123.into(), &[1, 2, 3]);

assert!(
!extended_svc
.check_asyncness(&EXTENDED_NAME_METHOD.encode())
.unwrap()
);

let (output, ..) = extended_svc
.try_handle_solidity(&EXTENDED_NAME_METHOD.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
assert_eq!(Ok(EXTENDED_NAME_RESULT.to_owned()), result);

assert!(
!extended_svc
.check_asyncness(&BASE_NAME_METHOD.encode())
.unwrap()
);
let _base: &<Base as Service>::Exposure = extended_svc.as_base_0();

let (output, ..) = extended_svc
.try_handle_solidity(&BASE_NAME_METHOD.encode(), &input)
.await
.unwrap();
let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
assert_eq!(Ok(BASE_NAME_RESULT.to_owned()), result);

let (output, ..) = extended_svc
.try_handle_solidity(&NAME_METHOD.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
assert_eq!(Ok(NAME_RESULT.to_owned()), result);
}

#[tokio::test]
async fn service_with_lifecycles_and_generics() {
#[test]
fn service_with_lifecycles_and_generics() {
use service_with_lifecycles_and_generics::MyGenericService;

const DO_THIS: &str = "DoThis";
Expand All @@ -138,15 +157,14 @@ async fn service_with_lifecycles_and_generics() {
let (output, ..) = my_service
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&DO_THIS.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
assert_eq!(Ok(42u32), result);
}

#[tokio::test]
async fn service_with_extends_and_lifetimes() {
#[test]
fn service_with_extends_and_lifetimes() {
use service_with_extends_and_lifetimes::{
BASE_NAME_RESULT, BaseWithLifetime, EXTENDED_NAME_RESULT, ExtendedWithLifetime, NAME_RESULT,
};
Expand All @@ -164,23 +182,20 @@ async fn service_with_extends_and_lifetimes() {

let (output, ..) = extended_svc
.try_handle_solidity(&EXTENDED_NAME_METHOD.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
assert_eq!(Ok(EXTENDED_NAME_RESULT.to_owned()), result);

let (output, ..) = extended_svc
.try_handle_solidity(&BASE_NAME_METHOD.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
assert_eq!(Ok(BASE_NAME_RESULT.to_owned()), result);

let (output, ..) = extended_svc
.try_handle_solidity(&NAME_METHOD.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
Expand All @@ -196,7 +211,7 @@ async fn service_with_export_unwrap_result() {
let input = (0u128, false, 42u32, "correct").abi_encode_sequence();
let (output, ..) = MyService
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&DO_THIS.encode(), input.as_slice())
.try_handle_solidity_async(&DO_THIS.encode(), input.as_slice())
.await
.unwrap();

Expand All @@ -214,7 +229,7 @@ async fn service_with_export_unwrap_result_panic() {

_ = MyService
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&PARSE.encode(), input.as_slice())
.try_handle_solidity_async(&PARSE.encode(), input.as_slice())
.await
.unwrap();
}
Expand All @@ -228,7 +243,7 @@ async fn service_with_reply_with_value() {
let input = (0u128, false, 42u32, "correct".to_owned()).abi_encode_sequence();
let (output, value, ..) = MyServiceWithReplyWithValue
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&DO_THIS.encode(), input.as_slice())
.try_handle_solidity_async(&DO_THIS.encode(), input.as_slice())
.await
.unwrap();

Expand All @@ -247,7 +262,7 @@ async fn service_with_reply_with_value_with_impl_from() {
let input = (0u128, false, 42u32, "correct".to_owned()).abi_encode_sequence();
let (output, value, ..) = MyServiceWithReplyWithValue
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&DO_THAT.encode(), input.as_slice())
.try_handle_solidity_async(&DO_THAT.encode(), input.as_slice())
.await
.unwrap();

Expand All @@ -267,7 +282,6 @@ async fn service_with_trait_bounds() {
let (output, ..) = MyServiceWithTraitBounds::<u32>::default()
.expose(MessageId::from(123), &[1, 2, 3])
.try_handle_solidity(&DO_THIS.encode(), &input)
.await
.unwrap();

let result = sails_rs::alloy_sol_types::SolValue::abi_decode(output.as_slice(), false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl MyService {
Ok(res)
}

#[allow(unused)]
pub fn this(&self, p1: bool) -> bool {
!p1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ impl MyServiceWithReplyWithValue {
(format!("{p1}: {p2}"), 100_000_000_000)
}

#[allow(unused)]
pub fn this(&self, p1: bool) -> bool {
!p1
}
Expand Down
Loading