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

Update to new scale-info and codec releases #663

Merged
merged 8 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ink_allocator = { version = "3.0.0-rc2", path = "../allocator/", default-feature
ink_primitives = { version = "3.0.0-rc2", path = "../primitives/", default-features = false }
ink_prelude = { version = "3.0.0-rc2", path = "../prelude/", default-features = false }

scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive", "full"] }
scale = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive", "full"] }
derive_more = { version = "0.99", default-features = false, features = ["from", "display"] }
num-traits = { version = "0.2", default-features = false, features = ["i128"] }
cfg-if = "1.0"
Expand All @@ -38,7 +38,7 @@ blake2 = { version = "0.9", optional = true }
# Sadly couldn't be marked as dev-dependency.
# Never use this crate outside of the off-chain environment!
rand = { version = "0.8", default-features = false, features = ["alloc"], optional = true }
scale-info = { version = "0.4", default-features = false, features = ["derive"], optional = true }
scale-info = { version = "0.5", default-features = false, features = ["derive"], optional = true }

[features]
default = ["std"]
Expand Down
8 changes: 4 additions & 4 deletions crates/env/src/call/execution_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ where
}

#[inline]
fn encode_to<O: scale::Output>(&self, output: &mut O) {
fn encode_to<O: scale::Output + ?Sized>(&self, output: &mut O) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice addition!

<T as scale::Encode>::encode_to(&self.arg, output)
}
}
Expand All @@ -164,7 +164,7 @@ impl scale::Encode for EmptyArgumentList {
}

#[inline]
fn encode_to<O: scale::Output>(&self, _output: &mut O) {}
fn encode_to<O: scale::Output + ?Sized>(&self, _output: &mut O) {}
}

impl<'a, Head, Rest> scale::Encode for ArgumentList<Argument<Head>, Rest>
Expand All @@ -178,7 +178,7 @@ where
}

#[inline]
fn encode_to<O: scale::Output>(&self, output: &mut O) {
fn encode_to<O: scale::Output + ?Sized>(&self, output: &mut O) {
// We reverse the order of encoding because we build up the list of
// arguments in reverse order, too. This way we encode the arguments
// in the same order in which they have been pushed to the argument list
Expand All @@ -198,7 +198,7 @@ where
}

#[inline]
fn encode_to<O: scale::Output>(&self, output: &mut O) {
fn encode_to<O: scale::Output + ?Sized>(&self, output: &mut O) {
scale::Encode::encode_to(&self.selector, output);
scale::Encode::encode_to(&self.args, output);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/off_chain/call_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl scale::Encode for CallData {
self.bytes.len()
}

fn encode_to<T: scale::Output>(&self, dest: &mut T) {
fn encode_to<T: scale::Output + ?Sized>(&self, dest: &mut T) {
dest.write(self.bytes.as_slice());
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/topics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
}

#[inline]
fn encode_to<T: scale::Output>(&self, dest: &mut T) {
fn encode_to<T: scale::Output + ?Sized>(&self, dest: &mut T) {
self.prefix.encode_to(dest);
self.value.encode_to(dest);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/lang/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ink_metadata = { version = "3.0.0-rc2", path = "../metadata", default-features =
ink_prelude = { version = "3.0.0-rc2", path = "../prelude/", default-features = false }
ink_lang_macro = { version = "3.0.0-rc2", path = "macro", default-features = false }

scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive", "full"] }
scale = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive", "full"] }
derive_more = { version = "0.99", default-features = false, features = ["from"] }
static_assertions = "1.1"

Expand Down
4 changes: 2 additions & 2 deletions crates/lang/macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ ink_lang_ir = { version = "3.0.0-rc2", path = "../ir", default-features = false
ink_lang_codegen = { version = "3.0.0-rc2", path = "../codegen", default-features = false }
ink_primitives = { version = "3.0.0-rc2", path = "../../primitives/", default-features = false }

scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive"] }
scale = { package = "parity-scale-codec", version = "2.0", default-features = false, features = ["derive"] }
syn = "1"
proc-macro2 = "1"

Expand All @@ -30,7 +30,7 @@ ink_storage = { version = "3.0.0-rc2", path = "../../storage/" }
ink_lang = { version = "3.0.0-rc2", path = ".." }

trybuild = "1.0.24"
scale-info = { version = "0.4", default-features = false, features = ["derive"] }
scale-info = { version = "0.5", default-features = false, features = ["derive"] }

[lib]
name = "ink_lang_macro"
Expand Down
2 changes: 1 addition & 1 deletion crates/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ink_primitives = { version = "3.0.0-rc2", path = "../primitives/", default-featu
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
impl-serde = "0.3.1"
derive_more = { version = "0.99", default-features = false, features = ["from"] }
scale-info = { version = "0.4", default-features = false, features = ["derive"] }
scale-info = { version = "0.5", default-features = false, features = ["derive", "serde"] }

[dev-dependencies]
pretty_assertions = "0.6.1"
Expand Down
72 changes: 38 additions & 34 deletions crates/metadata/src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ use ink_prelude::collections::btree_map::BTreeMap;
use ink_primitives::Key;
use scale_info::{
form::{
CompactForm,
Form,
MetaForm,
PortableForm,
},
meta_type,
IntoCompact,
IntoPortable,
Registry,
TypeInfo,
};
Expand Down Expand Up @@ -146,33 +146,37 @@ impl CellLayout {
}
}

impl IntoCompact for CellLayout {
type Output = CellLayout<CompactForm>;
impl IntoPortable for CellLayout {
type Output = CellLayout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
CellLayout {
key: self.key,
ty: registry.register_type(&self.ty),
}
}
}

impl IntoCompact for Layout {
type Output = Layout<CompactForm>;
impl IntoPortable for Layout {
type Output = Layout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
match self {
Layout::Cell(encoded_cell) => {
Layout::Cell(encoded_cell.into_compact(registry))
Layout::Cell(encoded_cell.into_portable(registry))
}
Layout::Hash(hash_layout) => {
Layout::Hash(hash_layout.into_portable(registry))
}
Layout::Hash(hash_layout) => Layout::Hash(hash_layout.into_compact(registry)),
Layout::Array(array_layout) => {
Layout::Array(array_layout.into_compact(registry))
Layout::Array(array_layout.into_portable(registry))
}
Layout::Struct(struct_layout) => {
Layout::Struct(struct_layout.into_compact(registry))
Layout::Struct(struct_layout.into_portable(registry))
}
Layout::Enum(enum_layout) => {
Layout::Enum(enum_layout.into_portable(registry))
}
Layout::Enum(enum_layout) => Layout::Enum(enum_layout.into_compact(registry)),
}
}
}
Expand Down Expand Up @@ -209,14 +213,14 @@ pub struct HashLayout<F: Form = MetaForm> {
layout: Box<Layout<F>>,
}

impl IntoCompact for HashLayout {
type Output = HashLayout<CompactForm>;
impl IntoPortable for HashLayout {
type Output = HashLayout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
HashLayout {
offset: self.offset,
strategy: self.strategy,
layout: Box::new(self.layout.into_compact(registry)),
layout: Box::new(self.layout.into_portable(registry)),
}
}
}
Expand Down Expand Up @@ -380,15 +384,15 @@ where
}
}

impl IntoCompact for ArrayLayout {
type Output = ArrayLayout<CompactForm>;
impl IntoPortable for ArrayLayout {
type Output = ArrayLayout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
ArrayLayout {
offset: self.offset,
len: self.len,
cells_per_elem: self.cells_per_elem,
layout: Box::new(self.layout.into_compact(registry)),
layout: Box::new(self.layout.into_portable(registry)),
}
}
}
Expand Down Expand Up @@ -426,15 +430,15 @@ where
}
}

impl IntoCompact for StructLayout {
type Output = StructLayout<CompactForm>;
impl IntoPortable for StructLayout {
type Output = StructLayout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
StructLayout {
fields: self
.fields
.into_iter()
.map(|field| field.into_compact(registry))
.map(|field| field.into_portable(registry))
.collect::<Vec<_>>(),
}
}
Expand Down Expand Up @@ -492,13 +496,13 @@ where
}
}

impl IntoCompact for FieldLayout {
type Output = FieldLayout<CompactForm>;
impl IntoPortable for FieldLayout {
type Output = FieldLayout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
FieldLayout {
name: self.name.map(|name| name.into_compact(registry)),
layout: self.layout.into_compact(registry),
name: self.name.map(|name| name.into_portable(registry)),
layout: self.layout.into_portable(registry),
}
}
}
Expand Down Expand Up @@ -563,17 +567,17 @@ where
}
}

impl IntoCompact for EnumLayout {
type Output = EnumLayout<CompactForm>;
impl IntoPortable for EnumLayout {
type Output = EnumLayout<PortableForm>;

fn into_compact(self, registry: &mut Registry) -> Self::Output {
fn into_portable(self, registry: &mut Registry) -> Self::Output {
EnumLayout {
dispatch_key: self.dispatch_key,
variants: self
.variants
.into_iter()
.map(|(discriminant, layout)| {
(discriminant, layout.into_compact(registry))
(discriminant, layout.into_portable(registry))
})
.collect(),
}
Expand Down
10 changes: 5 additions & 5 deletions crates/metadata/src/layout/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn named_fields_struct_layout(key_ptr: &mut KeyPtr) -> Layout {
fn named_fields_work() {
let layout = named_fields_struct_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
let mut registry = Registry::new();
let compacted = layout.into_compact(&mut registry);
let compacted = layout.into_portable(&mut registry);
let json = serde_json::to_value(&compacted).unwrap();
let expected = serde_json::json! {
{
Expand Down Expand Up @@ -101,7 +101,7 @@ fn tuple_struct_layout(key_ptr: &mut KeyPtr) -> Layout {
fn tuple_struct_work() {
let layout = tuple_struct_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
let mut registry = Registry::new();
let compacted = layout.into_compact(&mut registry);
let compacted = layout.into_portable(&mut registry);
let json = serde_json::to_value(&compacted).unwrap();
let expected = serde_json::json! {
{
Expand Down Expand Up @@ -156,7 +156,7 @@ fn clike_enum_layout(key_ptr: &mut KeyPtr) -> Layout {
fn clike_enum_work() {
let layout = clike_enum_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
let mut registry = Registry::new();
let compacted = layout.into_compact(&mut registry);
let compacted = layout.into_portable(&mut registry);
let json = serde_json::to_value(&compacted).unwrap();
let expected = serde_json::json! {
{
Expand Down Expand Up @@ -237,7 +237,7 @@ fn mixed_enum_layout(key_ptr: &mut KeyPtr) -> Layout {
fn mixed_enum_work() {
let layout = mixed_enum_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
let mut registry = Registry::new();
let compacted = layout.into_compact(&mut registry);
let compacted = layout.into_portable(&mut registry);
let json = serde_json::to_value(&compacted).unwrap();
let expected = serde_json::json! {
{
Expand Down Expand Up @@ -336,7 +336,7 @@ fn unbounded_hashing_layout(key_ptr: &mut KeyPtr) -> Layout {
fn unbounded_layout_works() {
let layout = unbounded_hashing_layout(&mut KeyPtr::from(Key::from([0x00; 32])));
let mut registry = Registry::new();
let compacted = layout.into_compact(&mut registry);
let compacted = layout.into_portable(&mut registry);
let json = serde_json::to_value(&compacted).unwrap();
let expected = serde_json::json! {
{
Expand Down
34 changes: 21 additions & 13 deletions crates/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,30 @@ use impl_serde::serialize as serde_hex;

#[cfg(feature = "derive")]
use scale_info::{
form::CompactForm,
IntoCompact as _,
form::{
FormString,
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is a FormString bringing to the table?

PortableForm,
},
IntoPortable as _,
PortableRegistry,
Registry,
RegistryReadOnly,
};
use serde::{
de::DeserializeOwned,
Deserialize,
Serialize,
};

/// An entire ink! project for metadata file generation purposes.
#[derive(Debug, Serialize, Deserialize)]
pub struct InkProject {
#[serde(bound(deserialize = "S: DeserializeOwned"))]
pub struct InkProject<S: FormString = &'static str> {
#[serde(flatten)]
registry: RegistryReadOnly,
registry: PortableRegistry<S>,
#[serde(rename = "storage")]
/// The layout of the storage data structure
layout: layout::Layout<CompactForm>,
spec: ContractSpec<CompactForm>,
layout: layout::Layout<PortableForm<S>>,
spec: ContractSpec<PortableForm<S>>,
}

impl InkProject {
Expand All @@ -77,26 +82,29 @@ impl InkProject {
let mut registry = Registry::new();

Self {
layout: layout.into().into_compact(&mut registry),
spec: spec.into().into_compact(&mut registry),
layout: layout.into().into_portable(&mut registry),
spec: spec.into().into_portable(&mut registry),
registry: registry.into(),
}
}
}

impl InkProject {
impl<S> InkProject<S>
where
S: FormString,
{
/// Returns a read-only registry of types in the contract.
pub fn registry(&self) -> &RegistryReadOnly {
pub fn registry(&self) -> &PortableRegistry<S> {
&self.registry
}

/// Returns the storage layout of the contract.
pub fn layout(&self) -> &layout::Layout<CompactForm> {
pub fn layout(&self) -> &layout::Layout<PortableForm<S>> {
&self.layout
}

/// Returns the specification of the contract.
pub fn spec(&self) -> &ContractSpec<CompactForm> {
pub fn spec(&self) -> &ContractSpec<PortableForm<S>> {
&self.spec
}
}
Loading