-
Notifications
You must be signed in to change notification settings - Fork 2
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
Subscription execution hook #8
Conversation
use sp_std::marker::PhantomData; | ||
|
||
pub trait WeightInfo { | ||
fn do_execute_subscriptions(itts: u32) -> Weight; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
itts
what an horrible abreviation haha
use the full work as much as you can
#[pallet::event] | ||
// #[pallet::generate_deposit(pub(super) fn deposit_event)] | ||
pub enum Event<T: Config> { | ||
SomethingStored(u32, T::AccountId), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add an event to signal execution of a subscription
src/lib.rs
Outdated
pub fn do_execute_subscriptions(n: T::BlockNumber) -> Weight { | ||
let mut itterations = 0; | ||
|
||
let mut subs = unwrap_or_return!(Subscriptions::<T>::get(), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subs
-> subscriptions
|
||
impl<T: Config> Pallet<T> { | ||
pub fn do_execute_subscriptions(n: T::BlockNumber) -> Weight { | ||
let mut itterations = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only one t
in iterations
} | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn do_execute_subscriptions(n: T::BlockNumber) -> Weight { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
n
is a terrible variable name.
use current_block_number
src/lib.rs
Outdated
return 0 | ||
} | ||
loop { | ||
if let Some((block, (mut sub, account))) = subs.pop() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sub
should be subscription
account
subscriber
block
block_number
pub fn do_execute_subscriptions(n: T::BlockNumber) -> Weight { | ||
let mut itterations = 0; | ||
|
||
let subs = unwrap_or_return!(Subscriptions::<T>::get(n), 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You just did a read, so don't return 0.
Return weight of a read
} | ||
T::WeightInfo::do_execute_subscriptions(itterations) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove the key we used (current_block) from the storage
let new_subs = Subscriptions::<T>::take(new_block).map(|mut subs| { | ||
subs.push((sub.clone(), account.clone())); | ||
subs | ||
}).unwrap_or_else(|| vec!((sub, account))); | ||
Subscriptions::<T>::insert(new_block, new_subs); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have to take it. You can just read it. You are going to replace the value with an insert just after anyhow.
let new_subs = Subscriptions::<T>::take(new_block).map(|mut subs| { | ||
subs.push((sub.clone(), account.clone())); | ||
subs | ||
}).unwrap_or_else(|| vec!((sub, account))); | ||
Subscriptions::<T>::insert(new_block, new_subs); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this you will do a read and a write for each subscription.
You should rather store the value you updated in a on-memory cache, update this cache as many time as you want and and only write them back to storage at the end of the whole loop
closes #6