-
Notifications
You must be signed in to change notification settings - Fork 480
Custom signature topic #2031
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
Custom signature topic #2031
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
8df58c3
basic setup
SkymanOne ff82dcb
successful parsing of args
SkymanOne 6706b96
working specification
SkymanOne 14429ac
fix tests
SkymanOne 5d3333b
add changelog
SkymanOne 48cec1f
fmt
SkymanOne b802df2
add signature_topic macro
SkymanOne ac0d28c
simpligy argument parsing
SkymanOne 8feaf2d
add tests
SkymanOne 21994a2
remove links
SkymanOne 683e180
use const array
SkymanOne 91b3f91
remove hex crate
SkymanOne 3c788c3
Refactor. Remove signature topic macro in favor of local custom ink a…
SkymanOne 490e5a6
Merge branch 'master' into gn/custom-signature-topic
SkymanOne 1c92ba8
minor fixes
SkymanOne 9e67b3e
remove trailing code
SkymanOne c1168e7
fix UI tests
SkymanOne 764443c
add UI tests
SkymanOne 67ed065
extra UI tests and improved error reporting
SkymanOne 07433cb
uncomment tests
SkymanOne 7eba6a8
rename function
SkymanOne c5e5e9f
Merge branch 'master' into gn/custom-signature-topic
SkymanOne 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,79 @@ | ||
| // Copyright (C) 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. | ||
|
|
||
| use crate::GenerateCode; | ||
| use derive_more::From; | ||
| use ir::HexLiteral; | ||
| use proc_macro2::TokenStream as TokenStream2; | ||
| use quote::quote; | ||
| use syn::spanned::Spanned; | ||
|
|
||
| /// Generates code to generate signature topic. | ||
| #[derive(From, Copy, Clone)] | ||
| pub struct SignatureTopic<'a> { | ||
| /// The event item to generate code for. | ||
| item: &'a ir::SignatureTopic, | ||
| } | ||
|
|
||
| impl GenerateCode for SignatureTopic<'_> { | ||
| /// Generates ink! signature topic item code. | ||
| fn generate_code(&self) -> TokenStream2 { | ||
| let item = self.item.item(); | ||
| let signature_topic = self.generate_signature_topic(); | ||
| let cfg_attrs = self.item.get_cfg_attrs(item.span()); | ||
|
|
||
| quote::quote! ( | ||
| #( #cfg_attrs )* | ||
| #signature_topic | ||
| #item | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl SignatureTopic<'_> { | ||
| /// Generates the implementation of `GetSignatureTopic` trait. | ||
| fn generate_signature_topic(&self) -> TokenStream2 { | ||
| let item_ident = &self.item.item().ident; | ||
| let signature_topic = if let Some(bytes) = self.item.signature_topic() { | ||
| let hash_bytes = bytes.map(|b| b.hex_padded_suffixed()); | ||
| quote! { ::core::option::Option::Some([ #( #hash_bytes ),* ]) } | ||
| } else { | ||
| let calculated_topic = signature_topic(&self.item.item().fields, item_ident); | ||
| quote! { ::core::option::Option::Some(#calculated_topic) } | ||
| }; | ||
|
|
||
| quote! { | ||
| impl ::ink::env::GetSignatureTopic for #item_ident { | ||
| const SIGNATURE_TOPIC: ::core::option::Option<[u8; 32]> = #signature_topic; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The signature topic of an event variant. | ||
| /// | ||
| /// Calculated with `blake2b("Event(field1_type,field2_type)")`. | ||
| fn signature_topic(fields: &syn::Fields, event_ident: &syn::Ident) -> TokenStream2 { | ||
| let fields = fields | ||
| .iter() | ||
| .map(|field| { | ||
| quote::ToTokens::to_token_stream(&field.ty) | ||
| .to_string() | ||
| .replace(' ', "") | ||
| }) | ||
| .collect::<Vec<_>>() | ||
| .join(","); | ||
| let topic_str = format!("{}({fields})", event_ident); | ||
| quote!(::ink::blake2x256!(#topic_str)) | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.