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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// 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/>.

//! Minimal beacon chain state transition implementation for Serenity.

#![cfg_attr(not(feature = "std"), no_std, feature(alloc), feature(alloc_prelude), feature(prelude_import))]

#![warn(missing_docs)]

#[cfg(not(feature = "std"))]
extern crate alloc;

#[cfg(not(feature = "std"))]
pub(crate) mod prelude {
	pub use core::prelude::v1::*;
	pub use alloc::prelude::v1::*;
}

#[cfg(not(feature = "std"))]
#[allow(unused)]
#[prelude_import]
use crate::prelude::*;

#[cfg(feature = "parity-codec")]
extern crate parity_codec as codec;

mod attestation;
mod block;
mod eth1;
mod slashing;
mod state;
mod validator;
mod util;
mod error;
mod config;
mod executive;
mod primitives;

pub use attestation::*;
pub use block::*;
pub use eth1::*;
pub use slashing::*;
pub use state::*;
pub use validator::*;
pub use error::*;
pub use config::*;
pub use executive::*;
pub use primitives::*;

/// Gwei as in the currency ETH.
pub type Gwei = u64;
/// Slot type.
pub type Slot = u64;
/// Epoch type.
pub type Epoch = u64;
/// Shard type.
pub type Shard = u64;
/// Timestamp type.
pub type Timestamp = u64;
/// Index type for validators.
pub type ValidatorIndex = u64;

/// Given a block, execute based on a parent state.
pub fn execute_block<C: Config>(block: &BeaconBlock, state: &mut BeaconState, config: &C) -> Result<(), Error> {
	let mut executive = Executive::new(state, config);

	while executive.state().slot < block.slot {
		executive.update_cache();

		if (executive.state().slot + 1) % config.slots_per_epoch() == 0 {
			executive.update_justification_and_finalization()?;
			executive.update_crosslinks()?;
			executive.update_eth1_period();
			executive.update_rewards()?;
			executive.update_ejections();
			executive.update_registry_and_shuffling_data()?;
			executive.update_slashings();
			executive.update_exit_queue();
			executive.update_finalize()?;
		}

		executive.advance_slot();

		if executive.state().slot == block.slot {
			executive.process_block_header(block)?;
			executive.process_randao(block)?;
			executive.process_eth1_data(block);

			if block.body.proposer_slashings.len() > config.max_proposer_slashings() {
				return Err(Error::TooManyProposerSlashings)
			}
			for slashing in &block.body.proposer_slashings {
				executive.push_proposer_slashing(slashing.clone())?;
			}

			if block.body.attester_slashings.len() > config.max_attester_slashings() {
				return Err(Error::TooManyAttesterSlashings)
			}
			for slashing in &block.body.attester_slashings {
				executive.push_attester_slashing(slashing.clone())?;
			}

			if block.body.attestations.len() > config.max_attestations() {
				return Err(Error::TooManyAttestations)
			}
			for attestation in &block.body.attestations {
				executive.push_attestation(attestation.clone())?;
			}

			if block.body.deposits.len() > config.max_deposits() {
				return Err(Error::TooManyDeposits)
			}
			for deposit in &block.body.deposits {
				executive.push_deposit(deposit.clone())?;
			}

			if block.body.voluntary_exits.len() > config.max_voluntary_exits() {
				return Err(Error::TooManyVoluntaryExits)
			}
			for voluntary_exit in &block.body.voluntary_exits {
				executive.push_voluntary_exit(voluntary_exit.clone())?;
			}

			if block.body.transfers.len() > config.max_transfers() {
				return Err(Error::TooManyTransfers)
			}
			for transfer in &block.body.transfers {
				executive.push_transfer(transfer.clone())?;
			}
		}
	}

	Ok(())
}