Skip to content

Commit

Permalink
take out deprecated extrinsic (#648)
Browse files Browse the repository at this point in the history
Authored-by: @ETeissonniere
  • Loading branch information
ETeissonniere committed Aug 2, 2022
1 parent a8aa73b commit 33b24c0
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 147 deletions.
28 changes: 5 additions & 23 deletions pallets/allocations/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ pub type MaxMembers = ConstU32<10>;

const SEED: u32 = 0;

pub struct BenchmarkConfig<T: Config> {
grantee: T::AccountId,
oracle: T::AccountId,
}

fn make_benchmark_config<T: Config>() -> BenchmarkConfig<T> {
let grantee: T::AccountId = account("grantee", 0, SEED);
let oracle: T::AccountId = account("oracle", 0, SEED);

let mut members = <BenchmarkOracles<T>>::get();
assert!(members.try_push(oracle.clone()).is_ok());
<BenchmarkOracles<T>>::put(&members);

BenchmarkConfig { grantee, oracle }
}

fn make_batch<T: Config>(b: u32) -> BoundedVec<(T::AccountId, BalanceOf<T>), T::MaxAllocs> {
let mut ret = BoundedVec::with_bounded_capacity(b as usize);

Expand All @@ -60,17 +44,15 @@ fn make_batch<T: Config>(b: u32) -> BoundedVec<(T::AccountId, BalanceOf<T>), T::
}

benchmarks! {
allocate {
let config = make_benchmark_config::<T>();
}: _(RawOrigin::Signed(config.oracle.clone()), config.grantee.clone(), T::ExistentialDeposit::get() * 10u32.into(), vec![])

batch {
let b in 1..T::MaxAllocs::get();

let config = make_benchmark_config::<T>();
let batch_arg = make_batch::<T>(b);

}: _(RawOrigin::Signed(config.oracle.clone()), batch_arg)
let oracle: T::AccountId = account("oracle", 0, SEED);
let mut members = <BenchmarkOracles<T>>::get();
assert!(members.try_push(oracle.clone()).is_ok());
<BenchmarkOracles<T>>::put(&members);
}: _(RawOrigin::Signed(oracle), batch_arg)

impl_benchmark_test_suite!(
Allocations,
Expand Down
21 changes: 0 additions & 21 deletions pallets/allocations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,27 +185,6 @@ pub mod pallet {

Ok(Pays::No.into())
}

/// Can only be called by an oracle, trigger a token mint and dispatch to
/// `amount`, minus protocol fees
#[pallet::weight(
<T as pallet::Config>::WeightInfo::allocate()
)]
// we add the `transactional` modifier here in the event that one of the
// transfers fail. the code itself should already prevent this but we add
// this as an additional guarantee.
#[deprecated(note = "allocate is sub-optimized and chain heavy")]
pub fn allocate(
origin: OriginFor<T>,
to: T::AccountId,
amount: BalanceOf<T>,
_proof: Vec<u8>,
) -> DispatchResultWithPostInfo {
let mut vec = BoundedVec::with_max_capacity();
vec.try_push((to, amount))
.expect("shouldn't panic because we have enough capacity");
Pallet::<T>::batch(origin, vec)
}
}

#[pallet::error]
Expand Down
104 changes: 1 addition & 103 deletions pallets/allocations/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,106 +267,4 @@ fn no_issuance() {
Errors::BatchEmpty
);
})
}

mod deprecated_extrinsic {
use super::*;

#[test]
fn non_oracle_can_not_trigger_allocation() {
new_test_ext().execute_with(|| {
assert_noop!(
Allocations::allocate(Origin::signed(Hacker::get()), Grantee::get(), 50, Vec::new(),),
Errors::OracleAccessDenied
);
})
}

#[test]
fn oracle_does_not_pay_fees() {
new_test_ext().execute_with(|| {
assert_eq!(
Allocations::allocate(Origin::signed(Oracle::get()), Grantee::get(), 50, Vec::new(),),
Ok(Pays::No.into())
);
})
}

#[test]
fn oracle_triggers_allocation() {
new_test_ext().execute_with(|| {
assert_eq!(Allocations::is_oracle(Oracle::get()), true);

assert_ok!(Allocations::allocate(
Origin::signed(Oracle::get()),
Grantee::get(),
50,
Vec::new(),
));
})
}

#[test]
fn hacker_triggers_zero_allocation() {
new_test_ext().execute_with(|| {
assert_noop!(
Allocations::allocate(Origin::signed(Hacker::get()), Grantee::get(), 0, Vec::new(),),
Errors::OracleAccessDenied
);
})
}

#[test]
fn allocate_the_right_amount_of_coins_to_everyone() {
new_test_ext().execute_with(|| {
assert_ok!(Allocations::allocate(
Origin::signed(Oracle::get()),
Grantee::get(),
50,
Vec::new(),
));

assert_eq!(Balances::free_balance(Grantee::get()), 45);
assert_eq!(Balances::free_balance(Receiver::get()), 5);
})
}

#[test]
fn error_if_too_small_for_existential_deposit() {
new_test_ext().execute_with(|| {
// grant smaller than deposit
assert_noop!(
Allocations::allocate(Origin::signed(Oracle::get()), Grantee::get(), 1, Vec::new()),
Errors::DoesNotSatisfyExistentialDeposit,
);

// grant satisfy deposit but would not be enough for both protocol and user
assert_noop!(
Allocations::allocate(Origin::signed(Oracle::get()), Grantee::get(), 2, Vec::new()),
Errors::DoesNotSatisfyExistentialDeposit,
);
assert_noop!(
Allocations::allocate(Origin::signed(Oracle::get()), Grantee::get(), 3, Vec::new()),
Errors::DoesNotSatisfyExistentialDeposit,
);

assert_eq!(Balances::free_balance(Grantee::get()), 0);
assert_eq!(Balances::free_balance(Receiver::get()), 0);
})
}

#[test]
fn can_not_allocate_more_coins_than_max() {
new_test_ext().execute_with(|| {
assert_noop!(
Allocations::allocate(
Origin::signed(Oracle::get()),
Grantee::get(),
CoinsLimit::get() + 1,
Vec::new(),
),
Errors::TooManyCoinsToAllocate
);
})
}
}
}

0 comments on commit 33b24c0

Please sign in to comment.