diff --git a/api/grpcserver/v2alpha1/activation_test.go b/api/grpcserver/v2alpha1/activation_test.go index 183bd11c8be..46a8391338a 100644 --- a/api/grpcserver/v2alpha1/activation_test.go +++ b/api/grpcserver/v2alpha1/activation_test.go @@ -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) @@ -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) @@ -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}) } @@ -221,9 +218,8 @@ 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). @@ -231,9 +227,8 @@ func TestActivationService_ActivationsCount(t *testing.T) { 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) diff --git a/checkpoint/recovery_collecting_deps_test.go b/checkpoint/recovery_collecting_deps_test.go index 1df6ca0a9b7..53df59841f7 100644 --- a/checkpoint/recovery_collecting_deps_test.go +++ b/checkpoint/recovery_collecting_deps_test.go @@ -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" @@ -29,7 +28,10 @@ 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{ @@ -37,7 +39,7 @@ func TestCollectingDeps(t *testing.T) { SmesherID: types.RandomNodeID(), MarriageATX: &mAtxID, } - atx := &types.ActivationTx{ + atx = &types.ActivationTx{ SmesherID: watx.SmesherID, } atx.SetID(watx.ID()) diff --git a/common/fixture/atxs.go b/common/fixture/atxs.go index af3ead08f2e..dc61c18b8ba 100644 --- a/common/fixture/atxs.go +++ b/common/fixture/atxs.go @@ -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. @@ -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 } diff --git a/datastore/store_test.go b/datastore/store_test.go index e928e1b226a..b3ae9fe55bd 100644 --- a/datastore/store_test.go +++ b/datastore/store_test.go @@ -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" @@ -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) @@ -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) diff --git a/fetch/handler_test.go b/fetch/handler_test.go index d42900a28a5..24749e1a2d1 100644 --- a/fetch/handler_test.go +++ b/fetch/handler_test.go @@ -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" @@ -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) { @@ -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()) } } @@ -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()) } @@ -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()) diff --git a/fetch/p2p_test.go b/fetch/p2p_test.go index a9dc85a8254..b76929320c4 100644 --- a/fetch/p2p_test.go +++ b/fetch/p2p_test.go @@ -171,8 +171,8 @@ func createP2PFetch( func (tpf *testP2PFetch) createATXs(epoch types.EpochID) []types.ATXID { atxIDs := make([]types.ATXID, 10) for i := range atxIDs { - atx, blob := newAtx(tpf.t, epoch) - require.NoError(tpf.t, atxs.Add(tpf.serverCDB, atx, blob)) + atx := newAtx(tpf.t, epoch) + require.NoError(tpf.t, atxs.Add(tpf.serverCDB, atx, types.AtxBlob{})) atxIDs[i] = atx.ID() } return atxIDs @@ -351,15 +351,14 @@ func TestP2PGetATXs(t *testing.T) { t, "database: no free connection", func(t *testing.T, ctx context.Context, tpf *testP2PFetch, errStr string) { epoch := types.EpochID(11) - atx, blob := newAtx(tpf.t, epoch) + atx := newAtx(tpf.t, epoch) + blob := types.AtxBlob{Blob: types.RandomBytes(100)} require.NoError(tpf.t, atxs.Add(tpf.serverCDB, atx, blob)) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetAtxs( - context.Background(), []types.ATXID{atx.ID()}) - }, + func() error { return tpf.clientFetch.GetAtxs(context.Background(), []types.ATXID{atx.ID()}) }, errStr, "atx", "hs/1", types.Hash32(atx.ID()), atx.ID().Bytes(), - blob.Blob) + blob.Blob, + ) }) } @@ -368,17 +367,13 @@ func TestP2PGetPoet(t *testing.T) { t, "database: no free connection", false, func(t *testing.T, ctx context.Context, tpf *testP2PFetch, errStr string) { ref := types.PoetProofRef{0x42, 0x43} - require.NoError(t, poets.Add( - tpf.serverCDB, ref, - []byte("proof1"), []byte("sid1"), "rid1")) + require.NoError(t, poets.Add(tpf.serverCDB, ref, []byte("proof1"), []byte("sid1"), "rid1")) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetPoetProof( - context.Background(), types.Hash32(ref)) - }, + func() error { return tpf.clientFetch.GetPoetProof(context.Background(), types.Hash32(ref)) }, errStr, "poet", "hs/1", types.Hash32(ref), ref[:], - []byte("proof1")) + []byte("proof1"), + ) }) } @@ -397,12 +392,10 @@ func TestP2PGetBallot(t *testing.T) { require.NoError(t, ballots.Add(tpf.serverCDB, b)) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetBallots( - context.Background(), []types.BallotID{b.ID()}) - }, + func() error { return tpf.clientFetch.GetBallots(context.Background(), []types.BallotID{b.ID()}) }, errStr, "ballot", "hs/1", b.ID().AsHash32(), b.ID().Bytes(), - codec.MustEncode(b)) + codec.MustEncode(b), + ) }) } @@ -418,11 +411,10 @@ func TestP2PGetActiveSet(t *testing.T) { require.NoError(tpf.t, activesets.Add(tpf.serverCDB, id, set)) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetActiveSet(context.Background(), id) - }, + func() error { return tpf.clientFetch.GetActiveSet(context.Background(), id) }, errStr, "activeset", "as/1", id, id.Bytes(), - codec.MustEncode(set)) + codec.MustEncode(set), + ) }) } @@ -435,12 +427,10 @@ func TestP2PGetBlock(t *testing.T) { require.NoError(t, blocks.Add(tpf.serverCDB, bk)) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetBlocks( - context.Background(), []types.BlockID{bk.ID()}) - }, + func() error { return tpf.clientFetch.GetBlocks(context.Background(), []types.BlockID{bk.ID()}) }, errStr, "block", "hs/1", bk.ID().AsHash32(), bk.ID().Bytes(), - codec.MustEncode(bk)) + codec.MustEncode(bk), + ) }) } @@ -488,12 +478,10 @@ func TestP2PGetBlockTransactions(t *testing.T) { tx := genTx(t, signer, types.Address{1}, 1, 1, 1) require.NoError(t, transactions.Add(tpf.serverCDB, &tx, time.Now())) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetBlockTxs( - context.Background(), []types.TransactionID{tx.ID}) - }, + func() error { return tpf.clientFetch.GetBlockTxs(context.Background(), []types.TransactionID{tx.ID}) }, errStr, "txBlock", "hs/1", types.Hash32(tx.ID), tx.ID.Bytes(), - tx.Raw) + tx.Raw, + ) }) } @@ -507,11 +495,11 @@ func TestP2PGetProposalTransactions(t *testing.T) { require.NoError(t, transactions.Add(tpf.serverCDB, &tx, time.Now())) tpf.verifyGetHash( func() error { - return tpf.clientFetch.GetProposalTxs( - context.Background(), []types.TransactionID{tx.ID}) + return tpf.clientFetch.GetProposalTxs(context.Background(), []types.TransactionID{tx.ID}) }, errStr, "txProposal", "hs/1", types.Hash32(tx.ID), tx.ID.Bytes(), - tx.Raw) + tx.Raw, + ) }) } @@ -521,14 +509,11 @@ func TestP2PGetMalfeasanceProofs(t *testing.T) { func(t *testing.T, ctx context.Context, tpf *testP2PFetch, errStr string) { nid := types.RandomNodeID() proof := types.RandomBytes(11) - require.NoError(t, identities.SetMalicious( - tpf.serverCDB, nid, proof, time.Now())) + require.NoError(t, identities.SetMalicious(tpf.serverCDB, nid, proof, time.Now())) tpf.verifyGetHash( - func() error { - return tpf.clientFetch.GetMalfeasanceProofs( - context.Background(), []types.NodeID{nid}) - }, + func() error { return tpf.clientFetch.GetMalfeasanceProofs(context.Background(), []types.NodeID{nid}) }, errStr, "mal", "hs/1", types.Hash32(nid), nid.Bytes(), - proof) + proof, + ) }) } diff --git a/sql/atxs/atxs_test.go b/sql/atxs/atxs_test.go index eb507fe969a..fcdfa95fd72 100644 --- a/sql/atxs/atxs_test.go +++ b/sql/atxs/atxs_test.go @@ -3,6 +3,7 @@ package atxs_test import ( "context" "errors" + "fmt" "os" "slices" "testing" @@ -11,8 +12,6 @@ import ( "github.com/stretchr/testify/require" "golang.org/x/exp/rand" - "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/signing" "github.com/spacemeshos/go-spacemesh/sql" @@ -36,9 +35,9 @@ func TestGet(t *testing.T) { for i := 0; i < 3; i++ { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(types.EpochID(i))) + atx := newAtx(t, sig, withPublishEpoch(types.EpochID(i))) atxList = append(atxList, atx) - require.NoError(t, atxs.Add(db, atx, blob)) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) } for _, want := range atxList { @@ -58,8 +57,8 @@ func TestAll(t *testing.T) { for i := 0; i < 3; i++ { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(types.EpochID(i))) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig, withPublishEpoch(types.EpochID(i))) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) expected = append(expected, atx.ID()) } @@ -75,8 +74,8 @@ func TestHasID(t *testing.T) { for i := 0; i < 3; i++ { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(types.EpochID(i))) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig, withPublishEpoch(types.EpochID(i))) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) atxList = append(atxList, atx) } @@ -101,8 +100,8 @@ func Test_IdentityExists(t *testing.T) { require.NoError(t, err) require.False(t, yes) - atx, blob := newAtx(t, sig) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) yes, err = atxs.IdentityExists(db, sig.NodeID()) require.NoError(t, err) @@ -120,10 +119,10 @@ func TestGetFirstIDByNodeID(t *testing.T) { require.NoError(t, err) // Arrange - atx1, _ := newAtx(t, sig1, withPublishEpoch(1)) - atx2, _ := newAtx(t, sig1, withPublishEpoch(2), withSequence(atx1.Sequence+1)) - atx3, _ := newAtx(t, sig2, withPublishEpoch(3)) - atx4, _ := newAtx(t, sig2, withPublishEpoch(4), withSequence(atx3.Sequence+1)) + atx1 := newAtx(t, sig1, withPublishEpoch(1)) + atx2 := newAtx(t, sig1, withPublishEpoch(2), withSequence(atx1.Sequence+1)) + atx3 := newAtx(t, sig2, withPublishEpoch(3)) + atx4 := newAtx(t, sig2, withPublishEpoch(4), withSequence(atx3.Sequence+1)) for _, atx := range []*types.ActivationTx{atx1, atx2, atx3, atx4} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -152,12 +151,12 @@ func TestLatestN(t *testing.T) { sig3, err := signing.NewEdSigner() require.NoError(t, err) - atx1, _ := newAtx(t, sig1, withPublishEpoch(1), withSequence(0)) - atx2, _ := newAtx(t, sig1, withPublishEpoch(2), withSequence(1)) - atx3, _ := newAtx(t, sig2, withPublishEpoch(3), withSequence(1)) - atx4, _ := newAtx(t, sig2, withPublishEpoch(4), withSequence(2)) - atx5, _ := newAtx(t, sig2, withPublishEpoch(5), withSequence(3)) - atx6, _ := newAtx(t, sig3, withPublishEpoch(1), withSequence(0)) + atx1 := newAtx(t, sig1, withPublishEpoch(1), withSequence(0)) + atx2 := newAtx(t, sig1, withPublishEpoch(2), withSequence(1)) + atx3 := newAtx(t, sig2, withPublishEpoch(3), withSequence(1)) + atx4 := newAtx(t, sig2, withPublishEpoch(4), withSequence(2)) + atx5 := newAtx(t, sig2, withPublishEpoch(5), withSequence(3)) + atx6 := newAtx(t, sig3, withPublishEpoch(1), withSequence(0)) for _, atx := range []*types.ActivationTx{atx1, atx2, atx3, atx4, atx5, atx6} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -242,8 +241,8 @@ func TestGetByEpochAndNodeID(t *testing.T) { sig2, err := signing.NewEdSigner() require.NoError(t, err) - atx1, _ := newAtx(t, sig1, withPublishEpoch(1)) - atx2, _ := newAtx(t, sig2, withPublishEpoch(2)) + atx1 := newAtx(t, sig1, withPublishEpoch(1)) + atx2 := newAtx(t, sig2, withPublishEpoch(2)) for _, atx := range []*types.ActivationTx{atx1, atx2} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -277,10 +276,10 @@ func TestGetLastIDByNodeID(t *testing.T) { require.NoError(t, err) // Arrange - atx1, _ := newAtx(t, sig1, withPublishEpoch(1)) - atx2, _ := newAtx(t, sig1, withPublishEpoch(2), withSequence(atx1.Sequence+1)) - atx3, _ := newAtx(t, sig2, withPublishEpoch(3)) - atx4, _ := newAtx(t, sig2, withPublishEpoch(4), withSequence(atx3.Sequence+1)) + atx1 := newAtx(t, sig1, withPublishEpoch(1)) + atx2 := newAtx(t, sig1, withPublishEpoch(2), withSequence(atx1.Sequence+1)) + atx3 := newAtx(t, sig2, withPublishEpoch(3)) + atx4 := newAtx(t, sig2, withPublishEpoch(4), withSequence(atx3.Sequence+1)) for _, atx := range []*types.ActivationTx{atx1, atx2, atx3, atx4} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -312,10 +311,10 @@ func TestGetIDByEpochAndNodeID(t *testing.T) { e2 := types.EpochID(2) e3 := types.EpochID(3) - atx1, _ := newAtx(t, sig1, withPublishEpoch(e1)) - atx2, _ := newAtx(t, sig1, withPublishEpoch(e2)) - atx3, _ := newAtx(t, sig2, withPublishEpoch(e2)) - atx4, _ := newAtx(t, sig2, withPublishEpoch(e3)) + atx1 := newAtx(t, sig1, withPublishEpoch(e1)) + atx2 := newAtx(t, sig1, withPublishEpoch(e2)) + atx3 := newAtx(t, sig2, withPublishEpoch(e2)) + atx4 := newAtx(t, sig2, withPublishEpoch(e3)) for _, atx := range []*types.ActivationTx{atx1, atx2, atx3, atx4} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -357,10 +356,10 @@ func TestGetIDsByEpoch(t *testing.T) { e2 := types.EpochID(2) e3 := types.EpochID(3) - atx1, _ := newAtx(t, sig1, withPublishEpoch(e1)) - atx2, _ := newAtx(t, sig1, withPublishEpoch(e2)) - atx3, _ := newAtx(t, sig2, withPublishEpoch(e2)) - atx4, _ := newAtx(t, sig2, withPublishEpoch(e3)) + atx1 := newAtx(t, sig1, withPublishEpoch(e1)) + atx2 := newAtx(t, sig1, withPublishEpoch(e2)) + atx3 := newAtx(t, sig2, withPublishEpoch(e2)) + atx4 := newAtx(t, sig2, withPublishEpoch(e3)) for _, atx := range []*types.ActivationTx{atx1, atx2, atx3, atx4} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -393,12 +392,12 @@ func TestGetIDsByEpochCached(t *testing.T) { e2 := types.EpochID(2) e3 := types.EpochID(3) - atx1, _ := newAtx(t, sig1, withPublishEpoch(e1)) - atx2, _ := newAtx(t, sig1, withPublishEpoch(e2)) - atx3, _ := newAtx(t, sig2, withPublishEpoch(e2)) - atx4, _ := newAtx(t, sig2, withPublishEpoch(e3)) - atx5, _ := newAtx(t, sig2, withPublishEpoch(e3)) - atx6, _ := newAtx(t, sig2, withPublishEpoch(e3)) + atx1 := newAtx(t, sig1, withPublishEpoch(e1)) + atx2 := newAtx(t, sig1, withPublishEpoch(e2)) + atx3 := newAtx(t, sig2, withPublishEpoch(e2)) + atx4 := newAtx(t, sig2, withPublishEpoch(e3)) + atx5 := newAtx(t, sig2, withPublishEpoch(e3)) + atx6 := newAtx(t, sig2, withPublishEpoch(e3)) for _, atx := range []*types.ActivationTx{atx1, atx2, atx3, atx4} { require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) @@ -462,8 +461,8 @@ func Test_IterateAtxsWithMalfeasance(t *testing.T) { for i := uint32(0); i < 20; i++ { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(types.EpochID(i/4))) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig, withPublishEpoch(types.EpochID(i/4))) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) malicious := (i % 2) == 0 m[atx.ID()] = malicious if malicious { @@ -492,8 +491,8 @@ func Test_IterateAtxIdsWithMalfeasance(t *testing.T) { for i := uint32(0); i < 20; i++ { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(types.EpochID(i/4))) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig, withPublishEpoch(types.EpochID(i/4))) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) malicious := (i % 2) == 0 m[atx.ID()] = malicious if malicious { @@ -521,11 +520,11 @@ func TestVRFNonce(t *testing.T) { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx1, blob := newAtx(t, sig, withPublishEpoch(20), withNonce(333)) - require.NoError(t, atxs.Add(db, atx1, blob)) + atx1 := newAtx(t, sig, withPublishEpoch(20), withNonce(333)) + require.NoError(t, atxs.Add(db, atx1, types.AtxBlob{})) - atx2, blob := newAtx(t, sig, withPublishEpoch(50), withNonce(777)) - require.NoError(t, atxs.Add(db, atx2, blob)) + atx2 := newAtx(t, sig, withPublishEpoch(50), withNonce(777)) + require.NoError(t, atxs.Add(db, atx2, types.AtxBlob{})) // Act & Assert @@ -554,7 +553,11 @@ func TestLoadBlob(t *testing.T) { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx1, blob := newAtx(t, sig, withPublishEpoch(1)) + atx1 := newAtx(t, sig, withPublishEpoch(1)) + blob := types.AtxBlob{ + Blob: []byte("blob"), + Version: types.AtxV1, + } require.NoError(t, atxs.Add(db, atx1, blob)) var blob1 sql.Blob @@ -569,9 +572,11 @@ func TestLoadBlob(t *testing.T) { require.Equal(t, []int{len(blob1.Bytes)}, blobSizes) var blob2 sql.Blob - atx2, blob := newAtx(t, sig) - blob.Blob = []byte("blob2 of different size") - blob.Version = types.AtxV2 + atx2 := newAtx(t, sig) + blob = types.AtxBlob{ + Blob: []byte("blob2 of different size"), + Version: types.AtxV2, + } require.NoError(t, atxs.Add(db, atx2, blob)) version, err = atxs.LoadBlob(ctx, db, atx2.ID().Bytes(), &blob2) @@ -605,8 +610,11 @@ func TestLoadBlob_DefaultsToV1(t *testing.T) { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig) - blob.Version = 0 + atx := newAtx(t, sig) + blob := types.AtxBlob{ + Blob: []byte("blob"), + Version: 0, + } require.NoError(t, atxs.Add(db, atx, blob)) @@ -623,7 +631,8 @@ func TestGetBlobCached(t *testing.T) { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(1)) + atx := newAtx(t, sig, withPublishEpoch(1)) + blob := types.AtxBlob{Blob: []byte("blob")} require.NoError(t, atxs.Add(db, atx, blob)) require.Equal(t, 2, db.QueryCount()) // insert atx + blob @@ -703,7 +712,8 @@ func TestCachedBlobEviction(t *testing.T) { blobs := make([][]byte, 11) var b sql.Blob for n := range addedATXs { - atx, blob := newAtx(t, sig, withPublishEpoch(1)) + atx := newAtx(t, sig, withPublishEpoch(1)) + blob := types.AtxBlob{Blob: []byte(fmt.Sprintf("blob %d", n))} require.NoError(t, atxs.Add(db, atx, blob)) addedATXs[n] = atx blobs[n] = blob.Blob @@ -736,7 +746,7 @@ func TestCheckpointATX(t *testing.T) { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, _ := newAtx(t, sig, withPublishEpoch(3), withSequence(4)) + atx := newAtx(t, sig, withPublishEpoch(3), withSequence(4)) catx := &atxs.CheckpointAtx{ ID: atx.ID(), Epoch: atx.PublishEpoch, @@ -786,10 +796,10 @@ func TestAdd(t *testing.T) { sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withPublishEpoch(1)) + atx := newAtx(t, sig, withPublishEpoch(1)) - require.NoError(t, atxs.Add(db, atx, blob)) - require.ErrorIs(t, atxs.Add(db, atx, blob), sql.ErrObjectExists) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) + require.ErrorIs(t, atxs.Add(db, atx, types.AtxBlob{}), sql.ErrObjectExists) got, err := atxs.Get(db, atx.ID()) require.NoError(t, err) @@ -822,24 +832,19 @@ func withCoinbase(addr types.Address) createAtxOpt { } } -func newAtx(t testing.TB, signer *signing.EdSigner, opts ...createAtxOpt) (*types.ActivationTx, types.AtxBlob) { - nonce := uint64(123) - watx := &wire.ActivationTxV1{ - InnerActivationTxV1: wire.InnerActivationTxV1{ - NIPostChallengeV1: wire.NIPostChallengeV1{ - PrevATXID: types.RandomATXID(), - }, - NumUnits: 2, - VRFNonce: &nonce, - }, +func newAtx(t testing.TB, signer *signing.EdSigner, opts ...createAtxOpt) *types.ActivationTx { + atx := &types.ActivationTx{ + NumUnits: 2, + TickCount: 1, + VRFNonce: types.VRFPostIndex(123), + SmesherID: signer.NodeID(), } - watx.Sign(signer) - - atx := fixture.ToAtx(t, watx) + atx.SetID(types.RandomATXID()) + atx.SetReceived(time.Now().Local()) for _, opt := range opts { opt(atx) } - return atx, watx.Blob() + return atx } type header struct { @@ -1031,12 +1036,12 @@ func Test_PrevATXCollisions(t *testing.T) { // create two ATXs with the same PrevATXID prevATXID := types.RandomATXID() - atx1, blob1 := newAtx(t, sig, withPublishEpoch(1)) - atx2, blob2 := newAtx(t, sig, withPublishEpoch(2)) + atx1 := newAtx(t, sig, withPublishEpoch(1)) + atx2 := newAtx(t, sig, withPublishEpoch(2)) - require.NoError(t, atxs.Add(db, atx1, blob1)) + require.NoError(t, atxs.Add(db, atx1, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx1.ID(), prevATXID, 0, sig.NodeID(), 10)) - require.NoError(t, atxs.Add(db, atx2, blob2)) + require.NoError(t, atxs.Add(db, atx2, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx2.ID(), prevATXID, 0, sig.NodeID(), 10)) // verify that the ATXs were added @@ -1053,13 +1058,11 @@ func Test_PrevATXCollisions(t *testing.T) { otherSig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, otherSig, withPublishEpoch(types.EpochID(i))) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, otherSig, withPublishEpoch(types.EpochID(i))) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) - atx2, blob2 := newAtx(t, otherSig, - withPublishEpoch(types.EpochID(i+1)), - ) - require.NoError(t, atxs.Add(db, atx2, blob2)) + atx2 := newAtx(t, otherSig, withPublishEpoch(types.EpochID(i+1))) + require.NoError(t, atxs.Add(db, atx2, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx2.ID(), atx.ID(), 0, sig.NodeID(), 10)) } @@ -1086,8 +1089,8 @@ func TestCoinbase(t *testing.T) { db := sql.InMemory() sig, err := signing.NewEdSigner() require.NoError(t, err) - atx, blob := newAtx(t, sig, withCoinbase(types.Address{1, 2, 3})) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig, withCoinbase(types.Address{1, 2, 3})) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) cb, err := atxs.Coinbase(db, sig.NodeID()) require.NoError(t, err) require.Equal(t, atx.Coinbase, cb) @@ -1097,10 +1100,10 @@ func TestCoinbase(t *testing.T) { db := sql.InMemory() sig, err := signing.NewEdSigner() require.NoError(t, err) - atx1, blob1 := newAtx(t, sig, withPublishEpoch(1), withCoinbase(types.Address{1, 2, 3})) - atx2, blob2 := newAtx(t, sig, withPublishEpoch(2), withCoinbase(types.Address{4, 5, 6})) - require.NoError(t, atxs.Add(db, atx1, blob1)) - require.NoError(t, atxs.Add(db, atx2, blob2)) + atx1 := newAtx(t, sig, withPublishEpoch(1), withCoinbase(types.Address{1, 2, 3})) + atx2 := newAtx(t, sig, withPublishEpoch(2), withCoinbase(types.Address{4, 5, 6})) + require.NoError(t, atxs.Add(db, atx1, types.AtxBlob{})) + require.NoError(t, atxs.Add(db, atx2, types.AtxBlob{})) cb, err := atxs.Coinbase(db, sig.NodeID()) require.NoError(t, err) require.Equal(t, atx2.Coinbase, cb) @@ -1162,8 +1165,8 @@ func Test_AtxWithPrevious(t *testing.T) { db := sql.InMemory() prev := types.RandomATXID() - atx, blob := newAtx(t, sig) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx.ID(), prev, 0, sig.NodeID(), 10)) id, err := atxs.AtxWithPrevious(db, prev, sig.NodeID()) @@ -1173,8 +1176,8 @@ func Test_AtxWithPrevious(t *testing.T) { t.Run("finds other ATX with same previous (empty)", func(t *testing.T) { db := sql.InMemory() - atx, blob := newAtx(t, sig) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx.ID(), types.EmptyATXID, 0, sig.NodeID(), 10)) id, err := atxs.AtxWithPrevious(db, types.EmptyATXID, sig.NodeID()) @@ -1188,12 +1191,12 @@ func Test_AtxWithPrevious(t *testing.T) { require.NoError(t, err) prev := types.RandomATXID() - atx, blob := newAtx(t, sig) - require.NoError(t, atxs.Add(db, atx, blob)) + atx := newAtx(t, sig) + require.NoError(t, atxs.Add(db, atx, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx.ID(), prev, 0, sig.NodeID(), 10)) - atx2, blob := newAtx(t, sig2) - require.NoError(t, atxs.Add(db, atx2, blob)) + atx2 := newAtx(t, sig2) + require.NoError(t, atxs.Add(db, atx2, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx2.ID(), prev, 0, sig2.NodeID(), 10)) id, err := atxs.AtxWithPrevious(db, prev, sig.NodeID()) @@ -1222,16 +1225,16 @@ func Test_FindDoublePublish(t *testing.T) { db := sql.InMemory() // one atx - atx0, blob := newAtx(t, sig, withPublishEpoch(1)) - require.NoError(t, atxs.Add(db, atx0, blob)) + atx0 := newAtx(t, sig, withPublishEpoch(1)) + require.NoError(t, atxs.Add(db, atx0, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx0.ID(), types.EmptyATXID, 0, atx0.SmesherID, 10)) _, err = atxs.FindDoublePublish(db, atx0.SmesherID, atx0.PublishEpoch) require.ErrorIs(t, err, sql.ErrNotFound) // two atxs in different epochs - atx1, blob := newAtx(t, sig, withPublishEpoch(atx0.PublishEpoch+1)) - require.NoError(t, atxs.Add(db, atx1, blob)) + atx1 := newAtx(t, sig, withPublishEpoch(atx0.PublishEpoch+1)) + require.NoError(t, atxs.Add(db, atx1, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx1.ID(), types.EmptyATXID, 0, atx0.SmesherID, 10)) _, err = atxs.FindDoublePublish(db, atx0.SmesherID, atx0.PublishEpoch) @@ -1241,17 +1244,17 @@ func Test_FindDoublePublish(t *testing.T) { t.Parallel() db := sql.InMemory() - atx0, blob := newAtx(t, sig) - require.NoError(t, atxs.Add(db, atx0, blob)) + atx0 := newAtx(t, sig) + require.NoError(t, atxs.Add(db, atx0, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx0.ID(), types.EmptyATXID, 0, atx0.SmesherID, 10)) - atx1, blob := newAtx(t, sig) - require.NoError(t, atxs.Add(db, atx1, blob)) + atx1 := newAtx(t, sig) + require.NoError(t, atxs.Add(db, atx1, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx1.ID(), types.EmptyATXID, 0, atx0.SmesherID, 10)) - atxids, err := atxs.FindDoublePublish(db, atx0.SmesherID, atx0.PublishEpoch) + atxIDs, err := atxs.FindDoublePublish(db, atx0.SmesherID, atx0.PublishEpoch) require.NoError(t, err) - require.ElementsMatch(t, []types.ATXID{atx0.ID(), atx1.ID()}, atxids) + require.ElementsMatch(t, []types.ATXID{atx0.ID(), atx1.ID()}, atxIDs) // filters by epoch _, err = atxs.FindDoublePublish(db, atx0.SmesherID, atx0.PublishEpoch+1) @@ -1264,16 +1267,16 @@ func Test_FindDoublePublish(t *testing.T) { atx0Signer, err := signing.NewEdSigner() require.NoError(t, err) - atx0, blob := newAtx(t, atx0Signer) - require.NoError(t, atxs.Add(db, atx0, blob)) + atx0 := newAtx(t, atx0Signer) + require.NoError(t, atxs.Add(db, atx0, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx0.ID(), types.EmptyATXID, 0, atx0.SmesherID, 10)) require.NoError(t, atxs.SetPost(db, atx0.ID(), types.EmptyATXID, 0, sig.NodeID(), 10)) atx1Signer, err := signing.NewEdSigner() require.NoError(t, err) - atx1, blob := newAtx(t, atx1Signer) - require.NoError(t, atxs.Add(db, atx1, blob)) + atx1 := newAtx(t, atx1Signer) + require.NoError(t, atxs.Add(db, atx1, types.AtxBlob{})) require.NoError(t, atxs.SetPost(db, atx1.ID(), types.EmptyATXID, 0, atx1.SmesherID, 10)) require.NoError(t, atxs.SetPost(db, atx1.ID(), types.EmptyATXID, 0, sig.NodeID(), 10))