Skip to content

Combine storage traits and derives into single crate #1389

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

Merged
merged 25 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
10 changes: 5 additions & 5 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: "lang/codegen metadata engine"
ALSO_WASM_CRATES: "env storage storage/derive allocator prelude primitives lang lang/macro lang/ir"
PURELY_STD_CRATES: "lang/codegen storage/traits/codegen metadata engine"
ALSO_WASM_CRATES: "env storage storage/traits storage/traits/derive allocator prelude primitives lang lang/macro lang/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 @@ -286,9 +286,9 @@ docs:
- ./crate-docs/
script:
- cargo doc --no-deps --all-features
-p scale-info -p ink_metadata
-p ink_env -p ink_storage -p ink_storage_derive
-p ink_primitives -p ink_prelude -p ink_primitives_derive
-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_primitives -p ink_prelude
-p ink_lang -p ink_lang_macro -p ink_lang_ir -p ink_lang_codegen
- mv ${CARGO_TARGET_DIR}/doc ./crate-docs
# FIXME: remove me after CI image gets nonroot
Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ members = [
"crates/engine",
"crates/env",
"crates/storage",
"crates/storage/derive",
"crates/storage/traits",
"crates/storage/traits/derive",
]
exclude = [
"examples/",
Expand Down
1 change: 1 addition & 0 deletions crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ categories = ["no-std", "embedded"]
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]

[dependencies]
ink_primitives = { path = "../../crates/primitives", default-features = false }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "full"] }
derive_more = { version = "0.99", default-features = false, features = ["from", "display"] }

Expand Down
10 changes: 6 additions & 4 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ categories = ["no-std", "embedded"]
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]

[dependencies]
ink_metadata = { version = "4.0.0-alpha.1", path = "../metadata/", default-features = false, features = ["derive"], optional = true }
ink_allocator = { version = "4.0.0-alpha.1", path = "../allocator/", default-features = false }
ink_primitives = { version = "4.0.0-alpha.1", path = "../primitives/", default-features = false }
ink_prelude = { version = "4.0.0-alpha.1", path = "../prelude/", default-features = false }
ink_metadata = { version = "4.0.0-alpha.1", path = "../metadata", default-features = false, features = ["derive"], optional = true }
ink_allocator = { version = "4.0.0-alpha.1", path = "../allocator", default-features = false }
ink_storage_traits = { version = "4.0.0-alpha.1", path = "../storage/traits", default-features = false }
ink_prelude = { version = "4.0.0-alpha.1", path = "../prelude", default-features = false }
ink_primitives = { version = "4.0.0-alpha.1", path = "../primitives", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive", "full"] }
derive_more = { version = "0.99", default-features = false, features = ["from", "display"] }
Expand Down Expand Up @@ -56,6 +57,7 @@ std = [
"ink_allocator/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage_traits/std",
"ink_engine/std",
"scale/std",
"scale-info/std",
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{
Environment,
Result,
};
use ink_primitives::traits::Storable;
use ink_storage_traits::Storable;

/// Returns the address of the caller of the executed contract.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
Environment,
Result,
};
use ink_primitives::traits::Storable;
use ink_storage_traits::Storable;

/// The flags to indicate further information about the end of a contract execution.
#[derive(Default)]
Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/call/call_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ use crate::{
ExecutionInput,
},
types::Gas,
Clear,
Environment,
Error,
};
use core::marker::PhantomData;
use ink_primitives::Clear;
use num_traits::Zero;

/// The final parameters to the cross-contract call.
Expand Down Expand Up @@ -222,9 +222,9 @@ where
/// # use ::ink_env::{
/// # Environment,
/// # DefaultEnvironment,
/// # Clear,
/// # call::{build_call, Selector, ExecutionInput, utils::ReturnType, DelegateCall},
/// # };
/// # use ink_primitives::Clear;
/// # type AccountId = <DefaultEnvironment as Environment>::AccountId;
/// let my_return_value: i32 = build_call::<DefaultEnvironment>()
/// .call_type(DelegateCall::new()
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use ink_engine::{
ext,
ext::Engine,
};
use ink_primitives::traits::Storable;
use ink_storage_traits::Storable;

/// The capacity of the static buffer.
/// This is the same size as the ink! on-chain environment. We chose to use the same size
Expand Down
4 changes: 2 additions & 2 deletions crates/env/src/engine/on_chain/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ impl<'a> ScopedBuffer<'a> {
#[inline(always)]
pub fn take_storable_encoded<T>(&mut self, value: &T) -> &'a mut [u8]
where
T: ink_primitives::traits::Storable,
T: ink_storage_traits::Storable,
{
debug_assert_eq!(self.offset, 0);
let buffer = core::mem::take(&mut self.buffer);
let mut encode_scope = EncodeScope::from(buffer);
ink_primitives::traits::Storable::encode(value, &mut encode_scope);
ink_storage_traits::Storable::encode(value, &mut encode_scope);
let encode_len = encode_scope.len();
let _ = core::mem::replace(&mut self.buffer, encode_scope.into_buffer());
self.take(encode_len)
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use crate::{
ReturnFlags,
TypedEnvBackend,
};
use ink_primitives::traits::Storable;
use ink_storage_traits::Storable;

impl CryptoHash for Blake2x128 {
fn hash(input: &[u8], output: &mut <Self as HashOutput>::Type) {
Expand Down
4 changes: 1 addition & 3 deletions crates/env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,13 @@ pub use self::{
},
topics::Topics,
types::{
AccountId,
Clear,
DefaultEnvironment,
Environment,
FromLittleEndian,
Hash,
NoChainExtension,
},
};
use ink_primitives::Clear;

cfg_if::cfg_if! {
if #[cfg(any(feature = "ink-debug", feature = "std"))] {
Expand Down
144 changes: 4 additions & 140 deletions crates/env/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@
//! the trait bounds on the `Environment` trait types.

use super::arithmetic::AtLeast32BitUnsigned;
use core::array::TryFromSliceError;
use derive_more::From;
use scale::{
Decode,
Encode,
use ink_primitives::{
AccountId,
Clear,
Hash,
};
#[cfg(feature = "std")]
use scale_info::TypeInfo;
Expand Down Expand Up @@ -194,138 +193,3 @@ pub type Gas = u64;

/// The default block number type.
pub type BlockNumber = u32;

/// The default environment `AccountId` type.
///
/// # Note
///
/// This is a mirror of the `AccountId` type used in the default configuration
/// of PALLET contracts.
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Encode,
Decode,
From,
Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub struct AccountId([u8; 32]);

impl AsRef<[u8; 32]> for AccountId {
#[inline]
fn as_ref(&self) -> &[u8; 32] {
&self.0
}
}

impl AsMut<[u8; 32]> for AccountId {
#[inline]
fn as_mut(&mut self) -> &mut [u8; 32] {
&mut self.0
}
}

impl AsRef<[u8]> for AccountId {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}

impl AsMut<[u8]> for AccountId {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}

impl<'a> TryFrom<&'a [u8]> for AccountId {
type Error = TryFromSliceError;

fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
let address = <[u8; 32]>::try_from(bytes)?;
Ok(Self(address))
}
}

/// The default environment `Hash` type.
///
/// # Note
///
/// This is a mirror of the `Hash` type used in the default configuration
/// of PALLET contracts.
#[derive(
Debug,
Copy,
Clone,
PartialEq,
Eq,
Ord,
PartialOrd,
Hash,
Encode,
Decode,
From,
Default,
)]
#[cfg_attr(feature = "std", derive(TypeInfo))]
pub struct Hash([u8; 32]);

impl<'a> TryFrom<&'a [u8]> for Hash {
type Error = TryFromSliceError;

fn try_from(bytes: &'a [u8]) -> Result<Self, TryFromSliceError> {
let address = <[u8; 32]>::try_from(bytes)?;
Ok(Self(address))
}
}

impl AsRef<[u8]> for Hash {
fn as_ref(&self) -> &[u8] {
&self.0[..]
}
}

impl AsMut<[u8]> for Hash {
fn as_mut(&mut self) -> &mut [u8] {
&mut self.0[..]
}
}

/// The equivalent of `Zero` for hashes.
///
/// A hash that consists only of 0 bits is clear.
pub trait Clear {
/// Returns `true` if the hash is clear.
fn is_clear(&self) -> bool;

/// Returns a clear hash.
fn clear() -> Self;
}

impl Clear for [u8; 32] {
fn is_clear(&self) -> bool {
self.as_ref().iter().all(|&byte| byte == 0x00)
}

fn clear() -> Self {
[0x00; 32]
}
}

impl Clear for Hash {
fn is_clear(&self) -> bool {
<[u8; 32] as Clear>::is_clear(&self.0)
}

fn clear() -> Self {
Self(<[u8; 32] as Clear>::clear())
}
}
2 changes: 1 addition & 1 deletion crates/lang/codegen/src/generator/storage_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl GenerateCode for StorageItem<'_> {
#[derive(
::ink_storage::traits::StorableHint,
::ink_storage::traits::StorageKey,
::ink_primitives::traits::Storable,
::ink_storage::traits::Storable,
)]
};
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
name = "ink_lang_ir"

[dependencies]
ink_storage_codegen = { version = "4.0.0-alpha.1", path = "../../storage/codegen" }
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
4 changes: 2 additions & 2 deletions crates/lang/macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ pub fn trait_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
/// StorageKey,
/// StorableHint,
/// };
/// use ink_primitives::traits::Storable;
/// use ink_storage::traits::Storable;
///
/// // Deriving `scale::Decode` and `scale::Encode` also derives blanket implementation of all
/// // required traits to be storable.
Expand Down Expand Up @@ -796,7 +796,7 @@ pub fn trait_definition(attr: TokenStream, item: TokenStream) -> TokenStream {
/// StorableHint,
/// StorageKey,
/// };
/// use ink_primitives::traits::Storable;
/// use ink_storage::traits::Storable;
///
/// #[ink_lang::storage_item(derive = false)]
/// #[derive(StorableHint, Storable, StorageKey)]
Expand Down
6 changes: 4 additions & 2 deletions crates/lang/src/codegen/dispatch/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ use ink_env::{
Environment,
ReturnFlags,
};
use ink_primitives::traits::Storable;
use ink_storage::traits::StorageKey;
use ink_storage::traits::{
Storable,
StorageKey,
};
use scale::Encode;

/// Returns `Ok` if the caller did not transfer additional value to the callee.
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,9 @@ where
/// # pub mod my_contract {
/// use ink_env::{
/// DefaultEnvironment,
/// Clear,
/// call::{build_call, DelegateCall, Selector, ExecutionInput, utils::ReturnType}
/// };
/// use ink_primitives::Clear;
///
/// #
/// # #[ink(storage)]
Expand Down
Loading