Skip to content

Add #[entry_point] macro attribute to generate entry points #701

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

Merged
merged 13 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions contracts/burner/Cargo.lock

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

5 changes: 3 additions & 2 deletions contracts/burner/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cosmwasm_std::{
attr, BankMsg, Deps, DepsMut, Env, HandleResponse, InitResponse, MessageInfo, MigrateResponse,
Order, QueryResponse, StdError, StdResult,
attr, entry_point, BankMsg, Deps, DepsMut, Env, HandleResponse, InitResponse, MessageInfo,
MigrateResponse, Order, QueryResponse, StdError, StdResult,
};

use crate::msg::{HandleMsg, InitMsg, MigrateMsg, QueryMsg};
Expand All @@ -27,6 +27,7 @@ pub fn handle(
))
}

#[entry_point]
pub fn migrate(
deps: DepsMut,
env: Env,
Expand Down
2 changes: 1 addition & 1 deletion contracts/burner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod contract;
pub mod msg;

#[cfg(target_arch = "wasm32")]
cosmwasm_std::create_entry_points_with_migration!(contract);
cosmwasm_std::create_entry_points!(contract);
8 changes: 8 additions & 0 deletions contracts/hackatom/Cargo.lock

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

8 changes: 8 additions & 0 deletions contracts/queue/Cargo.lock

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

8 changes: 8 additions & 0 deletions contracts/reflect/Cargo.lock

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

8 changes: 8 additions & 0 deletions contracts/staking/Cargo.lock

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

9 changes: 6 additions & 3 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{
attr, coin, to_binary, BankMsg, Decimal, Deps, DepsMut, Env, HandleResponse, HumanAddr,
InitResponse, MessageInfo, QuerierWrapper, QueryResponse, StakingMsg, StdError, StdResult,
Uint128, WasmMsg,
attr, coin, entry_point, to_binary, BankMsg, Decimal, Deps, DepsMut, Env, HandleResponse,
HumanAddr, InitResponse, MessageInfo, QuerierWrapper, QueryResponse, StakingMsg, StdError,
StdResult, Uint128, WasmMsg,
};

use crate::errors::{StakingError, Unauthorized};
Expand All @@ -16,6 +16,7 @@ use crate::state::{

const FALLBACK_RATIO: Decimal = Decimal::one();

#[entry_point]
pub fn init(deps: DepsMut, _env: Env, info: MessageInfo, msg: InitMsg) -> StdResult<InitResponse> {
// ensure the validator is registered
let vals = deps.querier.query_validators()?;
Expand Down Expand Up @@ -50,6 +51,7 @@ pub fn init(deps: DepsMut, _env: Env, info: MessageInfo, msg: InitMsg) -> StdRes
Ok(InitResponse::default())
}

#[entry_point]
pub fn handle(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -372,6 +374,7 @@ pub fn _bond_all_tokens(
Ok(res)
}

#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::TokenInfo {} => to_binary(&query_token_info(deps)?),
Expand Down
3 changes: 0 additions & 3 deletions contracts/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@ pub mod contract;
mod errors;
pub mod msg;
pub mod state;

#[cfg(target_arch = "wasm32")]
cosmwasm_std::create_entry_points!(contract);
20 changes: 20 additions & 0 deletions packages/derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "cosmwasm-derive"
version = "0.13.0"
authors = ["Simon Warta <[email protected]>"]
edition = "2018"
description = "Standard library for Wasm based smart contracts on Cosmos blockchains"
repository = "https://github.com/CosmWasm/cosmwasm/tree/master/packages/std"
license = "Apache-2.0"
readme = "README.md"

[lib]
proc-macro = true

[features]
default = []

[dependencies]
syn = { version = "1.0", features = ["full"] }

[dev-dependencies]
37 changes: 37 additions & 0 deletions packages/derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[macro_use]
extern crate syn;

use proc_macro::TokenStream;
use std::str::FromStr;

#[proc_macro_attribute]
pub fn entry_point(_attr: TokenStream, mut item: TokenStream) -> TokenStream {
let cloned = item.clone();
let function = parse_macro_input!(cloned as syn::ItemFn);
let name = function.sig.ident.to_string();
// The first argument is `deps`, the rest is region pointers
let args = function.sig.inputs.len() - 1;

// E.g. "ptr0: u32, ptr1: u32, ptr2: u32,"
let typed_ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{}: u32,", acc, i));
// E.g. "ptr0, ptr1, ptr2,"
let ptrs = (0..args).fold(String::new(), |acc, i| format!("{}ptr{},", acc, i));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ptr0,ptr1,ptr2 without spaces. I would add a space after the comma (in both) or change the comments

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well spotted. I did not see this because the code is automatically formatted when using cargo expand.


let new_code = format!(
r##"
#[cfg(target_arch = "wasm32")]
mod __wasm_export_{name} {{ // new module to avoid conflict of function name
#[no_mangle]
extern "C" fn {name}({typed_ptrs}) -> u32 {{
cosmwasm_std::do_{name}(&super::{name}, {ptrs})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at

pub fn do_handle<M, C, E>(
handle_fn: &dyn Fn(DepsMut, Env, MessageInfo, M) -> Result<HandleResponse<C>, E>,
env_ptr: u32,
info_ptr: u32,
msg_ptr: u32,
) -> u32
where
M: DeserializeOwned + JsonSchema,
C: Serialize + Clone + fmt::Debug + PartialEq + JsonSchema,
E: ToString,
{
let res = _do_handle(
handle_fn,
env_ptr as *mut Region,
info_ptr as *mut Region,
msg_ptr as *mut Region,
);
let v = to_vec(&res).unwrap();
release_buffer(v) as u32
}
I wonder if this do_{name} code could be auto-generated.

We have the concrete type info from the function we are decorating, the rest is mostly boilerplate.

(I would do that as a separate PR however)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jupp, much to explore from here.

I also though about Multi-value regions in #602, such that we can have always have one pointer input and one pointer output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also though about Multi-value regions in #602, such that we can have always have one pointer input and one pointer output.

I think that would actually make the auto-generation harder, as you are accepting a variable number of input slices and then mapping them to a fixed number of typed variables.

But yeah, plenty of room to explore here :)

}}
}}
"##,
name = name,
typed_ptrs = typed_ptrs,
ptrs = ptrs
);
let entry = TokenStream::from_str(&new_code).unwrap();
item.extend(entry);
item
}
1 change: 1 addition & 0 deletions packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ backtraces = []

[dependencies]
base64 = "0.13.0"
cosmwasm-derive = { path = "../derive", version = "0.13.0" }
serde-json-wasm = { version = "0.2.1" }
schemars = "0.7"
serde = { version = "1.0.103", default-features = false, features = ["derive", "alloc"] }
Expand Down
5 changes: 5 additions & 0 deletions packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ pub mod testing {
MockStorage, StakingQuerier, MOCK_CONTRACT_ADDR,
};
}

// Re-exports

extern crate cosmwasm_derive;
pub use cosmwasm_derive::entry_point;