1
// Copyright 2024 Moonbeam foundation
2
// This file is part of Moonbeam.
3

            
4
// Moonbeam is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Moonbeam is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Moonbeam.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
use crate::lazy_loading;
18
use crate::lazy_loading::backend::RPC;
19
use cumulus_primitives_core::BlockT;
20
use parity_scale_codec::Encode;
21
use sc_client_api::{Backend, BlockImportOperation, NewBlockState};
22
use sp_core::{twox_128, twox_64, H256};
23
use sp_runtime::traits::{Header, One};
24
use sp_runtime::Saturating;
25
use sp_storage::{StateVersion, Storage, StorageKey};
26
use std::sync::Arc;
27

            
28
pub fn produce_first_block<Block: BlockT + sp_runtime::DeserializeOwned>(
29
	backend: Arc<lazy_loading::backend::Backend<Block>>,
30
	fork_checkpoint: Block,
31
	mut state_overrides: Vec<(Vec<u8>, Vec<u8>)>,
32
) -> sp_blockchain::Result<()> {
33
	let mut op = backend.begin_operation()?;
34

            
35
	let header = fork_checkpoint.header().clone();
36
	let next_block_number = header.number().saturating_add(One::one());
37

            
38
	let header: Block::Header = Block::Header::new(
39
		next_block_number,
40
		Default::default(),
41
		Default::default(),
42
		header.hash(),
43
		Default::default(),
44
	);
45

            
46
	// IMPORTANT: Add first block after the fork to frame_system::BlockHash
47
	// This is required by CheckMortality/CheckEra in SignedExtension
48
	let key = [
49
		&twox_128(b"System"),
50
		&twox_128(b"BlockHash"),
51
		twox_64(&next_block_number.encode()).as_slice(),
52
		&next_block_number.encode(),
53
	]
54
	.concat();
55
	state_overrides.push((key, header.hash().encode()));
56

            
57
	let _ = op.reset_storage(
58
		Storage {
59
			top: state_overrides.into_iter().collect(),
60
			children_default: Default::default(),
61
		},
62
		StateVersion::V1,
63
	)?;
64

            
65
	// Create empty first block
66
	let _ = op.set_block_data(
67
		header.clone(),
68
		Some(Default::default()),
69
		None,
70
		None,
71
		NewBlockState::Final,
72
	);
73

            
74
	backend.commit_operation(op)
75
}
76

            
77
pub fn get_parachain_id(rpc_client: Arc<RPC>) -> Option<u32> {
78
	let key = [twox_128(b"ParachainInfo"), twox_128(b"ParachainId")].concat();
79
	let result = rpc_client.storage::<H256>(StorageKey(key), None);
80

            
81
	result
82
		.map(|o| {
83
			o.and_then(|data| {
84
				<u32 as parity_scale_codec::Decode>::decode(&mut data.0.as_slice()).ok()
85
			})
86
		})
87
		.ok()
88
		.flatten()
89
}