github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/kv/block.go (about)

     1  package kv
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
     6  	"github.com/prysmaticlabs/prysm/shared/copyutil"
     7  )
     8  
     9  // SaveBlockAttestation saves an block attestation in cache.
    10  func (c *AttCaches) SaveBlockAttestation(att *ethpb.Attestation) error {
    11  	if att == nil {
    12  		return nil
    13  	}
    14  	r, err := hashFn(att.Data)
    15  	if err != nil {
    16  		return errors.Wrap(err, "could not tree hash attestation")
    17  	}
    18  
    19  	c.blockAttLock.Lock()
    20  	defer c.blockAttLock.Unlock()
    21  	atts, ok := c.blockAtt[r]
    22  	if !ok {
    23  		atts = make([]*ethpb.Attestation, 0, 1)
    24  	}
    25  
    26  	// Ensure that this attestation is not already fully contained in an existing attestation.
    27  	for _, a := range atts {
    28  		if c, err := a.AggregationBits.Contains(att.AggregationBits); err != nil {
    29  			return err
    30  		} else if c {
    31  			return nil
    32  		}
    33  	}
    34  
    35  	c.blockAtt[r] = append(atts, copyutil.CopyAttestation(att))
    36  
    37  	return nil
    38  }
    39  
    40  // SaveBlockAttestations saves a list of block attestations in cache.
    41  func (c *AttCaches) SaveBlockAttestations(atts []*ethpb.Attestation) error {
    42  	for _, att := range atts {
    43  		if err := c.SaveBlockAttestation(att); err != nil {
    44  			return err
    45  		}
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // BlockAttestations returns the block attestations in cache.
    52  func (c *AttCaches) BlockAttestations() []*ethpb.Attestation {
    53  	atts := make([]*ethpb.Attestation, 0)
    54  
    55  	c.blockAttLock.RLock()
    56  	defer c.blockAttLock.RUnlock()
    57  	for _, att := range c.blockAtt {
    58  		atts = append(atts, att...)
    59  	}
    60  
    61  	return atts
    62  }
    63  
    64  // DeleteBlockAttestation deletes a block attestation in cache.
    65  func (c *AttCaches) DeleteBlockAttestation(att *ethpb.Attestation) error {
    66  	if att == nil {
    67  		return nil
    68  	}
    69  	r, err := hashFn(att.Data)
    70  	if err != nil {
    71  		return errors.Wrap(err, "could not tree hash attestation")
    72  	}
    73  
    74  	c.blockAttLock.Lock()
    75  	defer c.blockAttLock.Unlock()
    76  	delete(c.blockAtt, r)
    77  
    78  	return nil
    79  }