-
Notifications
You must be signed in to change notification settings - Fork 397
/
bandwidth_utils.go
95 lines (80 loc) · 2.55 KB
/
bandwidth_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// 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"
)
//OrderLimit aliases PayerBandwidthAllocation
type OrderLimit = PayerBandwidthAllocation
//Order aliases RenterBandwidthAllocation
type Order = RenterBandwidthAllocation
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 *OrderLimit) SetCerts(certs [][]byte) {
m.Certs = certs
}
// SetSignature updates the signature field, completing the auth.SignedMsg interface
func (m *OrderLimit) SetSignature(signature []byte) {
m.Signature = signature
}
// SetCerts updates the certs field, completing the auth.SignedMsg interface
func (m *Order) SetCerts(certs [][]byte) {
m.Certs = certs
}
// SetSignature updates the signature field, completing the auth.SignedMsg interface
func (m *Order) SetSignature(signature []byte) {
m.Signature = signature
}
// SetCerts updates the certs field, completing the auth.SignedMsg interface
func (m *SignedHash) SetCerts(certs [][]byte) {
m.Certs = certs
}
// SetSignature updates the signature field, completing the auth.SignedMsg interface
func (m *SignedHash) SetSignature(signature []byte) {
m.Signature = signature
}
// Clone creates a deep copy of PayerBandwidthAllocation
func (m *OrderLimit) Clone() (pba OrderLimit) {
pba = OrderLimit{
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
}