Skip to content

Commit

Permalink
Fix e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Oct 6, 2023
1 parent abbbaa0 commit 490ad72
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 91 deletions.
50 changes: 23 additions & 27 deletions activation/e2e/nipost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"golang.org/x/sync/errgroup"

Expand Down Expand Up @@ -140,17 +139,14 @@ func initPost(tb testing.TB, logger *zap.Logger, mgr *activation.PostSetupManage
}

func TestNIPostBuilderWithClients(t *testing.T) {
logger := zaptest.NewLogger(t)
ctrl := gomock.NewController(t)

sig, err := signing.NewEdSigner()
require.NoError(t, err)

postDir := t.TempDir()
logger := zaptest.NewLogger(t)
goldenATX := types.ATXID{2, 3, 4}

cfg := activation.DefaultPostConfig()

cdb := datastore.NewCachedDB(sql.InMemory(), log.NewFromLog(logger))

provingOpts := activation.DefaultPostProvingOpts()
Expand All @@ -159,6 +155,7 @@ func TestNIPostBuilderWithClients(t *testing.T) {
mgr, err := activation.NewPostSetupManager(sig.NodeID(), cfg, logger, cdb, goldenATX, provingOpts)
require.NoError(t, err)

postDir := t.TempDir()
opts := activation.DefaultPostSetupOpts()
opts.DataDir = postDir
opts.ProviderID.SetInt64(int64(initialization.CPUProviderID()))
Expand All @@ -185,12 +182,11 @@ func TestNIPostBuilderWithClients(t *testing.T) {
},
)

poetDb := activation.NewPoetDb(sql.InMemory(), log.NewFromLog(logger).Named("poetDb"))

verifier, err := activation.NewPostVerifier(mgr.Config(), logger.Named("verifier"))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, verifier.Close()) })

poetDb := activation.NewPoetDb(sql.InMemory(), log.NewFromLog(logger).Named("poetDb"))
v := activation.NewValidator(poetDb, mgr.Config(), mgr.LastOpts().Scrypt, verifier)

nb, err := activation.NewNIPostBuilder(
Expand All @@ -199,7 +195,7 @@ func TestNIPostBuilderWithClients(t *testing.T) {
poetDb,
[]string{poetProver.RestURL().String()},
t.TempDir(),
logtest.New(t, zapcore.DebugLevel),
log.NewFromLog(logger),
sig,
poetCfg,
mclock,
Expand Down Expand Up @@ -244,16 +240,19 @@ func TestNIPostBuilderWithClients(t *testing.T) {
}

func TestNIPostBuilder_Close(t *testing.T) {
r := require.New(t)

ctrl := gomock.NewController(t)

sig, err := signing.NewEdSigner()
require.NoError(t, err)

logger := zaptest.NewLogger(t)

nipostClient := activation.NewMocknipostClient(ctrl)
nipostClient.EXPECT().Status().Return(&activation.PostSetupStatus{State: activation.PostSetupStateComplete})
poetProver := spawnPoet(t, WithGenesis(time.Now()), WithEpochDuration(time.Second))

poetDb := activation.NewMockpoetDbAPI(ctrl)
challenge := types.NIPostChallenge{
PublishEpoch: postGenesisEpoch + 2,
}

mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
func(got types.LayerID) time.Time {
Expand All @@ -263,40 +262,38 @@ func TestNIPostBuilder_Close(t *testing.T) {
},
)

sig, err := signing.NewEdSigner()
r.NoError(err)
nb, err := activation.NewNIPostBuilder(
types.NodeID{1},
sig.NodeID(),
nipostClient,
poetDb,
[]string{poetProver.RestURL().String()},
t.TempDir(),
logtest.New(t),
log.NewFromLog(logger),
sig,
activation.PoetConfig{},
mclock,
)
r.NoError(err)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
cancel()
challenge := types.NIPostChallenge{
PublishEpoch: postGenesisEpoch + 2,
}
nipost, err := nb.BuildNIPost(ctx, &challenge)
r.ErrorIs(err, context.Canceled)
r.Nil(nipost)
require.ErrorIs(t, err, context.Canceled)
require.Nil(t, nipost)
}

func TestNewNIPostBuilderNotInitialized(t *testing.T) {
logger := zaptest.NewLogger(t)
ctrl := gomock.NewController(t)

sig, err := signing.NewEdSigner()
require.NoError(t, err)

postDir := t.TempDir()
logger := zaptest.NewLogger(t)
goldenATX := types.ATXID{2, 3, 4}

cfg := activation.DefaultPostConfig()

cdb := datastore.NewCachedDB(sql.InMemory(), log.NewFromLog(logger))

provingOpts := activation.DefaultPostProvingOpts()
Expand All @@ -314,7 +311,6 @@ func TestNewNIPostBuilderNotInitialized(t *testing.T) {
RequestRetryDelay: epoch / 100,
MaxRequestRetries: 10,
}

poetProver := spawnPoet(t, WithGenesis(time.Now()), WithEpochDuration(epoch), WithPhaseShift(poetCfg.PhaseShift), WithCycleGap(poetCfg.CycleGap))

mclock := activation.NewMocklayerClock(ctrl)
Expand All @@ -327,7 +323,6 @@ func TestNewNIPostBuilderNotInitialized(t *testing.T) {
)

poetDb := activation.NewPoetDb(sql.InMemory(), log.NewFromLog(logger).Named("poetDb"))

nipostValidator := activation.NewMocknipostValidator(ctrl)
nipostValidator.EXPECT().Post(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(nil)

Expand Down Expand Up @@ -356,6 +351,7 @@ func TestNewNIPostBuilderNotInitialized(t *testing.T) {
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

postDir := t.TempDir()
t.Cleanup(launchPostSupervisor(t, logger, grpcCfg, postDir))

select {
Expand All @@ -382,7 +378,7 @@ func TestNewNIPostBuilderNotInitialized(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, nipost)

verifier, err := activation.NewPostVerifier(mgr.Config(), zaptest.NewLogger(t).Named("verifier"))
verifier, err := activation.NewPostVerifier(mgr.Config(), logger.Named("verifier"))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, verifier.Close()) })

Expand Down
147 changes: 147 additions & 0 deletions activation/e2e/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package activation_test

import (
"context"
"fmt"
"testing"
"time"

"github.com/spacemeshos/post/config"
"github.com/spacemeshos/post/initialization"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"

"github.com/spacemeshos/go-spacemesh/activation"
"github.com/spacemeshos/go-spacemesh/api/grpcserver"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/signing"
"github.com/spacemeshos/go-spacemesh/sql"
)

func TestValidator_Validate(t *testing.T) {
ctrl := gomock.NewController(t)

sig, err := signing.NewEdSigner()
require.NoError(t, err)

logger := zaptest.NewLogger(t)
goldenATX := types.ATXID{2, 3, 4}
cfg := activation.DefaultPostConfig()
cdb := datastore.NewCachedDB(sql.InMemory(), log.NewFromLog(logger))

provingOpts := activation.DefaultPostProvingOpts()
provingOpts.Flags = config.RecommendedPowFlags()

mgr, err := activation.NewPostSetupManager(sig.NodeID(), cfg, logger, cdb, goldenATX, provingOpts)
require.NoError(t, err)

postDir := t.TempDir()
opts := activation.DefaultPostSetupOpts()
opts.DataDir = postDir
opts.ProviderID.SetInt64(int64(initialization.CPUProviderID()))
opts.Scrypt.N = 2 // Speedup initialization in tests.
initPost(t, logger.Named("manager"), mgr, opts)

epoch := layersPerEpoch * layerDuration
poetCfg := activation.PoetConfig{
PhaseShift: epoch / 2,
CycleGap: epoch / 5,
GracePeriod: epoch / 5,
RequestTimeout: epoch / 5,
RequestRetryDelay: epoch / 50,
MaxRequestRetries: 10,
}
poetProver := spawnPoet(t, WithGenesis(time.Now()), WithEpochDuration(epoch), WithPhaseShift(poetCfg.PhaseShift), WithCycleGap(poetCfg.CycleGap))

mclock := activation.NewMocklayerClock(ctrl)
mclock.EXPECT().LayerToTime(gomock.Any()).AnyTimes().DoAndReturn(
func(got types.LayerID) time.Time {
// time.Now() ~= currentLayer
genesis := time.Now().Add(-time.Duration(postGenesisEpoch.FirstLayer()) * layerDuration)
return genesis.Add(layerDuration * time.Duration(got))
},
)

verifier, err := activation.NewPostVerifier(mgr.Config(), logger.Named("verifier"))
require.NoError(t, err)
t.Cleanup(func() { assert.NoError(t, verifier.Close()) })

poetDb := activation.NewPoetDb(sql.InMemory(), log.NewFromLog(logger).Named("poetDb"))
v := activation.NewValidator(poetDb, mgr.Config(), mgr.LastOpts().Scrypt, verifier)

nb, err := activation.NewNIPostBuilder(
sig.NodeID(),
mgr,
poetDb,
[]string{poetProver.RestURL().String()},
t.TempDir(),
logtest.New(t, zapcore.DebugLevel),
sig,
poetCfg,
mclock,
activation.WithNipostValidator(v),
)
require.NoError(t, err)

connected := make(chan struct{})
con := grpcserver.NewMockpostConnectionListener(ctrl)
con.EXPECT().Connected(gomock.Any()).DoAndReturn(func(c activation.PostClient) {
close(connected)
}).Times(1)
con.EXPECT().Disconnected(gomock.Any()).Times(1)

svc := grpcserver.NewPostService(logger, nb, con)
grpcCfg, cleanup := launchServer(t, svc)
t.Cleanup(cleanup)

t.Cleanup(launchPostSupervisor(t, logger, grpcCfg, postDir))

select {
case <-connected:
case <-time.After(10 * time.Second):
require.Fail(t, "timed out waiting for connection")
}

challenge := types.NIPostChallenge{
PublishEpoch: postGenesisEpoch + 2,
}
challengeHash := challenge.Hash()

nipost, err := nb.BuildNIPost(context.Background(), &challenge)
require.NoError(t, err)

_, err = v.NIPost(context.Background(), sig.NodeID(), goldenATX, nipost, challengeHash, mgr.LastOpts().NumUnits)
require.NoError(t, err)

_, err = v.NIPost(context.Background(), sig.NodeID(), goldenATX, nipost, types.BytesToHash([]byte("lerner")), mgr.LastOpts().NumUnits)
require.ErrorContains(t, err, "invalid membership proof")

newNIPost := *nipost
newNIPost.Post = &types.Post{}
_, err = v.NIPost(context.Background(), sig.NodeID(), goldenATX, &newNIPost, challengeHash, mgr.LastOpts().NumUnits)
require.ErrorContains(t, err, "invalid Post")

newPostCfg := mgr.Config()
newPostCfg.MinNumUnits = mgr.LastOpts().NumUnits + 1
v = activation.NewValidator(poetDb, newPostCfg, mgr.LastOpts().Scrypt, nil)
_, err = v.NIPost(context.Background(), sig.NodeID(), goldenATX, nipost, challengeHash, mgr.LastOpts().NumUnits)
require.EqualError(t, err, fmt.Sprintf("invalid `numUnits`; expected: >=%d, given: %d", newPostCfg.MinNumUnits, mgr.LastOpts().NumUnits))

newPostCfg = mgr.Config()
newPostCfg.MaxNumUnits = mgr.LastOpts().NumUnits - 1
v = activation.NewValidator(poetDb, newPostCfg, mgr.LastOpts().Scrypt, nil)
_, err = v.NIPost(context.Background(), sig.NodeID(), goldenATX, nipost, challengeHash, mgr.LastOpts().NumUnits)
require.EqualError(t, err, fmt.Sprintf("invalid `numUnits`; expected: <=%d, given: %d", newPostCfg.MaxNumUnits, mgr.LastOpts().NumUnits))

newPostCfg = mgr.Config()
newPostCfg.LabelsPerUnit = nipost.PostMetadata.LabelsPerUnit + 1
v = activation.NewValidator(poetDb, newPostCfg, mgr.LastOpts().Scrypt, nil)
_, err = v.NIPost(context.Background(), sig.NodeID(), goldenATX, nipost, challengeHash, mgr.LastOpts().NumUnits)
require.EqualError(t, err, fmt.Sprintf("invalid `LabelsPerUnit`; expected: >=%d, given: %d", newPostCfg.LabelsPerUnit, nipost.PostMetadata.LabelsPerUnit))
}
64 changes: 0 additions & 64 deletions activation/e2e/validation_test.go.bak

This file was deleted.

0 comments on commit 490ad72

Please sign in to comment.