Skip to content
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

Add migration guide from ink! 4.x to 5.0 #301

Merged
merged 29 commits into from
Jan 30, 2024

Conversation

cmichi
Copy link
Collaborator

@cmichi cmichi commented Jan 24, 2024

Most static image files are already in master.

Comment on lines +273 to +313
### New Data Structure: `StorageVec`

We've added a `Vec`-like data structure, built on top of Mapping.

This allows to retrieve elements from a vector and grow it without
having to load and push all elements.
For `Vec`, the cost of reading or writing a single element grows linearly corresponding
to the number of elements in the vector (its length). Additionally, the maximum capacity
of the whole vector is limited by the size of [ink!'s static buffer](https://github.com/paritytech/ink/blob/master/ARCHITECTURE.md#communication-with-the-pallet)
used during ABI encoding and decoding (default 16 KiB).
`StorageVec` on the other hand allows to access each element individually.

With a `Vec` it's possible to e.g. introduce a security issue in your contract
where an attacker can fill the `Vec`, making it very costly for other users to
access it or write to it.

You can find verbatim documentation on `StorageVec` [here](/5.x/datastructures/storagevec).
The page explains when to use `StorageVec` and when not.
The Rust docs can be found [here](https://docs.rs/ink/5.0.0-rc/ink/storage/struct.StorageVec.html).

### Fallible methods for `Lazy`, `Mapping`, `StorageVec`

In [#1910](https://github.com/paritytech/ink/pull/1910) we added `try_*` methods for
reading and writing `Lazy` and `Mapping` values to and from storage.
The try methods correspond to `Mapping::{insert, get, take}`, `Lazy::{set, get}`.
For `StorageVec::{peek, get, set, pop, push}` we added `try_*` methods in
[#1995](https://github.com/paritytech/ink/pull/1995).

Please see the individual Rust docs for these new methods:

* [`StorageVec`](https://docs.rs/ink/5.0.0-rc/ink/storage/struct.StorageVec.html)
* [`Lazy`](https://docs.rs/ink/5.0.0-rc/ink/storage/struct.Lazy.html)
* [`Mapping`](https://docs.rs/ink/5.0.0-rc/ink/storage/struct.Mapping.html). For `Mapping`, the encoded size of the key is also accounted for.

You should use the `try_*` methods for dynamically sized values, unless you made sure
otherwise they will fit into the static buffer. The [static buffer in ink!](https://github.com/paritytech/ink/blob/master/ARCHITECTURE.md#communication-with-the-pallet)
is 16 kB by default.

We added a lint to `cargo-contract` 4.0 that will detect
potentially unsafe uses of methods for which there are safer alternatives:
[`non_fallible_api`](https://use.ink/5.x/linter/rules/non_fallible_api).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@cmichi cmichi merged commit 1d80186 into master Jan 30, 2024
4 checks passed
@cmichi cmichi deleted the cmichi-add-migration-guide-4.x-to-5.0 branch January 30, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants