Files
prysm/beacon-chain/operations/attestations/kv/block.go
Radosław Kapka 0fbb27d8e3 EIP-7549: Add aggregate attestation interfaces (#14029)
* interfaces move

* build fix

* remove annoying warning

* more build fixes

* review
2024-05-22 16:14:08 +00:00

68 lines
1.5 KiB
Go

package kv
import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
)
// SaveBlockAttestation saves an block attestation in cache.
func (c *AttCaches) SaveBlockAttestation(att ethpb.Att) error {
if att == nil {
return nil
}
r, err := hashFn(att.GetData())
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
c.blockAttLock.Lock()
defer c.blockAttLock.Unlock()
atts, ok := c.blockAtt[r]
if !ok {
atts = make([]ethpb.Att, 0, 1)
}
// Ensure that this attestation is not already fully contained in an existing attestation.
for _, a := range atts {
if c, err := a.GetAggregationBits().Contains(att.GetAggregationBits()); err != nil {
return err
} else if c {
return nil
}
}
c.blockAtt[r] = append(atts, att.Copy())
return nil
}
// BlockAttestations returns the block attestations in cache.
func (c *AttCaches) BlockAttestations() []ethpb.Att {
atts := make([]ethpb.Att, 0)
c.blockAttLock.RLock()
defer c.blockAttLock.RUnlock()
for _, att := range c.blockAtt {
atts = append(atts, att...)
}
return atts
}
// DeleteBlockAttestation deletes a block attestation in cache.
func (c *AttCaches) DeleteBlockAttestation(att ethpb.Att) error {
if att == nil {
return nil
}
r, err := hashFn(att.GetData())
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
c.blockAttLock.Lock()
defer c.blockAttLock.Unlock()
delete(c.blockAtt, r)
return nil
}