rustfmt the ghost-claims pallet
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
116ca39dc4
commit
40a8152f69
@ -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
|
||||
|
@ -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::<T, I>::minimum_balance();
|
||||
@ -22,23 +22,30 @@ mod benchmarks {
|
||||
Total::<T, I>::put(balance);
|
||||
|
||||
let pseudo_rank = 5u16;
|
||||
let _ = Club::<T, I>::do_add_member_to_rank(
|
||||
pseudo_account.clone(),
|
||||
pseudo_rank,
|
||||
false,
|
||||
);
|
||||
let _ = Club::<T, I>::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::<T, I>(ðereum_secret_key, &user_account.encode());
|
||||
let signature =
|
||||
crate::secp_utils::sig::<T, I>(ðereum_secret_key, &user_account.encode());
|
||||
|
||||
let prev_balance = CurrencyOf::<T, I>::free_balance(&user_account);
|
||||
let prev_rank = Club::<T, I>::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::<T, I>::free_balance(&user_account), prev_balance + balance);
|
||||
assert_eq!(CurrencyOf::<T, I>::free_balance(&pseudo_account), balance - balance);
|
||||
assert_eq!(
|
||||
CurrencyOf::<T, I>::free_balance(&user_account),
|
||||
prev_balance + balance
|
||||
);
|
||||
assert_eq!(
|
||||
CurrencyOf::<T, I>::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::<T, I>::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,);
|
||||
}
|
||||
|
@ -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).
|
||||
///
|
||||
@ -81,15 +82,14 @@ impl sp_std::fmt::Debug for EcdsaSignature {
|
||||
}
|
||||
|
||||
type CurrencyOf<T, I> = <<T as Config<I>>::VestingSchedule as VestingSchedule<
|
||||
<T as frame_system::Config>::AccountId
|
||||
<T as frame_system::Config>::AccountId,
|
||||
>>::Currency;
|
||||
|
||||
type BalanceOf<T, I> = <CurrencyOf<T, I> as Currency<
|
||||
<T as frame_system::Config>::AccountId>
|
||||
>::Balance;
|
||||
type BalanceOf<T, I> =
|
||||
<CurrencyOf<T, I> as Currency<<T as frame_system::Config>::AccountId>>::Balance;
|
||||
|
||||
type RankOf<T, I> = <pallet_ranked_collective::Pallet::<T, I> as RankedMembers>::Rank;
|
||||
type AccountIdOf<T, I> = <pallet_ranked_collective::Pallet::<T, I> as RankedMembers>::AccountId;
|
||||
type RankOf<T, I> = <pallet_ranked_collective::Pallet<T, I> as RankedMembers>::Rank;
|
||||
type AccountIdOf<T, I> = <pallet_ranked_collective::Pallet<T, I> as RankedMembers>::AccountId;
|
||||
|
||||
#[frame_support::pallet]
|
||||
pub mod pallet {
|
||||
@ -100,8 +100,11 @@ pub mod pallet {
|
||||
pub struct Pallet<T, I = ()>(_);
|
||||
|
||||
#[pallet::config]
|
||||
pub trait Config<I: 'static = ()>: frame_system::Config + pallet_ranked_collective::Config<I> {
|
||||
type RuntimeEvent: From<Event<Self, I>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
pub trait Config<I: 'static = ()>:
|
||||
frame_system::Config + pallet_ranked_collective::Config<I>
|
||||
{
|
||||
type RuntimeEvent: From<Event<Self, I>>
|
||||
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
|
||||
|
||||
type VestingSchedule: VestingSchedule<Self::AccountId, Moment = BlockNumberFor<Self>>;
|
||||
type BlockNumberProvider: BlockNumberProvider<BlockNumber = BlockNumberFor<Self>>;
|
||||
@ -159,7 +162,11 @@ pub mod pallet {
|
||||
.map(|(account_id, rank)| {
|
||||
assert!(
|
||||
pallet_ranked_collective::Pallet::<T, I>::do_add_member_to_rank(
|
||||
account_id.clone(), *rank, false).is_ok(),
|
||||
account_id.clone(),
|
||||
*rank,
|
||||
false
|
||||
)
|
||||
.is_ok(),
|
||||
"error during adding and promotion"
|
||||
);
|
||||
account_id
|
||||
@ -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::<T, I>::InvalidEthereumSignature)?;
|
||||
let recovered_address = Self::recover_ethereum_address(ðereum_signature, &data)
|
||||
.ok_or(Error::<T, I>::InvalidEthereumSignature)?;
|
||||
|
||||
ensure!(recovered_address == ethereum_address,
|
||||
Error::<T, I>::InvalidEthereumAddress);
|
||||
ensure!(
|
||||
recovered_address == ethereum_address,
|
||||
Error::<T, I>::InvalidEthereumAddress
|
||||
);
|
||||
|
||||
Self::do_claim(who, ethereum_address)
|
||||
}
|
||||
@ -243,12 +250,15 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
}
|
||||
|
||||
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::<T, I>::AddressDecodingFailed)?;
|
||||
|
||||
let balance_due = CurrencyOf::<T, I>::free_balance(&donor);
|
||||
ensure!(balance_due >= CurrencyOf::<T, I>::minimum_balance(),
|
||||
Error::<T, I>::NoBalanceToClaim);
|
||||
ensure!(
|
||||
balance_due >= CurrencyOf::<T, I>::minimum_balance(),
|
||||
Error::<T, I>::NoBalanceToClaim
|
||||
);
|
||||
|
||||
let new_total = Total::<T, I>::get()
|
||||
.checked_sub(&balance_due)
|
||||
@ -280,20 +290,32 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
)?;
|
||||
}
|
||||
|
||||
let rank = if let Some(rank) = <pallet_ranked_collective::Pallet::<T, I> as RankedMembers>::rank_of(&donor) {
|
||||
let rank = if let Some(rank) =
|
||||
<pallet_ranked_collective::Pallet<T, I> as RankedMembers>::rank_of(&donor)
|
||||
{
|
||||
pallet_ranked_collective::Pallet::<T, I>::do_remove_member_from_rank(&donor, rank)?;
|
||||
let new_rank = match <pallet_ranked_collective::Pallet::<T, I> as RankedMembers>::rank_of(&receiver) {
|
||||
let new_rank =
|
||||
match <pallet_ranked_collective::Pallet<T, I> 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::<T, I>::do_promote_member(receiver.clone(), None, false)?;
|
||||
pallet_ranked_collective::Pallet::<T, I>::do_promote_member(
|
||||
receiver.clone(),
|
||||
None,
|
||||
false,
|
||||
)?;
|
||||
}
|
||||
rank
|
||||
},
|
||||
}
|
||||
_ => {
|
||||
pallet_ranked_collective::Pallet::<T, I>::do_add_member_to_rank(receiver.clone(), rank, false)?;
|
||||
pallet_ranked_collective::Pallet::<T, I>::do_add_member_to_rank(
|
||||
receiver.clone(),
|
||||
rank,
|
||||
false,
|
||||
)?;
|
||||
rank
|
||||
},
|
||||
}
|
||||
};
|
||||
<T as pallet::Config<I>>::MemberSwappedHandler::swapped(&donor, &receiver, new_rank);
|
||||
Some(new_rank)
|
||||
|
@ -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() -> <Test as frame_system::Config>::AccountId { 69 }
|
||||
pub fn bob_account_id() -> <Test as frame_system::Config>::AccountId { 1337 }
|
||||
pub fn total_claims() -> u64 {
|
||||
10 + 100 + 1000
|
||||
}
|
||||
pub fn alice_account_id() -> <Test as frame_system::Config>::AccountId {
|
||||
69
|
||||
}
|
||||
pub fn bob_account_id() -> <Test as frame_system::Config>::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() -> <Test as frame_system::Config>::AccountId { crate::secp_utils::into_account_id::<Test, ()>(first_eth_public_key()) }
|
||||
pub fn second_account_id() -> <Test as frame_system::Config>::AccountId { crate::secp_utils::into_account_id::<Test, ()>(second_eth_public_key()) }
|
||||
pub fn third_account_id() -> <Test as frame_system::Config>::AccountId { crate::secp_utils::into_account_id::<Test, ()>(third_eth_public_key()) }
|
||||
pub fn fourth_account_id() -> <Test as frame_system::Config>::AccountId { crate::secp_utils::into_account_id::<Test, ()>(fourth_eth_public_key()) }
|
||||
pub fn first_account_id() -> <Test as frame_system::Config>::AccountId {
|
||||
crate::secp_utils::into_account_id::<Test, ()>(first_eth_public_key())
|
||||
}
|
||||
pub fn second_account_id() -> <Test as frame_system::Config>::AccountId {
|
||||
crate::secp_utils::into_account_id::<Test, ()>(second_eth_public_key())
|
||||
}
|
||||
pub fn third_account_id() -> <Test as frame_system::Config>::AccountId {
|
||||
crate::secp_utils::into_account_id::<Test, ()>(third_eth_public_key())
|
||||
}
|
||||
pub fn fourth_account_id() -> <Test as frame_system::Config>::AccountId {
|
||||
crate::secp_utils::into_account_id::<Test, ()>(fourth_eth_public_key())
|
||||
}
|
||||
|
||||
pub fn first_signature() -> EcdsaSignature { crate::secp_utils::sig::<Test, ()>(&first_eth_private_key(), &alice_account_id().encode()) }
|
||||
pub fn second_signature() -> EcdsaSignature { crate::secp_utils::sig::<Test, ()>(&second_eth_private_key(), &alice_account_id().encode()) }
|
||||
pub fn third_signature() -> EcdsaSignature { crate::secp_utils::sig::<Test, ()>(&third_eth_private_key(), &alice_account_id().encode()) }
|
||||
pub fn fourth_signature() -> EcdsaSignature { crate::secp_utils::sig::<Test, ()>(&fourth_eth_private_key(), &bob_account_id().encode()) }
|
||||
pub fn wrong_signature() -> EcdsaSignature { crate::secp_utils::sig::<Test, ()>(&wrong_eth_private_key(), &alice_account_id().encode()) }
|
||||
pub fn first_signature() -> EcdsaSignature {
|
||||
crate::secp_utils::sig::<Test, ()>(&first_eth_private_key(), &alice_account_id().encode())
|
||||
}
|
||||
pub fn second_signature() -> EcdsaSignature {
|
||||
crate::secp_utils::sig::<Test, ()>(&second_eth_private_key(), &alice_account_id().encode())
|
||||
}
|
||||
pub fn third_signature() -> EcdsaSignature {
|
||||
crate::secp_utils::sig::<Test, ()>(&third_eth_private_key(), &alice_account_id().encode())
|
||||
}
|
||||
pub fn fourth_signature() -> EcdsaSignature {
|
||||
crate::secp_utils::sig::<Test, ()>(&fourth_eth_private_key(), &bob_account_id().encode())
|
||||
}
|
||||
pub fn wrong_signature() -> EcdsaSignature {
|
||||
crate::secp_utils::sig::<Test, ()>(&wrong_eth_private_key(), &alice_account_id().encode())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
|
||||
@ -66,7 +128,6 @@ impl pallet_balances::Config for Test {
|
||||
type AccountStore = System;
|
||||
}
|
||||
|
||||
|
||||
parameter_types! {
|
||||
pub const MinVestedTransfer: u64 = 1;
|
||||
pub UnvestedFundsAllowedWithdrawReasons: WithdrawReasons =
|
||||
@ -79,8 +140,7 @@ 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;
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
@ -25,10 +26,7 @@ pub fn sig<T: Config<I>, I: 'static>(
|
||||
&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()[..]);
|
||||
|
@ -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() {
|
||||
@ -58,7 +56,8 @@ fn small_claiming_works() {
|
||||
assert_ok!(Claims::claim(
|
||||
RuntimeOrigin::signed(alice_account_id()),
|
||||
first_eth_public_key(),
|
||||
first_signature()));
|
||||
first_signature()
|
||||
));
|
||||
|
||||
assert_eq!(Balances::usable_balance(&alice_account_id()), 10);
|
||||
assert_eq!(Balances::usable_balance(&first_account_id()), 0);
|
||||
@ -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::<Test, ()>::get(), total_claims() - 1000);
|
||||
assert_eq!(
|
||||
ghost_claims::Total::<Test, ()>::get(),
|
||||
total_claims() - 1000
|
||||
);
|
||||
assert_ok!(Balances::transfer_allow_death(
|
||||
RuntimeOrigin::signed(alice_account_id()),
|
||||
bob_account_id(),
|
||||
200));
|
||||
200
|
||||
));
|
||||
})
|
||||
}
|
||||
|
||||
@ -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::<Test>::InvalidEthereumAddress);
|
||||
wrong_signature()
|
||||
),
|
||||
crate::Error::<Test>::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::<Test>::InvalidEthereumAddress);
|
||||
first_signature()
|
||||
),
|
||||
crate::Error::<Test>::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::<Test>::NoBalanceToClaim);
|
||||
fourth_signature()
|
||||
),
|
||||
crate::Error::<Test>::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);
|
||||
@ -263,8 +275,7 @@ fn event_emitted_during_claim() {
|
||||
third_eth_public_key(),
|
||||
third_signature(),
|
||||
));
|
||||
System::assert_has_event(RuntimeEvent::Claims(
|
||||
crate::Event::Claimed {
|
||||
System::assert_has_event(RuntimeEvent::Claims(crate::Event::Claimed {
|
||||
receiver: alice_account_id(),
|
||||
donor: account,
|
||||
amount,
|
||||
|
Loading…
Reference in New Issue
Block a user