2018-11-08 16:18:28 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-11-14 01:22:18 +00:00
|
|
|
package tally
|
2018-11-08 16:18:28 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-05 14:03:23 +00:00
|
|
|
"crypto/ecdsa"
|
2018-11-08 16:18:28 +00:00
|
|
|
"math/rand"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"go.uber.org/zap"
|
2018-12-07 09:59:31 +00:00
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
"storj.io/storj/internal/identity"
|
2018-12-07 11:55:25 +00:00
|
|
|
"storj.io/storj/internal/testcontext"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/internal/teststorj"
|
2018-11-26 21:49:55 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement"
|
2018-12-05 14:03:23 +00:00
|
|
|
"storj.io/storj/pkg/bwagreement/test"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/overlay"
|
|
|
|
"storj.io/storj/pkg/overlay/mocks"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
"storj.io/storj/pkg/pointerdb"
|
2018-11-29 18:39:27 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-12-07 09:59:31 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb"
|
2018-11-08 16:18:28 +00:00
|
|
|
"storj.io/storj/storage/teststore"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestIdentifyActiveNodes(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
func TestOnlineNodes(t *testing.T) {
|
2018-12-07 11:55:25 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
logger := zap.NewNop()
|
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, logger, pointerdb.Config{}, nil)
|
|
|
|
|
|
|
|
const N = 50
|
|
|
|
nodes := []*pb.Node{}
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs := storj.NodeIDList{}
|
2018-11-08 16:18:28 +00:00
|
|
|
expectedOnline := []*pb.Node{}
|
|
|
|
for i := 0; i < N; i++ {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeID := teststorj.NodeIDFromString(strconv.Itoa(i))
|
|
|
|
n := &pb.Node{Id: nodeID, Type: pb.NodeType_STORAGE, Address: &pb.NodeAddress{Address: ""}}
|
2018-11-08 16:18:28 +00:00
|
|
|
nodes = append(nodes, n)
|
|
|
|
if i%(rand.Intn(5)+2) == 0 {
|
2018-11-29 18:39:27 +00:00
|
|
|
id := teststorj.NodeIDFromString("id" + nodeID.String())
|
2018-11-08 16:18:28 +00:00
|
|
|
nodeIDs = append(nodeIDs, id)
|
|
|
|
} else {
|
2018-11-29 18:39:27 +00:00
|
|
|
nodeIDs = append(nodeIDs, nodeID)
|
2018-11-08 16:18:28 +00:00
|
|
|
expectedOnline = append(expectedOnline, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
overlayServer := mocks.NewOverlay(nodes)
|
|
|
|
kad := &kademlia.Kademlia{}
|
|
|
|
limit := 0
|
|
|
|
interval := time.Second
|
|
|
|
|
2018-11-26 21:49:55 +00:00
|
|
|
accountingDb, err := accounting.NewDb("sqlite3://file::memory:?mode=memory&cache=shared")
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 11:55:25 +00:00
|
|
|
defer ctx.Check(accountingDb.Close)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2018-12-07 09:59:31 +00:00
|
|
|
masterDB, err := satellitedb.NewDB("sqlite3://file::memory:?mode=memory&cache=shared")
|
2018-11-26 21:49:55 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 11:55:25 +00:00
|
|
|
defer ctx.Check(masterDB.Close)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2018-12-07 09:59:31 +00:00
|
|
|
tally := newTally(logger, accountingDb, masterDB.BandwidthAgreement(), pointerdb, overlayServer, kad, limit, interval)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
2018-11-08 16:18:28 +00:00
|
|
|
online, err := tally.onlineNodes(ctx, nodeIDs)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedOnline, online)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTallyAtRestStorage(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNeedToContact(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateGranularTable(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
2018-12-05 14:03:23 +00:00
|
|
|
|
|
|
|
func TestQueryNoAgreements(t *testing.T) {
|
2018-12-07 11:55:25 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
//get stuff we need
|
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, zap.NewNop(), pointerdb.Config{}, nil)
|
|
|
|
overlayServer := mocks.NewOverlay([]*pb.Node{})
|
|
|
|
kad := &kademlia.Kademlia{}
|
|
|
|
accountingDb, err := accounting.NewDb("sqlite3://file::memory:?mode=memory&cache=shared")
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 11:55:25 +00:00
|
|
|
defer ctx.Check(accountingDb.Close)
|
2018-12-07 09:59:31 +00:00
|
|
|
|
|
|
|
masterDB, err := satellitedb.NewDB("sqlite3://file::memory:?mode=memory&cache=shared")
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 11:55:25 +00:00
|
|
|
defer ctx.Check(masterDB.Close)
|
2018-12-07 09:59:31 +00:00
|
|
|
|
|
|
|
tally := newTally(zap.NewNop(), accountingDb, masterDB.BandwidthAgreement(), pointerdb, overlayServer, kad, 0, time.Second)
|
2018-12-05 14:03:23 +00:00
|
|
|
|
|
|
|
//check the db
|
|
|
|
err = tally.Query(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQueryWithBw(t *testing.T) {
|
2018-12-07 11:55:25 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2018-12-05 14:03:23 +00:00
|
|
|
//get stuff we need
|
|
|
|
pointerdb := pointerdb.NewServer(teststore.New(), &overlay.Cache{}, zap.NewNop(), pointerdb.Config{}, nil)
|
|
|
|
overlayServer := mocks.NewOverlay([]*pb.Node{})
|
|
|
|
kad := &kademlia.Kademlia{}
|
|
|
|
accountingDb, err := accounting.NewDb("sqlite3://file::memory:?mode=memory&cache=shared")
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 11:55:25 +00:00
|
|
|
defer ctx.Check(accountingDb.Close)
|
2018-12-07 09:59:31 +00:00
|
|
|
|
|
|
|
masterDB, err := satellitedb.NewDB("sqlite3://file::memory:?mode=memory&cache=shared")
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
2018-12-07 09:59:31 +00:00
|
|
|
err = masterDB.CreateTables()
|
|
|
|
assert.NoError(t, err)
|
2018-12-07 11:55:25 +00:00
|
|
|
defer ctx.Check(masterDB.Close)
|
2018-12-07 09:59:31 +00:00
|
|
|
|
|
|
|
bwDb := masterDB.BandwidthAgreement()
|
2018-12-05 14:03:23 +00:00
|
|
|
tally := newTally(zap.NewNop(), accountingDb, bwDb, pointerdb, overlayServer, kad, 0, time.Second)
|
|
|
|
|
|
|
|
//get a private key
|
|
|
|
fiC, err := testidentity.NewTestIdentity()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
k, ok := fiC.Key.(*ecdsa.PrivateKey)
|
|
|
|
assert.True(t, ok)
|
|
|
|
//generate an agreement with the key
|
|
|
|
pba, err := test.GeneratePayerBandwidthAllocation(pb.PayerBandwidthAllocation_GET, k)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rba, err := test.GenerateRenterBandwidthAllocation(pba, k)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
//save to db
|
2018-12-07 09:59:31 +00:00
|
|
|
err = bwDb.CreateAgreement(ctx, bwagreement.Agreement{Signature: rba.GetSignature(), Agreement: rba.GetData()})
|
2018-12-05 14:03:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
//check the db
|
|
|
|
err = tally.Query(ctx)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|