github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/state/v1/getters_checkpoint.go (about)

     1  package v1
     2  
     3  import (
     4  	"bytes"
     5  
     6  	types "github.com/prysmaticlabs/eth2-types"
     7  	"github.com/prysmaticlabs/go-bitfield"
     8  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     9  )
    10  
    11  // JustificationBits marking which epochs have been justified in the beacon chain.
    12  func (b *BeaconState) JustificationBits() bitfield.Bitvector4 {
    13  	if !b.hasInnerState() {
    14  		return nil
    15  	}
    16  	if b.state.JustificationBits == nil {
    17  		return nil
    18  	}
    19  
    20  	b.lock.RLock()
    21  	defer b.lock.RUnlock()
    22  
    23  	return b.justificationBits()
    24  }
    25  
    26  // justificationBits marking which epochs have been justified in the beacon chain.
    27  // This assumes that a lock is already held on BeaconState.
    28  func (b *BeaconState) justificationBits() bitfield.Bitvector4 {
    29  	if !b.hasInnerState() {
    30  		return nil
    31  	}
    32  	if b.state.JustificationBits == nil {
    33  		return nil
    34  	}
    35  
    36  	res := make([]byte, len(b.state.JustificationBits.Bytes()))
    37  	copy(res, b.state.JustificationBits.Bytes())
    38  	return res
    39  }
    40  
    41  // PreviousJustifiedCheckpoint denoting an epoch and block root.
    42  func (b *BeaconState) PreviousJustifiedCheckpoint() *ethpb.Checkpoint {
    43  	if !b.hasInnerState() {
    44  		return nil
    45  	}
    46  	if b.state.PreviousJustifiedCheckpoint == nil {
    47  		return nil
    48  	}
    49  
    50  	b.lock.RLock()
    51  	defer b.lock.RUnlock()
    52  
    53  	return b.previousJustifiedCheckpoint()
    54  }
    55  
    56  // previousJustifiedCheckpoint denoting an epoch and block root.
    57  // This assumes that a lock is already held on BeaconState.
    58  func (b *BeaconState) previousJustifiedCheckpoint() *ethpb.Checkpoint {
    59  	if !b.hasInnerState() {
    60  		return nil
    61  	}
    62  
    63  	return b.safeCopyCheckpoint(b.state.PreviousJustifiedCheckpoint)
    64  }
    65  
    66  // CurrentJustifiedCheckpoint denoting an epoch and block root.
    67  func (b *BeaconState) CurrentJustifiedCheckpoint() *ethpb.Checkpoint {
    68  	if !b.hasInnerState() {
    69  		return nil
    70  	}
    71  	if b.state.CurrentJustifiedCheckpoint == nil {
    72  		return nil
    73  	}
    74  
    75  	b.lock.RLock()
    76  	defer b.lock.RUnlock()
    77  
    78  	return b.currentJustifiedCheckpoint()
    79  }
    80  
    81  // currentJustifiedCheckpoint denoting an epoch and block root.
    82  // This assumes that a lock is already held on BeaconState.
    83  func (b *BeaconState) currentJustifiedCheckpoint() *ethpb.Checkpoint {
    84  	if !b.hasInnerState() {
    85  		return nil
    86  	}
    87  
    88  	return b.safeCopyCheckpoint(b.state.CurrentJustifiedCheckpoint)
    89  }
    90  
    91  // MatchCurrentJustifiedCheckpoint returns true if input justified checkpoint matches
    92  // the current justified checkpoint in state.
    93  func (b *BeaconState) MatchCurrentJustifiedCheckpoint(c *ethpb.Checkpoint) bool {
    94  	if !b.hasInnerState() {
    95  		return false
    96  	}
    97  	if b.state.CurrentJustifiedCheckpoint == nil {
    98  		return false
    99  	}
   100  
   101  	if c.Epoch != b.state.CurrentJustifiedCheckpoint.Epoch {
   102  		return false
   103  	}
   104  	return bytes.Equal(c.Root, b.state.CurrentJustifiedCheckpoint.Root)
   105  }
   106  
   107  // MatchPreviousJustifiedCheckpoint returns true if the input justified checkpoint matches
   108  // the previous justified checkpoint in state.
   109  func (b *BeaconState) MatchPreviousJustifiedCheckpoint(c *ethpb.Checkpoint) bool {
   110  	if !b.hasInnerState() {
   111  		return false
   112  	}
   113  	if b.state.PreviousJustifiedCheckpoint == nil {
   114  		return false
   115  	}
   116  
   117  	if c.Epoch != b.state.PreviousJustifiedCheckpoint.Epoch {
   118  		return false
   119  	}
   120  	return bytes.Equal(c.Root, b.state.PreviousJustifiedCheckpoint.Root)
   121  }
   122  
   123  // FinalizedCheckpoint denoting an epoch and block root.
   124  func (b *BeaconState) FinalizedCheckpoint() *ethpb.Checkpoint {
   125  	if !b.hasInnerState() {
   126  		return nil
   127  	}
   128  	if b.state.FinalizedCheckpoint == nil {
   129  		return nil
   130  	}
   131  
   132  	b.lock.RLock()
   133  	defer b.lock.RUnlock()
   134  
   135  	return b.finalizedCheckpoint()
   136  }
   137  
   138  // finalizedCheckpoint denoting an epoch and block root.
   139  // This assumes that a lock is already held on BeaconState.
   140  func (b *BeaconState) finalizedCheckpoint() *ethpb.Checkpoint {
   141  	if !b.hasInnerState() {
   142  		return nil
   143  	}
   144  
   145  	return b.safeCopyCheckpoint(b.state.FinalizedCheckpoint)
   146  }
   147  
   148  // FinalizedCheckpointEpoch returns the epoch value of the finalized checkpoint.
   149  func (b *BeaconState) FinalizedCheckpointEpoch() types.Epoch {
   150  	if !b.hasInnerState() {
   151  		return 0
   152  	}
   153  	if b.state.FinalizedCheckpoint == nil {
   154  		return 0
   155  	}
   156  	b.lock.RLock()
   157  	defer b.lock.RUnlock()
   158  
   159  	return b.state.FinalizedCheckpoint.Epoch
   160  }