Skip to content

Commit

Permalink
chore: remove dependency on activation/wire from tests outside `act…
Browse files Browse the repository at this point in the history
…ivation` (#6244)

## Motivation

Most packages should be unaware of wire types and not depend on packages implementing them (including tests).
This removes the dependency on `activation/wire` from most other packages.
  • Loading branch information
fasmat committed Aug 13, 2024
1 parent f135361 commit 11eb36b
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 253 deletions.
25 changes: 10 additions & 15 deletions api/grpcserver/v2alpha1/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ func TestActivationService_List(t *testing.T) {
activations := make([]types.ActivationTx, 100)
for i := range activations {
atx := gen.Next()
vAtx := fixture.ToAtx(t, atx)
require.NoError(t, atxs.Add(db, vAtx, atx.Blob()))
activations[i] = *vAtx
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{}))
activations[i] = *atx
}

svc := NewActivationService(db)
Expand Down Expand Up @@ -111,9 +110,8 @@ func TestActivationStreamService_Stream(t *testing.T) {
activations := make([]types.ActivationTx, 100)
for i := range activations {
atx := gen.Next()
vAtx := fixture.ToAtx(t, atx)
require.NoError(t, atxs.Add(db, vAtx, atx.Blob()))
activations[i] = *vAtx
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{}))
activations[i] = *atx
}

svc := NewActivationStreamService(db)
Expand Down Expand Up @@ -153,9 +151,8 @@ func TestActivationStreamService_Stream(t *testing.T) {
gen = fixture.NewAtxsGenerator().WithEpochs(start, 10)
var streamed []*events.ActivationTx
for i := 0; i < n; i++ {
watx := gen.Next()
atx := fixture.ToAtx(t, watx)
require.NoError(t, atxs.Add(db, atx, watx.Blob()))
atx := gen.Next()
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{}))
streamed = append(streamed, &events.ActivationTx{ActivationTx: atx})
}

Expand Down Expand Up @@ -221,19 +218,17 @@ func TestActivationService_ActivationsCount(t *testing.T) {
epoch3ATXs := make([]types.ActivationTx, 30)
for i := range epoch3ATXs {
atx := genEpoch3.Next()
vatx := fixture.ToAtx(t, atx)
require.NoError(t, atxs.Add(db, vatx, atx.Blob()))
epoch3ATXs[i] = *vatx
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{}))
epoch3ATXs[i] = *atx
}

genEpoch5 := fixture.NewAtxsGenerator().WithSeed(time.Now().UnixNano()+1).
WithEpochs(5, 1)
epoch5ATXs := make([]types.ActivationTx, 10) // ensure the number here is different from above
for i := range epoch5ATXs {
atx := genEpoch5.Next()
vatx := fixture.ToAtx(t, atx)
require.NoError(t, atxs.Add(db, vatx, atx.Blob()))
epoch5ATXs[i] = *vatx
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{}))
epoch5ATXs[i] = *atx
}

svc := NewActivationService(db)
Expand Down
8 changes: 5 additions & 3 deletions checkpoint/recovery_collecting_deps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"golang.org/x/exp/maps"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/common/fixture"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
Expand All @@ -29,15 +28,18 @@ func TestCollectingDeps(t *testing.T) {
},
SmesherID: types.RandomNodeID(),
}
require.NoError(t, atxs.Add(db, fixture.ToAtx(t, marriageATX), marriageATX.Blob()))
atx := wire.ActivationTxFromWireV1(marriageATX)
atx.SetReceived(time.Now().Local())
atx.TickCount = 1
require.NoError(t, atxs.Add(db, atx, marriageATX.Blob()))
mAtxID := marriageATX.ID()

watx := &wire.ActivationTxV2{
PositioningATX: golden,
SmesherID: types.RandomNodeID(),
MarriageATX: &mAtxID,
}
atx := &types.ActivationTx{
atx = &types.ActivationTx{
SmesherID: watx.SmesherID,
}
atx.SetID(watx.ID())
Expand Down
47 changes: 13 additions & 34 deletions common/fixture/atxs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package fixture

import (
"math/rand"
"testing"
"time"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/genvm/sdk/wallet"
"github.com/spacemeshos/go-spacemesh/signing"
)

// NewAtxsGenerator with some random parameters.
Expand Down Expand Up @@ -42,38 +39,20 @@ func (g *AtxsGenerator) WithEpochs(start, n int) *AtxsGenerator {
return g
}

// Next generates VerifiedActivationTx.
func (g *AtxsGenerator) Next() *wire.ActivationTxV1 {
var prevAtxId types.ATXID
g.rng.Read(prevAtxId[:])
var posAtxId types.ATXID
g.rng.Read(posAtxId[:])

signer, err := signing.NewEdSigner(signing.WithKeyFromRand(g.rng))
if err != nil {
panic("failed to create signer")
}

atx := &wire.ActivationTxV1{
InnerActivationTxV1: wire.InnerActivationTxV1{
NIPostChallengeV1: wire.NIPostChallengeV1{
Sequence: g.rng.Uint64(),
PrevATXID: prevAtxId,
PublishEpoch: g.Epochs[g.rng.Intn(len(g.Epochs))],
PositioningATXID: posAtxId,
},
Coinbase: wallet.Address(signer.PublicKey().Bytes()),
NumUnits: g.rng.Uint32(),
},
// Next generates ActivationTx.
func (g *AtxsGenerator) Next() *types.ActivationTx {
var nodeID types.NodeID
g.rng.Read(nodeID[:])

atx := &types.ActivationTx{
Sequence: g.rng.Uint64(),
PublishEpoch: g.Epochs[g.rng.Intn(len(g.Epochs))],
Coinbase: wallet.Address(nodeID.Bytes()),
NumUnits: g.rng.Uint32(),
TickCount: 1,
SmesherID: nodeID,
}
atx.Sign(signer)
return atx
}

func ToAtx(t testing.TB, watx *wire.ActivationTxV1) *types.ActivationTx {
t.Helper()
atx := wire.ActivationTxFromWireV1(watx)
atx.SetID(types.RandomATXID())
atx.SetReceived(time.Now().Local())
atx.TickCount = 1
return atx
}
30 changes: 10 additions & 20 deletions datastore/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/fixture"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
mwire "github.com/spacemeshos/go-spacemesh/malfeasance/wire"
Expand Down Expand Up @@ -85,19 +83,14 @@ func TestBlobStore_GetATXBlob(t *testing.T) {
bs := datastore.NewBlobStore(db, store.New())
ctx := context.Background()

atx := &wire.ActivationTxV1{
InnerActivationTxV1: wire.InnerActivationTxV1{
NIPostChallengeV1: wire.NIPostChallengeV1{
PublishEpoch: types.EpochID(22),
Sequence: 11,
},
NumUnits: 11,
},
atx := &types.ActivationTx{
PublishEpoch: types.EpochID(22),
Sequence: 11,
NumUnits: 11,
SmesherID: types.RandomNodeID(),
}
signer, err := signing.NewEdSigner()
require.NoError(t, err)
atx.Sign(signer)
vAtx := fixture.ToAtx(t, atx)
atx.SetID(types.RandomATXID())
atx.SetReceived(time.Now().Local())

has, err := bs.Has(datastore.ATXDB, atx.ID().Bytes())
require.NoError(t, err)
Expand All @@ -106,18 +99,15 @@ func TestBlobStore_GetATXBlob(t *testing.T) {
_, err = getBytes(ctx, bs, datastore.ATXDB, atx.ID())
require.ErrorIs(t, err, datastore.ErrNotFound)

require.NoError(t, atxs.Add(db, vAtx, atx.Blob()))
blob := types.AtxBlob{Blob: types.RandomBytes(100)}
require.NoError(t, atxs.Add(db, atx, blob))

has, err = bs.Has(datastore.ATXDB, atx.ID().Bytes())
require.NoError(t, err)
require.True(t, has)
got, err := getBytes(ctx, bs, datastore.ATXDB, atx.ID())
require.NoError(t, err)

gotA, err := wire.DecodeAtxV1(got)
require.NoError(t, err)
require.Equal(t, atx.ID(), gotA.ID())
require.Equal(t, atx, gotA)
require.Equal(t, blob.Blob, got)

_, err = getBytes(ctx, bs, datastore.BallotDB, atx.ID())
require.ErrorIs(t, err, datastore.ErrNotFound)
Expand Down
39 changes: 15 additions & 24 deletions fetch/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"

"github.com/spacemeshos/go-spacemesh/activation/wire"
"github.com/spacemeshos/go-spacemesh/codec"
"github.com/spacemeshos/go-spacemesh/common/fixture"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/p2p/server"
Expand Down Expand Up @@ -262,23 +260,16 @@ func TestHandleMeshHashReq(t *testing.T) {
}
}

func newAtx(t *testing.T, published types.EpochID) (*types.ActivationTx, types.AtxBlob) {
t.Helper()
nonce := uint64(123)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
atx := &wire.ActivationTxV1{
InnerActivationTxV1: wire.InnerActivationTxV1{
NIPostChallengeV1: wire.NIPostChallengeV1{
PublishEpoch: published,
PrevATXID: types.RandomATXID(),
},
NumUnits: 2,
VRFNonce: &nonce,
},
func newAtx(t *testing.T, published types.EpochID) *types.ActivationTx {
atx := &types.ActivationTx{
PublishEpoch: published,
NumUnits: 2,
VRFNonce: types.VRFPostIndex(123),
SmesherID: types.RandomNodeID(),
}
atx.Sign(signer)
return fixture.ToAtx(t, atx), atx.Blob()
atx.SetID(types.RandomATXID())
atx.SetReceived(time.Now().Local())
return atx
}

func TestHandleEpochInfoReq(t *testing.T) {
Expand All @@ -304,8 +295,8 @@ func TestHandleEpochInfoReq(t *testing.T) {
var expected EpochData
if !tc.missingData {
for i := 0; i < 10; i++ {
vatx, blob := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx, blob))
vatx := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx, types.AtxBlob{}))
expected.AtxIDs = append(expected.AtxIDs, vatx.ID())
}
}
Expand Down Expand Up @@ -353,8 +344,8 @@ func testHandleEpochInfoReqWithQueryCache(
var expected EpochData

for i := 0; i < 10; i++ {
vatx, blob := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx, blob))
vatx := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx, types.AtxBlob{}))
atxs.AtxAdded(th.cdb, vatx)
expected.AtxIDs = append(expected.AtxIDs, vatx.ID())
}
Expand All @@ -372,8 +363,8 @@ func testHandleEpochInfoReqWithQueryCache(
}

// Add another ATX which should be appended to the cached slice
vatx, blob := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx, blob))
vatx := newAtx(t, epoch)
require.NoError(t, atxs.Add(th.cdb, vatx, types.AtxBlob{}))
atxs.AtxAdded(th.cdb, vatx)
expected.AtxIDs = append(expected.AtxIDs, vatx.ID())
require.Equal(t, 23, qc.QueryCount())
Expand Down
Loading

0 comments on commit 11eb36b

Please sign in to comment.