types: safe accessors for current crosslinks

This commit is contained in:
Michael Sproul 2019-05-21 11:54:20 +10:00
parent 9a21e41017
commit f8d4e742ad
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914

View File

@ -629,6 +629,24 @@ impl<T: EthSpec> BeaconState<T> {
}
}
/// Get the current crosslink for a shard.
///
/// Spec v0.6.1
pub fn get_current_crosslink(&self, shard: u64) -> Result<&Crosslink, Error> {
self.current_crosslinks
.get(shard as usize)
.ok_or(Error::ShardOutOfBounds)
}
/// Get the previous crosslink for a shard.
///
/// Spec v0.6.1
pub fn get_previous_crosslink(&self, shard: u64) -> Result<&Crosslink, Error> {
self.previous_crosslinks
.get(shard as usize)
.ok_or(Error::ShardOutOfBounds)
}
/// Transform an attestation into the crosslink that it reinforces.
///
/// Spec v0.6.1
@ -636,15 +654,16 @@ impl<T: EthSpec> BeaconState<T> {
&self,
data: &AttestationData,
spec: &ChainSpec,
) -> Crosslink {
Crosslink {
) -> Result<Crosslink, Error> {
let current_crosslink_epoch = self.get_current_crosslink(data.shard)?.epoch;
Ok(Crosslink {
epoch: std::cmp::min(
data.target_epoch,
self.current_crosslinks[data.shard as usize].epoch + spec.max_crosslink_epochs,
current_crosslink_epoch + spec.max_crosslink_epochs,
),
previous_crosslink_root: data.previous_crosslink_root,
crosslink_data_root: data.crosslink_data_root,
}
})
}
/// Generate a seed for the given `epoch`.