1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2018 Parity Technologies (UK) Ltd.
// This file is part of Substrate Shasper.

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

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

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

use crate::primitives::*;
use crate::types::*;
use crate::{Config, BeaconState, BLSConfig, Error};
use bm_le::{MaxVec, Compact, tree_root};

/// Generate genesis state and genesis block from given deposits, timestamp and eth1 data.
pub fn genesis<C: Config, BLS: BLSConfig>(
	genesis_validator_deposits: &[Deposit],
	genesis_time: Uint,
	genesis_eth1_data: Eth1Data,
) -> Result<(BeaconBlock<C>, BeaconState<C>), Error> {
	let state = genesis_beacon_state::<C, BLS>(
		genesis_validator_deposits, genesis_time, genesis_eth1_data
	)?;

	Ok((BeaconBlock {
		state_root: tree_root::<C::Digest, _>(&state),
		..Default::default()
	}, state))
}

/// Generate genesis state from given deposits, timestamp, and eth1 data.
pub fn genesis_beacon_state<C: Config, BLS: BLSConfig>(
	deposits: &[Deposit],
	genesis_time: Uint,
	genesis_eth1_data: Eth1Data,
) -> Result<BeaconState<C>, Error> {
	let mut state = BeaconState {
		genesis_time,
		eth1_data: genesis_eth1_data,
		latest_block_header: BeaconBlockHeader {
			body_root: tree_root::<C::Digest, _>(
				&BeaconBlockBody::<C>::default()
			),
			..Default::default()
		},
		..BeaconState::<C>::default()
	};

	for deposit in deposits {
		state.process_deposit::<BLS>(deposit.clone())?;
	}

	for validator in state.validators.iter_mut() {
		if validator.effective_balance >= C::max_effective_balance() {
			validator.activation_eligibility_epoch = C::genesis_epoch();
			validator.activation_epoch = C::genesis_epoch();
		}
	}

	// Populate latest_active_index_roots
	let genesis_active_index_root = tree_root::<C::Digest, _>(
		&Compact(MaxVec::<_, C::ValidatorRegistryLimit>::from(
			state.active_validator_indices(C::genesis_epoch())
		))
	);
	for index in 0..C::epochs_per_historical_vector() {
		state.active_index_roots[index as usize] =
			genesis_active_index_root;
	}

	Ok(state)
}