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

Verify guard #147

Merged
merged 2 commits into from
Jun 10, 2021
Merged
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
10 changes: 6 additions & 4 deletions pallets/merkle/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ fn setup_tree<T: Config>(caller: T::AccountId, depth: u32) {
let manager_required = true;
let hasher = HashFunction::PoseidonDefault;
let backend = Backend::Bulletproofs(Curve::Curve25519);
let setup = Setup::new(hasher, backend);
<Merkle<T> as Tree<T::AccountId, T::BlockNumber, T::TreeId>>::create_tree(
caller,
manager_required,
hasher,
backend,
setup,
depth as u8,
)
.unwrap();
Expand All @@ -46,8 +46,10 @@ benchmarks! {
}: _(
RawOrigin::Signed(caller),
false,
HashFunction::PoseidonDefault,
Backend::Bulletproofs(Curve::Curve25519),
Setup::new(
HashFunction::PoseidonDefault,
Backend::Bulletproofs(Curve::Curve25519)
),
Some(d as u8)
)
verify {
Expand Down
26 changes: 14 additions & 12 deletions pallets/merkle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub use traits::Tree;
use utils::{
keys::ScalarBytes,
permissions::ensure_admin,
setup::{Backend, Curve, HashFunction, Setup},
setup::{Curve, Setup},
};
use weights::WeightInfo;

Expand Down Expand Up @@ -201,7 +201,7 @@ pub mod pallet {
/// The map of verifying keys for each backend
#[pallet::storage]
#[pallet::getter(fn verifying_keys)]
pub type VerifyingKeys<T: Config> = StorageMap<_, Blake2_128Concat, Backend, Option<Vec<u8>>, ValueQuery>;
pub type VerifyingKeys<T: Config> = StorageMap<_, Blake2_128Concat, (Setup, u8), Option<Vec<u8>>, ValueQuery>;

/// The map of (tree_id, index) to the leaf commitment
#[pallet::storage]
Expand Down Expand Up @@ -299,16 +299,15 @@ pub mod pallet {
pub fn create_tree(
origin: OriginFor<T>,
r_is_mgr: bool,
hasher: HashFunction,
backend: Backend,
setup: Setup,
_depth: Option<u8>,
) -> DispatchResultWithPostInfo {
let sender = ensure_signed(origin)?;
let depth = match _depth {
Some(d) => d,
None => T::MaxTreeDepth::get(),
};
let _ = <Self as Tree<_, _, _>>::create_tree(sender, r_is_mgr, hasher, backend, depth)?;
let _ = <Self as Tree<_, _, _>>::create_tree(sender, r_is_mgr, setup, depth)?;
Ok(().into())
}

Expand Down Expand Up @@ -498,8 +497,7 @@ impl<T: Config> Tree<T::AccountId, T::BlockNumber, T::TreeId> for Pallet<T> {
fn create_tree(
sender: T::AccountId,
is_manager_required: bool,
hasher: HashFunction,
backend: Backend,
setup: Setup,
depth: u8,
) -> Result<T::TreeId, DispatchError> {
ensure!(
Expand All @@ -512,7 +510,6 @@ impl<T: Config> Tree<T::AccountId, T::BlockNumber, T::TreeId> for Pallet<T> {
NextTreeId::<T>::mutate(|id| *id += One::one());

// Setting up the tree
let setup = Setup::new(hasher, backend);
let mtree = MerkleTree::new::<T>(setup, depth).map_err(|_| Error::<T>::Unimplemented)?;
Trees::<T>::insert(tree_id, Some(mtree));

Expand Down Expand Up @@ -554,6 +551,12 @@ impl<T: Config> Tree<T::AccountId, T::BlockNumber, T::TreeId> for Pallet<T> {
let mut tree = Trees::<T>::get(id).ok_or(Error::<T>::TreeDoesntExist)?;
let manager_data = Managers::<T>::get(id).ok_or(Error::<T>::ManagerDoesntExist)?;
// Check if the tree requires extrinsics to be called from a manager

let verifying_key = VerifyingKeys::<T>::get((tree.setup.clone(), tree.depth));
ensure!(
tree.setup.can_verify_with(&verifying_key),
Error::<T>::InvalidVerifierKey
);
ensure!(
Self::is_manager_required(sender.clone(), &manager_data),
Error::<T>::ManagerIsRequired
Expand Down Expand Up @@ -617,9 +620,8 @@ impl<T: Config> Tree<T::AccountId, T::BlockNumber, T::TreeId> for Pallet<T> {
Ok(())
}

fn add_verifying_key(id: T::TreeId, key: Vec<u8>) -> Result<(), DispatchError> {
let tree = Trees::<T>::get(id).ok_or(Error::<T>::TreeDoesntExist)?;
VerifyingKeys::<T>::insert(tree.setup.backend, Some(key));
fn add_verifying_key(setup: Setup, depth: u8, key: Vec<u8>) -> Result<(), DispatchError> {
VerifyingKeys::<T>::insert((setup, depth), Some(key));
Ok(())
}

Expand All @@ -636,7 +638,7 @@ impl<T: Config> Tree<T::AccountId, T::BlockNumber, T::TreeId> for Pallet<T> {
relayer: ScalarBytes,
) -> Result<(), DispatchError> {
let tree = Trees::<T>::get(tree_id).ok_or(Error::<T>::TreeDoesntExist)?;
let verifying_key = VerifyingKeys::<T>::get(&tree.setup.backend);
let verifying_key = VerifyingKeys::<T>::get(&(tree.setup.clone(), tree.depth));
// Ensure that root being checked against is in the cache
let old_roots = Self::cached_roots(block_number, tree_id);
ensure!(old_roots.iter().any(|r| *r == root), Error::<T>::InvalidMerkleRoot);
Expand Down
Loading