prysm-pulse/beacon-chain/operations/attestations/kv/unaggregated.go
Preston Van Loon c4c9a8465a
Faster hashing for attestation pool (#5217)
* use faster hash proto
* Merge branch 'master' into faster-att-pool
* gaz
* Merge branch 'faster-att-pool' of github.com:prysmaticlabs/prysm into faster-att-pool
* nil checks and failing tests
* Merge refs/heads/master into faster-att-pool
* Merge refs/heads/master into faster-att-pool
* Merge refs/heads/master into faster-att-pool
* Merge refs/heads/master into faster-att-pool
* Merge refs/heads/master into faster-att-pool
* Fix
* Merge branch 'faster-att-pool' of github.com:prysmaticlabs/prysm into faster-att-pool
* Fix tests
2020-03-26 23:55:25 +00:00

82 lines
2.1 KiB
Go

package kv
import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
)
// SaveUnaggregatedAttestation saves an unaggregated attestation in cache.
func (p *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
if helpers.IsAggregated(att) {
return errors.New("attestation is aggregated")
}
r, err := hashFn(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
p.unAggregatedAtt[r] = stateTrie.CopyAttestation(att) // Copied.
return nil
}
// SaveUnaggregatedAttestations saves a list of unaggregated attestations in cache.
func (p *AttCaches) SaveUnaggregatedAttestations(atts []*ethpb.Attestation) error {
for _, att := range atts {
if err := p.SaveUnaggregatedAttestation(att); err != nil {
return err
}
}
return nil
}
// UnaggregatedAttestations returns all the unaggregated attestations in cache.
func (p *AttCaches) UnaggregatedAttestations() []*ethpb.Attestation {
atts := make([]*ethpb.Attestation, 0)
p.unAggregateAttLock.RLock()
defer p.unAggregateAttLock.RUnlock()
for _, att := range p.unAggregatedAtt {
atts = append(atts, stateTrie.CopyAttestation(att) /* Copied */)
}
return atts
}
// DeleteUnaggregatedAttestation deletes the unaggregated attestations in cache.
func (p *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
if helpers.IsAggregated(att) {
return errors.New("attestation is aggregated")
}
r, err := hashFn(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
delete(p.unAggregatedAtt, r)
return nil
}
// UnaggregatedAttestationCount returns the number of unaggregated attestations key in the pool.
func (p *AttCaches) UnaggregatedAttestationCount() int {
p.unAggregateAttLock.RLock()
defer p.unAggregateAttLock.RUnlock()
return len(p.unAggregatedAtt)
}