-
Notifications
You must be signed in to change notification settings - Fork 432
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
Fix trait message return type metadata #1531
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a1bfa4b
Add failing message return type metadata test
ascjones 0f772b5
Set wrapped return type for trait message
ascjones a690440
Combine existing contractor metadata test
ascjones 60e856d
Common extract result function
ascjones 3c61605
Refactor fallible constructor test to use helpers
ascjones c3b5e6c
Remove dummy test
ascjones fcd5e5c
Fix clippy warning
ascjones File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// Copyright 2018-2022 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
#[ink::contract] | ||
mod contract { | ||
#[ink::trait_definition] | ||
pub trait TraitDefinition { | ||
#[ink(message)] | ||
fn get_value(&self) -> u32; | ||
} | ||
|
||
#[ink(storage)] | ||
pub struct Contract {} | ||
|
||
impl Contract { | ||
#[ink(constructor)] | ||
pub fn try_new() -> Result<Self, u8> { | ||
Err(1) | ||
} | ||
} | ||
|
||
impl TraitDefinition for Contract { | ||
#[ink(message)] | ||
fn get_value(&self) -> u32 { | ||
42 | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use scale_info::{ | ||
form::PortableForm, | ||
Type, | ||
TypeDef, | ||
TypeDefPrimitive, | ||
TypeDefTuple, | ||
}; | ||
|
||
fn generate_metadata() -> ink_metadata::InkProject { | ||
extern "Rust" { | ||
fn __ink_generate_metadata() -> ink_metadata::InkProject; | ||
} | ||
|
||
unsafe { __ink_generate_metadata() } | ||
} | ||
|
||
/// Extract the type defs of the `Ok` and `Error` variants of a `Result` type. | ||
/// | ||
/// Panics if the type def is not a valid result | ||
fn extract_result<'a>( | ||
metadata: &'a ink_metadata::InkProject, | ||
ty: &'a Type<PortableForm>, | ||
) -> (&'a Type<PortableForm>, &'a Type<PortableForm>) { | ||
assert_eq!( | ||
"Result", | ||
format!("{}", ty.path()), | ||
"Message return type should be a Result" | ||
); | ||
match ty.type_def() { | ||
TypeDef::Variant(variant) => { | ||
assert_eq!(2, variant.variants().len()); | ||
let ok_variant = &variant.variants()[0]; | ||
let ok_field = &ok_variant.fields()[0]; | ||
let ok_ty = resolve_type(metadata, ok_field.ty().id()); | ||
assert_eq!("Ok", ok_variant.name()); | ||
|
||
let err_variant = &variant.variants()[1]; | ||
let err_field = &err_variant.fields()[0]; | ||
let err_ty = resolve_type(metadata, err_field.ty().id()); | ||
assert_eq!("Err", err_variant.name()); | ||
|
||
(ok_ty, err_ty) | ||
} | ||
td => panic!("Expected a Variant type def enum, got {:?}", td), | ||
} | ||
} | ||
|
||
/// Resolve a type with the given id from the type registry | ||
fn resolve_type( | ||
metadata: &ink_metadata::InkProject, | ||
type_id: u32, | ||
) -> &Type<PortableForm> { | ||
metadata | ||
.registry() | ||
.resolve(type_id) | ||
.unwrap_or_else(|| panic!("No type found in registry with id {}", type_id)) | ||
} | ||
|
||
#[test] | ||
fn trait_message_metadata_return_value_is_result() { | ||
let metadata = generate_metadata(); | ||
|
||
let message = metadata.spec().messages().iter().next().unwrap(); | ||
assert_eq!("TraitDefinition::get_value", message.label()); | ||
|
||
let type_spec = message.return_type().opt_type().unwrap(); | ||
let ty = resolve_type(&metadata, type_spec.ty().id()); | ||
let (ok_ty, _) = extract_result(&metadata, ty); | ||
|
||
assert_eq!(&TypeDef::Primitive(TypeDefPrimitive::U32), ok_ty.type_def()); | ||
} | ||
|
||
#[test] | ||
fn fallible_constructor_metadata_is_nested_result() { | ||
let metadata = generate_metadata(); | ||
let constructor = metadata.spec().constructors().iter().next().unwrap(); | ||
|
||
assert_eq!("try_new", constructor.label()); | ||
let type_spec = constructor.return_type().opt_type().unwrap(); | ||
assert_eq!( | ||
"ink_primitives::ConstructorResult", | ||
format!("{}", type_spec.display_name()) | ||
); | ||
|
||
let outer_result_ty = resolve_type(&metadata, type_spec.ty().id()); | ||
let (outer_ok_ty, outer_err_ty) = extract_result(&metadata, outer_result_ty); | ||
let (inner_ok_ty, _) = extract_result(&metadata, outer_ok_ty); | ||
|
||
assert_eq!( | ||
format!("{}", outer_err_ty.path()), | ||
"ink_primitives::LangError" | ||
); | ||
|
||
let unit_ty = TypeDef::Tuple(TypeDefTuple::new_portable(vec![])); | ||
assert_eq!( | ||
&unit_ty, | ||
inner_ok_ty.type_def(), | ||
"Ok variant should be a unit `()` type" | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the sake of completeness, we may also want to add tests for inherent messages, inherent fallible messages, etc.
Right now as long as the tests in
lang-err-integration-tests
work our metadata stuff is probably fine, but it would be nice to be more explicit with our checksThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, we need more tests for the metadata like this. Work for a follow-on PR methinks.