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

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Sep 3, 2024
1 parent 209274b commit c8307f7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 1,166 deletions.
39 changes: 22 additions & 17 deletions bridges/snowbridge/pallets/ethereum-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ pub mod pallet {
#[pallet::storage]
pub type NextSyncCommittee<T: Config> = StorageValue<_, SyncCommitteePrepared, ValueQuery>;

/// The last period where a sync committee was updated for free.
#[pallet::storage]
pub type LatestFreeSyncCommitteeUpdatePeriod<T: Config> = StorageValue<_, u64, ValueQuery>;

/// The current operating mode of the pallet.
#[pallet::storage]
#[pallet::getter(fn operating_mode)]
Expand Down Expand Up @@ -329,10 +333,6 @@ pub mod pallet {
(update.next_sync_committee_update.is_some() &&
update_attested_period == store_period);

println!("update_has_next_sync_committee {}", update_has_next_sync_committee);
println!("update.attested_header.slot {}", update.attested_header.slot);
println!("latest_finalized_state.slot {}", latest_finalized_state.slot);

ensure!(
update.attested_header.slot > latest_finalized_state.slot ||
update_has_next_sync_committee,
Expand Down Expand Up @@ -448,6 +448,13 @@ pub mod pallet {
let latest_finalized_state =
FinalizedBeaconState::<T>::get(LatestFinalizedBlockRoot::<T>::get())
.ok_or(Error::<T>::NotBootstrapped)?;

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(),
};

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 @@ -462,13 +469,10 @@ pub mod pallet {
<Error<T>>::InvalidSyncCommitteeUpdate
);
<NextSyncCommittee<T>>::set(sync_committee_prepared);
println!("condition 1");
} else if update_finalized_period == store_period + 1 {
<CurrentSyncCommittee<T>>::set(<NextSyncCommittee<T>>::get());
<NextSyncCommittee<T>>::set(sync_committee_prepared);
println!("condition 2");
}
println!("SyncCommitteeUpdated at period {}", update_finalized_period);
log::info!(
target: LOG_TARGET,
"💫 SyncCommitteeUpdated at period {}.",
Expand All @@ -479,12 +483,6 @@ pub mod pallet {
});
};

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(),
};

if update.finalized_header.slot > latest_finalized_state.slot {
Self::store_finalized_header(update.finalized_header, update.block_roots_root)?;
}
Expand Down Expand Up @@ -666,13 +664,20 @@ pub mod pallet {
/// successful sync committee updates are free.
pub(super) fn check_refundable(update: &Update, latest_slot: u64) -> Pays {
// If the sync committee was successfully updated, the update may be free.
if update.next_sync_committee_update.is_some() {
let update_period = compute_period(update.finalized_header.slot);
let latest_free_update_period = <LatestFreeSyncCommitteeUpdatePeriod<T>>::get();
let may_be_free =
!<NextSyncCommittee<T>>::exists() || update_period > latest_free_update_period;
if update.next_sync_committee_update.is_some() && may_be_free {
<LatestFreeSyncCommitteeUpdatePeriod<T>>::set(update_period);
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 update.finalized_header.slot >= latest_slot + T::FreeHeadersInterval::get() as u64 {
// If the latest finalized header is larger than the minimum slot interval, the header
// import transaction is free.
if update.finalized_header.slot >=
latest_slot.saturating_add(T::FreeHeadersInterval::get() as u64)
{
return Pays::No;
}

Expand Down
17 changes: 2 additions & 15 deletions bridges/snowbridge/pallets/ethereum-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,13 @@ pub fn load_next_finalized_header_update_fixture() -> snowbridge_beacon_primitiv
load_fixture("next-finalized-header-update.json".to_string()).unwrap()
}

pub fn load_test_checkpoint_update_fixture(
) -> snowbridge_beacon_primitives::CheckpointUpdate<{ config::SYNC_COMMITTEE_SIZE }> {
load_fixture("initial-checkpoint-test.json".to_string()).unwrap()
}

pub fn load_test_sync_committee_update_fixture() -> snowbridge_beacon_primitives::Update<
pub fn load_sync_committee_update_period_0_fixture() -> snowbridge_beacon_primitives::Update<
{ config::SYNC_COMMITTEE_SIZE },
{ config::SYNC_COMMITTEE_BITS_SIZE },
> {
load_fixture("sync-committee-update-test.json".to_string()).unwrap()
load_fixture("sync-committee-update-period-0.json".to_string()).unwrap()
}

pub fn load_second_test_sync_committee_update_fixture() -> snowbridge_beacon_primitives::Update<
{ config::SYNC_COMMITTEE_SIZE },
{ config::SYNC_COMMITTEE_BITS_SIZE },
> {
load_fixture("second-sync-committee-update-test.json".to_string()).unwrap()
}


pub fn get_message_verification_payload() -> (Log, Proof) {
let inbound_fixture = snowbridge_pallet_ethereum_client_fixtures::make_inbound_fixture();
(inbound_fixture.message.event_log, inbound_fixture.message.proof)
Expand Down
28 changes: 16 additions & 12 deletions bridges/snowbridge/pallets/ethereum-client/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ fn submit_update_in_current_period() {
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
let result = EthereumBeaconClient::submit(RuntimeOrigin::signed(1), update.clone());
assert_ok!(result);
assert_eq!(result.unwrap().pays_fee, Pays::Yes);
assert_eq!(result.unwrap().pays_fee, Pays::No);
let block_root: H256 = update.finalized_header.hash_tree_root().unwrap();
assert!(<FinalizedBeaconState<Test>>::contains_key(block_root));
});
Expand Down Expand Up @@ -693,32 +693,36 @@ fn duplicate_sync_committee_updates_are_not_free() {
// Check that if the same update is submitted, the update is not free.
let second_result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), sync_committee_update);
assert_err!(second_result, Error::<Test>::IrrelevantUpdate);
assert_eq!(second_result.unwrap_err().post_info.pays_fee, Pays::Yes);
assert_ok!(second_result);
assert_eq!(second_result.unwrap().pays_fee, Pays::Yes);
});
}

#[test]
fn sync_committee_update_for_sync_committee_already_imported_are_not_free() {
let checkpoint = Box::new(load_test_checkpoint_update_fixture());
let sync_committee_update = Box::new(load_test_sync_committee_update_fixture());
let second_sync_committee_update = Box::new(load_second_test_sync_committee_update_fixture());
let checkpoint = Box::new(load_checkpoint_update_fixture());
let sync_committee_update = Box::new(load_sync_committee_update_fixture());
let second_sync_committee_update = Box::new(load_sync_committee_update_period_0_fixture());
let third_sync_committee_update = Box::new(load_next_sync_committee_update_fixture());

new_tester().execute_with(|| {
println!("checkpoint =============");
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint));
println!("submit 1 =============");

// Check that setting the next sync committee for period 0 is free (it is not set yet).
let result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), sync_committee_update.clone());
assert_ok!(result);
assert_eq!(result.unwrap().pays_fee, Pays::No);

println!("submit 2 =============");
// Check that if the same update is submitted, the update is not free.
// Check that setting the next sync committee for period 0 again is not free.
let second_result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), second_sync_committee_update);
//assert_err!(second_result, Error::<Test>::IrrelevantUpdate);
assert_eq!(second_result.unwrap_err().post_info.pays_fee, Pays::Yes);
assert_eq!(second_result.unwrap().pays_fee, Pays::Yes);

// Check that setting the next sync committee for period 1 is free.
let third_result =
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), third_sync_committee_update);
assert_eq!(third_result.unwrap().pays_fee, Pays::No);
});
}

Expand Down
Loading

0 comments on commit c8307f7

Please sign in to comment.