-
Notifications
You must be signed in to change notification settings - Fork 336
/
store.go
146 lines (126 loc) · 3.29 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
// 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"
"github.com/ethersphere/bee/pkg/postage"
)
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
putErr error
putErrDelayCnt int
resetCallCount int
}
// Option is a 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{}
for _, o := range opts {
o(bs)
}
return bs
}
// WithChainState will set the initial chainstate 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
}
}
// WithPutErr will set the put error returned by the ChainStore mock. The error
// will be returned on each subsequent call after delayCnt calls to Put have
// been made.
func WithPutErr(err error, delayCnt int) Option {
return func(bs *BatchStore) {
bs.putErr = err
bs.putErrDelayCnt = delayCnt
}
}
// 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--
}
if !bytes.Equal(bs.id, id) {
return nil, errors.New("no such id")
}
return bs.batch, nil
}
// Put mocks the Put method from the BatchStore
func (bs *BatchStore) Put(batch *postage.Batch, newValue *big.Int, newDepth uint8) error {
if bs.putErr != nil {
if bs.putErrDelayCnt == 0 {
return bs.putErr
}
bs.putErrDelayCnt--
}
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.putErr != nil {
if bs.putErrDelayCnt == 0 {
return bs.putErr
}
bs.putErrDelayCnt--
}
bs.cs = cs
return nil
}
func (bs *BatchStore) GetReserveState() *postage.ReserveState {
rs := new(postage.ReserveState)
if bs.rs != nil {
rs.Radius = bs.rs.Radius
rs.Available = bs.rs.Available
rs.Outer = bs.rs.Outer
rs.Inner = bs.rs.Inner
}
return rs
}
func (bs *BatchStore) SetRadiusSetter(r postage.RadiusSetter) {
panic("not implemented")
}
func (bs *BatchStore) Reset() error {
bs.resetCallCount++
return nil
}
func (bs *BatchStore) ResetCalls() int {
return bs.resetCallCount
}