Skip to content

Commit

Permalink
fix no default
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Oct 2, 2024
1 parent b1e5387 commit 9f108c3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
3 changes: 1 addition & 2 deletions substrate/frame/assets/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ use frame_benchmarking::v1::{
};
use frame_support::traits::{EnsureOrigin, Get, UnfilteredDispatchable};
use frame_system::RawOrigin as SystemOrigin;
use sp_runtime::traits::Bounded;
use sp_runtime::traits::Hash;
use sp_runtime::traits::{Bounded, Hash};

use crate::Pallet as Assets;

Expand Down
6 changes: 6 additions & 0 deletions substrate/frame/assets/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,16 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub(super) fn do_claim_distribution(
distribution_id: DistributionCounter,
merkle_proof: Vec<u8>,
hashes: u32,
) -> DispatchResult {
let proof =
codec::Decode::decode(&mut &merkle_proof[..]).map_err(|_| Error::<T, I>::BadProof)?;

let expected_hashes = T::VerifyExistenceProof::proof_to_hashes(&proof)
.map_err(|_| Error::<T, I>::BadProof)?;

ensure!(hashes >= expected_hashes, Error::<T, I>::TooManyHashes);

let DistributionInfo { asset_id, merkle_root, active } =
MerklizedDistribution::<T, I>::get(distribution_id).ok_or(Error::<T, I>::Unknown)?;

Expand Down
21 changes: 11 additions & 10 deletions substrate/frame/assets/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ use frame_support::{
Preservation::{Expendable, Preserve},
WithdrawConsequence,
},
BalanceStatus::Reserved, ProofToHashes,
Currency, EnsureOriginWithArg, Incrementable, ReservableCurrency, StoredMap,
BalanceStatus::Reserved,
Currency, EnsureOriginWithArg, Incrementable, ProofToHashes, ReservableCurrency, StoredMap,
VerifyExistenceProof,
},
};
Expand Down Expand Up @@ -299,7 +299,6 @@ pub mod pallet {
type Extra = ();
type CallbackHandle = ();
type WeightInfo = ();
type VerifyExistenceProof = NoTrie<sp_core::H256>;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}
Expand Down Expand Up @@ -408,7 +407,9 @@ pub mod pallet {
type CallbackHandle: AssetsCallback<Self::AssetId, Self::AccountId>;

/// A type used to verify merkle proofs used for distributions.
type VerifyExistenceProof: VerifyExistenceProof<Hash = <Self as frame_system::Config>::Hash> + ProofToHashes<Proof = DistributionProofOf<Self, I>>;
#[pallet::no_default]
type VerifyExistenceProof: VerifyExistenceProof<Hash = Self::Hash>
+ ProofToHashes<Proof = DistributionProofOf<Self, I>>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -736,8 +737,10 @@ pub mod pallet {
DistributionActive,
/// The proof provided could not be verified.
BadProof,
/// The a leaf node was extracted from the proof, but it did not match the expected format.
/// The leaf node was extracted from the proof, but it did not match the expected format.
CannotDecodeLeaf,
/// The proof will require more hashes than expected.
TooManyHashes,
}

#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
Expand Down Expand Up @@ -1884,17 +1887,15 @@ pub mod pallet {
///
/// Weight: `O(P)` where `P` is the size of the merkle proof.
#[pallet::call_index(34)]
#[pallet::weight({
let hashes = T::VerifyExistenceProof::proof_to_hashes(merkle_proof);
T::WeightInfo::trie_hash(hashes)
})]
#[pallet::weight(T::WeightInfo::trie_hash(*hashes))]
pub fn claim_distribution(
origin: OriginFor<T>,
distribution_id: DistributionCounter,
merkle_proof: Vec<u8>,
hashes: u32,
) -> DispatchResult {
ensure_signed(origin)?;
Self::do_claim_distribution(distribution_id, merkle_proof)?;
Self::do_claim_distribution(distribution_id, merkle_proof, hashes)?;
Ok(())
}

Expand Down
26 changes: 22 additions & 4 deletions substrate/frame/assets/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1963,24 +1963,42 @@ fn merklized_distribution_works() {
_,
>(flat_distribution, 6);

let hashes =
<Test as crate::Config>::VerifyExistenceProof::proof_to_hashes(&proof_for_1).unwrap();

// Use this trie root for the distribution
assert_ok!(Assets::mint_distribution(RuntimeOrigin::signed(1), 0, root));

// Now users claim their distributions permissionlessly with a proof.
assert_ok!(Assets::claim_distribution(RuntimeOrigin::signed(1), 0, proof_for_1.encode()));
assert_ok!(Assets::claim_distribution(
RuntimeOrigin::signed(1),
0,
proof_for_1.encode(),
hashes
));
assert_eq!(Assets::balance(0, 1), 1337);

// Other users can claim their tokens.
assert_ok!(Assets::claim_distribution(RuntimeOrigin::signed(55), 0, proof_for_69.encode()));
assert_ok!(Assets::claim_distribution(
RuntimeOrigin::signed(55),
0,
proof_for_69.encode(),
hashes
));
assert_eq!(Assets::balance(0, 69), 69);

// Owner (or anyone) can also distribute on behalf of the other users.
assert_ok!(Assets::claim_distribution(RuntimeOrigin::signed(1), 0, proof_for_6.encode()));
assert_ok!(Assets::claim_distribution(
RuntimeOrigin::signed(1),
0,
proof_for_6.encode(),
hashes
));
assert_eq!(Assets::balance(0, 6), 6);

// You cannot double claim.
assert_noop!(
Assets::claim_distribution(RuntimeOrigin::signed(6), 0, proof_for_6.encode()),
Assets::claim_distribution(RuntimeOrigin::signed(6), 0, proof_for_6.encode(), hashes),
Error::<Test>::AlreadyClaimed
);
});
Expand Down
2 changes: 0 additions & 2 deletions substrate/frame/assets/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,6 @@ pub type DistributionProofOf<T, I> =
pub type DistributionHashOf<T, I> =
<<T as Config<I>>::VerifyExistenceProof as VerifyExistenceProof>::Hash;

pub type HashOf<T> = <T as frame_system::Config>::Hash;

#[derive(Eq, PartialEq, Copy, Clone, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
pub struct DistributionInfo<AssetId, Hash> {
// The asset id we are distributing.
Expand Down

0 comments on commit 9f108c3

Please sign in to comment.