-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix border condition in Snowbridge free consensus Updates (#5671)
# Description A fix for a border condition introduced with new feature #5201. A malicious relayer could spam the Ethereum client with sync committee updates that have already been imported for the period. This PR adds a storage item to track the last imported sync committee period, so that subsequent irrelevant updates are not free. Original PR: Snowfork#172 ## Integration Downstream projects are not affected. Relayers will not be able to spam the Ethereum client with irrelevant sync committee updates for free. ## Review Notes Adds a storage item to track the last free sync committee update period, so that duplicate imports are not free. --------- Co-authored-by: Adrian Catangiu <[email protected]> (cherry picked from commit 1790e42)
- Loading branch information
1 parent
b61075b
commit 7f5c7ab
Showing
8 changed files
with
1,828 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]> | ||
pub use crate::mock::*; | ||
use crate::{ | ||
config::{EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT}, | ||
functions::compute_period, | ||
mock::{ | ||
get_message_verification_payload, load_checkpoint_update_fixture, | ||
load_finalized_header_update_fixture, load_next_finalized_header_update_fixture, | ||
load_next_sync_committee_update_fixture, load_sync_committee_update_fixture, | ||
}, | ||
sync_committee_sum, verify_merkle_branch, BeaconHeader, CompactBeaconState, Error, | ||
FinalizedBeaconState, LatestFinalizedBlockRoot, NextSyncCommittee, SyncCommitteePrepared, | ||
FinalizedBeaconState, LatestFinalizedBlockRoot, LatestSyncCommitteeUpdatePeriod, | ||
NextSyncCommittee, SyncCommitteePrepared, | ||
}; | ||
|
||
pub use crate::mock::*; | ||
|
||
use crate::config::{EPOCHS_PER_SYNC_COMMITTEE_PERIOD, SLOTS_PER_EPOCH, SLOTS_PER_HISTORICAL_ROOT}; | ||
use frame_support::{assert_err, assert_noop, assert_ok, pallet_prelude::Pays}; | ||
use hex_literal::hex; | ||
use snowbridge_beacon_primitives::{ | ||
|
@@ -374,7 +373,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)); | ||
}); | ||
|
@@ -711,8 +710,56 @@ 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_checkpoint_update_fixture()); | ||
let sync_committee_update = Box::new(load_sync_committee_update_fixture()); // slot 129 | ||
let second_sync_committee_update = load_sync_committee_update_period_0(); // slot 128 | ||
let third_sync_committee_update = load_sync_committee_update_period_0_newer_fixture(); // slot 224 | ||
let fourth_sync_committee_update = load_sync_committee_update_period_0_older_fixture(); // slot 96 | ||
let fith_sync_committee_update = Box::new(load_next_sync_committee_update_fixture()); // slot 8259 | ||
|
||
new_tester().execute_with(|| { | ||
assert_ok!(EthereumBeaconClient::process_checkpoint_update(&checkpoint)); | ||
assert_eq!(<LatestSyncCommitteeUpdatePeriod<Test>>::get(), 0); | ||
|
||
// 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); | ||
assert_eq!(<LatestSyncCommitteeUpdatePeriod<Test>>::get(), 0); | ||
|
||
// 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_eq!(second_result.unwrap().pays_fee, Pays::Yes); | ||
assert_eq!(<LatestSyncCommitteeUpdatePeriod<Test>>::get(), 0); | ||
|
||
// Check that setting an update with a sync committee that has already been set, but with a | ||
// newer finalized header, is free. | ||
let third_result = | ||
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), third_sync_committee_update); | ||
assert_eq!(third_result.unwrap().pays_fee, Pays::No); | ||
assert_eq!(<LatestSyncCommitteeUpdatePeriod<Test>>::get(), 0); | ||
|
||
// Check that setting the next sync committee for period 0 again with an earlier slot is not | ||
// free. | ||
let fourth_result = | ||
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), fourth_sync_committee_update); | ||
assert_err!(fourth_result, Error::<Test>::IrrelevantUpdate); | ||
assert_eq!(fourth_result.unwrap_err().post_info.pays_fee, Pays::Yes); | ||
|
||
// Check that setting the next sync committee for period 1 is free. | ||
let fith_result = | ||
EthereumBeaconClient::submit(RuntimeOrigin::signed(1), fith_sync_committee_update); | ||
assert_eq!(fith_result.unwrap().pays_fee, Pays::No); | ||
assert_eq!(<LatestSyncCommitteeUpdatePeriod<Test>>::get(), 1); | ||
}); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.