aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/chunk/chunk.go
blob: c44292bb92ab5415c0b51872ccab23cee4390954 (plain) (blame)
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
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package chunk

import (
    "context"
    "errors"
    "fmt"

    "github.com/ethereum/go-ethereum/common"
)

const (
    DefaultSize   = 4096
    MaxPO         = 16
    AddressLength = 32
)

var (
    ErrChunkNotFound = errors.New("chunk not found")
    ErrChunkInvalid  = errors.New("invalid chunk")
)

type Chunk interface {
    Address() Address
    Data() []byte
}

type chunk struct {
    addr  Address
    sdata []byte
}

func NewChunk(addr Address, data []byte) Chunk {
    return &chunk{
        addr:  addr,
        sdata: data,
    }
}

func (c *chunk) Address() Address {
    return c.addr
}

func (c *chunk) Data() []byte {
    return c.sdata
}

func (self *chunk) String() string {
    return fmt.Sprintf("Address: %v Chunksize: %v", self.addr.Log(), len(self.sdata))
}

type Address []byte

var ZeroAddr = Address(common.Hash{}.Bytes())

func (a Address) Hex() string {
    return fmt.Sprintf("%064x", []byte(a[:]))
}

func (a Address) Log() string {
    if len(a[:]) < 8 {
        return fmt.Sprintf("%x", []byte(a[:]))
    }
    return fmt.Sprintf("%016x", []byte(a[:8]))
}

func (a Address) String() string {
    return fmt.Sprintf("%064x", []byte(a))
}

func (a Address) MarshalJSON() (out []byte, err error) {
    return []byte(`"` + a.String() + `"`), nil
}

func (a *Address) UnmarshalJSON(value []byte) error {
    s := string(value)
    *a = make([]byte, 32)
    h := common.Hex2Bytes(s[1 : len(s)-1])
    copy(*a, h)
    return nil
}

// Proximity returns the proximity order of the MSB distance between x and y
//
// The distance metric MSB(x, y) of two equal length byte sequences x an y is the
// value of the binary integer cast of the x^y, ie., x and y bitwise xor-ed.
// the binary cast is big endian: most significant bit first (=MSB).
//
// Proximity(x, y) is a discrete logarithmic scaling of the MSB distance.
// It is defined as the reverse rank of the integer part of the base 2
// logarithm of the distance.
// It is calculated by counting the number of common leading zeros in the (MSB)
// binary representation of the x^y.
//
// (0 farthest, 255 closest, 256 self)
func Proximity(one, other []byte) (ret int) {
    b := (MaxPO-1)/8 + 1
    if b > len(one) {
        b = len(one)
    }
    m := 8
    for i := 0; i < b; i++ {
        oxo := one[i] ^ other[i]
        for j := 0; j < m; j++ {
            if (oxo>>uint8(7-j))&0x01 != 0 {
                return i*8 + j
            }
        }
    }
    return MaxPO
}

// ModeGet enumerates different Getter modes.
type ModeGet int

func (m ModeGet) String() string {
    switch m {
    case ModeGetRequest:
        return "Request"
    case ModeGetSync:
        return "Sync"
    case ModeGetLookup:
        return "Lookup"
    default:
        return "Unknown"
    }
}

// Getter modes.
const (
    // ModeGetRequest: when accessed for retrieval
    ModeGetRequest ModeGet = iota
    // ModeGetSync: when accessed for syncing or proof of custody request
    ModeGetSync
    // ModeGetLookup: when accessed to lookup a a chunk in feeds or other places
    ModeGetLookup
)

// ModePut enumerates different Putter modes.
type ModePut int

func (m ModePut) String() string {
    switch m {
    case ModePutRequest:
        return "Request"
    case ModePutSync:
        return "Sync"
    case ModePutUpload:
        return "Upload"
    default:
        return "Unknown"
    }
}

// Putter modes.
const (
    // ModePutRequest: when a chunk is received as a result of retrieve request and delivery
    ModePutRequest ModePut = iota
    // ModePutSync: when a chunk is received via syncing
    ModePutSync
    // ModePutUpload: when a chunk is created by local upload
    ModePutUpload
)

// ModeSet enumerates different Setter modes.
type ModeSet int

func (m ModeSet) String() string {
    switch m {
    case ModeSetAccess:
        return "Access"
    case ModeSetSync:
        return "Sync"
    case ModeSetRemove:
        return "Remove"
    default:
        return "Unknown"
    }
}

// Setter modes.
const (
    // ModeSetAccess: when an update request is received for a chunk or chunk is retrieved for delivery
    ModeSetAccess ModeSet = iota
    // ModeSetSync: when a chunk is added to a pull sync batch or when a push sync receipt is received
    ModeSetSync
    // ModeSetRemove: when a chunk is removed
    ModeSetRemove
)

// Descriptor holds information required for Pull syncing. This struct
// is provided by subscribing to pull index.
type Descriptor struct {
    Address Address
    BinID   uint64
}

func (d *Descriptor) String() string {
    if d == nil {
        return ""
    }
    return fmt.Sprintf("%s bin id %v", d.Address.Hex(), d.BinID)
}

type Store interface {
    Get(ctx context.Context, mode ModeGet, addr Address) (ch Chunk, err error)
    Put(ctx context.Context, mode ModePut, ch Chunk) (exists bool, err error)
    Has(ctx context.Context, addr Address) (yes bool, err error)
    Set(ctx context.Context, mode ModeSet, addr Address) (err error)
    LastPullSubscriptionBinID(bin uint8) (id uint64, err error)
    SubscribePull(ctx context.Context, bin uint8, since, until uint64) (c <-chan Descriptor, stop func())
    Close() (err error)
}

// Validator validates a chunk.
type Validator interface {
    Validate(ch Chunk) bool
}

// ValidatorStore encapsulates Store by decorting the Put method
// with validators check.
type ValidatorStore struct {
    Store
    validators []Validator
}

// NewValidatorStore returns a new ValidatorStore which uses
// provided validators to validate chunks on Put.
func NewValidatorStore(store Store, validators ...Validator) (s *ValidatorStore) {
    return &ValidatorStore{
        Store:      store,
        validators: validators,
    }
}

// Put overrides Store put method with validators check. If one of the validators
// return true, the chunk is considered valid and Store Put method is called.
// If all validators return false, ErrChunkInvalid is returned.
func (s *ValidatorStore) Put(ctx context.Context, mode ModePut, ch Chunk) (exists bool, err error) {
    for _, v := range s.validators {
        if v.Validate(ch) {
            return s.Store.Put(ctx, mode, ch)
        }
    }
    return false, ErrChunkInvalid
}