Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use named events in cumulus pallets #1260

Merged
merged 8 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
492 changes: 247 additions & 245 deletions Cargo.lock

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
NewInvulnerables(Vec<T::AccountId>),
NewDesiredCandidates(u32),
NewCandidacyBond(BalanceOf<T>),
CandidateAdded(T::AccountId, BalanceOf<T>),
CandidateRemoved(T::AccountId),
NewInvulnerables { invulnerables: Vec<T::AccountId> },
NewDesiredCandidates { desired_candidates: u32 },
NewCandidacyBond { bond_amount: BalanceOf<T> },
CandidateAdded { account_id: T::AccountId, deposit: BalanceOf<T> },
CandidateRemoved { account_id: T::AccountId },
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -308,7 +308,7 @@ pub mod pallet {
}

<Invulnerables<T>>::put(&new);
Self::deposit_event(Event::NewInvulnerables(new));
Self::deposit_event(Event::NewInvulnerables { invulnerables: new });
Ok(().into())
}

Expand All @@ -326,7 +326,7 @@ pub mod pallet {
log::warn!("max > T::MaxCandidates; you might need to run benchmarks again");
}
<DesiredCandidates<T>>::put(&max);
Self::deposit_event(Event::NewDesiredCandidates(max));
Self::deposit_event(Event::NewDesiredCandidates { desired_candidates: max });
Ok(().into())
}

Expand All @@ -338,7 +338,7 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
<CandidacyBond<T>>::put(&bond);
Self::deposit_event(Event::NewCandidacyBond(bond));
Self::deposit_event(Event::NewCandidacyBond { bond_amount: bond });
Ok(().into())
}

Expand Down Expand Up @@ -381,7 +381,7 @@ pub mod pallet {
}
})?;

Self::deposit_event(Event::CandidateAdded(who, deposit));
Self::deposit_event(Event::CandidateAdded { account_id: who, deposit });
Ok(Some(T::WeightInfo::register_as_candidate(current_count as u32)).into())
}

Expand Down Expand Up @@ -423,7 +423,7 @@ pub mod pallet {
<LastAuthoredBlock<T>>::remove(who.clone());
Ok(candidates.len())
})?;
Self::deposit_event(Event::CandidateRemoved(who.clone()));
Self::deposit_event(Event::CandidateRemoved { account_id: who.clone() });
Ok(current_count)
}

Expand Down
48 changes: 29 additions & 19 deletions pallets/dmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ pub mod pallet {
let used = Self::try_service_message(weight_limit, sent_at, &data[..])
.map_err(|_| Error::<T>::OverLimit)?;
Overweight::<T>::remove(index);
Self::deposit_event(Event::OverweightServiced(index, used));
Self::deposit_event(Event::OverweightServiced { overweight_index: index, used });
Ok(Some(used.saturating_add(1_000_000)).into())
}
}
Expand All @@ -162,22 +162,26 @@ pub mod pallet {
pub enum Event<T: Config> {
/// Downward message is invalid XCM.
/// \[ id \]
gilescope marked this conversation as resolved.
Show resolved Hide resolved
InvalidFormat(MessageId),
InvalidFormat { message_id: MessageId },
/// Downward message is unsupported version of XCM.
/// \[ id \]
UnsupportedVersion(MessageId),
UnsupportedVersion { message_id: MessageId },
/// Downward message executed with the given outcome.
/// \[ id, outcome \]
ExecutedDownward(MessageId, Outcome),
ExecutedDownward { message_id: MessageId, outcome: Outcome },
/// The weight limit for handling downward messages was reached.
/// \[ id, remaining, required \]
WeightExhausted(MessageId, Weight, Weight),
WeightExhausted { message_id: MessageId, remaining: Weight, required: Weight },
/// Downward message is overweight and was placed in the overweight queue.
/// \[ id, index, required \]
OverweightEnqueued(MessageId, OverweightIndex, Weight),
OverweightEnqueued {
message_id: MessageId,
overweight_index: OverweightIndex,
required: Weight,
},
/// Downward message from the overweight queue was executed.
/// \[ index, used \]
OverweightServiced(OverweightIndex, Weight),
OverweightServiced { overweight_index: OverweightIndex, used: Weight },
}

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -225,29 +229,29 @@ pub mod pallet {
_sent_at: RelayBlockNumber,
mut data: &[u8],
) -> Result<Weight, (MessageId, Weight)> {
let id = sp_io::hashing::blake2_256(data);
let message_id = sp_io::hashing::blake2_256(data);
let maybe_msg = VersionedXcm::<T::Call>::decode_all_with_depth_limit(
MAX_XCM_DECODE_DEPTH,
&mut data,
)
.map(Xcm::<T::Call>::try_from);
match maybe_msg {
Err(_) => {
Self::deposit_event(Event::InvalidFormat(id));
Self::deposit_event(Event::InvalidFormat { message_id });
Ok(0)
},
Ok(Err(())) => {
Self::deposit_event(Event::UnsupportedVersion(id));
Self::deposit_event(Event::UnsupportedVersion { message_id });
Ok(0)
},
Ok(Ok(x)) => {
let outcome = T::XcmExecutor::execute_xcm(Parent, x, limit);
match outcome {
Outcome::Error(XcmError::WeightLimitReached(required)) =>
Err((id, required)),
Err((message_id, required)),
outcome => {
let weight_used = outcome.weight_used();
Self::deposit_event(Event::ExecutedDownward(id, outcome));
Self::deposit_event(Event::ExecutedDownward { message_id, outcome });
Ok(weight_used)
},
}
Expand Down Expand Up @@ -286,15 +290,19 @@ pub mod pallet {
let remaining = limit.saturating_sub(used);
match Self::try_service_message(remaining, sent_at, &data[..]) {
Ok(consumed) => used += consumed,
Err((id, required)) =>
Err((message_id, required)) =>
// Too much weight required right now.
{
if required > config.max_individual {
// overweight - add to overweight queue and continue with
// message execution.
let index = page_index.overweight_count;
Overweight::<T>::insert(index, (sent_at, data));
Self::deposit_event(Event::OverweightEnqueued(id, index, required));
let overweight_index = page_index.overweight_count;
Overweight::<T>::insert(overweight_index, (sent_at, data));
Self::deposit_event(Event::OverweightEnqueued {
message_id,
overweight_index,
required,
});
page_index.overweight_count += 1;
// Not needed for control flow, but only to ensure that the compiler
// understands that we won't attempt to re-use `data` later.
Expand All @@ -304,9 +312,11 @@ pub mod pallet {
// from here on.
let item_count_left = item_count.saturating_sub(i);
maybe_enqueue_page = Some(Vec::with_capacity(item_count_left));
Self::deposit_event(Event::WeightExhausted(
id, remaining, required,
));
Self::deposit_event(Event::WeightExhausted {
message_id,
remaining,
required,
});
}
},
}
Expand Down
21 changes: 13 additions & 8 deletions pallets/parachain-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,9 @@ pub mod pallet {

Self::put_parachain_code(&validation_code);
<T::OnSystemEvent as OnSystemEvent>::on_validation_code_applied();
Self::deposit_event(Event::ValidationFunctionApplied(vfp.relay_parent_number));
Self::deposit_event(Event::ValidationFunctionApplied {
relay_chain_block_num: vfp.relay_parent_number,
});
},
Some(relay_chain::v2::UpgradeGoAhead::Abort) => {
<PendingValidationCode<T>>::kill();
Expand Down Expand Up @@ -389,7 +391,7 @@ pub mod pallet {

AuthorizedUpgrade::<T>::put(&code_hash);

Self::deposit_event(Event::UpgradeAuthorized(code_hash));
Self::deposit_event(Event::UpgradeAuthorized { code_hash });
Ok(())
}

Expand All @@ -411,17 +413,17 @@ pub mod pallet {
/// The validation function has been scheduled to apply.
ValidationFunctionStored,
/// The validation function was applied as of the contained relay chain block number.
ValidationFunctionApplied(RelayChainBlockNumber),
ValidationFunctionApplied { relay_chain_block_num: RelayChainBlockNumber },
/// The relay-chain aborted the upgrade process.
ValidationFunctionDiscarded,
/// An upgrade has been authorized.
UpgradeAuthorized(T::Hash),
UpgradeAuthorized { code_hash: T::Hash },
/// Some downward messages have been received and will be processed.
/// \[ count \]
DownwardMessagesReceived(u32),
DownwardMessagesReceived { count: u32 },
/// Downward messages were processed using the given weight.
/// \[ weight_used, result_mqc_head \]
DownwardMessagesProcessed(Weight, relay_chain::Hash),
DownwardMessagesProcessed { used: Weight, dmq_head: relay_chain::Hash },
}

#[pallet::error]
Expand Down Expand Up @@ -750,7 +752,7 @@ impl<T: Config> Pallet<T> {

let mut weight_used = 0;
if dm_count != 0 {
Self::deposit_event(Event::DownwardMessagesReceived(dm_count));
Self::deposit_event(Event::DownwardMessagesReceived { count: dm_count });
let max_weight =
<ReservedDmpWeightOverride<T>>::get().unwrap_or_else(T::ReservedDmpWeight::get);

Expand All @@ -763,7 +765,10 @@ impl<T: Config> Pallet<T> {
weight_used += T::DmpMessageHandler::handle_dmp_messages(message_iter, max_weight);
<LastDmqMqcHead<T>>::put(&dmq_head);

Self::deposit_event(Event::DownwardMessagesProcessed(weight_used, dmq_head.head()));
Self::deposit_event(Event::DownwardMessagesProcessed {
used: weight_used,
dmq_head: dmq_head.head(),
gilescope marked this conversation as resolved.
Show resolved Hide resolved
});
}

// After hashing each message in the message queue chain submitted by the collator, we
Expand Down
5 changes: 4 additions & 1 deletion pallets/parachain-system/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,10 @@ fn events() {
let events = System::events();
assert_eq!(
events[0].event,
Event::ParachainSystem(crate::Event::ValidationFunctionApplied(1234).into())
Event::ParachainSystem(
crate::Event::ValidationFunctionApplied { relay_chain_block_num: 1234 }
.into()
)
);
},
);
Expand Down
26 changes: 17 additions & 9 deletions pallets/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ pub mod pallet {
pub enum Event<T: Config> {
/// Downward message is invalid XCM.
/// \[ id \]
InvalidFormat([u8; 8]),
InvalidFormat { message_hash: [u8; 8] },
/// Downward message is unsupported version of XCM.
/// \[ id \]
UnsupportedVersion([u8; 8]),
UnsupportedVersion { message_hash: [u8; 8] },
/// Downward message executed with the given outcome.
/// \[ id, outcome \]
ExecutedDownward([u8; 8], Outcome),
ExecutedDownward { message_hash: [u8; 8], outcome: Outcome },
}

/// Origin for the parachains module.
Expand Down Expand Up @@ -121,12 +121,16 @@ impl<T: Config> DmpMessageHandler for UnlimitedDmpExecution<T> {
)
.map(Xcm::<T::Call>::try_from);
match msg {
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat(id)),
Ok(Err(())) => Pallet::<T>::deposit_event(Event::UnsupportedVersion(id)),
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat { message_hash: id }),
Ok(Err(())) =>
Pallet::<T>::deposit_event(Event::UnsupportedVersion { message_hash: id }),
Ok(Ok(x)) => {
let outcome = T::XcmExecutor::execute_xcm(Parent, x, limit);
used += outcome.weight_used();
Pallet::<T>::deposit_event(Event::ExecutedDownward(id, outcome));
Pallet::<T>::deposit_event(Event::ExecutedDownward {
message_hash: id,
outcome,
});
},
}
}
Expand Down Expand Up @@ -154,13 +158,17 @@ impl<T: Config> DmpMessageHandler for LimitAndDropDmpExecution<T> {
)
.map(Xcm::<T::Call>::try_from);
match msg {
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat(id)),
Ok(Err(())) => Pallet::<T>::deposit_event(Event::UnsupportedVersion(id)),
Err(_) => Pallet::<T>::deposit_event(Event::InvalidFormat { message_hash: id }),
Ok(Err(())) =>
Pallet::<T>::deposit_event(Event::UnsupportedVersion { message_hash: id }),
Ok(Ok(x)) => {
let weight_limit = limit.saturating_sub(used);
let outcome = T::XcmExecutor::execute_xcm(Parent, x, weight_limit);
used += outcome.weight_used();
Pallet::<T>::deposit_event(Event::ExecutedDownward(id, outcome));
Pallet::<T>::deposit_event(Event::ExecutedDownward {
message_hash: id,
outcome,
});
},
}
}
Expand Down
43 changes: 28 additions & 15 deletions pallets/xcmp-queue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub mod pallet {
let used = Self::handle_xcm_message(sender, sent_at, xcm, weight_limit)
.map_err(|_| Error::<T>::WeightOverLimit)?;
Overweight::<T>::remove(index);
Self::deposit_event(Event::OverweightServiced(index, used));
Self::deposit_event(Event::OverweightServiced { overweight_index: index, used });
Ok(Some(used.saturating_add(1_000_000)).into())
}

Expand Down Expand Up @@ -263,21 +263,26 @@ pub mod pallet {
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Some XCM was executed ok.
Success(Option<T::Hash>),
Success { message_hash: Option<T::Hash> },
/// Some XCM failed.
Fail(Option<T::Hash>, XcmError),
Fail { message_hash: Option<T::Hash>, error: XcmError },
/// Bad XCM version used.
BadVersion(Option<T::Hash>),
BadVersion { message_hash: Option<T::Hash> },
/// Bad XCM format used.
BadFormat(Option<T::Hash>),
BadFormat { message_hash: Option<T::Hash> },
/// An upward message was sent to the relay chain.
UpwardMessageSent(Option<T::Hash>),
UpwardMessageSent { message_hash: Option<T::Hash> },
/// An HRMP message was sent to a sibling parachain.
XcmpMessageSent(Option<T::Hash>),
XcmpMessageSent { message_hash: Option<T::Hash> },
/// An XCM exceeded the individual message weight budget.
OverweightEnqueued(ParaId, RelayBlockNumber, OverweightIndex, Weight),
OverweightEnqueued {
sender: ParaId,
sent_at: RelayBlockNumber,
overweight_index: OverweightIndex,
required: Weight,
},
/// An XCM from the overweight queue was executed with the given actual weight used.
OverweightServiced(OverweightIndex, Weight),
OverweightServiced { overweight_index: OverweightIndex, used: Weight },
}

#[pallet::error]
Expand Down Expand Up @@ -600,14 +605,17 @@ impl<T: Config> Pallet<T> {
Ok(xcm) => {
let location = (1, Parachain(sender.into()));
match T::XcmExecutor::execute_xcm(location, xcm, max_weight) {
Outcome::Error(e) => (Err(e), Event::Fail(Some(hash), e)),
Outcome::Complete(w) => (Ok(w), Event::Success(Some(hash))),
Outcome::Error(error) =>
(Err(error), Event::Fail { message_hash: Some(hash), error }),
Outcome::Complete(w) => (Ok(w), Event::Success { message_hash: Some(hash) }),
// As far as the caller is concerned, this was dispatched without error, so
// we just report the weight used.
Outcome::Incomplete(w, e) => (Ok(w), Event::Fail(Some(hash), e)),
Outcome::Incomplete(w, error) =>
(Ok(w), Event::Fail { message_hash: Some(hash), error }),
}
},
Err(()) => (Err(XcmError::UnhandledXcmVersion), Event::BadVersion(Some(hash))),
Err(()) =>
(Err(XcmError::UnhandledXcmVersion), Event::BadVersion { message_hash: Some(hash) }),
};
Self::deposit_event(event);
result
Expand Down Expand Up @@ -644,7 +652,12 @@ impl<T: Config> Pallet<T> {
.saturating_sub(remaining_fragments.len());
let overweight_xcm = last_remaining_fragments[..msg_len].to_vec();
let index = Self::stash_overweight(sender, sent_at, overweight_xcm);
let e = Event::OverweightEnqueued(sender, sent_at, index, required);
let e = Event::OverweightEnqueued {
sender,
sent_at,
overweight_index: index,
required,
};
Self::deposit_event(e);
},
Err(XcmError::WeightLimitReached(required))
Expand Down Expand Up @@ -1098,7 +1111,7 @@ impl<T: Config> SendXcm for Pallet<T> {
versioned_xcm,
)
.map_err(|e| SendError::Transport(<&'static str>::from(e)))?;
Self::deposit_event(Event::XcmpMessageSent(Some(hash)));
Self::deposit_event(Event::XcmpMessageSent { message_hash: Some(hash) });
Ok(())
},
// Anything else is unhandled. This includes a message this is meant for us.
Expand Down