github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/kv/forkchoice.go (about) 1 package kv 2 3 import ( 4 "github.com/pkg/errors" 5 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 6 "github.com/prysmaticlabs/prysm/shared/copyutil" 7 ) 8 9 // SaveForkchoiceAttestation saves an forkchoice attestation in cache. 10 func (c *AttCaches) SaveForkchoiceAttestation(att *ethpb.Attestation) error { 11 if att == nil { 12 return nil 13 } 14 r, err := hashFn(att) 15 if err != nil { 16 return errors.Wrap(err, "could not tree hash attestation") 17 } 18 19 att = copyutil.CopyAttestation(att) 20 c.forkchoiceAttLock.Lock() 21 defer c.forkchoiceAttLock.Unlock() 22 c.forkchoiceAtt[r] = att 23 24 return nil 25 } 26 27 // SaveForkchoiceAttestations saves a list of forkchoice attestations in cache. 28 func (c *AttCaches) SaveForkchoiceAttestations(atts []*ethpb.Attestation) error { 29 for _, att := range atts { 30 if err := c.SaveForkchoiceAttestation(att); err != nil { 31 return err 32 } 33 } 34 35 return nil 36 } 37 38 // ForkchoiceAttestations returns the forkchoice attestations in cache. 39 func (c *AttCaches) ForkchoiceAttestations() []*ethpb.Attestation { 40 c.forkchoiceAttLock.RLock() 41 defer c.forkchoiceAttLock.RUnlock() 42 43 atts := make([]*ethpb.Attestation, 0, len(c.forkchoiceAtt)) 44 for _, att := range c.forkchoiceAtt { 45 atts = append(atts, copyutil.CopyAttestation(att) /* Copied */) 46 } 47 48 return atts 49 } 50 51 // DeleteForkchoiceAttestation deletes a forkchoice attestation in cache. 52 func (c *AttCaches) DeleteForkchoiceAttestation(att *ethpb.Attestation) error { 53 if att == nil { 54 return nil 55 } 56 r, err := hashFn(att) 57 if err != nil { 58 return errors.Wrap(err, "could not tree hash attestation") 59 } 60 61 c.forkchoiceAttLock.Lock() 62 defer c.forkchoiceAttLock.Unlock() 63 delete(c.forkchoiceAtt, r) 64 65 return nil 66 } 67 68 // ForkchoiceAttestationCount returns the number of fork choice attestations key in the pool. 69 func (c *AttCaches) ForkchoiceAttestationCount() int { 70 c.forkchoiceAttLock.RLock() 71 defer c.forkchoiceAttLock.RUnlock() 72 return len(c.forkchoiceAtt) 73 }