From 537e610abcd6c62850f2b15a899214324e887455 Mon Sep 17 00:00:00 2001 From: Matthias <5011972+fasmat@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:47:37 +0000 Subject: [PATCH] Simplify tests --- activation/activation_multi_test.go | 82 ++++-------------- activation/activation_test.go | 129 ++++++++++------------------ blocks/certifier_test.go | 2 +- 3 files changed, 63 insertions(+), 150 deletions(-) diff --git a/activation/activation_multi_test.go b/activation/activation_multi_test.go index e6227e928ed..955bf39d685 100644 --- a/activation/activation_multi_test.go +++ b/activation/activation_multi_test.go @@ -12,26 +12,16 @@ import ( "github.com/spacemeshos/go-spacemesh/common/types" "github.com/spacemeshos/go-spacemesh/p2p/pubsub" - "github.com/spacemeshos/go-spacemesh/signing" "github.com/spacemeshos/go-spacemesh/sql" "github.com/spacemeshos/go-spacemesh/sql/atxs" "github.com/spacemeshos/go-spacemesh/sql/localsql/nipost" ) func Test_Builder_Multi_StartSmeshingCoinbase(t *testing.T) { - tab := newTestBuilder(t) + tab := newTestBuilder(t, 5) coinbase := types.Address{1, 1, 1} - smeshers := make(map[types.NodeID]*signing.EdSigner, 10) - for i := 0; i < 10; i++ { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab.Register(sig) - smeshers[sig.NodeID()] = sig - } - - for _, sig := range smeshers { + for _, sig := range tab.signers { tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).DoAndReturn( func(ctx context.Context, _ types.NodeID, _ []byte) (*types.Post, *types.PostInfo, error) { <-ctx.Done() @@ -50,7 +40,7 @@ func Test_Builder_Multi_StartSmeshingCoinbase(t *testing.T) { // calling StartSmeshing more than once before calling StopSmeshing is an error require.ErrorContains(t, tab.StartSmeshing(coinbase), "already started") - for _, sig := range smeshers { + for _, sig := range tab.signers { tab.mnipost.EXPECT().ResetState(sig.NodeID()).Return(nil) } require.NoError(t, tab.StopSmeshing(true)) @@ -58,16 +48,9 @@ func Test_Builder_Multi_StartSmeshingCoinbase(t *testing.T) { func Test_Builder_Multi_RestartSmeshing(t *testing.T) { getBuilder := func(t *testing.T) *Builder { - tab := newTestBuilder(t) - - smeshers := make(map[types.NodeID]*signing.EdSigner, 10) - for i := 0; i < 10; i++ { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab.Register(sig) - smeshers[sig.NodeID()] = sig + tab := newTestBuilder(t, 5) + for _, sig := range tab.signers { tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).AnyTimes().DoAndReturn( func(ctx context.Context, _ types.NodeID, _ []byte) (*types.Post, *types.PostInfo, error) { <-ctx.Done() @@ -112,7 +95,7 @@ func Test_Builder_Multi_RestartSmeshing(t *testing.T) { } func Test_Builder_Multi_StopSmeshing_Delete(t *testing.T) { - tab := newTestBuilder(t) + tab := newTestBuilder(t, 5) atx := types.RandomATXID() refChallenge := &types.NIPostChallenge{ @@ -130,14 +113,7 @@ func Test_Builder_Multi_StopSmeshing_Delete(t *testing.T) { return genesis.Add(layerDuration * time.Duration(got)) }).AnyTimes() - smeshers := make(map[types.NodeID]*signing.EdSigner, 10) - for i := 0; i < 10; i++ { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab.Register(sig) - smeshers[sig.NodeID()] = sig - + for _, sig := range tab.signers { tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).DoAndReturn( func(ctx context.Context, _ types.NodeID, _ []byte) (*types.Post, *types.PostInfo, error) { <-ctx.Done() @@ -151,7 +127,7 @@ func Test_Builder_Multi_StopSmeshing_Delete(t *testing.T) { require.NoError(t, tab.StartSmeshing(types.Address{})) require.NoError(t, tab.StopSmeshing(false)) - for _, sig := range smeshers { + for _, sig := range tab.signers { challenge, err := nipost.Challenge(tab.localDb, sig.NodeID()) require.NoError(t, err) require.Equal(t, refChallenge, challenge) // challenge still present @@ -168,7 +144,7 @@ func Test_Builder_Multi_StopSmeshing_Delete(t *testing.T) { require.NoError(t, tab.StartSmeshing(types.Address{})) require.NoError(t, tab.StopSmeshing(true)) - for _, sig := range smeshers { + for _, sig := range tab.signers { challenge, err := nipost.Challenge(tab.localDb, sig.NodeID()) require.ErrorIs(t, err, sql.ErrNotFound) require.Nil(t, challenge) // challenge deleted @@ -185,7 +161,7 @@ func Test_Builder_Multi_StopSmeshing_Delete(t *testing.T) { require.NoError(t, tab.StartSmeshing(types.Address{})) require.NoError(t, tab.StopSmeshing(true)) // no-op - for _, sig := range smeshers { + for _, sig := range tab.signers { challenge, err := nipost.Challenge(tab.localDb, sig.NodeID()) require.ErrorIs(t, err, sql.ErrNotFound) require.Nil(t, challenge) // challenge still deleted @@ -195,30 +171,19 @@ func Test_Builder_Multi_StopSmeshing_Delete(t *testing.T) { func TestRegossip(t *testing.T) { layer := types.LayerID(10) - smeshers := make(map[types.NodeID]*signing.EdSigner, 10) - for i := 0; i < 10; i++ { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - smeshers[sig.NodeID()] = sig - } - t.Run("not found", func(t *testing.T) { - tab := newTestBuilder(t) - for _, sig := range smeshers { - tab.Register(sig) + tab := newTestBuilder(t, 5) + for _, sig := range tab.signers { tab.mclock.EXPECT().CurrentLayer().Return(layer) require.NoError(t, tab.Regossip(context.Background(), sig.NodeID())) } }) t.Run("success", func(t *testing.T) { - tab := newTestBuilder(t) + tab := newTestBuilder(t, 5) var refAtx *types.VerifiedActivationTx - for _, sig := range smeshers { - tab.Register(sig) - + for _, sig := range tab.signers { atx := newActivationTx(t, sig, 0, types.EmptyATXID, types.EmptyATXID, nil, layer.GetEpoch(), 0, 1, types.Address{}, 1, &types.NIPost{}) @@ -240,10 +205,8 @@ func TestRegossip(t *testing.T) { }) t.Run("checkpointed", func(t *testing.T) { - tab := newTestBuilder(t) - for _, sig := range smeshers { - tab.Register(sig) - + tab := newTestBuilder(t, 5) + for _, sig := range tab.signers { require.NoError(t, atxs.AddCheckpointed(tab.cdb.Database, &atxs.CheckpointAtx{ID: types.RandomATXID(), Epoch: layer.GetEpoch(), SmesherID: sig.NodeID()})) @@ -254,18 +217,9 @@ func TestRegossip(t *testing.T) { } func Test_Builder_Multi_InitialPost(t *testing.T) { - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - smeshers := make(map[types.NodeID]*signing.EdSigner, 10) - for i := 0; i < 10; i++ { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab.Register(sig) - smeshers[sig.NodeID()] = sig - } - + tab := newTestBuilder(t, 5, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) var eg errgroup.Group - for _, sig := range smeshers { + for _, sig := range tab.signers { sig := sig eg.Go(func() error { tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).Return( diff --git a/activation/activation_test.go b/activation/activation_test.go index 1febbe257a3..5fadd518747 100644 --- a/activation/activation_test.go +++ b/activation/activation_test.go @@ -13,6 +13,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + "golang.org/x/exp/maps" "golang.org/x/sync/errgroup" "github.com/spacemeshos/go-spacemesh/codec" @@ -105,7 +106,7 @@ type testAtxBuilder struct { mValidator *MocknipostValidator } -func newTestBuilder(tb testing.TB, opts ...BuilderOption) *testAtxBuilder { +func newTestBuilder(tb testing.TB, numSigners int, opts ...BuilderOption) *testAtxBuilder { lg := logtest.New(tb) ctrl := gomock.NewController(tb) @@ -143,6 +144,13 @@ func newTestBuilder(tb testing.TB, opts ...BuilderOption) *testAtxBuilder { opts..., ) tab.Builder = b + + for i := 0; i < numSigners; i++ { + sig, err := signing.NewEdSigner() + require.NoError(tb, err) + tab.Register(sig) + } + return tab } @@ -216,11 +224,8 @@ func publishAtx( // ========== Tests ========== func Test_Builder_StartSmeshingCoinbase(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t) - tab.Register(sig) + tab := newTestBuilder(t, 1) + sig := maps.Values(tab.signers)[0] coinbase := types.Address{1, 1, 1} tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).DoAndReturn( @@ -246,11 +251,8 @@ func Test_Builder_StartSmeshingCoinbase(t *testing.T) { func TestBuilder_RestartSmeshing(t *testing.T) { getBuilder := func(t *testing.T) *Builder { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t) - tab.Register(sig) + tab := newTestBuilder(t, 1) + sig := maps.Values(tab.signers)[0] tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).AnyTimes().DoAndReturn( func(ctx context.Context, _ types.NodeID, _ []byte) (*types.Post, *types.PostInfo, error) { @@ -295,11 +297,8 @@ func TestBuilder_RestartSmeshing(t *testing.T) { } func TestBuilder_StopSmeshing_Delete(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t) - tab.Register(sig) + tab := newTestBuilder(t, 1) + sig := maps.Values(tab.signers)[0] currLayer := (postGenesisEpoch + 1).FirstLayer() tab.mclock.EXPECT().AwaitLayer(gomock.Any()).Return(make(chan struct{})).AnyTimes() @@ -354,16 +353,13 @@ func TestBuilder_StopSmeshing_Delete(t *testing.T) { } func TestBuilder_StopSmeshing_failsWhenNotStarted(t *testing.T) { - tab := newTestBuilder(t) + tab := newTestBuilder(t, 1) require.ErrorContains(t, tab.StopSmeshing(true), "not started") } func TestBuilder_PublishActivationTx_HappyFlow(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration})) + sig := maps.Values(tab.signers)[0] posEpoch := postGenesisEpoch currLayer := posEpoch.FirstLayer() @@ -405,11 +401,8 @@ func TestBuilder_PublishActivationTx_HappyFlow(t *testing.T) { // failing with ErrATXChallengeExpired. func TestBuilder_Loop_WaitsOnStaleChallenge(t *testing.T) { // Arrange - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] // current layer is too late to be able to build a nipost on time currLayer := (postGenesisEpoch + 1).FirstLayer() @@ -462,11 +455,8 @@ func TestBuilder_Loop_WaitsOnStaleChallenge(t *testing.T) { } func TestBuilder_PublishActivationTx_FaultyNet(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] posEpoch := postGenesisEpoch currLayer := postGenesisEpoch.FirstLayer() @@ -553,14 +543,11 @@ func TestBuilder_PublishActivationTx_FaultyNet(t *testing.T) { } func TestBuilder_PublishActivationTx_UsesExistingChallengeOnLatePublish(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - poetCfg := PoetConfig{ PhaseShift: layerDuration * 4, } - tab := newTestBuilder(t, WithPoetConfig(poetCfg)) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(poetCfg)) + sig := maps.Values(tab.signers)[0] posEpoch := postGenesisEpoch currLayer := (postGenesisEpoch + 1).FirstLayer().Add(5) // late for poet round start @@ -645,11 +632,8 @@ func TestBuilder_PublishActivationTx_UsesExistingChallengeOnLatePublish(t *testi } func TestBuilder_PublishActivationTx_RebuildNIPostWhenTargetEpochPassed(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] posEpoch := types.EpochID(2) currLayer := posEpoch.FirstLayer() @@ -751,11 +735,8 @@ func TestBuilder_PublishActivationTx_RebuildNIPostWhenTargetEpochPassed(t *testi } func TestBuilder_PublishActivationTx_NoPrevATX(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] posEpoch := postGenesisEpoch currLayer := posEpoch.FirstLayer() @@ -794,11 +775,8 @@ func TestBuilder_PublishActivationTx_NoPrevATX(t *testing.T) { } func TestBuilder_PublishActivationTx_NoPrevATX_PublishFails_InitialPost_preserved(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] posEpoch := postGenesisEpoch currLayer := posEpoch.FirstLayer() @@ -875,11 +853,8 @@ func TestBuilder_PublishActivationTx_PrevATXWithoutPrevATX(t *testing.T) { r := require.New(t) // Arrange - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] otherSigner, err := signing.NewEdSigner() r.NoError(err) @@ -999,11 +974,8 @@ func TestBuilder_PublishActivationTx_TargetsEpochBasedOnPosAtx(t *testing.T) { r := require.New(t) // Arrange - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] otherSigner, err := signing.NewEdSigner() r.NoError(err) @@ -1098,11 +1070,8 @@ func TestBuilder_PublishActivationTx_TargetsEpochBasedOnPosAtx(t *testing.T) { } func TestBuilder_PublishActivationTx_FailsWhenNIPostBuilderFails(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] posEpoch := postGenesisEpoch currLayer := posEpoch.FirstLayer() @@ -1220,15 +1189,14 @@ func TestBuilder_RetryPublishActivationTx(t *testing.T) { }, events.WithBuffer(100)) require.NoError(t, err) - sig, err := signing.NewEdSigner() - require.NoError(t, err) - retryInterval := 50 * time.Microsecond tab := newTestBuilder( t, + 1, WithPoetConfig(PoetConfig{PhaseShift: 150 * time.Millisecond}), WithPoetRetryInterval(retryInterval), ) + sig := maps.Values(tab.signers)[0] posEpoch := types.EpochID(0) challenge := types.NIPostChallenge{ Sequence: 1, @@ -1346,11 +1314,8 @@ func TestBuilder_RetryPublishActivationTx(t *testing.T) { } func TestBuilder_InitialProofGeneratedOnce(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).Return( &types.Post{Indices: make([]byte, 10)}, @@ -1402,11 +1367,8 @@ func TestBuilder_InitialProofGeneratedOnce(t *testing.T) { } func TestBuilder_InitialPostIsPersisted(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) - tab.Register(sig) + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{PhaseShift: layerDuration * 4})) + sig := maps.Values(tab.signers)[0] tab.mnipost.EXPECT().Proof(gomock.Any(), sig.NodeID(), shared.ZeroChallenge).Return( &types.Post{Indices: make([]byte, 10)}, @@ -1440,14 +1402,11 @@ func TestWaitPositioningAtx(t *testing.T) { } { tc := tc t.Run(tc.desc, func(t *testing.T) { - sig, err := signing.NewEdSigner() - require.NoError(t, err) - - tab := newTestBuilder(t, WithPoetConfig(PoetConfig{ + tab := newTestBuilder(t, 1, WithPoetConfig(PoetConfig{ PhaseShift: tc.shift, GracePeriod: tc.grace, })) - tab.Register(sig) + sig := maps.Values(tab.signers)[0] tab.mclock.EXPECT().CurrentLayer().Return(types.LayerID(0)).AnyTimes() tab.mclock.EXPECT().LayerToTime(gomock.Any()).DoAndReturn(func(lid types.LayerID) time.Time { diff --git a/blocks/certifier_test.go b/blocks/certifier_test.go index 947607cd161..7a0d23f7e8c 100644 --- a/blocks/certifier_test.go +++ b/blocks/certifier_test.go @@ -664,7 +664,7 @@ func Test_OldLayersPruned(t *testing.T) { func Test_CertifyIfEligible(t *testing.T) { numSigners := 3 - tc := newTestCertifier(t, 3) + tc := newTestCertifier(t, numSigners) b := generateBlock(t, tc.db) tc.mb.EXPECT().GetBeacon(b.LayerIndex.GetEpoch()).Return(types.RandomBeacon(), nil)