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

Simple Mapping Storage Primitive #946

Merged
merged 42 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
61d564d
Add `Mapping` storage collection
HCastano Sep 29, 2021
8ae8047
Implement `insert` and `get` for `Mapping`
HCastano Sep 29, 2021
3a9d97e
Implement `SpreadLayout` for `Mapping`
HCastano Sep 29, 2021
3e29a25
Fix typo
HCastano Sep 29, 2021
d1387f7
Add some basic tests
HCastano Sep 29, 2021
9e48c18
Fix some documentation formatting
HCastano Sep 29, 2021
b7cf4dc
Use `PackedLayout` as trait bound instead of `Encode/Decode`
HCastano Sep 30, 2021
dc87fba
Avoid using low level `ink_env` functions when interacting with storage
HCastano Sep 30, 2021
728462a
RustFmt
HCastano Sep 30, 2021
5b2a8af
Appease Clippy
HCastano Sep 30, 2021
a5d016e
Only use single `PhantomData` field
HCastano Sep 30, 2021
ad524c6
Change `get` API to take reference to `key`
HCastano Sep 30, 2021
e6293a3
Implement `TypeInfo` and `StorageLayout` for `Mapping`
HCastano Sep 30, 2021
b55a24e
Properly gate `TypeInfo` and `StorageLayout` impls behind `std`
HCastano Sep 30, 2021
9c6f853
Replace `HashMap` with `Mapping` in ERC-20 example
HCastano Sep 30, 2021
299ba86
Return `Option` from `Mapping::get`
HCastano Oct 1, 2021
78e69f3
Update ERC-20 to handle `Option` returns
HCastano Oct 1, 2021
00ce6ea
Merge branch 'master' into hc-simple-mapping-primitive
HCastano Oct 1, 2021
a43e0de
Merge branch 'master' into hc-simple-mapping-primitive
HCastano Oct 11, 2021
7c55dbf
Change `get` and `key` to use `Borrow`-ed values
HCastano Oct 11, 2021
84576ed
Add `Debug` and `Default` implementations
HCastano Oct 11, 2021
a5d01e4
Proper spelling
HCastano Oct 11, 2021
14c4347
Change `insert` to only accept borrowed K,V pairs
HCastano Oct 13, 2021
5508fc1
Update ERC-20 example accordingly
HCastano Oct 13, 2021
baa2c32
Make more explicit what each `key` is referring to
HCastano Oct 13, 2021
cede033
Try using a `RefCell` instead of passing `Key` around
HCastano Oct 13, 2021
e5c6ef5
Try using `UnsafeCell` instead
HCastano Oct 13, 2021
0135977
Revert "Try using a `RefCell` instead of passing `Key` around"
HCastano Oct 14, 2021
ad26982
Merge branch 'master' into hc-simple-mapping-primitive
HCastano Oct 19, 2021
644ab71
Clean up some of the documentation
HCastano Oct 19, 2021
2ed985f
Merge branch 'master' into hc-simple-mapping-primitive
HCastano Nov 4, 2021
b954162
Simple Mapping type improvements (#979)
Robbepop Nov 9, 2021
7e55e56
Merge branch 'master' into hc-simple-mapping-primitive
HCastano Nov 9, 2021
507cf5b
Merge branch 'master' into hc-simple-mapping-primitive
HCastano Nov 9, 2021
c23e679
Use new `initialize_contract()` function
HCastano Nov 9, 2021
334a9da
Derive `SpreadAllocate` for `ink(storage)` structs
HCastano Nov 9, 2021
408bf81
Stop manually implementing SpreadAllocate for ERC-20
HCastano Nov 9, 2021
b625a0a
Stop implementing `SpreadAllocate` in the storage codegen
HCastano Nov 9, 2021
2184cd6
Derive `SpreadAllocate` manually for ERC-20
HCastano Nov 9, 2021
1854dcf
RustFmt example
HCastano Nov 9, 2021
c4a951c
Move `Mapping` from `collections` to `lazy`
HCastano Nov 12, 2021
5e5a918
Remove extra `0` in docs
HCastano Nov 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/lang/codegen/src/generator/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl Storage<'_> {
derive(::ink_storage::traits::StorageLayout)
)]
#[derive(::ink_storage::traits::SpreadLayout)]
#[derive(::ink_storage::traits::SpreadAllocate)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

First: You cannot do this since not all #[ink(storage)] structs should implement SpreadAllocate which is why it is separate from SpreadLayout and has not been integrated into the already existing traits.
Second: If this was actually useful (which it isn't) it should reside in another PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For ink! codegen to automatically implement SpreadAllocate optionally we'd need support for Rust trivial trait bounds which currently are unstable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My bad. I thought that the idea behind merging #995 before the Mapping was so we could derive SpreadAllocate for storage types in the codegen instead of manually deriving it

Copy link
Collaborator

Choose a reason for hiding this comment

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

If Rust had trivial trait bounds we actually could do exactly that ... :(

#[cfg_attr(test, derive(::core::fmt::Debug))]
pub struct #ident {
#( #fields ),*
Expand Down
21 changes: 1 addition & 20 deletions examples/erc20/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,9 @@ use ink_lang as ink;

#[ink::contract]
mod erc20 {
use ink_primitives::{
Key,
KeyPtr,
};
use ink_storage::{
collections::mapping::Mapping,
lazy::Lazy,
traits::SpreadAllocate,
};

/// A simple ERC-20 contract.
Expand Down Expand Up @@ -60,25 +55,11 @@ mod erc20 {
/// The ERC-20 result type.
pub type Result<T> = core::result::Result<T, Error>;

impl SpreadAllocate for Erc20 {
fn allocate_spread(ptr: &mut KeyPtr) -> Self {
Self {
total_supply: SpreadAllocate::allocate_spread(ptr),
balances: SpreadAllocate::allocate_spread(ptr),
allowances: SpreadAllocate::allocate_spread(ptr),
}
}
}

impl Erc20 {
/// Creates a new ERC-20 contract with the specified initial supply.
#[ink(constructor)]
pub fn new(initial_supply: Balance) -> Self {
let root_key = Key::from([0x00; 32]);
let mut key_ptr = KeyPtr::from(root_key);
let mut instance = Self::allocate_spread(&mut key_ptr);
instance.new_init(initial_supply);
instance
ink_lang::codegen::initialize_contract(|contract| Self::new_init(contract, initial_supply))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Robbepop using the codegen module here feels wrong, is there a better way to expose this to users?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The actual plan is still that we provide some useful and well designed syntactic sugar so that this line of code won't be necessary but maybe that's not well thought through and maybe writing out this line of code actually isn't so bad. Then we could put this into some other module or into root.

}

/// Default initializes the ERC-20 contract with the specified initial supply.
Expand Down