Releases: use-ink/ink
ink! 3.0.0 RC 1
Version 3.0 (2020-10-09)
Be prepared for the ink! 3.0 release notes because the whole version was basically a rewrite of
all the major components that make up ink!. With our experience gained from previous releases
of ink! we were able to detect weak spots of the design and provided ink! with more tools,
more features and more efficiency as ever. Read more below …
Just. Be. Rust. 3.0
In the 3.0 update we further explored the space for ink! to just feel like it was plain Rust.
With this in mind we changed the syntax slightly in order to better map from ink! to the generated
Rust code. So what users see is mostly what will be generated by ink! later.
In this vein #[ink(storage)] and #[ink(event)] structs as well as #[ink(message)] and
#[ink(constructor)] methods now need to be specified with public visibility (pub).
The #[ink(constructor)] syntax also changes and no longer uses a &mut self receiver but
now follows the natural Rust constructors scheme. So it is no longer possible to shoot
yourself in the foot by accidentally forgetting to initialize some important data structures.
Old ink! 2.0:
#[ink(constructor)]
fn new_erc20(&mut self, initial_supply: Balance) {
let caller = self.env().caller();
self.total_supply.set(initial_supply);
self.balances.insert(caller, initial_supply);
}New ink! 3.0:
#[ink(constructor)]
pub fn new_erc20(initial_supply: Balance) -> Self {
let caller = self.env().caller();
let mut balances = ink_storage::HashMap::new();
balances.insert(caller, initial_supply);
Self {
total_supply: initial_supply,
balances,
}
}Also ink! 3.0 no longer requires a mandatory version field in the header of the ink! module attribute.
Syntactically this is all it takes to port your current ink! smart contracts over to ink! 3.0 syntax.
New Storage Module
The storage module has been reworked entirely.
Also it no longer lives in the ink_core crate but instead is defined as its own ink_storage crate.
In a sense it acts as the standard storage library for ink! smart contracts in that it provides all the
necessary tools and data structures to organize and operate the contract's storage intuitively and efficiently.
Lazy
The most fundamental change in how you should think about data structures provided by the new ink_storage
crate is that they are inherently lazy. We will explain what this means below!
The ink_storage crate provides high-level and low-level lazy data structures.
The difference between high-level and low-level lies in the distinction in how these data structures are aware
of the elements that they operate on. For high-level data structures they are fully aware about the elements
they contains, do all the clean-up by themselves so the user can concentrate on the business logic.
For low-level data structures the responsibility about the elements lies in the hands of the contract author.
Also they operate on cells (Option<T>) instead of entities of type T.
But what does that mean exactly?
The new ink_storage::Lazy type is what corresponds the most to the old ink_core::storage::Value type. Both cache their entities and both act lazily on the storage. This means that a read or write operation is only performed when it really needs to in order to satisfy other inputs.
Data types such as Rust primitives i32 or Rust's very own Vec or data structures can also be used to operate on the contract's storage, however, they will load their contents eagerly which is often not what you want.
An example follows with the below contract storage and a message that operates on either of the two fields.
#[ink(storage)]
pub struct TwoValues {
offset: i32,
a: i32,
b: i32,
}
impl TwoValues {
#[ink(message)]
pub fn set(&mut self, which: bool, new_value: i32) {
match which {
true => { self.a = self.offset + new_value; },
false => { self.b = self.offset + new_value; },
}
}
}Whenever we call TwoValues::set always both a and b are loaded despite the fact the we only operate on one of them at a time. This is very costly since storage accesses are in fact database look-ups.
In order to prevent this eager loading of storage contents we can make use of ink_storage::Lazy or other lazy data structures defined in that crate:
#[ink(storage)]
pub struct TwoValues {
offset: i32,
a: ink_storage::Lazy<i32>,
b: ink_storage::Lazy<i32>,
}
impl TwoValues {
#[ink(message)]
pub fn set(&mut self, which: bool, new_value: i32) {
match which {
true => { self.a = offset + new_value; },
false => { self.b = offset + new_value; },
}
}
}Now a and b are only loaded when the contract really needs their values.
Note that offset remained i32 since it is always needed and could spare the minor overhead of the ink_storage::Lazy wrapper.
HashMap
In the follow we explore the differences between the high-level ink_storage::collections::HashMap
and the low-level ink_storage::lazy::LazyHashMap. Both provide very similar functionality in that they map some generic key to some storage entity.
However, their APIs look very different. Whereas the HashMap provides a rich and high-level API that is comparable to that of Rust's very own HashMap, the LazyHashMap provides only a fraction of the API and also operates on Option<T> values types instead of T directly. It is more similar Solidity mappings than to Rust's HashMap.
The fundamental difference of both data structures is that HashMap is aware of the keys that have been stored in it and thus can reconstruct exactly which elements and storage regions apply to it. This enables it to provide iteration and automated deletion as well as efficient way to defragment its underlying storage to free some storage space again. This goes very well in the vein of Substrate's storage rent model where contracts have to pay for the storage they are using.
| Data Structure | level of abstraction | caching | lazy | element type | container |
|---|---|---|---|---|---|
T |
- | yes | no | T |
primitive value |
Lazy<T> |
high-level | yes | yes | T |
single element container |
LazyCell<T> |
low-level | yes | yes | Option<T> |
single element, no container |
Vec<T> |
high-level | yes | yes | T |
Rust vector-like container |
LazyIndexMap<T> |
low-level | yes | yes | Option<T> |
similar to Solidity mapping |
HashMap<K, V> |
high-level | yes | yes | V (key type K) |
Rust map-like container |
LazyHashMap<K, V> |
low-level | yes | yes | Option<V> (key type K) |
similar to Solidity mapping |
There are many more! For more information about the specifics please take a look into the ink_storage crate documentation.
Spread & Packed Modes
Storing or loading complex data structures to and from contract storage can be done in many different ways. You could store all information into a single storage cell or you could try to store all information into as many different cells as possible. Both strategies have pros and cons under different conditions.
For example it might be a very good idea to store all the information under the same cell if all the information is very compact. For example when we are dealing with a byte vector that is expected to never be larger than approx a thousand elements it would probably be more efficient if we store all those thousand bytes in the same cell and especially if we often access many of those (or all) in our contract messages.
On the other hand spreading information across as many cells as possible might be much more efficient if we are dealing with big data structures, a lot of information that is not compact, or when messages that operate on the data always only need a small fraction of the whole data.
An example for this use case is if you have a vector of user accounts where each account stores potentially a lot of information, e.g. a 32-byte hash etc and where our messages only every operate on only a few of those at a time.
The ink_storage crate provides the user full control over the strategy or a mix of these two root strategies through some fundamental abstractions that we are briefly presenting to you.
Default: Spreading Mode
By default ink! spreads information to as many cells as possible. For example if you have the following #[ink(storage)] struct every field will live in its own single storage cell. Note that for c all 32 bytes will share the same cell!
#[ink(storage)]
pub struct Spreaded {
a: i32,
b: ink_storage::Lazy<i32>,
c: [u8; 32],
}Packing Storage
We can alter this behaviour by using the ink_storage::Pack abstraction:
pub struct Spreaded {
a: i32,
b: ink_storage::Lazy<i32>,
c: [u8; 32],
}
#[ink(storage)]
pub struct Packed {
packed: ink_storage::Pack<Spreaded>,
}Now all fields of Spreaded will share the same storage cell. This means whenever one of them is stored to or loaded from the contract storage, all of them are stored or loaded. A user has to choose wisely what mode of operation is more suitable for their contract.
These abstractions can be combined in various ways, yielding full control to the users. For example, in the following only a and b share a common storage cell while c lives in its own:
pub struct Spreaded {
a: i32,
b: ink_storage::Lazy<i32>,
}
#[ink(storage)]
pub struct Packed {
packed: ink_storage::Pack<Spreaded>,
c: [u8; 32],
}Spreading Array Cells
If we prefer to store all bytes of c into their own storage cell we can mak...
ink! v2.1.0
Release Notes
- Add built-in support for cryptographic hashes
- Blake2 with 128-bit and 256-bit
- Sha2 with 256-bit
- Keccak with 256-bit
- Add
ink_core::hashmodule for high-level API to the new built-in hashes - Update
runtime-storageexample contract to demonstrate the new built-in hashes
ink! 2.0.0
Initial release of ink! v2.0.0.
For more information about writing ink! smart contract visit the Parity DevHub.