Skip to content

Commit

Permalink
Remove wire type from more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Aug 12, 2024
1 parent f0b477f commit 32dea94
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 84 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{Blob: types.RandomBytes(100)}))
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{Blob: types.RandomBytes(100)}))
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{Blob: types.RandomBytes(100)}))
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{Blob: types.RandomBytes(100)}))
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{Blob: types.RandomBytes(100)}))
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
46 changes: 11 additions & 35 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,17 @@ 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(),
SmesherID: nodeID,
}
atx.Sign(signer)
return atx
}

func ToAtx(t testing.TB, watx *wire.ActivationTxV1) *types.ActivationTx {
t.Helper()
atx := wire.ActivationTxFromWireV1(watx)
atx.SetReceived(time.Now().Local())
atx.TickCount = 1
return atx
}
22 changes: 8 additions & 14 deletions datastore/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"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 +84,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,7 +100,7 @@ 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()))
require.NoError(t, atxs.Add(db, atx, types.AtxBlob{Blob: types.RandomBytes(100)}))

has, err = bs.Has(datastore.ATXDB, atx.ID().Bytes())
require.NoError(t, err)
Expand Down
25 changes: 8 additions & 17 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 @@ -263,22 +261,15 @@ 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,
},
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, types.AtxBlob{Blob: types.RandomBytes(100)}
}

func TestHandleEpochInfoReq(t *testing.T) {
Expand Down

0 comments on commit 32dea94

Please sign in to comment.