From 85739f3b7202de84366a6bf5d278db205447cdb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Wo=C5=BAniak?= Date: Mon, 14 Oct 2024 12:40:48 +0200 Subject: [PATCH] feat: Remove `sv_replies` feature as it does not prevent semver breaking --- examples/Cargo.toml | 2 +- sylvia-derive/Cargo.toml | 2 - sylvia-derive/src/contract.rs | 8 +- sylvia-derive/src/contract/mt.rs | 13 +- sylvia-derive/src/entry_points.rs | 26 ++-- sylvia/Cargo.toml | 2 - sylvia/src/types.rs | 22 +-- sylvia/tests/legacy_replies.rs | 125 ------------------ sylvia/tests/messages_generation.rs | 10 +- sylvia/tests/reply.rs | 2 - sylvia/tests/reply_generation.rs | 30 ++++- .../msg/overlapping_reply_handlers.rs | 1 - .../msg/overlapping_reply_handlers.stderr | 8 +- 13 files changed, 56 insertions(+), 195 deletions(-) delete mode 100644 sylvia/tests/legacy_replies.rs diff --git a/examples/Cargo.toml b/examples/Cargo.toml index b156b4a8..cd94bc3c 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -27,7 +27,7 @@ version = "0.5.0" edition = "2021" [workspace.dependencies] -sylvia = { path = "../sylvia", features = ["sv_replies"] } +sylvia = { path = "../sylvia" } cw-storage-plus = "2.0.0" cw-utils = "2.0.0" cw2 = "2.0.0" diff --git a/sylvia-derive/Cargo.toml b/sylvia-derive/Cargo.toml index 57ee7c7e..4918d60c 100644 --- a/sylvia-derive/Cargo.toml +++ b/sylvia-derive/Cargo.toml @@ -14,7 +14,6 @@ readme = "../README.md" [features] mt = [] cosmwasm_1_2 = [] -sv_replies = [] [lib] proc-macro = true @@ -43,7 +42,6 @@ sylvia = { path = "../sylvia", features = [ "cosmwasm_1_2", "cosmwasm_1_3", "cosmwasm_1_4", - "sv_replies", ] } serde = { workspace = true } cosmwasm-schema = { workspace = true } diff --git a/sylvia-derive/src/contract.rs b/sylvia-derive/src/contract.rs index b52c980c..539d706c 100644 --- a/sylvia-derive/src/contract.rs +++ b/sylvia-derive/src/contract.rs @@ -186,13 +186,9 @@ impl<'a> ContractInput<'a> { } fn emit_reply(&self) -> TokenStream { - if cfg!(feature = "sv_replies") { - let variants = MsgVariants::new(self.item.as_variants(), MsgType::Reply, &[], &None); + let variants = MsgVariants::new(self.item.as_variants(), MsgType::Reply, &[], &None); - Reply::new(self.item, &self.generics, &variants).emit() - } else { - quote! {} - } + Reply::new(self.item, &self.generics, &variants).emit() } fn emit_instantiate_builder_trait(&self) -> TokenStream { diff --git a/sylvia-derive/src/contract/mt.rs b/sylvia-derive/src/contract/mt.rs index 45934199..fc3f4f58 100644 --- a/sylvia-derive/src/contract/mt.rs +++ b/sylvia-derive/src/contract/mt.rs @@ -529,16 +529,9 @@ impl<'a> MtHelpers<'a> { quote! { #contract_ident } }; - if cfg!(feature = "sv_replies") { - quote! { - let contract = #contract_turbofish ::new(); - dispatch_reply(deps, env, msg, contract).map_err(Into::into) - } - } else { - let reply_name = _reply.name().to_case(Case::Snake); - quote! { - self. #reply_name ((deps, env).into(), msg).map_err(Into::into) - } + quote! { + let contract = #contract_turbofish ::new(); + dispatch_reply(deps, env, msg, contract).map_err(Into::into) } }) .unwrap_or_else(|| { diff --git a/sylvia-derive/src/entry_points.rs b/sylvia-derive/src/entry_points.rs index 611c4aff..fe411907 100644 --- a/sylvia-derive/src/entry_points.rs +++ b/sylvia-derive/src/entry_points.rs @@ -2,7 +2,7 @@ use proc_macro2::{Span, TokenStream}; use proc_macro_error::emit_error; use quote::quote; use syn::fold::Fold; -use syn::{parse_quote, GenericParam, Ident, ItemImpl, Type, WhereClause}; +use syn::{parse_quote, GenericParam, ItemImpl, Type, WhereClause}; use crate::crate_module; use crate::fold::StripGenerics; @@ -63,7 +63,7 @@ pub struct EntryPoints<'a> { source: &'a ItemImpl, name: Type, error: Type, - reply: Option, + is_reply: bool, override_entry_points: Vec, generics: Vec<&'a GenericParam>, where_clause: &'a Option, @@ -81,17 +81,18 @@ impl<'a> EntryPoints<'a> { let generics: Vec<_> = source.generics.params.iter().collect(); let where_clause = &source.generics.where_clause; - let reply = + let is_reply = MsgVariants::::new(source.as_variants(), MsgType::Reply, &[], &None) .variants() .map(|variant| variant.function_name().clone()) - .next(); + .next() + .is_some(); Self { source, name, error, - reply, + is_reply, override_entry_points, generics, where_clause, @@ -102,7 +103,7 @@ impl<'a> EntryPoints<'a> { pub fn emit(&self) -> TokenStream { let Self { source, - reply, + is_reply, override_entry_points, generics, where_clause, @@ -146,7 +147,7 @@ impl<'a> EntryPoints<'a> { .get_entry_point(MsgType::Reply) .map(|_| quote! {}) .unwrap_or_else(|| { - if reply.is_some() { + if *is_reply { self.emit_default_entry_point(MsgType::Reply) } else { quote! {} @@ -168,11 +169,7 @@ impl<'a> EntryPoints<'a> { fn emit_default_entry_point(&self, msg_ty: MsgType) -> TokenStream { let Self { - name, - error, - attrs, - reply, - .. + name, error, attrs, .. } = self; let sylvia = crate_module(); @@ -201,13 +198,10 @@ impl<'a> EntryPoints<'a> { _ => quote! { msg: < #contract as #sylvia ::types::ContractApi> :: #associated_name }, }; let dispatch = match msg_ty { - MsgType::Reply if cfg!(feature = "sv_replies") => quote! { + MsgType::Reply => quote! { let contract = #contract_turbofish ::new(); sv::dispatch_reply(deps, env, msg, contract).map_err(Into::into) }, - MsgType::Reply => quote! { - #contract_turbofish ::new(). #reply((deps, env).into(), msg).map_err(Into::into) - }, _ => quote! { msg.dispatch(& #contract_turbofish ::new() , ( #values )).map_err(Into::into) }, diff --git a/sylvia/Cargo.toml b/sylvia/Cargo.toml index 32bad4db..92df9d7a 100644 --- a/sylvia/Cargo.toml +++ b/sylvia/Cargo.toml @@ -38,8 +38,6 @@ cosmwasm_2_0 = [ "cw-multi-test/cosmwasm_2_0", "cosmwasm_1_4", ] -# Enables better replies -sv_replies = ["sylvia-derive/sv_replies"] [dependencies] sylvia-derive = { workspace = true } diff --git a/sylvia/src/types.rs b/sylvia/src/types.rs index b3999c6e..c34feeb8 100644 --- a/sylvia/src/types.rs +++ b/sylvia/src/types.rs @@ -1,8 +1,8 @@ //! Module providing utilities to build and use sylvia contracts. -use cosmwasm_std::{Binary, Coin, Deps, DepsMut, Empty, Env, MessageInfo, WasmMsg}; -#[cfg(feature = "sv_replies")] -use cosmwasm_std::{Event, MsgResponse}; +use cosmwasm_std::{ + Binary, Coin, Deps, DepsMut, Empty, Env, Event, MessageInfo, MsgResponse, WasmMsg, +}; use derivative::Derivative; use schemars::JsonSchema; use serde::de::DeserializeOwned; @@ -461,7 +461,6 @@ impl<'a, Contract: ?Sized> AsRef for Remote<'a, Contract> { } /// Represantation of `reply` context received in entry point. -#[cfg(feature = "sv_replies")] pub struct ReplyCtx<'a, C: cosmwasm_std::CustomQuery = Empty> { pub deps: DepsMut<'a, C>, pub env: Env, @@ -470,13 +469,6 @@ pub struct ReplyCtx<'a, C: cosmwasm_std::CustomQuery = Empty> { pub msg_responses: Vec, } -/// Represantation of `reply` context received in entry point. -#[cfg(not(feature = "sv_replies"))] -pub struct ReplyCtx<'a, C: cosmwasm_std::CustomQuery = Empty> { - pub deps: DepsMut<'a, C>, - pub env: Env, -} - /// Represantation of `migrate` context received in entry point. pub struct MigrateCtx<'a, C: cosmwasm_std::CustomQuery = Empty> { pub deps: DepsMut<'a, C>, @@ -544,7 +536,6 @@ impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for MigrateCt } } -#[cfg(feature = "sv_replies")] impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env, u64, Vec, Vec)> for ReplyCtx<'a, C> { @@ -567,13 +558,6 @@ impl<'a, C: cosmwasm_std::CustomQuery> } } -#[cfg(not(feature = "sv_replies"))] -impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env)> for ReplyCtx<'a, C> { - fn from((deps, env): (DepsMut<'a, C>, Env)) -> Self { - Self { deps, env } - } -} - impl<'a, C: cosmwasm_std::CustomQuery> From<(DepsMut<'a, C>, Env, MessageInfo)> for ExecCtx<'a, C> { fn from((deps, env, info): (DepsMut<'a, C>, Env, MessageInfo)) -> Self { Self { deps, env, info } diff --git a/sylvia/tests/legacy_replies.rs b/sylvia/tests/legacy_replies.rs deleted file mode 100644 index 16c05b39..00000000 --- a/sylvia/tests/legacy_replies.rs +++ /dev/null @@ -1,125 +0,0 @@ -#![cfg(not(feature = "sv_replies"))] - -#[cfg(all(test, feature = "mt"))] -use cw_multi_test::IntoBech32; -use sylvia::cw_std::testing::{mock_dependencies, mock_env}; -use sylvia::cw_std::{from_json, Reply, SubMsgResponse, SubMsgResult}; - -#[allow(dead_code)] -mod noop_contract { - use sylvia::types::{ExecCtx, InstantiateCtx}; - use sylvia::{contract, entry_points}; - - use sylvia::cw_std::{Response, StdResult}; - - pub struct NoopContract; - - #[entry_points] - #[contract] - impl NoopContract { - pub const fn new() -> Self { - Self - } - - #[sv::msg(instantiate)] - fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult { - Ok(Response::new()) - } - - #[sv::msg(exec)] - fn noop(&self, _ctx: ExecCtx) -> StdResult { - Ok(Response::new()) - } - } -} - -mod reply_contract { - use cosmwasm_std::{Binary, Reply, SubMsgResult}; - use sylvia::types::{ExecCtx, InstantiateCtx, ReplyCtx}; - use sylvia::{contract, entry_points}; - - use sylvia::cw_std::{to_json_binary, Response, StdResult, SubMsg, WasmMsg}; - - use super::noop_contract; - - pub struct ReplyContract; - - #[allow(dead_code)] - #[entry_points] - #[contract] - impl ReplyContract { - pub const fn new() -> Self { - Self - } - - #[sv::msg(instantiate)] - fn instantiate(&self, _ctx: InstantiateCtx) -> StdResult { - Ok(Response::new()) - } - - #[sv::msg(exec)] - fn poke(&self, _ctx: ExecCtx, noop: String) -> StdResult { - let msg = noop_contract::sv::ExecMsg::Noop {}; - let msg = WasmMsg::Execute { - contract_addr: noop, - msg: to_json_binary(&msg)?, - funds: vec![], - }; - let msg = SubMsg::reply_always(msg, 1); - - let resp = Response::new().add_submessage(msg); - Ok(resp) - } - - #[sv::msg(reply)] - fn reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult { - let resp = Response::new().set_data(to_json_binary("data")?); - Ok(resp) - } - } -} - -#[test] -fn entry_point_generation() { - let msg = Reply { - id: 0, - payload: Default::default(), - gas_used: 0, - #[allow(deprecated)] - result: SubMsgResult::Ok(SubMsgResponse { - events: vec![], - data: None, - msg_responses: vec![], - }), - }; - let mut deps = mock_dependencies(); - let env = mock_env(); - - let resp = reply_contract::entry_points::reply(deps.as_mut(), env, msg).unwrap(); - let data: String = from_json(resp.data.unwrap()).unwrap(); - - assert_eq!(data, "data"); -} - -#[cfg(all(test, feature = "mt"))] -#[test] -fn mt_helper_generation() { - use crate::reply_contract::sv::mt::ReplyContractProxy; - let app = sylvia::multitest::App::default(); - let owner = "owner".into_bech32(); - - let noop_contract_code = noop_contract::sv::mt::CodeId::store_code(&app); - let noop_contract = noop_contract_code.instantiate().call(&owner).unwrap(); - - let reply_contract_code = reply_contract::sv::mt::CodeId::store_code(&app); - let reply_contract = reply_contract_code.instantiate().call(&owner).unwrap(); - - let resp = reply_contract - .poke(noop_contract.contract_addr.to_string()) - .call(&owner) - .unwrap(); - - let data: String = from_json(resp.data.unwrap()).unwrap(); - - assert_eq!(data, "data"); -} diff --git a/sylvia/tests/messages_generation.rs b/sylvia/tests/messages_generation.rs index 439febaa..a4a6a75b 100644 --- a/sylvia/tests/messages_generation.rs +++ b/sylvia/tests/messages_generation.rs @@ -1,4 +1,3 @@ -#![cfg(not(feature = "sv_replies"))] use std::fmt::Debug; use std::str::FromStr; @@ -68,7 +67,7 @@ pub mod interface { mod contract { use cosmwasm_std::{Binary, SubMsgResult}; use sylvia::contract; - use sylvia::cw_std::{Addr, Reply, Response, StdResult}; + use sylvia::cw_std::{Addr, Response, StdResult}; use sylvia::types::{ExecCtx, InstantiateCtx, MigrateCtx, QueryCtx, ReplyCtx, SudoCtx}; use sylvia_derive::entry_points; use thiserror::Error; @@ -149,7 +148,12 @@ mod contract { } #[sv::msg(reply)] - fn my_reply(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult { + fn my_reply( + &self, + _ctx: ReplyCtx, + _result: SubMsgResult, + #[sv::payload] _payload: Binary, + ) -> StdResult { Ok(Response::new()) } diff --git a/sylvia/tests/reply.rs b/sylvia/tests/reply.rs index 4c50daaf..0ae4ef47 100644 --- a/sylvia/tests/reply.rs +++ b/sylvia/tests/reply.rs @@ -1,5 +1,3 @@ -#![cfg(feature = "sv_replies")] - use cosmwasm_schema::cw_serde; use cosmwasm_std::{to_json_binary, BankMsg, CosmosMsg, Empty, SubMsgResult}; use cw_storage_plus::Item; diff --git a/sylvia/tests/reply_generation.rs b/sylvia/tests/reply_generation.rs index bf320208..c79c6e04 100644 --- a/sylvia/tests/reply_generation.rs +++ b/sylvia/tests/reply_generation.rs @@ -1,6 +1,5 @@ -#![cfg(feature = "sv_replies")] - -use cosmwasm_std::{Binary, SubMsgResult}; +use cosmwasm_std::testing::{mock_dependencies, mock_env}; +use cosmwasm_std::{from_json, to_json_binary, Binary, Reply, SubMsgResponse, SubMsgResult}; use itertools::Itertools; use sylvia::cw_std::{Response, StdResult}; use sylvia::types::{InstantiateCtx, ReplyCtx}; @@ -27,7 +26,8 @@ impl Contract { _result: SubMsgResult, #[sv::payload] _payload: Binary, ) -> StdResult { - Ok(Response::new()) + let resp = Response::new().set_data(to_json_binary("clean")?); + Ok(resp) } #[allow(dead_code)] @@ -75,6 +75,28 @@ impl Contract { } } +#[test] +fn entry_point_generation() { + let msg = Reply { + id: sv::CLEAN_REPLY_ID, + payload: Default::default(), + gas_used: 0, + #[allow(deprecated)] + result: SubMsgResult::Ok(SubMsgResponse { + events: vec![], + data: None, + msg_responses: vec![], + }), + }; + let mut deps = mock_dependencies(); + let env = mock_env(); + + let resp = entry_points::reply(deps.as_mut(), env, msg).unwrap(); + let data: String = from_json(resp.data.unwrap()).unwrap(); + + assert_eq!(data, "clean"); +} + #[test] fn reply_id_generation() { // Assert IDs uniqueness diff --git a/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.rs b/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.rs index 0a18cd84..1c71ab60 100644 --- a/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.rs +++ b/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.rs @@ -1,5 +1,4 @@ #![allow(unused_imports)] -#![cfg(feature = "sv_replies")] use sylvia::contract; use sylvia::cw_std::{Reply, Response, StdResult}; diff --git a/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.stderr b/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.stderr index 4b592cba..188d07d2 100644 --- a/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.stderr +++ b/sylvia/tests/ui/attributes/msg/overlapping_reply_handlers.stderr @@ -2,16 +2,16 @@ error: Duplicated reply handler. = note: Previous definition of handler=`HANDLER_1_REPLY_ID` for reply_on=`always` defined on `fn reply_always()` - --> tests/ui/attributes/msg/overlapping_reply_handlers.rs:26:32 + --> tests/ui/attributes/msg/overlapping_reply_handlers.rs:25:32 | -26 | #[sv::msg(reply, handlers=[handler1], reply_on=success)] +25 | #[sv::msg(reply, handlers=[handler1], reply_on=success)] | ^^^^^^^^ error: Duplicated reply handler. = note: Previous definition of handler=`HANDLER_2_REPLY_ID` for reply_on=`failure` defined on `fn some_reply()` - --> tests/ui/attributes/msg/overlapping_reply_handlers.rs:41:8 + --> tests/ui/attributes/msg/overlapping_reply_handlers.rs:40:8 | -41 | fn handler2(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult { +40 | fn handler2(&self, _ctx: ReplyCtx, _reply: Reply) -> StdResult { | ^^^^^^^^