#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
use ssz::{Codec, Encode, Decode};
use bm_le::{IntoTree, FromTree, MaxVec};
use vecarray::VecArray;
use crate::*;
use crate::primitives::*;
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Fork {
pub previous_version: Version,
pub current_version: Version,
pub epoch: Uint,
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Checkpoint {
pub epoch: Uint,
pub root: H256,
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Validator {
pub pubkey: ValidatorId,
pub withdrawal_credentials: H256,
pub effective_balance: Uint,
pub slashed: bool,
pub activation_eligibility_epoch: Uint,
pub activation_epoch: Uint,
pub exit_epoch: Uint,
pub withdrawable_epoch: Uint,
}
impl Validator {
pub fn is_active(&self, epoch: Uint) -> bool {
self.activation_epoch <= epoch && epoch < self.exit_epoch
}
pub fn is_slashable(&self, epoch: Uint) -> bool {
self.slashed == false &&
self.activation_epoch <= epoch && epoch < self.withdrawable_epoch
}
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Crosslink {
pub shard: Uint,
pub parent_root: H256,
pub start_epoch: Uint,
pub end_epoch: Uint,
pub data_root: H256,
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct AttestationData {
pub beacon_block_root: H256,
pub source: Checkpoint,
pub target: Checkpoint,
pub crosslink: Crosslink,
}
impl AttestationData {
pub fn is_slashable(&self, other: &AttestationData) -> bool {
(self != other && self.target.epoch == other.target.epoch) ||
(self.source.epoch < other.source.epoch &&
other.target.epoch < self.target.epoch)
}
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct AttestationDataAndCustodyBit {
pub data: AttestationData,
pub custody_bit: bool,
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct IndexedAttestation<C: Config> {
#[bm(compact)]
pub custody_bit_0_indices: MaxVec<Uint, C::MaxValidatorsPerCommittee>,
#[bm(compact)]
pub custody_bit_1_indices: MaxVec<Uint, C::MaxValidatorsPerCommittee>,
pub data: AttestationData,
pub signature: Signature,
}
impl<C: Config> From<IndexedAttestation<C>> for SigningIndexedAttestation<C> {
fn from(indexed: IndexedAttestation<C>) -> Self {
Self {
custody_bit_0_indices: indexed.custody_bit_0_indices,
custody_bit_1_indices: indexed.custody_bit_1_indices,
data: indexed.data
}
}
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct SigningIndexedAttestation<C: Config> {
#[bm(compact)]
pub custody_bit_0_indices: MaxVec<Uint, C::MaxValidatorsPerCommittee>,
#[bm(compact)]
pub custody_bit_1_indices: MaxVec<Uint, C::MaxValidatorsPerCommittee>,
pub data: AttestationData,
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct PendingAttestation<C: Config> {
#[bm(compact)]
#[cfg_attr(feature = "serde", serde(serialize_with = "crate::utils::serialize_bitlist"))]
#[cfg_attr(feature = "serde", serde(deserialize_with = "crate::utils::deserialize_bitlist"))]
pub aggregation_bits: MaxVec<bool, C::MaxValidatorsPerCommittee>,
pub data: AttestationData,
pub inclusion_delay: Uint,
pub proposer_index: Uint,
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Eth1Data {
pub deposit_root: H256,
pub deposit_count: Uint,
pub block_hash: H256,
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct HistoricalBatch<C: Config> {
pub block_roots: VecArray<H256, C::SlotsPerHistoricalRoot>,
pub state_roots: VecArray<H256, C::SlotsPerHistoricalRoot>,
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct DepositData {
pub pubkey: ValidatorId,
pub withdrawal_credentials: H256,
pub amount: Uint,
pub signature: Signature,
}
impl From<DepositData> for SigningDepositData {
fn from(data: DepositData) -> Self {
Self {
pubkey: data.pubkey,
withdrawal_credentials: data.withdrawal_credentials,
amount: data.amount,
}
}
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct SigningDepositData {
pub pubkey: ValidatorId,
pub withdrawal_credentials: H256,
pub amount: Uint,
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct CompactCommittee<C: Config> {
pub pubkeys: MaxVec<ValidatorId, C::MaxValidatorsPerCommittee>,
#[bm(compact)]
pub compact_validators: MaxVec<Uint, C::MaxValidatorsPerCommittee>,
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct BeaconBlockHeader {
pub slot: Uint,
pub parent_root: H256,
pub state_root: H256,
pub body_root: H256,
pub signature: Signature,
}
impl From<BeaconBlockHeader> for SigningBeaconBlockHeader {
fn from(header: BeaconBlockHeader) -> Self {
Self {
slot: header.slot,
parent_root: header.parent_root,
state_root: header.state_root,
body_root: header.body_root,
}
}
}
#[derive(Codec, Encode, Decode, FromTree, IntoTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct SigningBeaconBlockHeader {
pub slot: Uint,
pub parent_root: H256,
pub state_root: H256,
pub body_root: H256,
}