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

Move storage derives to ink crate #1400

Merged
merged 14 commits into from
Sep 20, 2022
53 changes: 50 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ variables:
# CI_IMAGE is changed to "-:staging" when the CI image gets rebuilt
# read more https://github.com/paritytech/scripts/pull/244
CI_IMAGE: "paritytech/ink-ci-linux:production"
PURELY_STD_CRATES: "ink/codegen storage/traits/codegen metadata engine"
ALSO_WASM_CRATES: "env storage storage/traits storage/traits/derive allocator prelude primitives ink ink/macro ink/ir"
PURELY_STD_CRATES: "ink/codegen metadata engine"
ALSO_WASM_CRATES: "env storage storage/traits allocator prelude primitives ink ink/macro ink/ir"
ALL_CRATES: "${PURELY_STD_CRATES} ${ALSO_WASM_CRATES}"
DELEGATOR_SUBCONTRACTS: "accumulator adder subber"
UPGRADEABLE_CONTRACTS: "forward-calls set-code-hash"
Expand Down Expand Up @@ -282,14 +282,61 @@ docs:
script:
- cargo doc --no-deps --all-features
-p scale-info -p ink_metadata -p ink_env
-p ink_storage -p ink_storage_traits -p ink_storage_codegen -p ink_storage_derive
-p ink_storage -p ink_storage_traits
-p ink_primitives -p ink_prelude
-p ink -p ink_macro -p ink_ir -p ink_codegen
- mv ${CARGO_TARGET_DIR}/doc ./crate-docs
# FIXME: remove me after CI image gets nonroot
- chown -R nonroot:nonroot ./crate-docs


codecov:
stage: workspace
<<: *docker-env
<<: *test-refs
needs:
- job: check-std
artifacts: false
variables:
# For codecov it's sufficient to run the fuzz tests only once.
QUICKCHECK_TESTS: 1
INK_COVERAGE_REPORTING: "true"
CARGO_INCREMENTAL: 0
# Needed because `codecov` requires nightly features to work
# (see `-Z` in the `RUSTFLAGS` below).
RUSTC_BOOTSTRAP: "1"
# Variables partly came from https://github.com/mozilla/grcov/blob/master/README.md
RUSTFLAGS: "-Zprofile -Zmir-opt-level=0 -Ccodegen-units=1
-Clink-dead-code -Copt-level=0 -Coverflow-checks=off"
# The `cargo-taurpalin` coverage reporting tool seems to have better code instrumentation and thus
# produces better results for Rust codebases in general. However, unlike `grcov` it requires
# running docker with `--security-opt seccomp=unconfined` which is why we use `grcov` instead.
before_script:
- *rust-info-script
# RUSTFLAGS are the cause target cache can't be used here
# FIXME: cust-covfix doesn't support the external target dir
# https://github.com/Kogia-sima/rust-covfix/issues/7
- unset "CARGO_TARGET_DIR"
- cargo clean
# make sure there's no stale coverage artifacts
- find . -name "*.profraw" -type f -delete
- find . -name "*.gcda" -type f -delete
script:
# RUSTFLAGS are the cause target cache can't be used here
- cargo build --verbose --all-features --workspace
- cargo test --verbose --all-features --no-fail-fast --workspace
# coverage with branches
- grcov . --binary-path ./target/debug/ --source-dir . --output-type lcov --llvm --branch
--ignore-not-existing --ignore "/*" --ignore "tests/*" --output-path lcov-w-branch.info
- rust-covfix lcov-w-branch.info --output lcov-w-branch-fixed.info
- codecov --token "$CODECOV_P_TOKEN" --file lcov-w-branch-fixed.info --nonZero
# lines coverage
- grcov . --binary-path ./target/debug/ --source-dir . --output-type lcov --llvm
--ignore-not-existing --ignore "/*" --ignore "tests/*" --output-path lcov-lines.info
- rust-covfix lcov-lines.info --output lcov-lines-fixed.info
- codecov --token "$CODECOV_TOKEN" --file lcov-lines-fixed.info --nonZero


#### stage: examples

examples-test:
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ members = [
"crates/env",
"crates/storage",
"crates/storage/traits",
"crates/storage/traits/derive",
]
exclude = [
"examples/",
Expand Down
1 change: 0 additions & 1 deletion crates/ink/ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
name = "ink_ir"

[dependencies]
ink_storage_codegen = { version = "4.0.0-alpha.1", path = "../../storage/traits/codegen" }
quote = "1"
syn = { version = "1.0", features = ["parsing", "full", "visit", "extra-traits"] }
proc-macro2 = "1.0"
Expand Down
36 changes: 33 additions & 3 deletions crates/ink/ir/src/ir/storage_item/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

mod config;

use crate::utils::find_storage_key_salt;
use config::StorageItemConfig;
use ink_storage_codegen::DeriveUtils;
use proc_macro2::TokenStream as TokenStream2;
use quote::{
quote,
ToTokens,
};
use std::collections::HashSet;

/// A checked ink! storage item with its configuration.
pub struct StorageItem {
Expand Down Expand Up @@ -59,7 +60,36 @@ impl StorageItem {

/// Returns all types that were used in the storage declaration.
pub fn all_used_types(&self) -> Vec<syn::Type> {
self.ast.all_types()
let res: Vec<_> = match self.data().clone() {
syn::Data::Struct(st) => {
st.fields.iter().map(|field| field.ty.clone()).collect()
}
syn::Data::Enum(en) => {
en.variants
.iter()
.flat_map(|variant| variant.fields.iter())
.map(|field| field.ty.clone())
.collect()
}
syn::Data::Union(un) => {
un.fields
.named
.iter()
.map(|field| field.ty.clone())
.collect()
}
};
let mut set = HashSet::new();
res.into_iter()
.filter(|ty| {
if !set.contains(ty) {
set.insert(ty.clone());
true
} else {
false
}
})
.collect()
}

/// Returns the config of the storage.
Expand Down Expand Up @@ -94,7 +124,7 @@ impl StorageItem {

/// Returns salt for storage key.
pub fn salt(&self) -> TokenStream2 {
if let Some(param) = self.ast.find_salt() {
if let Some(param) = find_storage_key_salt(&self.ast) {
param.ident.to_token_stream()
} else {
quote! { () }
Expand Down
21 changes: 21 additions & 0 deletions crates/ink/ir/src/ir/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,24 @@ where
name
))
}

/// Finds the salt of a struct, enum or union.
/// The salt is any generic that has bound `StorageKey`.
/// In most cases it is the parent storage key or the auto-generated storage key.
pub fn find_storage_key_salt(input: &syn::DeriveInput) -> Option<syn::TypeParam> {
input.generics.params.iter().find_map(|param| {
if let syn::GenericParam::Type(type_param) = param {
if let Some(syn::TypeParamBound::Trait(trait_bound)) =
type_param.bounds.first()
{
let segments = &trait_bound.path.segments;
if let Some(last) = segments.last() {
if last.ident == "StorageKey" {
return Some(type_param.clone())
}
}
}
}
None
})
}
2 changes: 2 additions & 0 deletions crates/ink/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ ink_primitives = { version = "4.0.0-alpha.1", path = "../../primitives/", defaul

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
syn = "1"
synstructure = "0.12.6"
proc-macro2 = "1"
quote = "1"

[dev-dependencies]
ink_env = { path = "../../env" }
Expand Down
Loading