Skip to content

Commit 17ec5fa

Browse files
SkymanOnecmichiascjones
authored
Document storage compatibility (#1384)
* document storage compatibility * more clarity on storage restriction * fix spelling * ignore code snippets in docs * formatting * Apply suggestions from Michi's code review Co-authored-by: Michael Müller <[email protected]> Co-authored-by: Andrew Jones <[email protected]> * Apply suggestions from code review Co-authored-by: Michael Müller <[email protected]> Co-authored-by: Andrew Jones <[email protected]>
1 parent a314b34 commit 17ec5fa

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

crates/env/src/api.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,74 @@ where
630630
/// # Errors
631631
///
632632
/// `ReturnCode::CodeNotFound` in case the supplied `code_hash` cannot be found on-chain.
633+
///
634+
/// # Storage Compatibility
635+
///
636+
/// When the smart contract code is modified,
637+
/// it is important to observe an additional virtual restriction
638+
/// that is imposed on this procedure:
639+
/// you should not change the order in which the contract state variables
640+
/// are declared, nor their type.
641+
///
642+
/// Violating the restriction will not prevent a successful compilation,
643+
/// but will result in the mix-up of values or failure to read the storage correctly.
644+
/// This can result in severe errors in the application utilizing the contract.
645+
///
646+
/// If the storage of your contract looks like this:
647+
///
648+
/// ```ignore
649+
/// #[ink(storage)]
650+
/// pub struct YourContract {
651+
/// x: u32,
652+
/// y: bool,
653+
/// }
654+
/// ```
655+
///
656+
/// The procedures listed below will make it invalid:
657+
///
658+
/// Changing the order of variables:
659+
///
660+
/// ```ignore
661+
/// #[ink(storage)]
662+
/// pub struct YourContract {
663+
/// y: bool,
664+
/// x: u32,
665+
/// }
666+
/// ```
667+
///
668+
/// Removing existing variable:
669+
///
670+
/// ```ignore
671+
/// #[ink(storage)]
672+
/// pub struct YourContract {
673+
/// x: u32,
674+
/// }
675+
/// ```
676+
///
677+
/// Changing type of a variable:
678+
///
679+
/// ```ignore
680+
/// #[ink(storage)]
681+
/// pub struct YourContract {
682+
/// x: u64,
683+
/// y: bool,
684+
/// }
685+
/// ```
686+
///
687+
/// Introducing a new variable before any of the existing ones:
688+
///
689+
/// ```ignore
690+
/// #[ink(storage)]
691+
/// pub struct YourContract {
692+
/// z: Vec<u32>,
693+
/// x: u32,
694+
/// y: bool,
695+
/// }
696+
/// ```
697+
///
698+
/// Please refer to the
699+
/// [Open Zeppelin docs](https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable#modifying-your-contracts)
700+
/// for more details and examples.
633701
pub fn set_code_hash(code_hash: &[u8; 32]) -> Result<()> {
634702
<EnvInstance as OnInstance>::on_instance(|instance| instance.set_code_hash(code_hash))
635703
}

examples/upgradeable-contracts/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,10 @@ more information on proxy patterns.
2020
This effectively replaces the code which is executed for the contract address.
2121
* The other contract (`updated-incrementer`) needs to be deployed on-chain.
2222
* State is stored in the storage of the originally instantiated contract (`incrementer`).
23+
24+
## Storage Compatibility
25+
26+
When working on the contract upgradeability, it is important to observe additional rules that are imposed on
27+
the modifications of storage:
28+
29+
Please refer to the section of [Storage Compatibility](https://paritytech.github.io/ink/ink_env/fn.set_code_hash.html) in the ink! crate documentation.

0 commit comments

Comments
 (0)