Skip to content

Commit

Permalink
Disperse tests in test package to where they belong
Browse files Browse the repository at this point in the history
Originally, all tests were put in `test` package to avoid circular
dependency. This no longer holds true. Therefore:

* Move all simulation tests to `sim`
* Move `sim/signing` package to `internal` since it is used by a number
  of tests across the repo as well as `cmd/f3`.
* Move signing suite tests to the now existing `internal/signing`

These changes keep with the go convention of keeping tests in the
package they are testing.
  • Loading branch information
masih committed Oct 8, 2024
1 parent cbf9c5f commit 0d05237
Show file tree
Hide file tree
Showing 30 changed files with 215 additions and 228 deletions.
2 changes: 1 addition & 1 deletion certexchange/polling/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/filecoin-project/go-f3/certs"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/internal/signing"
"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/signing"

"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion certexchange/polling/poller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/filecoin-project/go-f3/certexchange"
"github.com/filecoin-project/go-f3/certexchange/polling"
"github.com/filecoin-project/go-f3/certstore"
"github.com/filecoin-project/go-f3/sim/signing"
"github.com/filecoin-project/go-f3/internal/signing"

"github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
Expand Down
2 changes: 1 addition & 1 deletion certexchange/polling/subscriber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/filecoin-project/go-f3/certexchange/polling"
"github.com/filecoin-project/go-f3/certstore"
"github.com/filecoin-project/go-f3/internal/clock"
"github.com/filecoin-project/go-f3/sim/signing"
"github.com/filecoin-project/go-f3/internal/signing"

"github.com/ipfs/go-datastore"
ds_sync "github.com/ipfs/go-datastore/sync"
Expand Down
2 changes: 1 addition & 1 deletion certs/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/filecoin-project/go-f3/certs"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/internal/signing"
"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/signing"
"github.com/filecoin-project/go-state-types/big"
"github.com/stretchr/testify/require"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/f3/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/filecoin-project/go-f3"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/internal/consensus"
"github.com/filecoin-project/go-f3/internal/signing"
"github.com/filecoin-project/go-f3/manifest"
"github.com/filecoin-project/go-f3/sim/signing"
leveldb "github.com/ipfs/go-ds-leveldb"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p"
Expand Down
2 changes: 1 addition & 1 deletion f3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/filecoin-project/go-f3/internal/clock"
"github.com/filecoin-project/go-f3/internal/consensus"
"github.com/filecoin-project/go-f3/internal/psutil"
"github.com/filecoin-project/go-f3/internal/signing"
"github.com/filecoin-project/go-f3/manifest"
"github.com/filecoin-project/go-f3/sim/signing"

"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/failstore"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package signing_test

import (
"context"
Expand All @@ -12,7 +12,7 @@ import (
"github.com/filecoin-project/go-f3/blssig"
"github.com/filecoin-project/go-f3/gpbft"
bls12381 "github.com/filecoin-project/go-f3/internal/gnark"
"github.com/filecoin-project/go-f3/sim/signing"
"github.com/filecoin-project/go-f3/internal/signing"
)

type (
Expand Down Expand Up @@ -122,7 +122,7 @@ func (s *SigningTestSuite) TestAggregateAndVerify() {
require.Error(t, err)

_, err = aggregator.Aggregate(mask, [][]byte{sigs[0]})
require.Error(t, err, "Missmatched pubkeys and sigs lengths should fail")
require.Error(t, err, "mismatched pubkeys and sigs lengths should fail")

{
pubKeys2 := slices.Clone(pubKeys)
Expand Down
11 changes: 5 additions & 6 deletions test/absent_test.go → sim/absent_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package test
package sim

import (
"testing"

"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/adversary"
"github.com/stretchr/testify/require"
)
Expand All @@ -17,14 +16,14 @@ func FuzzAbsentAdversary(f *testing.F) {
f.Add(-855) // Takes 12 rounds to complete.
f.Fuzz(func(t *testing.T, latencySeed int) {
t.Parallel()
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
asyncOptions(latencySeed,
// Total network size of 3 + 1, where the adversary has 1/4 of power.
sim.AddHonestParticipants(
AddHonestParticipants(
3,
sim.NewUniformECChainGenerator(tipSetGeneratorSeed, 1, 5),
NewUniformECChainGenerator(tipSetGeneratorSeed, 1, 5),
uniformOneStoragePower),
sim.WithAdversary(adversary.NewAbsentGenerator(oneStoragePower)),
WithAdversary(adversary.NewAbsentGenerator(oneStoragePower)),
)...)
require.NoError(t, err)
require.NoErrorf(t, sm.Run(1, maxRounds+2), "%s", sm.Describe())
Expand Down
2 changes: 1 addition & 1 deletion test/ballast_test.go → sim/ballast_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package sim

var ballast []byte

Expand Down
2 changes: 1 addition & 1 deletion sim/cmd/f3sim/f3sim.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"time"

"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/internal/signing"
"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/latency"
"github.com/filecoin-project/go-f3/sim/signing"
)

func main() {
Expand Down
25 changes: 12 additions & 13 deletions test/constants_test.go → sim/constants_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package test
package sim

import (
"time"

"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/latency"
)

Expand All @@ -20,7 +19,7 @@ const (

var (
oneStoragePower = gpbft.NewStoragePower(1)
uniformOneStoragePower = sim.UniformStoragePower(oneStoragePower)
uniformOneStoragePower = UniformStoragePower(oneStoragePower)

// testGpbftOptions is configuration constants used across most tests.
// These values are not intended to reflect real-world conditions.
Expand All @@ -33,22 +32,22 @@ var (
}
)

func syncOptions(o ...sim.Option) []sim.Option {
func syncOptions(o ...Option) []Option {
return append(o,
sim.WithLatencyModeler(func() (latency.Model, error) { return latency.None, nil }),
sim.WithECEpochDuration(EcEpochDuration),
sim.WitECStabilisationDelay(EcStabilisationDelay),
sim.WithGpbftOptions(testGpbftOptions...),
WithLatencyModeler(func() (latency.Model, error) { return latency.None, nil }),
WithECEpochDuration(EcEpochDuration),
WitECStabilisationDelay(EcStabilisationDelay),
WithGpbftOptions(testGpbftOptions...),
)
}

func asyncOptions(latencySeed int, o ...sim.Option) []sim.Option {
func asyncOptions(latencySeed int, o ...Option) []Option {
return append(o,
sim.WithLatencyModeler(func() (latency.Model, error) {
WithLatencyModeler(func() (latency.Model, error) {
return latency.NewLogNormal(int64(latencySeed), latencyAsync), nil
}),
sim.WithECEpochDuration(EcEpochDuration),
sim.WitECStabilisationDelay(EcStabilisationDelay),
sim.WithGpbftOptions(testGpbftOptions...),
WithECEpochDuration(EcEpochDuration),
WitECStabilisationDelay(EcStabilisationDelay),
WithGpbftOptions(testGpbftOptions...),
)
}
69 changes: 34 additions & 35 deletions test/decide_test.go → sim/decide_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package test
package sim

import (
"fmt"
"math/rand"
"testing"

"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/adversary"
"github.com/stretchr/testify/require"
)
Expand All @@ -20,18 +19,18 @@ func FuzzImmediateDecideAdversary(f *testing.F) {
f.Fuzz(func(t *testing.T, seed int) {
t.Parallel()
rng := rand.New(rand.NewSource(int64(seed)))
tsg := sim.NewTipSetGenerator(tipSetGeneratorSeed)
tsg := NewTipSetGenerator(tipSetGeneratorSeed)
baseChain := generateECChain(t, tsg)
adversaryValue := baseChain.Extend(tsg.Sample())
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
asyncOptions(rng.Int(),
sim.AddHonestParticipants(
AddHonestParticipants(
1,
sim.NewUniformECChainGenerator(rng.Uint64(), 1, 5),
NewUniformECChainGenerator(rng.Uint64(), 1, 5),
uniformOneStoragePower),
sim.WithBaseChain(&baseChain),
WithBaseChain(&baseChain),
// Add the adversary to the simulation with 3/4 of total power.
sim.WithAdversary(adversary.NewImmediateDecideGenerator(adversaryValue, gpbft.NewStoragePower(3))),
WithAdversary(adversary.NewImmediateDecideGenerator(adversaryValue, gpbft.NewStoragePower(3))),
)...)
require.NoError(t, err)

Expand All @@ -52,18 +51,18 @@ func TestIllegalCommittee_OutOfRange(t *testing.T) {
const seed = 98562314
t.Parallel()
rng := rand.New(rand.NewSource(int64(seed)))
tsg := sim.NewTipSetGenerator(tipSetGeneratorSeed)
tsg := NewTipSetGenerator(tipSetGeneratorSeed)
baseChain := generateECChain(t, tsg)
adversaryValue := baseChain.Extend(tsg.Sample())
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
asyncOptions(rng.Int(),
sim.AddHonestParticipants(
AddHonestParticipants(
1,
sim.NewUniformECChainGenerator(rng.Uint64(), 1, 5),
NewUniformECChainGenerator(rng.Uint64(), 1, 5),
uniformOneStoragePower),
sim.WithBaseChain(&baseChain),
WithBaseChain(&baseChain),
// Add the adversary to the simulation with 3/4 of total power.
sim.WithAdversary(adversary.NewImmediateDecideGenerator(
WithAdversary(adversary.NewImmediateDecideGenerator(
adversaryValue,
gpbft.NewStoragePower(3),
adversary.ImmediateDecideWithNthParticipant(100))),
Expand All @@ -78,21 +77,21 @@ func TestIllegalCommittee_NoPower(t *testing.T) {
const seed = 98562314
t.Parallel()
rng := rand.New(rand.NewSource(int64(seed)))
tsg := sim.NewTipSetGenerator(tipSetGeneratorSeed)
tsg := NewTipSetGenerator(tipSetGeneratorSeed)
baseChain := generateECChain(t, tsg)
adversaryValue := baseChain.Extend(tsg.Sample())
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
asyncOptions(rng.Int(),
sim.AddHonestParticipants(
AddHonestParticipants(
1,
sim.NewUniformECChainGenerator(rng.Uint64(), 1, 5),
sim.UniformStoragePower(gpbft.NewStoragePower(1))),
sim.WithBaseChain(&baseChain),
NewUniformECChainGenerator(rng.Uint64(), 1, 5),
UniformStoragePower(gpbft.NewStoragePower(1))),
WithBaseChain(&baseChain),
// Add the adversary to the simulation with enough power to make the honest
// node's power a rounding error. Then have that adversary include the
// honest participant in their decision (which is illegal because the
// decision has no effective power due to rounding).
sim.WithAdversary(adversary.NewImmediateDecideGenerator(
WithAdversary(adversary.NewImmediateDecideGenerator(
adversaryValue,
gpbft.NewStoragePower(0xffff),
adversary.ImmediateDecideWithNthParticipant(1))),
Expand All @@ -107,17 +106,17 @@ func TestIllegalCommittee_WrongValue(t *testing.T) {
const seed = 98562314
t.Parallel()
rng := rand.New(rand.NewSource(int64(seed)))
tsg := sim.NewTipSetGenerator(tipSetGeneratorSeed)
tsg := NewTipSetGenerator(tipSetGeneratorSeed)
baseChain := generateECChain(t, tsg)
adversaryValue := baseChain.Extend(tsg.Sample())
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
asyncOptions(rng.Int(),
sim.AddHonestParticipants(
AddHonestParticipants(
1,
sim.NewUniformECChainGenerator(rng.Uint64(), 1, 5),
sim.UniformStoragePower(gpbft.NewStoragePower(1))),
sim.WithBaseChain(&baseChain),
sim.WithAdversary(adversary.NewImmediateDecideGenerator(
NewUniformECChainGenerator(rng.Uint64(), 1, 5),
UniformStoragePower(gpbft.NewStoragePower(1))),
WithBaseChain(&baseChain),
WithAdversary(adversary.NewImmediateDecideGenerator(
adversaryValue,
gpbft.NewStoragePower(3),
// Justify the base-chain, but attempt to decide on something else.
Expand All @@ -133,17 +132,17 @@ func TestIllegalCommittee_WrongSupplementalData(t *testing.T) {
const seed = 98562314
t.Parallel()
rng := rand.New(rand.NewSource(int64(seed)))
tsg := sim.NewTipSetGenerator(tipSetGeneratorSeed)
tsg := NewTipSetGenerator(tipSetGeneratorSeed)
baseChain := generateECChain(t, tsg)
adversaryValue := baseChain.Extend(tsg.Sample())
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
asyncOptions(rng.Int(),
sim.AddHonestParticipants(
AddHonestParticipants(
1,
sim.NewUniformECChainGenerator(rng.Uint64(), 1, 5),
sim.UniformStoragePower(gpbft.NewStoragePower(1))),
sim.WithBaseChain(&baseChain),
sim.WithAdversary(adversary.NewImmediateDecideGenerator(
NewUniformECChainGenerator(rng.Uint64(), 1, 5),
UniformStoragePower(gpbft.NewStoragePower(1))),
WithBaseChain(&baseChain),
WithAdversary(adversary.NewImmediateDecideGenerator(
adversaryValue,
gpbft.NewStoragePower(3),
// Justify the base-chain, but attempt to decide on something else.
Expand Down
27 changes: 13 additions & 14 deletions test/deny_test.go → sim/deny_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package test
package sim

import (
"fmt"
"math"
"testing"

"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/sim"
"github.com/filecoin-project/go-f3/sim/adversary"
"github.com/stretchr/testify/require"
)
Expand All @@ -20,13 +19,13 @@ func TestDeny_SkipsToFuture(t *testing.T) {
gst = 100 * EcEpochDuration
)

ecChainGenerator := sim.NewUniformECChainGenerator(54445, 1, 5)
sm, err := sim.NewSimulation(
ecChainGenerator := NewUniformECChainGenerator(54445, 1, 5)
sm, err := NewSimulation(
asyncOptions(2342342,
sim.AddHonestParticipants(6, ecChainGenerator, uniformOneStoragePower),
sim.WithAdversary(adversary.NewDenyGenerator(oneStoragePower, gst, adversary.DenyAllMessages, adversary.DenyToOrFrom, denialTarget)),
sim.WithGlobalStabilizationTime(gst),
sim.WithIgnoreConsensusFor(denialTarget),
AddHonestParticipants(6, ecChainGenerator, uniformOneStoragePower),
WithAdversary(adversary.NewDenyGenerator(oneStoragePower, gst, adversary.DenyAllMessages, adversary.DenyToOrFrom, denialTarget)),
WithGlobalStabilizationTime(gst),
WithIgnoreConsensusFor(denialTarget),
)...,
)
require.NoError(t, err)
Expand All @@ -48,18 +47,18 @@ func TestDenyPhase(t *testing.T) {
for _, denyMode := range []adversary.DenyTargetMode{adversary.DenyToOrFrom, adversary.DenyFrom, adversary.DenyTo} {
t.Run(fmt.Sprintf("%s/%s", denyMode, phase), func(t *testing.T) {
t.Parallel()
ecGen := sim.NewUniformECChainGenerator(4332432, 1, 5)
ecGen := NewUniformECChainGenerator(4332432, 1, 5)

var attacked []gpbft.ActorID
for i := gpbft.ActorID(1); i <= participants; i++ {
attacked = append(attacked, i)
}
sm, err := sim.NewSimulation(
sm, err := NewSimulation(
syncOptions(
sim.AddHonestParticipants(1, ecGen, sim.UniformStoragePower(gpbft.NewStoragePower(100*participants))),
sim.AddHonestParticipants(participants, ecGen, sim.UniformStoragePower(gpbft.NewStoragePower(100))),
sim.WithAdversary(adversary.NewDenyGenerator(gpbft.NewStoragePower(1), gst, adversary.DenyPhase(phase), denyMode, attacked...)),
sim.WithGlobalStabilizationTime(gst),
AddHonestParticipants(1, ecGen, UniformStoragePower(gpbft.NewStoragePower(100*participants))),
AddHonestParticipants(participants, ecGen, UniformStoragePower(gpbft.NewStoragePower(100))),
WithAdversary(adversary.NewDenyGenerator(gpbft.NewStoragePower(1), gst, adversary.DenyPhase(phase), denyMode, attacked...)),
WithGlobalStabilizationTime(gst),
)...,
)
require.NoError(t, err)
Expand Down
Loading

0 comments on commit 0d05237

Please sign in to comment.