referrerpolicy=no-referrer-when-downgrade

snowbridge_pallet_ethereum_client/
mock.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>
3use crate as ethereum_beacon_client;
4use crate::config;
5use frame_support::{derive_impl, dispatch::DispatchResult, parameter_types};
6use pallet_timestamp;
7use snowbridge_beacon_primitives::{Fork, ForkVersions};
8use snowbridge_verification_primitives::{Log, Proof};
9use sp_std::default::Default;
10use std::{fs::File, path::PathBuf};
11
12type Block = frame_system::mocking::MockBlock<Test>;
13use frame_support::traits::ConstU32;
14use hex_literal::hex;
15use sp_runtime::BuildStorage;
16
17fn load_fixture<T>(basename: String) -> Result<T, serde_json::Error>
18where
19	T: for<'de> serde::Deserialize<'de>,
20{
21	let filepath: PathBuf =
22		[env!("CARGO_MANIFEST_DIR"), "tests", "fixtures", &basename].iter().collect();
23	serde_json::from_reader(File::open(filepath).unwrap())
24}
25
26pub fn load_execution_proof_fixture() -> snowbridge_beacon_primitives::ExecutionProof {
27	load_fixture("execution-proof.json".to_string()).unwrap()
28}
29
30pub fn load_checkpoint_update_fixture(
31) -> snowbridge_beacon_primitives::CheckpointUpdate<{ config::SYNC_COMMITTEE_SIZE }> {
32	load_fixture("initial-checkpoint.json".to_string()).unwrap()
33}
34
35pub fn load_sync_committee_update_fixture() -> snowbridge_beacon_primitives::Update<
36	{ config::SYNC_COMMITTEE_SIZE },
37	{ config::SYNC_COMMITTEE_BITS_SIZE },
38> {
39	load_fixture("sync-committee-update.json".to_string()).unwrap()
40}
41
42pub fn load_finalized_header_update_fixture() -> snowbridge_beacon_primitives::Update<
43	{ config::SYNC_COMMITTEE_SIZE },
44	{ config::SYNC_COMMITTEE_BITS_SIZE },
45> {
46	load_fixture("finalized-header-update.json".to_string()).unwrap()
47}
48
49pub fn load_next_sync_committee_update_fixture() -> snowbridge_beacon_primitives::Update<
50	{ config::SYNC_COMMITTEE_SIZE },
51	{ config::SYNC_COMMITTEE_BITS_SIZE },
52> {
53	load_fixture("next-sync-committee-update.json".to_string()).unwrap()
54}
55
56pub fn load_next_finalized_header_update_fixture() -> snowbridge_beacon_primitives::Update<
57	{ config::SYNC_COMMITTEE_SIZE },
58	{ config::SYNC_COMMITTEE_BITS_SIZE },
59> {
60	load_fixture("next-finalized-header-update.json".to_string()).unwrap()
61}
62
63pub fn load_sync_committee_update_period_0() -> Box<
64	snowbridge_beacon_primitives::Update<
65		{ config::SYNC_COMMITTEE_SIZE },
66		{ config::SYNC_COMMITTEE_BITS_SIZE },
67	>,
68> {
69	Box::new(load_fixture("sync-committee-update-period-0.json".to_string()).unwrap())
70}
71
72pub fn load_sync_committee_update_period_0_older_fixture() -> Box<
73	snowbridge_beacon_primitives::Update<
74		{ config::SYNC_COMMITTEE_SIZE },
75		{ config::SYNC_COMMITTEE_BITS_SIZE },
76	>,
77> {
78	Box::new(load_fixture("sync-committee-update-period-0-older.json".to_string()).unwrap())
79}
80
81pub fn load_sync_committee_update_period_0_newer_fixture() -> Box<
82	snowbridge_beacon_primitives::Update<
83		{ config::SYNC_COMMITTEE_SIZE },
84		{ config::SYNC_COMMITTEE_BITS_SIZE },
85	>,
86> {
87	Box::new(load_fixture("sync-committee-update-period-0-newer.json".to_string()).unwrap())
88}
89
90pub fn get_message_verification_payload() -> (Log, Proof) {
91	let inbound_fixture = snowbridge_pallet_ethereum_client_fixtures::make_inbound_fixture();
92	(inbound_fixture.event.event_log, inbound_fixture.event.proof)
93}
94
95frame_support::construct_runtime!(
96	pub enum Test {
97		System: frame_system::{Pallet, Call, Storage, Event<T>},
98		Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
99		EthereumBeaconClient: ethereum_beacon_client::{Pallet, Call, Storage, Event<T>},
100	}
101);
102
103#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
104impl frame_system::Config for Test {
105	type Block = Block;
106}
107
108impl pallet_timestamp::Config for Test {
109	type Moment = u64;
110	type OnTimestampSet = ();
111	type MinimumPeriod = ();
112	type WeightInfo = ();
113}
114
115parameter_types! {
116	pub const ChainForkVersions: ForkVersions = ForkVersions {
117		genesis: Fork {
118			version: hex!("00000000"),
119			epoch: 0,
120		},
121		altair: Fork {
122			version: hex!("01000000"),
123			epoch: 0,
124		},
125		bellatrix: Fork {
126			version: hex!("02000000"),
127			epoch: 0,
128		},
129		capella: Fork {
130			version: hex!("03000000"),
131			epoch: 0,
132		},
133		deneb: Fork {
134			version: hex!("04000000"),
135			epoch: 0,
136		},
137		electra: Fork {
138			version: hex!("05000000"),
139			epoch: 0,
140		}
141	};
142}
143
144pub const FREE_SLOTS_INTERVAL: u32 = config::SLOTS_PER_EPOCH as u32;
145
146impl ethereum_beacon_client::Config for Test {
147	type RuntimeEvent = RuntimeEvent;
148	type ForkVersions = ChainForkVersions;
149	type FreeHeadersInterval = ConstU32<FREE_SLOTS_INTERVAL>;
150	type WeightInfo = ();
151}
152
153// Build genesis storage according to the mock runtime.
154pub fn new_tester() -> sp_io::TestExternalities {
155	let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
156	let ext = sp_io::TestExternalities::new(t);
157	ext
158}
159
160pub fn initialize_storage() -> DispatchResult {
161	let inbound_fixture = snowbridge_pallet_ethereum_client_fixtures::make_inbound_fixture();
162	EthereumBeaconClient::store_finalized_header(
163		inbound_fixture.finalized_header,
164		inbound_fixture.block_roots_root,
165	)
166}