-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
set_code_hash
function and example (#1203)
* Add a function `ink_env::set_code_hash` * Add an example of `ink_env::set_code_hash`. * Fix spelling mistake * Apply suggestions from code review Co-authored-by: Michael Müller <mich@elmueller.net>
- Loading branch information
Showing
10 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
[package] | ||
name = "incrementer" | ||
version = "3.0.1" | ||
edition = "2021" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ink_primitives = { version = "3.0.1", path = "../../../crates/primitives", default-features = false } | ||
ink_prelude = { version = "3.0.1", path = "../../../crates/prelude", default-features = false } | ||
ink_metadata = { version = "3.0.1", path = "../../../crates/metadata", default-features = false, features = ["derive"], optional = true } | ||
ink_env = { version = "3.0.1", path = "../../../crates/env", default-features = false } | ||
ink_storage = { version = "3.0.1", path = "../../../crates/storage", default-features = false } | ||
ink_lang = { version = "3.0.1", path = "../../../crates/lang", default-features = false } | ||
|
||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } | ||
|
||
[lib] | ||
name = "incrementer" | ||
path = "lib.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink_primitives/std", | ||
"ink_metadata/std", | ||
"ink_env/std", | ||
"ink_storage/std", | ||
"ink_lang/std", | ||
"scale/std", | ||
"scale-info/std", | ||
] | ||
ink-as-dependency = [] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use ink_lang as ink; | ||
|
||
#[ink::contract] | ||
pub mod incrementer { | ||
|
||
/// This struct contains the smart contract storage. | ||
/// The storage will always be retained, even when `set_code_hash` is called. | ||
#[ink(storage)] | ||
pub struct Incrementer { | ||
count: u32, | ||
} | ||
|
||
impl Incrementer { | ||
/// Creates a new counter smart contract initialized with the given base value. | ||
#[ink(constructor)] | ||
pub fn new(init_value: u32) -> Self { | ||
Self { count: init_value } | ||
} | ||
|
||
/// Creates a new counter smart contract initialized to `0`. | ||
#[ink(constructor)] | ||
pub fn default() -> Self { | ||
Self::new(0) | ||
} | ||
|
||
/// 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 {:?} due to {:?}", | ||
code_hash, err | ||
) | ||
}); | ||
ink_env::debug_println!("Switched code hash to {:?}.", code_hash); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
examples/upgradeable-contracts/set-code-hash/new-increamenter/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "updated-incrementer" | ||
version = "3.0.1" | ||
edition = "2021" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
publish = false | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
ink_primitives = { version = "3.0.1", path = "../../../../crates/primitives", default-features = false } | ||
ink_metadata = { version = "3.0.1", path = "../../../../crates/metadata", default-features = false, features = ["derive"], optional = true } | ||
ink_env = { version = "3.0.1", path = "../../../../crates/env", default-features = false, features = ["ink-debug"] } | ||
ink_storage = { version = "3.0.1", path = "../../../../crates/storage", default-features = false } | ||
ink_lang = { version = "3.0.1", path = "../../../../crates/lang", default-features = false } | ||
|
||
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } | ||
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true } | ||
|
||
[lib] | ||
name = "updated_incrementer" | ||
path = "lib.rs" | ||
crate-type = ["cdylib"] | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"ink_primitives/std", | ||
"ink_metadata/std", | ||
"ink_env/std", | ||
"ink_storage/std", | ||
"ink_lang/std", | ||
"scale/std", | ||
"scale-info/std", | ||
] | ||
ink-as-dependency = [] |
64 changes: 64 additions & 0 deletions
64
examples/upgradeable-contracts/set-code-hash/new-increamenter/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
use ink_lang as ink; | ||
|
||
#[ink::contract] | ||
pub mod incrementer { | ||
|
||
/// This struct contains the smart contract storage. | ||
/// | ||
/// *Note:* We use exactly the same storage struct as in the originally deployed `incrementer`. | ||
#[ink(storage)] | ||
pub struct Incrementer { | ||
count: u32, | ||
} | ||
|
||
impl Incrementer { | ||
/// Creates a new counter smart contract initialized with the given base value. | ||
/// | ||
/// Note that with our upgrade-workflow this constructor will never actually be called, | ||
/// since we merely replace the code used to execute a contract that was already | ||
/// initiated on-chain. | ||
#[ink(constructor)] | ||
pub fn new(init_value: u32) -> Self { | ||
Self { count: init_value } | ||
} | ||
|
||
/// Creates a new counter smart contract initialized to `0`. | ||
#[ink(constructor)] | ||
pub fn default() -> Self { | ||
Self::new(0) | ||
} | ||
|
||
/// Increments the counter value which is stored in the contract's storage. | ||
/// | ||
/// *Note:* We use a different step size here than in the original `incrementer`. | ||
#[ink(message)] | ||
pub fn inc(&mut self) { | ||
self.count += 4; | ||
ink_env::debug_println!("The new count is {}, it was modified using the updated `new_incrementer` 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 {:?} due to {:?}", | ||
code_hash, err | ||
) | ||
}); | ||
ink_env::debug_println!("Switched code hash to {:?}.", code_hash); | ||
} | ||
} | ||
} |