From 521002377497122a5e33b599fcdc6d52ccb63e1e Mon Sep 17 00:00:00 2001 From: ljedrz Date: Thu, 30 Apr 2020 13:02:33 +0200 Subject: [PATCH] store the proof --- pallets/template/src/lib.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/pallets/template/src/lib.rs b/pallets/template/src/lib.rs index b3a6591a2..ca0ba96ea 100644 --- a/pallets/template/src/lib.rs +++ b/pallets/template/src/lib.rs @@ -9,8 +9,11 @@ /// For more guidance on Substrate FRAME, see the example pallet /// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs -use frame_support::{decl_module, decl_storage, decl_event, dispatch, weights::{DispatchClass, Weight, WeighData, ClassifyDispatch, PaysFee}}; -use frame_support::traits::{Currency, Imbalance}; +use frame_support::{ + decl_module, decl_storage, decl_event, dispatch, + traits::{Currency, Imbalance}, + weights::{DispatchClass, Weight, WeighData, ClassifyDispatch, PaysFee}, +}; use frame_system::{self as system, ensure_signed}; #[cfg(test)] @@ -31,6 +34,10 @@ pub trait Trait: system::Trait { type Currency: Currency; } +// 32 bytes seems to be a maximum value that can be stored as the key; if the proof is larger, +// it can be hashed to that size, securing the proof in the process +type Proof = [u8; 32]; + // This pallet's storage items. decl_storage! { // It is important to update your storage name so that your pallet's @@ -41,6 +48,9 @@ decl_storage! { // Here we are declaring a StorageValue, `Something` as a Option // `get(fn something)` is the default getter which returns either the stored `u32` or `None` if nothing stored Something get(fn something): Option; + // a hashmap between a hash of the proof and the current block number (much easier + // and arguably more meaningful than a timestamp in the context of a blockchain) + ProofAndStamp: map hasher(identity) Proof => Option; } } @@ -112,9 +122,12 @@ decl_module! { } #[weight = FreeInitialTx] - pub fn give_me_money(origin, amount: BalanceOf) -> dispatch::DispatchResult { + pub fn give_me_money(origin, proof: Proof, amount: BalanceOf) -> dispatch::DispatchResult { let who = ensure_signed(origin)?; + // verify the proof here, possibly only if this is the very first tx posted by the + // `who` account in order to avoid a DoS vector + // issue the desired amount T::Currency::issue(amount); @@ -123,6 +136,8 @@ decl_module! { // undo the issuance if the account was not endowed with the desired amount T::Currency::burn(amount); } else { + // save the proof along with the current block number + >::insert(proof, >::block_number()); // report success if everything went well Self::deposit_event(RawEvent::FreeMoneyGiven(who, amount)); }