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

BoundedVec for AccountLocks #8580

Merged
merged 12 commits into from
Apr 13, 2021
Merged

Conversation

kianenigma
Copy link
Contributor

Example of using: #8556

@kianenigma kianenigma added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. labels Apr 9, 2021
@@ -859,7 +852,11 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
system::Pallet::<T>::dec_consumers(who);
}
} else {
Locks::<T, I>::insert(who, locks);
let bounded_locks = <BoundedVec<BalanceLock<T::Balance>, T::MaxLocks>>::force_from(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha we need to fix this at some point, but yeah this looks good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What alternative do you have in mind?

I tried to replicate the old code, and the old code was not strict, so I added this force_from.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the way this API is written historically is not strict. ideally something like set_lock would return a result, and we would check that wherever set_lock is used

But that is beyond the scope here.

@shawntabrizi
Copy link
Member

Another example actually doing the check?

@kianenigma kianenigma added the D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit. label Apr 9, 2021
@@ -257,7 +257,7 @@ pub(crate) fn balances(who: &u64) -> (u64, u64) {
}

pub(crate) fn locks(who: &u64) -> Vec<u64> {
Balances::locks(who).iter().map(|l| l.amount).collect::<Vec<u64>>()
Balances::locks(who).inner().iter().map(|l| l.amount).collect::<Vec<u64>>()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be improved, I should be able to use the iter directly defined on BoundedVec

frame/staking/src/tests.rs Outdated Show resolved Hide resolved
frame/staking/src/tests.rs Outdated Show resolved Hide resolved
@kianenigma
Copy link
Contributor Author

Added an example for collective as well. The changes are quite small, lets merge this into the base PR?

proposals.len() <= T::MaxProposals::get() as usize,
Error::<T, I>::TooManyProposals
);
proposals.try_push(proposal_hash).map_err(|_| Error::<T, I>::TooManyProposals)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wonderful

}
impl pallet_balances::Config for Test {
type MaxLocks = ();
type MaxLocks = MaxLocks;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there such a magic trick that () would be a boundless vec?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No in this case it is the opposite. Default for () is 0 so it will be a non-usable vec.

@kianenigma kianenigma merged commit b3e876c into kiz-bounded-vec Apr 13, 2021
@kianenigma kianenigma deleted the kiz-bounded-vec-example branch April 13, 2021 11:48
ghost pushed a commit that referenced this pull request Apr 16, 2021
* prototype for shawn

* Clean and document it

* Add more docs

* Move imports

* Some changes for easier compat.

* revert exmaple pallet

* rename

* BoundedVec for AccountLocks (#8580)

* Example with balances

* Fix tests

* Make it indexable

* fix

* Fix tests

* fix test

* Fix collective as well

* Fix test

* Update frame/support/src/storage/mod.rs

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>

* Repot and add for value

* Add for map and double map

* Final touches.

* Update frame/support/src/storage/bounded_vec.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Add a few more tests

* Add import

Co-authored-by: Peter Goodspeed-Niklaus <coriolinus@users.noreply.github.com>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D2-notlive 💤 PR contains changes in a runtime directory that is not deployed to a chain that requires an audit.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants