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"
|
2018-12-07 09:59:31 +00:00
|
|
|
"time"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
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-01-23 19:58:44 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/auth"
|
|
|
|
"storj.io/storj/pkg/identity"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
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-01-02 17:53:27 +00:00
|
|
|
// DB stores bandwidth agreements.
|
2018-12-07 09:59:31 +00:00
|
|
|
type DB interface {
|
2019-01-02 17:53:27 +00:00
|
|
|
// CreateAgreement adds a new bandwidth agreement.
|
2019-01-09 15:02:03 +00:00
|
|
|
CreateAgreement(context.Context, string, Agreement) error
|
2019-01-02 17:53:27 +00:00
|
|
|
// GetAgreements gets all bandwidth agreements.
|
2018-12-07 09:59:31 +00:00
|
|
|
GetAgreements(context.Context) ([]Agreement, error)
|
2019-01-02 17:53:27 +00:00
|
|
|
// GetAgreementsSince gets all bandwidth agreements since specific time.
|
2018-12-07 09:59:31 +00:00
|
|
|
GetAgreementsSince(context.Context, time.Time) ([]Agreement, error)
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// Server is an implementation of the pb.BandwidthServer interface
|
|
|
|
type Server struct {
|
2018-12-07 09:59:31 +00:00
|
|
|
db DB
|
2019-01-25 18:05:21 +00:00
|
|
|
NodeID storj.NodeID
|
2018-11-12 21:59:30 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
// Agreement is a struct that contains a bandwidth agreement and the associated signature
|
|
|
|
type Agreement struct {
|
|
|
|
Agreement []byte
|
|
|
|
Signature []byte
|
2018-12-07 09:59:31 +00:00
|
|
|
CreatedAt time.Time
|
2019-01-10 18:30:55 +00:00
|
|
|
ExpiresAt time.Time
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
// NewServer creates instance of Server
|
2019-01-25 18:05:21 +00:00
|
|
|
func NewServer(db DB, logger *zap.Logger, nodeID storj.NodeID) *Server {
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: reorder arguments, rename logger -> log
|
2019-01-25 18:05:21 +00:00
|
|
|
return &Server{db: db, logger: logger, 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-01-25 18:05:21 +00:00
|
|
|
func (s *Server) BandwidthAgreements(ctx context.Context, rba *pb.RenterBandwidthAllocation) (reply *pb.AgreementsSummary, err error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-15 19:06:09 +00:00
|
|
|
s.logger.Debug("Received Agreement...")
|
|
|
|
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-25 18:05:21 +00:00
|
|
|
rbad, pba, pbad, err := rba.Unpack()
|
|
|
|
if err != nil {
|
|
|
|
return reply, err
|
2019-01-09 15:02:03 +00:00
|
|
|
}
|
2019-01-25 18:05:21 +00:00
|
|
|
//verify message content
|
|
|
|
pi, err := identity.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil || rbad.StorageNodeId != pi.ID {
|
|
|
|
return reply, auth.BadID.New("Storage Node ID: %s vs %s", rbad.StorageNodeId, pi.ID)
|
2019-01-09 15:02:03 +00:00
|
|
|
}
|
2019-01-25 18:05:21 +00:00
|
|
|
//todo: use whitelist for uplinks?
|
|
|
|
if pbad.SatelliteId != s.NodeID {
|
|
|
|
return reply, pb.Payer.New("Satellite ID: %s vs %s", pbad.SatelliteId, s.NodeID)
|
2019-01-11 03:31:47 +00:00
|
|
|
}
|
2019-01-25 18:05:21 +00:00
|
|
|
serialNum := pbad.GetSerialNumber() + rbad.StorageNodeId.String()
|
2019-01-09 15:02:03 +00:00
|
|
|
if len(pbad.SerialNumber) == 0 {
|
2019-01-25 18:05:21 +00:00
|
|
|
return reply, pb.Payer.Wrap(pb.Missing.New("Serial"))
|
2019-01-09 15:02:03 +00:00
|
|
|
}
|
2019-01-10 18:30:55 +00:00
|
|
|
exp := time.Unix(pbad.GetExpirationUnixSec(), 0).UTC()
|
|
|
|
if exp.Before(time.Now().UTC()) {
|
2019-01-25 18:05:21 +00:00
|
|
|
return reply, pb.Payer.Wrap(auth.Expired.New("%v vs %v", exp, time.Now().UTC()))
|
2019-01-10 18:30:55 +00:00
|
|
|
}
|
2019-01-25 18:05:21 +00:00
|
|
|
//verify message crypto
|
|
|
|
if err := auth.VerifyMsg(rba, pbad.UplinkId); err != nil {
|
|
|
|
return reply, pb.Renter.Wrap(err)
|
|
|
|
}
|
|
|
|
if err := auth.VerifyMsg(pba, pbad.SatelliteId); err != nil {
|
|
|
|
return reply, pb.Payer.Wrap(err)
|
|
|
|
}
|
|
|
|
//save and return rersults
|
2019-01-09 15:02:03 +00:00
|
|
|
err = s.db.CreateAgreement(ctx, serialNum, Agreement{
|
2019-01-25 18:05:21 +00:00
|
|
|
Signature: rba.GetSignature(),
|
|
|
|
Agreement: rba.GetData(),
|
2019-01-10 18:30:55 +00:00
|
|
|
ExpiresAt: exp,
|
2018-12-07 09:59:31 +00:00
|
|
|
})
|
2018-11-15 19:06:09 +00:00
|
|
|
if err != nil {
|
2019-01-11 18:15:49 +00:00
|
|
|
//todo: better classify transport errors (AgreementsSummary_FAIL) vs logical (AgreementsSummary_REJECTED)
|
2019-01-25 18:05:21 +00:00
|
|
|
return reply, pb.Payer.Wrap(auth.Serial.Wrap(err))
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2018-11-15 19:06:09 +00:00
|
|
|
reply.Status = pb.AgreementsSummary_OK
|
|
|
|
s.logger.Debug("Stored Agreement...")
|
|
|
|
return reply, nil
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|