2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-05 15:23:54 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bwagreement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-07 19:22:49 +00:00
|
|
|
"crypto"
|
2019-03-19 12:56:59 +00:00
|
|
|
"io"
|
2018-12-07 09:59:31 +00:00
|
|
|
"time"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2019-02-07 19:22:49 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
2019-01-23 19:58:44 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-05 15:23:54 +00:00
|
|
|
"go.uber.org/zap"
|
2019-03-19 12:56:59 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2019-01-23 19:58:44 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2019-04-23 12:13:57 +01:00
|
|
|
"storj.io/storj/internal/dbutil/pgutil"
|
|
|
|
"storj.io/storj/internal/dbutil/sqliteutil"
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/auth"
|
2019-02-07 19:22:49 +00:00
|
|
|
"storj.io/storj/pkg/certdb"
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-02-07 20:39:20 +00:00
|
|
|
"storj.io/storj/pkg/pkcrypto"
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-05 15:23:54 +00:00
|
|
|
)
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
var (
|
|
|
|
// Error the default bwagreement errs class
|
|
|
|
Error = errs.Class("bwagreement error")
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is a configuration struct that is everything you need to start an
|
|
|
|
// agreement receiver responsibility
|
|
|
|
type Config struct {
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:57:21 +00:00
|
|
|
//UplinkStat contains information about an uplink's returned Orders
|
2019-02-01 18:50:12 +00:00
|
|
|
type UplinkStat struct {
|
|
|
|
NodeID storj.NodeID
|
|
|
|
TotalBytes int64
|
|
|
|
PutActionCount int
|
|
|
|
GetActionCount int
|
|
|
|
TotalTransactions int
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:57:21 +00:00
|
|
|
//SavedOrder is information from an Order pertaining to accounting
|
|
|
|
type SavedOrder struct {
|
|
|
|
Serialnum string
|
|
|
|
StorageNodeID storj.NodeID
|
|
|
|
UplinkID storj.NodeID
|
|
|
|
Action int64
|
|
|
|
Total int64
|
|
|
|
CreatedAt time.Time
|
|
|
|
ExpiresAt time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// DB stores orders for accounting purposes
|
2018-12-07 09:59:31 +00:00
|
|
|
type DB interface {
|
2019-03-12 20:57:21 +00:00
|
|
|
// SaveOrder saves an order for accounting
|
2019-03-14 21:12:47 +00:00
|
|
|
SaveOrder(context.Context, *pb.Order) error
|
2019-02-01 18:50:12 +00:00
|
|
|
// GetTotalsSince returns the sum of each bandwidth type after (exluding) a given date range
|
|
|
|
GetTotals(context.Context, time.Time, time.Time) (map[storj.NodeID][]int64, error)
|
|
|
|
//GetTotals returns stats about an uplink
|
|
|
|
GetUplinkStats(context.Context, time.Time, time.Time) ([]UplinkStat, error)
|
2019-03-12 20:57:21 +00:00
|
|
|
//GetExpired gets orders that are expired and were created before some time
|
2019-03-14 21:12:47 +00:00
|
|
|
GetExpired(context.Context, time.Time, time.Time) ([]SavedOrder, error)
|
2019-03-12 20:57:21 +00:00
|
|
|
//DeleteExpired deletes orders that are expired and were created before some time
|
2019-03-14 21:12:47 +00:00
|
|
|
DeleteExpired(context.Context, time.Time, time.Time) error
|
2018-12-07 09:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// Server is an implementation of the pb.BandwidthServer interface
|
|
|
|
type Server struct {
|
2019-02-07 19:22:49 +00:00
|
|
|
bwdb DB
|
|
|
|
certdb certdb.DB
|
|
|
|
pkey crypto.PublicKey
|
2019-01-25 18:05:21 +00:00
|
|
|
NodeID storj.NodeID
|
2019-03-19 12:56:59 +00:00
|
|
|
log *zap.Logger
|
2018-11-12 21:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
2019-03-19 12:56:59 +00:00
|
|
|
func NewServer(db DB, upldb certdb.DB, pkey crypto.PublicKey, log *zap.Logger, nodeID storj.NodeID) *Server {
|
|
|
|
// TODO: reorder arguments
|
|
|
|
return &Server{bwdb: db, certdb: upldb, pkey: pkey, log: log, NodeID: nodeID}
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// Close closes resources
|
|
|
|
func (s *Server) Close() error { return nil }
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
// BandwidthAgreements receives and stores bandwidth agreements from storage nodes
|
2019-02-22 21:17:35 +00:00
|
|
|
func (s *Server) BandwidthAgreements(ctx context.Context, rba *pb.Order) (reply *pb.AgreementsSummary, err error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-19 12:56:59 +00:00
|
|
|
s.log.Debug("Received Agreement...")
|
2018-11-15 19:06:09 +00:00
|
|
|
reply = &pb.AgreementsSummary{
|
2019-01-11 18:15:49 +00:00
|
|
|
Status: pb.AgreementsSummary_REJECTED,
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
pba := rba.PayerAllocation
|
2019-01-25 18:05:21 +00:00
|
|
|
//verify message content
|
|
|
|
pi, err := identity.PeerIdentityFromContext(ctx)
|
2019-01-28 19:45:25 +00:00
|
|
|
if err != nil || rba.StorageNodeId != pi.ID {
|
2019-02-01 18:50:12 +00:00
|
|
|
return reply, auth.ErrBadID.New("Storage Node ID: %v vs %v", rba.StorageNodeId, pi.ID)
|
2019-01-09 15:02:03 +00:00
|
|
|
}
|
2019-01-25 18:05:21 +00:00
|
|
|
//todo: use whitelist for uplinks?
|
2019-01-28 19:45:25 +00:00
|
|
|
if pba.SatelliteId != s.NodeID {
|
2019-02-01 18:50:12 +00:00
|
|
|
return reply, pb.ErrPayer.New("Satellite ID: %v vs %v", pba.SatelliteId, s.NodeID)
|
2019-01-11 03:31:47 +00:00
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
exp := time.Unix(pba.GetExpirationUnixSec(), 0).UTC()
|
2019-01-10 18:30:55 +00:00
|
|
|
if exp.Before(time.Now().UTC()) {
|
2019-01-28 19:45:25 +00:00
|
|
|
return reply, pb.ErrPayer.Wrap(auth.ErrExpired.New("%v vs %v", exp, time.Now().UTC()))
|
2019-01-10 18:30:55 +00:00
|
|
|
}
|
2019-02-07 19:22:49 +00:00
|
|
|
|
|
|
|
if err = s.verifySignature(ctx, rba); err != nil {
|
|
|
|
return reply, err
|
2019-01-25 18:05:21 +00:00
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
//save and return rersults
|
2019-03-14 21:12:47 +00:00
|
|
|
if err = s.bwdb.SaveOrder(ctx, rba); err != nil {
|
2019-04-23 12:13:57 +01:00
|
|
|
if pgutil.IsConstraintError(err) || sqliteutil.IsConstraintError(err) {
|
2019-02-01 18:50:12 +00:00
|
|
|
return reply, pb.ErrPayer.Wrap(auth.ErrSerial.Wrap(err))
|
|
|
|
}
|
|
|
|
reply.Status = pb.AgreementsSummary_FAIL
|
|
|
|
return reply, pb.ErrPayer.Wrap(err)
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2018-11-15 19:06:09 +00:00
|
|
|
reply.Status = pb.AgreementsSummary_OK
|
2019-03-19 12:56:59 +00:00
|
|
|
s.log.Debug("Stored Agreement...")
|
2018-11-15 19:06:09 +00:00
|
|
|
return reply, nil
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2019-02-07 19:22:49 +00:00
|
|
|
|
2019-03-19 12:56:59 +00:00
|
|
|
// Settlement receives and handles agreements.
|
|
|
|
func (s *Server) Settlement(client pb.Bandwidth_SettlementServer) (err error) {
|
|
|
|
ctx := client.Context()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
peer, err := identity.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return status.Error(codes.Unauthenticated, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
formatError := func(err error) error {
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return status.Error(codes.Unknown, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Debug("Settlement", zap.Any("storage node ID", peer.ID))
|
|
|
|
for {
|
|
|
|
request, err := client.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return formatError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if request == nil || request.Allocation == nil {
|
|
|
|
return status.Error(codes.InvalidArgument, "allocation missing")
|
|
|
|
}
|
|
|
|
allocation := request.Allocation
|
|
|
|
payerAllocation := allocation.PayerAllocation
|
|
|
|
|
|
|
|
if allocation.StorageNodeId != peer.ID {
|
|
|
|
return status.Error(codes.Unauthenticated, "only specified storage node can settle allocation")
|
|
|
|
}
|
|
|
|
|
|
|
|
allocationExpiration := time.Unix(payerAllocation.GetExpirationUnixSec(), 0)
|
|
|
|
if allocationExpiration.Before(time.Now()) {
|
|
|
|
s.log.Debug("allocation expired", zap.String("serial", payerAllocation.SerialNumber), zap.Error(err))
|
|
|
|
err := client.Send(&pb.BandwidthSettlementResponse{
|
|
|
|
SerialNumber: payerAllocation.SerialNumber,
|
|
|
|
Status: pb.AgreementsSummary_REJECTED,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return formatError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.verifySignature(ctx, allocation); err != nil {
|
|
|
|
s.log.Debug("signature verification failed", zap.String("serial", payerAllocation.SerialNumber), zap.Error(err))
|
|
|
|
err := client.Send(&pb.BandwidthSettlementResponse{
|
|
|
|
SerialNumber: payerAllocation.SerialNumber,
|
|
|
|
Status: pb.AgreementsSummary_REJECTED,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return formatError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = s.bwdb.SaveOrder(ctx, allocation); err != nil {
|
|
|
|
s.log.Debug("saving order failed", zap.String("serial", payerAllocation.SerialNumber), zap.Error(err))
|
2019-04-23 12:13:57 +01:00
|
|
|
duplicateRequest := pgutil.IsConstraintError(err) || sqliteutil.IsConstraintError(err)
|
2019-03-19 12:56:59 +00:00
|
|
|
if duplicateRequest {
|
|
|
|
err := client.Send(&pb.BandwidthSettlementResponse{
|
|
|
|
SerialNumber: payerAllocation.SerialNumber,
|
|
|
|
Status: pb.AgreementsSummary_REJECTED,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return formatError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Send(&pb.BandwidthSettlementResponse{
|
|
|
|
SerialNumber: payerAllocation.SerialNumber,
|
|
|
|
Status: pb.AgreementsSummary_OK,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return formatError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 21:17:35 +00:00
|
|
|
func (s *Server) verifySignature(ctx context.Context, rba *pb.Order) error {
|
2019-02-07 19:22:49 +00:00
|
|
|
pba := rba.GetPayerAllocation()
|
|
|
|
|
|
|
|
// Get renter's public key from uplink agreement db
|
|
|
|
uplinkInfo, err := s.certdb.GetPublicKey(ctx, pba.UplinkId)
|
|
|
|
if err != nil {
|
2019-02-22 21:17:35 +00:00
|
|
|
return pb.ErrRenter.Wrap(auth.ErrVerify.New("Failed to unmarshal OrderLimit: %+v", err))
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify Renter's (uplink) signature
|
|
|
|
rbad := *rba
|
|
|
|
rbad.SetSignature(nil)
|
|
|
|
rbad.SetCerts(nil)
|
|
|
|
rbadBytes, err := proto.Marshal(&rbad)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("marshalling error: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
if err := pkcrypto.HashAndVerifySignature(uplinkInfo, rbadBytes, rba.GetSignature()); err != nil {
|
|
|
|
return pb.ErrRenter.Wrap(auth.ErrVerify.Wrap(err))
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// verify Payer's (satellite) signature
|
|
|
|
pbad := pba
|
|
|
|
pbad.SetSignature(nil)
|
|
|
|
pbad.SetCerts(nil)
|
|
|
|
pbadBytes, err := proto.Marshal(&pbad)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("marshalling error: %+v", err)
|
|
|
|
}
|
|
|
|
|
2019-02-07 20:39:20 +00:00
|
|
|
if err := pkcrypto.HashAndVerifySignature(s.pkey, pbadBytes, pba.GetSignature()); err != nil {
|
|
|
|
return pb.ErrPayer.Wrap(auth.ErrVerify.Wrap(err))
|
2019-02-07 19:22:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|