prysm-pulse/beacon-chain/operations/attestations/kv/forkchoice.go
Preston Van Loon fd80f73286
Improve make() capacity allocations (#6517)
* attestationutil.AttestingIndices: Minor improvement on indices array allocation
* progress
* more progress on makes
* progress
* more progress
* Merge branch 'master' of github.com:prysmaticlabs/prysm into memory1
* gaz
* fmt and imports
* Merge branch 'master' into memory2
* Min()
* remove spaces
* Merge branch 'master' of github.com:prysmaticlabs/prysm into memory2
* revert beacon-chain/operations/attestations/kv/block.go
* partially revert beacon-chain/operations/attestations/kv/aggregated.go
* Merge branch 'master' into memory2
* Merge branch 'master' into memory2
2020-07-09 15:50:58 +00:00

66 lines
1.6 KiB
Go

package kv
import (
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
)
// SaveForkchoiceAttestation saves an forkchoice attestation in cache.
func (p *AttCaches) SaveForkchoiceAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
r, err := hashFn(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
p.forkchoiceAttLock.Lock()
defer p.forkchoiceAttLock.Unlock()
p.forkchoiceAtt[r] = stateTrie.CopyAttestation(att) // Copied.
return nil
}
// SaveForkchoiceAttestations saves a list of forkchoice attestations in cache.
func (p *AttCaches) SaveForkchoiceAttestations(atts []*ethpb.Attestation) error {
for _, att := range atts {
if err := p.SaveForkchoiceAttestation(att); err != nil {
return err
}
}
return nil
}
// ForkchoiceAttestations returns the forkchoice attestations in cache.
func (p *AttCaches) ForkchoiceAttestations() []*ethpb.Attestation {
p.forkchoiceAttLock.RLock()
defer p.forkchoiceAttLock.RUnlock()
atts := make([]*ethpb.Attestation, 0, len(p.forkchoiceAtt))
for _, att := range p.forkchoiceAtt {
atts = append(atts, stateTrie.CopyAttestation(att) /* Copied */)
}
return atts
}
// DeleteForkchoiceAttestation deletes a forkchoice attestation in cache.
func (p *AttCaches) DeleteForkchoiceAttestation(att *ethpb.Attestation) error {
if att == nil {
return nil
}
r, err := hashFn(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}
p.forkchoiceAttLock.Lock()
defer p.forkchoiceAttLock.Unlock()
delete(p.forkchoiceAtt, r)
return nil
}