github.com/prysmaticlabs/prysm@v1.4.4/beacon-chain/operations/attestations/kv/unaggregated.go (about) 1 package kv 2 3 import ( 4 "context" 5 6 "github.com/prysmaticlabs/prysm/shared/copyutil" 7 8 "github.com/pkg/errors" 9 types "github.com/prysmaticlabs/eth2-types" 10 "github.com/prysmaticlabs/prysm/beacon-chain/core/helpers" 11 ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1" 12 "go.opencensus.io/trace" 13 ) 14 15 // SaveUnaggregatedAttestation saves an unaggregated attestation in cache. 16 func (c *AttCaches) SaveUnaggregatedAttestation(att *ethpb.Attestation) error { 17 if att == nil { 18 return nil 19 } 20 if helpers.IsAggregated(att) { 21 return errors.New("attestation is aggregated") 22 } 23 24 seen, err := c.hasSeenBit(att) 25 if err != nil { 26 return err 27 } 28 if seen { 29 return nil 30 } 31 32 r, err := hashFn(att) 33 if err != nil { 34 return errors.Wrap(err, "could not tree hash attestation") 35 } 36 att = copyutil.CopyAttestation(att) // Copied. 37 c.unAggregateAttLock.Lock() 38 defer c.unAggregateAttLock.Unlock() 39 c.unAggregatedAtt[r] = att 40 41 return nil 42 } 43 44 // SaveUnaggregatedAttestations saves a list of unaggregated attestations in cache. 45 func (c *AttCaches) SaveUnaggregatedAttestations(atts []*ethpb.Attestation) error { 46 for _, att := range atts { 47 if err := c.SaveUnaggregatedAttestation(att); err != nil { 48 return err 49 } 50 } 51 52 return nil 53 } 54 55 // UnaggregatedAttestations returns all the unaggregated attestations in cache. 56 func (c *AttCaches) UnaggregatedAttestations() ([]*ethpb.Attestation, error) { 57 c.unAggregateAttLock.Lock() 58 defer c.unAggregateAttLock.Unlock() 59 unAggregatedAtts := c.unAggregatedAtt 60 atts := make([]*ethpb.Attestation, 0, len(unAggregatedAtts)) 61 for _, att := range unAggregatedAtts { 62 seen, err := c.hasSeenBit(att) 63 if err != nil { 64 return nil, err 65 } 66 if !seen { 67 atts = append(atts, copyutil.CopyAttestation(att) /* Copied */) 68 } 69 } 70 return atts, nil 71 } 72 73 // UnaggregatedAttestationsBySlotIndex returns the unaggregated attestations in cache, 74 // filtered by committee index and slot. 75 func (c *AttCaches) UnaggregatedAttestationsBySlotIndex(ctx context.Context, slot types.Slot, committeeIndex types.CommitteeIndex) []*ethpb.Attestation { 76 ctx, span := trace.StartSpan(ctx, "operations.attestations.kv.UnaggregatedAttestationsBySlotIndex") 77 defer span.End() 78 79 atts := make([]*ethpb.Attestation, 0) 80 81 c.unAggregateAttLock.RLock() 82 defer c.unAggregateAttLock.RUnlock() 83 84 unAggregatedAtts := c.unAggregatedAtt 85 for _, a := range unAggregatedAtts { 86 if slot == a.Data.Slot && committeeIndex == a.Data.CommitteeIndex { 87 atts = append(atts, a) 88 } 89 } 90 91 return atts 92 } 93 94 // DeleteUnaggregatedAttestation deletes the unaggregated attestations in cache. 95 func (c *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error { 96 if att == nil { 97 return nil 98 } 99 if helpers.IsAggregated(att) { 100 return errors.New("attestation is aggregated") 101 } 102 103 if err := c.insertSeenBit(att); err != nil { 104 return err 105 } 106 107 r, err := hashFn(att) 108 if err != nil { 109 return errors.Wrap(err, "could not tree hash attestation") 110 } 111 112 c.unAggregateAttLock.Lock() 113 defer c.unAggregateAttLock.Unlock() 114 delete(c.unAggregatedAtt, r) 115 116 return nil 117 } 118 119 // DeleteSeenUnaggregatedAttestations deletes the unaggregated attestations in cache 120 // that have been already processed once. Returns number of attestations deleted. 121 func (c *AttCaches) DeleteSeenUnaggregatedAttestations() (int, error) { 122 c.unAggregateAttLock.Lock() 123 defer c.unAggregateAttLock.Unlock() 124 125 count := 0 126 for _, att := range c.unAggregatedAtt { 127 if att == nil || helpers.IsAggregated(att) { 128 continue 129 } 130 if seen, err := c.hasSeenBit(att); err == nil && seen { 131 r, err := hashFn(att) 132 if err != nil { 133 return count, errors.Wrap(err, "could not tree hash attestation") 134 } 135 delete(c.unAggregatedAtt, r) 136 count++ 137 } 138 } 139 return count, nil 140 } 141 142 // UnaggregatedAttestationCount returns the number of unaggregated attestations key in the pool. 143 func (c *AttCaches) UnaggregatedAttestationCount() int { 144 c.unAggregateAttLock.RLock() 145 defer c.unAggregateAttLock.RUnlock() 146 return len(c.unAggregatedAtt) 147 }