#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize, de::DeserializeOwned};
use ssz::{Codec, Encode, Decode};
use bm_le::{IntoTree, FromTree, MaxVec};
use crate::*;
use crate::primitives::*;
use crate::types::*;
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(bound = "C: Config + Serialize + Clone + DeserializeOwned + 'static"))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct BeaconBlockBody<C: Config> {
pub randao_reveal: H768,
pub eth1_data: Eth1Data,
pub graffiti: H256,
pub proposer_slashings: MaxVec<ProposerSlashing, C::MaxProposerSlashings>,
pub attester_slashings: MaxVec<AttesterSlashing<C>, C::MaxAttesterSlashings>,
pub attestations: MaxVec<Attestation<C>, C::MaxAttestations>,
pub deposits: MaxVec<Deposit, C::MaxDeposits>,
pub voluntary_exits: MaxVec<VoluntaryExit, C::MaxVoluntaryExits>,
pub transfers: MaxVec<Transfer, C::MaxTransfers>,
}
pub trait Block {
type Config: Config;
fn slot(&self) -> u64;
fn parent_root(&self) -> &H256;
fn state_root(&self) -> &H256;
fn body(&self) -> &BeaconBlockBody<Self::Config>;
fn signature(&self) -> Option<&Signature>;
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(bound = "C: Config + Serialize + Clone + DeserializeOwned + 'static"))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct BeaconBlock<C: Config> {
pub slot: Uint,
pub parent_root: H256,
pub state_root: H256,
pub body: BeaconBlockBody<C>,
pub signature: Signature,
}
impl<C: Config> Block for BeaconBlock<C> {
type Config = C;
fn slot(&self) -> u64 { self.slot }
fn parent_root(&self) -> &H256 { &self.parent_root }
fn state_root(&self) -> &H256 { &self.state_root }
fn body(&self) -> &BeaconBlockBody<C> { &self.body }
fn signature(&self) -> Option<&Signature> { Some(&self.signature) }
}
impl<'a, C: Config, T: Block<Config=C>> From<&'a T> for UnsealedBeaconBlock<C> {
fn from(t: &'a T) -> UnsealedBeaconBlock<C> {
UnsealedBeaconBlock {
slot: t.slot(),
parent_root: t.parent_root().clone(),
state_root: t.state_root().clone(),
body: t.body().clone(),
}
}
}
#[derive(Codec, Encode, Decode, IntoTree, FromTree, Clone, PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(deny_unknown_fields))]
#[cfg_attr(feature = "serde", serde(bound = "C: Config + Serialize + Clone + DeserializeOwned + 'static"))]
#[cfg_attr(feature = "parity-codec", derive(parity_codec::Encode, parity_codec::Decode))]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct UnsealedBeaconBlock<C: Config> {
pub slot: Uint,
pub parent_root: H256,
pub state_root: H256,
pub body: BeaconBlockBody<C>,
}
impl<C: Config> Block for UnsealedBeaconBlock<C> {
type Config = C;
fn slot(&self) -> u64 { self.slot }
fn parent_root(&self) -> &H256 { &self.parent_root }
fn state_root(&self) -> &H256 { &self.state_root }
fn body(&self) -> &BeaconBlockBody<C> { &self.body }
fn signature(&self) -> Option<&Signature> { None }
}
impl<C: Config> UnsealedBeaconBlock<C> {
pub fn fake_seal(self) -> BeaconBlock<C> {
BeaconBlock {
slot: self.slot,
parent_root: self.parent_root,
state_root: self.state_root,
body: self.body,
signature: Default::default(),
}
}
}