Skip to content

Commit

Permalink
Use DeepSSZEqual to satisfy go tests (#8499)
Browse files Browse the repository at this point in the history
* Use DeepSSZEqual to satisfy go tests

* New line

* New line

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
terencechain and prylabs-bulldozer[bot] authored Feb 23, 2021
1 parent 0c59952 commit 1db3c86
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 48 deletions.
15 changes: 7 additions & 8 deletions beacon-chain/db/kv/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"reflect"
"testing"

"github.com/gogo/protobuf/proto"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand Down Expand Up @@ -57,7 +56,7 @@ func TestGenesisState_CanSaveRetrieve(t *testing.T) {

savedGenesisS, err := db.GenesisState(context.Background())
require.NoError(t, err)
assert.DeepEqual(t, st.InnerStateUnsafe(), savedGenesisS.InnerStateUnsafe(), "Did not retrieve saved state")
assert.DeepSSZEqual(t, st.InnerStateUnsafe(), savedGenesisS.InnerStateUnsafe(), "Did not retrieve saved state")
require.NoError(t, db.SaveGenesisBlockRoot(context.Background(), [32]byte{'C'}))
}

Expand Down Expand Up @@ -197,15 +196,15 @@ func TestStore_SaveDeleteState_CanGetHighestBelow(t *testing.T) {

highest, err := db.HighestSlotStatesBelow(context.Background(), 2)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), s0), "Did not retrieve saved state: %v != %v", highest, s0)
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s0)

highest, err = db.HighestSlotStatesBelow(context.Background(), 101)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), s1), "Did not retrieve saved state: %v != %v", highest, s1)
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s1)

highest, err = db.HighestSlotStatesBelow(context.Background(), 1001)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), s2), "Did not retrieve saved state: %v != %v", highest, s2)
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), s2)
}

func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) {
Expand All @@ -230,14 +229,14 @@ func TestStore_GenesisState_CanGetHighestBelow(t *testing.T) {

highest, err := db.HighestSlotStatesBelow(context.Background(), 2)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), st.InnerStateUnsafe()))
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), st.InnerStateUnsafe())

highest, err = db.HighestSlotStatesBelow(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()))
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe())
highest, err = db.HighestSlotStatesBelow(context.Background(), 0)
require.NoError(t, err)
assert.Equal(t, true, proto.Equal(highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe()))
assert.DeepSSZEqual(t, highest[0].InnerStateUnsafe(), genesisState.InnerStateUnsafe())
}

func TestStore_CleanUpDirtyStates_AboveThreshold(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion beacon-chain/db/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path"
"testing"

types "github.com/prysmaticlabs/eth2-types"
"github.com/prysmaticlabs/prysm/beacon-chain/db/kv"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
Expand Down Expand Up @@ -65,7 +66,7 @@ func TestRestore(t *testing.T) {
require.NoError(t, err)
headBlock, err := restoredDb.HeadBlock(ctx)
require.NoError(t, err)
assert.Equal(t, uint64(5000), headBlock.Block.Slot, "Restored database has incorrect data")
assert.Equal(t, types.Slot(5000), headBlock.Block.Slot, "Restored database has incorrect data")
assert.LogsContain(t, logHook, "Restore completed successfully")

}
2 changes: 1 addition & 1 deletion beacon-chain/operations/attestations/kv/aggregated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,6 @@ func TestKV_Aggregated_DuplicateAggregatedAttestations(t *testing.T) {
returned := cache.AggregatedAttestations()

// It should have only returned att2.
assert.DeepEqual(t, att2, returned[0], "Did not receive correct aggregated atts")
assert.DeepSSZEqual(t, att2, returned[0], "Did not receive correct aggregated atts")
assert.Equal(t, 1, len(returned), "Did not receive correct aggregated atts")
}
7 changes: 6 additions & 1 deletion beacon-chain/rpc/beacon/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ func TestServer_ListBlocks_Genesis_MultiBlocks(t *testing.T) {
}

func TestServer_ListBlocks_Pagination(t *testing.T) {
params.UseMinimalConfig()
defer params.UseMainnetConfig()

db := dbTest.SetupDB(t)
chain := &chainMock.ChainService{
CanonicalRoots: map[[32]byte]bool{},
Expand Down Expand Up @@ -357,8 +360,10 @@ func TestServer_GetChainHead_NoHeadBlock(t *testing.T) {
}

func TestServer_GetChainHead(t *testing.T) {
db := dbTest.SetupDB(t)
params.UseMinimalConfig()
defer params.UseMainnetConfig()

db := dbTest.SetupDB(t)
genBlock := testutil.NewBeaconBlock()
genBlock.Block.ParentRoot = bytesutil.PadTo([]byte{'G'}, 32)
require.NoError(t, db.SaveBlock(context.Background(), genBlock))
Expand Down
10 changes: 8 additions & 2 deletions beacon-chain/rpc/beacon/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1556,8 +1556,10 @@ func TestServer_GetValidatorParticipation_CurrentAndPrevEpoch(t *testing.T) {
}

func TestServer_GetValidatorParticipation_OrphanedUntilGenesis(t *testing.T) {
beaconDB := dbTest.SetupDB(t)
helpers.ClearCache()
params.UseMainnetConfig()

beaconDB := dbTest.SetupDB(t)
ctx := context.Background()
validatorCount := uint64(100)

Expand Down Expand Up @@ -1644,6 +1646,10 @@ func TestGetValidatorPerformance_Syncing(t *testing.T) {
}

func TestGetValidatorPerformance_OK(t *testing.T) {
helpers.ClearCache()
params.UseMinimalConfig()
defer params.UseMainnetConfig()

ctx := context.Background()
epoch := types.Epoch(1)
headState, err := testutil.NewBeaconState()
Expand Down Expand Up @@ -1916,9 +1922,9 @@ func TestServer_GetIndividualVotes_RequestFutureSlot(t *testing.T) {
}

func TestServer_GetIndividualVotes_ValidatorsDontExist(t *testing.T) {

params.UseMinimalConfig()
defer params.UseMainnetConfig()

beaconDB := dbTest.SetupDB(t)
ctx := context.Background()

Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/rpc/validator/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func TestSubmitAggregateAndProof_PreferOwnAttestation(t *testing.T) {

res, err := aggregatorServer.SubmitAggregateSelectionProof(ctx, req)
require.NoError(t, err)
assert.DeepEqual(t, att1, res.AggregateAndProof.Aggregate, "Did not receive wanted attestation")
assert.DeepSSZEqual(t, att1, res.AggregateAndProof.Aggregate, "Did not receive wanted attestation")
}

func TestSubmitAggregateAndProof_SelectsMostBitsWhenOwnAttestationNotPresent(t *testing.T) {
Expand Down Expand Up @@ -401,7 +401,7 @@ func TestSubmitAggregateAndProof_SelectsMostBitsWhenOwnAttestationNotPresent(t *

res, err := aggregatorServer.SubmitAggregateSelectionProof(ctx, req)
require.NoError(t, err)
assert.DeepEqual(t, att1, res.AggregateAndProof.Aggregate, "Did not receive wanted attestation")
assert.DeepSSZEqual(t, att1, res.AggregateAndProof.Aggregate, "Did not receive wanted attestation")
}

func TestSubmitSignedAggregateSelectionProof_ZeroHashesSignatures(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestEpochBoundaryStateCache_CanSave(t *testing.T) {
got, exists, err = e.getByRoot([32]byte{'a'})
require.NoError(t, err)
assert.Equal(t, true, exists, "Should exist")
assert.DeepEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")

got, exists, err = e.getBySlot(2)
require.NoError(t, err)
Expand All @@ -45,7 +45,7 @@ func TestEpochBoundaryStateCache_CanSave(t *testing.T) {
got, exists, err = e.getBySlot(1)
require.NoError(t, err)
assert.Equal(t, true, exists, "Should exist")
assert.DeepEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
assert.DeepSSZEqual(t, s.InnerStateUnsafe(), got.state.InnerStateUnsafe(), "Should have the same state")
}

func TestEpochBoundaryStateCache_CanTrim(t *testing.T) {
Expand Down
24 changes: 6 additions & 18 deletions beacon-chain/state/stategen/getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"testing"

"github.com/gogo/protobuf/proto"
types "github.com/prysmaticlabs/eth2-types"

"github.com/prysmaticlabs/prysm/beacon-chain/core/blocks"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
Expand Down Expand Up @@ -36,9 +36,7 @@ func TestStateByRoot_ColdState(t *testing.T) {
require.NoError(t, service.beaconDB.SaveGenesisBlockRoot(ctx, bRoot))
loadedState, err := service.StateByRoot(ctx, bRoot)
require.NoError(t, err)
if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly save state")
}
require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe())
}

func TestStateByRoot_HotStateUsingEpochBoundaryCacheNoReplay(t *testing.T) {
Expand Down Expand Up @@ -97,9 +95,7 @@ func TestStateByRoot_HotStateCached(t *testing.T) {

loadedState, err := service.StateByRoot(ctx, r)
require.NoError(t, err)
if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly cache state")
}
require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe())
}

func TestStateByRootInitialSync_UseEpochStateCache(t *testing.T) {
Expand Down Expand Up @@ -133,9 +129,7 @@ func TestStateByRootInitialSync_UseCache(t *testing.T) {

loadedState, err := service.StateByRootInitialSync(ctx, r)
require.NoError(t, err)
if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly cache state")
}
require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe())
if service.hotStateCache.has(r) {
t.Error("Hot state cache was not invalidated")
}
Expand Down Expand Up @@ -234,10 +228,7 @@ func TestLoadeStateByRoot_Cached(t *testing.T) {
// This tests where hot state was already cached.
loadedState, err := service.loadStateByRoot(ctx, r)
require.NoError(t, err)

if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly cache state")
}
require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe())
}

func TestLoadeStateByRoot_FinalizedState(t *testing.T) {
Expand All @@ -261,10 +252,7 @@ func TestLoadeStateByRoot_FinalizedState(t *testing.T) {
// This tests where hot state was already cached.
loadedState, err := service.loadStateByRoot(ctx, gRoot)
require.NoError(t, err)

if !proto.Equal(loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe()) {
t.Error("Did not correctly retrieve finalized state")
}
require.DeepSSZEqual(t, loadedState.InnerStateUnsafe(), beaconState.InnerStateUnsafe())
}

func TestLoadeStateByRoot_EpochBoundaryStateCanProcess(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/state/stategen/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestMigrateToCold_HappyPath(t *testing.T) {

gotState, err := service.beaconDB.State(ctx, fRoot)
require.NoError(t, err)
assert.DeepEqual(t, beaconState.InnerStateUnsafe(), gotState.InnerStateUnsafe(), "Did not save state")
assert.DeepSSZEqual(t, beaconState.InnerStateUnsafe(), gotState.InnerStateUnsafe(), "Did not save state")
gotRoot := service.beaconDB.ArchivedPointRoot(ctx, stateSlot/service.slotsPerArchivedPoint)
assert.Equal(t, fRoot, gotRoot, "Did not save archived root")
lastIndex, err := service.beaconDB.LastArchivedSlot(ctx)
Expand Down
6 changes: 2 additions & 4 deletions beacon-chain/state/stategen/replay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ func TestLastSavedState_Genesis(t *testing.T) {

savedState, err := s.lastSavedState(ctx, 0)
require.NoError(t, err)
require.DeepEqual(t, gState.InnerStateUnsafe(), savedState.InnerStateUnsafe())
require.DeepSSZEqual(t, gState.InnerStateUnsafe(), savedState.InnerStateUnsafe())
}

func TestLastSavedState_CanGet(t *testing.T) {
Expand Down Expand Up @@ -394,9 +394,7 @@ func TestLastSavedState_CanGet(t *testing.T) {

savedState, err := s.lastSavedState(ctx, s.finalizedInfo.slot+100)
require.NoError(t, err)
if !proto.Equal(st.InnerStateUnsafe(), savedState.InnerStateUnsafe()) {
t.Error("Did not save correct root")
}
require.DeepSSZEqual(t, st.InnerStateUnsafe(), savedState.InnerStateUnsafe())
}

func TestLastSavedState_NoSavedBlockState(t *testing.T) {
Expand Down
6 changes: 1 addition & 5 deletions beacon-chain/state/stategen/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/gogo/protobuf/proto"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
testDB "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/shared/params"
Expand All @@ -30,10 +29,7 @@ func TestResume(t *testing.T) {

resumeState, err := service.Resume(ctx)
require.NoError(t, err)

if !proto.Equal(beaconState.InnerStateUnsafe(), resumeState.InnerStateUnsafe()) {
t.Error("Diff saved state")
}
require.DeepSSZEqual(t, beaconState.InnerStateUnsafe(), resumeState.InnerStateUnsafe())
assert.Equal(t, params.BeaconConfig().SlotsPerEpoch, service.finalizedInfo.slot, "Did not get watned slot")
assert.Equal(t, service.finalizedInfo.root, root, "Did not get wanted root")
assert.NotNil(t, service.finalizedState(), "Wanted a non nil finalized state")
Expand Down
4 changes: 1 addition & 3 deletions beacon-chain/state/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ func TestBeaconState_ProtoBeaconStateCompatibility(t *testing.T) {
cloned, ok := proto.Clone(genesis).(*pb.BeaconState)
assert.Equal(t, true, ok, "Object is not of type *pb.BeaconState")
custom := customState.CloneInnerState()
if !proto.Equal(cloned, custom) {
t.Fatal("Cloned states did not match")
}
assert.DeepSSZEqual(t, cloned, custom)

r1, err := customState.HashTreeRoot(ctx)
require.NoError(t, err)
Expand Down

0 comments on commit 1db3c86

Please sign in to comment.