prysm-pulse/beacon-chain/operations/attestations/kv/aggregated.go

58 lines
1.5 KiB
Go
Raw Normal View History

package kv
import (
"github.com/patrickmn/go-cache"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
)
// SaveAggregatedAttestation saves an aggregated attestation in cache.
func (p *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
if !aggregated(att.AggregationBits) {
return errors.New("attestation is not aggregated")
}
r, err := ssz.HashTreeRoot(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
// DefaultExpiration is set to what was given to New(). In this case
// it's one epoch.
p.aggregatedAtt.Set(string(r[:]), att, cache.DefaultExpiration)
return nil
}
// AggregatedAttestation returns the aggregated attestations in cache.
func (p *AttCaches) AggregatedAttestation() []*ethpb.Attestation {
atts := make([]*ethpb.Attestation, 0, p.aggregatedAtt.ItemCount())
for s, i := range p.aggregatedAtt.Items() {
// Type assertion for the worst case. This shouldn't happen.
att, ok := i.Object.(*ethpb.Attestation)
if !ok {
p.aggregatedAtt.Delete(s)
}
atts = append(atts, att)
}
return atts
}
// DeleteAggregatedAttestation deletes the aggregated attestations in cache.
func (p *AttCaches) DeleteAggregatedAttestation(att *ethpb.Attestation) error {
if !aggregated(att.AggregationBits) {
return errors.New("attestation is not aggregated")
}
r, err := ssz.HashTreeRoot(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
p.aggregatedAtt.Delete(string(r[:]))
return nil
}