-
Notifications
You must be signed in to change notification settings - Fork 0
/
stampissuer.go
178 lines (155 loc) · 5.83 KB
/
stampissuer.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package voucher
import (
"encoding/binary"
"math/big"
"sync"
"github.com/redesblock/mop/core/cluster"
"github.com/vmihailenco/msgpack/v5"
)
// stampIssuerData groups related StampIssuer data.
// The data are factored out in order to make
// serialization/deserialization easier and at the same
// time not to export the fields outside of the package.
type stampIssuerData struct {
Label string `msgpack:"label"` // Label to identify the batch period/importance.
KeyID string `msgpack:"keyID"` // Owner identity.
BatchID []byte `msgpack:"batchID"` // The batch stamps are issued from.
BatchAmount *big.Int `msgpack:"batchAmount"` // Amount paid for the batch.
BatchDepth uint8 `msgpack:"batchDepth"` // Batch depth: batch size = 2^{depth}.
BucketDepth uint8 `msgpack:"bucketDepth"` // Bucket depth: the depth of collision Buckets uniformity.
Buckets []uint32 `msgpack:"buckets"` // Collision Buckets: counts per neighbourhoods (limited to 2^{batchdepth-bucketdepth}).
MaxBucketCount uint32 `msgpack:"maxBucketCount"` // the count of the fullest bucket
BlockNumber uint64 `msgpack:"blockNumber"` // BlockNumber when this batch was created
ImmutableFlag bool `msgpack:"immutableFlag"` // Specifies immutability of the created batch.
Expired bool `msgpack:"expired"` // Specifies the expiry of the batch
}
// StampIssuer is a local extension of a batch issuing stamps for uploads.
// A StampIssuer instance extends a batch with bucket collision tracking
// embedded in multiple Stampers, can be used concurrently.
type StampIssuer struct {
bucketMu sync.Mutex
data stampIssuerData
}
// NewStampIssuer constructs a StampIssuer as an extension of a batch for local
// upload.
//
// BucketDepth must always be smaller than batchDepth otherwise inc() panics.
func NewStampIssuer(label, keyID string, batchID []byte, batchAmount *big.Int, batchDepth, bucketDepth uint8, blockNumber uint64, immutableFlag bool) *StampIssuer {
return &StampIssuer{
data: stampIssuerData{
Label: label,
KeyID: keyID,
BatchID: batchID,
BatchAmount: batchAmount,
BatchDepth: batchDepth,
BucketDepth: bucketDepth,
Buckets: make([]uint32, 1<<bucketDepth),
BlockNumber: blockNumber,
ImmutableFlag: immutableFlag,
},
}
}
// inc increments the count in the correct collision bucket for a newly stamped
// chunk with address addr.
func (si *StampIssuer) inc(addr cluster.Address) ([]byte, error) {
si.bucketMu.Lock()
defer si.bucketMu.Unlock()
b := toBucket(si.BucketDepth(), addr)
bucketCount := si.data.Buckets[b]
if bucketCount == 1<<(si.Depth()-si.BucketDepth()) {
return nil, ErrBucketFull
}
si.data.Buckets[b]++
if si.data.Buckets[b] > si.data.MaxBucketCount {
si.data.MaxBucketCount = si.data.Buckets[b]
}
return indexToBytes(b, bucketCount), nil
}
// toBucket calculates the index of the collision bucket for a cluster address
// bucket index := collision bucket depth number of bits as bigendian uint32
func toBucket(depth uint8, addr cluster.Address) uint32 {
i := binary.BigEndian.Uint32(addr.Bytes()[:4])
return i >> (32 - depth)
}
// indexToBytes creates an uint64 index from
// - bucket index (neighbourhood index, uint32 <2^depth, bytes 2-4)
// - and the within-bucket index (uint32 <2^(batchdepth-bucketdepth), bytes 5-8)
func indexToBytes(bucket, index uint32) []byte {
buf := make([]byte, IndexSize)
binary.BigEndian.PutUint32(buf, bucket)
binary.BigEndian.PutUint32(buf[4:], index)
return buf
}
func bytesToIndex(buf []byte) (bucket, index uint32) {
index64 := binary.BigEndian.Uint64(buf)
bucket = uint32(index64 >> 32)
index = uint32(index64)
return bucket, index
}
// Label returns the label of the issuer.
func (si *StampIssuer) Label() string {
return si.data.Label
}
// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (si *StampIssuer) MarshalBinary() ([]byte, error) {
return msgpack.Marshal(si.data)
}
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (si *StampIssuer) UnmarshalBinary(data []byte) error {
return msgpack.Unmarshal(data, &si.data)
}
// Utilization returns the batch utilization in the form of
// an integer between 0 and 4294967295. Batch fullness can be
// calculated with: max_bucket_value / 2 ^ (batch_depth - bucket_depth)
func (si *StampIssuer) Utilization() uint32 {
si.bucketMu.Lock()
defer si.bucketMu.Unlock()
return si.data.MaxBucketCount
}
// ID returns the BatchID for this batch.
func (si *StampIssuer) ID() []byte {
id := make([]byte, len(si.data.BatchID))
copy(id, si.data.BatchID)
return id
}
// Depth represent issued batch depth.
func (si *StampIssuer) Depth() uint8 {
return si.data.BatchDepth
}
// Amount represent issued batch amount paid.
func (si *StampIssuer) Amount() *big.Int {
return si.data.BatchAmount
}
// BucketDepth the depth of collision Buckets uniformity.
func (si *StampIssuer) BucketDepth() uint8 {
return si.data.BucketDepth
}
// BucketUpperBound returns the maximum number of collisions
// possible in a bucket given the batch's depth and bucket
// depth.
func (si *StampIssuer) BucketUpperBound() uint32 {
return 1 << (si.Depth() - si.BucketDepth())
}
// BlockNumber when this batch was created.
func (si *StampIssuer) BlockNumber() uint64 {
return si.data.BlockNumber
}
// ImmutableFlag immutability of the created batch.
func (si *StampIssuer) ImmutableFlag() bool {
return si.data.ImmutableFlag
}
func (si *StampIssuer) Buckets() []uint32 {
si.bucketMu.Lock()
b := make([]uint32, len(si.data.Buckets))
copy(b, si.data.Buckets)
si.bucketMu.Unlock()
return b
}
// Expired returns the expired property of stamp
func (si *StampIssuer) Expired() bool {
return si.data.Expired
}
// SetExpired is setter for Expired property
func (si *StampIssuer) SetExpired(e bool) {
si.data.Expired = e
}