Skip to content
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

Extend Consideration trait #1813

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion substrate/frame/preimage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub mod pallet {
type ManagerOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// A means of providing some cost while data is stored on-chain.
type Consideration: Consideration<Self::AccountId>;
type Consideration: Consideration<Self::AccountId, BalanceOf<Self>>;
}

#[pallet::pallet]
Expand Down
13 changes: 11 additions & 2 deletions substrate/frame/support/src/traits/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,17 @@ where
/// treated as one*. Don't type to duplicate it, and remember to drop it when you're done with
/// it.
#[must_use]
pub trait Consideration<AccountId>: Member + FullCodec + TypeInfo + MaxEncodedLen {
pub trait Consideration<AccountId, Balance>: Member + FullCodec + TypeInfo + MaxEncodedLen {
Copy link
Member

Choose a reason for hiding this comment

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

Gah! No!

Copy link
Member

Choose a reason for hiding this comment

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

Considerations need not have anything to do with balances.

/// Create a ticket for the `new` footprint attributable to `who`. This ticket *must* ultimately
/// be consumed through `update` or `drop` once the footprint changes or is removed.
fn new(who: &AccountId, new: Footprint) -> Result<Self, DispatchError>;

/// Create a ticket for a `new` balance attributable to `who`. This ticket *must* ultimately
/// be consumed through `update` or `drop` once the footprint changes or is removed.
/// This is useful when a new ticket needs to be created with a precise balance, instead of
/// deriving it from a footprint
Comment on lines +216 to +219
Copy link
Contributor

Choose a reason for hiding this comment

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

The doc comment still mentions a footprint

fn new_from_exact(who: &AccountId, new: Balance) -> Result<Self, DispatchError>;

/// Optionally consume an old ticket and alter the footprint, enforcing the new cost to `who`
/// and returning the new ticket (or an error if there was an issue).
///
Expand All @@ -232,10 +238,13 @@ pub trait Consideration<AccountId>: Member + FullCodec + TypeInfo + MaxEncodedLe
}
}

impl<A> Consideration<A> for () {
impl<A, B> Consideration<A, B> for () {
fn new(_: &A, _: Footprint) -> Result<Self, DispatchError> {
Ok(())
}
fn new_from_exact(_: &A, _: B) -> Result<Self, DispatchError> {
Ok(())
}
fn update(self, _: &A, _: Footprint) -> Result<(), DispatchError> {
Ok(())
}
Expand Down
24 changes: 20 additions & 4 deletions substrate/frame/support/src/traits/tokens/fungible/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,17 @@ impl<
F: 'static + MutateFreeze<A>,
R: 'static + Get<F::Id>,
D: 'static + Convert<Footprint, F::Balance>,
> Consideration<A> for FreezeConsideration<A, F, R, D>
> Consideration<A, F::Balance> for FreezeConsideration<A, F, R, D>
{
fn new(who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
let new = D::convert(footprint);
F::increase_frozen(&R::get(), who, new)?;
Ok(Self(new, PhantomData))
}
fn new_from_exact(who: &A, new: F::Balance) -> Result<Self, DispatchError> {
F::increase_frozen(&R::get(), who, new)?;
Ok(Self(new, PhantomData))
}
fn update(self, who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
let new = D::convert(footprint);
if self.0 > new {
Expand Down Expand Up @@ -139,13 +143,17 @@ impl<
F: 'static + MutateHold<A>,
R: 'static + Get<F::Reason>,
D: 'static + Convert<Footprint, F::Balance>,
> Consideration<A> for HoldConsideration<A, F, R, D>
> Consideration<A, F::Balance> for HoldConsideration<A, F, R, D>
{
fn new(who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
let new = D::convert(footprint);
F::hold(&R::get(), who, new)?;
Ok(Self(new, PhantomData))
}
fn new_from_exact(who: &A, new: F::Balance) -> Result<Self, DispatchError> {
F::hold(&R::get(), who, new)?;
Ok(Self(new, PhantomData))
}
fn update(self, who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
let new = D::convert(footprint);
if self.0 > new {
Expand Down Expand Up @@ -188,12 +196,16 @@ impl<
Fx: 'static + MutateFreeze<A>,
Rx: 'static + Get<Fx::Id>,
D: 'static + Convert<Footprint, Fx::Balance>,
> Consideration<A> for LoneFreezeConsideration<A, Fx, Rx, D>
> Consideration<A, Fx::Balance> for LoneFreezeConsideration<A, Fx, Rx, D>
{
fn new(who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
ensure!(Fx::balance_frozen(&Rx::get(), who).is_zero(), DispatchError::Unavailable);
Fx::set_frozen(&Rx::get(), who, D::convert(footprint), Polite).map(|_| Self(PhantomData))
}
fn new_from_exact(who: &A, new: Fx::Balance) -> Result<Self, DispatchError> {
ensure!(Fx::balance_frozen(&Rx::get(), who).is_zero(), DispatchError::Unavailable);
Fx::set_frozen(&Rx::get(), who, new, Polite).map(|_| Self(PhantomData))
}
fn update(self, who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
Fx::set_frozen(&Rx::get(), who, D::convert(footprint), Polite).map(|_| Self(PhantomData))
}
Expand Down Expand Up @@ -227,12 +239,16 @@ impl<
F: 'static + MutateHold<A>,
R: 'static + Get<F::Reason>,
D: 'static + Convert<Footprint, F::Balance>,
> Consideration<A> for LoneHoldConsideration<A, F, R, D>
> Consideration<A, F::Balance> for LoneHoldConsideration<A, F, R, D>
{
fn new(who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
ensure!(F::balance_on_hold(&R::get(), who).is_zero(), DispatchError::Unavailable);
F::set_on_hold(&R::get(), who, D::convert(footprint)).map(|_| Self(PhantomData))
}
fn new_from_exact(who: &A, new: F::Balance) -> Result<Self, DispatchError> {
ensure!(F::balance_on_hold(&R::get(), who).is_zero(), DispatchError::Unavailable);
F::set_on_hold(&R::get(), who, new).map(|_| Self(PhantomData))
}
fn update(self, who: &A, footprint: Footprint) -> Result<Self, DispatchError> {
F::set_on_hold(&R::get(), who, D::convert(footprint)).map(|_| Self(PhantomData))
}
Expand Down
Loading