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

Events 2.0 #1827

Merged
merged 152 commits into from
Jul 24, 2023
Merged
Show file tree
Hide file tree
Changes from 147 commits
Commits
Show all changes
152 commits
Select commit Hold shift + click to select a range
4841460
WIP derive Event
ascjones Apr 26, 2023
bce872d
Move tests to top level
ascjones Apr 26, 2023
cbdf8ff
Add failing Event derive test
ascjones Apr 26, 2023
0e42a02
Test compiles, now fails
ascjones Apr 26, 2023
bb979c9
Passing test, now impl
ascjones Apr 27, 2023
5cf66c2
Passing test with no fields
ascjones Apr 27, 2023
a78360a
Add events integration-tests example
ascjones Apr 27, 2023
eb1b771
Expose Event derive macro at the top level ::ink::Event
ascjones Apr 27, 2023
c0dbde2
Remove println!
ascjones Apr 27, 2023
e979a9a
Remove ContractEventBase
ascjones Apr 27, 2023
8f44244
WIP rewiring event codegen for use with derive.
ascjones Apr 27, 2023
a320001
Direct impl of emit_event
ascjones May 4, 2023
a75e7f7
Use bound_impl
ascjones May 4, 2023
5e92390
WIP impl derive EventMetadata
ascjones May 4, 2023
fc39589
WIP add event metadata derive
ascjones May 5, 2023
5fea6ff
Add some metadata derive todos
ascjones May 5, 2023
c1ba2fa
WIP Collect metadata from linked events
ascjones May 5, 2023
dc88a33
Refactor event metadata collection
ascjones May 5, 2023
1fcf5e2
Add metadata test
ascjones May 5, 2023
5f7f99f
Check for external and Inline events
ascjones May 5, 2023
884e82c
Add todo for inline events
ascjones May 9, 2023
e5951e7
Merge branch 'master' into aj/shared-events-redux
ascjones May 9, 2023
1b51949
Merge branch 'master' into aj/shared-events-redux
ascjones May 19, 2023
e2658d6
WIP adding compile time topics check to emit_event.
ascjones May 22, 2023
a82a6ad
fmt
ascjones May 22, 2023
b084d61
Refactor EventRespectsTopicsLimit to use const assertion
ascjones May 22, 2023
6c4ad8e
Fmt
ascjones May 22, 2023
07d9b85
Remove unused code from legacy events generation
ascjones May 22, 2023
b7c7199
test and generate SIGNATURE topic
ascjones May 22, 2023
7258197
use built in ink macro for generating signature topic
ascjones May 23, 2023
76a176f
Remove all warnings
ascjones May 23, 2023
b3b2587
Implement anonymous event
ascjones May 25, 2023
d0d917b
Remove PrefixedValue
ascjones May 25, 2023
951543a
Implement field topics
ascjones May 25, 2023
c152723
Use TOPICS_LEN for topics remaining
ascjones May 25, 2023
affd57d
WIP don't push Option::None topics
ascjones May 25, 2023
e66f697
Fix only publishing if not `Option::None`
ascjones May 26, 2023
ef6693c
Fix only publishing if not `Option::None`
ascjones May 26, 2023
cacb540
Only publish value of `Some` topic, not none.
ascjones May 26, 2023
c2ac444
Fix event derive codegen tests
ascjones May 26, 2023
5a378e7
Add test for None topic
ascjones May 26, 2023
bbdb5fb
Rename Topics trait to Event
ascjones May 30, 2023
45460fa
Fmt
ascjones May 30, 2023
57e6ac4
Remove println
ascjones May 30, 2023
4c731ea
Remove EmitEvent imports
ascjones May 30, 2023
938b361
WIP adding E2E test for topics
ascjones Jun 1, 2023
562b044
Clippy
ascjones Jun 1, 2023
b83bba9
Unnused imports
ascjones Jun 1, 2023
20b2c44
E2E test contract events emitted
ascjones Jun 1, 2023
56ef583
WIP e2e testing topics
ascjones Jun 1, 2023
0714290
Merge branch 'master' into aj/shared-events-redux
ascjones Jun 1, 2023
dbd0dbe
Add e2e test which checks topics
ascjones Jun 1, 2023
694d8f5
Merge branch 'master' into aj/shared-events-redux
ascjones Jun 8, 2023
916399f
Add signature topic to event spec metadata
ascjones Jun 8, 2023
e1eba71
Use Result in EventMetadata derive
ascjones Jun 9, 2023
39bd2d9
Tests for generating events metadata
ascjones Jun 9, 2023
d97c51e
Remove max topics len compile time check
ascjones Jun 9, 2023
0704d94
Merge branch 'master' into aj/shared-events-redux
ascjones Jun 12, 2023
c74da8c
Add failing test for topics len validation at metadata generation time
ascjones Jun 12, 2023
ca861ae
Check for max topics limit breach in metadata generation
ascjones Jun 13, 2023
39e6e28
Add extra event with no signature topic
ascjones Jun 13, 2023
fc40629
Remove checks for generics and pub visibility
ascjones Jun 13, 2023
bbbee5e
Fix anonymous attr
ascjones Jun 13, 2023
43e3ab0
Move topics attr validation to derive macro
ascjones Jun 13, 2023
6e93717
Fmt and remove remaining MAX_TOPICS check
ascjones Jun 13, 2023
28b2d30
Remove event spec field type name
ascjones Jun 13, 2023
98a73c2
Merge branch 'master' into aj/shared-events-redux
ascjones Jun 19, 2023
298cfa8
Clippy
ascjones Jun 19, 2023
e7e1837
WIP add `#[ink::event]` which expands to derives
ascjones Jun 19, 2023
6616265
Merge branch 'master' into aj/shared-events-redux
ascjones Jun 20, 2023
e8f186b
Check for signature topics collisions when building metadata
ascjones Jun 22, 2023
a232e30
Merge branch 'master' into aj/shared-events-redux
ascjones Jun 23, 2023
309e30c
Commentl
ascjones Jun 23, 2023
03f7e7c
Remove EventRespectsTopicLimits
ascjones Jun 26, 2023
eeecbc7
Remove cfg attr test
ascjones Jun 26, 2023
5391171
Remove unused topics attr test
ascjones Jun 26, 2023
0891fd3
Fmt
ascjones Jun 26, 2023
81d62ae
Fix trait erc20 tests
ascjones Jun 26, 2023
4073c7e
Fix erc20 tests and fmt
ascjones Jun 26, 2023
b423df0
Fix duplicate inline attrs. Remove offchain duplicate topics check.
ascjones Jun 26, 2023
5414c95
Fmt
ascjones Jun 26, 2023
a0c0218
Clippy
ascjones Jun 26, 2023
cb52b1a
clippy
ascjones Jun 26, 2023
4eb0e81
events clippy
ascjones Jun 26, 2023
7bf21cc
Push 0 topic if `None`
ascjones Jun 28, 2023
7fef4a6
Expose signature_topic
ascjones Jun 28, 2023
ef91f80
Change SignatureTopic to expose as_bytes
ascjones Jun 28, 2023
7f5f6d8
Add test for None topic value
ascjones Jun 28, 2023
4334aac
Fix erc20 topic tests
ascjones Jun 28, 2023
37115d9
Add module_path
ascjones Jun 29, 2023
f8bbb3b
Fix max topics in tests
ascjones Jun 29, 2023
ad144ca
Fix event docs test
ascjones Jun 29, 2023
4af676f
Clippy
ascjones Jun 29, 2023
8908f10
Use path dependency for ink_env
ascjones Jun 29, 2023
d63afe5
Remove ink_env dependency
ascjones Jun 29, 2023
c7636fb
RUSTFLAGS for test to fix linking issue
ascjones Jun 29, 2023
4ca3065
RUSTFLAGS for test to fix linking issue
ascjones Jun 29, 2023
87a965f
Fix custom environment, remove extra topic
ascjones Jun 29, 2023
986147a
Merge branch 'master' into aj/shared-events-redux
ascjones Jul 3, 2023
346b380
Use ink::event syntax
ascjones Jul 4, 2023
f1bd57f
Add ui test for cfg attributes
ascjones Jul 4, 2023
1ed1e88
Add success ui tests for ink::event
ascjones Jul 4, 2023
bb26312
UI test for `enum` should fail
ascjones Jul 4, 2023
3d3c898
Refactor topic attribute fn to accept BindingInfo
ascjones Jul 4, 2023
f27176e
Merge branch 'master' into aj/shared-events-redux
ascjones Jul 4, 2023
7b0abe9
Fmt
ascjones Jul 4, 2023
4fd7cf6
Remove commented out code
ascjones Jul 4, 2023
5aa6366
Event docs
ascjones Jul 4, 2023
49281d5
Add event field docs
ascjones Jul 4, 2023
4327ce0
Fix event param spec doc tests
ascjones Jul 4, 2023
f6d1953
Fix metadata codegen tests
ascjones Jul 4, 2023
e4c3572
Implement docs for `Event` derive macro
ascjones Jul 4, 2023
4d48669
Spellcheck
ascjones Jul 5, 2023
3dcc12f
Check for multiple ink attributes
ascjones Jul 5, 2023
1defd81
Spellcheck
ascjones Jul 5, 2023
a1e3879
Ink attribute validation
ascjones Jul 5, 2023
89e3df6
Remove Topic
ascjones Jul 5, 2023
a80bb74
Oops
ascjones Jul 5, 2023
15bc04a
Oops again
ascjones Jul 5, 2023
802c799
Test for events in different crates being used in the metadata.
ascjones Jul 5, 2023
ad8b3e7
Test for unused event not included in metadata
ascjones Jul 5, 2023
4dcc771
Add test for inline event definition metadata.
ascjones Jul 5, 2023
47b8e70
Add test for emitting inline event
ascjones Jul 5, 2023
4a6431b
Fmt
ascjones Jul 5, 2023
9dea0aa
Remove todo, duplicate attributes are checked in the derive impls
ascjones Jul 5, 2023
9612515
Add docs
ascjones Jul 5, 2023
ce7c4e0
Fix examples-test
ascjones Jul 5, 2023
0b2d2de
Add docs for `#[ink::event]`
ascjones Jul 5, 2023
7bc0183
Add docs for `EventMetadata` trait
ascjones Jul 5, 2023
100cbaa
Update Event trait comments
ascjones Jul 5, 2023
850c38a
Docs
ascjones Jul 5, 2023
d8185cc
SIGNATURE_TOPIC docs
ascjones Jul 5, 2023
b7376e6
Fix docs
ascjones Jul 5, 2023
205f876
fmt
ascjones Jul 5, 2023
c558972
Try setting lto = "thin" for metadata crate to fix met
ascjones Jul 5, 2023
083fad8
Add lto to test profile of unit test
ascjones Jul 6, 2023
df91ae5
Add test to check disallow generics for Event derive
ascjones Jul 7, 2023
f7a4f4f
Fmt
ascjones Jul 7, 2023
8f8d346
Change to `#[ink::event(anonymous = true)]` syntax
ascjones Jul 10, 2023
26245ac
Fix test
ascjones Jul 10, 2023
f912b7b
Annotate derived event struct with anon
ascjones Jul 10, 2023
68688b1
UI test
ascjones Jul 10, 2023
1a10613
Document limitation of signature topic derive
ascjones Jul 13, 2023
c60a786
Fix doctest
ascjones Jul 13, 2023
59fe50e
Merge branch 'master' into aj/shared-events-redux
ascjones Jul 13, 2023
dd8d861
Fix up errors after merge
ascjones Jul 13, 2023
1a8b157
Usage of the crate includes defined there events (#1842)
xgreenx Jul 14, 2023
cac0f95
Update anonymous syntax
ascjones Jul 17, 2023
4d97e78
Anonymous comment
ascjones Jul 19, 2023
c39b678
Merge branch 'master' into aj/shared-events-redux
ascjones Jul 19, 2023
1371e35
Move haashing macro inline signature topic gen fn
ascjones Jul 24, 2023
9fb00fc
Fix signature_topic return type
ascjones Jul 24, 2023
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
8 changes: 8 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ test:
- job: check-std
artifacts: false
variables:
# Fix for linking of `linkme` for `cargo test`: https://github.com/dtolnay/linkme/issues/49
RUSTFLAGS: "-Clink-arg=-z -Clink-arg=nostart-stop-gc"
# Since we run the tests with `--all-features` this implies the feature
# `ink-fuzz-tests` as well -- i.e. the fuzz tests are run.
# There's no way to disable a single feature while enabling all features
Expand Down Expand Up @@ -372,6 +374,9 @@ examples-test:
needs:
- job: clippy-std
artifacts: false
variables:
# Fix linking of `linkme`: https://github.com/dtolnay/linkme/issues/49
RUSTFLAGS: "-Clink-arg=-z -Clink-arg=nostart-stop-gc"
script:
- for example in integration-tests/*/; do
if [ "$example" = "integration-tests/lang-err-integration-tests/" ]; then continue; fi;
Expand Down Expand Up @@ -401,6 +406,9 @@ examples-contract-build:
stage: examples
<<: *docker-env
<<: *test-refs
variables:
# Fix for linking of `linkme` for `cargo contract build`: https://github.com/dtolnay/linkme/issues/49
RUSTFLAGS: "-Clink-arg=-z -Clink-arg=nostart-stop-gc"
script:
- rustup component add rust-src --toolchain stable
- cargo contract -V
Expand Down
87 changes: 31 additions & 56 deletions crates/e2e/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ use super::{
constructor_exec_input,
CreateBuilderPartial,
},
events::{
CodeStoredEvent,
ContractInstantiatedEvent,
EventWithTopics,
},
log_error,
log_info,
sr25519,
Expand Down Expand Up @@ -49,18 +54,15 @@ use std::{
path::PathBuf,
};

use crate::events;
use subxt::{
blocks::ExtrinsicEvents,
config::ExtrinsicParams,
events::EventDetails,
ext::{
scale_decode,
scale_encode,
scale_value::{
Composite,
Value,
ValueDef,
},
ext::scale_value::{
Composite,
Value,
ValueDef,
},
tx::PairSigner,
};
Expand All @@ -80,54 +82,6 @@ pub type CallBuilderFinal<E, Args, RetType> = ink_env::call::CallBuilder<
Set<ReturnType<RetType>>,
>;

/// A contract was successfully instantiated.
#[derive(
Debug,
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
struct ContractInstantiatedEvent<E: Environment> {
/// Account id of the deployer.
pub deployer: E::AccountId,
/// Account id where the contract was instantiated to.
pub contract: E::AccountId,
}

impl<E> subxt::events::StaticEvent for ContractInstantiatedEvent<E>
where
E: Environment,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "Instantiated";
}

/// Code with the specified hash has been stored.
#[derive(
Debug,
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
struct CodeStoredEvent<E: Environment> {
/// Hash under which the contract code was stored.
pub code_hash: E::Hash,
}

impl<E> subxt::events::StaticEvent for CodeStoredEvent<E>
where
E: Environment,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "CodeStored";
}

/// The `Client` takes care of communicating with the node.
///
/// This node's RPC interface will be used for instantiating the contract
Expand Down Expand Up @@ -726,4 +680,25 @@ impl<E: Environment, V, C: subxt::Config> CallResult<E, V, ExtrinsicEvents<C>> {
event.pallet_name() == pallet_name && event.variant_name() == variant_name
})
}

/// Returns all the `ContractEmitted` events emitted by the contract.
pub fn contract_emitted_events(
&self,
) -> Result<Vec<EventWithTopics<events::ContractEmitted<E>>>, subxt::Error>
where
C::Hash: Into<sp_core::H256>,
{
let mut events_with_topics = Vec::new();
for event in self.events.iter() {
let event = event?;
if let Some(decoded_event) = event.as_event::<events::ContractEmitted<E>>()? {
let event_with_topics = EventWithTopics {
event: decoded_event,
topics: event.topics().iter().cloned().map(Into::into).collect(),
};
events_with_topics.push(event_with_topics);
}
}
Ok(events_with_topics)
}
}
102 changes: 102 additions & 0 deletions crates/e2e/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// 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.

use ink_env::Environment;
#[cfg(feature = "std")]
use std::fmt::Debug;

use subxt::{
events::StaticEvent,
ext::{
scale_decode,
scale_encode,
},
};

/// A contract was successfully instantiated.
#[derive(
Debug,
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct ContractInstantiatedEvent<E: Environment> {
/// Account id of the deployer.
pub deployer: E::AccountId,
/// Account id where the contract was instantiated to.
pub contract: E::AccountId,
}

impl<E> StaticEvent for ContractInstantiatedEvent<E>
where
E: Environment,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "Instantiated";
}

/// Code with the specified hash has been stored.
#[derive(
Debug,
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
pub struct CodeStoredEvent<E: Environment> {
/// Hash under which the contract code was stored.
pub code_hash: E::Hash,
}

impl<E> StaticEvent for CodeStoredEvent<E>
where
E: Environment,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "CodeStored";
}

#[derive(
scale::Decode,
scale::Encode,
scale_decode::DecodeAsType,
scale_encode::EncodeAsType,
Debug,
)]
#[decode_as_type(trait_bounds = "", crate_path = "subxt::ext::scale_decode")]
#[encode_as_type(crate_path = "subxt::ext::scale_encode")]
/// A custom event emitted by the contract.
pub struct ContractEmitted<E: Environment> {
pub contract: E::AccountId,
pub data: Vec<u8>,
}

impl<E> StaticEvent for ContractEmitted<E>
where
E: Environment,
{
const PALLET: &'static str = "Contracts";
const EVENT: &'static str = "ContractEmitted";
}

/// A decoded event with its associated topics.
pub struct EventWithTopics<T> {
pub topics: Vec<sp_core::H256>,
pub event: T,
}
1 change: 1 addition & 0 deletions crates/e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod client;
mod contract_results;
mod default_accounts;
mod error;
pub mod events;
mod node_proc;
mod xts;

Expand Down
8 changes: 4 additions & 4 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ use crate::{
EnvInstance,
OnInstance,
},
event::Event,
hash::{
CryptoHash,
HashOutput,
},
topics::Topics,
types::Gas,
Environment,
Result,
Expand Down Expand Up @@ -175,13 +175,13 @@ where
}

/// Emits an event with the given event data.
pub fn emit_event<E, Event>(event: Event)
pub fn emit_event<E, Evt>(event: Evt)
SkymanOne marked this conversation as resolved.
Show resolved Hide resolved
where
E: Environment,
Event: Topics + scale::Encode,
Evt: Event,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::emit_event::<E, Event>(instance, event)
TypedEnvBackend::emit_event::<E, Evt>(instance, event)
})
}

Expand Down
6 changes: 3 additions & 3 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::{
DelegateCall,
FromAccountId,
},
event::Event,
hash::{
CryptoHash,
HashOutput,
},
topics::Topics,
Environment,
Result,
};
Expand Down Expand Up @@ -405,10 +405,10 @@ pub trait TypedEnvBackend: EnvBackend {
/// # Note
///
/// For more details visit: [`emit_event`][`crate::emit_event`]
fn emit_event<E, Event>(&mut self, event: Event)
fn emit_event<E, Evt>(&mut self, event: Evt)
where
E: Environment,
Event: Topics + scale::Encode;
Evt: Event;

/// Invokes a contract message and returns its result.
///
Expand Down
16 changes: 6 additions & 10 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ use crate::{
DelegateCall,
FromAccountId,
},
event::{
Event,
TopicsBuilderBackend,
},
hash::{
Blake2x128,
Blake2x256,
Expand All @@ -30,10 +34,6 @@ use crate::{
Keccak256,
Sha2x256,
},
topics::{
Topics,
TopicsBuilderBackend,
},
Clear,
EnvBackend,
Environment,
Expand Down Expand Up @@ -150,10 +150,6 @@ where
}
let off_hash = result.as_ref();
let off_hash = off_hash.to_vec();
debug_assert!(
!self.topics.contains(&off_hash),
"duplicate topic hash discovered!"
);
self.topics.push(off_hash);
}

Expand Down Expand Up @@ -422,10 +418,10 @@ impl TypedEnvBackend for EnvInstance {
})
}

fn emit_event<E, Event>(&mut self, event: Event)
fn emit_event<E, Evt>(&mut self, event: Evt)
where
E: Environment,
Event: Topics + scale::Encode,
Evt: Event,
{
let builder = TopicsBuilder::default();
let enc_topics = event.topics::<E, _>(builder.into());
Expand Down
2 changes: 1 addition & 1 deletion crates/env/src/engine/off_chain/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use crate::{
engine::off_chain::impls::TopicsBuilder,
topics::TopicsBuilderBackend,
event::TopicsBuilderBackend,
Result,
};

Expand Down
Loading