-
Notifications
You must be signed in to change notification settings - Fork 338
/
swarm.go
294 lines (250 loc) · 7.03 KB
/
swarm.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package swarm contains most basic and general Swarm concepts.
package swarm
import (
"bytes"
"encoding"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
)
const (
StampIndexSize = 8 // TODO: use this size in related code.
StampTimestampSize = 8 // TODO: use this size in related code.
SpanSize = 8
SectionSize = 32
Branches = 128
EncryptedBranches = Branches / 2
BmtBranches = 128
ChunkSize = SectionSize * Branches
HashSize = 32
MaxPO uint8 = 31
ExtendedPO uint8 = MaxPO + 5
MaxBins = MaxPO + 1
ChunkWithSpanSize = ChunkSize + SpanSize
SocSignatureSize = 65
SocMinChunkSize = HashSize + SocSignatureSize + SpanSize
SocMaxChunkSize = SocMinChunkSize + ChunkSize
)
var (
ErrInvalidChunk = errors.New("invalid chunk")
)
var (
// EmptyAddress is the address that is all zeroes.
EmptyAddress = NewAddress(make([]byte, HashSize))
// ZeroAddress is the address that has no value.
ZeroAddress = NewAddress(nil)
)
// Address represents an address in Swarm metric space of
// Node and Chunk addresses.
type Address struct {
b []byte
}
// NewAddress constructs Address from a byte slice.
func NewAddress(b []byte) Address {
return Address{b: b}
}
// ParseHexAddress returns an Address from a hex-encoded string representation.
func ParseHexAddress(s string) (a Address, err error) {
b, err := hex.DecodeString(s)
if err != nil {
return a, err
}
return NewAddress(b), nil
}
// MustParseHexAddress returns an Address from a hex-encoded string
// representation, and panics if there is a parse error.
func MustParseHexAddress(s string) Address {
a, err := ParseHexAddress(s)
if err != nil {
panic(err)
}
return a
}
// String returns a hex-encoded representation of the Address.
func (a Address) String() string {
return hex.EncodeToString(a.b)
}
// Equal returns true if two addresses are identical.
func (a Address) Equal(b Address) bool {
return bytes.Equal(a.b, b.b)
}
// MemberOf returns true if the address is a member of the
// provided set.
func (a Address) MemberOf(addrs []Address) bool {
return ContainsAddress(addrs, a)
}
// IsZero returns true if the Address is not set to any value.
func (a Address) IsZero() bool {
return a.Equal(ZeroAddress)
}
// IsEmpty returns true if the Address is all zeroes.
func (a Address) IsEmpty() bool {
return a.Equal(EmptyAddress)
}
// IsValidLength returns true if the Address is of valid length.
func (a Address) IsValidLength() bool {
return len(a.b) == HashSize
}
// IsValidNonEmpty returns true if the Address has valid length and is not empty.
func (a Address) IsValidNonEmpty() bool {
return a.IsValidLength() && !a.IsEmpty()
}
// Bytes returns bytes representation of the Address.
func (a Address) Bytes() []byte {
return a.b
}
// ByteString returns raw Address string without encoding.
func (a Address) ByteString() string {
return string(a.Bytes())
}
// UnmarshalJSON sets Address to a value from JSON-encoded representation.
func (a *Address) UnmarshalJSON(b []byte) (err error) {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
*a, err = ParseHexAddress(s)
return err
}
// MarshalJSON returns JSON-encoded representation of Address.
func (a Address) MarshalJSON() ([]byte, error) {
return json.Marshal(a.String())
}
// Closer returns if x is closer to a than y
func (x Address) Closer(a Address, y Address) (bool, error) {
cmp, err := DistanceCmp(a, x, y)
return cmp == 1, err
}
// Clone returns a new swarm address which is a copy of this one.
func (a Address) Clone() Address {
if a.b == nil {
return Address{}
}
return Address{b: append(make([]byte, 0, len(a.b)), a.Bytes()...)}
}
// Compare returns an integer comparing two addresses lexicographically.
// The result will be 0 if a == b, -1 if a < b, and +1 if a > b.
func (a Address) Compare(b Address) int {
return bytes.Compare(a.b, b.b)
}
// AddressIterFunc is a callback on every address that is found by the iterator.
type AddressIterFunc func(address Address) error
type Chunk interface {
// Address returns the chunk address.
Address() Address
// Data returns the chunk data.
Data() []byte
// TagID returns the tag ID for this chunk.
TagID() uint32
// WithTagID attaches the tag ID to the chunk.
WithTagID(t uint32) Chunk
// Stamp returns the postage stamp associated with this chunk.
Stamp() Stamp
// WithStamp attaches a postage stamp to the chunk.
WithStamp(Stamp) Chunk
// Radius is the PO above which the batch is preserved.
Radius() uint8
// Depth returns the batch depth of the stamp - allowed batch size = 2^{depth}.
Depth() uint8
// BucketDepth returns the bucket depth of the batch of the stamp - always < depth.
BucketDepth() uint8
// Immutable returns whether the batch is immutable
Immutable() bool
// WithBatch attaches batch parameters to the chunk.
WithBatch(radius, depth, bucketDepth uint8, immutable bool) Chunk
// Equal checks if the chunk is equal to another.
Equal(Chunk) bool
}
// ChunkType indicates different categories of chunks.
type ChunkType uint8
// String implements Stringer interface.
func (ct ChunkType) String() string {
switch ct {
case ChunkTypeContentAddressed:
return "CAC"
case ChunkTypeSingleOwner:
return "SOC"
default:
return "unspecified"
}
}
const (
ChunkTypeUnspecified ChunkType = iota
ChunkTypeContentAddressed
ChunkTypeSingleOwner
)
// Stamp interface for postage.Stamp to avoid circular dependency
type Stamp interface {
BatchID() []byte
Index() []byte
Sig() []byte
Timestamp() []byte
Clone() Stamp
encoding.BinaryMarshaler
encoding.BinaryUnmarshaler
}
type chunk struct {
addr Address
sdata []byte
tagID uint32
stamp Stamp
radius uint8
depth uint8
bucketDepth uint8
immutable bool
}
func NewChunk(addr Address, data []byte) Chunk {
return &chunk{
addr: addr,
sdata: data,
}
}
func (c *chunk) WithTagID(t uint32) Chunk {
c.tagID = t
return c
}
func (c *chunk) WithStamp(stamp Stamp) Chunk {
c.stamp = stamp
return c
}
func (c *chunk) WithBatch(radius, depth, bucketDepth uint8, immutable bool) Chunk {
c.radius = radius
c.depth = depth
c.bucketDepth = bucketDepth
c.immutable = immutable
return c
}
func (c *chunk) Address() Address {
return c.addr
}
func (c *chunk) Data() []byte {
return c.sdata
}
func (c *chunk) TagID() uint32 {
return c.tagID
}
func (c *chunk) Stamp() Stamp {
return c.stamp
}
func (c *chunk) Radius() uint8 {
return c.radius
}
func (c *chunk) Depth() uint8 {
return c.depth
}
func (c *chunk) BucketDepth() uint8 {
return c.bucketDepth
}
func (c *chunk) Immutable() bool {
return c.immutable
}
func (c *chunk) String() string {
return fmt.Sprintf("Address: %v Chunksize: %v", c.addr.String(), len(c.sdata))
}
func (c *chunk) Equal(cp Chunk) bool {
return c.Address().Equal(cp.Address()) && bytes.Equal(c.Data(), cp.Data())
}