Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Jul 30, 2024
1 parent c82647f commit e3281f8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
27 changes: 8 additions & 19 deletions bridges/snowbridge/pallets/ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
#[pallet::constant]
type ForkVersions: Get<ForkVersions>;
/// Minimum gap between finalized headers for an update to be free.
#[pallet::constant]
type FreeHeadersInterval: Get<Option<u32>>;
type FreeHeadersInterval: Get<u32>;
type WeightInfo: WeightInfo;
}

Expand Down Expand Up @@ -442,7 +443,6 @@ pub mod pallet {
let latest_finalized_state =
FinalizedBeaconState::<T>::get(LatestFinalizedBlockRoot::<T>::get())
.ok_or(Error::<T>::NotBootstrapped)?;
let mut sync_committee_updated = false;
if let Some(next_sync_committee_update) = &update.next_sync_committee_update {
let store_period = compute_period(latest_finalized_state.slot);
let update_finalized_period = compute_period(update.finalized_header.slot);
Expand All @@ -469,14 +469,9 @@ pub mod pallet {
Self::deposit_event(Event::SyncCommitteeUpdated {
period: update_finalized_period,
});
sync_committee_updated = true;
};

let pays_fee = Self::may_refund_call_fee(
latest_finalized_state.slot,
update.finalized_header.slot,
sync_committee_updated,
);
let pays_fee = Self::check_refundable(update, latest_finalized_state.slot);
let actual_weight = match update.next_sync_committee_update {
None => T::WeightInfo::submit(),
Some(_) => T::WeightInfo::submit_with_sync_committee(),
Expand Down Expand Up @@ -651,7 +646,7 @@ pub mod pallet {
config::SLOTS_PER_EPOCH as u64,
));
let domain_type = config::DOMAIN_SYNC_COMMITTEE.to_vec();
// Domains are used for for seeds, for signatures, and for selecting aggregators.
// Domains are used for seeds, for signatures, and for selecting aggregators.
let domain = Self::compute_domain(domain_type, fork_version, validators_root)?;
// Hash tree root of SigningData - object root + domain
let signing_root = Self::compute_signing_root(header, domain)?;
Expand All @@ -661,22 +656,16 @@ pub mod pallet {
/// Updates are free if the update is successful and the interval between the latest
/// finalized header in storage and the newly imported header is large enough. All
/// successful sync committee updates are free.
pub(super) fn may_refund_call_fee(
latest_slot: u64,
improved_by_slot: u64,
sync_committee_updated: bool,
) -> Pays {
pub(super) fn check_refundable(update: &Update, latest_slot: u64) -> Pays {
// If the sync committee was successfully updated, the update may be free.
if sync_committee_updated {
if update.next_sync_committee_update.is_some() {
return Pays::No;
}

// If free headers are allowed and the latest finalized header is larger than the
// minimum slot interval, the header import transaction is free.
if let Some(free_headers_interval) = T::FreeHeadersInterval::get() {
if improved_by_slot >= latest_slot + free_headers_interval as u64 {
return Pays::No;
}
if update.finalized_header.slot >= latest_slot + T::FreeHeadersInterval::get() as u64 {
return Pays::No;
}

Pays::Yes
Expand Down
11 changes: 8 additions & 3 deletions bridges/snowbridge/pallets/ethereum-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,18 @@ pub fn compute_domain_bls() {

#[test]
pub fn may_refund_call_fee() {
let finalized_update = Box::new(load_next_finalized_header_update_fixture());
let sync_committee_update = Box::new(load_sync_committee_update_fixture());
new_tester().execute_with(|| {
// Not free, smaller than the allowed free header interval
assert_eq!(EthereumBeaconClient::may_refund_call_fee(96, 100, false), Pays::Yes);
assert_eq!(
EthereumBeaconClient::check_refundable(&finalized_update.clone(), 8190),
Pays::Yes
);
// Is free, larger than the minimum interval
assert_eq!(EthereumBeaconClient::may_refund_call_fee(96, 300, false), Pays::No);
assert_eq!(EthereumBeaconClient::check_refundable(&finalized_update, 8000), Pays::No);
// Is free, valid sync committee update
assert_eq!(EthereumBeaconClient::may_refund_call_fee(96, 100, true), Pays::No);
assert_eq!(EthereumBeaconClient::check_refundable(&sync_committee_update, 8190), Pays::No);
});
}

Expand Down

0 comments on commit e3281f8

Please sign in to comment.