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_gloas_checkpoint_fixture(
36) -> snowbridge_beacon_primitives::CheckpointUpdate<{ config::SYNC_COMMITTEE_SIZE }> {
37	load_fixture("gloas-checkpoint.json".to_string()).unwrap()
38}
39
40pub fn load_sync_committee_update_fixture() -> snowbridge_beacon_primitives::Update<
41	{ config::SYNC_COMMITTEE_SIZE },
42	{ config::SYNC_COMMITTEE_BITS_SIZE },
43> {
44	load_fixture("sync-committee-update.json".to_string()).unwrap()
45}
46
47pub fn load_finalized_header_update_fixture() -> snowbridge_beacon_primitives::Update<
48	{ config::SYNC_COMMITTEE_SIZE },
49	{ config::SYNC_COMMITTEE_BITS_SIZE },
50> {
51	load_fixture("finalized-header-update.json".to_string()).unwrap()
52}
53
54pub fn load_next_sync_committee_update_fixture() -> snowbridge_beacon_primitives::Update<
55	{ config::SYNC_COMMITTEE_SIZE },
56	{ config::SYNC_COMMITTEE_BITS_SIZE },
57> {
58	load_fixture("next-sync-committee-update.json".to_string()).unwrap()
59}
60
61pub fn load_next_finalized_header_update_fixture() -> snowbridge_beacon_primitives::Update<
62	{ config::SYNC_COMMITTEE_SIZE },
63	{ config::SYNC_COMMITTEE_BITS_SIZE },
64> {
65	load_fixture("next-finalized-header-update.json".to_string()).unwrap()
66}
67
68pub fn load_sync_committee_update_period_0() -> Box<
69	snowbridge_beacon_primitives::Update<
70		{ config::SYNC_COMMITTEE_SIZE },
71		{ config::SYNC_COMMITTEE_BITS_SIZE },
72	>,
73> {
74	Box::new(load_fixture("sync-committee-update-period-0.json".to_string()).unwrap())
75}
76
77pub fn load_sync_committee_update_period_0_older_fixture() -> Box<
78	snowbridge_beacon_primitives::Update<
79		{ config::SYNC_COMMITTEE_SIZE },
80		{ config::SYNC_COMMITTEE_BITS_SIZE },
81	>,
82> {
83	Box::new(load_fixture("sync-committee-update-period-0-older.json".to_string()).unwrap())
84}
85
86pub fn load_sync_committee_update_period_0_newer_fixture() -> Box<
87	snowbridge_beacon_primitives::Update<
88		{ config::SYNC_COMMITTEE_SIZE },
89		{ config::SYNC_COMMITTEE_BITS_SIZE },
90	>,
91> {
92	Box::new(load_fixture("sync-committee-update-period-0-newer.json".to_string()).unwrap())
93}
94
95pub fn get_message_verification_payload() -> (Log, Proof) {
96	let inbound_fixture = snowbridge_pallet_ethereum_client_fixtures::make_inbound_fixture();
97	(inbound_fixture.event.event_log, inbound_fixture.event.proof)
98}
99
100frame_support::construct_runtime!(
101	pub enum Test {
102		System: frame_system::{Pallet, Call, Storage, Event<T>},
103		Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
104		EthereumBeaconClient: ethereum_beacon_client::{Pallet, Call, Storage, Event<T>},
105	}
106);
107
108#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
109impl frame_system::Config for Test {
110	type Block = Block;
111}
112
113impl pallet_timestamp::Config for Test {
114	type Moment = u64;
115	type OnTimestampSet = ();
116	type MinimumPeriod = ();
117	type WeightInfo = ();
118}
119
120parameter_types! {
121	pub const ChainForkVersions: ForkVersions = ForkVersions {
122		genesis: Fork {
123			version: hex!("00000000"),
124			epoch: 0,
125		},
126		altair: Fork {
127			version: hex!("01000000"),
128			epoch: 0,
129		},
130		bellatrix: Fork {
131			version: hex!("02000000"),
132			epoch: 0,
133		},
134		capella: Fork {
135			version: hex!("03000000"),
136			epoch: 0,
137		},
138		deneb: Fork {
139			version: hex!("04000000"),
140			epoch: 0,
141		},
142		electra: Fork {
143			version: hex!("05000000"),
144			epoch: 0,
145		},
146		fulu: Fork {
147			version: hex!("06000000"),
148			epoch: 2000,
149		},
150		gloas: Fork {
151			version: hex!("80733183"),
152			epoch: 3000,
153		}
154	};
155}
156
157pub const FREE_SLOTS_INTERVAL: u32 = config::SLOTS_PER_EPOCH as u32;
158
159impl ethereum_beacon_client::Config for Test {
160	type RuntimeEvent = RuntimeEvent;
161	type ForkVersions = ChainForkVersions;
162	type FreeHeadersInterval = ConstU32<FREE_SLOTS_INTERVAL>;
163	type WeightInfo = ();
164}
165
166// Build genesis storage according to the mock runtime.
167pub fn new_tester() -> sp_io::TestExternalities {
168	let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
169	let ext = sp_io::TestExternalities::new(t);
170	ext
171}
172
173pub fn initialize_storage() -> DispatchResult {
174	let inbound_fixture = snowbridge_pallet_ethereum_client_fixtures::make_inbound_fixture();
175	EthereumBeaconClient::store_finalized_header(
176		inbound_fixture.finalized_header,
177		inbound_fixture.block_roots_root,
178	)
179}