Skip to content

Commit f0de09d

Browse files
authored
Check before saving attestation to pool (#6912)
* Pool: check before save atts * Pool: test * Pool: update metrics * Merge branch 'master' into check-before-save * Merge refs/heads/master into check-before-save
1 parent ac82308 commit f0de09d

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

beacon-chain/operations/attestations/kv/aggregated.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ func (p *AttCaches) SaveAggregatedAttestation(att *ethpb.Attestation) error {
7474
if !helpers.IsAggregated(att) {
7575
return errors.New("attestation is not aggregated")
7676
}
77+
has, err := p.HasAggregatedAttestation(att)
78+
if err != nil {
79+
return err
80+
}
81+
if has {
82+
return nil
83+
}
84+
7785
r, err := hashFn(att.Data)
7886
if err != nil {
7987
return errors.Wrap(err, "could not tree hash attestation")

beacon-chain/operations/attestations/kv/aggregated_test.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestKV_Aggregated_AggregateUnaggregatedAttestations(t *testing.T) {
4141
}
4242
}
4343

44-
func TestKV_Aggregated_SaveUnaggregatedAttestation(t *testing.T) {
44+
func TestKV_Aggregated_SaveAggregatedAttestation(t *testing.T) {
4545
tests := []struct {
4646
name string
4747
att *ethpb.Attestation
@@ -101,7 +101,47 @@ func TestKV_Aggregated_SaveUnaggregatedAttestation(t *testing.T) {
101101
return
102102
}
103103
if len(cache.aggregatedAtt) != tt.count {
104-
t.Errorf("Wrong attestation count, want: %d, got: %d", tt.count, len(cache.unAggregatedAtt))
104+
t.Errorf("Wrong attestation count, want: %d, got: %d", tt.count, len(cache.aggregatedAtt))
105+
}
106+
if cache.AggregatedAttestationCount() != tt.count {
107+
t.Errorf("Wrong attestation count, want: %d, got: %d", tt.count, cache.AggregatedAttestationCount())
108+
}
109+
})
110+
}
111+
}
112+
113+
func TestKV_Aggregated_SaveAggregatedAttestations(t *testing.T) {
114+
tests := []struct {
115+
name string
116+
atts []*ethpb.Attestation
117+
count int
118+
wantErrString string
119+
}{
120+
{
121+
name: "no duplicates",
122+
atts: []*ethpb.Attestation{
123+
{Data: &ethpb.AttestationData{Slot: 1},
124+
AggregationBits: bitfield.Bitlist{0b1101}},
125+
{Data: &ethpb.AttestationData{Slot: 1},
126+
AggregationBits: bitfield.Bitlist{0b1101}},
127+
},
128+
count: 1,
129+
},
130+
}
131+
132+
for _, tt := range tests {
133+
t.Run(tt.name, func(t *testing.T) {
134+
cache := NewAttCaches()
135+
if len(cache.aggregatedAtt) != 0 {
136+
t.Errorf("Invalid start pool, atts: %d", len(cache.unAggregatedAtt))
137+
}
138+
err := cache.SaveAggregatedAttestations(tt.atts)
139+
if tt.wantErrString == "" && err != nil {
140+
t.Error(err)
141+
return
142+
}
143+
if len(cache.aggregatedAtt) != tt.count {
144+
t.Errorf("Wrong attestation count, want: %d, got: %d", tt.count, len(cache.aggregatedAtt))
105145
}
106146
if cache.AggregatedAttestationCount() != tt.count {
107147
t.Errorf("Wrong attestation count, want: %d, got: %d", tt.count, cache.AggregatedAttestationCount())

beacon-chain/operations/attestations/prune_expired.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func (s *Service) pruneAttsPool() {
1414
select {
1515
case <-ticker.C:
1616
s.pruneExpiredAtts()
17+
s.updateMetrics()
1718
case <-s.ctx.Done():
1819
log.Debug("Context closed, exiting routine")
1920
ticker.Stop()

0 commit comments

Comments
 (0)