-
Notifications
You must be signed in to change notification settings - Fork 441
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
bf92a87
Move `set-code-hash` example out of `upgradeable-contracts` folder
HCastano 4f99a64
Clean up code a bit
HCastano 70d81d6
Rename folder to use snake_case
HCastano 342b1cf
More snake casing
HCastano 5d35329
Make it more explicit that upgraded constructor won't be called
HCastano 502831e
Add an E2E test showing upgrade workflow
HCastano 00d5636
Remove `forward-calls` example
HCastano f360060
Remove `upgradeable-contracts` from CI
HCastano d410b06
Move `edition` after `author` field
HCastano 7447a43
Use American spelling, I guess...
HCastano cd1752c
Fix some paths
HCastano d7ce96e
More path fixes
HCastano c2c9c27
Remove comment
HCastano 5536fb7
Move `edition` below `authors` key
HCastano 7f5803b
Appease Clippy
HCastano e321aac
Ensure that initial `inc` is only by `1`
HCastano 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 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,138 @@ | ||
#![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)); | ||
|
||
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; | ||
assert!(matches!(get_res.return_value(), 1)); | ||
|
||
// 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(), 5)); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
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.
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