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
// 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 ssz_derive::Ssz;

#[cfg(feature = "serde")]
use serde_derive::{Serialize, Deserialize};
#[cfg(feature = "parity-codec")]
use codec::{Encode, Decode};

use crate::primitives::{Signature, ValidatorId, H256};
use crate::Config;

#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
/// Validator record.
pub struct Validator {
	/// BLS public key
	pub pubkey: ValidatorId,
	/// Withdrawal credentials
	pub withdrawal_credentials: H256,
	/// Epoch when validator activated
	pub activation_epoch: u64,
	/// Epoch when validator exited
	pub exit_epoch: u64,
	/// Epoch when validator is eligible to withdraw
	pub withdrawable_epoch: u64,
	/// Did the validator initiate an exit
	pub initiated_exit: bool,
	/// Was the validator slashed
	pub slashed: bool,
}

impl Validator {
	/// Activate the validator.
	pub fn activate<C: Config>(&mut self, delayed_activation_exit_epoch: u64, is_genesis: bool, config: &C) {
		if is_genesis {
			self.activation_epoch = config.genesis_epoch();
		} else {
			self.activation_epoch = delayed_activation_exit_epoch;
		}
	}

	/// Initiate exit for this validator.
	pub fn initiate_exit(&mut self) {
		self.initiated_exit = true;
	}

	/// Exit the validator.
	pub fn exit(&mut self, delayed_activation_exit_epoch: u64) {
		if self.exit_epoch <= delayed_activation_exit_epoch {
			return
		} else {
			self.exit_epoch = delayed_activation_exit_epoch;
		}
	}

	/// Whether the validator is active in given epoch.
	pub fn is_active(&self, epoch: u64) -> bool {
		self.activation_epoch <= epoch && epoch < self.exit_epoch
	}
}

#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
/// Block voluntary exit.
pub struct VoluntaryExit {
	/// Minimum epoch for processing exit
	pub epoch: u64,
	/// Index of the exiting validator
	pub validator_index: u64,
	/// Validator signature
	#[ssz(truncate)]
	pub signature: Signature,
}

#[derive(Ssz, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(Encode, Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
/// Block transfer.
pub struct Transfer {
	/// Sender index
	pub sender: u64,
	/// Recipient index
	pub recipient: u64,
	/// Amount in Gwei
	pub amount: u64,
	/// Fee in Gwei for block proposer
	pub fee: u64,
	/// Inclusion slot
	pub slot: u64,
	/// Sender withdrawal pubkey
	pub pubkey: ValidatorId,
	/// Sender signature
	#[ssz(truncate)]
	pub signature: Signature,
}