Skip to content
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

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

Merged
merged 13 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# CHANGELOG

## 0.13.1 (unreleased)

**cosmwasm-std**

- Add the new `#[entry_point]` macro attribute that serves as an alternative
implementation to `cosmwasm_std::create_entry_points!(contract)` and
`cosmwasm_std::create_entry_points_with_migration!(contract)`. Both ways are
supported in the 0.13 series.

## 0.13.0 (2020-01-06)

**all**
Expand Down
9 changes: 9 additions & 0 deletions Cargo.lock

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

73 changes: 73 additions & 0 deletions MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,79 @@ This guide explains what is needed to upgrade contracts when migrating over
major releases of `cosmwasm`. Note that you can also view the
[complete CHANGELOG](./CHANGELOG.md) to understand the differences.

## 0.13 -> 0.14 (unreleased)

- Update CosmWasm dependencies in Cargo.toml (skip the ones you don't use):

```
[dependencies]
cosmwasm-std = "0.14.0"
cosmwasm-storage = "0.14.0"
# ...

[dev-dependencies]
cosmwasm-schema = "0.14.0"
cosmwasm-vm = "0.14.0"
# ...
```

- Use the new entry point system. From `lib.rs` remove

```rust
#[cfg(target_arch = "wasm32")]
cosmwasm_std::create_entry_points!(contract);

// or

#[cfg(target_arch = "wasm32")]
cosmwasm_std::create_entry_points_with_migration!(contract);
```

Then add the macro attribute `#[entry_point]` to your `contract.rs` as
follows:

```rust
use cosmwasm_std::{entry_point, … };

// …

#[entry_point]
pub fn init(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InitMsg,
) -> StdResult<InitResponse> {
// …
}

#[entry_point]
pub fn handle(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: HandleMsg,
) -> StdResult<HandleResponse> {
// …
}

// only if you have migrate
#[entry_point]
pub fn migrate(
deps: DepsMut,
env: Env,
_info: MessageInfo,
msg: MigrateMsg,
) -> StdResult<MigrateResponse> {
// …
}

#[entry_point]
pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<QueryResponse> {
// …
}
```

## 0.12 -> 0.13

- The minimum Rust supported version for 0.13 is 1.47.0.
Expand Down
20 changes: 20 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ docker run --rm -v "$(pwd)":/code \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
cosmwasm/rust-optimizer:0.10.5 ./contracts/staking
```

## Entry points

The development contracts in this folder contain a variety of different entry
points in order to demonstrate and test the flexibility we have.

| Contract | Macro | Has `query` | Has `migrate` |
| -------- | --------------------------------------------- | ----------- | ------------- |
| burner | `#[entry_point]` | yes | yes |
| hackatom | [`create_entry_points_with_migration!`][cepm] | yes | yes |
| queue | mixed<sup>1</sup> | yes | yes |
| reflect | [`create_entry_points!`][cep] | yes | no |
| staking | `#[entry_point]` | yes | no |

<sup>1</sup> Because we can. Don't try this at home.

[cepm]:
https://docs.rs/cosmwasm-std/0.13.0/cosmwasm_std/macro.create_entry_points_with_migration.html
[cep]:
https://docs.rs/cosmwasm-std/0.13.0/cosmwasm_std/macro.create_entry_points.html
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.

8 changes: 6 additions & 2 deletions contracts/burner/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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};

#[entry_point]
pub fn init(
_deps: DepsMut,
_env: Env,
Expand All @@ -16,6 +17,7 @@ pub fn init(
))
}

#[entry_point]
pub fn handle(
_deps: DepsMut,
_env: Env,
Expand All @@ -27,6 +29,7 @@ pub fn handle(
))
}

#[entry_point]
pub fn migrate(
deps: DepsMut,
env: Env,
Expand Down Expand Up @@ -61,6 +64,7 @@ pub fn migrate(
})
}

#[entry_point]
pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<QueryResponse> {
Err(StdError::generic_err(
"You can only use this contract for migrations",
Expand Down
3 changes: 0 additions & 3 deletions contracts/burner/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
pub mod contract;
pub mod msg;

#[cfg(target_arch = "wasm32")]
cosmwasm_std::create_entry_points_with_migration!(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.

6 changes: 3 additions & 3 deletions contracts/queue/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use queue::contract::{
CountResponse, HandleMsg, InitMsg, Item, ListResponse, QueryMsg, SumResponse,
};
use queue::contract::{CountResponse, HandleMsg, Item, ListResponse, QueryMsg, SumResponse};
use queue::msg::{InitMsg, MigrateMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
Expand All @@ -15,6 +14,7 @@ fn main() {

export_schema(&schema_for!(InitMsg), &out_dir);
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema(&schema_for!(MigrateMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(Item), &out_dir);
export_schema(&schema_for!(CountResponse), &out_dir);
Expand Down
5 changes: 5 additions & 0 deletions contracts/queue/schema/migrate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MigrateMsg",
"type": "object"
}
50 changes: 39 additions & 11 deletions contracts/queue/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
from_slice, to_binary, to_vec, Binary, Deps, DepsMut, Env, HandleResponse, InitResponse,
MessageInfo, Order, QueryResponse, StdResult,
entry_point, from_slice, to_binary, to_vec, Binary, Deps, DepsMut, Env, HandleResponse,
InitResponse, MessageInfo, MigrateResponse, Order, QueryResponse, StdResult, Storage,
};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InitMsg {}
use crate::msg::{InitMsg, MigrateMsg};

// we store one entry for each item in the queue
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
Expand Down Expand Up @@ -82,16 +81,21 @@ pub fn handle(
msg: HandleMsg,
) -> StdResult<HandleResponse> {
match msg {
HandleMsg::Enqueue { value } => enqueue(deps, value),
HandleMsg::Dequeue {} => dequeue(deps),
HandleMsg::Enqueue { value } => handle_enqueue(deps, value),
HandleMsg::Dequeue {} => handle_dequeue(deps),
}
}

const FIRST_KEY: u8 = 0;

fn enqueue(deps: DepsMut, value: i32) -> StdResult<HandleResponse> {
fn handle_enqueue(deps: DepsMut, value: i32) -> StdResult<HandleResponse> {
enqueue(deps.storage, value)?;
Ok(HandleResponse::default())
}

fn enqueue(storage: &mut dyn Storage, value: i32) -> StdResult<()> {
// find the last element in the queue and extract key
let last_item = deps.storage.range(None, None, Order::Descending).next();
let last_item = storage.range(None, None, Order::Descending).next();

let new_key = match last_item {
None => FIRST_KEY,
Expand All @@ -101,11 +105,11 @@ fn enqueue(deps: DepsMut, value: i32) -> StdResult<HandleResponse> {
};
let new_value = to_vec(&Item { value })?;

deps.storage.set(&[new_key], &new_value);
Ok(HandleResponse::default())
storage.set(&[new_key], &new_value);
Ok(())
}

fn dequeue(deps: DepsMut) -> StdResult<HandleResponse> {
fn handle_dequeue(deps: DepsMut) -> StdResult<HandleResponse> {
// find the first element in the queue and extract value
let first = deps.storage.range(None, None, Order::Ascending).next();

Expand All @@ -120,6 +124,30 @@ fn dequeue(deps: DepsMut) -> StdResult<HandleResponse> {
}
}

#[entry_point]
pub fn migrate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: MigrateMsg,
) -> StdResult<MigrateResponse> {
// clear all
let keys: Vec<_> = deps
.storage
.range(None, None, Order::Ascending)
.map(|(key, _)| key)
.collect();
for key in keys {
deps.storage.remove(&key);
}

// Write new values
enqueue(deps.storage, 100)?;
enqueue(deps.storage, 101)?;
enqueue(deps.storage, 102)?;
Ok(MigrateResponse::default())
}

pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<QueryResponse> {
match msg {
QueryMsg::Count {} => to_binary(&query_count(deps)?),
Expand Down
1 change: 1 addition & 0 deletions contracts/queue/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod contract;
pub mod msg;

#[cfg(target_arch = "wasm32")]
cosmwasm_std::create_entry_points!(contract);
10 changes: 10 additions & 0 deletions contracts/queue/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![allow(clippy::field_reassign_with_default)] // see https://github.com/CosmWasm/cosmwasm/issues/685

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct InitMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct MigrateMsg {}
Loading