Skip to content

Commit

Permalink
Addressed feedback. All test passing
Browse files Browse the repository at this point in the history
  • Loading branch information
terencechain committed Apr 13, 2020
1 parent 4644568 commit b3c0166
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 98 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (s *Service) saveHead(ctx context.Context, headRoot [32]byte) error {
// If the head state is not available, just return nil.
// There's nothing to cache
if !featureconfig.Get().DisableNewStateMgmt {
if !s.stateGen.StateSummaryExists(ctx, headRoot) && !s.beaconDB.HasState(ctx, headRoot) {
if !s.stateGen.StateSummaryExists(ctx, headRoot) {
return nil
}
} else {
Expand Down
2 changes: 2 additions & 0 deletions beacon-chain/blockchain/head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/go-ssz"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
)

Expand Down Expand Up @@ -47,6 +48,7 @@ func TestSaveHead_Different(t *testing.T) {
newRoot, _ := ssz.HashTreeRoot(newHeadBlock)
headState := testutil.NewBeaconState()
headState.SetSlot(1)
service.beaconDB.SaveStateSummary(context.Background(), &pb.StateSummary{Slot: 1, Root: newRoot[:]})
service.beaconDB.SaveState(context.Background(), headState, newRoot)
if err := service.saveHead(context.Background(), newRoot); err != nil {
t.Fatal(err)
Expand Down
6 changes: 0 additions & 6 deletions beacon-chain/operations/attestations/kv/aggregated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ func TestKV_AggregateUnaggregatedAttestations(t *testing.T) {
if len(cache.AggregatedAttestationsBySlotIndex(2, 0)) != 1 {
t.Fatal("Did not aggregate correctly")
}
if len(cache.UnAggregatedAttestationsBySlotIndex(1, 0)) != 0 {
t.Fatal("Did not clear unaggregated correctly")
}
if len(cache.UnAggregatedAttestationsBySlotIndex(2, 0)) != 0 {
t.Fatal("Did not clear unaggregated correctly")
}
}

func TestKV_Aggregated_CanSaveRetrieve(t *testing.T) {
Expand Down
39 changes: 0 additions & 39 deletions beacon-chain/operations/attestations/kv/unaggregated.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,6 @@ func (p *AttCaches) UnaggregatedAttestations() []*ethpb.Attestation {
return atts
}

// UnAggregatedAttestationsBySlotIndex returns the unaggregated attestations in cache,
// filtered by committee index and slot.
func (p *AttCaches) UnAggregatedAttestationsBySlotIndex(slot uint64, committeeIndex uint64) []*ethpb.Attestation {
atts := make([]*ethpb.Attestation, 0)

p.unAggregateAttLock.RLock()
defer p.unAggregateAttLock.RUnlock()
for _, a := range p.unAggregatedAtt {
if slot == a.Data.Slot && committeeIndex == a.Data.CommitteeIndex {
atts = append(atts, a)
}
}

return atts
}

// DeleteUnaggregatedAttestation deletes the unaggregated attestations in cache.
func (p *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error {
if att == nil {
Expand All @@ -89,29 +73,6 @@ func (p *AttCaches) DeleteUnaggregatedAttestation(att *ethpb.Attestation) error
return nil
}

// DeleteUnaggregatedAttestations deletes the unaggregated attestations in cache.
func (p *AttCaches) DeleteUnaggregatedAttestations(atts []*ethpb.Attestation) error {
p.unAggregateAttLock.Lock()
defer p.unAggregateAttLock.Unlock()
for _, att := range atts {
if att == nil {
continue
}
if helpers.IsAggregated(att) {
return errors.New("attestation is aggregated")
}

r, err := hashFn(att)
if err != nil {
return errors.Wrap(err, "could not tree hash attestation")
}

delete(p.unAggregatedAtt, r)
}

return nil
}

// UnaggregatedAttestationCount returns the number of unaggregated attestations key in the pool.
func (p *AttCaches) UnaggregatedAttestationCount() int {
p.unAggregateAttLock.RLock()
Expand Down
48 changes: 0 additions & 48 deletions beacon-chain/operations/attestations/kv/unaggregated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,3 @@ func TestKV_Unaggregated_CanDelete(t *testing.T) {
t.Error("Did not receive correct aggregated atts")
}
}

func TestKV_Unaggregated_FilterBySlotsAndCommitteeIDs(t *testing.T) {
cache := NewAttCaches()

att1 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b101}}
att2 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 2, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b110}}
att3 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b110}}
att4 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1, CommitteeIndex: 2}, AggregationBits: bitfield.Bitlist{0b110}}

atts := []*ethpb.Attestation{att1, att2, att3, att4}

for _, att := range atts {
if err := cache.SaveUnaggregatedAttestation(att); err != nil {
t.Fatal(err)
}
}

returned := cache.UnAggregatedAttestationsBySlotIndex(1, 1)

if !reflect.DeepEqual([]*ethpb.Attestation{att1, att3}, returned) {
t.Error("Did not receive correct aggregated atts")
}
}

func TestKV_Unaggregated_BatchDelete(t *testing.T) {
cache := NewAttCaches()

att1 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b101}}
att2 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 2, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b110}}
att3 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1, CommitteeIndex: 1}, AggregationBits: bitfield.Bitlist{0b110}}
att4 := &ethpb.Attestation{Data: &ethpb.AttestationData{Slot: 1, CommitteeIndex: 2}, AggregationBits: bitfield.Bitlist{0b110}}

atts := []*ethpb.Attestation{att1, att2, att3, att4}

for _, att := range atts {
if err := cache.SaveUnaggregatedAttestation(att); err != nil {
t.Fatal(err)
}
}
if err := cache.DeleteUnaggregatedAttestations(atts); err != nil {
t.Fatal(err)
}

returned := cache.UnaggregatedAttestations()
if !reflect.DeepEqual([]*ethpb.Attestation{}, returned) {
t.Error("Did not receive correct aggregated atts")
}
}
2 changes: 0 additions & 2 deletions beacon-chain/operations/attestations/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ type Pool interface {
SaveUnaggregatedAttestation(att *ethpb.Attestation) error
SaveUnaggregatedAttestations(atts []*ethpb.Attestation) error
UnaggregatedAttestations() []*ethpb.Attestation
UnAggregatedAttestationsBySlotIndex(slot uint64, committeeIndex uint64) []*ethpb.Attestation
DeleteUnaggregatedAttestation(att *ethpb.Attestation) error
DeleteUnaggregatedAttestations(atts []*ethpb.Attestation) error
UnaggregatedAttestationCount() int
// For attestations that were included in the block.
SaveBlockAttestation(att *ethpb.Attestation) error
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/state/stategen/getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (s *State) StateBySlot(ctx context.Context, slot uint64) (*state.BeaconStat
return s.loadHotStateBySlot(ctx, slot)
}

// StateSummaryExists returns true if the corresponding state of the input block either
// exists in the DB or it can be generated by state gen.
// StateSummaryExists returns true if the corresponding state summary of the input block root either
// exists in the DB or in the cache.
func (s *State) StateSummaryExists(ctx context.Context, blockRoot [32]byte) bool {
return s.beaconDB.HasStateSummary(ctx, blockRoot) || s.stateSummaryCache.Has(blockRoot)
}
Expand Down

0 comments on commit b3c0166

Please sign in to comment.