solutions/pallets/reward/src/lib.rs

140 lines
3.3 KiB
Rust
Executable File

#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use pallet_zkp_verify::ZKPVerifyInterface;
#[pallet::pallet]
pub struct Pallet<T>(_);
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;
///
type ZKPVerify: ZKPVerifyInterface;
}
// The pallet's runtime storage items.
// https://docs.substrate.io/main-docs/build/runtime-storage/
#[pallet::storage]
#[pallet::getter(fn something)]
// Learn more about declaring storage items:
// https://docs.substrate.io/main-docs/build/runtime-storage/#declaring-storage-items
pub type Something<T> = StorageValue<_, u32>;
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
RewardSuccess { who: T::AccountId },
}
#[pallet::error]
pub enum Error<T> {
RewardFailed,
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::cause_error())]
pub fn verify_proof(
origin: OriginFor<T>,
// vk: Vec<u8>,
vk_alpha_1: Vec<u8>,
vk_alpha_2: Vec<u8>,
vk_beta_1_1: Vec<u8>,
vk_beta_1_2: Vec<u8>,
vk_beta_2_1: Vec<u8>,
vk_beta_2_2: Vec<u8>,
vk_gamma_1_1: Vec<u8>,
vk_gamma_1_2: Vec<u8>,
vk_gamma_2_1: Vec<u8>,
vk_gamma_2_2: Vec<u8>,
vk_delta_1_1: Vec<u8>,
vk_delta_1_2: Vec<u8>,
vk_delta_2_1: Vec<u8>,
vk_delta_2_2: Vec<u8>,
vk_gamma_abc_1_1: Vec<u8>,
vk_gamma_abc_1_2: Vec<u8>,
vk_gamma_abc_2_1: Vec<u8>,
vk_gamma_abc_2_2: Vec<u8>,
inputs: Vec<u8>,
// proof: Vec<u8>,
proof_a_1: Vec<u8>,
proof_a_2: Vec<u8>,
proof_b_1_1: Vec<u8>,
proof_b_1_2: Vec<u8>,
proof_b_2_1: Vec<u8>,
proof_b_2_2: Vec<u8>,
proof_c_1: Vec<u8>,
proof_c_2: Vec<u8>,
) -> DispatchResult {
let who = ensure_signed(origin)?;
let result = T::ZKPVerify::verify_proof(
origin,
vk_alpha_1,
vk_alpha_2,
vk_beta_1_1,
vk_beta_1_2,
vk_beta_2_1,
vk_beta_2_2,
vk_gamma_1_1,
vk_gamma_1_2,
vk_gamma_2_1,
vk_gamma_2_2,
vk_delta_1_1,
vk_delta_1_2,
vk_delta_2_1,
vk_delta_2_2,
vk_gamma_abc_1_1,
vk_gamma_abc_1_2,
vk_gamma_abc_2_1,
vk_gamma_abc_2_2,
inputs,
proof_a_1,
proof_a_2,
proof_b_1_1,
proof_b_1_2,
proof_b_2_1,
proof_b_2_2,
proof_c_1,
proof_c_2,
);
if result {
Self::deposit_event(Event::RewardSuccess { who });
Ok(())
} else {
Err(Error::<T>::RewardFailed.into())
}
}
}
}