-
Notifications
You must be signed in to change notification settings - Fork 449
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
Clean up upgradeable contract examples #1697
Changes from 15 commits
bf92a87
4f99a64
70d81d6
342b1cf
5d35329
502831e
00d5636
f360060
d410b06
7447a43
cd1752c
d7ce96e
c2c9c27
5536fb7
7f5803b
e321aac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
[package] | ||
name = "incrementer" | ||
version = "4.0.1" | ||
edition = "2021" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
edition = "2021" | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ink = { path = "../../../crates/ink", default-features = false } | ||
ink = { path = "../../crates/ink", default-features = false } | ||
|
||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true } | ||
|
||
[dev-dependencies] | ||
ink_e2e = { path = "../../crates/e2e" } | ||
|
||
[lib] | ||
name = "incrementer" | ||
path = "lib.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[features] | ||
default = ["std"] | ||
|
@@ -26,5 +25,4 @@ std = [ | |
"scale-info/std", | ||
] | ||
ink-as-dependency = [] | ||
|
||
|
||
e2e-tests = [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
//! Demonstrates how to use [`set_code_hash`](https://docs.rs/ink_env/latest/ink_env/fn.set_code_hash.html) | ||
//! to swap out the `code_hash` of an on-chain contract. | ||
//! | ||
//! We will swap the code of our `Incrementer` contract with that of the an `Incrementer` found in | ||
//! the `updated_incrementer` folder. | ||
//! | ||
//! See the included End-to-End tests an example update workflow. | ||
|
||
#[ink::contract] | ||
pub mod incrementer { | ||
|
||
/// Track a counter in storage. | ||
/// | ||
/// # Note | ||
/// | ||
/// Is is important to realize that after the call to `set_code_hash` the contract's storage | ||
/// remains the same. | ||
/// | ||
/// If you change the storage layout in your storage struct you may introduce undefined | ||
/// behavior to your contract! | ||
#[ink(storage)] | ||
#[derive(Default)] | ||
pub struct Incrementer { | ||
count: u32, | ||
} | ||
|
||
impl Incrementer { | ||
/// Creates a new counter smart contract initialized with the given base value. | ||
#[ink(constructor)] | ||
pub fn new() -> Self { | ||
Default::default() | ||
} | ||
|
||
/// Increments the counter value which is stored in the contract's storage. | ||
#[ink(message)] | ||
pub fn inc(&mut self) { | ||
self.count += 1; | ||
ink::env::debug_println!( | ||
"The new count is {}, it was modified using the original contract code.", | ||
self.count | ||
); | ||
} | ||
|
||
/// Returns the counter value which is stored in this contract's storage. | ||
#[ink(message)] | ||
pub fn get(&self) -> u32 { | ||
self.count | ||
} | ||
|
||
/// Modifies the code which is used to execute calls to this contract address (`AccountId`). | ||
/// | ||
/// We use this to upgrade the contract logic. We don't do any authorization here, any caller | ||
/// can execute this method. | ||
/// | ||
/// In a production contract you would do some authorization here! | ||
#[ink(message)] | ||
pub fn set_code(&mut self, code_hash: [u8; 32]) { | ||
ink::env::set_code_hash(&code_hash).unwrap_or_else(|err| { | ||
panic!("Failed to `set_code_hash` to {code_hash:?} due to {err:?}") | ||
}); | ||
ink::env::debug_println!("Switched code hash to {:?}.", code_hash); | ||
} | ||
} | ||
|
||
#[cfg(all(test, feature = "e2e-tests"))] | ||
mod e2e_tests { | ||
use super::*; | ||
use ink_e2e::build_message; | ||
|
||
type E2EResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; | ||
|
||
#[ink_e2e::test(additional_contracts = "./updated_incrementer/Cargo.toml")] | ||
async fn set_code_works(mut client: ink_e2e::Client<C, E>) -> E2EResult<()> { | ||
// Given | ||
let constructor = IncrementerRef::new(); | ||
let contract_acc_id = client | ||
.instantiate("incrementer", &ink_e2e::alice(), constructor, 0, None) | ||
.await | ||
.expect("instantiate failed") | ||
.account_id; | ||
|
||
let get = build_message::<IncrementerRef>(contract_acc_id.clone()) | ||
.call(|incrementer| incrementer.get()); | ||
let get_res = client.call_dry_run(&ink_e2e::alice(), &get, 0, None).await; | ||
assert!(matches!(get_res.return_value(), 0)); | ||
|
||
// When | ||
let new_code_hash = client | ||
.upload("updated_incrementer", &ink_e2e::alice(), None) | ||
.await | ||
.expect("uploading `updated_incrementer` failed") | ||
.code_hash; | ||
|
||
let new_code_hash = new_code_hash.as_ref().try_into().unwrap(); | ||
let set_code = build_message::<IncrementerRef>(contract_acc_id.clone()) | ||
.call(|incrementer| incrementer.set_code(new_code_hash)); | ||
|
||
let _set_code_result = client | ||
.call(&ink_e2e::alice(), set_code, 0, None) | ||
.await | ||
.expect("`set_code` failed"); | ||
|
||
// Then | ||
// Note that our contract's `AccountId` (so `contract_acc_id`) has stayed the same | ||
// between updates! | ||
let inc = build_message::<IncrementerRef>(contract_acc_id.clone()) | ||
.call(|incrementer| incrementer.inc()); | ||
|
||
let _inc_result = client | ||
.call(&ink_e2e::alice(), inc, 0, None) | ||
.await | ||
.expect("`inc` failed"); | ||
|
||
let get = build_message::<IncrementerRef>(contract_acc_id.clone()) | ||
.call(|incrementer| incrementer.get()); | ||
let get_res = client.call_dry_run(&ink_e2e::alice(), &get, 0, None).await; | ||
|
||
// Remember, we updated our incrementer contract to increment by `4`. | ||
assert!(matches!(get_res.return_value(), 4)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An extremely tiny 🔎 🤏 nitpick, we should call |
||
|
||
Ok(()) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
[package] | ||
name = "forward_calls" | ||
name = "updated_incrementer" | ||
version = "4.0.1" | ||
authors = ["Parity Technologies <[email protected]>"] | ||
edition = "2021" | ||
|
@@ -12,9 +12,7 @@ scale = { package = "parity-scale-codec", version = "3", default-features = fals | |
scale-info = { version = "2.3", default-features = false, features = ["derive"], optional = true } | ||
|
||
[lib] | ||
name = "forward_calls" | ||
path = "lib.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[features] | ||
default = ["std"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering whether we should add this to
EnvAccess
so you can doself.env().set_code_hash()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like this: #1698
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya that'd be nice