Skip to content

Commit

Permalink
Update seen for attestation pool (prysmaticlabs#4669)
Browse files Browse the repository at this point in the history
* Use `contain` instead of `overlap`
* Tests
* Update seen
* Merge branch 'master' into use-contain
* Use OR to track all of the bits we have seen so far
* Added one more test case
* gofmt
* Conflict
* Picked up Preston's changes
* Merge branch 'use-contain' of git+ssh://github.com/prysmaticlabs/prysm into use-contain
  • Loading branch information
terencechain authored and cryptomental committed Feb 24, 2020
1 parent dddbc97 commit f3af357
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
12 changes: 9 additions & 3 deletions beacon-chain/operations/attestations/prepare_forkchoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,23 @@ func (s *Service) seen(att *ethpb.Attestation) (bool, error) {
if err != nil {
return false, err
}
incomingBits := att.AggregationBits
savedBits, ok := s.forkChoiceProcessedRoots.Get(string(attRoot[:]))
if ok {
savedBitlist, ok := savedBits.(bitfield.Bitlist)
if !ok {
return false, errors.New("not a bit field")
}
if savedBitlist.Len() == att.AggregationBits.Len() && savedBitlist.Overlaps(att.AggregationBits) {
return true, nil
if savedBitlist.Len() == incomingBits.Len() {
// Returns true if the node has seen all the bits in the new bit field of the incoming attestation.
if savedBitlist.Contains(incomingBits) {
return true, nil
}
// Update the bit fields by Or'ing them with the new ones.
incomingBits = incomingBits.Or(savedBitlist)
}
}

s.forkChoiceProcessedRoots.Set(string(attRoot[:]), att.AggregationBits, 1 /*cost*/)
s.forkChoiceProcessedRoots.Set(string(attRoot[:]), incomingBits, 1 /*cost*/)
return false, nil
}
78 changes: 76 additions & 2 deletions beacon-chain/operations/attestations/prepare_forkchoice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestSeenAttestations_PresentInCache(t *testing.T) {
t.Fatal(err)
}

att1 := &ethpb.Attestation{Data: &ethpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x03}}
att1 := &ethpb.Attestation{Data: &ethpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x13} /* 0b00010011 */}
got, err := s.seen(att1)
if err != nil {
t.Fatal(err)
Expand All @@ -231,12 +231,86 @@ func TestSeenAttestations_PresentInCache(t *testing.T) {

time.Sleep(100 * time.Millisecond)

att2 := &ethpb.Attestation{Data: &ethpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x03}}
att2 := &ethpb.Attestation{Data: &ethpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x17} /* 0b00010111 */}
got, err = s.seen(att2)
if err != nil {
t.Fatal(err)
}
if got {
t.Error("Wanted false, got true")
}

time.Sleep(100 * time.Millisecond)

att3 := &ethpb.Attestation{Data: &ethpb.AttestationData{}, Signature: []byte{'A'}, AggregationBits: bitfield.Bitlist{0x17} /* 0b00010111 */}
got, err = s.seen(att3)
if err != nil {
t.Fatal(err)
}
if !got {
t.Error("Wanted true, got false")
}
}

func TestService_seen(t *testing.T) {
// Attestation are checked in order of this list.
tests := []struct {
att *ethpb.Attestation
want bool
}{
{
att: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b11011},
Data: &ethpb.AttestationData{Slot: 1},
},
want: false,
},
{
att: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b11011},
Data: &ethpb.AttestationData{Slot: 1},
},
want: true, // Exact same attestation should return true
},
{
att: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b10101},
Data: &ethpb.AttestationData{Slot: 1},
},
want: false, // Haven't seen the bit at index 2 yet.
},
{
att: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b11111},
Data: &ethpb.AttestationData{Slot: 1},
},
want: true, // We've full committee at this point.
},
{
att: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b11111},
Data: &ethpb.AttestationData{Slot: 2},
},
want: false, // Different root is different bitlist.
},
{
att: &ethpb.Attestation{
AggregationBits: bitfield.Bitlist{0b11111001},
Data: &ethpb.AttestationData{Slot: 1},
},
want: false, // Sanity test that an attestation of different lengths does not panic.
},
}

s, err := NewService(context.Background(), &Config{Pool: NewPool()})
if err != nil {
t.Fatal(err)
}

for i, tt := range tests {
if got, _ := s.seen(tt.att); got != tt.want {
t.Errorf("Test %d failed. Got=%v want=%v", i, got, tt.want)
}
time.Sleep(10) // Sleep briefly for cache to routine to buffer.
}
}

0 comments on commit f3af357

Please sign in to comment.