From e3281f8f1504d092eb0cfca042d1dec4dea7ad33 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Tue, 30 Jul 2024 16:06:03 +0200 Subject: [PATCH] fixes --- .../pallets/ethereum-client/src/lib.rs | 27 ++++++------------- .../pallets/ethereum-client/src/tests.rs | 11 +++++--- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/bridges/snowbridge/pallets/ethereum-client/src/lib.rs b/bridges/snowbridge/pallets/ethereum-client/src/lib.rs index 418a8ca1cfa4..09c0bb54d8db 100644 --- a/bridges/snowbridge/pallets/ethereum-client/src/lib.rs +++ b/bridges/snowbridge/pallets/ethereum-client/src/lib.rs @@ -86,8 +86,9 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; #[pallet::constant] type ForkVersions: Get; + /// Minimum gap between finalized headers for an update to be free. #[pallet::constant] - type FreeHeadersInterval: Get>; + type FreeHeadersInterval: Get; type WeightInfo: WeightInfo; } @@ -442,7 +443,6 @@ pub mod pallet { let latest_finalized_state = FinalizedBeaconState::::get(LatestFinalizedBlockRoot::::get()) .ok_or(Error::::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); @@ -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(), @@ -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)?; @@ -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 diff --git a/bridges/snowbridge/pallets/ethereum-client/src/tests.rs b/bridges/snowbridge/pallets/ethereum-client/src/tests.rs index d969c6724dea..bc0ecaed674a 100644 --- a/bridges/snowbridge/pallets/ethereum-client/src/tests.rs +++ b/bridges/snowbridge/pallets/ethereum-client/src/tests.rs @@ -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); }); }