Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Nov 10, 2024
1 parent af17284 commit 3815ef2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 34 deletions.
5 changes: 3 additions & 2 deletions custom-pallets/anonymous-account/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
17 changes: 17 additions & 0 deletions custom-pallets/anonymous-account/src/extras.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use super::*;

use sp_io::hashing::blake2_256;
use sp_runtime::AccountId32;

impl<T: Config> Pallet<T> {
pub fn update_hash_incrementally(current_hash: [u8; 32], account_id: Vec<u8>) -> [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(&current_hash);
input_data.extend_from_slice(account_id.as_ref());

// Recalculate the hash with the new account ID
blake2_256(&input_data)
}
}
32 changes: 23 additions & 9 deletions custom-pallets/anonymous-account/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -55,6 +57,15 @@ pub mod pallet {
pub type HashByRange<T: Config> =
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<T: Config> = 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]
Expand All @@ -72,28 +83,31 @@ 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.
// 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> {
/// 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<T>, something: u32) -> DispatchResult {
#[pallet::weight(0)]
pub fn calculate_hash(origin: OriginFor<T>) -> 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.
<Something<T>>::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(())
}
Expand Down
36 changes: 13 additions & 23 deletions custom-pallets/anonymous-account/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::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::<Test>::NoneValue
);
});
}

0 comments on commit 3815ef2

Please sign in to comment.