updated version of slow clap
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
c3ef933c20
commit
d3c5380ba7
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.3.57"
|
version = "0.4.0"
|
||||||
description = "Applause protocol for the EVM bridge"
|
description = "Applause protocol for the EVM bridge"
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
@ -28,6 +28,7 @@ sp-io = { workspace = true }
|
|||||||
sp-std = { workspace = true }
|
sp-std = { workspace = true }
|
||||||
|
|
||||||
ghost-networks = { workspace = true }
|
ghost-networks = { workspace = true }
|
||||||
|
ghost-traits = { workspace = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
pallet-balances = { workspace = true }
|
pallet-balances = { workspace = true }
|
||||||
|
|||||||
@ -3,7 +3,6 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use frame_benchmarking::v1::*;
|
use frame_benchmarking::v1::*;
|
||||||
|
|
||||||
use frame_support::traits::fungible::Inspect;
|
|
||||||
use frame_system::RawOrigin;
|
use frame_system::RawOrigin;
|
||||||
|
|
||||||
pub fn create_account<T: Config>() -> T::AccountId {
|
pub fn create_account<T: Config>() -> T::AccountId {
|
||||||
@ -12,16 +11,35 @@ pub fn create_account<T: Config>() -> T::AccountId {
|
|||||||
.expect("32 bytes always construct an AccountId32")
|
.expect("32 bytes always construct an AccountId32")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn prepare_evm_network<T: Config>(network_id: NetworkIdOf<T>) {
|
||||||
|
let network_data = NetworkData {
|
||||||
|
chain_name: "Ethereum".into(),
|
||||||
|
default_endpoints: vec![b"https://other.endpoint.network.com".to_vec()],
|
||||||
|
finality_delay: 69,
|
||||||
|
avg_block_speed: 69,
|
||||||
|
rate_limit_delay: 69,
|
||||||
|
block_distance: 69,
|
||||||
|
network_type: ghost_networks::NetworkType::Evm,
|
||||||
|
gatekeeper: b"0x4d224452801ACEd8B2F0aebE155379bb5D594381".to_vec(),
|
||||||
|
topic_name: b"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef".to_vec(),
|
||||||
|
incoming_fee: 0,
|
||||||
|
outgoing_fee: 0,
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = T::NetworkDataHandler::register(network_id, network_data.clone());
|
||||||
|
}
|
||||||
|
|
||||||
benchmarks! {
|
benchmarks! {
|
||||||
slow_clap {
|
slow_clap {
|
||||||
|
let network_id = NetworkIdOf::<T>::default();
|
||||||
|
prepare_evm_network::<T>(network_id);
|
||||||
|
|
||||||
let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
|
let minimum_balance = <<T as pallet::Config>::Currency>::minimum_balance();
|
||||||
let receiver = create_account::<T>();
|
let receiver = create_account::<T>();
|
||||||
let amount = minimum_balance + minimum_balance;
|
let amount = minimum_balance + minimum_balance;
|
||||||
|
|
||||||
let network_id = NetworkIdOf::<T>::default();
|
|
||||||
let session_index = T::ValidatorSet::session_index();
|
let session_index = T::ValidatorSet::session_index();
|
||||||
let transaction_hash = H256::repeat_byte(1u8);
|
let transaction_hash = H256::repeat_byte(1u8);
|
||||||
let args_hash = Pallet::<T>::generate_unique_hash(&receiver, &amount, &network_id);
|
|
||||||
|
|
||||||
let authorities = vec![T::AuthorityId::generate_pair(None)];
|
let authorities = vec![T::AuthorityId::generate_pair(None)];
|
||||||
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
|
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
|
||||||
@ -39,6 +57,7 @@ benchmarks! {
|
|||||||
receiver: receiver.clone(),
|
receiver: receiver.clone(),
|
||||||
amount,
|
amount,
|
||||||
};
|
};
|
||||||
|
let args_hash = Pallet::<T>::generate_unique_hash(&clap);
|
||||||
|
|
||||||
let authority_id = authorities
|
let authority_id = authorities
|
||||||
.get(authority_index as usize)
|
.get(authority_index as usize)
|
||||||
@ -46,16 +65,20 @@ benchmarks! {
|
|||||||
let signature = authority_id.sign(&clap.encode())
|
let signature = authority_id.sign(&clap.encode())
|
||||||
.ok_or("couldn't make signature")?;
|
.ok_or("couldn't make signature")?;
|
||||||
|
|
||||||
|
let empty_bitmap = BitMap::new(1);
|
||||||
|
DisabledAuthorityIndexes::<T>::insert(session_index, empty_bitmap);
|
||||||
|
|
||||||
|
assert_eq!(ApplauseDetails::<T>::get(&session_index, &args_hash).is_none(), true);
|
||||||
}: _(RawOrigin::None, clap, signature)
|
}: _(RawOrigin::None, clap, signature)
|
||||||
verify {
|
verify {
|
||||||
let clap_key = (session_index, transaction_hash, args_hash);
|
assert_eq!(ApplauseDetails::<T>::get(&session_index, &args_hash).is_some(), true);
|
||||||
assert_eq!(ReceivedClaps::<T>::get(&clap_key).get(&authority_index).is_some(), true);
|
|
||||||
assert_eq!(<<T as pallet::Config>::Currency>::total_balance(&receiver), amount);
|
assert_eq!(<<T as pallet::Config>::Currency>::total_balance(&receiver), amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
commit_block {
|
commit_block {
|
||||||
let session_index = T::ValidatorSet::session_index();
|
let session_index = T::ValidatorSet::session_index();
|
||||||
let network_id = NetworkIdOf::<T>::default();
|
let network_id = NetworkIdOf::<T>::default();
|
||||||
|
prepare_evm_network::<T>(network_id);
|
||||||
|
|
||||||
let authorities = vec![T::AuthorityId::generate_pair(None)];
|
let authorities = vec![T::AuthorityId::generate_pair(None)];
|
||||||
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
|
let bounded_authorities = WeakBoundedVec::<_, T::MaxAuthorities>::try_from(authorities.clone())
|
||||||
@ -68,8 +91,8 @@ benchmarks! {
|
|||||||
authority_index,
|
authority_index,
|
||||||
network_id,
|
network_id,
|
||||||
commitment: CommitmentDetails {
|
commitment: CommitmentDetails {
|
||||||
last_registered_block: 69,
|
last_stored_block: 69,
|
||||||
last_seen_block: 420,
|
commits: 420,
|
||||||
last_updated: 1337,
|
last_updated: 1337,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -86,8 +109,8 @@ benchmarks! {
|
|||||||
.get(&authority_index)
|
.get(&authority_index)
|
||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
assert_eq!(stored_commitment.last_registered_block, 69);
|
assert_eq!(stored_commitment.last_stored_block, 69);
|
||||||
assert_eq!(stored_commitment.last_seen_block, 420);
|
assert_eq!(stored_commitment.commits, 1);
|
||||||
assert_eq!(stored_commitment.last_updated, 1337);
|
assert_eq!(stored_commitment.last_updated, 1337);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -54,19 +54,14 @@ impl EvmResponseType {
|
|||||||
network_id: NetworkIdOf<T>,
|
network_id: NetworkIdOf<T>,
|
||||||
) -> BlockCommitment<NetworkIdOf<T>> {
|
) -> BlockCommitment<NetworkIdOf<T>> {
|
||||||
let last_updated = sp_io::offchain::timestamp().unix_millis();
|
let last_updated = sp_io::offchain::timestamp().unix_millis();
|
||||||
let current_block = match self {
|
|
||||||
EvmResponseType::BlockNumber(block_number) => *block_number,
|
|
||||||
EvmResponseType::TransactionLogs(_) => Default::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
BlockCommitment {
|
BlockCommitment {
|
||||||
session_index,
|
session_index,
|
||||||
authority_index,
|
authority_index,
|
||||||
network_id,
|
network_id,
|
||||||
commitment: CommitmentDetails {
|
commitment: CommitmentDetails {
|
||||||
last_registered_block: from_block,
|
last_stored_block: from_block,
|
||||||
last_seen_block: current_block,
|
|
||||||
last_updated,
|
last_updated,
|
||||||
|
commits: 420,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@ -19,8 +19,8 @@ use sp_staking::{
|
|||||||
|
|
||||||
use sp_runtime::BuildStorage;
|
use sp_runtime::BuildStorage;
|
||||||
|
|
||||||
use crate as slow_clap;
|
use crate::{self as slow_clap, AuthIndex};
|
||||||
use crate::{ApplauseListener, Config, EraIndex};
|
use crate::{ExposureListener, Config, EraIndex};
|
||||||
|
|
||||||
type Block = frame_system::mocking::MockBlock<Runtime>;
|
type Block = frame_system::mocking::MockBlock<Runtime>;
|
||||||
|
|
||||||
@ -36,17 +36,13 @@ frame_support::construct_runtime!(
|
|||||||
);
|
);
|
||||||
|
|
||||||
parameter_types! {
|
parameter_types! {
|
||||||
pub static Validators: Option<Vec<u64>> = Some(vec![
|
pub static FixedValidators: Vec<u64> = vec![0, 1, 2, 3];
|
||||||
1,
|
|
||||||
2,
|
|
||||||
3,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TestSessionManager;
|
pub struct TestSessionManager;
|
||||||
impl pallet_session::SessionManager<u64> for TestSessionManager {
|
impl pallet_session::SessionManager<u64> for TestSessionManager {
|
||||||
fn new_session(_new_index: SessionIndex) -> Option<Vec<u64>> {
|
fn new_session(_new_index: SessionIndex) -> Option<Vec<u64>> {
|
||||||
Validators::mutate(|l| l.take())
|
Some(FixedValidators::get())
|
||||||
}
|
}
|
||||||
fn end_session(_: SessionIndex) {}
|
fn end_session(_: SessionIndex) {}
|
||||||
fn start_session(_: SessionIndex) {}
|
fn start_session(_: SessionIndex) {}
|
||||||
@ -54,17 +50,14 @@ impl pallet_session::SessionManager<u64> for TestSessionManager {
|
|||||||
|
|
||||||
impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager {
|
impl pallet_session::historical::SessionManager<u64, u64> for TestSessionManager {
|
||||||
fn new_session(_new_index: SessionIndex) -> Option<Vec<(u64, u64)>> {
|
fn new_session(_new_index: SessionIndex) -> Option<Vec<(u64, u64)>> {
|
||||||
Validators::mutate(|l| {
|
Some(FixedValidators::get().iter().map(|l| (*l, *l)).collect())
|
||||||
l.take()
|
|
||||||
.map(|validators| validators.iter().map(|v| (*v, *v)).collect())
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
fn end_session(_: SessionIndex) {}
|
fn end_session(_: SessionIndex) {}
|
||||||
fn start_session(_: SessionIndex) {}
|
fn start_session(_: SessionIndex) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
type IdentificationTuple = (u64, u64);
|
type IdentificationTuple = (u64, u64);
|
||||||
type Offence = crate::ThrottlingOffence<IdentificationTuple>;
|
type Offence = crate::SlowClapOffence<IdentificationTuple>;
|
||||||
|
|
||||||
parameter_types! {
|
parameter_types! {
|
||||||
pub static Offences: Vec<(Vec<u64>, Offence)> = vec![];
|
pub static Offences: Vec<(Vec<u64>, Offence)> = vec![];
|
||||||
@ -90,7 +83,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
|
|||||||
let mut result = sp_io::TestExternalities::new(t);
|
let mut result = sp_io::TestExternalities::new(t);
|
||||||
|
|
||||||
result.execute_with(|| {
|
result.execute_with(|| {
|
||||||
for i in 1..=3 {
|
for i in 0..=3 {
|
||||||
System::inc_providers(&i);
|
System::inc_providers(&i);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
Session::set_keys(RuntimeOrigin::signed(i), i.into(), vec![],),
|
Session::set_keys(RuntimeOrigin::signed(i), i.into(), vec![],),
|
||||||
@ -176,8 +169,8 @@ impl pallet_balances::Config for Runtime {
|
|||||||
|
|
||||||
type Balance = u64;
|
type Balance = u64;
|
||||||
|
|
||||||
pub struct TestSomeCoolTrait;
|
pub struct TestExposureListener;
|
||||||
impl ApplauseListener<Balance> for TestSomeCoolTrait
|
impl ExposureListener<Balance, EraIndex, AuthIndex> for TestExposureListener
|
||||||
where
|
where
|
||||||
Balance: AtLeast32BitUnsigned + From<u64>,
|
Balance: AtLeast32BitUnsigned + From<u64>,
|
||||||
{
|
{
|
||||||
@ -185,15 +178,16 @@ where
|
|||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_threshold_amount(_era: EraIndex) -> Balance {
|
fn get_total_exposure(_era: EraIndex) -> Balance {
|
||||||
666_666_667u64.into()
|
1_000_000_000u64.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_validator_total_exposure(_era: EraIndex, index: usize) -> Balance {
|
fn get_validator_exposure(index: AuthIndex) -> Balance {
|
||||||
match index {
|
match index {
|
||||||
0 => 500_000_000u64,
|
0 => 250_000_000u64,
|
||||||
1 => 300_000_000u64,
|
1 => 200_000_000u64,
|
||||||
2 => 200_000_000u64,
|
2 => 250_000_000u64,
|
||||||
|
3 => 300_000_000u64,
|
||||||
_ => 0,
|
_ => 0,
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
@ -210,14 +204,13 @@ impl Config for Runtime {
|
|||||||
type BlockNumberProvider = System;
|
type BlockNumberProvider = System;
|
||||||
type ReportUnresponsiveness = OffenceHandler;
|
type ReportUnresponsiveness = OffenceHandler;
|
||||||
type DisabledValidators = Session;
|
type DisabledValidators = Session;
|
||||||
type ApplauseListener = TestSomeCoolTrait;
|
type ExposureListener = TestExposureListener;
|
||||||
|
|
||||||
type MaxAuthorities = ConstU32<5>;
|
type MaxAuthorities = ConstU32<5>;
|
||||||
type ApplauseThreshold = ConstU32<50>;
|
type ApplauseThreshold = ConstU32<500_000_000>;
|
||||||
type OffenceThreshold = ConstU32<0>;
|
|
||||||
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
|
type UnsignedPriority = ConstU64<{ 1 << 20 }>;
|
||||||
type HistoryDepth = HistoryDepth;
|
type HistoryDepth = HistoryDepth;
|
||||||
type MinAuthoritiesNumber = ConstU32<2>;
|
type MinAuthoritiesNumber = ConstU32<1>;
|
||||||
|
|
||||||
type WeightInfo = ();
|
type WeightInfo = ();
|
||||||
}
|
}
|
||||||
@ -236,32 +229,7 @@ pub fn advance_session() {
|
|||||||
let now = System::block_number().max(1);
|
let now = System::block_number().max(1);
|
||||||
System::set_block_number(now + 1);
|
System::set_block_number(now + 1);
|
||||||
Session::rotate_session();
|
Session::rotate_session();
|
||||||
let session_index = Session::current_index();
|
|
||||||
|
|
||||||
let authorities = Session::validators()
|
|
||||||
.into_iter()
|
|
||||||
.map(UintAuthorityId)
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
SlowClap::set_test_authorities(session_index, authorities);
|
|
||||||
assert_eq!(session_index, (now / Period::get()) as u32);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn advance_session_with_authority(authority: u64) {
|
|
||||||
let now = System::block_number().max(1);
|
|
||||||
System::set_block_number(now + 1);
|
|
||||||
Session::rotate_session();
|
|
||||||
|
|
||||||
let session_index = Session::current_index();
|
let session_index = Session::current_index();
|
||||||
|
|
||||||
SlowClap::set_test_authorities(
|
|
||||||
session_index,
|
|
||||||
vec![
|
|
||||||
UintAuthorityId::from(authority),
|
|
||||||
UintAuthorityId::from(69),
|
|
||||||
UintAuthorityId::from(420),
|
|
||||||
UintAuthorityId::from(1337),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
assert_eq!(session_index, (now / Period::get()) as u32);
|
assert_eq!(session_index, (now / Period::get()) as u32);
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user