-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathswarm.go
186 lines (153 loc) · 3.74 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
// 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/hex"
"encoding/json"
"fmt"
"golang.org/x/crypto/sha3"
)
const (
SpanSize = 8
SectionSize = 32
Branches = 128
ChunkSize = SectionSize * Branches
HashSize = 32
MaxPO uint8 = 15
MaxBins = MaxPO + 1
ChunkWithSpanSize = ChunkSize + SpanSize
)
var (
NewHasher = sha3.NewLegacyKeccak256
)
// 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)
}
// IsZero returns true if the Address is not set to any value.
func (a Address) IsZero() bool {
return a.Equal(ZeroAddress)
}
// 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())
}
// ZeroAddress is the address that has no value.
var ZeroAddress = NewAddress(nil)
type Chunk interface {
Address() Address
Data() []byte
PinCounter() uint64
WithPinCounter(p uint64) Chunk
TagID() uint32
WithTagID(t uint32) Chunk
Equal(Chunk) bool
}
type chunk struct {
addr Address
sdata []byte
pinCounter uint64
tagID uint32
}
func NewChunk(addr Address, data []byte) Chunk {
return &chunk{
addr: addr,
sdata: data,
}
}
func (c *chunk) WithPinCounter(p uint64) Chunk {
c.pinCounter = p
return c
}
func (c *chunk) WithTagID(t uint32) Chunk {
c.tagID = t
return c
}
func (c *chunk) Address() Address {
return c.addr
}
func (c *chunk) Data() []byte {
return c.sdata
}
func (c *chunk) PinCounter() uint64 {
return c.pinCounter
}
func (c *chunk) TagID() uint32 {
return c.tagID
}
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())
}
type Validator interface {
Validate(ch Chunk) (valid bool)
}
type chunkValidator struct {
set []Validator
Validator
}
func NewChunkValidator(v ...Validator) Validator {
return &chunkValidator{
set: v,
}
}
func (c *chunkValidator) Validate(ch Chunk) bool {
for _, v := range c.set {
if v.Validate(ch) {
return true
}
}
return false
}