prysm-pulse/beacon-chain/operations/attestations/kv/aggregated.go
terence tsao f75a5a5df8 Implement Atts Pool Curl Methods (#4191)
* New pool
* Better namings
* Fmt
* Gazelle
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into define-pool
* Raul's feedback
* Raul's feedback
* Update to use go-cache
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into define-pool-1
* Update workspace
* Update workspace
* Update pool to use interface
* Move kv init methods
* Curd for aggregated
* Curd for unaggregated
* Gaz
* Tests for aggregated
* Fixed test
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into curd
* Minor fixes
* Typoe
* pool test
* Added deletions as well
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into curd
* Update beacon-chain/operations/attestations/kv/aggregated.go
* Update beacon-chain/operations/attestations/kv/aggregated.go
* Update beacon-chain/operations/attestations/kv/unaggregated_test.go
* Update beacon-chain/operations/attestations/kv/kv.go
2019-12-04 18:30:45 +00:00

58 lines
1.5 KiB
Go

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
}