-
Notifications
You must be signed in to change notification settings - Fork 478
Ensure topics are unique #594
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
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e9a2371
Ensure topics are unique
cmichi d7c7bd2
Remove unavailable method
cmichi 4456985
Fix salt appending in Wasm
cmichi 45131d7
Remove salt param
cmichi 6865687
Add missing assignment
cmichi d050a8f
Update crates/lang/src/events.rs
11cc54e
Add suggestions
cmichi 88c4773
Remove always inline
cmichi df969a1
Fix event hashing in examples
cmichi 38cd811
Make nightly clippy happy
cmichi 733c7ea
Remove unnecessary return param
cmichi de30905
Do not use internal type for hash calculation
cmichi 4579c4c
Hide internal type from docs
cmichi bd7ce1f
Expose `PrefixedValue` from ink_env::topics
cmichi 9103858
Apply cargo fmt
cmichi f108c91
Apply cargo fmt
cmichi 1a23197
Merge branch 'master' into cmichi-err-on-duplicate-topic
cmichi 21dd624
Merge branch 'master' into cmichi-err-on-duplicate-topic
cmichi 3c6334f
Apply comments
cmichi b6ae71b
Apply comments
cmichi 96833ea
Apply comments
cmichi 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
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,98 @@ | ||
| // Copyright 2018-2020 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)] | ||
|
|
||
| use ink_lang as ink; | ||
|
|
||
| #[ink::contract] | ||
| mod my_contract { | ||
| #[ink(storage)] | ||
| pub struct MyContract {} | ||
|
|
||
| /// Exemplary event | ||
| #[ink(event)] | ||
| pub struct MyEvent { | ||
| #[ink(topic)] | ||
| v0: Option<AccountId>, | ||
| #[ink(topic)] | ||
| v1: Balance, | ||
| #[ink(topic)] | ||
| v2: bool, | ||
| #[ink(topic)] | ||
| v3: bool, | ||
cmichi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| impl MyContract { | ||
| /// Creates a new `MyContract` instance. | ||
| #[ink(constructor)] | ||
| pub fn new() -> Self { | ||
| MyContract {} | ||
| } | ||
|
|
||
| /// Emits a `MyEvent`. | ||
| #[ink(message)] | ||
| pub fn emit_my_event(&self) { | ||
| Self::env().emit_event(MyEvent { | ||
| v0: None, | ||
| v1: 0, | ||
| v2: false, | ||
| v3: false, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ink_env::test::EmittedEvent; | ||
| use ink_lang as ink; | ||
|
|
||
| #[ink::test] | ||
| fn event_must_have_unique_topics() { | ||
| // given | ||
| let my_contract = MyContract::new(); | ||
|
|
||
| // when | ||
| MyContract::emit_my_event(&my_contract); | ||
|
|
||
| // then | ||
| // all topics must be unique | ||
| let emitted_events = | ||
| ink_env::test::recorded_events().collect::<Vec<EmittedEvent>>(); | ||
| let mut encoded_topics: std::vec::Vec<&[u8]> = emitted_events[0] | ||
| .topics | ||
| .iter() | ||
| .map(|topic| topic.encoded_bytes().expect("encoded bytes must exist")) | ||
| .collect(); | ||
| assert!(!has_duplicates(&mut encoded_topics)); | ||
| } | ||
| } | ||
|
|
||
| /// Finds duplicates in a given vector. | ||
| /// | ||
| /// This function has complexity of `O(n * log n)` and no additional memory | ||
| /// is required, although the order of items is not preserved. | ||
| fn has_duplicates<T: PartialEq + AsRef<[u8]>>(items: &mut Vec<T>) -> bool { | ||
| // Sort the vector | ||
| items.sort_by(|a, b| Ord::cmp(a.as_ref(), b.as_ref())); | ||
| // And then find any two consecutive equal elements. | ||
| items.windows(2).any(|w| { | ||
| match w { | ||
| &[ref a, ref b] => a == b, | ||
| _ => false, | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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
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.