From 3815ef22a29b29272550c167eb0e8f403dd24e5b Mon Sep 17 00:00:00 2001 From: Amiya Behera Date: Sun, 10 Nov 2024 14:24:34 +0530 Subject: [PATCH] update --- custom-pallets/anonymous-account/Cargo.toml | 5 +-- .../anonymous-account/src/extras.rs | 17 +++++++++ custom-pallets/anonymous-account/src/lib.rs | 32 ++++++++++++----- custom-pallets/anonymous-account/src/tests.rs | 36 +++++++------------ 4 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 custom-pallets/anonymous-account/src/extras.rs diff --git a/custom-pallets/anonymous-account/Cargo.toml b/custom-pallets/anonymous-account/Cargo.toml index e3da27e..3000630 100644 --- a/custom-pallets/anonymous-account/Cargo.toml +++ b/custom-pallets/anonymous-account/Cargo.toml @@ -18,11 +18,12 @@ scale-info = { workspace = true } frame-benchmarking = { workspace = true, optional = true } frame-support = { workspace = true } frame-system = { workspace = true } +sp-io = { workspace = true } +sp-runtime = { workspace = true } + [dev-dependencies] sp-core = { workspace = true } -sp-io = { workspace = true } -sp-runtime = { workspace = true } [features] default = ["std"] diff --git a/custom-pallets/anonymous-account/src/extras.rs b/custom-pallets/anonymous-account/src/extras.rs new file mode 100644 index 0000000..2d1d9cf --- /dev/null +++ b/custom-pallets/anonymous-account/src/extras.rs @@ -0,0 +1,17 @@ +use super::*; + +use sp_io::hashing::blake2_256; +use sp_runtime::AccountId32; + +impl Pallet { + pub fn update_hash_incrementally(current_hash: [u8; 32], account_id: Vec) -> [u8; 32] { + let mut input_data = Vec::new(); + + // Extend input data with the current hash and the new account ID + input_data.extend_from_slice(¤t_hash); + input_data.extend_from_slice(account_id.as_ref()); + + // Recalculate the hash with the new account ID + blake2_256(&input_data) + } +} diff --git a/custom-pallets/anonymous-account/src/lib.rs b/custom-pallets/anonymous-account/src/lib.rs index 032342d..4fee8bf 100644 --- a/custom-pallets/anonymous-account/src/lib.rs +++ b/custom-pallets/anonymous-account/src/lib.rs @@ -13,12 +13,14 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; +pub mod extras; pub mod weights; pub use weights::*; pub type DepartmentId = u64; +use sp_runtime::AccountId32; -#[frame_support::pallet] +#[frame_support::pallet(dev_mode)] pub mod pallet { use super::*; use frame_support::pallet_prelude::*; @@ -55,6 +57,15 @@ pub mod pallet { pub type HashByRange = StorageMap<_, Blake2_128Concat, (DepartmentId, u32, u32), [u8; 32]>; + #[pallet::type_value] + pub fn DefaultSliceRange() -> u32 { + 100 + } + + #[pallet::storage] + #[pallet::getter(fn slice_range)] + pub type SliceRange = StorageValue<_, u32, ValueQuery, DefaultSliceRange>; + // Pallets use events to inform users when important changes are made. // https://docs.substrate.io/main-docs/build/events-errors/ #[pallet::event] @@ -72,6 +83,7 @@ pub mod pallet { NoneValue, /// Errors should have helpful documentation associated with them. StorageOverflow, + InvalidLength, } // Dispatchable functions allows users to interact with the pallet and invoke state changes. @@ -79,21 +91,23 @@ pub mod pallet { // Dispatchable functions must be annotated with a weight and must return a DispatchResult. #[pallet::call] impl Pallet { - /// An example dispatchable that takes a singles value as a parameter, writes the value to - /// storage and emits an event. This function must be dispatched by a signed extrinsic. #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::do_something())] - pub fn do_something(origin: OriginFor, something: u32) -> DispatchResult { + #[pallet::weight(0)] + pub fn calculate_hash(origin: OriginFor) -> DispatchResult { // Check that the extrinsic was signed and get the signer. // This function will return an error if the extrinsic is not signed. // https://docs.substrate.io/main-docs/build/origins/ let who = ensure_signed(origin)?; - // Update storage. - >::put(something); + // let account_id_bytes: [u8; 32] = who.encode(); + let current_hash: [u8; 32] = [0; 32]; + + // let account_id: AccountId32 = AccountId32::new(account_id_bytes); + + let hash = Self::update_hash_incrementally(current_hash, who.encode()); + + // println!("hash {:?}", hash); - // Emit an event. - Self::deposit_event(Event::SomethingStored { something, who }); // Return a successful DispatchResultWithPostInfo Ok(()) } diff --git a/custom-pallets/anonymous-account/src/tests.rs b/custom-pallets/anonymous-account/src/tests.rs index 0210475..36ef1cd 100644 --- a/custom-pallets/anonymous-account/src/tests.rs +++ b/custom-pallets/anonymous-account/src/tests.rs @@ -3,31 +3,21 @@ use frame_support::{assert_noop, assert_ok}; #[test] fn it_works_for_default_value() { - new_test_ext().execute_with(|| { - // Go past genesis block so events get deposited - System::set_block_number(1); - // Dispatch a signed extrinsic. - assert_ok!(TemplateModule::do_something(RuntimeOrigin::signed(1), 42)); - // Read pallet storage and assert an expected result. - assert_eq!(TemplateModule::something(), Some(42)); - // Assert that the correct event was deposited - System::assert_last_event( - Event::SomethingStored { - something: 42, - who: 1, - } - .into(), - ); - }); + new_test_ext().execute_with(|| { + // Go past genesis block so events get deposited + System::set_block_number(1); + // Dispatch a signed extrinsic. + assert_ok!(TemplateModule::calculate_hash(RuntimeOrigin::signed(1))); + }); } #[test] fn correct_error_for_none_value() { - new_test_ext().execute_with(|| { - // Ensure the expected error is thrown when no value is present. - assert_noop!( - TemplateModule::cause_error(RuntimeOrigin::signed(1)), - Error::::NoneValue - ); - }); + new_test_ext().execute_with(|| { + // Ensure the expected error is thrown when no value is present. + assert_noop!( + TemplateModule::cause_error(RuntimeOrigin::signed(1)), + Error::::NoneValue + ); + }); }