storj/pkg/pb/bandwidth_utils.go
Natalie Villasana c3d3f41d30 removes some SignedMessage use (#1258)
Removes most instances of pb.SignedMessage (there's more to take out but they shouldn't hurt anyone as is).

There used to be places in psserver where a PieceID was hmac'd with the SatelliteID, which was gotten from a SignedMessage. This PR makes it so some functions access the SatelliteID from the Payer Bandwidth Allocation instead.

This requires passing a SatelliteID into psserver functions where they weren't before, so the following proto messages have been changed:

 * PieceId - satellite_id field added
   This is so the psserver.Piece function has access to the SatelliteID when it needs to get the namespaced pieceID.
   This proto message should probably be renamed to PieceRequest, or a new PieceRequest message should be created so this isn't misnamed.

 * PieceDelete - satellite_id field added
   This is so the psserver.Delete function has access to the SatelliteID when receiving a request to Delete.
2019-02-19 23:36:08 -06:00

80 lines
2.2 KiB
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package pb
import (
"bytes"
reflect "reflect"
proto "github.com/gogo/protobuf/proto"
"github.com/zeebo/errs"
)
var (
//ErrRenter wraps errors related to renter bandwidth allocations
ErrRenter = errs.Class("Renter agreement")
//ErrPayer wraps errors related to payer bandwidth allocations
ErrPayer = errs.Class("Payer agreement")
)
//Equal compares two Protobuf messages via serialization
func Equal(msg1, msg2 proto.Message) bool {
//reflect.DeepEqual and proto.Equal don't seem work in all cases
//todo: see how slow this is compared to custom equality checks
if msg1 == nil {
return msg2 == nil
}
if reflect.TypeOf(msg1) != reflect.TypeOf(msg2) {
return false
}
msg1Bytes, err := proto.Marshal(msg1)
if err != nil {
return false
}
msg2Bytes, err := proto.Marshal(msg2)
if err != nil {
return false
}
return bytes.Compare(msg1Bytes, msg2Bytes) == 0
}
//SetCerts updates the certs field, completing the auth.SignedMsg interface
func (m *PayerBandwidthAllocation) SetCerts(certs [][]byte) {
m.Certs = certs
}
//SetSignature updates the signature field, completing the auth.SignedMsg interface
func (m *PayerBandwidthAllocation) SetSignature(signature []byte) {
m.Signature = signature
}
//SetCerts updates the certs field, completing the auth.SignedMsg interface
func (m *RenterBandwidthAllocation) SetCerts(certs [][]byte) {
m.Certs = certs
}
//SetSignature updates the signature field, completing the auth.SignedMsg interface
func (m *RenterBandwidthAllocation) SetSignature(signature []byte) {
m.Signature = signature
}
// Clone creates a deep copy of PayerBandwidthAllocation
func (m *PayerBandwidthAllocation) Clone() (pba PayerBandwidthAllocation) {
pba = PayerBandwidthAllocation{
SatelliteId: m.SatelliteId,
UplinkId: m.UplinkId,
MaxSize: m.MaxSize,
ExpirationUnixSec: m.ExpirationUnixSec,
SerialNumber: m.SerialNumber,
Action: m.Action,
CreatedUnixSec: m.CreatedUnixSec,
}
pba.Certs = make([][]byte, len(m.Certs))
copy(pba.Certs, m.Certs)
pba.Signature = make([]byte, len(m.Signature))
copy(pba.Signature, m.Signature)
return pba
}