implement cross session claim via self_applause
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
parent
f245879925
commit
46aa18aafe
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ghost-slow-clap"
|
name = "ghost-slow-clap"
|
||||||
version = "0.3.40"
|
version = "0.3.41"
|
||||||
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
|
||||||
|
|||||||
@ -574,32 +574,55 @@ impl<T: Config> Pallet<T> {
|
|||||||
|
|
||||||
fn applause_if_posible(
|
fn applause_if_posible(
|
||||||
network_id: NetworkIdOf<T>,
|
network_id: NetworkIdOf<T>,
|
||||||
session_index: SessionIndex,
|
prev_session_index: SessionIndex,
|
||||||
transaction_hash: H256,
|
transaction_hash: H256,
|
||||||
receiver: T::AccountId,
|
receiver: T::AccountId,
|
||||||
amount: BalanceOf<T>,
|
amount: BalanceOf<T>,
|
||||||
) -> DispatchResult {
|
) -> DispatchResult {
|
||||||
|
let curr_session_index = prev_session_index.saturating_add(1);
|
||||||
let clap_unique_hash = Self::generate_unique_hash(&receiver, &amount, &network_id);
|
let clap_unique_hash = Self::generate_unique_hash(&receiver, &amount, &network_id);
|
||||||
let received_claps_key = (session_index, &transaction_hash, &clap_unique_hash);
|
let prev_received_claps_key = (prev_session_index, &transaction_hash, &clap_unique_hash);
|
||||||
|
let curr_received_claps_key = (curr_session_index, &transaction_hash, &clap_unique_hash);
|
||||||
|
|
||||||
|
let prev_authorities = Authorities::<T>::get(&prev_session_index);
|
||||||
|
let curr_authorities = Authorities::<T>::get(&curr_session_index);
|
||||||
|
|
||||||
|
let prev_received_claps = ReceivedClaps::<T>::get(&prev_received_claps_key).into_inner();
|
||||||
|
let curr_received_claps = ReceivedClaps::<T>::get(&curr_received_claps_key).into_inner();
|
||||||
|
|
||||||
|
let summary_authority_claps_length = curr_received_claps
|
||||||
|
.difference(&prev_received_claps)
|
||||||
|
.filter_map(|&index| {
|
||||||
|
curr_authorities
|
||||||
|
.get(index as usize)
|
||||||
|
.map(|curr_authority| {
|
||||||
|
prev_authorities
|
||||||
|
.iter()
|
||||||
|
.position(|prev_authority| curr_authority == prev_authority)
|
||||||
|
})
|
||||||
|
.flatten()
|
||||||
|
})
|
||||||
|
.count()
|
||||||
|
.saturating_add(curr_received_claps.len());
|
||||||
|
|
||||||
let clap = Clap {
|
let clap = Clap {
|
||||||
authority_index: Default::default(),
|
authority_index: Default::default(),
|
||||||
block_number: Default::default(),
|
block_number: Default::default(),
|
||||||
removed: false,
|
removed: Default::default(),
|
||||||
session_index,
|
session_index: Default::default(),
|
||||||
|
transaction_hash: Default::default(),
|
||||||
network_id,
|
network_id,
|
||||||
receiver,
|
receiver,
|
||||||
amount,
|
amount,
|
||||||
transaction_hash,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let enough_authorities = Perbill::from_rational(
|
let enough_authorities = Perbill::from_rational(
|
||||||
ReceivedClaps::<T>::get(&received_claps_key).len() as u32,
|
summary_authority_claps_length as u32,
|
||||||
Authorities::<T>::get(session_index).len() as u32,
|
Authorities::<T>::get(prev_session_index).len() as u32,
|
||||||
) > Perbill::from_percent(T::ApplauseThreshold::get());
|
) > Perbill::from_percent(T::ApplauseThreshold::get());
|
||||||
|
|
||||||
ensure!(enough_authorities, Error::<T>::NotEnoughClaps);
|
ensure!(enough_authorities, Error::<T>::NotEnoughClaps);
|
||||||
Self::try_applause(&clap, &received_claps_key)?;
|
Self::try_applause(&clap, &prev_received_claps_key)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -766,14 +789,17 @@ impl<T: Config> Pallet<T> {
|
|||||||
Some(_) if from_block.le(&to_block) => {
|
Some(_) if from_block.le(&to_block) => {
|
||||||
let adjusted_to_block = estimated_block
|
let adjusted_to_block = estimated_block
|
||||||
.checked_sub(from_block)
|
.checked_sub(from_block)
|
||||||
.map(|current_distance| current_distance
|
.map(|current_distance| {
|
||||||
.le(&max_block_distance)
|
current_distance
|
||||||
.then(|| estimated_block)
|
.le(&max_block_distance)
|
||||||
)
|
.then(|| estimated_block)
|
||||||
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
.unwrap_or(from_block
|
.unwrap_or(
|
||||||
.saturating_add(max_block_distance)
|
from_block
|
||||||
.min(estimated_block));
|
.saturating_add(max_block_distance)
|
||||||
|
.min(estimated_block),
|
||||||
|
);
|
||||||
(from_block, adjusted_to_block)
|
(from_block, adjusted_to_block)
|
||||||
}
|
}
|
||||||
_ => (to_block, to_block),
|
_ => (to_block, to_block),
|
||||||
|
|||||||
@ -991,7 +991,6 @@ fn should_self_applause_if_enough_received_claps() {
|
|||||||
|
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
assert_ok!(do_clap_from(session_index, network_id, 0, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
assert_ok!(do_clap_from(session_index, network_id, 1, false));
|
||||||
assert_ok!(do_clap_from(session_index, network_id, 2, false));
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
||||||
@ -999,14 +998,17 @@ fn should_self_applause_if_enough_received_claps() {
|
|||||||
);
|
);
|
||||||
assert_eq!(Balances::balance(&receiver), 0);
|
assert_eq!(Balances::balance(&receiver), 0);
|
||||||
|
|
||||||
assert_ok!(SlowClap::self_applause(
|
assert_err!(
|
||||||
RuntimeOrigin::signed(receiver),
|
SlowClap::self_applause(
|
||||||
network_id,
|
RuntimeOrigin::signed(receiver),
|
||||||
session_index,
|
network_id,
|
||||||
transaction_hash,
|
session_index,
|
||||||
receiver,
|
transaction_hash,
|
||||||
amount,
|
receiver,
|
||||||
));
|
amount,
|
||||||
|
),
|
||||||
|
Error::<Runtime>::NotEnoughClaps
|
||||||
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
||||||
false
|
false
|
||||||
@ -1015,6 +1017,26 @@ fn should_self_applause_if_enough_received_claps() {
|
|||||||
|
|
||||||
Networks::on_finalize(System::block_number());
|
Networks::on_finalize(System::block_number());
|
||||||
|
|
||||||
|
advance_session();
|
||||||
|
let next_session_index = Session::session_index();
|
||||||
|
let next_storage_key = (
|
||||||
|
next_session_index,
|
||||||
|
transaction_hash,
|
||||||
|
unique_transaction_hash,
|
||||||
|
);
|
||||||
|
assert_ok!(do_clap_from(next_session_index, network_id, 2, false));
|
||||||
|
|
||||||
|
assert_err!(
|
||||||
|
SlowClap::self_applause(
|
||||||
|
RuntimeOrigin::signed(receiver),
|
||||||
|
network_id,
|
||||||
|
next_session_index,
|
||||||
|
transaction_hash,
|
||||||
|
receiver,
|
||||||
|
amount,
|
||||||
|
),
|
||||||
|
Error::<Runtime>::NotEnoughClaps
|
||||||
|
);
|
||||||
assert_ok!(SlowClap::self_applause(
|
assert_ok!(SlowClap::self_applause(
|
||||||
RuntimeOrigin::signed(receiver),
|
RuntimeOrigin::signed(receiver),
|
||||||
network_id,
|
network_id,
|
||||||
@ -1027,6 +1049,10 @@ fn should_self_applause_if_enough_received_claps() {
|
|||||||
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
pallet::ApplausesForTransaction::<Runtime>::get(&storage_key),
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
pallet::ApplausesForTransaction::<Runtime>::get(&next_storage_key),
|
||||||
|
false
|
||||||
|
);
|
||||||
assert_eq!(Balances::balance(&receiver), amount);
|
assert_eq!(Balances::balance(&receiver), amount);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user