-
Notifications
You must be signed in to change notification settings - Fork 484
Add set_code_hash function and example
#1203
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
cmichi
merged 5 commits into
use-ink:master
from
willser:will/add-seal_set_code_hash-function-and-example
Apr 22, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da4fcb4
Add a function `ink_env::set_code_hash`
willser 0623907
Add an example of `ink_env::set_code_hash`.
willser b3bf653
Fix spelling mistake
willser 25268c6
Apply suggestions from code review
cmichi 6465f0b
Merge branch 'master' into will/add-seal_set_code_hash-function-and-e…
cmichi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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)] | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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 hidden or 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 hidden or 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)] | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pub struct Incrementer { | ||
| count: u32, | ||
| } | ||
|
|
||
| impl Incrementer { | ||
| /// Creates a new counter smart contract initialized with the given base value. | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// 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)] | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.