From f8d4e742ad754bc3d480e837ab043125f188c5c1 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 21 May 2019 11:54:20 +1000 Subject: [PATCH] types: safe accessors for current crosslinks --- eth2/types/src/beacon_state.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 5dcd66e97a..a96cfecb8a 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -629,6 +629,24 @@ impl BeaconState { } } + /// 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 BeaconState { &self, data: &AttestationData, spec: &ChainSpec, - ) -> Crosslink { - Crosslink { + ) -> Result { + 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`.