Compare commits

..

No commits in common. "4c79048b49601c863408ffd90772844a66f8e2fc" and "028afc089f40470ea5316bdd4b1e93c5b1d80ba3" have entirely different histories.

6 changed files with 33 additions and 191 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "ghost-slow-clap" name = "ghost-slow-clap"
version = "0.4.22" version = "0.4.19"
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

View File

@ -43,7 +43,6 @@ use ghost_networks::{
}; };
use ghost_traits::exposure::ExposureListener; use ghost_traits::exposure::ExposureListener;
pub mod migrations;
pub mod weights; pub mod weights;
pub use crate::weights::WeightInfo; pub use crate::weights::WeightInfo;
mod benchmarking; mod benchmarking;
@ -80,7 +79,8 @@ const FETCH_TIMEOUT_PERIOD: u64 = 3_000;
const LOCK_BLOCK_EXPIRATION: u64 = 20; const LOCK_BLOCK_EXPIRATION: u64 = 20;
const ONE_HOUR_MILLIS: u64 = 3_600_000; const ONE_HOUR_MILLIS: u64 = 3_600_000;
const BLOCK_CHECK_CYCLES: u64 = 8;
const CHECK_BLOCK_INTERVAL: u64 = 300;
const BLOCK_COMMITMENT_DELAY: u64 = 100; const BLOCK_COMMITMENT_DELAY: u64 = 100;
pub type AuthIndex = u32; pub type AuthIndex = u32;
@ -271,7 +271,7 @@ type OffchainResult<T, A> = Result<A, OffchainErr<NetworkIdOf<T>>>;
pub mod pallet { pub mod pallet {
use super::*; use super::*;
const STORAGE_VERSION: StorageVersion = StorageVersion::new(3); const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
#[pallet::pallet] #[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)] #[pallet::storage_version(STORAGE_VERSION)]
@ -317,9 +317,6 @@ pub mod pallet {
#[pallet::constant] #[pallet::constant]
type MinAuthoritiesNumber: Get<u32>; type MinAuthoritiesNumber: Get<u32>;
#[pallet::constant]
type EpochDuration: Get<u64>;
type WeightInfo: WeightInfo; type WeightInfo: WeightInfo;
} }
@ -482,20 +479,16 @@ pub mod pallet {
#[pallet::hooks] #[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> { impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(current_block: BlockNumberFor<T>) -> Weight { fn on_initialize(current_block: BlockNumberFor<T>) -> Weight {
// TODO: what about start of the session???
let mut weight = T::DbWeight::get().reads(1); let mut weight = T::DbWeight::get().reads(1);
let networks_count = T::NetworkDataHandler::count(); let networks_count = T::NetworkDataHandler::count();
let current_block_number: u64 = current_block.unique_saturated_into(); let current_block_number: u64 = current_block.unique_saturated_into();
//let cycle_start = (current_block_number / CHECK_BLOCK_INTERVAL) * CHECK_BLOCK_INTERVAL;
let cycle_offset = current_block_number % CHECK_BLOCK_INTERVAL;
let check_block_interval = T::EpochDuration::get().saturating_div(BLOCK_CHECK_CYCLES); if cycle_offset >= networks_count.into() {
if check_block_interval == 0 { return Default::default();
return weight;
}
let cycle_offset = current_block_number % check_block_interval;
let block_in_epoch = current_block_number % T::EpochDuration::get();
if cycle_offset >= networks_count.into() || block_in_epoch < check_block_interval {
return weight;
} }
let converted_block: usize = current_block_number.unique_saturated_into(); let converted_block: usize = current_block_number.unique_saturated_into();
@ -520,11 +513,9 @@ pub mod pallet {
.unwrap_or(BitMap::new(validators.len() as u32)); .unwrap_or(BitMap::new(validators.len() as u32));
let max_external_block_deviation = ONE_HOUR_MILLIS let max_external_block_deviation = ONE_HOUR_MILLIS
.saturating_mul(6) .saturating_mul(6) // TODO: make it constant or calculate
.saturating_div(network_data.avg_block_speed); .saturating_div(network_data.avg_block_speed);
let max_internal_block_deviation = check_block_interval.unique_saturated_into();
let mut stored_blocks: Vec<(AuthIndex, ExternalBlockNumber)> = Vec::new(); let mut stored_blocks: Vec<(AuthIndex, ExternalBlockNumber)> = Vec::new();
let mut block_updates: Vec<(AuthIndex, BlockNumberFor<T>)> = Vec::new(); let mut block_updates: Vec<(AuthIndex, BlockNumberFor<T>)> = Vec::new();
@ -548,7 +539,7 @@ pub mod pallet {
&disabled_bitmap, &disabled_bitmap,
&mut block_updates, &mut block_updates,
validators_len, validators_len,
max_internal_block_deviation, CHECK_BLOCK_INTERVAL.unique_saturated_into(), // TODO: revisit
); );
let offence_bitmap = let offence_bitmap =
@ -1327,6 +1318,7 @@ impl<T: Config> Pallet<T> {
Authorities::<T>::set(&session_index, bounded_authorities); Authorities::<T>::set(&session_index, bounded_authorities);
let mut disabled_bitmap = BitMap::new(authorities_len as AuthIndex); let mut disabled_bitmap = BitMap::new(authorities_len as AuthIndex);
// TODO: make me better
for disabled_index in T::DisabledValidators::disabled_validators() { for disabled_index in T::DisabledValidators::disabled_validators() {
disabled_bitmap.set(disabled_index); disabled_bitmap.set(disabled_index);
} }

View File

@ -1,9 +0,0 @@
pub mod v3;
pub type MigrateV2ToV3<T> = frame_support::migrations::VersionedMigration<
2,
3,
v3::CommitmentDetailsTypesChanged<T>,
crate::Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

View File

@ -1,71 +0,0 @@
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
storage,
traits::{Get, UncheckedOnRuntimeUpgrade},
weights::Weight,
};
use ghost_networks::NetworkDataInspectHandler;
use scale_info::TypeInfo;
use sp_runtime::traits::{BlockNumberProvider, UniqueSaturatedInto};
use sp_runtime::RuntimeDebug;
use sp_std::marker::PhantomData;
use crate::{AuthIndex, BTreeMap, BlockCommitments, CommitmentDetails, Config, Pallet, LOG_TARGET};
#[derive(
RuntimeDebug,
Default,
Copy,
Clone,
Eq,
PartialEq,
Ord,
PartialOrd,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
)]
pub struct OldCommitmentDetails {
pub last_stored_block: u64,
pub last_updated: u64,
pub commits: u64,
}
pub struct CommitmentDetailsTypesChanged<T>(PhantomData<T>);
impl<T: Config> UncheckedOnRuntimeUpgrade for CommitmentDetailsTypesChanged<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight = T::DbWeight::get().reads(3);
let current_block_number = <Pallet<T> as BlockNumberProvider>::current_block_number();
for network_id in T::NetworkDataHandler::iter_indexes() {
let mut new_btree_map = BTreeMap::new();
let key_hash = BlockCommitments::<T>::hashed_key_for(&network_id);
let old_commitments = storage::unhashed::take_or_default::<
BTreeMap<AuthIndex, OldCommitmentDetails>,
>(&key_hash);
for (authority_index, details) in old_commitments.iter() {
new_btree_map.insert(
authority_index,
CommitmentDetails {
last_stored_block: details.last_stored_block,
last_updated: current_block_number,
commits: details.commits.unique_saturated_into(),
},
);
}
BlockCommitments::<T>::insert(&network_id, new_btree_map);
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1));
log::info!(
target: LOG_TARGET,
"👻 Block commitments migrated from unix timestamp to internal block for network {:?}",
network_id,
);
}
weight
}
}

View File

@ -159,7 +159,6 @@ parameter_types! {
pub static ExistentialDeposit: u64 = 2; pub static ExistentialDeposit: u64 = 2;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE; pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub const HistoryDepth: u32 = 10; pub const HistoryDepth: u32 = 10;
pub const EpochDuration: u64 = 80;
} }
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)] #[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
@ -214,7 +213,6 @@ impl Config for Runtime {
type UnsignedPriority = ConstU64<{ 1 << 20 }>; type UnsignedPriority = ConstU64<{ 1 << 20 }>;
type HistoryDepth = HistoryDepth; type HistoryDepth = HistoryDepth;
type MinAuthoritiesNumber = ConstU32<1>; type MinAuthoritiesNumber = ConstU32<1>;
type EpochDuration = EpochDuration;
type WeightInfo = (); type WeightInfo = ();
} }

View File

@ -3,8 +3,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use super::*; use super::*;
use crate::evm_types::Log;
use crate::mock::*; use crate::mock::*;
use crate::{evm_types::Log, migrations::v3::OldCommitmentDetails};
use frame_support::{assert_err, assert_ok, dispatch}; use frame_support::{assert_err, assert_ok, dispatch};
use sp_core::offchain::{ use sp_core::offchain::{
@ -1105,8 +1105,10 @@ fn should_disable_on_commitment_inactivity() {
prepare_evm_network(None, None); prepare_evm_network(None, None);
assert_eq!(BlockCommitments::<Runtime>::get(network_id).len(), 0); assert_eq!(BlockCommitments::<Runtime>::get(network_id).len(), 0);
System::set_block_number(5 * EpochDuration::get() / 2); System::set_block_number(420);
let last_stored_block = 69;
let last_stored_block = 1337;
let current_block = SlowClap::current_block_number();
for i in 0..3 { for i in 0..3 {
assert_ok!(do_block_commitment( assert_ok!(do_block_commitment(
@ -1117,8 +1119,10 @@ fn should_disable_on_commitment_inactivity() {
)); ));
} }
let current_block = System::current_block_number(); let delay = ONE_HOUR_MILLIS.saturating_mul(6);
SlowClap::on_initialize(current_block); System::set_block_number(current_block + delay + 1);
SlowClap::on_initialize(CHECK_BLOCK_INTERVAL);
System::assert_has_event(RuntimeEvent::SlowClap( System::assert_has_event(RuntimeEvent::SlowClap(
crate::Event::SomeAuthoritiesDelayed { crate::Event::SomeAuthoritiesDelayed {
delayed: vec![(3, 3)], delayed: vec![(3, 3)],
@ -1201,11 +1205,7 @@ fn should_disable_on_commitment_block_deviation() {
bad_last_stored_block, bad_last_stored_block,
)); ));
let to_initialize = EpochDuration::get() SlowClap::on_initialize(CHECK_BLOCK_INTERVAL);
.saturating_div(BLOCK_CHECK_CYCLES)
.saturating_mul(2);
SlowClap::on_initialize(to_initialize);
System::assert_has_event(RuntimeEvent::SlowClap( System::assert_has_event(RuntimeEvent::SlowClap(
crate::Event::SomeAuthoritiesDelayed { crate::Event::SomeAuthoritiesDelayed {
delayed: vec![(3, 3)], delayed: vec![(3, 3)],
@ -1292,7 +1292,6 @@ fn should_not_slash_by_applause_if_disabled_by_commitment() {
prepare_evm_network(None, None); prepare_evm_network(None, None);
assert_eq!(BlockCommitments::<Runtime>::get(network_id).len(), 0); assert_eq!(BlockCommitments::<Runtime>::get(network_id).len(), 0);
System::set_block_number(5 * EpochDuration::get() / 2);
let last_stored_block = 9_500_000; let last_stored_block = 9_500_000;
let current_block = SlowClap::current_block_number(); let current_block = SlowClap::current_block_number();
@ -1318,7 +1317,7 @@ fn should_not_slash_by_applause_if_disabled_by_commitment() {
)); ));
} }
SlowClap::on_initialize(current_block); SlowClap::on_initialize(CHECK_BLOCK_INTERVAL);
System::assert_has_event(RuntimeEvent::SlowClap( System::assert_has_event(RuntimeEvent::SlowClap(
crate::Event::AuthoritiesCommitmentEquilibrium, crate::Event::AuthoritiesCommitmentEquilibrium,
)); ));
@ -1388,8 +1387,9 @@ fn should_split_commit_slash_between_active_validators() {
prepare_evm_network(None, None); prepare_evm_network(None, None);
assert_eq!(BlockCommitments::<Runtime>::get(network_id).len(), 0); assert_eq!(BlockCommitments::<Runtime>::get(network_id).len(), 0);
System::set_block_number(5 * EpochDuration::get() / 2);
let last_stored_block = 69; let last_stored_block = 9_500_000;
let current_block = SlowClap::current_block_number();
for i in 0..3 { for i in 0..3 {
assert_ok!(do_block_commitment( assert_ok!(do_block_commitment(
@ -1399,12 +1399,10 @@ fn should_split_commit_slash_between_active_validators() {
last_stored_block, last_stored_block,
)); ));
} }
let current_block = SlowClap::current_block_number();
let offences = Offences::get(); let offences = Offences::get();
assert_eq!(offences.len(), 0); assert_eq!(offences.len(), 0);
SlowClap::on_initialize(current_block); SlowClap::on_initialize(CHECK_BLOCK_INTERVAL);
System::assert_has_event(RuntimeEvent::SlowClap( System::assert_has_event(RuntimeEvent::SlowClap(
crate::Event::SomeAuthoritiesDelayed { crate::Event::SomeAuthoritiesDelayed {
delayed: vec![(3, 3)], delayed: vec![(3, 3)],
@ -1444,15 +1442,8 @@ fn should_check_different_networks_during_on_initialize() {
prepare_evm_network(Some(network_id), None); prepare_evm_network(Some(network_id), None);
} }
let expected_events_count: u64 = BLOCK_CHECK_CYCLES let check_interval = times * CHECK_BLOCK_INTERVAL;
.saturating_sub(1) for check_block in 0..check_interval {
.saturating_mul(networks_count as u64)
.saturating_mul(times);
let expected_events_count: usize = expected_events_count.try_into().unwrap();
let epochs_passed = times * EpochDuration::get();
for check_block in 0..epochs_passed {
SlowClap::on_initialize(check_block); SlowClap::on_initialize(check_block);
} }
@ -1477,13 +1468,12 @@ fn should_check_different_networks_during_on_initialize() {
} }
}); });
assert_eq!(total_number_of_events, expected_events_count); let expected_events = networks_count * (times as u32);
assert_eq!(total_number_of_events, expected_events as usize);
assert_eq!(check_commitment_events.len() as u32, networks_count); assert_eq!(check_commitment_events.len() as u32, networks_count);
for network_id in 0..networks_count { for network_id in 0..networks_count {
let network_id_count = check_commitment_events.get(&network_id).unwrap(); let network_id_count = check_commitment_events.get(&network_id).unwrap();
let expected_events_count = assert_eq!(*network_id_count, times);
expected_events_count.saturating_div(networks_count as usize);
assert_eq!(*network_id_count, expected_events_count);
} }
}); });
} }
@ -1629,64 +1619,6 @@ fn should_get_balanced_responses_correctly() {
}); });
} }
#[test]
fn migration_from_v2_to_v3_works_fine() {
new_test_ext().execute_with(|| {
let network_ids = vec![1, 57232, 232, 775];
StorageVersion::new(2).put::<SlowClap>();
System::set_block_number(69);
for network_id in network_ids.iter() {
prepare_evm_network(Some(*network_id), None);
let mut fake_old_commits = BTreeMap::new();
fake_old_commits.insert(
0,
OldCommitmentDetails {
last_stored_block: (*network_id).into(),
last_updated: 133742069,
commits: 420,
},
);
fake_old_commits.insert(
2,
OldCommitmentDetails {
last_stored_block: (*network_id).into(),
last_updated: 133742069,
commits: 5,
},
);
let key_hash = BlockCommitments::<Runtime>::hashed_key_for(&network_id);
frame_support::storage::unhashed::put(&key_hash, &fake_old_commits);
}
type Migrate = crate::migrations::MigrateV2ToV3<Runtime>;
<Migrate as frame_support::traits::OnRuntimeUpgrade>::on_runtime_upgrade();
let current_block_number = System::current_block_number();
for network_id in network_ids.iter() {
BlockCommitments::<Runtime>::get(&network_id)
.iter()
.for_each(|(authority_id, details)| match authority_id {
0 => {
assert_eq!(details.last_stored_block, *network_id as u64);
assert_eq!(details.last_updated, current_block_number);
assert_eq!(details.commits, u8::MAX);
}
2 => {
assert_eq!(details.last_stored_block, *network_id as u64);
assert_eq!(details.last_updated, current_block_number);
assert_eq!(details.commits, 5);
}
_ => panic!("non registered block commitment exists after migration"),
});
}
});
}
fn assert_clapped_amount( fn assert_clapped_amount(
session_index: &SessionIndex, session_index: &SessionIndex,
unique_hash: &H256, unique_hash: &H256,