2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package orders_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-07-02 17:06:12 +01:00
|
|
|
"time"
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-06-25 10:46:29 +01:00
|
|
|
"storj.io/storj/internal/testidentity"
|
2019-06-26 11:38:51 +01:00
|
|
|
"storj.io/storj/internal/testrand"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/auth/signing"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/storj"
|
|
|
|
"storj.io/storj/storagenode"
|
|
|
|
"storj.io/storj/storagenode/orders"
|
|
|
|
"storj.io/storj/storagenode/storagenodedb/storagenodedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestOrders(t *testing.T) {
|
|
|
|
storagenodedbtest.Run(t, func(t *testing.T, db storagenode.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
ordersdb := db.Orders()
|
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
storagenode := testidentity.MustPregeneratedSignedIdentity(0, storj.LatestIDVersion())
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-04-08 19:15:19 +01:00
|
|
|
satellite0 := testidentity.MustPregeneratedSignedIdentity(1, storj.LatestIDVersion())
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
piece := storj.NewPieceID()
|
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
serialNumber := testrand.SerialNumber()
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// basic test
|
2019-03-21 13:24:26 +00:00
|
|
|
emptyUnsent, err := ordersdb.ListUnsent(ctx, 100)
|
2019-03-18 10:55:06 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-21 13:24:26 +00:00
|
|
|
require.Len(t, emptyUnsent, 0)
|
|
|
|
|
|
|
|
emptyArchive, err := ordersdb.ListArchived(ctx, 100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, emptyArchive, 0)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-07-02 17:06:12 +01:00
|
|
|
now := time.Now()
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
limit, err := signing.SignOrderLimit(ctx, signing.SignerFromFullIdentity(satellite0), &pb.OrderLimit{
|
2019-03-18 10:55:06 +00:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
SatelliteId: satellite0.ID,
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-03-18 10:55:06 +00:00
|
|
|
StorageNodeId: storagenode.ID,
|
|
|
|
PieceId: piece,
|
|
|
|
Limit: 100,
|
|
|
|
Action: pb.PieceAction_GET,
|
2019-07-02 17:06:12 +01:00
|
|
|
OrderCreation: now.AddDate(0, 0, -1),
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceExpiration: now,
|
|
|
|
OrderExpiration: now,
|
2019-03-18 10:55:06 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
order, err := signing.SignUplinkOrder(ctx, piecePrivateKey, &pb.Order{
|
2019-03-18 10:55:06 +00:00
|
|
|
SerialNumber: serialNumber,
|
|
|
|
Amount: 50,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-19 18:30:27 +00:00
|
|
|
info := &orders.Info{
|
2019-07-09 22:33:45 +01:00
|
|
|
Limit: limit,
|
|
|
|
Order: order,
|
2019-03-19 18:30:27 +00:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
// basic add
|
|
|
|
err = ordersdb.Enqueue(ctx, info)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// duplicate add
|
|
|
|
err = ordersdb.Enqueue(ctx, info)
|
|
|
|
require.Error(t, err, "duplicate add")
|
|
|
|
|
|
|
|
unsent, err := ordersdb.ListUnsent(ctx, 100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, cmp.Diff([]*orders.Info{info}, unsent, cmp.Comparer(pb.Equal)))
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
// list by group
|
|
|
|
unsentGrouped, err := ordersdb.ListUnsentBySatellite(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedGrouped := map[storj.NodeID][]*orders.Info{
|
2019-06-27 19:52:50 +01:00
|
|
|
satellite0.ID: {
|
2019-03-21 13:24:26 +00:00
|
|
|
{Limit: limit, Order: order},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Empty(t, cmp.Diff(expectedGrouped, unsentGrouped, cmp.Comparer(pb.Equal)))
|
|
|
|
|
|
|
|
// test archival
|
|
|
|
err = ordersdb.Archive(ctx, satellite0.ID, serialNumber, orders.StatusAccepted)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// duplicate archive
|
|
|
|
err = ordersdb.Archive(ctx, satellite0.ID, serialNumber, orders.StatusRejected)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// shouldn't be in unsent list
|
|
|
|
unsent, err = ordersdb.ListUnsent(ctx, 100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, unsent, 0)
|
|
|
|
|
|
|
|
// it should now be in the archive
|
|
|
|
archived, err := ordersdb.ListArchived(ctx, 100)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, archived, 1)
|
|
|
|
|
|
|
|
require.Empty(t, cmp.Diff([]*orders.ArchivedInfo{
|
|
|
|
{
|
2019-07-09 22:33:45 +01:00
|
|
|
Limit: limit,
|
|
|
|
Order: order,
|
2019-03-21 13:24:26 +00:00
|
|
|
|
|
|
|
Status: orders.StatusAccepted,
|
|
|
|
ArchivedAt: archived[0].ArchivedAt,
|
|
|
|
},
|
|
|
|
}, archived, cmp.Comparer(pb.Equal)))
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
})
|
|
|
|
}
|