From 40a8152f69fb8c0b1cc66a2e9d2b816fcff3a1bd Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Wed, 25 Jun 2025 18:17:14 +0300 Subject: [PATCH] rustfmt the ghost-claims pallet Signed-off-by: Uncle Stinky --- pallets/claims/Cargo.toml | 2 +- pallets/claims/src/benchmarking.rs | 39 +++---- pallets/claims/src/lib.rs | 164 ++++++++++++++++------------- pallets/claims/src/mock.rs | 148 ++++++++++++++++++-------- pallets/claims/src/secp_utils.rs | 12 +-- pallets/claims/src/tests.rs | 145 +++++++++++++------------ 6 files changed, 302 insertions(+), 208 deletions(-) diff --git a/pallets/claims/Cargo.toml b/pallets/claims/Cargo.toml index e2ac8c3..037c685 100644 --- a/pallets/claims/Cargo.toml +++ b/pallets/claims/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-claims" -version = "0.2.3" +version = "0.2.4" description = "Ghost balance and rank claims based on EVM actions" license.workspace = true authors.workspace = true diff --git a/pallets/claims/src/benchmarking.rs b/pallets/claims/src/benchmarking.rs index 0ee7bb1..f5a60a9 100644 --- a/pallets/claims/src/benchmarking.rs +++ b/pallets/claims/src/benchmarking.rs @@ -6,14 +6,14 @@ use frame_benchmarking::v2::*; #[instance_benchmarks] mod benchmarks { use super::*; - use pallet_ranked_collective::Pallet as Club; use frame_support::dispatch::RawOrigin; + use pallet_ranked_collective::Pallet as Club; #[benchmark] fn claim() { let i = 1337u32; - let ethereum_secret_key = libsecp256k1::SecretKey::parse( - &keccak_256(&i.to_le_bytes())).unwrap(); + let ethereum_secret_key = + libsecp256k1::SecretKey::parse(&keccak_256(&i.to_le_bytes())).unwrap(); let eth_address = crate::secp_utils::eth(ðereum_secret_key); let balance = CurrencyOf::::minimum_balance(); @@ -22,23 +22,30 @@ mod benchmarks { Total::::put(balance); let pseudo_rank = 5u16; - let _ = Club::::do_add_member_to_rank( - pseudo_account.clone(), - pseudo_rank, - false, - ); + let _ = Club::::do_add_member_to_rank(pseudo_account.clone(), pseudo_rank, false); let user_account: T::AccountId = account("user", i, 0); - let signature = crate::secp_utils::sig::(ðereum_secret_key, &user_account.encode()); - + let signature = + crate::secp_utils::sig::(ðereum_secret_key, &user_account.encode()); + let prev_balance = CurrencyOf::::free_balance(&user_account); let prev_rank = Club::::rank_of(&user_account); #[extrinsic_call] - claim(RawOrigin::Signed(user_account.clone()), eth_address, signature); + claim( + RawOrigin::Signed(user_account.clone()), + eth_address, + signature, + ); - assert_eq!(CurrencyOf::::free_balance(&user_account), prev_balance + balance); - assert_eq!(CurrencyOf::::free_balance(&pseudo_account), balance - balance); + assert_eq!( + CurrencyOf::::free_balance(&user_account), + prev_balance + balance + ); + assert_eq!( + CurrencyOf::::free_balance(&pseudo_account), + balance - balance + ); let rank = match prev_rank { Some(current_rank) if pseudo_rank <= current_rank => Some(current_rank), @@ -48,9 +55,5 @@ mod benchmarks { assert_eq!(Club::::rank_of(&pseudo_account), None); } - impl_benchmark_test_suite!( - Pallet, - crate::mock::new_test_ext(), - crate::mock::Test, - ); + impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test,); } diff --git a/pallets/claims/src/lib.rs b/pallets/claims/src/lib.rs index 862244d..a922a23 100644 --- a/pallets/claims/src/lib.rs +++ b/pallets/claims/src/lib.rs @@ -1,19 +1,20 @@ #![cfg_attr(not(feature = "std"), no_std)] use frame_support::{ - ensure, pallet_prelude::*, + ensure, + pallet_prelude::*, traits::{ - Currency, ExistenceRequirement, Get, RankedMembers, - RankedMembersSwapHandler, VestingSchedule, + Currency, ExistenceRequirement, Get, RankedMembers, RankedMembersSwapHandler, + VestingSchedule, }, DefaultNoBound, }; use frame_system::pallet_prelude::*; -use serde::{self, Deserialize, Deserializer, Serialize, Serializer}; pub use pallet::*; +use serde::{self, Deserialize, Deserializer, Serialize, Serializer}; use sp_io::{crypto::secp256k1_ecdsa_recover, hashing::keccak_256}; -use sp_runtime::traits::{CheckedSub, CheckedDiv, BlockNumberProvider}; +use sp_runtime::traits::{BlockNumberProvider, CheckedDiv, CheckedSub}; use sp_std::prelude::*; extern crate alloc; @@ -23,10 +24,10 @@ use alloc::{format, string::String}; mod weights; pub use crate::weights::WeightInfo; -mod tests; -mod mock; mod benchmarking; +mod mock; mod secp_utils; +mod tests; /// An ethereum address (i.e. 20 bytes, used to represent an Ethereum account). /// @@ -56,7 +57,7 @@ impl<'de> Deserialize<'de> for EthereumAddress { Err(serde::de::Error::custom( "Bad length of Ethereum address (should be 42 including `0x`)", ))?; - } + } let raw: Vec = rustc_hex::FromHex::from_hex(s) .map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?; let mut r = Self::default(); @@ -81,15 +82,14 @@ impl sp_std::fmt::Debug for EcdsaSignature { } type CurrencyOf = <>::VestingSchedule as VestingSchedule< - ::AccountId + ::AccountId, >>::Currency; -type BalanceOf = as Currency< - ::AccountId> ->::Balance; +type BalanceOf = + as Currency<::AccountId>>::Balance; -type RankOf = as RankedMembers>::Rank; -type AccountIdOf = as RankedMembers>::AccountId; +type RankOf = as RankedMembers>::Rank; +type AccountIdOf = as RankedMembers>::AccountId; #[frame_support::pallet] pub mod pallet { @@ -100,8 +100,11 @@ pub mod pallet { pub struct Pallet(_); #[pallet::config] - pub trait Config: frame_system::Config + pallet_ranked_collective::Config { - type RuntimeEvent: From> + IsType<::RuntimeEvent>; + pub trait Config: + frame_system::Config + pallet_ranked_collective::Config + { + type RuntimeEvent: From> + + IsType<::RuntimeEvent>; type VestingSchedule: VestingSchedule>; type BlockNumberProvider: BlockNumberProvider>; @@ -118,13 +121,13 @@ pub mod pallet { type WeightInfo: WeightInfo; } - + #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event, I: 'static = ()> { Claimed { - receiver: T::AccountId, - donor: T::AccountId, + receiver: T::AccountId, + donor: T::AccountId, amount: BalanceOf, rank: Option>, }, @@ -159,12 +162,16 @@ pub mod pallet { .map(|(account_id, rank)| { assert!( pallet_ranked_collective::Pallet::::do_add_member_to_rank( - account_id.clone(), *rank, false).is_ok(), - "error during adding and promotion" + account_id.clone(), + *rank, + false + ) + .is_ok(), + "error during adding and promotion" ); account_id }) - .collect(); + .collect(); assert!( self.members_and_ranks.len() == cult_accounts.len(), @@ -186,13 +193,13 @@ pub mod pallet { ) -> DispatchResult { let who = ensure_signed(origin)?; let data = who.using_encoded(to_ascii_hex); - let recovered_address = Self::recover_ethereum_address( - ðereum_signature, - &data, - ).ok_or(Error::::InvalidEthereumSignature)?; + let recovered_address = Self::recover_ethereum_address(ðereum_signature, &data) + .ok_or(Error::::InvalidEthereumSignature)?; - ensure!(recovered_address == ethereum_address, - Error::::InvalidEthereumAddress); + ensure!( + recovered_address == ethereum_address, + Error::::InvalidEthereumAddress + ); Self::do_claim(who, ethereum_address) } @@ -200,37 +207,37 @@ pub mod pallet { } fn to_ascii_hex(data: &[u8]) -> Vec { - let mut r = Vec::with_capacity(data.len() * 2); - let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n }); - for &b in data.iter() { - push_nibble(b / 16); - push_nibble(b % 16); - } - r + let mut r = Vec::with_capacity(data.len() * 2); + let mut push_nibble = |n| r.push(if n < 10 { b'0' + n } else { b'a' - 10 + n }); + for &b in data.iter() { + push_nibble(b / 16); + push_nibble(b % 16); + } + r } impl, I: 'static> Pallet { fn ethereum_signable_message(what: &[u8]) -> Vec { let prefix = T::Prefix::get(); - let mut l = prefix.len() + what.len(); - let mut rev = Vec::new(); - while l > 0 { - rev.push(b'0' + (l % 10) as u8); - l /= 10; - } - let mut v = b"\x19Ethereum Signed Message:\n".to_vec(); - v.extend(rev.into_iter().rev()); - v.extend_from_slice(prefix); - v.extend_from_slice(what); - v + let mut l = prefix.len() + what.len(); + let mut rev = Vec::new(); + while l > 0 { + rev.push(b'0' + (l % 10) as u8); + l /= 10; + } + let mut v = b"\x19Ethereum Signed Message:\n".to_vec(); + v.extend(rev.into_iter().rev()); + v.extend_from_slice(prefix); + v.extend_from_slice(what); + v } fn recover_ethereum_address(s: &EcdsaSignature, what: &[u8]) -> Option { let msg = keccak_256(&Self::ethereum_signable_message(what)); - let mut res = EthereumAddress::default(); - res.0 - .copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]); - Some(res) + let mut res = EthereumAddress::default(); + res.0 + .copy_from_slice(&keccak_256(&secp256k1_ecdsa_recover(&s.0, &msg).ok()?[..])[12..]); + Some(res) } fn into_account_id(address: EthereumAddress) -> Result { @@ -243,21 +250,24 @@ impl, I: 'static> Pallet { } fn do_claim(receiver: T::AccountId, ethereum_address: EthereumAddress) -> DispatchResult { - let donor = Self::into_account_id(ethereum_address).ok() + let donor = Self::into_account_id(ethereum_address) + .ok() .ok_or(Error::::AddressDecodingFailed)?; let balance_due = CurrencyOf::::free_balance(&donor); - ensure!(balance_due >= CurrencyOf::::minimum_balance(), - Error::::NoBalanceToClaim); + ensure!( + balance_due >= CurrencyOf::::minimum_balance(), + Error::::NoBalanceToClaim + ); let new_total = Total::::get() .checked_sub(&balance_due) .ok_or(Error::::PotUnderflow)?; CurrencyOf::::transfer( - &donor, - &receiver, - balance_due, + &donor, + &receiver, + balance_due, ExistenceRequirement::AllowDeath, )?; @@ -273,28 +283,40 @@ impl, I: 'static> Pallet { .ok_or(Error::::ArithmeticError)?; T::VestingSchedule::add_vesting_schedule( - &receiver, + &receiver, vesting_balance, per_block_balance, T::BlockNumberProvider::current_block_number(), )?; } - let rank = if let Some(rank) = as RankedMembers>::rank_of(&donor) { + let rank = if let Some(rank) = + as RankedMembers>::rank_of(&donor) + { pallet_ranked_collective::Pallet::::do_remove_member_from_rank(&donor, rank)?; - let new_rank = match as RankedMembers>::rank_of(&receiver) { - Some(current_rank) if current_rank >= rank => current_rank, - Some(current_rank) if current_rank < rank => { - for _ in 0..rank - current_rank { - pallet_ranked_collective::Pallet::::do_promote_member(receiver.clone(), None, false)?; + let new_rank = + match as RankedMembers>::rank_of(&receiver) + { + Some(current_rank) if current_rank >= rank => current_rank, + Some(current_rank) if current_rank < rank => { + for _ in 0..rank - current_rank { + pallet_ranked_collective::Pallet::::do_promote_member( + receiver.clone(), + None, + false, + )?; + } + rank } - rank - }, - _ => { - pallet_ranked_collective::Pallet::::do_add_member_to_rank(receiver.clone(), rank, false)?; - rank - }, - }; + _ => { + pallet_ranked_collective::Pallet::::do_add_member_to_rank( + receiver.clone(), + rank, + false, + )?; + rank + } + }; >::MemberSwappedHandler::swapped(&donor, &receiver, new_rank); Some(new_rank) } else { @@ -303,7 +325,7 @@ impl, I: 'static> Pallet { Total::::put(new_total); Self::deposit_event(Event::::Claimed { - receiver, + receiver, donor, amount: balance_due, rank, diff --git a/pallets/claims/src/mock.rs b/pallets/claims/src/mock.rs index c61fe56..c5ecf1f 100644 --- a/pallets/claims/src/mock.rs +++ b/pallets/claims/src/mock.rs @@ -4,51 +4,113 @@ use super::*; pub use crate as ghost_claims; use frame_support::{ - parameter_types, derive_impl, - traits::{PollStatus, Polling, WithdrawReasons, ConstU16, ConstU64}, + derive_impl, parameter_types, + traits::{ConstU16, ConstU64, PollStatus, Polling, WithdrawReasons}, }; use frame_system::EnsureRootWithSuccess; -use sp_runtime::{BuildStorage, traits::Convert}; -pub use pallet_ranked_collective::{TallyOf, Rank}; +pub use pallet_ranked_collective::{Rank, TallyOf}; +use sp_runtime::{traits::Convert, BuildStorage}; pub mod eth_keys { - use crate::{ - mock::Test, - EcdsaSignature, EthereumAddress, - }; - use hex_literal::hex; + use crate::{mock::Test, EcdsaSignature, EthereumAddress}; use codec::Encode; + use hex_literal::hex; - pub fn total_claims() -> u64 { 10 + 100 + 1000 } - pub fn alice_account_id() -> ::AccountId { 69 } - pub fn bob_account_id() -> ::AccountId { 1337 } + pub fn total_claims() -> u64 { + 10 + 100 + 1000 + } + pub fn alice_account_id() -> ::AccountId { + 69 + } + pub fn bob_account_id() -> ::AccountId { + 1337 + } - pub fn first_eth_public_known() -> EthereumAddress { EthereumAddress(hex!("1A69d2D5568D1878023EeB121a73d33B9116A760")) } - pub fn second_eth_public_known() -> EthereumAddress { EthereumAddress(hex!("2f86cfBED3fbc1eCf2989B9aE5fc019a837A9C12")) } - pub fn third_eth_public_known() -> EthereumAddress { EthereumAddress(hex!("e83f67361Ac74D42A48E2DAfb6706eb047D8218D")) } - pub fn fourth_eth_public_known() -> EthereumAddress { EthereumAddress(hex!("827ee4ad9b259b6fa1390ed60921508c78befd63")) } + pub fn first_eth_public_known() -> EthereumAddress { + EthereumAddress(hex!("1A69d2D5568D1878023EeB121a73d33B9116A760")) + } + pub fn second_eth_public_known() -> EthereumAddress { + EthereumAddress(hex!("2f86cfBED3fbc1eCf2989B9aE5fc019a837A9C12")) + } + pub fn third_eth_public_known() -> EthereumAddress { + EthereumAddress(hex!("e83f67361Ac74D42A48E2DAfb6706eb047D8218D")) + } + pub fn fourth_eth_public_known() -> EthereumAddress { + EthereumAddress(hex!("827ee4ad9b259b6fa1390ed60921508c78befd63")) + } - fn first_eth_private_key() -> libsecp256k1::SecretKey { libsecp256k1::SecretKey::parse(&hex!("01c928771aea942a1e7ac06adf2b73dfbc9a25d9eaa516e3673116af7f345198")).unwrap() } - fn second_eth_private_key() -> libsecp256k1::SecretKey { libsecp256k1::SecretKey::parse(&hex!("b19a435901872f817185f7234a1484eae837613f9d10cf21927a23c2d8cb9139")).unwrap() } - fn third_eth_private_key() -> libsecp256k1::SecretKey { libsecp256k1::SecretKey::parse(&hex!("d3baf57b74d65719b2dc33f5a464176022d0cc5edbca002234229f3e733875fc")).unwrap() } - fn fourth_eth_private_key() -> libsecp256k1::SecretKey { libsecp256k1::SecretKey::parse(&hex!("c4683d566436af6b58b4a59c8f501319226e85b21869bf93d5eeb4596d4791d4")).unwrap() } - fn wrong_eth_private_key() -> libsecp256k1::SecretKey { libsecp256k1::SecretKey::parse(&hex!("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef")).unwrap() } + fn first_eth_private_key() -> libsecp256k1::SecretKey { + libsecp256k1::SecretKey::parse(&hex!( + "01c928771aea942a1e7ac06adf2b73dfbc9a25d9eaa516e3673116af7f345198" + )) + .unwrap() + } + fn second_eth_private_key() -> libsecp256k1::SecretKey { + libsecp256k1::SecretKey::parse(&hex!( + "b19a435901872f817185f7234a1484eae837613f9d10cf21927a23c2d8cb9139" + )) + .unwrap() + } + fn third_eth_private_key() -> libsecp256k1::SecretKey { + libsecp256k1::SecretKey::parse(&hex!( + "d3baf57b74d65719b2dc33f5a464176022d0cc5edbca002234229f3e733875fc" + )) + .unwrap() + } + fn fourth_eth_private_key() -> libsecp256k1::SecretKey { + libsecp256k1::SecretKey::parse(&hex!( + "c4683d566436af6b58b4a59c8f501319226e85b21869bf93d5eeb4596d4791d4" + )) + .unwrap() + } + fn wrong_eth_private_key() -> libsecp256k1::SecretKey { + libsecp256k1::SecretKey::parse(&hex!( + "deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef" + )) + .unwrap() + } - pub fn first_eth_public_key() -> EthereumAddress { crate::secp_utils::eth(&first_eth_private_key()) } - pub fn second_eth_public_key() -> EthereumAddress { crate::secp_utils::eth(&second_eth_private_key()) } - pub fn third_eth_public_key() -> EthereumAddress { crate::secp_utils::eth(&third_eth_private_key()) } - pub fn fourth_eth_public_key() -> EthereumAddress { crate::secp_utils::eth(&fourth_eth_private_key()) } + pub fn first_eth_public_key() -> EthereumAddress { + crate::secp_utils::eth(&first_eth_private_key()) + } + pub fn second_eth_public_key() -> EthereumAddress { + crate::secp_utils::eth(&second_eth_private_key()) + } + pub fn third_eth_public_key() -> EthereumAddress { + crate::secp_utils::eth(&third_eth_private_key()) + } + pub fn fourth_eth_public_key() -> EthereumAddress { + crate::secp_utils::eth(&fourth_eth_private_key()) + } - pub fn first_account_id() -> ::AccountId { crate::secp_utils::into_account_id::(first_eth_public_key()) } - pub fn second_account_id() -> ::AccountId { crate::secp_utils::into_account_id::(second_eth_public_key()) } - pub fn third_account_id() -> ::AccountId { crate::secp_utils::into_account_id::(third_eth_public_key()) } - pub fn fourth_account_id() -> ::AccountId { crate::secp_utils::into_account_id::(fourth_eth_public_key()) } + pub fn first_account_id() -> ::AccountId { + crate::secp_utils::into_account_id::(first_eth_public_key()) + } + pub fn second_account_id() -> ::AccountId { + crate::secp_utils::into_account_id::(second_eth_public_key()) + } + pub fn third_account_id() -> ::AccountId { + crate::secp_utils::into_account_id::(third_eth_public_key()) + } + pub fn fourth_account_id() -> ::AccountId { + crate::secp_utils::into_account_id::(fourth_eth_public_key()) + } - pub fn first_signature() -> EcdsaSignature { crate::secp_utils::sig::(&first_eth_private_key(), &alice_account_id().encode()) } - pub fn second_signature() -> EcdsaSignature { crate::secp_utils::sig::(&second_eth_private_key(), &alice_account_id().encode()) } - pub fn third_signature() -> EcdsaSignature { crate::secp_utils::sig::(&third_eth_private_key(), &alice_account_id().encode()) } - pub fn fourth_signature() -> EcdsaSignature { crate::secp_utils::sig::(&fourth_eth_private_key(), &bob_account_id().encode()) } - pub fn wrong_signature() -> EcdsaSignature { crate::secp_utils::sig::(&wrong_eth_private_key(), &alice_account_id().encode()) } + pub fn first_signature() -> EcdsaSignature { + crate::secp_utils::sig::(&first_eth_private_key(), &alice_account_id().encode()) + } + pub fn second_signature() -> EcdsaSignature { + crate::secp_utils::sig::(&second_eth_private_key(), &alice_account_id().encode()) + } + pub fn third_signature() -> EcdsaSignature { + crate::secp_utils::sig::(&third_eth_private_key(), &alice_account_id().encode()) + } + pub fn fourth_signature() -> EcdsaSignature { + crate::secp_utils::sig::(&fourth_eth_private_key(), &bob_account_id().encode()) + } + pub fn wrong_signature() -> EcdsaSignature { + crate::secp_utils::sig::(&wrong_eth_private_key(), &alice_account_id().encode()) + } } #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] @@ -66,10 +128,9 @@ impl pallet_balances::Config for Test { type AccountStore = System; } - parameter_types! { pub const MinVestedTransfer: u64 = 1; - pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons = + pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons = WithdrawReasons::except(WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE); } @@ -79,9 +140,8 @@ impl pallet_vesting::Config for Test { type BlockNumberToBalance = sp_runtime::traits::ConvertInto; type MinVestedTransfer = MinVestedTransfer; type WeightInfo = (); - type UnvestedFundsAllowedWithdrawReasons = - UnvestedFundsAllowedWithdrawReasons; - + type UnvestedFundsAllowedWithdrawReasons = UnvestedFundsAllowedWithdrawReasons; + type BlockNumberProvider = System; const MAX_VESTING_SCHEDULES: u32 = 28; } @@ -148,7 +208,7 @@ impl pallet_ranked_collective::Config for Test { type PromoteOrigin = EnsureRootWithSuccess>; type DemoteOrigin = EnsureRootWithSuccess>; type ExchangeOrigin = EnsureRootWithSuccess>; - + type Polls = TestPolls; type MemberSwappedHandler = (); type MinRankOfClass = MinRankOfClass; @@ -200,8 +260,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities { (crate::mock::eth_keys::third_account_id(), 1000), ], } - .assimilate_storage(&mut t) - .unwrap(); + .assimilate_storage(&mut t) + .unwrap(); ghost_claims::GenesisConfig:: { total: crate::mock::eth_keys::total_claims(), @@ -210,8 +270,8 @@ pub fn new_test_ext() -> sp_io::TestExternalities { (crate::mock::eth_keys::third_account_id(), 3), ], } - .assimilate_storage(&mut t) - .unwrap(); + .assimilate_storage(&mut t) + .unwrap(); let mut ext = sp_io::TestExternalities::new(t); ext.execute_with(|| { diff --git a/pallets/claims/src/secp_utils.rs b/pallets/claims/src/secp_utils.rs index 0449f1e..3ba53dd 100644 --- a/pallets/claims/src/secp_utils.rs +++ b/pallets/claims/src/secp_utils.rs @@ -8,7 +8,8 @@ pub fn public(secret: &libsecp256k1::SecretKey) -> libsecp256k1::PublicKey { pub fn eth(secret: &libsecp256k1::SecretKey) -> EthereumAddress { let mut res = EthereumAddress::default(); - res.0.copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]); + res.0 + .copy_from_slice(&keccak_256(&public(secret).serialize()[1..65])[12..]); res } @@ -18,17 +19,14 @@ pub fn into_account_id, I: 'static>(address: EthereumAddress) -> T: } pub fn sig, I: 'static>( - secret: &libsecp256k1::SecretKey, + secret: &libsecp256k1::SecretKey, what: &[u8], ) -> EcdsaSignature { let msg = keccak_256(&super::Pallet::::ethereum_signable_message( - &crate::to_ascii_hex(what)[..], + &crate::to_ascii_hex(what)[..], )); - let (sig, recovery_id) = libsecp256k1::sign( - &libsecp256k1::Message::parse(&msg), - secret, - ); + let (sig, recovery_id) = libsecp256k1::sign(&libsecp256k1::Message::parse(&msg), secret); let mut r = [0u8; 65]; r[0..64].copy_from_slice(&sig.serialize()[..]); diff --git a/pallets/claims/src/tests.rs b/pallets/claims/src/tests.rs index d71e98a..2ad35d3 100644 --- a/pallets/claims/src/tests.rs +++ b/pallets/claims/src/tests.rs @@ -1,21 +1,19 @@ #![cfg(test)] -use mock::{ - new_test_ext, ghost_claims, - Test, System, Balances, Club, Vesting, Claims, RuntimeOrigin, RuntimeEvent, - eth_keys::{ - alice_account_id, total_claims, first_eth_public_known, - first_eth_public_key, first_account_id, first_signature, - second_eth_public_known, second_eth_public_key, second_account_id, - second_signature, third_eth_public_known, third_eth_public_key, - third_account_id, third_signature, fourth_eth_public_known, - fourth_eth_public_key, fourth_account_id, fourth_signature, - wrong_signature, bob_account_id, - } -}; -use hex_literal::hex; -use frame_support::{assert_err, assert_ok}; use super::*; +use frame_support::{assert_err, assert_ok}; +use hex_literal::hex; +use mock::{ + eth_keys::{ + alice_account_id, bob_account_id, first_account_id, first_eth_public_key, + first_eth_public_known, first_signature, fourth_account_id, fourth_eth_public_key, + fourth_eth_public_known, fourth_signature, second_account_id, second_eth_public_key, + second_eth_public_known, second_signature, third_account_id, third_eth_public_key, + third_eth_public_known, third_signature, total_claims, wrong_signature, + }, + ghost_claims, new_test_ext, Balances, Claims, Club, RuntimeEvent, RuntimeOrigin, System, Test, + Vesting, +}; #[test] fn serde_works() { @@ -41,12 +39,12 @@ fn basic_setup_works() { assert_eq!(Balances::usable_balance(&first_account_id()), 10); assert_eq!(Balances::usable_balance(&second_account_id()), 100); assert_eq!(Balances::usable_balance(&third_account_id()), 1000); - + assert_eq!(Club::rank_of(&alice_account_id()), None); assert_eq!(Club::rank_of(&first_account_id()), None); assert_eq!(Club::rank_of(&second_account_id()), Some(1)); assert_eq!(Club::rank_of(&third_account_id()), Some(3)); - + assert_eq!(Vesting::vesting_balance(&alice_account_id()), None); assert_eq!(ghost_claims::Total::::get(), total_claims()); }); @@ -56,9 +54,10 @@ fn basic_setup_works() { fn small_claiming_works() { new_test_ext().execute_with(|| { assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - first_eth_public_key(), - first_signature())); + RuntimeOrigin::signed(alice_account_id()), + first_eth_public_key(), + first_signature() + )); assert_eq!(Balances::usable_balance(&alice_account_id()), 10); assert_eq!(Balances::usable_balance(&first_account_id()), 0); @@ -77,9 +76,9 @@ fn small_claiming_works() { fn medium_claiming_works() { new_test_ext().execute_with(|| { assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - second_eth_public_key(), - second_signature(), + RuntimeOrigin::signed(alice_account_id()), + second_eth_public_key(), + second_signature(), )); assert_eq!(Balances::usable_balance(&alice_account_id()), 100); @@ -99,9 +98,9 @@ fn medium_claiming_works() { fn big_claiming_works() { new_test_ext().execute_with(|| { assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - third_eth_public_key(), - third_signature(), + RuntimeOrigin::signed(alice_account_id()), + third_eth_public_key(), + third_signature(), )); assert_eq!(Balances::usable_balance(&alice_account_id()), 200); @@ -113,11 +112,15 @@ fn big_claiming_works() { assert_eq!(Club::rank_of(&third_account_id()), None); assert_eq!(Vesting::vesting_balance(&alice_account_id()), Some(800)); - assert_eq!(ghost_claims::Total::::get(), total_claims() - 1000); + assert_eq!( + ghost_claims::Total::::get(), + total_claims() - 1000 + ); assert_ok!(Balances::transfer_allow_death( - RuntimeOrigin::signed(alice_account_id()), - bob_account_id(), - 200)); + RuntimeOrigin::signed(alice_account_id()), + bob_account_id(), + 200 + )); }) } @@ -125,19 +128,19 @@ fn big_claiming_works() { fn multiple_accounts_claiming_works() { new_test_ext().execute_with(|| { assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - first_eth_public_key(), - first_signature(), + RuntimeOrigin::signed(alice_account_id()), + first_eth_public_key(), + first_signature(), )); assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - second_eth_public_key(), - second_signature(), + RuntimeOrigin::signed(alice_account_id()), + second_eth_public_key(), + second_signature(), )); assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - third_eth_public_key(), - third_signature(), + RuntimeOrigin::signed(alice_account_id()), + third_eth_public_key(), + third_signature(), )); assert_eq!(Balances::usable_balance(&alice_account_id()), 310); @@ -157,19 +160,19 @@ fn multiple_accounts_claiming_works() { fn multiple_accounts_reverese_claiming_works() { new_test_ext().execute_with(|| { assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - third_eth_public_key(), - third_signature(), + RuntimeOrigin::signed(alice_account_id()), + third_eth_public_key(), + third_signature(), )); assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - second_eth_public_key(), - second_signature(), + RuntimeOrigin::signed(alice_account_id()), + second_eth_public_key(), + second_signature(), )); assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - first_eth_public_key(), - first_signature(), + RuntimeOrigin::signed(alice_account_id()), + first_eth_public_key(), + first_signature(), )); assert_eq!(Balances::usable_balance(&alice_account_id()), 310); @@ -188,11 +191,14 @@ fn multiple_accounts_reverese_claiming_works() { #[test] fn cannot_claim_with_bad_signature() { new_test_ext().execute_with(|| { - assert_err!(Claims::claim( + assert_err!( + Claims::claim( RuntimeOrigin::signed(alice_account_id()), first_eth_public_key(), - wrong_signature()), - crate::Error::::InvalidEthereumAddress); + wrong_signature() + ), + crate::Error::::InvalidEthereumAddress + ); assert_eq!(Balances::usable_balance(&alice_account_id()), 0); assert_eq!(Balances::usable_balance(&first_account_id()), 10); @@ -212,11 +218,14 @@ fn cannot_claim_with_bad_signature() { #[test] fn cannot_claim_with_wrong_address() { new_test_ext().execute_with(|| { - assert_err!(Claims::claim( + assert_err!( + Claims::claim( RuntimeOrigin::signed(bob_account_id()), first_eth_public_key(), - first_signature()), - crate::Error::::InvalidEthereumAddress); + first_signature() + ), + crate::Error::::InvalidEthereumAddress + ); assert_eq!(Balances::usable_balance(&bob_account_id()), 0); assert_eq!(Balances::usable_balance(&alice_account_id()), 0); @@ -237,11 +246,14 @@ fn cannot_claim_with_wrong_address() { #[test] fn cannot_claim_nothing() { new_test_ext().execute_with(|| { - assert_err!(Claims::claim( + assert_err!( + Claims::claim( RuntimeOrigin::signed(bob_account_id()), fourth_eth_public_key(), - fourth_signature()), - crate::Error::::NoBalanceToClaim); + fourth_signature() + ), + crate::Error::::NoBalanceToClaim + ); assert_eq!(Balances::usable_balance(&bob_account_id()), 0); assert_eq!(Balances::usable_balance(&fourth_account_id()), 0); assert_eq!(Vesting::vesting_balance(&bob_account_id()), None); @@ -259,16 +271,15 @@ fn event_emitted_during_claim() { System::reset_events(); assert_eq!(System::event_count(), 0); assert_ok!(Claims::claim( - RuntimeOrigin::signed(alice_account_id()), - third_eth_public_key(), - third_signature(), + RuntimeOrigin::signed(alice_account_id()), + third_eth_public_key(), + third_signature(), )); - System::assert_has_event(RuntimeEvent::Claims( - crate::Event::Claimed { - receiver: alice_account_id(), - donor: account, - amount, - rank, - })); + System::assert_has_event(RuntimeEvent::Claims(crate::Event::Claimed { + receiver: alice_account_id(), + donor: account, + amount, + rank, + })); }) }