-
Notifications
You must be signed in to change notification settings - Fork 338
/
store.go
242 lines (206 loc) · 5.62 KB
/
store.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
// 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 mock
import (
"bytes"
"errors"
"math/big"
"sync"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/postage/batchstore"
"github.com/ethersphere/bee/pkg/storage"
)
var _ postage.Storer = (*BatchStore)(nil)
// BatchStore is a mock BatchStorer
type BatchStore struct {
rs *postage.ReserveState
cs *postage.ChainState
id []byte
batch *postage.Batch
getErr error
getErrDelayCnt int
updateErr error
updateErrDelayCnt int
resetCallCount int
radiusSetter postage.StorageRadiusSetter
existsFn func([]byte) (bool, error)
mtx sync.Mutex
}
func (bs *BatchStore) SetBatchExpiryHandler(eh postage.BatchExpiryHandler) {}
// Option is an option passed to New.
type Option func(*BatchStore)
// New creates a new mock BatchStore
func New(opts ...Option) *BatchStore {
bs := &BatchStore{}
bs.cs = &postage.ChainState{}
bs.rs = &postage.ReserveState{}
for _, o := range opts {
o(bs)
}
return bs
}
// WithReserveState will set the initial reservestate in the ChainStore mock.
func WithReserveState(rs *postage.ReserveState) Option {
return func(bs *BatchStore) {
bs.rs = rs
}
}
// WithChainState will set the initial chainstate in the ChainStore mock.
func WithChainState(cs *postage.ChainState) Option {
return func(bs *BatchStore) {
bs.cs = cs
}
}
// WithGetErr will set the get error returned by the ChainStore mock. The error
// will be returned on each subsequent call after delayCnt calls to Get have
// been made.
func WithGetErr(err error, delayCnt int) Option {
return func(bs *BatchStore) {
bs.getErr = err
bs.getErrDelayCnt = delayCnt
}
}
// WithUpdateErr will set the put error returned by the ChainStore mock.
// The error will be returned on each subsequent call after delayCnt
// calls to Update have been made.
func WithUpdateErr(err error, delayCnt int) Option {
return func(bs *BatchStore) {
bs.updateErr = err
bs.updateErrDelayCnt = delayCnt
}
}
// WithBatch will set batch to the one provided by user.
// This will be returned in the next Get.
func WithBatch(b *postage.Batch) Option {
return func(bs *BatchStore) {
bs.batch = b
bs.id = b.ID
}
}
func WithExistsFunc(f func([]byte) (bool, error)) Option {
return func(bs *BatchStore) {
bs.existsFn = f
}
}
func WithAcceptAllExistsFunc() Option {
return func(bs *BatchStore) {
bs.existsFn = func(_ []byte) (bool, error) {
return true, nil
}
}
}
// Get mocks the Get method from the BatchStore.
func (bs *BatchStore) Get(id []byte) (*postage.Batch, error) {
if bs.getErr != nil {
if bs.getErrDelayCnt == 0 {
return nil, bs.getErr
}
bs.getErrDelayCnt--
}
exists, err := bs.Exists(id)
if err != nil {
return nil, err
}
if !exists {
return nil, storage.ErrNotFound
}
return bs.batch, nil
}
// Iterate mocks the Iterate method from the BatchStore
func (bs *BatchStore) Iterate(f func(*postage.Batch) (bool, error)) error {
if bs.batch == nil {
return nil
}
_, err := f(bs.batch)
return err
}
// Save mocks the Save method from the BatchStore.
func (bs *BatchStore) Save(batch *postage.Batch) error {
if bs.batch != nil {
return errors.New("batch already taken")
}
bs.batch = batch
bs.id = batch.ID
return nil
}
// Update mocks the Update method from the BatchStore.
func (bs *BatchStore) Update(batch *postage.Batch, newValue *big.Int, newDepth uint8) error {
if bs.batch == nil || !bytes.Equal(batch.ID, bs.id) {
return batchstore.ErrNotFound
}
if bs.updateErr != nil {
if bs.updateErrDelayCnt == 0 {
return bs.updateErr
}
bs.updateErrDelayCnt--
}
bs.batch = batch
batch.Depth = newDepth
batch.Value.Set(newValue)
bs.id = batch.ID
return nil
}
// GetChainState mocks the GetChainState method from the BatchStore
func (bs *BatchStore) GetChainState() *postage.ChainState {
return bs.cs
}
// PutChainState mocks the PutChainState method from the BatchStore
func (bs *BatchStore) PutChainState(cs *postage.ChainState) error {
if bs.updateErr != nil {
if bs.updateErrDelayCnt == 0 {
return bs.updateErr
}
bs.updateErrDelayCnt--
}
bs.cs = cs
return nil
}
func (bs *BatchStore) GetReserveState() *postage.ReserveState {
bs.mtx.Lock()
defer bs.mtx.Unlock()
rs := new(postage.ReserveState)
if bs.rs != nil {
rs.Radius = bs.rs.Radius
rs.StorageRadius = bs.rs.StorageRadius
}
return rs
}
func (bs *BatchStore) SetStorageRadiusSetter(r postage.StorageRadiusSetter) {
bs.radiusSetter = r
}
func (bs *BatchStore) SetStorageRadius(f func(uint8) uint8) error {
bs.mtx.Lock()
defer bs.mtx.Unlock()
bs.rs.StorageRadius = f(bs.rs.StorageRadius)
if bs.radiusSetter != nil {
bs.radiusSetter.SetStorageRadius(bs.rs.StorageRadius)
}
return nil
}
func (bs *BatchStore) Unreserve(_ postage.UnreserveIteratorFn) error {
panic("not implemented")
}
// Exists reports whether batch referenced by the give id exists.
func (bs *BatchStore) Exists(id []byte) (bool, error) {
if bs.existsFn != nil {
return bs.existsFn(id)
}
return bytes.Equal(bs.id, id), nil
}
func (bs *BatchStore) Reset() error {
bs.resetCallCount++
return nil
}
func (bs *BatchStore) ResetCalls() int {
return bs.resetCallCount
}
type MockEventUpdater struct {
inProgress bool
err error
}
func NewNotReady() *MockEventUpdater { return &MockEventUpdater{inProgress: true} }
func NewWithError(err error) *MockEventUpdater { return &MockEventUpdater{inProgress: false, err: err} }
func (s *MockEventUpdater) GetSyncStatus() (isDone bool, err error) {
return !s.inProgress, s.err
}