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

Commit

Permalink
finish off feature
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Jun 6, 2024
1 parent f3e569b commit 0ef0d5b
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,20 @@
use super::*;
mod util;

use crate::Pallet as EthereumBeaconClient;
use crate::{
migration::{
v0::{
CompactExecutionHeader, ExecutionHeaderIndex, ExecutionHeaderMapping,
ExecutionHeaderState, ExecutionHeaders, LatestExecutionState,
},
EthereumExecutionHeaderCleanup,
},
Pallet as EthereumBeaconClient,
};
use frame_benchmarking::v2::*;
use frame_support::{migrations::SteppedMigration, weights::WeightMeter};
use frame_system::RawOrigin;
use hex_literal::hex;

use snowbridge_pallet_ethereum_client_fixtures::*;

Expand Down Expand Up @@ -125,5 +136,60 @@ mod benchmarks {
Ok(())
}

use frame_support::parameter_types;

parameter_types! {
pub ExecutionHeaderCount: u32 = 1;
}

#[benchmark]
fn step() {
let block_root: H256 =
hex!("4e4ed8c829bf771f94c60caa052dc3b703b24165a2e6459350e3a43a80ab7a8f").into();
ExecutionHeaders::<T>::insert(
block_root,
CompactExecutionHeader {
parent_hash: hex!(
"e0a5ca63886dfa16d53347ba347289e0187f7c38320768d094fc48d331ac7a23"
)
.into(),
block_number: 48242,
state_root: hex!(
"b3f33b6950fd047b634dcea0d09f002f07431d3e6648213604e54caa822055a6"
)
.into(),
receipts_root: hex!(
"f744e1ebe846b2961a7daa3c0d9023d8b109cf9e425b9e9973f039180e487b67"
)
.into(),
},
);
ExecutionHeaderMapping::<T>::insert(0u32, block_root);
LatestExecutionState::<T>::set(ExecutionHeaderState {
beacon_block_root: hex!(
"b3f33b6950fd047b634dcea0d09f002f07431d3e6648213604e54caa822055a6"
)
.into(),
beacon_slot: 5353,
block_hash: hex!("e0a5ca63886dfa16d53347ba347289e0187f7c38320768d094fc48d331ac7a23")
.into(),
block_number: 5454,
});
ExecutionHeaderIndex::<T>::set(0);
let mut meter = WeightMeter::new();

#[block]
{
EthereumExecutionHeaderCleanup::<T, (), ExecutionHeaderCount>::step(None, &mut meter)
.unwrap();
}

// Check that the header is removed
assert_eq!(ExecutionHeaderMapping::<T>::get(0u32), H256::zero());
assert!(ExecutionHeaders::<T>::get(block_root).is_none());
assert!(LatestExecutionState::<T>::get().beacon_block_root == H256::zero());
assert!(ExecutionHeaderIndex::<T>::get() == 0);
}

impl_benchmark_test_suite!(EthereumBeaconClient, crate::mock::new_tester(), crate::mock::Test);
}
18 changes: 8 additions & 10 deletions bridges/snowbridge/pallets/ethereum-client/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use crate::pallet::Config;
use crate::{pallet::Config, WeightInfo};
use frame_support::{
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
pallet_prelude::PhantomData,
Expand All @@ -9,23 +9,21 @@ use frame_support::{
use sp_core::Get;

mod test;
pub mod weights;

pub const PALLET_MIGRATIONS_ID: &[u8; 26] = b"ethereum-execution-headers";
pub const LOG_TARGET: &str = "ethereum-client-migration";

/// Module containing the old Ethereum execution headers that should be cleaned up.
mod v0 {
use super::Config;
use crate::pallet::Pallet;
pub mod v0 {
use crate::pallet::{Config, Pallet};
use frame_support::{
pallet_prelude::{Decode, Encode, MaxEncodedLen, OptionQuery, TypeInfo, ValueQuery},
storage_alias, CloneNoBound, Identity, PartialEqNoBound, RuntimeDebugNoBound,
};
use sp_core::H256;

#[storage_alias]
pub(super) type LatestExecutionState<T: Config> =
pub type LatestExecutionState<T: Config> =
StorageValue<Pallet<T>, ExecutionHeaderState, ValueQuery>;

#[storage_alias]
Expand Down Expand Up @@ -66,10 +64,10 @@ mod v0 {
}
}

pub struct EthereumExecutionHeaderCleanup<T: Config, W: weights::WeightInfo, M: Get<u32>>(
pub struct EthereumExecutionHeaderCleanup<T: Config, W: WeightInfo, M: Get<u32>>(
PhantomData<(T, W, M)>,
);
impl<T: Config, W: weights::WeightInfo, M: Get<u32>> SteppedMigration
impl<T: Config, W: WeightInfo, M: Get<u32>> SteppedMigration
for EthereumExecutionHeaderCleanup<T, W, M>
{
type Cursor = u32;
Expand All @@ -83,7 +81,7 @@ impl<T: Config, W: weights::WeightInfo, M: Get<u32>> SteppedMigration
mut cursor: Option<Self::Cursor>,
meter: &mut WeightMeter,
) -> Result<Option<Self::Cursor>, SteppedMigrationError> {
log::info!(target: LOG_TARGET, "Starting stepped migration iteration.");
log::info!(target: LOG_TARGET, "Starting step iteration for Ethereum execution header cleanup.");
let required = W::step();
// If there is not enough weight for a single step, return an error. This case can be
// problematic if it is the first migration that ran in this block. But there is nothing
Expand All @@ -101,7 +99,7 @@ impl<T: Config, W: weights::WeightInfo, M: Get<u32>> SteppedMigration
let index = if let Some(last_key) = cursor {
last_key.saturating_add(1)
} else {
log::info!(target: LOG_TARGET, "Starting migration");
log::info!(target: LOG_TARGET, "Cursor is 0, starting migration.");
// If no cursor is provided, start iterating from the beginning.
0
};
Expand Down
20 changes: 9 additions & 11 deletions bridges/snowbridge/pallets/ethereum-client/src/migration/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@
#![cfg(all(test, not(feature = "runtime-benchmarks")))]

use crate::{
migration::{
v0::{
CompactExecutionHeader, ExecutionHeaderIndex, ExecutionHeaderMapping,
ExecutionHeaderState, ExecutionHeaders, LatestExecutionState,
},
weights::{SubstrateWeight, WeightInfo},
migration::v0::{
CompactExecutionHeader, ExecutionHeaderIndex, ExecutionHeaderMapping, ExecutionHeaderState,
ExecutionHeaders, LatestExecutionState,
},
mock::new_tester,
tests::{
run_to_block_with_migrator, AllPalletsWithSystem, ExecutionHeaderCount,
mock::{
new_tester, run_to_block_with_migrator, AllPalletsWithSystem, ExecutionHeaderCount,
MigratorServiceWeight, System, Test,
},
pallet,
weights::WeightInfo as _,
};
use frame_support::traits::OnRuntimeUpgrade;
use pallet_migrations::WeightInfo as _;
Expand Down Expand Up @@ -51,10 +49,10 @@ fn ethereum_execution_header_migration_works() {
block_roots.push(block_root);
}

// Give it enough weight to do exactly 16 iterations:
// Give it enough weight to do 16 iterations:
let limit = <Test as pallet_migrations::Config>::WeightInfo::progress_mbms_none() +
pallet_migrations::Pallet::<Test>::exec_migration_max_weight() +
SubstrateWeight::<Test>::step() * 16;
<Test as pallet::Config>::WeightInfo::step() * 16;
MigratorServiceWeight::set(&limit);
ExecutionHeaderCount::set(&(execution_header_count as u32));

Expand Down

This file was deleted.

9 changes: 2 additions & 7 deletions bridges/snowbridge/pallets/ethereum-client/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,8 @@ parameter_types! {
#[derive_impl(pallet_migrations::config_preludes::TestDefaultConfig)]
impl pallet_migrations::Config for Test {
#[cfg(not(feature = "runtime-benchmarks"))]
type Migrations = (
crate::migration::EthereumExecutionHeaderCleanup<
Test,
crate::migration::weights::SubstrateWeight<Test>,
ExecutionHeaderCount,
>,
);
type Migrations =
(crate::migration::EthereumExecutionHeaderCleanup<Test, (), ExecutionHeaderCount>,);
#[cfg(feature = "runtime-benchmarks")]
type Migrations = pallet_migrations::mock_helpers::MockedMigrations;
type MaxServiceWeight = MigratorServiceWeight;
Expand Down
8 changes: 8 additions & 0 deletions bridges/snowbridge/pallets/ethereum-client/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub trait WeightInfo {
fn force_checkpoint() -> Weight;
fn submit() -> Weight;
fn submit_with_sync_committee() -> Weight;
fn step() -> Weight;
}

// For backwards compatibility and tests
Expand All @@ -58,4 +59,11 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(6))
.saturating_add(RocksDbWeight::get().writes(1))
}

fn step() -> Weight {
Weight::from_parts(10_000_000, 0)
.saturating_add(Weight::from_parts(0, 3680))
.saturating_add(RocksDbWeight::get().reads(1))
.saturating_add(RocksDbWeight::get().writes(2))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use snowbridge_pallet_inbound_queue_fixtures::{
};
use snowbridge_pallet_system;
use snowbridge_router_primitives::inbound::{
Command, ConvertMessage, Destination, GlobalConsensusEthereumConvertsFor, MessageV1, VersionedMessage,
Command, ConvertMessage, Destination, GlobalConsensusEthereumConvertsFor, MessageV1,
VersionedMessage,
};
use sp_core::H256;
use sp_runtime::{DispatchError::Token, TokenError::FundsUnavailable};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,30 @@
// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

use crate::{xcm_config::UniversalLocation, Runtime, RuntimeEvent, Balances, EthereumSystem,
EthereumOutboundQueue, TreasuryAccount, EthereumInboundQueue, MessageQueue, TransactionByteFee, xcm_config, XcmRouter};
use snowbridge_router_primitives::outbound::EthereumBlobExporter;
use testnet_parachains_constants::rococo::snowbridge::EthereumNetwork;
use snowbridge_beacon_primitives::{Fork, ForkVersions};
use snowbridge_core::{
gwei, meth, AllowSiblingsOnly, PricingParameters, Rewards,
};
use snowbridge_router_primitives::inbound::MessageToXcm;
use testnet_parachains_constants::rococo::snowbridge::INBOUND_QUEUE_PALLET_INDEX;
use testnet_parachains_constants::rococo::{
currency::*, fee::WeightToFee,
use crate::{
xcm_config, xcm_config::UniversalLocation, Balances, EthereumInboundQueue,
EthereumOutboundQueue, EthereumSystem, MessageQueue, Runtime, RuntimeEvent, TransactionByteFee,
TreasuryAccount,
};
use parachains_common::{AccountId, Balance};
use snowbridge_beacon_primitives::{Fork, ForkVersions};
use snowbridge_core::{gwei, meth, AllowSiblingsOnly, PricingParameters, Rewards};
use snowbridge_router_primitives::{inbound::MessageToXcm, outbound::EthereumBlobExporter};
use sp_core::H160;
use testnet_parachains_constants::rococo::{
currency::*,
fee::WeightToFee,
snowbridge::{EthereumNetwork, INBOUND_QUEUE_PALLET_INDEX},
};

use sp_runtime::{traits::{ConstU32, ConstU8, Keccak256}, FixedU128};
#[cfg(feature = "runtime-benchmarks")]
use benchmark_helpers::DoNothingRouter;
use frame_support::{parameter_types, weights::ConstantMultiplier};
use pallet_xcm::EnsureXcm;
use sp_runtime::{
traits::{ConstU32, ConstU8, Keccak256},
FixedU128,
};

/// Exports message to the Ethereum Gateway contract.
pub type SnowbridgeExporter = EthereumBlobExporter<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use sp_api::impl_runtime_apis;
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{Block as BlockT},
traits::Block as BlockT,
transaction_validity::{TransactionSource, TransactionValidity},
ApplyExtrinsicResult,
};
Expand All @@ -70,9 +70,7 @@ use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
};
use testnet_parachains_constants::rococo::{
consensus::*, currency::*, fee::WeightToFee, time::*,
};
use testnet_parachains_constants::rococo::{consensus::*, currency::*, fee::WeightToFee, time::*};

use bp_runtime::HeaderId;
use bridge_hub_common::{
Expand All @@ -89,11 +87,11 @@ pub use sp_runtime::BuildStorage;

use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate};
use rococo_runtime_constants::system_parachain::{ASSET_HUB_ID, BRIDGE_HUB_ID};
use xcm::latest::prelude::*;
use snowbridge_core::{
outbound::{Command, Fee},
AgentId, PricingParameters,
};
use xcm::latest::prelude::*;

use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight};

Expand Down
Loading

0 comments on commit 0ef0d5b

Please sign in to comment.