2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package ecclient_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/vivint/infectious"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/pb"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/pkg/signing"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-11-14 19:46:15 +00:00
|
|
|
"storj.io/storj/private/memory"
|
|
|
|
"storj.io/storj/private/testcontext"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/private/testrand"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/storagenode"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/uplink/ecclient"
|
|
|
|
"storj.io/storj/uplink/eestream"
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dataSize = 32 * memory.KiB
|
|
|
|
storageNodes = 4
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestECClient(t *testing.T) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
planet, err := testplanet.New(t, 1, storageNodes, 1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer ctx.Check(planet.Shutdown)
|
|
|
|
|
|
|
|
planet.Start(ctx)
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
ec := ecclient.NewClient(planet.Uplinks[0].Log.Named("ecclient"), planet.Uplinks[0].Dialer, 0)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
k := storageNodes / 2
|
|
|
|
n := storageNodes
|
|
|
|
fc, err := infectious.NewFEC(k, n)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
es := eestream.NewRSScheme(fc, dataSize.Int()/n)
|
|
|
|
rs, err := eestream.NewRedundancyStrategy(es, 0, 0)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-26 11:38:51 +01:00
|
|
|
data, err := ioutil.ReadAll(io.LimitReader(testrand.Reader(), dataSize.Int64()))
|
2019-03-18 10:55:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Erasure encode some random data and upload the pieces
|
|
|
|
successfulNodes, successfulHashes := testPut(ctx, t, planet, ec, rs, data)
|
|
|
|
|
|
|
|
// Download the pieces and erasure decode the data
|
|
|
|
testGet(ctx, t, planet, ec, es, data, successfulNodes, successfulHashes)
|
|
|
|
|
|
|
|
// Delete the pieces
|
|
|
|
testDelete(ctx, t, planet, ec, successfulNodes, successfulHashes)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testPut(ctx context.Context, t *testing.T, planet *testplanet.Planet, ec ecclient.Client, rs eestream.RedundancyStrategy, data []byte) ([]*pb.Node, []*pb.PieceHash) {
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
limits := make([]*pb.AddressedOrderLimit, rs.TotalCount())
|
|
|
|
for i := 0; i < len(limits); i++ {
|
2019-07-11 21:51:40 +01:00
|
|
|
limits[i], err = newAddressedOrderLimit(ctx, pb.PieceAction_PUT, planet.Satellites[0], piecePublicKey, planet.StorageNodes[i], storj.NewPieceID())
|
2019-03-18 10:55:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ttl := time.Now()
|
|
|
|
|
|
|
|
r := bytes.NewReader(data)
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
successfulNodes, successfulHashes, err := ec.Put(ctx, limits, piecePrivateKey, rs, r, ttl)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, len(limits), len(successfulNodes))
|
|
|
|
|
|
|
|
slowNodes := 0
|
|
|
|
for i := range limits {
|
|
|
|
if successfulNodes[i] == nil && limits[i] != nil {
|
|
|
|
slowNodes++
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, limits[i].GetLimit().StorageNodeId, successfulNodes[i].Id)
|
|
|
|
if successfulNodes[i] != nil {
|
|
|
|
assert.NotNil(t, successfulHashes[i])
|
|
|
|
assert.Equal(t, limits[i].GetLimit().PieceId, successfulHashes[i].PieceId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if slowNodes > rs.TotalCount()-rs.RequiredCount() {
|
|
|
|
assert.Fail(t, fmt.Sprintf("Too many slow nodes: \n"+
|
|
|
|
"expected: <= %d\n"+
|
|
|
|
"actual : %d", rs.TotalCount()-rs.RequiredCount(), slowNodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
return successfulNodes, successfulHashes
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGet(ctx context.Context, t *testing.T, planet *testplanet.Planet, ec ecclient.Client, es eestream.ErasureScheme, data []byte, successfulNodes []*pb.Node, successfulHashes []*pb.PieceHash) {
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
limits := make([]*pb.AddressedOrderLimit, es.TotalCount())
|
|
|
|
for i := 0; i < len(limits); i++ {
|
|
|
|
if successfulNodes[i] != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
limits[i], err = newAddressedOrderLimit(ctx, pb.PieceAction_GET, planet.Satellites[0], piecePublicKey, planet.StorageNodes[i], successfulHashes[i].PieceId)
|
2019-03-18 10:55:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
rr, err := ec.Get(ctx, limits, piecePrivateKey, es, dataSize.Int64())
|
2019-03-18 10:55:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r, err := rr.Range(ctx, 0, rr.Size())
|
|
|
|
require.NoError(t, err)
|
|
|
|
readData, err := ioutil.ReadAll(r)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, data, readData)
|
|
|
|
assert.NoError(t, r.Close())
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDelete(ctx context.Context, t *testing.T, planet *testplanet.Planet, ec ecclient.Client, successfulNodes []*pb.Node, successfulHashes []*pb.PieceHash) {
|
2019-07-11 21:51:40 +01:00
|
|
|
piecePublicKey, piecePrivateKey, err := storj.NewPieceKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
limits := make([]*pb.AddressedOrderLimit, len(successfulNodes))
|
|
|
|
for i := 0; i < len(limits); i++ {
|
|
|
|
if successfulNodes[i] != nil {
|
2019-07-11 21:51:40 +01:00
|
|
|
limits[i], err = newAddressedOrderLimit(ctx, pb.PieceAction_DELETE, planet.Satellites[0], piecePublicKey, planet.StorageNodes[i], successfulHashes[i].PieceId)
|
2019-03-18 10:55:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 21:51:40 +01:00
|
|
|
err = ec.Delete(ctx, limits, piecePrivateKey)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2019-09-17 21:14:49 +01:00
|
|
|
func newAddressedOrderLimit(ctx context.Context, action pb.PieceAction, satellite *testplanet.SatelliteSystem, piecePublicKey storj.PiecePublicKey, storageNode *storagenode.Peer, pieceID storj.PieceID) (*pb.AddressedOrderLimit, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
// TODO refactor to avoid OrderLimit duplication
|
2019-06-26 11:38:51 +01:00
|
|
|
serialNumber := testrand.SerialNumber()
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
now := time.Now()
|
2019-07-02 17:06:12 +01:00
|
|
|
|
2019-07-01 16:54:11 +01:00
|
|
|
limit := &pb.OrderLimit{
|
2019-06-26 11:38:51 +01:00
|
|
|
SerialNumber: serialNumber,
|
2019-03-18 10:55:06 +00:00
|
|
|
SatelliteId: satellite.ID(),
|
2019-07-11 21:51:40 +01:00
|
|
|
UplinkPublicKey: piecePublicKey,
|
2019-03-18 10:55:06 +00:00
|
|
|
StorageNodeId: storageNode.ID(),
|
|
|
|
PieceId: pieceID,
|
|
|
|
Action: action,
|
|
|
|
Limit: dataSize.Int64(),
|
2019-07-09 22:54:00 +01:00
|
|
|
PieceExpiration: time.Time{},
|
|
|
|
OrderCreation: now,
|
|
|
|
OrderExpiration: now.Add(24 * time.Hour),
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 22:54:00 +01:00
|
|
|
limit, err := signing.SignOrderLimit(ctx, signing.SignerFromFullIdentity(satellite.Identity), limit)
|
2019-03-18 10:55:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.AddressedOrderLimit{
|
|
|
|
StorageNodeAddress: storageNode.Local().Address,
|
|
|
|
Limit: limit,
|
|
|
|
}, nil
|
|
|
|
}
|