Skip to content

Commit

Permalink
Improve On_demand_assigner events (paritytech#4339)
Browse files Browse the repository at this point in the history
title: Improving `on_demand_assigner` emitted events

doc:
  - audience: Rutime User
description: OnDemandOrderPlaced event that is useful for indexers to
save data related to on demand orders. Check [discussion
here](https://substrate.stackexchange.com/questions/11366/ondemandassignmentprovider-ondemandorderplaced-event-was-removed/11389#11389).

Closes paritytech#4254 

crates: [ 'runtime-parachain]

---------

Co-authored-by: Maciej <[email protected]>
  • Loading branch information
2 people authored and TarekkMA committed Aug 2, 2024
1 parent 33f2093 commit a0c4cb3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
38 changes: 24 additions & 14 deletions polkadot/runtime/parachains/src/assigner_on_demand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl QueueStatusType {
fn consume_index(&mut self, removed_index: QueueIndex) {
if removed_index != self.smallest_index {
self.freed_indices.push(removed_index.reverse());
return
return;
}
let mut index = self.smallest_index.0.overflowing_add(1).0;
// Even more to advance?
Expand Down Expand Up @@ -368,10 +368,10 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// An order was placed at some spot price amount.
OnDemandOrderPlaced { para_id: ParaId, spot_price: BalanceOf<T> },
/// The value of the spot traffic multiplier changed.
SpotTrafficSet { traffic: FixedU128 },
/// An order was placed at some spot price amount by orderer ordered_by
OnDemandOrderPlaced { para_id: ParaId, spot_price: BalanceOf<T>, ordered_by: T::AccountId },
/// The value of the spot price has likely changed
SpotPriceSet { spot_price: BalanceOf<T> },
}

#[pallet::error]
Expand Down Expand Up @@ -410,12 +410,11 @@ pub mod pallet {
///
/// Errors:
/// - `InsufficientBalance`: from the Currency implementation
/// - `InvalidParaId`
/// - `QueueFull`
/// - `SpotPriceHigherThanMaxAmount`
///
/// Events:
/// - `SpotOrderPlaced`
/// - `OnDemandOrderPlaced`
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::place_order_allow_death(QueueStatus::<T>::get().size()))]
pub fn place_order_allow_death(
Expand All @@ -437,12 +436,11 @@ pub mod pallet {
///
/// Errors:
/// - `InsufficientBalance`: from the Currency implementation
/// - `InvalidParaId`
/// - `QueueFull`
/// - `SpotPriceHigherThanMaxAmount`
///
/// Events:
/// - `SpotOrderPlaced`
/// - `OnDemandOrderPlaced`
#[pallet::call_index(1)]
#[pallet::weight(<T as Config>::WeightInfo::place_order_keep_alive(QueueStatus::<T>::get().size()))]
pub fn place_order_keep_alive(
Expand Down Expand Up @@ -539,12 +537,11 @@ where
///
/// Errors:
/// - `InsufficientBalance`: from the Currency implementation
/// - `InvalidParaId`
/// - `QueueFull`
/// - `SpotPriceHigherThanMaxAmount`
///
/// Events:
/// - `SpotOrderPlaced`
/// - `OnDemandOrderPlaced`
fn do_place_order(
sender: <T as frame_system::Config>::AccountId,
max_amount: BalanceOf<T>,
Expand Down Expand Up @@ -578,6 +575,12 @@ where
Error::<T>::QueueFull
);
Pallet::<T>::add_on_demand_order(queue_status, para_id, QueuePushDirection::Back);
Pallet::<T>::deposit_event(Event::<T>::OnDemandOrderPlaced {
para_id,
spot_price,
ordered_by: sender,
});

Ok(())
})
}
Expand All @@ -599,7 +602,14 @@ where
// Only update storage on change
if new_traffic != old_traffic {
queue_status.traffic = new_traffic;
Pallet::<T>::deposit_event(Event::<T>::SpotTrafficSet { traffic: new_traffic });

// calculate the new spot price
let spot_price: BalanceOf<T> = new_traffic.saturating_mul_int(
config.scheduler_params.on_demand_base_fee.saturated_into::<BalanceOf<T>>(),
);

// emit the event for updated new price
Pallet::<T>::deposit_event(Event::<T>::SpotPriceSet { spot_price });
}
},
Err(err) => {
Expand Down Expand Up @@ -721,7 +731,7 @@ where
"Decreased affinity for a para that has not been served on a core?"
);
if affinity != Some(0) {
return
return;
}
// No affinity more for entries on this core, free any entries:
//
Expand Down Expand Up @@ -754,7 +764,7 @@ where
} else {
*maybe_affinity = None;
}
return Some(new_count)
return Some(new_count);
} else {
None
}
Expand Down
13 changes: 13 additions & 0 deletions prdoc/pr_4339.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Improving on_demand_assigner emitted events

doc:
- audience: Runtime User
description: |
Registering OnDemandOrderPlaced event that is useful for indexers to save data related to on demand orders. Adds SpotPriceSet as a new event to monitor on-demand spot prices. It updates whenever the price changes due to traffic.

crates:
- name: polkadot-runtime-parachains
bump: minor

0 comments on commit a0c4cb3

Please sign in to comment.