Skip to content

Commit

Permalink
More blockchain pkg tests (#8343)
Browse files Browse the repository at this point in the history
* Add more blockchain pkg tests

* Deepsource feedback

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
  • Loading branch information
terencechain and rauljordan committed Jan 27, 2021
1 parent 91fe32a commit 1cfae7e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
8 changes: 0 additions & 8 deletions beacon-chain/blockchain/head.go
Expand Up @@ -234,14 +234,6 @@ func (s *Service) headGenesisValidatorRoot() [32]byte {
return bytesutil.ToBytes32(s.head.state.GenesisValidatorRoot())
}

// HasHeadState returns true if head state exists.
func (s *Service) HasHeadState() bool {
s.headLock.RLock()
defer s.headLock.RUnlock()

return s.hasHeadState()
}

// Returns true if head state exists.
// This is the lock free version.
func (s *Service) hasHeadState() bool {
Expand Down
12 changes: 12 additions & 0 deletions beacon-chain/blockchain/metrics_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

eth "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
Expand All @@ -24,3 +25,14 @@ func TestReportEpochMetrics_BadAttestation(t *testing.T) {
err := reportEpochMetrics(context.Background(), s, h)
require.ErrorContains(t, "attestation with inclusion delay of 0", err)
}

func TestReportEpochMetrics_SlashedValidatorOutOfBound(t *testing.T) {
h, _ := testutil.DeterministicGenesisState(t, 1)
v, err := h.ValidatorAtIndex(0)
require.NoError(t, err)
v.Slashed = true
require.NoError(t, h.UpdateValidatorAtIndex(0, v))
require.NoError(t, h.SetCurrentEpochAttestations([]*pb.PendingAttestation{{InclusionDelay: 1, Data: testutil.HydrateAttestationData(&eth.AttestationData{})}}))
err = reportEpochMetrics(context.Background(), h, h)
require.ErrorContains(t, "slot 0 out of bounds", err)
}
16 changes: 16 additions & 0 deletions beacon-chain/blockchain/service_test.go
Expand Up @@ -12,6 +12,8 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache"
b "github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
"github.com/prysmaticlabs/prysm/beacon-chain/core/feed"
statefeed "github.com/prysmaticlabs/prysm/beacon-chain/core/feed/state"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
"github.com/prysmaticlabs/prysm/beacon-chain/db"
Expand Down Expand Up @@ -455,6 +457,20 @@ func TestServiceStop_SaveCachedBlocks(t *testing.T) {
require.Equal(t, true, s.beaconDB.HasBlock(ctx, r))
}

func TestProcessChainStartTime_ReceivedFeed(t *testing.T) {
beaconDB := testDB.SetupDB(t)
service := setupBeaconChain(t, beaconDB)
stateChannel := make(chan *feed.Event, 1)
stateSub := service.stateNotifier.StateFeed().Subscribe(stateChannel)
defer stateSub.Unsubscribe()
service.processChainStartTime(context.Background(), time.Now())

stateEvent := <-stateChannel
require.Equal(t, int(stateEvent.Type), int(statefeed.Initialized))
_, ok := stateEvent.Data.(*statefeed.InitializedData)
require.Equal(t, true, ok)
}

func BenchmarkHasBlockDB(b *testing.B) {
beaconDB := testDB.SetupDB(b)
ctx := context.Background()
Expand Down

0 comments on commit 1cfae7e

Please sign in to comment.