Remove IncRefError, DecRefError and StoredMapError (#9384)

All of them are a subset of `DispatchError` anyway, no need to have
special errors IMHO.
This commit is contained in:
Bastian Köcher
2021-07-20 10:39:26 +02:00
committed by GitHub
parent 6d9e05cb9a
commit 2b282a9435
9 changed files with 45 additions and 98 deletions
+3 -3
View File
@@ -17,7 +17,7 @@
//! Smaller traits used in FRAME which don't need their own file.
use sp_runtime::traits::{StoredMapError, Block as BlockT};
use sp_runtime::{traits::Block as BlockT, DispatchError};
use sp_arithmetic::traits::AtLeast32Bit;
use crate::dispatch::Parameter;
@@ -157,10 +157,10 @@ pub trait OnKilledAccount<AccountId> {
/// A simple, generic one-parameter event notifier/handler.
pub trait HandleLifetime<T> {
/// An account was created.
fn created(_t: &T) -> Result<(), StoredMapError> { Ok(()) }
fn created(_t: &T) -> Result<(), DispatchError> { Ok(()) }
/// An account was killed.
fn killed(_t: &T) -> Result<(), StoredMapError> { Ok(()) }
fn killed(_t: &T) -> Result<(), DispatchError> { Ok(()) }
}
impl<T> HandleLifetime<T> for () {}
@@ -18,7 +18,7 @@
//! Traits and associated datatypes for managing abstract stored values.
use codec::FullCodec;
use sp_runtime::traits::StoredMapError;
use sp_runtime::DispatchError;
use crate::storage::StorageMap;
use crate::traits::misc::HandleLifetime;
@@ -31,7 +31,7 @@ pub trait StoredMap<K, T: Default> {
/// Maybe mutate the item only if an `Ok` value is returned from `f`. Do nothing if an `Err` is
/// returned. It is removed or reset to default value if it has been mutated to `None`
fn try_mutate_exists<R, E: From<StoredMapError>>(
fn try_mutate_exists<R, E: From<DispatchError>>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> Result<R, E>,
) -> Result<R, E>;
@@ -39,7 +39,7 @@ pub trait StoredMap<K, T: Default> {
// Everything past here has a default implementation.
/// Mutate the item.
fn mutate<R>(k: &K, f: impl FnOnce(&mut T) -> R) -> Result<R, StoredMapError> {
fn mutate<R>(k: &K, f: impl FnOnce(&mut T) -> R) -> Result<R, DispatchError> {
Self::mutate_exists(k, |maybe_account| match maybe_account {
Some(ref mut account) => f(account),
x @ None => {
@@ -57,15 +57,15 @@ pub trait StoredMap<K, T: Default> {
fn mutate_exists<R>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> R,
) -> Result<R, StoredMapError> {
Self::try_mutate_exists(k, |x| -> Result<R, StoredMapError> { Ok(f(x)) })
) -> Result<R, DispatchError> {
Self::try_mutate_exists(k, |x| -> Result<R, DispatchError> { Ok(f(x)) })
}
/// Set the item to something new.
fn insert(k: &K, t: T) -> Result<(), StoredMapError> { Self::mutate(k, |i| *i = t) }
fn insert(k: &K, t: T) -> Result<(), DispatchError> { Self::mutate(k, |i| *i = t) }
/// Remove the item or otherwise replace it with its default value; we don't care which.
fn remove(k: &K) -> Result<(), StoredMapError> { Self::mutate_exists(k, |x| *x = None) }
fn remove(k: &K) -> Result<(), DispatchError> { Self::mutate_exists(k, |x| *x = None) }
}
/// A shim for placing around a storage item in order to use it as a `StoredValue`. Ideally this
@@ -87,27 +87,27 @@ impl<
T: FullCodec + Default,
> StoredMap<K, T> for StorageMapShim<S, L, K, T> {
fn get(k: &K) -> T { S::get(k) }
fn insert(k: &K, t: T) -> Result<(), StoredMapError> {
fn insert(k: &K, t: T) -> Result<(), DispatchError> {
if !S::contains_key(&k) {
L::created(k)?;
}
S::insert(k, t);
Ok(())
}
fn remove(k: &K) -> Result<(), StoredMapError> {
fn remove(k: &K) -> Result<(), DispatchError> {
if S::contains_key(&k) {
L::killed(&k)?;
S::remove(k);
}
Ok(())
}
fn mutate<R>(k: &K, f: impl FnOnce(&mut T) -> R) -> Result<R, StoredMapError> {
fn mutate<R>(k: &K, f: impl FnOnce(&mut T) -> R) -> Result<R, DispatchError> {
if !S::contains_key(&k) {
L::created(k)?;
}
Ok(S::mutate(k, f))
}
fn mutate_exists<R>(k: &K, f: impl FnOnce(&mut Option<T>) -> R) -> Result<R, StoredMapError> {
fn mutate_exists<R>(k: &K, f: impl FnOnce(&mut Option<T>) -> R) -> Result<R, DispatchError> {
S::try_mutate_exists(k, |maybe_value| {
let existed = maybe_value.is_some();
let r = f(maybe_value);
@@ -121,7 +121,7 @@ impl<
Ok(r)
})
}
fn try_mutate_exists<R, E: From<StoredMapError>>(
fn try_mutate_exists<R, E: From<DispatchError>>(
k: &K,
f: impl FnOnce(&mut Option<T>) -> Result<R, E>,
) -> Result<R, E> {