Skip to content

Commit

Permalink
Improve events (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: Shady Khalifa <shekohex@gmail.com>
  • Loading branch information
Filip Lazovic and Shady Khalifa committed Mar 22, 2021
1 parent 757b4f6 commit e8b43d2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
18 changes: 7 additions & 11 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@ name: Set-Up & Build & Test

# Controls when the action will run.
on:
push:
branches:
- "**" # matches every branch
- "!master" # excludes master
pull_request:
branches:
- "**" # matches every branch
- "!master" # excludes master

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
push:
branches:
- "**" # matches every branch
- "!master" # excludes master

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
Expand Down
16 changes: 11 additions & 5 deletions pallets/merkle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,12 @@ pub mod pallet {

#[pallet::event]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
#[pallet::metadata(T::AccountId = "AccountId", T::GroupId = "GroupId")]
pub enum Event<T: Config> {
/// New tree created
NewTree(T::GroupId, T::AccountId, bool),
/// New members/leaves added to the tree
NewMember(T::GroupId, T::AccountId, Vec<ScalarData>),
NewMembers(T::GroupId, T::AccountId, u32, Vec<ScalarData>),
}

/// Old name generated by `decl_event`.
Expand Down Expand Up @@ -498,8 +501,10 @@ impl<T: Config> Group<T::AccountId, T::BlockNumber, T::GroupId> for Pallet<T> {
Groups::<T>::insert(group_id, Some(mtree));

// Setting up the manager
let manager = Manager::<T>::new(sender, is_manager_required);
let manager = Manager::<T>::new(sender.clone(), is_manager_required);
Managers::<T>::insert(group_id, Some(manager));

Self::deposit_event(Event::NewTree(group_id, sender, is_manager_required));
Ok(group_id)
}

Expand Down Expand Up @@ -549,9 +554,10 @@ impl<T: Config> Group<T::AccountId, T::BlockNumber, T::GroupId> for Pallet<T> {
Self::is_manager_required(sender.clone(), &manager_data),
Error::<T>::ManagerIsRequired
);
let num_points = members.len() as u32;
let leaf_count_before = tree.leaf_count;
let num_members = members.len() as u32;
ensure!(
tree.leaf_count + num_points <= tree.max_leaves,
leaf_count_before + num_members <= tree.max_leaves,
Error::<T>::ExceedsMaxLeaves
);

Expand All @@ -564,7 +570,7 @@ impl<T: Config> Group<T::AccountId, T::BlockNumber, T::GroupId> for Pallet<T> {
Groups::<T>::insert(id, Some(tree));

// Raising the New Member event for the client to build a tree locally
Self::deposit_event(Event::NewMember(id, sender, members));
Self::deposit_event(Event::NewMembers(id, sender, leaf_count_before, members));
Ok(())
}

Expand Down
48 changes: 41 additions & 7 deletions pallets/mixer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,32 @@ pub mod pallet {

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::metadata(<T as frame_system::Config>::AccountId = "AccountId", <T as merkle::Config>::GroupId = "GroupId")]
#[pallet::metadata(T::AccountId = "AccountId", T::GroupId = "GroupId", BalanceOf<T> = "Balance")]
pub enum Event<T: Config> {
/// New deposit added to the specific mixer
Deposit(
<T as merkle::Config>::GroupId,
<T as frame_system::Config>::AccountId,
ScalarData,
/// Id of the tree
T::GroupId,
/// Account id of the sender
T::AccountId,
/// Deposit size
BalanceOf<T>,
/// Number of leaves before insertion
u32,
/// Leaf data
Vec<ScalarData>,
),
/// Withdrawal from the specific mixer
Withdraw(
<T as merkle::Config>::GroupId,
<T as frame_system::Config>::AccountId,
/// Id of the tree
T::GroupId,
/// Account id of the sender
T::AccountId,
/// Account id of the recipient
T::AccountId,
/// Account id of the relayer
T::AccountId,
/// Merkle root
ScalarData,
),
}
Expand Down Expand Up @@ -257,9 +271,21 @@ pub mod pallet {
<TotalValueLocked<T>>::insert(mixer_id, tvl + deposit);
// add elements to the mixer group's merkle tree and save the leaves
T::Group::add_members(Self::account_id(), mixer_id.into(), data_points.clone())?;
mixer_info.leaves.extend(data_points);

// number of leaves before the insertion of new leaves
let num_leaves_before = mixer_info.leaves.len() as u32;
let deposit_size = mixer_info.fixed_deposit_size;
mixer_info.leaves.extend(data_points.clone());
MixerGroups::<T>::insert(mixer_id, mixer_info);

Self::deposit_event(Event::Deposit(
mixer_id,
sender,
deposit_size,
num_leaves_before,
data_points,
));

Ok(().into())
}

Expand Down Expand Up @@ -315,6 +341,14 @@ pub mod pallet {
withdraw_proof.mixer_id.into(),
withdraw_proof.nullifier_hash,
)?;

Self::deposit_event(Event::Withdraw(
withdraw_proof.mixer_id,
sender,
recipient,
relayer,
withdraw_proof.cached_root,
));
Ok(().into())
}

Expand Down

0 comments on commit e8b43d2

Please sign in to comment.